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.
28 * Copyright (c) 2018, Joyent, Inc.
32 * Kernel's linker/loader
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/sysmacros.h>
38 #include <sys/systm.h>
41 #include <sys/reboot.h>
42 #include <sys/bootconf.h>
43 #include <sys/debug.h>
46 #include <sys/vnode.h>
50 #include <vm/seg_kp.h>
51 #include <vm/seg_kmem.h>
53 #include <sys/elf_notes.h>
54 #include <sys/vmsystm.h>
56 #include <sys/atomic.h>
61 #include <sys/ksyms.h>
63 #include <sys/modctl.h>
64 #include <sys/varargs.h>
65 #include <sys/kstat.h>
66 #include <sys/kobj_impl.h>
67 #include <sys/fs/decomp.h>
68 #include <sys/callb.h>
69 #include <sys/cmn_err.h>
70 #include <sys/tnf_probe.h>
73 #include <krtld/reloc.h>
74 #include <krtld/kobj_kdi.h>
76 #include <sys/crypto/elfsign.h>
79 #include <sys/bootvfs.h>
83 * do_symbols() error codes
85 #define DOSYM_UNDEF -1 /* undefined symbol */
86 #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */
89 static void synthetic_bootaux(char *, val_t
*);
92 static struct module
*load_exec(val_t
*, char *);
93 static void load_linker(val_t
*);
94 static struct modctl
*add_primary(const char *filename
, int);
95 static int bind_primary(val_t
*, int);
96 static int load_primary(struct module
*, int);
97 static int load_kmdb(val_t
*);
98 static int get_progbits(struct module
*, struct _buf
*);
99 static int get_syms(struct module
*, struct _buf
*);
100 static int get_ctf(struct module
*, struct _buf
*);
101 static void get_signature(struct module
*, struct _buf
*);
102 static int do_common(struct module
*);
103 static void add_dependent(struct module
*, struct module
*);
104 static int do_dependents(struct modctl
*, char *, size_t);
105 static int do_symbols(struct module
*, Elf64_Addr
);
106 static void module_assign(struct modctl
*, struct module
*);
107 static void free_module_data(struct module
*);
108 static char *depends_on(struct module
*);
109 static char *getmodpath(const char *);
110 static char *basename(char *);
111 static void attr_val(val_t
*);
112 static char *find_libmacro(char *);
113 static char *expand_libmacro(char *, char *, char *);
114 static int read_bootflags(void);
115 static int kobj_comp_setup(struct _buf
*, struct compinfo
*);
116 static int kobj_uncomp_blk(struct _buf
*, caddr_t
, uint_t
);
117 static int kobj_read_blks(struct _buf
*, caddr_t
, uint_t
, uint_t
);
118 static int kobj_boot_open(char *, int);
119 static int kobj_boot_close(int);
120 static int kobj_boot_seek(int, off_t
, off_t
);
121 static int kobj_boot_read(int, caddr_t
, size_t);
122 static int kobj_boot_fstat(int, struct bootstat
*);
123 static int kobj_boot_compinfo(int, struct compinfo
*);
124 extern int kobj_boot_mountroot(void);
126 static Sym
*lookup_one(struct module
*, const char *);
127 static void sym_insert(struct module
*, char *, symid_t
);
128 static Sym
*sym_lookup(struct module
*, Sym
*);
130 static struct kobjopen_tctl
*kobjopen_alloc(char *filename
);
131 static void kobjopen_free(struct kobjopen_tctl
*ltp
);
132 static void kobjopen_thread(struct kobjopen_tctl
*ltp
);
133 static int kobj_is_compressed(intptr_t);
135 extern int kcopy(const void *, void *, size_t);
136 extern int elf_mach_ok(Ehdr
*);
137 extern int alloc_gottable(struct module
*, caddr_t
*, caddr_t
*);
139 static void tnf_unsplice_probes(uint_t
, struct modctl
*);
140 extern tnf_probe_control_t
*__tnf_probe_list_head
;
141 extern tnf_tag_data_t
*__tnf_tag_list_head
;
143 extern int modrootloaded
;
144 extern int swaploaded
;
145 extern int bop_io_quiesced
;
146 extern int last_module_id
;
148 extern char stubs_base
[];
149 extern char stubs_end
[];
153 * Values that can be or'd in to kobj_debug and their effects:
155 * D_DEBUG - misc. debugging information.
156 * D_SYMBOLS - list symbols and their values as they are entered
157 * into the hash table
158 * D_RELOCATIONS - display relocation processing information
159 * D_LOADING - display information about each module as it
164 #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \
165 (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s))
167 #define KOBJ_MARK(s) /* discard */
170 #define MODPATH_PROPNAME "module-path"
172 #define _moddebug get_weakish_int(&moddebug)
173 #define _modrootloaded get_weakish_int(&modrootloaded)
174 #define _swaploaded get_weakish_int(&swaploaded)
175 #define _ioquiesced get_weakish_int(&bop_io_quiesced)
177 #define mod(X) (struct module *)((X)->modl_modp->mod_mp)
179 void *romp
; /* rom vector (opaque to us) */
180 struct bootops
*ops
; /* bootops vector */
181 void *dbvec
; /* debug vector */
184 * kobjopen thread control structure
186 struct kobjopen_tctl
{
188 char *name
; /* name of file */
189 struct vnode
*vp
; /* vnode return from vn_open() */
190 int Errno
; /* error return from vnopen */
194 * Structure for defining dynamically expandable library macros
197 struct lib_macro_info
{
198 char *lmi_list
; /* ptr to list of possible choices */
199 char *lmi_macroname
; /* pointer to macro name */
200 ushort_t lmi_ba_index
; /* index into bootaux vector */
201 ushort_t lmi_macrolen
; /* macro length */
203 { NULL
, "CPU", BA_CPU
, 0 },
204 { NULL
, "MMU", BA_MMU
, 0 }
207 #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info)
209 char *boot_cpu_compatible_list
; /* make $CPU available */
211 char *kobj_module_path
; /* module search path */
212 vmem_t
*text_arena
; /* module text arena */
213 static vmem_t
*data_arena
; /* module data & bss arena */
214 static vmem_t
*ctf_arena
; /* CTF debug data arena */
215 static struct modctl
*kobj_modules
= NULL
; /* modules loaded */
216 int kobj_mmu_pagesize
; /* system pagesize */
217 static int lg_pagesize
; /* "large" pagesize */
218 static int kobj_last_module_id
= 0; /* id assignment */
219 static kmutex_t kobj_lock
; /* protects mach memory list */
222 * The following functions have been implemented by the kernel.
223 * However, many 3rd party drivers provide their own implementations
224 * of these functions. When such drivers are loaded, messages
225 * indicating that these symbols have been multiply defined will be
226 * emitted to the console. To avoid alarming customers for no good
227 * reason, we simply suppress such warnings for the following set of
230 static char *suppress_sym_list
[] =
246 NULL
/* This entry must exist */
249 /* indexed by KOBJ_NOTIFY_* */
250 static kobj_notify_list_t
*kobj_notifiers
[KOBJ_NOTIFY_MAX
+ 1];
253 * TNF probe management globals
255 tnf_probe_control_t
*__tnf_probe_list_head
= NULL
;
256 tnf_tag_data_t
*__tnf_tag_list_head
= NULL
;
257 int tnf_changed_probe_list
= 0;
260 * Prefix for statically defined tracing (SDT) DTrace probes.
262 const char *sdt_prefix
= "__dtrace_probe_";
265 * Beginning and end of the kernel's dynamic text/data segments.
267 static caddr_t _text
;
268 static caddr_t _etext
;
269 static caddr_t _data
;
272 * The sparc linker doesn't create a memory location
273 * for a variable named _edata, so _edata can only be
274 * referred to, not modified. krtld needs a static
275 * variable to modify it - within krtld, of course -
276 * outside of krtld, e_data is used in all kernels.
278 extern caddr_t _edata
;
280 Addr dynseg
= 0; /* load address of "dynamic" segment */
281 size_t dynsize
; /* "dynamic" segment size */
284 int standalone
= 1; /* an unwholey kernel? */
285 int use_iflush
; /* iflush after relocations */
288 * _kobj_printf() and _vkobj_printf()
290 * Common printf function pointer. Can handle only one conversion
291 * specification in the format string. Some of the functions invoked
292 * through this function pointer cannot handle more that one conversion
293 * specification in the format string.
295 void (*_kobj_printf
)(void *, const char *, ...); /* printf routine */
296 void (*_vkobj_printf
)(void *, const char *, va_list); /* vprintf routine */
299 * Standalone function pointers for use within krtld.
300 * Many platforms implement optimized platmod versions of
301 * utilities such as bcopy and any such are not yet available
302 * until the kernel is more completely stitched together.
305 void (*kobj_bcopy
)(const void *, void *, size_t);
306 void (*kobj_bzero
)(void *, size_t);
307 size_t (*kobj_strlcat
)(char *, const char *, size_t);
309 static kobj_stat_t kobj_stat
;
311 #define MINALIGN 8 /* at least a double-word */
314 get_weakish_int(int *ip
)
318 return (ip
== NULL
? 0 : *ip
);
322 get_weakish_pointer(void **ptrp
)
326 return (ptrp
== NULL
? 0 : *ptrp
);
330 * XXX fix dependencies on "kernel"; this should work
331 * for other standalone binaries as well.
333 * XXX Fix hashing code to use one pointer to
347 * Load, bind and relocate all modules that
348 * form the primary kernel. At this point, our
349 * externals have not been relocated.
355 struct bootops
*bootvec
,
361 char filename
[MAXPATHLEN
];
364 * Save these to pass on to
365 * the booted standalone.
371 kobj_setup_standalone_vectors();
373 KOBJ_MARK("Entered kobj_init()");
375 (void) BOP_GETPROP(ops
, "whoami", filename
);
378 * We don't support standalone debuggers anymore. The use of kadb
379 * will interfere with the later use of kmdb. Let the user mend
380 * their ways now. Users will reach this message if they still
381 * have the kadb binary on their system (perhaps they used an old
382 * bfu, or maybe they intentionally copied it there) and have
383 * specified its use in a way that eluded our checking in the boot
387 _kobj_printf(ops
, "\nWARNING: Standalone debuggers such as "
388 "kadb are no longer supported\n\n");
394 * OBP allows us to read both the ramdisk and
395 * the underlying root fs when root is a disk.
396 * This can lower incidences of unbootable systems
397 * when the archive is out-of-date with the /etc
400 if (BOP_MOUNTROOT() != BOOT_SVC_OK
) {
401 _kobj_printf(ops
, "can't mount boot fs\n");
406 /* on x86, we always boot with a ramdisk */
407 (void) kobj_boot_mountroot();
410 * Now that the ramdisk is mounted, finish boot property
417 * The early boot code does -not- hand us any of the dynamic
418 * metadata about the executable. In particular, it does not read
419 * in, map or otherwise look at the program headers. We fake all
422 * We do this early as DTrace static probes both call undefined
423 * references. We have to process those relocations before calling
426 * OBP tells kobj_start() where the ELF image is in memory, so it
427 * synthesized bootaux before kobj_init() was called
429 if (bootaux
[BA_PHDR
].ba_ptr
== NULL
)
430 synthetic_bootaux(filename
, bootaux
);
435 * Save the interesting attribute-values
436 * (scanned by kobj_boot).
441 * Set the module search path.
443 kobj_module_path
= getmodpath(filename
);
445 boot_cpu_compatible_list
= find_libmacro("CPU");
448 * These two modules have actually been
449 * loaded by boot, but we finish the job
450 * by introducing them into the world of
454 mp
= load_exec(bootaux
, filename
);
455 load_linker(bootaux
);
458 * Load all the primary dependent modules.
460 if (load_primary(mp
, KOBJ_LM_PRIMARY
) == -1)
466 if (bind_primary(bootaux
, KOBJ_LM_PRIMARY
) == -1)
469 entry
= bootaux
[BA_ENTRY
].ba_val
;
476 if (boothowto
& RB_VERBOSE
)
477 kobj_lm_dump(KOBJ_LM_PRIMARY
);
481 if (boothowto
& RB_KMDB
) {
482 if (load_kmdb(bootaux
) < 0)
494 kobj_sync_instruction_memory(s_text
, e_text
- s_text
);
497 if (kobj_debug
& D_DEBUG
)
499 "krtld: transferring control to: 0x%p\n", entry
);
503 * Make sure the mod system knows about the modules already loaded.
505 last_module_id
= kobj_last_module_id
;
506 bcopy(kobj_modules
, &modules
, sizeof (modules
));
509 if (modp
->mod_next
== kobj_modules
)
510 modp
->mod_next
= &modules
;
511 if (modp
->mod_prev
== kobj_modules
)
512 modp
->mod_prev
= &modules
;
513 } while ((modp
= modp
->mod_next
) != &modules
);
518 if (kobj_debug
& D_DEBUG
)
520 "krtld: really transferring control to: 0x%p\n", entry
);
523 /* restore printf/bcopy/bzero vectors before returning */
524 kobj_restore_vectors();
528 * krtld was called from a dboot ELF section, the embedded
529 * dboot code contains the real entry via bootaux
531 exitto((caddr_t
)entry
);
534 * krtld was directly called from startup
541 _kobj_printf(ops
, "krtld: error during initial load/link phase\n");
542 _kobj_printf(ops
, "\n");
543 _kobj_printf(ops
, "krtld could neither locate nor resolve symbols"
545 _kobj_printf(ops
, " %s\n", filename
);
546 _kobj_printf(ops
, "in the boot archive. Please verify that this"
548 _kobj_printf(ops
, "matches what is found in the boot archive.\n");
549 bop_panic("Unable to boot");
554 * Synthesize additional metadata that describes the executable as
555 * krtld's caller didn't do it.
558 synthetic_bootaux(char *filename
, val_t
*bootaux
)
568 KOBJ_MARK("synthetic_bootaux()");
570 file
= kobj_open_file(filename
);
571 if (file
== (struct _buf
*)-1) {
572 _kobj_printf(ops
, "krtld: failed to open '%s'\n", filename
);
575 KOBJ_MARK("reading program headers");
576 if (kobj_read_file(file
, (char *)&ehdr
, sizeof (ehdr
), 0) < 0) {
577 _kobj_printf(ops
, "krtld: %s: failed to read ehder\n",
585 bootaux
[BA_PHNUM
].ba_val
= ehdr
.e_phnum
;
586 bootaux
[BA_PHENT
].ba_val
= ehdr
.e_phentsize
;
587 n
= ehdr
.e_phentsize
* ehdr
.e_phnum
;
589 phdrbase
= kobj_alloc(n
, KM_WAIT
| KM_TMP
);
591 if (kobj_read_file(file
, phdrbase
, n
, ehdr
.e_phoff
) < 0) {
592 _kobj_printf(ops
, "krtld: %s: failed to read phdrs\n",
596 bootaux
[BA_PHDR
].ba_ptr
= phdrbase
;
597 kobj_close_file(file
);
598 KOBJ_MARK("closed file");
601 * Find the dynamic section address
603 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
604 Phdr
*phdr
= (Phdr
*)(phdrbase
+ ehdr
.e_phentsize
* i
);
606 if (phdr
->p_type
== PT_DYNAMIC
) {
607 bootaux
[BA_DYNAMIC
].ba_ptr
= (void *)phdr
->p_vaddr
;
611 KOBJ_MARK("synthetic_bootaux() done");
616 * Set up any global information derived
617 * from attribute/values in the boot or
621 attr_val(val_t
*bootaux
)
627 KOBJ_MARK("attr_val()");
628 kobj_mmu_pagesize
= bootaux
[BA_PAGESZ
].ba_val
;
629 lg_pagesize
= bootaux
[BA_LPAGESZ
].ba_val
;
630 use_iflush
= bootaux
[BA_IFLUSH
].ba_val
;
632 phdr
= (Phdr
*)bootaux
[BA_PHDR
].ba_ptr
;
633 phnum
= bootaux
[BA_PHNUM
].ba_val
;
634 phsize
= bootaux
[BA_PHENT
].ba_val
;
635 for (i
= 0; i
< phnum
; i
++) {
636 phdr
= (Phdr
*)(bootaux
[BA_PHDR
].ba_val
+ i
* phsize
);
638 if (phdr
->p_type
!= PT_LOAD
) {
642 * Bounds of the various segments.
644 if (!(phdr
->p_flags
& PF_X
)) {
647 * sparc kernel puts the dynamic info
648 * into a separate segment, which is
649 * free'd in bop_fini()
651 ASSERT(phdr
->p_vaddr
!= 0);
652 dynseg
= phdr
->p_vaddr
;
653 dynsize
= phdr
->p_memsz
;
655 ASSERT(phdr
->p_vaddr
== 0);
658 if (phdr
->p_flags
& PF_W
) {
659 _data
= (caddr_t
)phdr
->p_vaddr
;
660 _edata
= _data
+ phdr
->p_memsz
;
662 _text
= (caddr_t
)phdr
->p_vaddr
;
663 _etext
= _text
+ phdr
->p_memsz
;
668 /* To do the kobj_alloc, _edata needs to be set. */
669 for (i
= 0; i
< NLIBMACROS
; i
++) {
670 if (bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
!= NULL
) {
671 libmacros
[i
].lmi_list
= kobj_alloc(
672 strlen(bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
) +
674 (void) strcpy(libmacros
[i
].lmi_list
,
675 bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
);
677 libmacros
[i
].lmi_macrolen
= strlen(libmacros
[i
].lmi_macroname
);
682 * Set up the booted executable.
684 static struct module
*
685 load_exec(val_t
*bootaux
, char *filename
)
691 int i
, lsize
, osize
, nsize
, allocsize
;
693 char path
[MAXPATHLEN
];
696 if (kobj_debug
& D_DEBUG
)
697 _kobj_printf(ops
, "module path '%s'\n", kobj_module_path
);
700 KOBJ_MARK("add_primary");
701 cp
= add_primary(filename
, KOBJ_LM_PRIMARY
);
703 KOBJ_MARK("struct module");
704 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
708 * We don't have the following information
709 * since this module is an executable and not
712 mp
->symtbl_section
= 0;
717 * Since this module is the only exception,
718 * we cons up some section headers.
721 mp
->symhdr
= kobj_zalloc(sizeof (Shdr
), KM_WAIT
);
724 mp
->strhdr
= kobj_zalloc(sizeof (Shdr
), KM_WAIT
);
726 mp
->symhdr
->sh_type
= SHT_SYMTAB
;
727 mp
->strhdr
->sh_type
= SHT_STRTAB
;
729 * Scan the dynamic structure.
731 for (dyn
= (Dyn
*) bootaux
[BA_DYNAMIC
].ba_ptr
;
732 dyn
->d_tag
!= DT_NULL
; dyn
++) {
733 switch (dyn
->d_tag
) {
735 mp
->symspace
= mp
->symtbl
= (char *)dyn
->d_un
.d_ptr
;
736 mp
->symhdr
->sh_addr
= dyn
->d_un
.d_ptr
;
739 mp
->nsyms
= *((uint_t
*)dyn
->d_un
.d_ptr
+ 1);
740 mp
->hashsize
= *(uint_t
*)dyn
->d_un
.d_ptr
;
743 mp
->strings
= (char *)dyn
->d_un
.d_ptr
;
744 mp
->strhdr
->sh_addr
= dyn
->d_un
.d_ptr
;
747 mp
->strhdr
->sh_size
= dyn
->d_un
.d_val
;
750 mp
->symhdr
->sh_entsize
= dyn
->d_un
.d_val
;
756 * Collapse any DT_NEEDED entries into one string.
759 allocsize
= MAXPATHLEN
;
761 KOBJ_MARK("depends_on");
762 mp
->depends_on
= kobj_alloc(allocsize
, KM_WAIT
);
764 for (dyn
= (Dyn
*) bootaux
[BA_DYNAMIC
].ba_ptr
;
765 dyn
->d_tag
!= DT_NULL
; dyn
++)
766 if (dyn
->d_tag
== DT_NEEDED
) {
769 libname
= mp
->strings
+ dyn
->d_un
.d_val
;
770 if (strchr(libname
, '$') != NULL
) {
771 if ((_lib
= expand_libmacro(libname
,
772 path
, path
)) != NULL
)
775 _kobj_printf(ops
, "krtld: "
776 "load_exec: fail to "
777 "expand %s\n", libname
);
779 lsize
= strlen(libname
);
781 if (nsize
+ 1 > allocsize
) {
782 KOBJ_MARK("grow depends_on");
783 tmp
= kobj_alloc(allocsize
+ MAXPATHLEN
,
785 bcopy(mp
->depends_on
, tmp
, osize
);
786 kobj_free(mp
->depends_on
, allocsize
);
787 mp
->depends_on
= tmp
;
788 allocsize
+= MAXPATHLEN
;
790 bcopy(libname
, mp
->depends_on
+ osize
, lsize
);
791 *(mp
->depends_on
+ nsize
) = ' '; /* separate */
796 mp
->depends_on
[nsize
- 1] = '\0'; /* terminate the string */
798 * alloc with exact size and copy whatever it got over
800 KOBJ_MARK("realloc depends_on");
801 tmp
= kobj_alloc(nsize
, KM_WAIT
);
802 bcopy(mp
->depends_on
, tmp
, nsize
);
803 kobj_free(mp
->depends_on
, allocsize
);
804 mp
->depends_on
= tmp
;
806 kobj_free(mp
->depends_on
, allocsize
);
807 mp
->depends_on
= NULL
;
810 mp
->flags
= KOBJ_EXEC
|KOBJ_PRIM
; /* NOT a relocatable .o */
811 mp
->symhdr
->sh_size
= mp
->nsyms
* mp
->symhdr
->sh_entsize
;
813 * We allocate our own table since we don't
814 * hash undefined references.
817 mp
->chains
= kobj_zalloc(mp
->nsyms
* sizeof (symid_t
), KM_WAIT
);
818 KOBJ_MARK("buckets");
819 mp
->buckets
= kobj_zalloc(mp
->hashsize
* sizeof (symid_t
), KM_WAIT
);
824 mp
->text_size
= _etext
- _text
;
825 mp
->data_size
= _edata
- _data
;
827 cp
->mod_text
= mp
->text
;
828 cp
->mod_text_size
= mp
->text_size
;
830 mp
->filename
= cp
->mod_filename
;
833 if (kobj_debug
& D_LOADING
) {
834 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
835 _kobj_printf(ops
, "\ttext: 0x%p", mp
->text
);
836 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
837 _kobj_printf(ops
, "\tdata: 0x%p", mp
->data
);
838 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
840 #endif /* KOBJ_DEBUG */
843 * Insert symbols into the hash table.
845 for (i
= 0; i
< mp
->nsyms
; i
++) {
846 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
848 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
851 sym_insert(mp
, mp
->strings
+ sp
->st_name
, i
);
854 KOBJ_MARK("load_exec done");
859 * Set up the linker module (if it's compiled in, LDNAME is NULL)
862 load_linker(val_t
*bootaux
)
864 struct module
*kmp
= (struct module
*)kobj_modules
->mod_mp
;
871 char *dlname
= (char *)bootaux
[BA_LDNAME
].ba_ptr
;
874 * On some architectures, krtld is compiled into the kernel.
879 cp
= add_primary(dlname
, KOBJ_LM_PRIMARY
);
881 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
884 mp
->hdr
= *(Ehdr
*)bootaux
[BA_LDELF
].ba_ptr
;
885 shsize
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
886 mp
->shdrs
= kobj_alloc(shsize
, KM_WAIT
);
887 bcopy(bootaux
[BA_LDSHDR
].ba_ptr
, mp
->shdrs
, shsize
);
889 for (i
= 1; i
< (int)mp
->hdr
.e_shnum
; i
++) {
890 shp
= (Shdr
*)(mp
->shdrs
+ (i
* mp
->hdr
.e_shentsize
));
892 if (shp
->sh_flags
& SHF_ALLOC
) {
893 if (shp
->sh_flags
& SHF_WRITE
) {
894 if (mp
->data
== NULL
)
895 mp
->data
= (char *)shp
->sh_addr
;
896 } else if (mp
->text
== NULL
) {
897 mp
->text
= (char *)shp
->sh_addr
;
900 if (shp
->sh_type
== SHT_SYMTAB
) {
901 mp
->symtbl_section
= i
;
903 mp
->symspace
= mp
->symtbl
= (char *)shp
->sh_addr
;
906 mp
->nsyms
= mp
->symhdr
->sh_size
/ mp
->symhdr
->sh_entsize
;
907 mp
->flags
= KOBJ_INTERP
|KOBJ_PRIM
;
908 mp
->strhdr
= (Shdr
*)
909 (mp
->shdrs
+ mp
->symhdr
->sh_link
* mp
->hdr
.e_shentsize
);
910 mp
->strings
= (char *)mp
->strhdr
->sh_addr
;
911 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
913 mp
->symsize
= mp
->symhdr
->sh_size
+ mp
->strhdr
->sh_size
+ sizeof (int) +
914 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
);
916 mp
->chains
= kobj_zalloc(mp
->nsyms
* sizeof (symid_t
), KM_WAIT
);
917 mp
->buckets
= kobj_zalloc(mp
->hashsize
* sizeof (symid_t
), KM_WAIT
);
919 mp
->bss
= bootaux
[BA_BSS
].ba_val
;
920 mp
->bss_align
= 0; /* pre-aligned during allocation */
921 mp
->bss_size
= (uintptr_t)_edata
- mp
->bss
;
922 mp
->text_size
= _etext
- mp
->text
;
923 mp
->data_size
= _edata
- mp
->data
;
924 mp
->filename
= cp
->mod_filename
;
925 cp
->mod_text
= mp
->text
;
926 cp
->mod_text_size
= mp
->text_size
;
929 * Now that we've figured out where the linker is,
930 * set the limits for the booted object.
932 kmp
->text_size
= (size_t)(mp
->text
- kmp
->text
);
933 kmp
->data_size
= (size_t)(mp
->data
- kmp
->data
);
934 kobj_modules
->mod_text_size
= kmp
->text_size
;
937 if (kobj_debug
& D_LOADING
) {
938 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
939 _kobj_printf(ops
, "\ttext:0x%p", mp
->text
);
940 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
941 _kobj_printf(ops
, "\tdata:0x%p", mp
->data
);
942 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
944 #endif /* KOBJ_DEBUG */
947 * Insert the symbols into the hash table.
949 for (i
= 0; i
< mp
->nsyms
; i
++) {
950 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
952 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
954 if (ELF_ST_BIND(sp
->st_info
) == STB_GLOBAL
) {
955 if (sp
->st_shndx
== SHN_COMMON
)
956 sp
->st_shndx
= SHN_ABS
;
958 sym_insert(mp
, mp
->strings
+ sp
->st_name
, i
);
963 static kobj_notify_list_t
**
964 kobj_notify_lookup(uint_t type
)
966 ASSERT(type
!= 0 && type
< sizeof (kobj_notifiers
) /
967 sizeof (kobj_notify_list_t
*));
969 return (&kobj_notifiers
[type
]);
973 kobj_notify_add(kobj_notify_list_t
*knp
)
975 kobj_notify_list_t
**knl
;
977 knl
= kobj_notify_lookup(knp
->kn_type
);
982 mutex_enter(&kobj_lock
);
985 (*knl
)->kn_prev
= knp
;
990 mutex_exit(&kobj_lock
);
995 kobj_notify_remove(kobj_notify_list_t
*knp
)
997 kobj_notify_list_t
**knl
= kobj_notify_lookup(knp
->kn_type
);
998 kobj_notify_list_t
*tknp
;
1000 mutex_enter(&kobj_lock
);
1003 if (tknp
= knp
->kn_next
)
1004 tknp
->kn_prev
= knp
->kn_prev
;
1007 if (tknp
= knp
->kn_prev
)
1008 tknp
->kn_next
= knp
->kn_next
;
1010 *knl
= knp
->kn_next
;
1012 mutex_exit(&kobj_lock
);
1018 * Notify all interested callbacks of a specified change in module state.
1021 kobj_notify(int type
, struct modctl
*modp
)
1023 kobj_notify_list_t
*knp
;
1025 if (modp
->mod_loadflags
& MOD_NONOTIFY
|| standalone
)
1028 mutex_enter(&kobj_lock
);
1030 for (knp
= *(kobj_notify_lookup(type
)); knp
!= NULL
; knp
= knp
->kn_next
)
1031 knp
->kn_func(type
, modp
);
1034 * KDI notification must be last (it has to allow for work done by the
1035 * other notification callbacks), so we call it manually.
1037 kobj_kdi_mod_notify(type
, modp
);
1039 mutex_exit(&kobj_lock
);
1043 * Create the module path.
1046 getmodpath(const char *filename
)
1048 char *path
= kobj_zalloc(MAXPATHLEN
, KM_WAIT
);
1051 * Platform code gets first crack, then add
1052 * the default components
1054 mach_modpath(path
, filename
);
1056 (void) strcat(path
, " ");
1057 return (strcat(path
, MOD_DEFPATH
));
1060 static struct modctl
*
1061 add_primary(const char *filename
, int lmid
)
1065 cp
= kobj_zalloc(sizeof (struct modctl
), KM_WAIT
);
1067 cp
->mod_filename
= kobj_alloc(strlen(filename
) + 1, KM_WAIT
);
1070 * For symbol lookup, we assemble our own
1071 * modctl list of the primary modules.
1074 (void) strcpy(cp
->mod_filename
, filename
);
1075 cp
->mod_modname
= basename(cp
->mod_filename
);
1077 /* set values for modinfo assuming that the load will work */
1080 cp
->mod_installed
= 1;
1081 cp
->mod_loadcnt
= 1;
1082 cp
->mod_loadflags
= MOD_NOAUTOUNLOAD
;
1084 cp
->mod_id
= kobj_last_module_id
++;
1087 * Link the module in. We'll pass this info on
1088 * to the mod squad later.
1090 if (kobj_modules
== NULL
) {
1092 cp
->mod_prev
= cp
->mod_next
= cp
;
1094 cp
->mod_prev
= kobj_modules
->mod_prev
;
1095 cp
->mod_next
= kobj_modules
;
1096 kobj_modules
->mod_prev
->mod_next
= cp
;
1097 kobj_modules
->mod_prev
= cp
;
1100 kobj_lm_append(lmid
, cp
);
1106 bind_primary(val_t
*bootaux
, int lmid
)
1108 struct modctl_list
*linkmap
= kobj_lm_lookup(lmid
);
1109 struct modctl_list
*lp
;
1113 * Do common symbols.
1115 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1119 * Don't do common section relocations for modules that
1122 if (mp
->flags
& (KOBJ_EXEC
|KOBJ_INTERP
))
1125 if (do_common(mp
) < 0)
1132 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1135 if (do_symbols(mp
, 0) < 0)
1142 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1145 if (mp
->flags
& KOBJ_EXEC
) {
1147 Word relasz
= 0, relaent
= 0;
1151 for (dyn
= (Dyn
*)bootaux
[BA_DYNAMIC
].ba_ptr
;
1152 dyn
->d_tag
!= DT_NULL
; dyn
++) {
1153 switch (dyn
->d_tag
) {
1156 relasz
= dyn
->d_un
.d_val
;
1160 relaent
= dyn
->d_un
.d_val
;
1164 rela
= (char *)dyn
->d_un
.d_ptr
;
1168 rela
= (char *)dyn
->d_un
.d_ptr
;
1173 relaent
== 0 || rela
== NULL
) {
1174 _kobj_printf(ops
, "krtld: bind_primary(): "
1175 "no relocation information found for "
1176 "module %s\n", mp
->filename
);
1180 if (kobj_debug
& D_RELOCATIONS
)
1181 _kobj_printf(ops
, "krtld: relocating: file=%s "
1182 "KOBJ_EXEC\n", mp
->filename
);
1184 if (do_relocate(mp
, rela
, shtype
, relasz
/relaent
,
1185 relaent
, (Addr
)mp
->text
) < 0)
1188 if (do_relocations(mp
) < 0)
1192 kobj_sync_instruction_memory(mp
->text
, mp
->text_size
);
1195 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1199 * We need to re-read the full symbol table for the boot file,
1200 * since we couldn't use the full one before. We also need to
1201 * load the CTF sections of both the boot file and the
1204 if (mp
->flags
& KOBJ_EXEC
) {
1208 file
= kobj_open_file(mp
->filename
);
1209 if (file
== (struct _buf
*)-1)
1211 if (kobj_read_file(file
, (char *)&mp
->hdr
,
1212 sizeof (mp
->hdr
), 0) < 0)
1214 n
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
1215 mp
->shdrs
= kobj_alloc(n
, KM_WAIT
);
1216 if (kobj_read_file(file
, mp
->shdrs
, n
,
1217 mp
->hdr
.e_shoff
) < 0)
1219 if (get_syms(mp
, file
) < 0)
1221 if (get_ctf(mp
, file
) < 0)
1223 kobj_close_file(file
);
1224 mp
->flags
|= KOBJ_RELOCATED
;
1226 } else if (mp
->flags
& KOBJ_INTERP
) {
1229 file
= kobj_open_path(mp
->filename
, 1);
1230 if (file
== (struct _buf
*)-1)
1232 if (get_ctf(mp
, file
) < 0)
1234 kobj_close_file(file
);
1235 mp
->flags
|= KOBJ_RELOCATED
;
1242 static struct modctl
*
1243 mod_already_loaded(char *modname
)
1245 struct modctl
*mctl
= kobj_modules
;
1248 if (strcmp(modname
, mctl
->mod_filename
) == 0)
1250 mctl
= mctl
->mod_next
;
1252 } while (mctl
!= kobj_modules
);
1258 * Load all the primary dependent modules.
1261 load_primary(struct module
*mp
, int lmid
)
1266 char modname
[MODMAXNAMELEN
];
1268 if ((p
= mp
->depends_on
) == NULL
)
1271 /* CONSTANTCONDITION */
1276 while (*p
&& (*p
== ' ' || *p
== '\t'))
1282 while (*p
&& *p
!= ' ' && *p
!= '\t')
1290 * Check for dup dependencies.
1292 if (strcmp(modname
, "dtracestubs") == 0 ||
1293 mod_already_loaded(modname
) != NULL
)
1296 cp
= add_primary(modname
, lmid
);
1301 (void) kobj_load_module(cp
, 1);
1304 if ((dmp
= cp
->mod_mp
) == NULL
) {
1306 cp
->mod_installed
= 0;
1307 cp
->mod_loadcnt
= 0;
1311 add_dependent(mp
, dmp
);
1312 dmp
->flags
|= KOBJ_PRIM
;
1317 if (load_primary(dmp
, lmid
) == -1) {
1319 cp
->mod_installed
= 0;
1320 cp
->mod_loadcnt
= 0;
1328 console_is_usb_serial(void)
1333 if ((len
= BOP_GETPROPLEN(ops
, "console")) == -1)
1336 console
= kobj_zalloc(len
, KM_WAIT
|KM_TMP
);
1337 (void) BOP_GETPROP(ops
, "console", console
);
1338 ret
= (strcmp(console
, "usb-serial") == 0);
1339 kobj_free(console
, len
);
1345 load_kmdb(val_t
*bootaux
)
1347 struct modctl
*mctl
;
1351 if (console_is_usb_serial()) {
1352 _kobj_printf(ops
, "kmdb not loaded "
1353 "(unsupported on usb serial console)\n");
1357 _kobj_printf(ops
, "Loading kmdb...\n");
1359 if ((mctl
= add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER
)) == NULL
)
1363 (void) kobj_load_module(mctl
, 1);
1366 if ((mp
= mctl
->mod_mp
) == NULL
)
1369 mp
->flags
|= KOBJ_PRIM
;
1371 if (load_primary(mp
, KOBJ_LM_DEBUGGER
) < 0)
1374 if (boothowto
& RB_VERBOSE
)
1375 kobj_lm_dump(KOBJ_LM_DEBUGGER
);
1377 if (bind_primary(bootaux
, KOBJ_LM_DEBUGGER
) < 0)
1380 if ((sym
= lookup_one(mctl
->mod_mp
, "kctl_boot_activate")) == NULL
)
1384 if (kobj_debug
& D_DEBUG
) {
1385 _kobj_printf(ops
, "calling kctl_boot_activate() @ 0x%lx\n",
1387 _kobj_printf(ops
, "\tops 0x%p\n", ops
);
1388 _kobj_printf(ops
, "\tromp 0x%p\n", romp
);
1392 if (((kctl_boot_activate_f
*)sym
->st_value
)(ops
, romp
, 0,
1393 (const char **)kobj_kmdb_argv
) < 0)
1400 * Return a string listing module dependencies.
1403 depends_on(struct module
*mp
)
1409 * The module doesn't have a depends_on value, so let's try it the
1410 * old-fashioned way - via "_depends_on"
1412 if ((sp
= lookup_one(mp
, "_depends_on")) == NULL
)
1415 q
= (char *)sp
->st_value
;
1419 * _depends_on is a deprecated interface, so we warn about its use
1420 * irrespective of subsequent processing errors. How else are we going
1421 * to be able to deco this interface completely?
1422 * Changes initially limited to DEBUG because third-party modules
1423 * should be flagged to developers before general use base.
1426 "Warning: %s uses deprecated _depends_on interface.\n",
1428 _kobj_printf(ops
, "Please notify module developer or vendor.\n");
1432 * Idiot checks. Make sure it's
1433 * in-bounds and NULL terminated.
1435 if (kobj_addrcheck(mp
, q
) || q
[sp
->st_size
- 1] != '\0') {
1436 _kobj_printf(ops
, "Error processing dependency for %s\n",
1441 depstr
= (char *)kobj_alloc(strlen(q
) + 1, KM_WAIT
);
1442 (void) strcpy(depstr
, q
);
1448 kobj_getmodinfo(void *xmp
, struct modinfo
*modinfo
)
1451 mp
= (struct module
*)xmp
;
1453 modinfo
->mi_base
= mp
->text
;
1454 modinfo
->mi_size
= mp
->text_size
+ mp
->data_size
;
1458 * kobj_export_ksyms() performs the following services:
1460 * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena.
1461 * (2) Removes unneeded symbols to save space.
1462 * (3) Reduces memory footprint by using VM_BESTFIT allocations.
1463 * (4) Makes the symbol table visible to /dev/ksyms.
1466 kobj_export_ksyms(struct module
*mp
)
1468 Sym
*esp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_size
);
1474 size_t symsize
= mp
->symhdr
->sh_entsize
;
1479 * Make a copy of the original module structure.
1481 omp
= kobj_alloc(sizeof (struct module
), KM_WAIT
);
1482 bcopy(mp
, omp
, sizeof (struct module
));
1485 * Compute the sizes of the new symbol table sections.
1487 for (nsyms
= strsize
= 1, osp
= (Sym
*)omp
->symtbl
; osp
< esp
; osp
++) {
1488 if (osp
->st_value
== 0)
1490 if (sym_lookup(omp
, osp
) == NULL
)
1492 name
= omp
->strings
+ osp
->st_name
;
1493 namelen
= strlen(name
);
1494 if (ELF_ST_BIND(osp
->st_info
) == STB_LOCAL
)
1497 strsize
+= namelen
+ 1;
1501 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
1504 * ksyms_lock must be held as writer during any operation that
1505 * modifies ksyms_arena, including allocation from same, and
1506 * must not be dropped until the arena is vmem_walk()able.
1508 rw_enter(&ksyms_lock
, RW_WRITER
);
1511 * Allocate space for the new section headers (symtab and strtab),
1512 * symbol table, buckets, chains, and strings.
1514 mp
->symsize
= (2 * sizeof (Shdr
)) + (nsyms
* symsize
) +
1515 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
) + strsize
;
1517 if (mp
->flags
& KOBJ_NOKSYMS
) {
1518 mp
->symspace
= kobj_alloc(mp
->symsize
, KM_WAIT
);
1520 mp
->symspace
= vmem_alloc(ksyms_arena
, mp
->symsize
,
1521 VM_BESTFIT
| VM_SLEEP
);
1523 bzero(mp
->symspace
, mp
->symsize
);
1526 * Divvy up symspace.
1528 mp
->shdrs
= mp
->symspace
;
1529 mp
->symhdr
= (Shdr
*)mp
->shdrs
;
1530 mp
->strhdr
= (Shdr
*)(mp
->symhdr
+ 1);
1531 mp
->symtbl
= (char *)(mp
->strhdr
+ 1);
1532 mp
->buckets
= (symid_t
*)(mp
->symtbl
+ (nsyms
* symsize
));
1533 mp
->chains
= (symid_t
*)(mp
->buckets
+ mp
->hashsize
);
1534 mp
->strings
= (char *)(mp
->chains
+ nsyms
);
1537 * Fill in the new section headers (symtab and strtab).
1539 mp
->hdr
.e_shnum
= 2;
1540 mp
->symtbl_section
= 0;
1542 mp
->symhdr
->sh_type
= SHT_SYMTAB
;
1543 mp
->symhdr
->sh_addr
= (Addr
)mp
->symtbl
;
1544 mp
->symhdr
->sh_size
= nsyms
* symsize
;
1545 mp
->symhdr
->sh_link
= 1;
1546 mp
->symhdr
->sh_info
= locals
;
1547 mp
->symhdr
->sh_addralign
= sizeof (Addr
);
1548 mp
->symhdr
->sh_entsize
= symsize
;
1550 mp
->strhdr
->sh_type
= SHT_STRTAB
;
1551 mp
->strhdr
->sh_addr
= (Addr
)mp
->strings
;
1552 mp
->strhdr
->sh_size
= strsize
;
1553 mp
->strhdr
->sh_addralign
= 1;
1556 * Construct the new symbol table.
1558 for (nsyms
= strsize
= 1, osp
= (Sym
*)omp
->symtbl
; osp
< esp
; osp
++) {
1559 if (osp
->st_value
== 0)
1561 if (sym_lookup(omp
, osp
) == NULL
)
1563 name
= omp
->strings
+ osp
->st_name
;
1564 namelen
= strlen(name
);
1565 sp
= (Sym
*)(mp
->symtbl
+ symsize
* nsyms
);
1566 bcopy(osp
, sp
, symsize
);
1567 bcopy(name
, mp
->strings
+ strsize
, namelen
);
1568 sp
->st_name
= strsize
;
1569 sym_insert(mp
, name
, nsyms
);
1571 strsize
+= namelen
+ 1;
1574 rw_exit(&ksyms_lock
);
1577 * Free the old section headers -- we'll never need them again.
1579 if (!(mp
->flags
& KOBJ_PRIM
)) {
1583 for (shn
= 1; shn
< omp
->hdr
.e_shnum
; shn
++) {
1584 shp
= (Shdr
*)(omp
->shdrs
+ shn
* omp
->hdr
.e_shentsize
);
1585 switch (shp
->sh_type
) {
1588 if (shp
->sh_addr
!= (uintptr_t)NULL
) {
1589 kobj_free((void *)shp
->sh_addr
,
1595 kobj_free(omp
->shdrs
, omp
->hdr
.e_shentsize
* omp
->hdr
.e_shnum
);
1598 * Discard the old symbol table and our copy of the module strucure.
1600 if (!(mp
->flags
& KOBJ_PRIM
))
1601 kobj_free(omp
->symspace
, omp
->symsize
);
1602 kobj_free(omp
, sizeof (struct module
));
1606 kobj_export_ctf(struct module
*mp
)
1608 char *data
= mp
->ctfdata
;
1609 size_t size
= mp
->ctfsize
;
1612 if (_moddebug
& MODDEBUG_NOCTF
) {
1616 mp
->ctfdata
= vmem_alloc(ctf_arena
, size
,
1617 VM_BESTFIT
| VM_SLEEP
);
1618 bcopy(data
, mp
->ctfdata
, size
);
1621 if (!(mp
->flags
& KOBJ_PRIM
))
1622 kobj_free(data
, size
);
1627 kobj_export_module(struct module
*mp
)
1629 kobj_export_ksyms(mp
);
1630 kobj_export_ctf(mp
);
1632 mp
->flags
|= KOBJ_EXPORTED
;
1636 process_dynamic(struct module
*mp
, char *dyndata
, char *strdata
)
1638 char *path
= NULL
, *depstr
= NULL
;
1639 int allocsize
= 0, osize
= 0, nsize
= 0;
1640 char *libname
, *tmp
;
1644 for (dynp
= (Dyn
*)dyndata
; dynp
&& dynp
->d_tag
!= DT_NULL
; dynp
++) {
1645 switch (dynp
->d_tag
) {
1648 * Read the DT_NEEDED entries, expanding the macros they
1649 * contain (if any), and concatenating them into a
1650 * single space-separated dependency list.
1652 libname
= (ulong_t
)dynp
->d_un
.d_ptr
+ strdata
;
1654 if (strchr(libname
, '$') != NULL
) {
1658 path
= kobj_alloc(MAXPATHLEN
, KM_WAIT
);
1659 if ((_lib
= expand_libmacro(libname
, path
,
1663 _kobj_printf(ops
, "krtld: "
1664 "process_dynamic: failed to expand "
1669 lsize
= strlen(libname
);
1671 if (nsize
+ 1 > allocsize
) {
1672 tmp
= kobj_alloc(allocsize
+ MAXPATHLEN
,
1674 if (depstr
!= NULL
) {
1675 bcopy(depstr
, tmp
, osize
);
1676 kobj_free(depstr
, allocsize
);
1679 allocsize
+= MAXPATHLEN
;
1681 bcopy(libname
, depstr
+ osize
, lsize
);
1682 *(depstr
+ nsize
) = ' '; /* separator */
1688 if (dynp
->d_un
.d_val
& DF_1_IGNMULDEF
)
1689 mp
->flags
|= KOBJ_IGNMULDEF
;
1690 if (dynp
->d_un
.d_val
& DF_1_NOKSYMS
)
1691 mp
->flags
|= KOBJ_NOKSYMS
;
1698 * finish up the depends string (if any)
1700 if (depstr
!= NULL
) {
1701 *(depstr
+ nsize
- 1) = '\0'; /* overwrite separator w/term */
1703 kobj_free(path
, MAXPATHLEN
);
1705 tmp
= kobj_alloc(nsize
, KM_WAIT
);
1706 bcopy(depstr
, tmp
, nsize
);
1707 kobj_free(depstr
, allocsize
);
1710 mp
->depends_on
= depstr
;
1717 do_dynamic(struct module
*mp
, struct _buf
*file
)
1719 Shdr
*dshp
, *dstrp
, *shp
;
1720 char *dyndata
, *dstrdata
;
1723 /* find and validate the dynamic section (if any) */
1725 for (dshp
= NULL
, shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
1726 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
1727 switch (shp
->sh_type
) {
1730 _kobj_printf(ops
, "krtld: get_dynamic: %s, ",
1733 "multiple dynamic sections\n");
1746 if (dshp
->sh_link
> mp
->hdr
.e_shnum
) {
1747 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1748 _kobj_printf(ops
, "no section for sh_link %d\n", dshp
->sh_link
);
1751 dstrp
= (Shdr
*)(mp
->shdrs
+ dshp
->sh_link
* mp
->hdr
.e_shentsize
);
1753 if (dstrp
->sh_type
!= SHT_STRTAB
) {
1754 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1755 _kobj_printf(ops
, "sh_link not a string table for section %d\n",
1760 /* read it from disk */
1762 dyndata
= kobj_alloc(dshp
->sh_size
, KM_WAIT
|KM_TMP
);
1763 if (kobj_read_file(file
, dyndata
, dshp
->sh_size
, dshp
->sh_offset
) < 0) {
1764 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1765 _kobj_printf(ops
, "error reading section %d\n", dshn
);
1767 kobj_free(dyndata
, dshp
->sh_size
);
1771 dstrdata
= kobj_alloc(dstrp
->sh_size
, KM_WAIT
|KM_TMP
);
1772 if (kobj_read_file(file
, dstrdata
, dstrp
->sh_size
,
1773 dstrp
->sh_offset
) < 0) {
1774 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1775 _kobj_printf(ops
, "error reading section %d\n", dshp
->sh_link
);
1777 kobj_free(dyndata
, dshp
->sh_size
);
1778 kobj_free(dstrdata
, dstrp
->sh_size
);
1782 /* pull the interesting pieces out */
1784 rc
= process_dynamic(mp
, dyndata
, dstrdata
);
1786 kobj_free(dyndata
, dshp
->sh_size
);
1787 kobj_free(dstrdata
, dstrp
->sh_size
);
1793 kobj_set_ctf(struct module
*mp
, caddr_t data
, size_t size
)
1796 if (mp
->ctfdata
!= NULL
) {
1797 if (vmem_contains(ctf_arena
, mp
->ctfdata
,
1799 vmem_free(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
);
1801 kobj_free(mp
->ctfdata
, mp
->ctfsize
);
1807 * The order is very important here. We need to make sure that
1808 * consumers, at any given instant, see a consistent state. We'd
1809 * rather they see no CTF data than the address of one buffer and the
1820 kobj_load_module(struct modctl
*modp
, int use_path
)
1822 char *filename
= modp
->mod_filename
;
1823 char *modname
= modp
->mod_modname
;
1827 struct module
*mp
= NULL
;
1829 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
1832 * We need to prevent kmdb's symbols from leaking into /dev/ksyms.
1833 * kmdb contains a bunch of symbols with well-known names, symbols
1834 * which will mask the real versions, thus causing no end of trouble
1837 if (strcmp(modp
->mod_modname
, "kmdbmod") == 0)
1838 mp
->flags
|= KOBJ_NOKSYMS
;
1840 file
= kobj_open_path(filename
, use_path
);
1841 if (file
== (struct _buf
*)-1) {
1842 if (file
== (struct _buf
*)-1) {
1843 kobj_free(mp
, sizeof (*mp
));
1848 mp
->filename
= kobj_alloc(strlen(file
->_name
) + 1, KM_WAIT
);
1849 (void) strcpy(mp
->filename
, file
->_name
);
1851 if (kobj_read_file(file
, (char *)&mp
->hdr
, sizeof (mp
->hdr
), 0) < 0) {
1852 _kobj_printf(ops
, "kobj_load_module: %s read header failed\n",
1854 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1855 kobj_free(mp
, sizeof (*mp
));
1858 for (i
= 0; i
< SELFMAG
; i
++) {
1859 if (mp
->hdr
.e_ident
[i
] != ELFMAG
[i
]) {
1860 if (_moddebug
& MODDEBUG_ERRMSG
)
1861 _kobj_printf(ops
, "%s not an elf module\n",
1863 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1864 kobj_free(mp
, sizeof (*mp
));
1869 * It's ELF, but is it our ISA? Interpreting the header
1870 * from a file for a byte-swapped ISA could cause a huge
1871 * and unsatisfiable value to be passed to kobj_alloc below
1872 * and therefore hang booting.
1874 if (!elf_mach_ok(&mp
->hdr
)) {
1875 if (_moddebug
& MODDEBUG_ERRMSG
)
1876 _kobj_printf(ops
, "%s not an elf module for this ISA\n",
1878 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1879 kobj_free(mp
, sizeof (*mp
));
1884 * All modules, save for unix, should be relocatable (as opposed to
1885 * dynamic). Dynamic modules come with PLTs and GOTs, which can't
1886 * currently be processed by krtld.
1888 if (mp
->hdr
.e_type
!= ET_REL
) {
1889 if (_moddebug
& MODDEBUG_ERRMSG
)
1890 _kobj_printf(ops
, "%s isn't a relocatable (ET_REL) "
1891 "module\n", modname
);
1892 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1893 kobj_free(mp
, sizeof (*mp
));
1897 n
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
1898 mp
->shdrs
= kobj_alloc(n
, KM_WAIT
);
1900 if (kobj_read_file(file
, mp
->shdrs
, n
, mp
->hdr
.e_shoff
) < 0) {
1901 _kobj_printf(ops
, "kobj_load_module: %s error reading "
1902 "section headers\n", modname
);
1903 kobj_free(mp
->shdrs
, n
);
1904 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1905 kobj_free(mp
, sizeof (*mp
));
1909 kobj_notify(KOBJ_NOTIFY_MODLOADING
, modp
);
1910 module_assign(modp
, mp
);
1912 /* read in sections */
1913 if (get_progbits(mp
, file
) < 0) {
1914 _kobj_printf(ops
, "%s error reading sections\n", modname
);
1918 if (do_dynamic(mp
, file
) < 0) {
1919 _kobj_printf(ops
, "%s error reading dynamic section\n",
1924 modp
->mod_text
= mp
->text
;
1925 modp
->mod_text_size
= mp
->text_size
;
1927 /* read in symbols; adjust values for each section's real address */
1928 if (get_syms(mp
, file
) < 0) {
1929 _kobj_printf(ops
, "%s error reading symbols\n",
1935 * If we didn't dependency information from the dynamic section, look
1936 * for it the old-fashioned way.
1938 if (mp
->depends_on
== NULL
)
1939 mp
->depends_on
= depends_on(mp
);
1941 if (get_ctf(mp
, file
) < 0) {
1942 _kobj_printf(ops
, "%s debug information will not "
1943 "be available\n", modname
);
1946 /* primary kernel modules do not have a signature section */
1947 if (!(mp
->flags
& KOBJ_PRIM
))
1948 get_signature(mp
, file
);
1951 if (kobj_debug
& D_LOADING
) {
1952 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
1953 _kobj_printf(ops
, "\ttext:0x%p", mp
->text
);
1954 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
1955 _kobj_printf(ops
, "\tdata:0x%p", mp
->data
);
1956 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
1958 #endif /* KOBJ_DEBUG */
1961 * For primary kernel modules, we defer
1962 * symbol resolution and relocation until
1963 * all primary objects have been loaded.
1967 char *dependent_modname
;
1968 /* load all dependents */
1969 dependent_modname
= kobj_zalloc(MODMAXNAMELEN
, KM_WAIT
);
1970 ddrval
= do_dependents(modp
, dependent_modname
, MODMAXNAMELEN
);
1973 * resolve undefined and common symbols,
1974 * also allocates common space
1976 if ((dcrval
= do_common(mp
)) < 0) {
1979 _kobj_printf(ops
, "WARNING: mod_load: "
1980 "MT-unsafe module '%s' rejected\n",
1984 _kobj_printf(ops
, "WARNING: mod_load: "
1985 "cannot load module '%s'\n",
1988 _kobj_printf(ops
, "WARNING: %s: ",
1991 "unable to resolve dependency, "
1992 "module '%s' not found\n",
1998 kobj_free(dependent_modname
, MODMAXNAMELEN
);
2002 /* process relocation tables */
2003 if (do_relocations(mp
) < 0) {
2004 _kobj_printf(ops
, "%s error doing relocations\n",
2009 if (mp
->destination
) {
2010 off_t off
= (uintptr_t)mp
->destination
& PAGEOFFSET
;
2011 caddr_t base
= (caddr_t
)mp
->destination
- off
;
2012 size_t size
= P2ROUNDUP(mp
->text_size
+ off
, PAGESIZE
);
2014 hat_unload(kas
.a_hat
, base
, size
, HAT_UNLOAD_UNLOCK
);
2015 vmem_free(heap_arena
, base
, size
);
2018 /* sync_instruction_memory */
2019 kobj_sync_instruction_memory(mp
->text
, mp
->text_size
);
2020 kobj_export_module(mp
);
2021 kobj_notify(KOBJ_NOTIFY_MODLOADED
, modp
);
2023 kobj_close_file(file
);
2026 if (file
!= (struct _buf
*)-1)
2027 kobj_close_file(file
);
2028 if (modp
->mod_mp
!= NULL
)
2029 free_module_data(modp
->mod_mp
);
2031 module_assign(modp
, NULL
);
2032 return ((file
== (struct _buf
*)-1) ? ENOENT
: EINVAL
);
2036 kobj_load_primary_module(struct modctl
*modp
)
2041 if (kobj_load_module(modp
, 0) != 0)
2045 mp
->flags
|= KOBJ_PRIM
;
2047 /* Bind new module to its dependents */
2048 if (mp
->depends_on
!= NULL
&& (dep
=
2049 mod_already_loaded(mp
->depends_on
)) == NULL
) {
2051 if (kobj_debug
& D_DEBUG
) {
2052 _kobj_printf(ops
, "krtld: failed to resolve deps "
2053 "for primary %s\n", modp
->mod_modname
);
2059 add_dependent(mp
, dep
->mod_mp
);
2062 * Relocate it. This module may not be part of a link map, so we
2063 * can't use bind_primary.
2065 if (do_common(mp
) < 0 || do_symbols(mp
, 0) < 0 ||
2066 do_relocations(mp
) < 0) {
2068 if (kobj_debug
& D_DEBUG
) {
2069 _kobj_printf(ops
, "krtld: failed to relocate "
2070 "primary %s\n", modp
->mod_modname
);
2080 module_assign(struct modctl
*cp
, struct module
*mp
)
2086 mutex_enter(&mod_lock
);
2089 mutex_exit(&mod_lock
);
2093 kobj_unload_module(struct modctl
*modp
)
2095 struct module
*mp
= modp
->mod_mp
;
2097 if ((_moddebug
& MODDEBUG_KEEPTEXT
) && mp
) {
2098 _kobj_printf(ops
, "text for %s ", mp
->filename
);
2099 _kobj_printf(ops
, "was at %p\n", mp
->text
);
2100 mp
->text
= NULL
; /* don't actually free it */
2103 kobj_notify(KOBJ_NOTIFY_MODUNLOADING
, modp
);
2106 * Null out mod_mp first, so consumers (debuggers) know not to look
2107 * at the module structure any more.
2109 mutex_enter(&mod_lock
);
2110 modp
->mod_mp
= NULL
;
2111 mutex_exit(&mod_lock
);
2113 kobj_notify(KOBJ_NOTIFY_MODUNLOADED
, modp
);
2114 free_module_data(mp
);
2118 free_module_data(struct module
*mp
)
2120 struct module_list
*lp
, *tmp
;
2121 int ksyms_exported
= 0;
2127 kobj_free((char *)tmp
, sizeof (*tmp
));
2130 rw_enter(&ksyms_lock
, RW_WRITER
);
2132 if (vmem_contains(ksyms_arena
, mp
->symspace
, mp
->symsize
)) {
2133 vmem_free(ksyms_arena
, mp
->symspace
, mp
->symsize
);
2136 if (mp
->flags
& KOBJ_NOKSYMS
)
2138 kobj_free(mp
->symspace
, mp
->symsize
);
2141 rw_exit(&ksyms_lock
);
2144 if (vmem_contains(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
))
2145 vmem_free(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
);
2147 kobj_free(mp
->ctfdata
, mp
->ctfsize
);
2151 kobj_free(mp
->sigdata
, mp
->sigsize
);
2154 * We did not get far enough into kobj_export_ksyms() to free allocated
2155 * buffers because we encounted error conditions. Free the buffers.
2157 if ((ksyms_exported
== 0) && (mp
->shdrs
!= NULL
)) {
2161 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2162 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2163 switch (shp
->sh_type
) {
2166 if (shp
->sh_addr
!= 0)
2167 kobj_free((void *)shp
->sh_addr
,
2173 if (!(mp
->flags
& KOBJ_PRIM
)) {
2174 kobj_free(mp
->shdrs
,
2175 mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
);
2180 vmem_free(data_arena
, (void *)mp
->bss
, mp
->bss_size
);
2183 kobj_texthole_free(mp
->fbt_tab
, mp
->fbt_size
);
2185 if (mp
->textwin_base
)
2186 kobj_textwin_free(mp
);
2188 if (mp
->sdt_probes
!= NULL
) {
2189 sdt_probedesc_t
*sdp
= mp
->sdt_probes
, *next
;
2191 while (sdp
!= NULL
) {
2192 next
= sdp
->sdpd_next
;
2193 kobj_free(sdp
->sdpd_name
, strlen(sdp
->sdpd_name
) + 1);
2194 kobj_free(sdp
, sizeof (sdt_probedesc_t
));
2200 kobj_texthole_free(mp
->sdt_tab
, mp
->sdt_size
);
2202 vmem_free(text_arena
, mp
->text
, mp
->text_size
);
2204 vmem_free(data_arena
, mp
->data
, mp
->data_size
);
2206 kobj_free(mp
->depends_on
, strlen(mp
->depends_on
)+1);
2208 kobj_free(mp
->filename
, strlen(mp
->filename
)+1);
2210 kobj_free((char *)mp
, sizeof (*mp
));
2214 get_progbits(struct module
*mp
, struct _buf
*file
)
2216 struct proginfo
*tp
, *dp
, *sdp
;
2218 reloc_dest_t dest
= NULL
;
2220 uintptr_t text
= 0, data
, textptr
;
2224 tp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2225 dp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2226 sdp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2228 * loop through sections to find out how much space we need
2229 * for text, data, (also bss that is already assigned)
2231 if (get_progbits_size(mp
, tp
, dp
, sdp
) < 0)
2234 mp
->text_size
= tp
->size
;
2235 mp
->data_size
= dp
->size
;
2238 caddr_t limit
= _data
;
2240 if (lg_pagesize
&& _text
+ lg_pagesize
< limit
)
2241 limit
= _text
+ lg_pagesize
;
2243 mp
->text
= kobj_segbrk(&_etext
, mp
->text_size
,
2246 * If we can't grow the text segment, try the
2247 * data segment before failing.
2249 if (mp
->text
== NULL
) {
2250 mp
->text
= kobj_segbrk(&_edata
, mp
->text_size
,
2254 mp
->data
= kobj_segbrk(&_edata
, mp
->data_size
, dp
->align
, 0);
2256 if (mp
->text
== NULL
|| mp
->data
== NULL
)
2260 if (text_arena
== NULL
)
2261 kobj_vmem_init(&text_arena
, &data_arena
);
2264 * some architectures may want to load the module on a
2265 * page that is currently read only. It may not be
2266 * possible for those architectures to remap their page
2267 * on the fly. So we provide a facility for them to hang
2268 * a private hook where the memory they assign the module
2269 * is not the actual place where the module loads.
2271 * In this case there are two addresses that deal with the
2273 * 1) the final destination of the module
2274 * 2) the address that is used to view the newly
2275 * loaded module until all the relocations relative to 1
2276 * above are completed.
2278 * That is what dest is used for below.
2280 mp
->text_size
+= tp
->align
;
2281 mp
->data_size
+= dp
->align
;
2283 mp
->text
= kobj_text_alloc(text_arena
, mp
->text_size
);
2286 * a remap is taking place. Align the text ptr relative
2287 * to the secondary mapping. That is where the bits will
2290 if (kvseg
.s_base
!= NULL
&& !vmem_contains(heaptext_arena
,
2291 mp
->text
, mp
->text_size
)) {
2292 off_t off
= (uintptr_t)mp
->text
& PAGEOFFSET
;
2293 size_t size
= P2ROUNDUP(mp
->text_size
+ off
, PAGESIZE
);
2294 caddr_t map
= vmem_alloc(heap_arena
, size
, VM_SLEEP
);
2295 caddr_t orig
= mp
->text
- off
;
2296 pgcnt_t pages
= size
/ PAGESIZE
;
2298 dest
= (reloc_dest_t
)(map
+ off
);
2299 text
= ALIGN((uintptr_t)dest
, tp
->align
);
2302 hat_devload(kas
.a_hat
, map
, PAGESIZE
,
2303 hat_getpfnum(kas
.a_hat
, orig
),
2304 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
2305 HAT_LOAD_NOCONSIST
| HAT_LOAD_LOCK
);
2310 * Since we set up a non-cacheable mapping, we need
2311 * to flush any old entries in the cache that might
2312 * be left around from the read-only mapping.
2317 mp
->data
= vmem_alloc(data_arena
, mp
->data_size
,
2318 VM_SLEEP
| VM_BESTFIT
);
2320 textptr
= (uintptr_t)mp
->text
;
2321 textptr
= ALIGN(textptr
, tp
->align
);
2322 mp
->destination
= dest
;
2325 * This is the case where a remap is not being done.
2328 text
= ALIGN((uintptr_t)mp
->text
, tp
->align
);
2329 data
= ALIGN((uintptr_t)mp
->data
, dp
->align
);
2331 /* now loop though sections assigning addresses and loading the data */
2332 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2333 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2334 if (!(shp
->sh_flags
& SHF_ALLOC
))
2337 if ((shp
->sh_flags
& SHF_WRITE
) == 0)
2342 bits_ptr
= ALIGN(bits_ptr
, shp
->sh_addralign
);
2344 if (shp
->sh_type
== SHT_NOBITS
) {
2348 bzero((caddr_t
)bits_ptr
, shp
->sh_size
);
2349 shp
->sh_type
= SHT_PROGBITS
;
2351 if (kobj_read_file(file
, (char *)bits_ptr
,
2352 shp
->sh_size
, shp
->sh_offset
) < 0)
2356 if (shp
->sh_flags
& SHF_WRITE
) {
2357 shp
->sh_addr
= bits_ptr
;
2359 textptr
= ALIGN(textptr
, shp
->sh_addralign
);
2360 shp
->sh_addr
= textptr
;
2361 textptr
+= shp
->sh_size
;
2364 bits_ptr
+= shp
->sh_size
;
2365 if ((shp
->sh_flags
& SHF_WRITE
) == 0)
2374 * Free and mark as freed the section headers here so that
2375 * free_module_data() does not have to worry about this buffer.
2377 * This buffer is freed here because one of the possible reasons
2378 * for error is a section with non-zero sh_addr and in that case
2379 * free_module_data() would have no way of recognizing that this
2380 * buffer was unallocated.
2383 kobj_free(mp
->shdrs
, mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
);
2387 (void) kobj_free(tp
, sizeof (struct proginfo
));
2388 (void) kobj_free(dp
, sizeof (struct proginfo
));
2389 (void) kobj_free(sdp
, sizeof (struct proginfo
));
2395 * Go through suppress_sym_list to see if "multiply defined"
2396 * warning of this symbol should be suppressed. Return 1 if
2397 * warning should be suppressed, 0 otherwise.
2400 kobj_suppress_warning(char *symname
)
2404 for (i
= 0; suppress_sym_list
[i
] != NULL
; i
++) {
2405 if (strcmp(suppress_sym_list
[i
], symname
) == 0)
2413 get_syms(struct module
*mp
, struct _buf
*file
)
2423 * Find the interesting sections.
2425 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2426 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2427 switch (shp
->sh_type
) {
2429 mp
->symtbl_section
= shn
;
2442 /* KM_TMP since kobj_free'd in do_relocations */
2443 shp
->sh_addr
= (Addr
)
2444 kobj_alloc(shp
->sh_size
, KM_WAIT
|KM_TMP
);
2446 if (kobj_read_file(file
, (char *)shp
->sh_addr
,
2447 shp
->sh_size
, shp
->sh_offset
) < 0) {
2448 _kobj_printf(ops
, "krtld: get_syms: %s, ",
2450 _kobj_printf(ops
, "error reading section %d\n",
2459 * This is true for a stripped executable. In the case of
2460 * 'unix' it can be stripped but it still contains the SHT_DYNSYM,
2461 * and since that symbol information is still present everything
2465 if (mp
->flags
& KOBJ_EXEC
)
2467 _kobj_printf(ops
, "krtld: get_syms: %s ",
2469 _kobj_printf(ops
, "no SHT_SYMTAB symbol table found\n");
2474 * get the associated string table header
2476 if ((mp
->symhdr
== 0) || (mp
->symhdr
->sh_link
>= mp
->hdr
.e_shnum
))
2478 mp
->strhdr
= (Shdr
*)
2479 (mp
->shdrs
+ mp
->symhdr
->sh_link
* mp
->hdr
.e_shentsize
);
2481 mp
->nsyms
= mp
->symhdr
->sh_size
/ mp
->symhdr
->sh_entsize
;
2482 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
2485 * Allocate space for the symbol table, buckets, chains, and strings.
2487 mp
->symsize
= mp
->symhdr
->sh_size
+
2488 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
) + mp
->strhdr
->sh_size
;
2489 mp
->symspace
= kobj_zalloc(mp
->symsize
, KM_WAIT
|KM_SCRATCH
);
2491 mp
->symtbl
= mp
->symspace
;
2492 mp
->buckets
= (symid_t
*)(mp
->symtbl
+ mp
->symhdr
->sh_size
);
2493 mp
->chains
= mp
->buckets
+ mp
->hashsize
;
2494 mp
->strings
= (char *)(mp
->chains
+ mp
->nsyms
);
2496 if (kobj_read_file(file
, mp
->symtbl
,
2497 mp
->symhdr
->sh_size
, mp
->symhdr
->sh_offset
) < 0 ||
2498 kobj_read_file(file
, mp
->strings
,
2499 mp
->strhdr
->sh_size
, mp
->strhdr
->sh_offset
) < 0)
2503 * loop through the symbol table adjusting values to account
2504 * for where each section got loaded into memory. Also
2505 * fill in the hash table.
2507 for (i
= 1; i
< mp
->nsyms
; i
++) {
2508 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
2509 if (sp
->st_shndx
< SHN_LORESERVE
) {
2510 if (sp
->st_shndx
>= mp
->hdr
.e_shnum
) {
2511 _kobj_printf(ops
, "%s bad shndx ",
2513 _kobj_printf(ops
, "in symbol %d\n", i
);
2518 sp
->st_shndx
* mp
->hdr
.e_shentsize
);
2519 if (!(mp
->flags
& KOBJ_EXEC
))
2520 sp
->st_value
+= shp
->sh_addr
;
2523 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
2525 if (sp
->st_name
>= mp
->strhdr
->sh_size
)
2528 symname
= mp
->strings
+ sp
->st_name
;
2530 if (!(mp
->flags
& KOBJ_EXEC
) &&
2531 ELF_ST_BIND(sp
->st_info
) == STB_GLOBAL
) {
2532 ksp
= kobj_lookup_all(mp
, symname
, 0);
2534 if (ksp
&& ELF_ST_BIND(ksp
->st_info
) == STB_GLOBAL
&&
2535 !kobj_suppress_warning(symname
) &&
2536 sp
->st_shndx
!= SHN_UNDEF
&&
2537 sp
->st_shndx
!= SHN_COMMON
&&
2538 ksp
->st_shndx
!= SHN_UNDEF
&&
2539 ksp
->st_shndx
!= SHN_COMMON
) {
2541 * Unless this symbol is a stub, it's multiply
2542 * defined. Multiply-defined symbols are
2543 * usually bad, but some objects (kmdb) have
2544 * a legitimate need to have their own
2545 * copies of common functions.
2548 ksp
->st_value
< (uintptr_t)stubs_base
||
2549 ksp
->st_value
>= (uintptr_t)stubs_end
) &&
2550 !(mp
->flags
& KOBJ_IGNMULDEF
)) {
2552 "%s symbol ", file
->_name
);
2554 "%s multiply defined\n", symname
);
2559 sym_insert(mp
, symname
, i
);
2566 get_ctf(struct module
*mp
, struct _buf
*file
)
2568 char *shstrtab
, *ctfdata
;
2573 if (_moddebug
& MODDEBUG_NOCTF
)
2574 return (0); /* do not attempt to even load CTF data */
2576 if (mp
->hdr
.e_shstrndx
>= mp
->hdr
.e_shnum
) {
2577 _kobj_printf(ops
, "krtld: get_ctf: %s, ",
2579 _kobj_printf(ops
, "corrupt e_shstrndx %u\n",
2580 mp
->hdr
.e_shstrndx
);
2584 shp
= (Shdr
*)(mp
->shdrs
+ mp
->hdr
.e_shstrndx
* mp
->hdr
.e_shentsize
);
2585 shstrlen
= shp
->sh_size
;
2586 shstrtab
= kobj_alloc(shstrlen
, KM_WAIT
|KM_TMP
);
2588 if (kobj_read_file(file
, shstrtab
, shstrlen
, shp
->sh_offset
) < 0) {
2589 _kobj_printf(ops
, "krtld: get_ctf: %s, ",
2591 _kobj_printf(ops
, "error reading section %u\n",
2592 mp
->hdr
.e_shstrndx
);
2593 kobj_free(shstrtab
, shstrlen
);
2597 for (i
= 0; i
< mp
->hdr
.e_shnum
; i
++) {
2598 shp
= (Shdr
*)(mp
->shdrs
+ i
* mp
->hdr
.e_shentsize
);
2600 if (shp
->sh_size
!= 0 && shp
->sh_name
< shstrlen
&&
2601 strcmp(shstrtab
+ shp
->sh_name
, ".SUNW_ctf") == 0) {
2602 ctfdata
= kobj_alloc(shp
->sh_size
, KM_WAIT
|KM_SCRATCH
);
2604 if (kobj_read_file(file
, ctfdata
, shp
->sh_size
,
2605 shp
->sh_offset
) < 0) {
2606 _kobj_printf(ops
, "krtld: get_ctf: %s, error "
2607 "reading .SUNW_ctf data\n", mp
->filename
);
2608 kobj_free(ctfdata
, shp
->sh_size
);
2609 kobj_free(shstrtab
, shstrlen
);
2613 mp
->ctfdata
= ctfdata
;
2614 mp
->ctfsize
= shp
->sh_size
;
2619 kobj_free(shstrtab
, shstrlen
);
2623 #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */
2626 * Return the hash of the ELF sections that are memory resident.
2627 * i.e. text and data. We skip a SHT_NOBITS section since it occupies
2628 * no space in the file. We use SHA1 here since libelfsign uses
2629 * it and both places need to use the same algorithm.
2632 crypto_es_hash(struct module
*mp
, char *hash
, char *shstrtab
)
2640 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2641 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2642 if (!(shp
->sh_flags
& SHF_ALLOC
) || shp
->sh_size
== 0)
2646 * The check should ideally be shp->sh_type == SHT_NOBITS.
2647 * However, we can't do that check here as get_progbits()
2650 if (strcmp(shstrtab
+ shp
->sh_name
, ".bss") == 0)
2653 if (kobj_debug
& D_DEBUG
)
2655 "krtld: crypto_es_hash: updating hash with"
2656 " %s data size=%d\n", shstrtab
+ shp
->sh_name
,
2659 ASSERT(shp
->sh_addr
!= (uintptr_t)NULL
);
2660 SHA1Update(&ctx
, (const uint8_t *)shp
->sh_addr
, shp
->sh_size
);
2663 SHA1Final((uchar_t
*)hash
, &ctx
);
2667 * Get the .SUNW_signature section for the module, it it exists.
2669 * This section exists only for crypto modules. None of the
2670 * primary modules have this section currently.
2673 get_signature(struct module
*mp
, struct _buf
*file
)
2675 char *shstrtab
, *sigdata
= NULL
;
2680 if (mp
->hdr
.e_shstrndx
>= mp
->hdr
.e_shnum
) {
2681 _kobj_printf(ops
, "krtld: get_signature: %s, ",
2683 _kobj_printf(ops
, "corrupt e_shstrndx %u\n",
2684 mp
->hdr
.e_shstrndx
);
2688 shp
= (Shdr
*)(mp
->shdrs
+ mp
->hdr
.e_shstrndx
* mp
->hdr
.e_shentsize
);
2689 shstrlen
= shp
->sh_size
;
2690 shstrtab
= kobj_alloc(shstrlen
, KM_WAIT
|KM_TMP
);
2692 if (kobj_read_file(file
, shstrtab
, shstrlen
, shp
->sh_offset
) < 0) {
2693 _kobj_printf(ops
, "krtld: get_signature: %s, ",
2695 _kobj_printf(ops
, "error reading section %u\n",
2696 mp
->hdr
.e_shstrndx
);
2697 kobj_free(shstrtab
, shstrlen
);
2701 for (i
= 0; i
< mp
->hdr
.e_shnum
; i
++) {
2702 shp
= (Shdr
*)(mp
->shdrs
+ i
* mp
->hdr
.e_shentsize
);
2703 if (shp
->sh_size
!= 0 && shp
->sh_name
< shstrlen
&&
2704 strcmp(shstrtab
+ shp
->sh_name
,
2705 ELF_SIGNATURE_SECTION
) == 0) {
2706 filesig_vers_t filesig_version
;
2707 size_t sigsize
= shp
->sh_size
+ SHA1_DIGEST_LENGTH
;
2708 sigdata
= kobj_alloc(sigsize
, KM_WAIT
|KM_SCRATCH
);
2710 if (kobj_read_file(file
, sigdata
, shp
->sh_size
,
2711 shp
->sh_offset
) < 0) {
2712 _kobj_printf(ops
, "krtld: get_signature: %s,"
2713 " error reading .SUNW_signature data\n",
2715 kobj_free(sigdata
, sigsize
);
2716 kobj_free(shstrtab
, shstrlen
);
2719 filesig_version
= ((struct filesignatures
*)sigdata
)->
2720 filesig_sig
.filesig_version
;
2721 if (!(filesig_version
== FILESIG_VERSION1
||
2722 filesig_version
== FILESIG_VERSION3
)) {
2723 /* skip versions we don't understand */
2724 kobj_free(sigdata
, sigsize
);
2725 kobj_free(shstrtab
, shstrlen
);
2729 mp
->sigdata
= sigdata
;
2730 mp
->sigsize
= sigsize
;
2735 if (sigdata
!= NULL
) {
2736 crypto_es_hash(mp
, sigdata
+ shp
->sh_size
, shstrtab
);
2739 kobj_free(shstrtab
, shstrlen
);
2743 add_dependent(struct module
*mp
, struct module
*dep
)
2745 struct module_list
*lp
;
2747 for (lp
= mp
->head
; lp
; lp
= lp
->next
) {
2749 return; /* already on the list */
2753 lp
= kobj_zalloc(sizeof (*lp
), KM_WAIT
);
2758 mp
->tail
->next
= lp
;
2766 do_dependents(struct modctl
*modp
, char *modname
, size_t modnamelen
)
2772 char *err_modname
= NULL
;
2776 if ((p
= mp
->depends_on
) == NULL
)
2783 while (*p
&& (*p
== ' ' || *p
== '\t'))
2791 while (*p
&& *p
!= ' ' && *p
!= '\t') {
2792 if (c
< modnamelen
- 1) {
2802 if (c
== modnamelen
- 1) {
2803 char *dep
= kobj_alloc(p
- d
+ 1, KM_WAIT
|KM_TMP
);
2805 (void) strncpy(dep
, d
, p
- d
+ 1);
2808 _kobj_printf(ops
, "%s: dependency ", modp
->mod_modname
);
2809 _kobj_printf(ops
, "'%s' too long ", dep
);
2810 _kobj_printf(ops
, "(max %d chars)\n", modnamelen
);
2812 kobj_free(dep
, p
- d
+ 1);
2818 if ((req
= mod_load_requisite(modp
, modname
)) == NULL
) {
2820 if (_moddebug
& MODDEBUG_LOADMSG
) {
2821 #endif /* KOBJ_DEBUG */
2823 "%s: unable to resolve dependency, ",
2825 _kobj_printf(ops
, "cannot load module '%s'\n",
2829 #endif /* KOBJ_DEBUG */
2830 if (err_modname
== NULL
) {
2832 * This must be the same size as the modname
2835 err_modname
= kobj_zalloc(MODMAXNAMELEN
,
2839 * We can use strcpy() here without fearing
2840 * the NULL terminator because the size of
2841 * err_modname is the same as one of modname,
2842 * and it's filled with zeros.
2844 (void) strcpy(err_modname
, modname
);
2849 add_dependent(mp
, req
->mod_mp
);
2850 mod_release_mod(req
);
2854 if (err_modname
!= NULL
) {
2856 * Copy the first module name where you detect an error to keep
2857 * its behavior the same as before.
2858 * This way keeps minimizing the memory use for error
2859 * modules, and this might be important at boot time because
2860 * the memory usage is a crucial factor for booting in most
2861 * cases. You can expect more verbose messages when using
2862 * a debug kernel or setting a bit in moddebug.
2864 bzero(modname
, MODMAXNAMELEN
);
2865 (void) strcpy(modname
, err_modname
);
2866 kobj_free(err_modname
, MODMAXNAMELEN
);
2874 do_common(struct module
*mp
)
2879 * first time through, assign all symbols defined in other
2880 * modules, and count up how much common space will be needed
2881 * (bss_size and bss_align)
2883 if ((err
= do_symbols(mp
, 0)) < 0)
2886 * increase bss_size by the maximum delta that could be
2887 * computed by the ALIGN below
2889 mp
->bss_size
+= mp
->bss_align
;
2892 mp
->bss
= (uintptr_t)kobj_segbrk(&_edata
, mp
->bss_size
,
2895 mp
->bss
= (uintptr_t)vmem_alloc(data_arena
,
2896 mp
->bss_size
, VM_SLEEP
| VM_BESTFIT
);
2897 bzero((void *)mp
->bss
, mp
->bss_size
);
2898 /* now assign addresses to all common symbols */
2899 if ((err
= do_symbols(mp
, ALIGN(mp
->bss
, mp
->bss_align
))) < 0)
2906 do_symbols(struct module
*mp
, Elf64_Addr bss_base
)
2918 * Nothing left to do (optimization).
2920 if (mp
->flags
& KOBJ_RESOLVED
)
2923 assign
= (bss_base
) ? 1 : 0;
2928 for (i
= 1; i
< mp
->nsyms
; i
++) {
2929 sp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_entsize
* i
);
2931 * we know that st_name is in bounds, since get_sections
2932 * has already checked all of the symbols
2934 name
= mp
->strings
+ sp
->st_name
;
2935 if (sp
->st_shndx
!= SHN_UNDEF
&& sp
->st_shndx
!= SHN_COMMON
)
2938 * TLS symbols are ignored in the kernel
2940 if (ELF_ST_TYPE(sp
->st_info
) == STT_TLS
) {
2941 _kobj_printf(ops
, "%s: TLS symbol ",
2943 _kobj_printf(ops
, "not supported '%s'\n",
2949 if (ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
) {
2950 if ((sp1
= kobj_lookup_all(mp
, name
, 0)) != NULL
) {
2951 sp
->st_shndx
= SHN_ABS
;
2952 sp
->st_value
= sp1
->st_value
;
2957 if (sp
->st_shndx
== SHN_UNDEF
) {
2960 if (strncmp(name
, sdt_prefix
, strlen(sdt_prefix
)) == 0)
2964 * If it's not a weak reference and it's
2965 * not a primary object, it's an error.
2966 * (Primary objects may take more than
2967 * one pass to resolve)
2969 if (!(mp
->flags
& KOBJ_PRIM
) &&
2970 ELF_ST_BIND(sp
->st_info
) != STB_WEAK
) {
2971 _kobj_printf(ops
, "%s: undefined symbol",
2973 _kobj_printf(ops
, " '%s'\n", name
);
2975 * Try to determine whether this symbol
2976 * represents a dependency on obsolete
2977 * unsafe driver support. This is just
2978 * to make the warning more informative.
2980 if (strcmp(name
, "sleep") == 0 ||
2981 strcmp(name
, "unsleep") == 0 ||
2982 strcmp(name
, "wakeup") == 0 ||
2983 strcmp(name
, "bsd_compat_ioctl") == 0 ||
2984 strcmp(name
, "unsafe_driver") == 0 ||
2985 strncmp(name
, "spl", 3) == 0 ||
2986 strncmp(name
, "i_ddi_spl", 9) == 0)
2994 * It's a common symbol - st_value is the
2995 * required alignment.
2997 if (sp
->st_value
> bss_align
)
2998 bss_align
= sp
->st_value
;
2999 bss_ptr
= ALIGN(bss_ptr
, sp
->st_value
);
3001 sp
->st_shndx
= SHN_ABS
;
3002 sp
->st_value
= bss_ptr
;
3004 bss_ptr
+= sp
->st_size
;
3008 if (assign
== 0 && mp
->bss
== (uintptr_t)NULL
) {
3009 mp
->bss_align
= bss_align
;
3010 mp
->bss_size
= bss_ptr
;
3011 } else if (resolved
) {
3012 mp
->flags
|= KOBJ_RESOLVED
;
3019 kobj_hash_name(const char *p
)
3026 hval
= (hval
<< 4) + *p
++;
3027 if ((g
= (hval
& 0xf0000000)) != 0)
3034 /* look for name in all modules */
3036 kobj_getsymvalue(char *name
, int kernelonly
)
3039 struct modctl
*modp
;
3041 uintptr_t value
= 0;
3043 if ((sp
= kobj_lookup_kernel(name
)) != NULL
)
3044 return ((uintptr_t)sp
->st_value
);
3047 return (0); /* didn't find it in the kernel so give up */
3049 mutex_enter(&mod_lock
);
3052 mp
= (struct module
*)modp
->mod_mp
;
3053 if (mp
&& !(mp
->flags
& KOBJ_PRIM
) && modp
->mod_loaded
&&
3054 (sp
= lookup_one(mp
, name
))) {
3055 value
= (uintptr_t)sp
->st_value
;
3058 } while ((modp
= modp
->mod_next
) != &modules
);
3059 mutex_exit(&mod_lock
);
3063 /* look for a symbol near value. */
3065 kobj_getsymname(uintptr_t value
, ulong_t
*offset
)
3068 struct modctl
*modp
;
3070 struct modctl_list
*lp
;
3074 * Loop through the primary kernel modules.
3076 for (lp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); lp
; lp
= lp
->modl_next
) {
3079 if ((name
= kobj_searchsym(mp
, value
, offset
)) != NULL
)
3083 mutex_enter(&mod_lock
);
3086 mp
= (struct module
*)modp
->mod_mp
;
3087 if (mp
&& !(mp
->flags
& KOBJ_PRIM
) && modp
->mod_loaded
&&
3088 (name
= kobj_searchsym(mp
, value
, offset
)))
3090 } while ((modp
= modp
->mod_next
) != &modules
);
3091 mutex_exit(&mod_lock
);
3095 /* return address of symbol and size */
3098 kobj_getelfsym(char *name
, void *mp
, int *size
)
3103 sp
= kobj_lookup_kernel(name
);
3105 sp
= lookup_one(mp
, name
);
3110 *size
= (int)sp
->st_size
;
3111 return ((uintptr_t)sp
->st_value
);
3115 kobj_lookup(struct module
*mod
, const char *name
)
3119 sp
= lookup_one(mod
, name
);
3124 return ((uintptr_t)sp
->st_value
);
3128 kobj_searchsym(struct module
*mp
, uintptr_t value
, ulong_t
*offset
)
3137 *offset
= (ulong_t
)-1l; /* assume not found */
3140 if (kobj_addrcheck(mp
, (void *)value
) != 0)
3141 return (NULL
); /* not in this module */
3143 strtabptr
= mp
->strings
;
3144 symtabptr
= (Sym
*)mp
->symtbl
;
3147 * Scan the module's symbol table for a symbol <= value
3149 for (symnum
= 1, sym
= symtabptr
+ 1;
3150 symnum
< mp
->nsyms
; symnum
++, sym
= (Sym
*)
3151 ((uintptr_t)sym
+ mp
->symhdr
->sh_entsize
)) {
3152 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
) {
3153 if (ELF_ST_BIND(sym
->st_info
) != STB_LOCAL
)
3155 if (ELF_ST_TYPE(sym
->st_info
) != STT_OBJECT
&&
3156 ELF_ST_TYPE(sym
->st_info
) != STT_FUNC
)
3160 curval
= (uintptr_t)sym
->st_value
;
3166 * If one or both are functions...
3168 if (ELF_ST_TYPE(sym
->st_info
) == STT_FUNC
|| (cursym
!= NULL
&&
3169 ELF_ST_TYPE(cursym
->st_info
) == STT_FUNC
)) {
3170 /* Ignore if the address is out of the bounds */
3171 if (value
- sym
->st_value
>= sym
->st_size
)
3174 if (cursym
!= NULL
&&
3175 ELF_ST_TYPE(cursym
->st_info
) == STT_FUNC
) {
3176 /* Prefer the function to the non-function */
3177 if (ELF_ST_TYPE(sym
->st_info
) != STT_FUNC
)
3180 /* Prefer the larger of the two functions */
3181 if (sym
->st_size
<= cursym
->st_size
)
3184 } else if (value
- curval
>= *offset
) {
3188 *offset
= (ulong_t
)(value
- curval
);
3194 return (strtabptr
+ cursym
->st_name
);
3198 kobj_lookup_all(struct module
*mp
, char *name
, int include_self
)
3201 struct module_list
*mlp
;
3202 struct modctl_list
*clp
;
3205 if (include_self
&& (sp
= lookup_one(mp
, name
)) != NULL
)
3208 for (mlp
= mp
->head
; mlp
; mlp
= mlp
->next
) {
3209 if ((sp
= lookup_one(mlp
->mp
, name
)) != NULL
&&
3210 ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
)
3215 * Loop through the primary kernel modules.
3217 for (clp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); clp
; clp
= clp
->modl_next
) {
3220 if (mmp
== NULL
|| mp
== mmp
)
3223 if ((sp
= lookup_one(mmp
, name
)) != NULL
&&
3224 ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
)
3231 kobj_lookup_kernel(const char *name
)
3233 struct modctl_list
*lp
;
3238 * Loop through the primary kernel modules.
3240 for (lp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); lp
; lp
= lp
->modl_next
) {
3246 if ((sp
= lookup_one(mp
, name
)) != NULL
)
3253 lookup_one(struct module
*mp
, const char *name
)
3259 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3260 ip
= &mp
->chains
[*ip
]) {
3261 sp
= (Sym
*)(mp
->symtbl
+
3262 mp
->symhdr
->sh_entsize
* *ip
);
3263 name1
= mp
->strings
+ sp
->st_name
;
3264 if (strcmp(name
, name1
) == 0 &&
3265 ELF_ST_TYPE(sp
->st_info
) != STT_FILE
&&
3266 sp
->st_shndx
!= SHN_UNDEF
&&
3267 sp
->st_shndx
!= SHN_COMMON
)
3274 * Lookup a given symbol pointer in the module's symbol hash. If the symbol
3275 * is hashed, return the symbol pointer; otherwise return NULL.
3278 sym_lookup(struct module
*mp
, Sym
*ksp
)
3280 char *name
= mp
->strings
+ ksp
->st_name
;
3284 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3285 ip
= &mp
->chains
[*ip
]) {
3286 sp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_entsize
* *ip
);
3294 sym_insert(struct module
*mp
, char *name
, symid_t index
)
3299 if (kobj_debug
& D_SYMBOLS
) {
3300 static struct module
*lastmp
= NULL
;
3304 "krtld: symbol entry: file=%s\n",
3307 "krtld:\tsymndx\tvalue\t\t"
3311 sp
= (Sym
*)(mp
->symtbl
+
3312 index
* mp
->symhdr
->sh_entsize
);
3313 _kobj_printf(ops
, "krtld:\t[%3d]", index
);
3314 _kobj_printf(ops
, "\t0x%lx", sp
->st_value
);
3315 _kobj_printf(ops
, "\t%s\n", name
);
3319 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3320 ip
= &mp
->chains
[*ip
]) {
3327 kobj_boot_mod_lookup(const char *modname
)
3329 struct modctl
*mctl
= kobj_modules
;
3332 if (strcmp(modname
, mctl
->mod_modname
) == 0)
3334 } while ((mctl
= mctl
->mod_next
) != kobj_modules
);
3340 * Determine if the module exists.
3343 kobj_path_exists(char *name
, int use_path
)
3347 file
= kobj_open_path(name
, use_path
);
3348 if (file
== (struct _buf
*)-1)
3350 kobj_close_file(file
);
3355 * fullname is dynamically allocated to be able to hold the
3356 * maximum size string that can be constructed from name.
3357 * path is exactly like the shell PATH variable.
3360 kobj_open_path(char *name
, int use_path
)
3370 pathp
= ""; /* use name as specified */
3372 pathp
= kobj_module_path
;
3373 /* use configured default path */
3375 pathpsave
= pathp
; /* keep this for error reporting */
3378 * Allocate enough space for the largest possible fullname.
3379 * since path is of the form <directory> : <directory> : ...
3380 * we're potentially allocating a little more than we need to
3381 * but we'll allocate the exact amount when we find the right directory.
3382 * (The + 3 below is one for NULL terminator and one for the '/'
3383 * we might have to add at the beginning of path and one for
3384 * the '/' between path and name.)
3386 maxpathlen
= strlen(pathp
) + strlen(name
) + 3;
3387 fullname
= kobj_zalloc(maxpathlen
, KM_WAIT
);
3391 if (*pathp
!= '\0' && *pathp
!= '/')
3392 *p
++ = '/'; /* path must start with '/' */
3393 while (*pathp
&& *pathp
!= ':' && *pathp
!= ' ')
3395 if (p
!= fullname
&& p
[-1] != '/')
3401 if ((file
= kobj_open_file(fullname
)) != (struct _buf
*)-1) {
3402 kobj_free(fullname
, maxpathlen
);
3405 while (*pathp
== ' ' || *pathp
== ':')
3411 kobj_free(fullname
, maxpathlen
);
3412 if (_moddebug
& MODDEBUG_ERRMSG
) {
3413 _kobj_printf(ops
, "can't open %s,", name
);
3414 _kobj_printf(ops
, " path is %s\n", pathpsave
);
3416 return ((struct _buf
*)-1);
3420 kobj_open(char *filename
)
3425 if (_modrootloaded
) {
3426 struct kobjopen_tctl
*ltp
= kobjopen_alloc(filename
);
3430 * Hand off the open to a thread who has a
3431 * stack size capable handling the request.
3433 if (curthread
!= &t0
) {
3434 (void) thread_create(NULL
, DEFAULTSTKSZ
* 2,
3435 kobjopen_thread
, ltp
, 0, &p0
, TS_RUN
, maxclsyspri
);
3441 * 1098067: module creds should not be those of the
3444 cred_t
*saved_cred
= curthread
->t_cred
;
3445 curthread
->t_cred
= kcred
;
3446 Errno
= vn_openat(filename
, UIO_SYSSPACE
, FREAD
, 0, &vp
,
3448 curthread
->t_cred
= saved_cred
;
3453 if (_moddebug
& MODDEBUG_ERRMSG
) {
3455 "kobj_open: vn_open of %s fails, ",
3457 _kobj_printf(ops
, "Errno = %d\n", Errno
);
3461 if (_moddebug
& MODDEBUG_ERRMSG
) {
3462 _kobj_printf(ops
, "kobj_open: '%s'", filename
);
3463 _kobj_printf(ops
, " vp = %p\n", vp
);
3465 return ((intptr_t)vp
);
3468 fd
= kobj_boot_open(filename
, 0);
3470 if (_moddebug
& MODDEBUG_ERRMSG
) {
3473 "kobj_open: can't open %s\n", filename
);
3475 _kobj_printf(ops
, "kobj_open: '%s'", filename
);
3476 _kobj_printf(ops
, " descr = 0x%x\n", fd
);
3479 return ((intptr_t)fd
);
3484 * Calls to kobj_open() are handled off to this routine as a separate thread.
3487 kobjopen_thread(struct kobjopen_tctl
*ltp
)
3492 mutex_init(&cpr_lk
, NULL
, MUTEX_DEFAULT
, NULL
);
3493 CALLB_CPR_INIT(&cpr_i
, &cpr_lk
, callb_generic_cpr
, "kobjopen");
3494 ltp
->Errno
= vn_open(ltp
->name
, UIO_SYSSPACE
, FREAD
, 0, &(ltp
->vp
),
3497 mutex_enter(&cpr_lk
);
3498 CALLB_CPR_EXIT(&cpr_i
);
3499 mutex_destroy(&cpr_lk
);
3504 * allocate and initialize a kobjopen thread structure
3506 static struct kobjopen_tctl
*
3507 kobjopen_alloc(char *filename
)
3509 struct kobjopen_tctl
*ltp
= kmem_zalloc(sizeof (*ltp
), KM_SLEEP
);
3511 ASSERT(filename
!= NULL
);
3513 ltp
->name
= kmem_alloc(strlen(filename
) + 1, KM_SLEEP
);
3514 bcopy(filename
, ltp
->name
, strlen(filename
) + 1);
3515 sema_init(<p
->sema
, 0, NULL
, SEMA_DEFAULT
, NULL
);
3520 * free a kobjopen thread control structure
3523 kobjopen_free(struct kobjopen_tctl
*ltp
)
3525 sema_destroy(<p
->sema
);
3526 kmem_free(ltp
->name
, strlen(ltp
->name
) + 1);
3527 kmem_free(ltp
, sizeof (*ltp
));
3531 kobj_read(intptr_t descr
, char *buf
, uint_t size
, uint_t offset
)
3536 if (_modrootloaded
) {
3537 if ((stat
= vn_rdwr(UIO_READ
, (struct vnode
*)descr
, buf
, size
,
3538 (offset_t
)offset
, UIO_SYSSPACE
, 0, 0, CRED(),
3541 "vn_rdwr failed with error 0x%x\n", stat
);
3544 return (size
- resid
);
3548 if (kobj_boot_seek((int)descr
, (off_t
)0, offset
) != 0) {
3550 "kobj_read: seek 0x%x failed\n", offset
);
3554 count
= kobj_boot_read((int)descr
, buf
, size
);
3556 if (_moddebug
& MODDEBUG_ERRMSG
) {
3558 "kobj_read: req %d bytes, ", size
);
3559 _kobj_printf(ops
, "got %d\n", count
);
3567 kobj_close(intptr_t descr
)
3569 if (_moddebug
& MODDEBUG_ERRMSG
)
3570 _kobj_printf(ops
, "kobj_close: 0x%lx\n", descr
);
3572 if (_modrootloaded
) {
3573 struct vnode
*vp
= (struct vnode
*)descr
;
3574 (void) fop_close(vp
, FREAD
, 1, 0, CRED(), NULL
);
3577 (void) kobj_boot_close((int)descr
);
3581 kobj_fstat(intptr_t descr
, struct bootstat
*buf
)
3586 if (_modrootloaded
) {
3588 struct vnode
*vp
= (struct vnode
*)descr
;
3589 if (fop_getattr(vp
, &vattr
, 0, kcred
, NULL
) != 0)
3593 * The vattr and bootstat structures are similar, but not
3594 * identical. We do our best to fill in the bootstat structure
3595 * from the contents of vattr (transfering only the ones that
3599 buf
->st_mode
= (uint32_t)vattr
.va_mode
;
3600 buf
->st_nlink
= (uint32_t)vattr
.va_nlink
;
3601 buf
->st_uid
= (int32_t)vattr
.va_uid
;
3602 buf
->st_gid
= (int32_t)vattr
.va_gid
;
3603 buf
->st_rdev
= (uint64_t)vattr
.va_rdev
;
3604 buf
->st_size
= (uint64_t)vattr
.va_size
;
3605 buf
->st_atim
.tv_sec
= (int64_t)vattr
.va_atime
.tv_sec
;
3606 buf
->st_atim
.tv_nsec
= (int64_t)vattr
.va_atime
.tv_nsec
;
3607 buf
->st_mtim
.tv_sec
= (int64_t)vattr
.va_mtime
.tv_sec
;
3608 buf
->st_mtim
.tv_nsec
= (int64_t)vattr
.va_mtime
.tv_nsec
;
3609 buf
->st_ctim
.tv_sec
= (int64_t)vattr
.va_ctime
.tv_sec
;
3610 buf
->st_ctim
.tv_nsec
= (int64_t)vattr
.va_ctime
.tv_nsec
;
3611 buf
->st_blksize
= (int32_t)vattr
.va_blksize
;
3612 buf
->st_blocks
= (int64_t)vattr
.va_nblocks
;
3617 return (kobj_boot_fstat((int)descr
, buf
));
3622 kobj_open_file(char *name
)
3625 struct compinfo cbuf
;
3628 if ((fd
= kobj_open(name
)) == -1) {
3629 return ((struct _buf
*)-1);
3632 file
= kobj_zalloc(sizeof (struct _buf
), KM_WAIT
|KM_TMP
);
3634 file
->_name
= kobj_alloc(strlen(name
)+1, KM_WAIT
|KM_TMP
);
3635 file
->_cnt
= file
->_size
= file
->_off
= 0;
3637 file
->_ptr
= file
->_base
;
3638 (void) strcpy(file
->_name
, name
);
3641 * Before root is mounted, we must check
3642 * for a compressed file and do our own
3645 if (_modrootloaded
) {
3646 file
->_base
= kobj_zalloc(MAXBSIZE
, KM_WAIT
);
3647 file
->_bsize
= MAXBSIZE
;
3649 /* Check if the file is compressed */
3650 file
->_iscmp
= kobj_is_compressed(fd
);
3652 if (kobj_boot_compinfo(fd
, &cbuf
) != 0) {
3653 kobj_close_file(file
);
3654 return ((struct _buf
*)-1);
3656 file
->_iscmp
= cbuf
.iscmp
;
3658 if (kobj_comp_setup(file
, &cbuf
) != 0) {
3659 kobj_close_file(file
);
3660 return ((struct _buf
*)-1);
3663 file
->_base
= kobj_zalloc(cbuf
.blksize
, KM_WAIT
|KM_TMP
);
3664 file
->_bsize
= cbuf
.blksize
;
3671 kobj_comp_setup(struct _buf
*file
, struct compinfo
*cip
)
3673 struct comphdr
*hdr
;
3676 * read the compressed image into memory,
3677 * so we can deompress from there
3679 file
->_dsize
= cip
->fsize
;
3680 file
->_dbuf
= kobj_alloc(cip
->fsize
, KM_WAIT
|KM_TMP
);
3681 if (kobj_read(file
->_fd
, file
->_dbuf
, cip
->fsize
, 0) != cip
->fsize
) {
3682 kobj_free(file
->_dbuf
, cip
->fsize
);
3686 hdr
= kobj_comphdr(file
);
3687 if (hdr
->ch_magic
!= CH_MAGIC_ZLIB
|| hdr
->ch_version
!= CH_VERSION
||
3688 hdr
->ch_algorithm
!= CH_ALG_ZLIB
|| hdr
->ch_fsize
== 0 ||
3689 !ISP2(hdr
->ch_blksize
)) {
3690 kobj_free(file
->_dbuf
, cip
->fsize
);
3693 file
->_base
= kobj_alloc(hdr
->ch_blksize
, KM_WAIT
|KM_TMP
);
3694 file
->_bsize
= hdr
->ch_blksize
;
3699 kobj_close_file(struct _buf
*file
)
3701 kobj_close(file
->_fd
);
3702 if (file
->_base
!= NULL
)
3703 kobj_free(file
->_base
, file
->_bsize
);
3704 if (file
->_dbuf
!= NULL
)
3705 kobj_free(file
->_dbuf
, file
->_dsize
);
3706 kobj_free(file
->_name
, strlen(file
->_name
)+1);
3707 kobj_free(file
, sizeof (struct _buf
));
3711 kobj_read_file(struct _buf
*file
, char *buf
, uint_t size
, uint_t off
)
3714 int b_off
; /* Offset into buffer for start of bcopy */
3718 if (_moddebug
& MODDEBUG_ERRMSG
) {
3719 _kobj_printf(ops
, "kobj_read_file: size=%x,", size
);
3720 _kobj_printf(ops
, " offset=%x at", off
);
3721 _kobj_printf(ops
, " buf=%x\n", buf
);
3725 * Handle compressed (gzip for now) file here. First get the
3726 * compressed size, then read the image into memory and finally
3727 * call zlib to decompress the image at the supplied memory buffer.
3729 if (file
->_iscmp
== CH_MAGIC_GZIP
) {
3732 struct vnode
*vp
= (struct vnode
*)file
->_fd
;
3736 if (fop_getattr(vp
, &vattr
, 0, kcred
, NULL
) != 0)
3739 file
->_dbuf
= kobj_alloc(vattr
.va_size
, KM_WAIT
|KM_TMP
);
3740 file
->_dsize
= vattr
.va_size
;
3742 /* Read the compressed file into memory */
3743 if ((err
= vn_rdwr(UIO_READ
, vp
, file
->_dbuf
, vattr
.va_size
,
3744 (offset_t
)(0), UIO_SYSSPACE
, 0, 0, CRED(),
3747 _kobj_printf(ops
, "kobj_read_file :vn_rdwr() failed, "
3748 "error code 0x%x\n", err
);
3754 /* Decompress the image at the supplied memory buffer */
3755 if ((err
= z_uncompress(buf
, &dlen
, file
->_dbuf
,
3756 vattr
.va_size
)) != Z_OK
) {
3757 _kobj_printf(ops
, "kobj_read_file: z_uncompress "
3758 "failed, error code : 0x%x\n", err
);
3763 _kobj_printf(ops
, "kobj_read_file: z_uncompress "
3764 "failed to uncompress (size returned 0x%x , "
3765 "expected size: 0x%x)\n", dlen
, size
);
3773 page_addr
= F_PAGE(file
, off
);
3774 b_size
= file
->_size
;
3776 * If we have the filesystem page the caller's referring to
3777 * and we have something in the buffer,
3778 * satisfy as much of the request from the buffer as we can.
3780 if (page_addr
== file
->_off
&& b_size
> 0) {
3781 b_off
= B_OFFSET(file
, off
);
3782 c_size
= b_size
- b_off
;
3784 * If there's nothing to copy, we're at EOF.
3791 if (_moddebug
& MODDEBUG_ERRMSG
)
3792 _kobj_printf(ops
, "copying %x bytes\n",
3794 bcopy(file
->_base
+b_off
, buf
, c_size
);
3800 _kobj_printf(ops
, "kobj_read: system error");
3806 * If the caller's offset is page aligned and
3807 * the caller want's at least a filesystem page and
3808 * the caller provided a buffer,
3809 * read directly into the caller's buffer.
3811 if (page_addr
== off
&&
3812 (c_size
= F_BLKS(file
, size
)) && buf
) {
3813 c_size
= kobj_read_blks(file
, buf
, c_size
,
3820 if (c_size
!= F_BLKS(file
, size
))
3826 * Otherwise, read into our buffer and copy next time
3830 file
->_off
= page_addr
;
3831 c_size
= kobj_read_blks(file
, file
->_base
,
3832 file
->_bsize
, page_addr
);
3833 file
->_ptr
= file
->_base
;
3834 file
->_cnt
= c_size
;
3835 file
->_size
= c_size
;
3837 * If a _filbuf call or nothing read, break.
3839 if (buf
== NULL
|| c_size
<= 0) {
3844 if (_moddebug
& MODDEBUG_ERRMSG
)
3845 _kobj_printf(ops
, "read %x bytes\n", c_size
);
3848 if (_moddebug
& MODDEBUG_ERRMSG
)
3849 _kobj_printf(ops
, "count = %x\n", count
);
3855 kobj_read_blks(struct _buf
*file
, char *buf
, uint_t size
, uint_t off
)
3859 ASSERT(B_OFFSET(file
, size
) == 0 && B_OFFSET(file
, off
) == 0);
3865 for (blks
= size
/ file
->_bsize
; blks
!= 0; blks
--) {
3866 nret
= kobj_uncomp_blk(file
, buf
, off
);
3872 if (nret
< file
->_bsize
)
3876 ret
= kobj_read(file
->_fd
, buf
, size
, off
);
3881 kobj_uncomp_blk(struct _buf
*file
, char *buf
, uint_t off
)
3883 struct comphdr
*hdr
= kobj_comphdr(file
);
3888 dlen
= file
->_bsize
;
3889 i
= off
/ file
->_bsize
;
3890 src
= file
->_dbuf
+ hdr
->ch_blkmap
[i
];
3891 if (i
== hdr
->ch_fsize
/ file
->_bsize
)
3892 slen
= file
->_dsize
- hdr
->ch_blkmap
[i
];
3894 slen
= hdr
->ch_blkmap
[i
+ 1] - hdr
->ch_blkmap
[i
];
3895 if (z_uncompress(buf
, &dlen
, src
, slen
) != Z_OK
)
3901 kobj_filbuf(struct _buf
*f
)
3903 if (kobj_read_file(f
, NULL
, f
->_bsize
, f
->_off
+ f
->_size
) > 0)
3904 return (kobj_getc(f
));
3909 kobj_free(void *address
, size_t size
)
3914 kmem_free(address
, size
);
3915 kobj_stat
.nfree_calls
++;
3916 kobj_stat
.nfree
+= size
;
3920 kobj_zalloc(size_t size
, int flag
)
3924 if ((v
= kobj_alloc(size
, flag
)) != 0) {
3932 kobj_alloc(size_t size
, int flag
)
3935 * If we are running standalone in the
3936 * linker, we ask boot for memory.
3937 * Either it's temporary memory that we lose
3938 * once boot is mapped out or we allocate it
3939 * permanently using the dynamic data segment.
3943 if (flag
& (KM_TMP
| KM_SCRATCH
))
3944 return (bop_temp_alloc(size
, MINALIGN
));
3946 if (flag
& (KM_TMP
| KM_SCRATCH
))
3947 return (BOP_ALLOC(ops
, 0, size
, MINALIGN
));
3949 return (kobj_segbrk(&_edata
, size
, MINALIGN
, 0));
3952 kobj_stat
.nalloc_calls
++;
3953 kobj_stat
.nalloc
+= size
;
3955 return (kmem_alloc(size
, (flag
& KM_NOWAIT
) ? KM_NOSLEEP
: KM_SLEEP
));
3959 * Allow the "mod" system to sync up with the work
3960 * already done by kobj during the initial loading
3961 * of the kernel. This also gives us a chance
3962 * to reallocate memory that belongs to boot.
3967 struct modctl_list
*lp
, **lpp
;
3970 * The module path can be set in /etc/system via 'moddir' commands
3972 if (default_path
!= NULL
)
3973 kobj_module_path
= default_path
;
3975 default_path
= kobj_module_path
;
3977 ksyms_arena
= vmem_create("ksyms", NULL
, 0, sizeof (uint64_t),
3978 segkmem_alloc
, segkmem_free
, heap_arena
, 0, VM_SLEEP
);
3980 ctf_arena
= vmem_create("ctf", NULL
, 0, sizeof (uint_t
),
3981 segkmem_alloc
, segkmem_free
, heap_arena
, 0, VM_SLEEP
);
3984 * Move symbol tables from boot memory to ksyms_arena.
3986 for (lpp
= kobj_linkmaps
; *lpp
!= NULL
; lpp
++) {
3987 for (lp
= *lpp
; lp
!= NULL
; lp
= lp
->modl_next
)
3988 kobj_export_module(mod(lp
));
3993 kobj_segbrk(caddr_t
*spp
, size_t size
, size_t align
, caddr_t limit
)
3996 size_t alloc_pgsz
= kobj_mmu_pagesize
;
3997 size_t alloc_align
= BO_NO_ALIGN
;
4001 * If we are using "large" mappings for the kernel,
4002 * request aligned memory from boot using the
4006 alloc_align
= lg_pagesize
;
4007 alloc_pgsz
= lg_pagesize
;
4011 va
= ALIGN((uintptr_t)*spp
, align
);
4012 pva
= P2ROUNDUP((uintptr_t)*spp
, alloc_pgsz
);
4016 if (va
+ size
> pva
) {
4019 alloc_size
= P2ROUNDUP(size
- (pva
- va
), alloc_pgsz
);
4021 * Check for overlapping segments.
4023 if (limit
&& limit
<= *spp
+ alloc_size
) {
4024 return ((caddr_t
)0);
4027 npva
= (uintptr_t)BOP_ALLOC(ops
, (caddr_t
)pva
,
4028 alloc_size
, alloc_align
);
4030 if (npva
== (uintptr_t)NULL
) {
4031 _kobj_printf(ops
, "BOP_ALLOC failed, 0x%lx bytes",
4033 _kobj_printf(ops
, " aligned %lx", alloc_align
);
4034 _kobj_printf(ops
, " at 0x%lx\n", pva
);
4038 *spp
= (caddr_t
)(va
+ size
);
4040 return ((caddr_t
)va
);
4044 * Calculate the number of output hash buckets.
4045 * We use the next prime larger than n / 4,
4046 * so the average hash chain is about 4 entries.
4047 * More buckets would just be a waste of memory.
4050 kobj_gethashsize(uint_t n
)
4053 int hsize
= MAX(n
/ 4, 2);
4055 for (f
= 2; f
* f
<= hsize
; f
++)
4063 * Get the file size.
4065 * Before root is mounted, files are compressed in the boot_archive ramdisk
4066 * (in the memory). kobj_fstat would return the compressed file size.
4067 * In order to get the uncompressed file size, read the file to the end and
4071 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
4077 if (_modrootloaded
) {
4078 struct bootstat bst
;
4080 if (kobj_fstat(file
->_fd
, &bst
) != 0)
4082 *size
= bst
.st_size
;
4084 if (file
->_iscmp
== CH_MAGIC_GZIP
) {
4086 * Read the last 4 bytes of the compressed (gzip)
4087 * image to get the size of its uncompressed
4090 if ((err
= vn_rdwr(UIO_READ
, (struct vnode
*)file
->_fd
,
4091 (char *)(&buf
), 4, (offset_t
)(*size
- 4),
4092 UIO_SYSSPACE
, 0, 0, CRED(), &resid
))
4094 _kobj_printf(ops
, "kobj_get_filesize: "
4095 "vn_rdwr() failed with error 0x%x\n", err
);
4099 *size
= (uint64_t)buf
;
4104 struct bootstat bsb
;
4107 struct comphdr
*hdr
= kobj_comphdr(file
);
4109 *size
= hdr
->ch_fsize
;
4110 } else if (kobj_boot_fstat(file
->_fd
, &bsb
) != 0)
4113 *size
= bsb
.st_size
;
4117 uint64_t offset
= 0;
4119 buf
= kmem_alloc(MAXBSIZE
, KM_SLEEP
);
4121 count
= kobj_read_file(file
, buf
, MAXBSIZE
, offset
);
4123 kmem_free(buf
, MAXBSIZE
);
4127 } while (count
== MAXBSIZE
);
4128 kmem_free(buf
, MAXBSIZE
);
4148 return (q
? q
+ 1 : s
);
4152 kobj_stat_get(kobj_stat_t
*kp
)
4160 return (lg_pagesize
);
4164 kobj_textwin_alloc(struct module
*mp
)
4166 ASSERT(MUTEX_HELD(&mod_lock
));
4168 if (mp
->textwin
!= NULL
)
4172 * If the text is not contained in the heap, then it is not contained
4173 * by a writable mapping. (Specifically, it's on the nucleus page.)
4174 * We allocate a read/write mapping for this module's text to allow
4175 * the text to be patched without calling hot_patch_kernel_text()
4176 * (which is quite slow).
4178 if (!vmem_contains(heaptext_arena
, mp
->text
, mp
->text_size
)) {
4179 uintptr_t text
= (uintptr_t)mp
->text
;
4180 uintptr_t size
= (uintptr_t)mp
->text_size
;
4183 size_t sz
= ((text
+ size
+ PAGESIZE
- 1) & PAGEMASK
) -
4186 va
= mp
->textwin_base
= vmem_alloc(heap_arena
, sz
, VM_SLEEP
);
4188 for (i
= text
& PAGEMASK
; i
< text
+ size
; i
+= PAGESIZE
) {
4189 hat_devload(kas
.a_hat
, va
, PAGESIZE
,
4190 hat_getpfnum(kas
.a_hat
, (caddr_t
)i
),
4191 PROT_READ
| PROT_WRITE
,
4192 HAT_LOAD_LOCK
| HAT_LOAD_NOCONSIST
);
4196 mp
->textwin
= mp
->textwin_base
+ (text
& PAGEOFFSET
);
4198 mp
->textwin
= mp
->text
;
4203 kobj_textwin_free(struct module
*mp
)
4205 uintptr_t text
= (uintptr_t)mp
->text
;
4206 uintptr_t tsize
= (uintptr_t)mp
->text_size
;
4207 size_t size
= (((text
+ tsize
+ PAGESIZE
- 1) & PAGEMASK
) -
4212 if (mp
->textwin_base
== NULL
)
4215 hat_unload(kas
.a_hat
, mp
->textwin_base
, size
, HAT_UNLOAD_UNLOCK
);
4216 vmem_free(heap_arena
, mp
->textwin_base
, size
);
4217 mp
->textwin_base
= NULL
;
4221 find_libmacro(char *name
)
4225 for (lmi
= 0; lmi
< NLIBMACROS
; lmi
++) {
4226 if (strcmp(name
, libmacros
[lmi
].lmi_macroname
) == 0)
4227 return (libmacros
[lmi
].lmi_list
);
4233 * Check for $MACRO in tail (string to expand) and expand it in path at pathend
4234 * returns path if successful, else NULL
4235 * Support multiple $MACROs expansion and the first valid path will be returned
4236 * Caller's responsibility to provide enough space in path to expand
4239 expand_libmacro(char *tail
, char *path
, char *pathend
)
4241 char c
, *p
, *p1
, *p2
, *path2
, *endp
;
4242 int diff
, lmi
, macrolen
, valid_macro
, more_macro
;
4246 * check for $MACROS between nulls or slashes
4248 p
= strchr(tail
, '$');
4251 for (lmi
= 0; lmi
< NLIBMACROS
; lmi
++) {
4252 macrolen
= libmacros
[lmi
].lmi_macrolen
;
4253 if (strncmp(p
+ 1, libmacros
[lmi
].lmi_macroname
, macrolen
) == 0)
4258 if (lmi
< NLIBMACROS
) {
4260 * The following checks are used to restrict expansion of
4261 * macros to those that form a full directory/file name
4262 * and to keep the behavior same as before. If this
4263 * restriction is removed or no longer valid in the future,
4264 * the checks below can be deleted.
4266 if ((p
== tail
) || (*(p
- 1) == '/')) {
4267 c
= *(p
+ macrolen
+ 1);
4268 if (c
== '/' || c
== '\0')
4274 p2
= strchr(p
, '/');
4276 * if no more macro to expand, then just copy whatever left
4277 * and check whether it exists
4279 if (p2
== NULL
|| strchr(p2
, '$') == NULL
) {
4280 (void) strcpy(pathend
, tail
);
4281 if ((file
= kobj_open_path(path
, 1)) !=
4282 (struct _buf
*)-1) {
4283 kobj_close_file(file
);
4289 * copy all chars before '/' and call expand_libmacro()
4293 bcopy(tail
, pathend
, diff
);
4296 return (expand_libmacro(p2
, path
, pathend
));
4302 endp
= p
+ macrolen
+ 1;
4303 if (strchr(endp
, '$') != NULL
)
4309 * copy lmi_list and split it into components.
4310 * then put the part of tail before $MACRO into path
4315 bcopy(tail
, pathend
, diff
);
4316 path2
= pathend
+ diff
;
4317 p1
= libmacros
[lmi
].lmi_list
;
4318 while (p1
&& (*p1
!= '\0')) {
4319 p2
= strchr(p1
, ':');
4322 bcopy(p1
, path2
, diff
);
4323 *(path2
+ diff
) = '\0';
4326 bcopy(p1
, path2
, diff
+ 1);
4328 /* copy endp only if there isn't any more macro to expand */
4329 if (!more_macro
&& (endp
!= NULL
))
4330 (void) strcat(path2
, endp
);
4331 file
= kobj_open_path(path
, 1);
4332 if (file
!= (struct _buf
*)-1) {
4333 kobj_close_file(file
);
4335 * if more macros to expand then call expand_libmacro(),
4336 * else return path which has the whole path
4338 if (!more_macro
|| (expand_libmacro(endp
, path
,
4339 path2
+ diff
) != NULL
)) {
4352 tnf_add_notifyunload(kobj_notify_f
*fp
)
4354 kobj_notify_list_t
*entry
;
4356 entry
= kobj_alloc(sizeof (kobj_notify_list_t
), KM_WAIT
);
4357 entry
->kn_type
= KOBJ_NOTIFY_MODUNLOADING
;
4358 entry
->kn_func
= fp
;
4359 (void) kobj_notify_add(entry
);
4364 tnf_unsplice_probes(uint_t what
, struct modctl
*mod
)
4366 tnf_probe_control_t
**p
;
4368 struct module
*mp
= mod
->mod_mp
;
4370 if (!(mp
->flags
& KOBJ_TNF_PROBE
))
4373 for (p
= &__tnf_probe_list_head
; *p
; )
4374 if (kobj_addrcheck(mp
, (char *)*p
) == 0)
4379 for (q
= &__tnf_tag_list_head
; *q
; )
4380 if (kobj_addrcheck(mp
, (char *)*q
) == 0)
4381 *q
= (tnf_tag_data_t
*)(*q
)->tag_version
;
4383 q
= (tnf_tag_data_t
**)&(*q
)->tag_version
;
4385 tnf_changed_probe_list
= 1;
4389 tnf_splice_probes(int boot_load
, tnf_probe_control_t
*plist
,
4390 tnf_tag_data_t
*tlist
)
4393 static int add_notify
= 1;
4396 tnf_probe_control_t
*pl
;
4398 for (pl
= plist
; pl
->next
; )
4402 mutex_enter(&mod_lock
);
4403 tnf_changed_probe_list
= 1;
4404 pl
->next
= __tnf_probe_list_head
;
4405 __tnf_probe_list_head
= plist
;
4407 mutex_exit(&mod_lock
);
4414 for (tl
= tlist
; tl
->tag_version
; )
4415 tl
= (tnf_tag_data_t
*)tl
->tag_version
;
4418 mutex_enter(&mod_lock
);
4419 tl
->tag_version
= (tnf_tag_version_t
*)__tnf_tag_list_head
;
4420 __tnf_tag_list_head
= tlist
;
4422 mutex_exit(&mod_lock
);
4425 if (!boot_load
&& result
&& add_notify
) {
4426 tnf_add_notifyunload(tnf_unsplice_probes
);
4432 char *kobj_file_buf
;
4433 int kobj_file_bufsize
;
4436 * This code is for the purpose of manually recording which files
4437 * needs to go into the boot archive on any given system.
4439 * To enable the code, set kobj_file_bufsize in /etc/system
4440 * and reboot the system, then use mdb to look at kobj_file_buf.
4443 kobj_record_file(char *filename
)
4446 static int size
= 0;
4449 if (kobj_file_bufsize
== 0) /* don't bother */
4452 if (kobj_file_buf
== NULL
) { /* allocate buffer */
4453 size
= kobj_file_bufsize
;
4454 buf
= kobj_file_buf
= kobj_alloc(size
, KM_WAIT
|KM_TMP
);
4457 n
= snprintf(buf
, size
, "%s\n", filename
);
4465 kobj_boot_fstat(int fd
, struct bootstat
*stp
)
4468 if (!standalone
&& _ioquiesced
)
4470 return (BOP_FSTAT(ops
, fd
, stp
));
4472 return (BRD_FSTAT(bfs_ops
, fd
, stp
));
4477 kobj_boot_open(char *filename
, int flags
)
4482 * If io via bootops is quiesced, it means boot is no longer
4483 * available to us. We make it look as if we can't open the
4484 * named file - which is reasonably accurate.
4486 if (!standalone
&& _ioquiesced
)
4489 kobj_record_file(filename
);
4490 return (BOP_OPEN(filename
, flags
));
4492 kobj_record_file(filename
);
4493 return (BRD_OPEN(bfs_ops
, filename
, flags
));
4498 kobj_boot_close(int fd
)
4501 if (!standalone
&& _ioquiesced
)
4504 return (BOP_CLOSE(fd
));
4506 return (BRD_CLOSE(bfs_ops
, fd
));
4512 kobj_boot_seek(int fd
, off_t hi
, off_t lo
)
4515 return (BOP_SEEK(fd
, lo
) == -1 ? -1 : 0);
4517 return (BRD_SEEK(bfs_ops
, fd
, lo
, SEEK_SET
));
4522 kobj_boot_read(int fd
, caddr_t buf
, size_t size
)
4525 return (BOP_READ(fd
, buf
, size
));
4527 return (BRD_READ(bfs_ops
, fd
, buf
, size
));
4532 kobj_boot_compinfo(int fd
, struct compinfo
*cb
)
4534 return (boot_compinfo(fd
, cb
));
4538 * Check if the file is compressed (for now we handle only gzip).
4539 * It returns CH_MAGIC_GZIP if the file is compressed and 0 otherwise.
4542 kobj_is_compressed(intptr_t fd
)
4544 struct vnode
*vp
= (struct vnode
*)fd
;
4549 if ((err
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)((intptr_t)&magic_buf
),
4550 sizeof (magic_buf
), (offset_t
)(0),
4551 UIO_SYSSPACE
, 0, 0, CRED(), &resid
)) != 0) {
4553 _kobj_printf(ops
, "kobj_is_compressed: vn_rdwr() failed, "
4554 "error code 0x%x\n", err
);
4558 if (magic_buf
== CH_MAGIC_GZIP
)
4559 return (CH_MAGIC_GZIP
);