4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2011 Bayard G. Bell <buffer.g.overflow@gmail.com>.
27 * All rights reserved. Use is subject to license terms.
31 * Kernel's linker/loader
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/sysmacros.h>
37 #include <sys/systm.h>
40 #include <sys/reboot.h>
41 #include <sys/bootconf.h>
42 #include <sys/debug.h>
45 #include <sys/vnode.h>
49 #include <vm/seg_kp.h>
50 #include <vm/seg_kmem.h>
52 #include <sys/elf_notes.h>
53 #include <sys/vmsystm.h>
55 #include <sys/atomic.h>
60 #include <sys/ksyms.h>
62 #include <sys/modctl.h>
63 #include <sys/varargs.h>
64 #include <sys/kstat.h>
65 #include <sys/kobj_impl.h>
66 #include <sys/fs/decomp.h>
67 #include <sys/callb.h>
68 #include <sys/cmn_err.h>
69 #include <sys/tnf_probe.h>
72 #include <krtld/reloc.h>
73 #include <krtld/kobj_kdi.h>
75 #include <sys/crypto/elfsign.h>
78 #include <sys/bootvfs.h>
82 * do_symbols() error codes
84 #define DOSYM_UNDEF -1 /* undefined symbol */
85 #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */
88 static void synthetic_bootaux(char *, val_t
*);
91 static struct module
*load_exec(val_t
*, char *);
92 static void load_linker(val_t
*);
93 static struct modctl
*add_primary(const char *filename
, int);
94 static int bind_primary(val_t
*, int);
95 static int load_primary(struct module
*, int);
96 static int load_kmdb(val_t
*);
97 static int get_progbits(struct module
*, struct _buf
*);
98 static int get_syms(struct module
*, struct _buf
*);
99 static int get_ctf(struct module
*, struct _buf
*);
100 static void get_signature(struct module
*, struct _buf
*);
101 static int do_common(struct module
*);
102 static void add_dependent(struct module
*, struct module
*);
103 static int do_dependents(struct modctl
*, char *, size_t);
104 static int do_symbols(struct module
*, Elf64_Addr
);
105 static void module_assign(struct modctl
*, struct module
*);
106 static void free_module_data(struct module
*);
107 static char *depends_on(struct module
*);
108 static char *getmodpath(const char *);
109 static char *basename(char *);
110 static void attr_val(val_t
*);
111 static char *find_libmacro(char *);
112 static char *expand_libmacro(char *, char *, char *);
113 static int read_bootflags(void);
114 static int kobj_comp_setup(struct _buf
*, struct compinfo
*);
115 static int kobj_uncomp_blk(struct _buf
*, caddr_t
, uint_t
);
116 static int kobj_read_blks(struct _buf
*, caddr_t
, uint_t
, uint_t
);
117 static int kobj_boot_open(char *, int);
118 static int kobj_boot_close(int);
119 static int kobj_boot_seek(int, off_t
, off_t
);
120 static int kobj_boot_read(int, caddr_t
, size_t);
121 static int kobj_boot_fstat(int, struct bootstat
*);
122 static int kobj_boot_compinfo(int, struct compinfo
*);
123 extern int kobj_boot_mountroot(void);
125 static Sym
*lookup_one(struct module
*, const char *);
126 static void sym_insert(struct module
*, char *, symid_t
);
127 static Sym
*sym_lookup(struct module
*, Sym
*);
129 static struct kobjopen_tctl
*kobjopen_alloc(char *filename
);
130 static void kobjopen_free(struct kobjopen_tctl
*ltp
);
131 static void kobjopen_thread(struct kobjopen_tctl
*ltp
);
132 static int kobj_is_compressed(intptr_t);
134 extern int kcopy(const void *, void *, size_t);
135 extern int elf_mach_ok(Ehdr
*);
136 extern int alloc_gottable(struct module
*, caddr_t
*, caddr_t
*);
138 static void tnf_unsplice_probes(uint_t
, struct modctl
*);
139 extern tnf_probe_control_t
*__tnf_probe_list_head
;
140 extern tnf_tag_data_t
*__tnf_tag_list_head
;
142 extern int modrootloaded
;
143 extern int swaploaded
;
144 extern int bop_io_quiesced
;
145 extern int last_module_id
;
147 extern char stubs_base
[];
148 extern char stubs_end
[];
152 * Values that can be or'd in to kobj_debug and their effects:
154 * D_DEBUG - misc. debugging information.
155 * D_SYMBOLS - list symbols and their values as they are entered
156 * into the hash table
157 * D_RELOCATIONS - display relocation processing information
158 * D_LOADING - display information about each module as it
163 #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \
164 (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s))
166 #define KOBJ_MARK(s) /* discard */
169 #define MODPATH_PROPNAME "module-path"
171 #define _moddebug get_weakish_int(&moddebug)
172 #define _modrootloaded get_weakish_int(&modrootloaded)
173 #define _swaploaded get_weakish_int(&swaploaded)
174 #define _ioquiesced get_weakish_int(&bop_io_quiesced)
176 #define mod(X) (struct module *)((X)->modl_modp->mod_mp)
178 void *romp
; /* rom vector (opaque to us) */
179 struct bootops
*ops
; /* bootops vector */
180 void *dbvec
; /* debug vector */
183 * kobjopen thread control structure
185 struct kobjopen_tctl
{
187 char *name
; /* name of file */
188 struct vnode
*vp
; /* vnode return from vn_open() */
189 int Errno
; /* error return from vnopen */
193 * Structure for defining dynamically expandable library macros
196 struct lib_macro_info
{
197 char *lmi_list
; /* ptr to list of possible choices */
198 char *lmi_macroname
; /* pointer to macro name */
199 ushort_t lmi_ba_index
; /* index into bootaux vector */
200 ushort_t lmi_macrolen
; /* macro length */
202 { NULL
, "CPU", BA_CPU
, 0 },
203 { NULL
, "MMU", BA_MMU
, 0 }
206 #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info)
208 char *boot_cpu_compatible_list
; /* make $CPU available */
210 char *kobj_module_path
; /* module search path */
211 vmem_t
*text_arena
; /* module text arena */
212 static vmem_t
*data_arena
; /* module data & bss arena */
213 static vmem_t
*ctf_arena
; /* CTF debug data arena */
214 static struct modctl
*kobj_modules
= NULL
; /* modules loaded */
215 int kobj_mmu_pagesize
; /* system pagesize */
216 static int lg_pagesize
; /* "large" pagesize */
217 static int kobj_last_module_id
= 0; /* id assignment */
218 static kmutex_t kobj_lock
; /* protects mach memory list */
221 * The following functions have been implemented by the kernel.
222 * However, many 3rd party drivers provide their own implementations
223 * of these functions. When such drivers are loaded, messages
224 * indicating that these symbols have been multiply defined will be
225 * emitted to the console. To avoid alarming customers for no good
226 * reason, we simply suppress such warnings for the following set of
229 static char *suppress_sym_list
[] =
245 NULL
/* This entry must exist */
248 /* indexed by KOBJ_NOTIFY_* */
249 static kobj_notify_list_t
*kobj_notifiers
[KOBJ_NOTIFY_MAX
+ 1];
252 * TNF probe management globals
254 tnf_probe_control_t
*__tnf_probe_list_head
= NULL
;
255 tnf_tag_data_t
*__tnf_tag_list_head
= NULL
;
256 int tnf_changed_probe_list
= 0;
259 * Prefix for statically defined tracing (SDT) DTrace probes.
261 const char *sdt_prefix
= "__dtrace_probe_";
264 * Beginning and end of the kernel's dynamic text/data segments.
266 static caddr_t _text
;
267 static caddr_t _etext
;
268 static caddr_t _data
;
271 * The sparc linker doesn't create a memory location
272 * for a variable named _edata, so _edata can only be
273 * referred to, not modified. krtld needs a static
274 * variable to modify it - within krtld, of course -
275 * outside of krtld, e_data is used in all kernels.
277 extern caddr_t _edata
;
279 Addr dynseg
= 0; /* load address of "dynamic" segment */
280 size_t dynsize
; /* "dynamic" segment size */
283 int standalone
= 1; /* an unwholey kernel? */
284 int use_iflush
; /* iflush after relocations */
289 * Common printf function pointer. Can handle only one conversion
290 * specification in the format string. Some of the functions invoked
291 * through this function pointer cannot handle more that one conversion
292 * specification in the format string.
294 void (*_kobj_printf
)(void *, const char *, ...); /* printf routine */
297 * Standalone function pointers for use within krtld.
298 * Many platforms implement optimized platmod versions of
299 * utilities such as bcopy and any such are not yet available
300 * until the kernel is more completely stitched together.
303 void (*kobj_bcopy
)(const void *, void *, size_t);
304 void (*kobj_bzero
)(void *, size_t);
305 size_t (*kobj_strlcat
)(char *, const char *, size_t);
307 static kobj_stat_t kobj_stat
;
309 #define MINALIGN 8 /* at least a double-word */
312 get_weakish_int(int *ip
)
316 return (ip
== NULL
? 0 : *ip
);
320 get_weakish_pointer(void **ptrp
)
324 return (ptrp
== NULL
? 0 : *ptrp
);
328 * XXX fix dependencies on "kernel"; this should work
329 * for other standalone binaries as well.
331 * XXX Fix hashing code to use one pointer to
345 * Load, bind and relocate all modules that
346 * form the primary kernel. At this point, our
347 * externals have not been relocated.
353 struct bootops
*bootvec
,
359 char filename
[MAXPATHLEN
];
362 * Save these to pass on to
363 * the booted standalone.
369 kobj_setup_standalone_vectors();
371 KOBJ_MARK("Entered kobj_init()");
373 (void) BOP_GETPROP(ops
, "whoami", filename
);
376 * We don't support standalone debuggers anymore. The use of kadb
377 * will interfere with the later use of kmdb. Let the user mend
378 * their ways now. Users will reach this message if they still
379 * have the kadb binary on their system (perhaps they used an old
380 * bfu, or maybe they intentionally copied it there) and have
381 * specified its use in a way that eluded our checking in the boot
385 _kobj_printf(ops
, "\nWARNING: Standalone debuggers such as "
386 "kadb are no longer supported\n\n");
392 * OBP allows us to read both the ramdisk and
393 * the underlying root fs when root is a disk.
394 * This can lower incidences of unbootable systems
395 * when the archive is out-of-date with the /etc
398 if (BOP_MOUNTROOT() != BOOT_SVC_OK
) {
399 _kobj_printf(ops
, "can't mount boot fs\n");
404 /* on x86, we always boot with a ramdisk */
405 (void) kobj_boot_mountroot();
408 * Now that the ramdisk is mounted, finish boot property
415 * The early boot code does -not- hand us any of the dynamic
416 * metadata about the executable. In particular, it does not read
417 * in, map or otherwise look at the program headers. We fake all
420 * We do this early as DTrace static probes both call undefined
421 * references. We have to process those relocations before calling
424 * OBP tells kobj_start() where the ELF image is in memory, so it
425 * synthesized bootaux before kobj_init() was called
427 if (bootaux
[BA_PHDR
].ba_ptr
== NULL
)
428 synthetic_bootaux(filename
, bootaux
);
433 * Save the interesting attribute-values
434 * (scanned by kobj_boot).
439 * Set the module search path.
441 kobj_module_path
= getmodpath(filename
);
443 boot_cpu_compatible_list
= find_libmacro("CPU");
446 * These two modules have actually been
447 * loaded by boot, but we finish the job
448 * by introducing them into the world of
452 mp
= load_exec(bootaux
, filename
);
453 load_linker(bootaux
);
456 * Load all the primary dependent modules.
458 if (load_primary(mp
, KOBJ_LM_PRIMARY
) == -1)
464 if (bind_primary(bootaux
, KOBJ_LM_PRIMARY
) == -1)
467 entry
= bootaux
[BA_ENTRY
].ba_val
;
474 if (boothowto
& RB_VERBOSE
)
475 kobj_lm_dump(KOBJ_LM_PRIMARY
);
479 if (boothowto
& RB_KMDB
) {
480 if (load_kmdb(bootaux
) < 0)
492 kobj_sync_instruction_memory(s_text
, e_text
- s_text
);
495 if (kobj_debug
& D_DEBUG
)
497 "krtld: transferring control to: 0x%p\n", entry
);
501 * Make sure the mod system knows about the modules already loaded.
503 last_module_id
= kobj_last_module_id
;
504 bcopy(kobj_modules
, &modules
, sizeof (modules
));
507 if (modp
->mod_next
== kobj_modules
)
508 modp
->mod_next
= &modules
;
509 if (modp
->mod_prev
== kobj_modules
)
510 modp
->mod_prev
= &modules
;
511 } while ((modp
= modp
->mod_next
) != &modules
);
516 if (kobj_debug
& D_DEBUG
)
518 "krtld: really transferring control to: 0x%p\n", entry
);
521 /* restore printf/bcopy/bzero vectors before returning */
522 kobj_restore_vectors();
526 * krtld was called from a dboot ELF section, the embedded
527 * dboot code contains the real entry via bootaux
529 exitto((caddr_t
)entry
);
532 * krtld was directly called from startup
539 _kobj_printf(ops
, "krtld: error during initial load/link phase\n");
540 _kobj_printf(ops
, "\n");
541 _kobj_printf(ops
, "krtld could neither locate nor resolve symbols"
543 _kobj_printf(ops
, " %s\n", filename
);
544 _kobj_printf(ops
, "in the boot archive. Please verify that this"
546 _kobj_printf(ops
, "matches what is found in the boot archive.\n");
547 bop_panic("Unable to boot");
552 * Synthesize additional metadata that describes the executable as
553 * krtld's caller didn't do it.
556 synthetic_bootaux(char *filename
, val_t
*bootaux
)
566 KOBJ_MARK("synthetic_bootaux()");
568 file
= kobj_open_file(filename
);
569 if (file
== (struct _buf
*)-1) {
570 _kobj_printf(ops
, "krtld: failed to open '%s'\n", filename
);
573 KOBJ_MARK("reading program headers");
574 if (kobj_read_file(file
, (char *)&ehdr
, sizeof (ehdr
), 0) < 0) {
575 _kobj_printf(ops
, "krtld: %s: failed to read ehder\n",
583 bootaux
[BA_PHNUM
].ba_val
= ehdr
.e_phnum
;
584 bootaux
[BA_PHENT
].ba_val
= ehdr
.e_phentsize
;
585 n
= ehdr
.e_phentsize
* ehdr
.e_phnum
;
587 phdrbase
= kobj_alloc(n
, KM_WAIT
| KM_TMP
);
589 if (kobj_read_file(file
, phdrbase
, n
, ehdr
.e_phoff
) < 0) {
590 _kobj_printf(ops
, "krtld: %s: failed to read phdrs\n",
594 bootaux
[BA_PHDR
].ba_ptr
= phdrbase
;
595 kobj_close_file(file
);
596 KOBJ_MARK("closed file");
599 * Find the dynamic section address
601 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
602 Phdr
*phdr
= (Phdr
*)(phdrbase
+ ehdr
.e_phentsize
* i
);
604 if (phdr
->p_type
== PT_DYNAMIC
) {
605 bootaux
[BA_DYNAMIC
].ba_ptr
= (void *)phdr
->p_vaddr
;
609 KOBJ_MARK("synthetic_bootaux() done");
614 * Set up any global information derived
615 * from attribute/values in the boot or
619 attr_val(val_t
*bootaux
)
625 KOBJ_MARK("attr_val()");
626 kobj_mmu_pagesize
= bootaux
[BA_PAGESZ
].ba_val
;
627 lg_pagesize
= bootaux
[BA_LPAGESZ
].ba_val
;
628 use_iflush
= bootaux
[BA_IFLUSH
].ba_val
;
630 phdr
= (Phdr
*)bootaux
[BA_PHDR
].ba_ptr
;
631 phnum
= bootaux
[BA_PHNUM
].ba_val
;
632 phsize
= bootaux
[BA_PHENT
].ba_val
;
633 for (i
= 0; i
< phnum
; i
++) {
634 phdr
= (Phdr
*)(bootaux
[BA_PHDR
].ba_val
+ i
* phsize
);
636 if (phdr
->p_type
!= PT_LOAD
) {
640 * Bounds of the various segments.
642 if (!(phdr
->p_flags
& PF_X
)) {
645 * sparc kernel puts the dynamic info
646 * into a separate segment, which is
647 * free'd in bop_fini()
649 ASSERT(phdr
->p_vaddr
!= 0);
650 dynseg
= phdr
->p_vaddr
;
651 dynsize
= phdr
->p_memsz
;
653 ASSERT(phdr
->p_vaddr
== 0);
656 if (phdr
->p_flags
& PF_W
) {
657 _data
= (caddr_t
)phdr
->p_vaddr
;
658 _edata
= _data
+ phdr
->p_memsz
;
660 _text
= (caddr_t
)phdr
->p_vaddr
;
661 _etext
= _text
+ phdr
->p_memsz
;
666 /* To do the kobj_alloc, _edata needs to be set. */
667 for (i
= 0; i
< NLIBMACROS
; i
++) {
668 if (bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
!= NULL
) {
669 libmacros
[i
].lmi_list
= kobj_alloc(
670 strlen(bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
) +
672 (void) strcpy(libmacros
[i
].lmi_list
,
673 bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
);
675 libmacros
[i
].lmi_macrolen
= strlen(libmacros
[i
].lmi_macroname
);
680 * Set up the booted executable.
682 static struct module
*
683 load_exec(val_t
*bootaux
, char *filename
)
689 int i
, lsize
, osize
, nsize
, allocsize
;
691 char path
[MAXPATHLEN
];
694 if (kobj_debug
& D_DEBUG
)
695 _kobj_printf(ops
, "module path '%s'\n", kobj_module_path
);
698 KOBJ_MARK("add_primary");
699 cp
= add_primary(filename
, KOBJ_LM_PRIMARY
);
701 KOBJ_MARK("struct module");
702 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
706 * We don't have the following information
707 * since this module is an executable and not
710 mp
->symtbl_section
= 0;
715 * Since this module is the only exception,
716 * we cons up some section headers.
719 mp
->symhdr
= kobj_zalloc(sizeof (Shdr
), KM_WAIT
);
722 mp
->strhdr
= kobj_zalloc(sizeof (Shdr
), KM_WAIT
);
724 mp
->symhdr
->sh_type
= SHT_SYMTAB
;
725 mp
->strhdr
->sh_type
= SHT_STRTAB
;
727 * Scan the dynamic structure.
729 for (dyn
= (Dyn
*) bootaux
[BA_DYNAMIC
].ba_ptr
;
730 dyn
->d_tag
!= DT_NULL
; dyn
++) {
731 switch (dyn
->d_tag
) {
733 mp
->symspace
= mp
->symtbl
= (char *)dyn
->d_un
.d_ptr
;
734 mp
->symhdr
->sh_addr
= dyn
->d_un
.d_ptr
;
737 mp
->nsyms
= *((uint_t
*)dyn
->d_un
.d_ptr
+ 1);
738 mp
->hashsize
= *(uint_t
*)dyn
->d_un
.d_ptr
;
741 mp
->strings
= (char *)dyn
->d_un
.d_ptr
;
742 mp
->strhdr
->sh_addr
= dyn
->d_un
.d_ptr
;
745 mp
->strhdr
->sh_size
= dyn
->d_un
.d_val
;
748 mp
->symhdr
->sh_entsize
= dyn
->d_un
.d_val
;
754 * Collapse any DT_NEEDED entries into one string.
757 allocsize
= MAXPATHLEN
;
759 KOBJ_MARK("depends_on");
760 mp
->depends_on
= kobj_alloc(allocsize
, KM_WAIT
);
762 for (dyn
= (Dyn
*) bootaux
[BA_DYNAMIC
].ba_ptr
;
763 dyn
->d_tag
!= DT_NULL
; dyn
++)
764 if (dyn
->d_tag
== DT_NEEDED
) {
767 libname
= mp
->strings
+ dyn
->d_un
.d_val
;
768 if (strchr(libname
, '$') != NULL
) {
769 if ((_lib
= expand_libmacro(libname
,
770 path
, path
)) != NULL
)
773 _kobj_printf(ops
, "krtld: "
774 "load_exec: fail to "
775 "expand %s\n", libname
);
777 lsize
= strlen(libname
);
779 if (nsize
+ 1 > allocsize
) {
780 KOBJ_MARK("grow depends_on");
781 tmp
= kobj_alloc(allocsize
+ MAXPATHLEN
,
783 bcopy(mp
->depends_on
, tmp
, osize
);
784 kobj_free(mp
->depends_on
, allocsize
);
785 mp
->depends_on
= tmp
;
786 allocsize
+= MAXPATHLEN
;
788 bcopy(libname
, mp
->depends_on
+ osize
, lsize
);
789 *(mp
->depends_on
+ nsize
) = ' '; /* separate */
794 mp
->depends_on
[nsize
- 1] = '\0'; /* terminate the string */
796 * alloc with exact size and copy whatever it got over
798 KOBJ_MARK("realloc depends_on");
799 tmp
= kobj_alloc(nsize
, KM_WAIT
);
800 bcopy(mp
->depends_on
, tmp
, nsize
);
801 kobj_free(mp
->depends_on
, allocsize
);
802 mp
->depends_on
= tmp
;
804 kobj_free(mp
->depends_on
, allocsize
);
805 mp
->depends_on
= NULL
;
808 mp
->flags
= KOBJ_EXEC
|KOBJ_PRIM
; /* NOT a relocatable .o */
809 mp
->symhdr
->sh_size
= mp
->nsyms
* mp
->symhdr
->sh_entsize
;
811 * We allocate our own table since we don't
812 * hash undefined references.
815 mp
->chains
= kobj_zalloc(mp
->nsyms
* sizeof (symid_t
), KM_WAIT
);
816 KOBJ_MARK("buckets");
817 mp
->buckets
= kobj_zalloc(mp
->hashsize
* sizeof (symid_t
), KM_WAIT
);
822 mp
->text_size
= _etext
- _text
;
823 mp
->data_size
= _edata
- _data
;
825 cp
->mod_text
= mp
->text
;
826 cp
->mod_text_size
= mp
->text_size
;
828 mp
->filename
= cp
->mod_filename
;
831 if (kobj_debug
& D_LOADING
) {
832 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
833 _kobj_printf(ops
, "\ttext: 0x%p", mp
->text
);
834 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
835 _kobj_printf(ops
, "\tdata: 0x%p", mp
->data
);
836 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
838 #endif /* KOBJ_DEBUG */
841 * Insert symbols into the hash table.
843 for (i
= 0; i
< mp
->nsyms
; i
++) {
844 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
846 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
849 sym_insert(mp
, mp
->strings
+ sp
->st_name
, i
);
852 KOBJ_MARK("load_exec done");
857 * Set up the linker module (if it's compiled in, LDNAME is NULL)
860 load_linker(val_t
*bootaux
)
862 struct module
*kmp
= (struct module
*)kobj_modules
->mod_mp
;
869 char *dlname
= (char *)bootaux
[BA_LDNAME
].ba_ptr
;
872 * On some architectures, krtld is compiled into the kernel.
877 cp
= add_primary(dlname
, KOBJ_LM_PRIMARY
);
879 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
882 mp
->hdr
= *(Ehdr
*)bootaux
[BA_LDELF
].ba_ptr
;
883 shsize
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
884 mp
->shdrs
= kobj_alloc(shsize
, KM_WAIT
);
885 bcopy(bootaux
[BA_LDSHDR
].ba_ptr
, mp
->shdrs
, shsize
);
887 for (i
= 1; i
< (int)mp
->hdr
.e_shnum
; i
++) {
888 shp
= (Shdr
*)(mp
->shdrs
+ (i
* mp
->hdr
.e_shentsize
));
890 if (shp
->sh_flags
& SHF_ALLOC
) {
891 if (shp
->sh_flags
& SHF_WRITE
) {
892 if (mp
->data
== NULL
)
893 mp
->data
= (char *)shp
->sh_addr
;
894 } else if (mp
->text
== NULL
) {
895 mp
->text
= (char *)shp
->sh_addr
;
898 if (shp
->sh_type
== SHT_SYMTAB
) {
899 mp
->symtbl_section
= i
;
901 mp
->symspace
= mp
->symtbl
= (char *)shp
->sh_addr
;
904 mp
->nsyms
= mp
->symhdr
->sh_size
/ mp
->symhdr
->sh_entsize
;
905 mp
->flags
= KOBJ_INTERP
|KOBJ_PRIM
;
906 mp
->strhdr
= (Shdr
*)
907 (mp
->shdrs
+ mp
->symhdr
->sh_link
* mp
->hdr
.e_shentsize
);
908 mp
->strings
= (char *)mp
->strhdr
->sh_addr
;
909 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
911 mp
->symsize
= mp
->symhdr
->sh_size
+ mp
->strhdr
->sh_size
+ sizeof (int) +
912 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
);
914 mp
->chains
= kobj_zalloc(mp
->nsyms
* sizeof (symid_t
), KM_WAIT
);
915 mp
->buckets
= kobj_zalloc(mp
->hashsize
* sizeof (symid_t
), KM_WAIT
);
917 mp
->bss
= bootaux
[BA_BSS
].ba_val
;
918 mp
->bss_align
= 0; /* pre-aligned during allocation */
919 mp
->bss_size
= (uintptr_t)_edata
- mp
->bss
;
920 mp
->text_size
= _etext
- mp
->text
;
921 mp
->data_size
= _edata
- mp
->data
;
922 mp
->filename
= cp
->mod_filename
;
923 cp
->mod_text
= mp
->text
;
924 cp
->mod_text_size
= mp
->text_size
;
927 * Now that we've figured out where the linker is,
928 * set the limits for the booted object.
930 kmp
->text_size
= (size_t)(mp
->text
- kmp
->text
);
931 kmp
->data_size
= (size_t)(mp
->data
- kmp
->data
);
932 kobj_modules
->mod_text_size
= kmp
->text_size
;
935 if (kobj_debug
& D_LOADING
) {
936 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
937 _kobj_printf(ops
, "\ttext:0x%p", mp
->text
);
938 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
939 _kobj_printf(ops
, "\tdata:0x%p", mp
->data
);
940 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
942 #endif /* KOBJ_DEBUG */
945 * Insert the symbols into the hash table.
947 for (i
= 0; i
< mp
->nsyms
; i
++) {
948 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
950 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
952 if (ELF_ST_BIND(sp
->st_info
) == STB_GLOBAL
) {
953 if (sp
->st_shndx
== SHN_COMMON
)
954 sp
->st_shndx
= SHN_ABS
;
956 sym_insert(mp
, mp
->strings
+ sp
->st_name
, i
);
961 static kobj_notify_list_t
**
962 kobj_notify_lookup(uint_t type
)
964 ASSERT(type
!= 0 && type
< sizeof (kobj_notifiers
) /
965 sizeof (kobj_notify_list_t
*));
967 return (&kobj_notifiers
[type
]);
971 kobj_notify_add(kobj_notify_list_t
*knp
)
973 kobj_notify_list_t
**knl
;
975 knl
= kobj_notify_lookup(knp
->kn_type
);
980 mutex_enter(&kobj_lock
);
983 (*knl
)->kn_prev
= knp
;
988 mutex_exit(&kobj_lock
);
993 kobj_notify_remove(kobj_notify_list_t
*knp
)
995 kobj_notify_list_t
**knl
= kobj_notify_lookup(knp
->kn_type
);
996 kobj_notify_list_t
*tknp
;
998 mutex_enter(&kobj_lock
);
1001 if (tknp
= knp
->kn_next
)
1002 tknp
->kn_prev
= knp
->kn_prev
;
1005 if (tknp
= knp
->kn_prev
)
1006 tknp
->kn_next
= knp
->kn_next
;
1008 *knl
= knp
->kn_next
;
1010 mutex_exit(&kobj_lock
);
1016 * Notify all interested callbacks of a specified change in module state.
1019 kobj_notify(int type
, struct modctl
*modp
)
1021 kobj_notify_list_t
*knp
;
1023 if (modp
->mod_loadflags
& MOD_NONOTIFY
|| standalone
)
1026 mutex_enter(&kobj_lock
);
1028 for (knp
= *(kobj_notify_lookup(type
)); knp
!= NULL
; knp
= knp
->kn_next
)
1029 knp
->kn_func(type
, modp
);
1032 * KDI notification must be last (it has to allow for work done by the
1033 * other notification callbacks), so we call it manually.
1035 kobj_kdi_mod_notify(type
, modp
);
1037 mutex_exit(&kobj_lock
);
1041 * Create the module path.
1044 getmodpath(const char *filename
)
1046 char *path
= kobj_zalloc(MAXPATHLEN
, KM_WAIT
);
1049 * Platform code gets first crack, then add
1050 * the default components
1052 mach_modpath(path
, filename
);
1054 (void) strcat(path
, " ");
1055 return (strcat(path
, MOD_DEFPATH
));
1058 static struct modctl
*
1059 add_primary(const char *filename
, int lmid
)
1063 cp
= kobj_zalloc(sizeof (struct modctl
), KM_WAIT
);
1065 cp
->mod_filename
= kobj_alloc(strlen(filename
) + 1, KM_WAIT
);
1068 * For symbol lookup, we assemble our own
1069 * modctl list of the primary modules.
1072 (void) strcpy(cp
->mod_filename
, filename
);
1073 cp
->mod_modname
= basename(cp
->mod_filename
);
1075 /* set values for modinfo assuming that the load will work */
1078 cp
->mod_installed
= 1;
1079 cp
->mod_loadcnt
= 1;
1080 cp
->mod_loadflags
= MOD_NOAUTOUNLOAD
;
1082 cp
->mod_id
= kobj_last_module_id
++;
1085 * Link the module in. We'll pass this info on
1086 * to the mod squad later.
1088 if (kobj_modules
== NULL
) {
1090 cp
->mod_prev
= cp
->mod_next
= cp
;
1092 cp
->mod_prev
= kobj_modules
->mod_prev
;
1093 cp
->mod_next
= kobj_modules
;
1094 kobj_modules
->mod_prev
->mod_next
= cp
;
1095 kobj_modules
->mod_prev
= cp
;
1098 kobj_lm_append(lmid
, cp
);
1104 bind_primary(val_t
*bootaux
, int lmid
)
1106 struct modctl_list
*linkmap
= kobj_lm_lookup(lmid
);
1107 struct modctl_list
*lp
;
1111 * Do common symbols.
1113 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1117 * Don't do common section relocations for modules that
1120 if (mp
->flags
& (KOBJ_EXEC
|KOBJ_INTERP
))
1123 if (do_common(mp
) < 0)
1130 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1133 if (do_symbols(mp
, 0) < 0)
1140 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1143 if (mp
->flags
& KOBJ_EXEC
) {
1145 Word relasz
= 0, relaent
= 0;
1149 for (dyn
= (Dyn
*)bootaux
[BA_DYNAMIC
].ba_ptr
;
1150 dyn
->d_tag
!= DT_NULL
; dyn
++) {
1151 switch (dyn
->d_tag
) {
1154 relasz
= dyn
->d_un
.d_val
;
1158 relaent
= dyn
->d_un
.d_val
;
1162 rela
= (char *)dyn
->d_un
.d_ptr
;
1166 rela
= (char *)dyn
->d_un
.d_ptr
;
1171 relaent
== 0 || rela
== NULL
) {
1172 _kobj_printf(ops
, "krtld: bind_primary(): "
1173 "no relocation information found for "
1174 "module %s\n", mp
->filename
);
1178 if (kobj_debug
& D_RELOCATIONS
)
1179 _kobj_printf(ops
, "krtld: relocating: file=%s "
1180 "KOBJ_EXEC\n", mp
->filename
);
1182 if (do_relocate(mp
, rela
, shtype
, relasz
/relaent
,
1183 relaent
, (Addr
)mp
->text
) < 0)
1186 if (do_relocations(mp
) < 0)
1190 kobj_sync_instruction_memory(mp
->text
, mp
->text_size
);
1193 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1197 * We need to re-read the full symbol table for the boot file,
1198 * since we couldn't use the full one before. We also need to
1199 * load the CTF sections of both the boot file and the
1202 if (mp
->flags
& KOBJ_EXEC
) {
1206 file
= kobj_open_file(mp
->filename
);
1207 if (file
== (struct _buf
*)-1)
1209 if (kobj_read_file(file
, (char *)&mp
->hdr
,
1210 sizeof (mp
->hdr
), 0) < 0)
1212 n
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
1213 mp
->shdrs
= kobj_alloc(n
, KM_WAIT
);
1214 if (kobj_read_file(file
, mp
->shdrs
, n
,
1215 mp
->hdr
.e_shoff
) < 0)
1217 if (get_syms(mp
, file
) < 0)
1219 if (get_ctf(mp
, file
) < 0)
1221 kobj_close_file(file
);
1222 mp
->flags
|= KOBJ_RELOCATED
;
1224 } else if (mp
->flags
& KOBJ_INTERP
) {
1227 file
= kobj_open_path(mp
->filename
, 1);
1228 if (file
== (struct _buf
*)-1)
1230 if (get_ctf(mp
, file
) < 0)
1232 kobj_close_file(file
);
1233 mp
->flags
|= KOBJ_RELOCATED
;
1240 static struct modctl
*
1241 mod_already_loaded(char *modname
)
1243 struct modctl
*mctl
= kobj_modules
;
1246 if (strcmp(modname
, mctl
->mod_filename
) == 0)
1248 mctl
= mctl
->mod_next
;
1250 } while (mctl
!= kobj_modules
);
1256 * Load all the primary dependent modules.
1259 load_primary(struct module
*mp
, int lmid
)
1264 char modname
[MODMAXNAMELEN
];
1266 if ((p
= mp
->depends_on
) == NULL
)
1269 /* CONSTANTCONDITION */
1274 while (*p
&& (*p
== ' ' || *p
== '\t'))
1280 while (*p
&& *p
!= ' ' && *p
!= '\t')
1288 * Check for dup dependencies.
1290 if (strcmp(modname
, "dtracestubs") == 0 ||
1291 mod_already_loaded(modname
) != NULL
)
1294 cp
= add_primary(modname
, lmid
);
1299 (void) kobj_load_module(cp
, 1);
1302 if ((dmp
= cp
->mod_mp
) == NULL
) {
1304 cp
->mod_installed
= 0;
1305 cp
->mod_loadcnt
= 0;
1309 add_dependent(mp
, dmp
);
1310 dmp
->flags
|= KOBJ_PRIM
;
1315 if (load_primary(dmp
, lmid
) == -1) {
1317 cp
->mod_installed
= 0;
1318 cp
->mod_loadcnt
= 0;
1326 console_is_usb_serial(void)
1331 if ((len
= BOP_GETPROPLEN(ops
, "console")) == -1)
1334 console
= kobj_zalloc(len
, KM_WAIT
|KM_TMP
);
1335 (void) BOP_GETPROP(ops
, "console", console
);
1336 ret
= (strcmp(console
, "usb-serial") == 0);
1337 kobj_free(console
, len
);
1343 load_kmdb(val_t
*bootaux
)
1345 struct modctl
*mctl
;
1349 if (console_is_usb_serial()) {
1350 _kobj_printf(ops
, "kmdb not loaded "
1351 "(unsupported on usb serial console)\n");
1355 _kobj_printf(ops
, "Loading kmdb...\n");
1357 if ((mctl
= add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER
)) == NULL
)
1361 (void) kobj_load_module(mctl
, 1);
1364 if ((mp
= mctl
->mod_mp
) == NULL
)
1367 mp
->flags
|= KOBJ_PRIM
;
1369 if (load_primary(mp
, KOBJ_LM_DEBUGGER
) < 0)
1372 if (boothowto
& RB_VERBOSE
)
1373 kobj_lm_dump(KOBJ_LM_DEBUGGER
);
1375 if (bind_primary(bootaux
, KOBJ_LM_DEBUGGER
) < 0)
1378 if ((sym
= lookup_one(mctl
->mod_mp
, "kctl_boot_activate")) == NULL
)
1382 if (kobj_debug
& D_DEBUG
) {
1383 _kobj_printf(ops
, "calling kctl_boot_activate() @ 0x%lx\n",
1385 _kobj_printf(ops
, "\tops 0x%p\n", ops
);
1386 _kobj_printf(ops
, "\tromp 0x%p\n", romp
);
1390 if (((kctl_boot_activate_f
*)sym
->st_value
)(ops
, romp
, 0,
1391 (const char **)kobj_kmdb_argv
) < 0)
1398 * Return a string listing module dependencies.
1401 depends_on(struct module
*mp
)
1407 * The module doesn't have a depends_on value, so let's try it the
1408 * old-fashioned way - via "_depends_on"
1410 if ((sp
= lookup_one(mp
, "_depends_on")) == NULL
)
1413 q
= (char *)sp
->st_value
;
1417 * _depends_on is a deprecated interface, so we warn about its use
1418 * irrespective of subsequent processing errors. How else are we going
1419 * to be able to deco this interface completely?
1420 * Changes initially limited to DEBUG because third-party modules
1421 * should be flagged to developers before general use base.
1424 "Warning: %s uses deprecated _depends_on interface.\n",
1426 _kobj_printf(ops
, "Please notify module developer or vendor.\n");
1430 * Idiot checks. Make sure it's
1431 * in-bounds and NULL terminated.
1433 if (kobj_addrcheck(mp
, q
) || q
[sp
->st_size
- 1] != '\0') {
1434 _kobj_printf(ops
, "Error processing dependency for %s\n",
1439 depstr
= (char *)kobj_alloc(strlen(q
) + 1, KM_WAIT
);
1440 (void) strcpy(depstr
, q
);
1446 kobj_getmodinfo(void *xmp
, struct modinfo
*modinfo
)
1449 mp
= (struct module
*)xmp
;
1451 modinfo
->mi_base
= mp
->text
;
1452 modinfo
->mi_size
= mp
->text_size
+ mp
->data_size
;
1456 * kobj_export_ksyms() performs the following services:
1458 * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena.
1459 * (2) Removes unneeded symbols to save space.
1460 * (3) Reduces memory footprint by using VM_BESTFIT allocations.
1461 * (4) Makes the symbol table visible to /dev/ksyms.
1464 kobj_export_ksyms(struct module
*mp
)
1466 Sym
*esp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_size
);
1472 size_t symsize
= mp
->symhdr
->sh_entsize
;
1477 * Make a copy of the original module structure.
1479 omp
= kobj_alloc(sizeof (struct module
), KM_WAIT
);
1480 bcopy(mp
, omp
, sizeof (struct module
));
1483 * Compute the sizes of the new symbol table sections.
1485 for (nsyms
= strsize
= 1, osp
= (Sym
*)omp
->symtbl
; osp
< esp
; osp
++) {
1486 if (osp
->st_value
== 0)
1488 if (sym_lookup(omp
, osp
) == NULL
)
1490 name
= omp
->strings
+ osp
->st_name
;
1491 namelen
= strlen(name
);
1492 if (ELF_ST_BIND(osp
->st_info
) == STB_LOCAL
)
1495 strsize
+= namelen
+ 1;
1499 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
1502 * ksyms_lock must be held as writer during any operation that
1503 * modifies ksyms_arena, including allocation from same, and
1504 * must not be dropped until the arena is vmem_walk()able.
1506 rw_enter(&ksyms_lock
, RW_WRITER
);
1509 * Allocate space for the new section headers (symtab and strtab),
1510 * symbol table, buckets, chains, and strings.
1512 mp
->symsize
= (2 * sizeof (Shdr
)) + (nsyms
* symsize
) +
1513 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
) + strsize
;
1515 if (mp
->flags
& KOBJ_NOKSYMS
) {
1516 mp
->symspace
= kobj_alloc(mp
->symsize
, KM_WAIT
);
1518 mp
->symspace
= vmem_alloc(ksyms_arena
, mp
->symsize
,
1519 VM_BESTFIT
| VM_SLEEP
);
1521 bzero(mp
->symspace
, mp
->symsize
);
1524 * Divvy up symspace.
1526 mp
->shdrs
= mp
->symspace
;
1527 mp
->symhdr
= (Shdr
*)mp
->shdrs
;
1528 mp
->strhdr
= (Shdr
*)(mp
->symhdr
+ 1);
1529 mp
->symtbl
= (char *)(mp
->strhdr
+ 1);
1530 mp
->buckets
= (symid_t
*)(mp
->symtbl
+ (nsyms
* symsize
));
1531 mp
->chains
= (symid_t
*)(mp
->buckets
+ mp
->hashsize
);
1532 mp
->strings
= (char *)(mp
->chains
+ nsyms
);
1535 * Fill in the new section headers (symtab and strtab).
1537 mp
->hdr
.e_shnum
= 2;
1538 mp
->symtbl_section
= 0;
1540 mp
->symhdr
->sh_type
= SHT_SYMTAB
;
1541 mp
->symhdr
->sh_addr
= (Addr
)mp
->symtbl
;
1542 mp
->symhdr
->sh_size
= nsyms
* symsize
;
1543 mp
->symhdr
->sh_link
= 1;
1544 mp
->symhdr
->sh_info
= locals
;
1545 mp
->symhdr
->sh_addralign
= sizeof (Addr
);
1546 mp
->symhdr
->sh_entsize
= symsize
;
1548 mp
->strhdr
->sh_type
= SHT_STRTAB
;
1549 mp
->strhdr
->sh_addr
= (Addr
)mp
->strings
;
1550 mp
->strhdr
->sh_size
= strsize
;
1551 mp
->strhdr
->sh_addralign
= 1;
1554 * Construct the new symbol table.
1556 for (nsyms
= strsize
= 1, osp
= (Sym
*)omp
->symtbl
; osp
< esp
; osp
++) {
1557 if (osp
->st_value
== 0)
1559 if (sym_lookup(omp
, osp
) == NULL
)
1561 name
= omp
->strings
+ osp
->st_name
;
1562 namelen
= strlen(name
);
1563 sp
= (Sym
*)(mp
->symtbl
+ symsize
* nsyms
);
1564 bcopy(osp
, sp
, symsize
);
1565 bcopy(name
, mp
->strings
+ strsize
, namelen
);
1566 sp
->st_name
= strsize
;
1567 sym_insert(mp
, name
, nsyms
);
1569 strsize
+= namelen
+ 1;
1572 rw_exit(&ksyms_lock
);
1575 * Free the old section headers -- we'll never need them again.
1577 if (!(mp
->flags
& KOBJ_PRIM
)) {
1581 for (shn
= 1; shn
< omp
->hdr
.e_shnum
; shn
++) {
1582 shp
= (Shdr
*)(omp
->shdrs
+ shn
* omp
->hdr
.e_shentsize
);
1583 switch (shp
->sh_type
) {
1586 if (shp
->sh_addr
!= (uintptr_t)NULL
) {
1587 kobj_free((void *)shp
->sh_addr
,
1593 kobj_free(omp
->shdrs
, omp
->hdr
.e_shentsize
* omp
->hdr
.e_shnum
);
1596 * Discard the old symbol table and our copy of the module strucure.
1598 if (!(mp
->flags
& KOBJ_PRIM
))
1599 kobj_free(omp
->symspace
, omp
->symsize
);
1600 kobj_free(omp
, sizeof (struct module
));
1604 kobj_export_ctf(struct module
*mp
)
1606 char *data
= mp
->ctfdata
;
1607 size_t size
= mp
->ctfsize
;
1610 if (_moddebug
& MODDEBUG_NOCTF
) {
1614 mp
->ctfdata
= vmem_alloc(ctf_arena
, size
,
1615 VM_BESTFIT
| VM_SLEEP
);
1616 bcopy(data
, mp
->ctfdata
, size
);
1619 if (!(mp
->flags
& KOBJ_PRIM
))
1620 kobj_free(data
, size
);
1625 kobj_export_module(struct module
*mp
)
1627 kobj_export_ksyms(mp
);
1628 kobj_export_ctf(mp
);
1630 mp
->flags
|= KOBJ_EXPORTED
;
1634 process_dynamic(struct module
*mp
, char *dyndata
, char *strdata
)
1636 char *path
= NULL
, *depstr
= NULL
;
1637 int allocsize
= 0, osize
= 0, nsize
= 0;
1638 char *libname
, *tmp
;
1642 for (dynp
= (Dyn
*)dyndata
; dynp
&& dynp
->d_tag
!= DT_NULL
; dynp
++) {
1643 switch (dynp
->d_tag
) {
1646 * Read the DT_NEEDED entries, expanding the macros they
1647 * contain (if any), and concatenating them into a
1648 * single space-separated dependency list.
1650 libname
= (ulong_t
)dynp
->d_un
.d_ptr
+ strdata
;
1652 if (strchr(libname
, '$') != NULL
) {
1656 path
= kobj_alloc(MAXPATHLEN
, KM_WAIT
);
1657 if ((_lib
= expand_libmacro(libname
, path
,
1661 _kobj_printf(ops
, "krtld: "
1662 "process_dynamic: failed to expand "
1667 lsize
= strlen(libname
);
1669 if (nsize
+ 1 > allocsize
) {
1670 tmp
= kobj_alloc(allocsize
+ MAXPATHLEN
,
1672 if (depstr
!= NULL
) {
1673 bcopy(depstr
, tmp
, osize
);
1674 kobj_free(depstr
, allocsize
);
1677 allocsize
+= MAXPATHLEN
;
1679 bcopy(libname
, depstr
+ osize
, lsize
);
1680 *(depstr
+ nsize
) = ' '; /* separator */
1686 if (dynp
->d_un
.d_val
& DF_1_IGNMULDEF
)
1687 mp
->flags
|= KOBJ_IGNMULDEF
;
1688 if (dynp
->d_un
.d_val
& DF_1_NOKSYMS
)
1689 mp
->flags
|= KOBJ_NOKSYMS
;
1696 * finish up the depends string (if any)
1698 if (depstr
!= NULL
) {
1699 *(depstr
+ nsize
- 1) = '\0'; /* overwrite separator w/term */
1701 kobj_free(path
, MAXPATHLEN
);
1703 tmp
= kobj_alloc(nsize
, KM_WAIT
);
1704 bcopy(depstr
, tmp
, nsize
);
1705 kobj_free(depstr
, allocsize
);
1708 mp
->depends_on
= depstr
;
1715 do_dynamic(struct module
*mp
, struct _buf
*file
)
1717 Shdr
*dshp
, *dstrp
, *shp
;
1718 char *dyndata
, *dstrdata
;
1721 /* find and validate the dynamic section (if any) */
1723 for (dshp
= NULL
, shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
1724 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
1725 switch (shp
->sh_type
) {
1728 _kobj_printf(ops
, "krtld: get_dynamic: %s, ",
1731 "multiple dynamic sections\n");
1744 if (dshp
->sh_link
> mp
->hdr
.e_shnum
) {
1745 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1746 _kobj_printf(ops
, "no section for sh_link %d\n", dshp
->sh_link
);
1749 dstrp
= (Shdr
*)(mp
->shdrs
+ dshp
->sh_link
* mp
->hdr
.e_shentsize
);
1751 if (dstrp
->sh_type
!= SHT_STRTAB
) {
1752 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1753 _kobj_printf(ops
, "sh_link not a string table for section %d\n",
1758 /* read it from disk */
1760 dyndata
= kobj_alloc(dshp
->sh_size
, KM_WAIT
|KM_TMP
);
1761 if (kobj_read_file(file
, dyndata
, dshp
->sh_size
, dshp
->sh_offset
) < 0) {
1762 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1763 _kobj_printf(ops
, "error reading section %d\n", dshn
);
1765 kobj_free(dyndata
, dshp
->sh_size
);
1769 dstrdata
= kobj_alloc(dstrp
->sh_size
, KM_WAIT
|KM_TMP
);
1770 if (kobj_read_file(file
, dstrdata
, dstrp
->sh_size
,
1771 dstrp
->sh_offset
) < 0) {
1772 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1773 _kobj_printf(ops
, "error reading section %d\n", dshp
->sh_link
);
1775 kobj_free(dyndata
, dshp
->sh_size
);
1776 kobj_free(dstrdata
, dstrp
->sh_size
);
1780 /* pull the interesting pieces out */
1782 rc
= process_dynamic(mp
, dyndata
, dstrdata
);
1784 kobj_free(dyndata
, dshp
->sh_size
);
1785 kobj_free(dstrdata
, dstrp
->sh_size
);
1791 kobj_set_ctf(struct module
*mp
, caddr_t data
, size_t size
)
1794 if (mp
->ctfdata
!= NULL
) {
1795 if (vmem_contains(ctf_arena
, mp
->ctfdata
,
1797 vmem_free(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
);
1799 kobj_free(mp
->ctfdata
, mp
->ctfsize
);
1805 * The order is very important here. We need to make sure that
1806 * consumers, at any given instant, see a consistent state. We'd
1807 * rather they see no CTF data than the address of one buffer and the
1818 kobj_load_module(struct modctl
*modp
, int use_path
)
1820 char *filename
= modp
->mod_filename
;
1821 char *modname
= modp
->mod_modname
;
1825 struct module
*mp
= NULL
;
1827 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
1830 * We need to prevent kmdb's symbols from leaking into /dev/ksyms.
1831 * kmdb contains a bunch of symbols with well-known names, symbols
1832 * which will mask the real versions, thus causing no end of trouble
1835 if (strcmp(modp
->mod_modname
, "kmdbmod") == 0)
1836 mp
->flags
|= KOBJ_NOKSYMS
;
1838 file
= kobj_open_path(filename
, use_path
);
1839 if (file
== (struct _buf
*)-1) {
1840 if (file
== (struct _buf
*)-1) {
1841 kobj_free(mp
, sizeof (*mp
));
1846 mp
->filename
= kobj_alloc(strlen(file
->_name
) + 1, KM_WAIT
);
1847 (void) strcpy(mp
->filename
, file
->_name
);
1849 if (kobj_read_file(file
, (char *)&mp
->hdr
, sizeof (mp
->hdr
), 0) < 0) {
1850 _kobj_printf(ops
, "kobj_load_module: %s read header failed\n",
1852 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1853 kobj_free(mp
, sizeof (*mp
));
1856 for (i
= 0; i
< SELFMAG
; i
++) {
1857 if (mp
->hdr
.e_ident
[i
] != ELFMAG
[i
]) {
1858 if (_moddebug
& MODDEBUG_ERRMSG
)
1859 _kobj_printf(ops
, "%s not an elf module\n",
1861 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1862 kobj_free(mp
, sizeof (*mp
));
1867 * It's ELF, but is it our ISA? Interpreting the header
1868 * from a file for a byte-swapped ISA could cause a huge
1869 * and unsatisfiable value to be passed to kobj_alloc below
1870 * and therefore hang booting.
1872 if (!elf_mach_ok(&mp
->hdr
)) {
1873 if (_moddebug
& MODDEBUG_ERRMSG
)
1874 _kobj_printf(ops
, "%s not an elf module for this ISA\n",
1876 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1877 kobj_free(mp
, sizeof (*mp
));
1882 * All modules, save for unix, should be relocatable (as opposed to
1883 * dynamic). Dynamic modules come with PLTs and GOTs, which can't
1884 * currently be processed by krtld.
1886 if (mp
->hdr
.e_type
!= ET_REL
) {
1887 if (_moddebug
& MODDEBUG_ERRMSG
)
1888 _kobj_printf(ops
, "%s isn't a relocatable (ET_REL) "
1889 "module\n", modname
);
1890 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1891 kobj_free(mp
, sizeof (*mp
));
1895 n
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
1896 mp
->shdrs
= kobj_alloc(n
, KM_WAIT
);
1898 if (kobj_read_file(file
, mp
->shdrs
, n
, mp
->hdr
.e_shoff
) < 0) {
1899 _kobj_printf(ops
, "kobj_load_module: %s error reading "
1900 "section headers\n", modname
);
1901 kobj_free(mp
->shdrs
, n
);
1902 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1903 kobj_free(mp
, sizeof (*mp
));
1907 kobj_notify(KOBJ_NOTIFY_MODLOADING
, modp
);
1908 module_assign(modp
, mp
);
1910 /* read in sections */
1911 if (get_progbits(mp
, file
) < 0) {
1912 _kobj_printf(ops
, "%s error reading sections\n", modname
);
1916 if (do_dynamic(mp
, file
) < 0) {
1917 _kobj_printf(ops
, "%s error reading dynamic section\n",
1922 modp
->mod_text
= mp
->text
;
1923 modp
->mod_text_size
= mp
->text_size
;
1925 /* read in symbols; adjust values for each section's real address */
1926 if (get_syms(mp
, file
) < 0) {
1927 _kobj_printf(ops
, "%s error reading symbols\n",
1933 * If we didn't dependency information from the dynamic section, look
1934 * for it the old-fashioned way.
1936 if (mp
->depends_on
== NULL
)
1937 mp
->depends_on
= depends_on(mp
);
1939 if (get_ctf(mp
, file
) < 0) {
1940 _kobj_printf(ops
, "%s debug information will not "
1941 "be available\n", modname
);
1944 /* primary kernel modules do not have a signature section */
1945 if (!(mp
->flags
& KOBJ_PRIM
))
1946 get_signature(mp
, file
);
1949 if (kobj_debug
& D_LOADING
) {
1950 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
1951 _kobj_printf(ops
, "\ttext:0x%p", mp
->text
);
1952 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
1953 _kobj_printf(ops
, "\tdata:0x%p", mp
->data
);
1954 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
1956 #endif /* KOBJ_DEBUG */
1959 * For primary kernel modules, we defer
1960 * symbol resolution and relocation until
1961 * all primary objects have been loaded.
1965 char *dependent_modname
;
1966 /* load all dependents */
1967 dependent_modname
= kobj_zalloc(MODMAXNAMELEN
, KM_WAIT
);
1968 ddrval
= do_dependents(modp
, dependent_modname
, MODMAXNAMELEN
);
1971 * resolve undefined and common symbols,
1972 * also allocates common space
1974 if ((dcrval
= do_common(mp
)) < 0) {
1977 _kobj_printf(ops
, "WARNING: mod_load: "
1978 "MT-unsafe module '%s' rejected\n",
1982 _kobj_printf(ops
, "WARNING: mod_load: "
1983 "cannot load module '%s'\n",
1986 _kobj_printf(ops
, "WARNING: %s: ",
1989 "unable to resolve dependency, "
1990 "module '%s' not found\n",
1996 kobj_free(dependent_modname
, MODMAXNAMELEN
);
2000 /* process relocation tables */
2001 if (do_relocations(mp
) < 0) {
2002 _kobj_printf(ops
, "%s error doing relocations\n",
2007 if (mp
->destination
) {
2008 off_t off
= (uintptr_t)mp
->destination
& PAGEOFFSET
;
2009 caddr_t base
= (caddr_t
)mp
->destination
- off
;
2010 size_t size
= P2ROUNDUP(mp
->text_size
+ off
, PAGESIZE
);
2012 hat_unload(kas
.a_hat
, base
, size
, HAT_UNLOAD_UNLOCK
);
2013 vmem_free(heap_arena
, base
, size
);
2016 /* sync_instruction_memory */
2017 kobj_sync_instruction_memory(mp
->text
, mp
->text_size
);
2018 kobj_export_module(mp
);
2019 kobj_notify(KOBJ_NOTIFY_MODLOADED
, modp
);
2021 kobj_close_file(file
);
2024 if (file
!= (struct _buf
*)-1)
2025 kobj_close_file(file
);
2026 if (modp
->mod_mp
!= NULL
)
2027 free_module_data(modp
->mod_mp
);
2029 module_assign(modp
, NULL
);
2030 return ((file
== (struct _buf
*)-1) ? ENOENT
: EINVAL
);
2034 kobj_load_primary_module(struct modctl
*modp
)
2039 if (kobj_load_module(modp
, 0) != 0)
2043 mp
->flags
|= KOBJ_PRIM
;
2045 /* Bind new module to its dependents */
2046 if (mp
->depends_on
!= NULL
&& (dep
=
2047 mod_already_loaded(mp
->depends_on
)) == NULL
) {
2049 if (kobj_debug
& D_DEBUG
) {
2050 _kobj_printf(ops
, "krtld: failed to resolve deps "
2051 "for primary %s\n", modp
->mod_modname
);
2057 add_dependent(mp
, dep
->mod_mp
);
2060 * Relocate it. This module may not be part of a link map, so we
2061 * can't use bind_primary.
2063 if (do_common(mp
) < 0 || do_symbols(mp
, 0) < 0 ||
2064 do_relocations(mp
) < 0) {
2066 if (kobj_debug
& D_DEBUG
) {
2067 _kobj_printf(ops
, "krtld: failed to relocate "
2068 "primary %s\n", modp
->mod_modname
);
2078 module_assign(struct modctl
*cp
, struct module
*mp
)
2084 mutex_enter(&mod_lock
);
2087 mutex_exit(&mod_lock
);
2091 kobj_unload_module(struct modctl
*modp
)
2093 struct module
*mp
= modp
->mod_mp
;
2095 if ((_moddebug
& MODDEBUG_KEEPTEXT
) && mp
) {
2096 _kobj_printf(ops
, "text for %s ", mp
->filename
);
2097 _kobj_printf(ops
, "was at %p\n", mp
->text
);
2098 mp
->text
= NULL
; /* don't actually free it */
2101 kobj_notify(KOBJ_NOTIFY_MODUNLOADING
, modp
);
2104 * Null out mod_mp first, so consumers (debuggers) know not to look
2105 * at the module structure any more.
2107 mutex_enter(&mod_lock
);
2108 modp
->mod_mp
= NULL
;
2109 mutex_exit(&mod_lock
);
2111 kobj_notify(KOBJ_NOTIFY_MODUNLOADED
, modp
);
2112 free_module_data(mp
);
2116 free_module_data(struct module
*mp
)
2118 struct module_list
*lp
, *tmp
;
2119 int ksyms_exported
= 0;
2125 kobj_free((char *)tmp
, sizeof (*tmp
));
2128 rw_enter(&ksyms_lock
, RW_WRITER
);
2130 if (vmem_contains(ksyms_arena
, mp
->symspace
, mp
->symsize
)) {
2131 vmem_free(ksyms_arena
, mp
->symspace
, mp
->symsize
);
2134 if (mp
->flags
& KOBJ_NOKSYMS
)
2136 kobj_free(mp
->symspace
, mp
->symsize
);
2139 rw_exit(&ksyms_lock
);
2142 if (vmem_contains(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
))
2143 vmem_free(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
);
2145 kobj_free(mp
->ctfdata
, mp
->ctfsize
);
2149 kobj_free(mp
->sigdata
, mp
->sigsize
);
2152 * We did not get far enough into kobj_export_ksyms() to free allocated
2153 * buffers because we encounted error conditions. Free the buffers.
2155 if ((ksyms_exported
== 0) && (mp
->shdrs
!= NULL
)) {
2159 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2160 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2161 switch (shp
->sh_type
) {
2164 if (shp
->sh_addr
!= 0)
2165 kobj_free((void *)shp
->sh_addr
,
2171 if (!(mp
->flags
& KOBJ_PRIM
)) {
2172 kobj_free(mp
->shdrs
,
2173 mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
);
2178 vmem_free(data_arena
, (void *)mp
->bss
, mp
->bss_size
);
2181 kobj_texthole_free(mp
->fbt_tab
, mp
->fbt_size
);
2183 if (mp
->textwin_base
)
2184 kobj_textwin_free(mp
);
2186 if (mp
->sdt_probes
!= NULL
) {
2187 sdt_probedesc_t
*sdp
= mp
->sdt_probes
, *next
;
2189 while (sdp
!= NULL
) {
2190 next
= sdp
->sdpd_next
;
2191 kobj_free(sdp
->sdpd_name
, strlen(sdp
->sdpd_name
) + 1);
2192 kobj_free(sdp
, sizeof (sdt_probedesc_t
));
2198 kobj_texthole_free(mp
->sdt_tab
, mp
->sdt_size
);
2200 vmem_free(text_arena
, mp
->text
, mp
->text_size
);
2202 vmem_free(data_arena
, mp
->data
, mp
->data_size
);
2204 kobj_free(mp
->depends_on
, strlen(mp
->depends_on
)+1);
2206 kobj_free(mp
->filename
, strlen(mp
->filename
)+1);
2208 kobj_free((char *)mp
, sizeof (*mp
));
2212 get_progbits(struct module
*mp
, struct _buf
*file
)
2214 struct proginfo
*tp
, *dp
, *sdp
;
2216 reloc_dest_t dest
= NULL
;
2218 uintptr_t text
= 0, data
, textptr
;
2222 tp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2223 dp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2224 sdp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2226 * loop through sections to find out how much space we need
2227 * for text, data, (also bss that is already assigned)
2229 if (get_progbits_size(mp
, tp
, dp
, sdp
) < 0)
2232 mp
->text_size
= tp
->size
;
2233 mp
->data_size
= dp
->size
;
2236 caddr_t limit
= _data
;
2238 if (lg_pagesize
&& _text
+ lg_pagesize
< limit
)
2239 limit
= _text
+ lg_pagesize
;
2241 mp
->text
= kobj_segbrk(&_etext
, mp
->text_size
,
2244 * If we can't grow the text segment, try the
2245 * data segment before failing.
2247 if (mp
->text
== NULL
) {
2248 mp
->text
= kobj_segbrk(&_edata
, mp
->text_size
,
2252 mp
->data
= kobj_segbrk(&_edata
, mp
->data_size
, dp
->align
, 0);
2254 if (mp
->text
== NULL
|| mp
->data
== NULL
)
2258 if (text_arena
== NULL
)
2259 kobj_vmem_init(&text_arena
, &data_arena
);
2262 * some architectures may want to load the module on a
2263 * page that is currently read only. It may not be
2264 * possible for those architectures to remap their page
2265 * on the fly. So we provide a facility for them to hang
2266 * a private hook where the memory they assign the module
2267 * is not the actual place where the module loads.
2269 * In this case there are two addresses that deal with the
2271 * 1) the final destination of the module
2272 * 2) the address that is used to view the newly
2273 * loaded module until all the relocations relative to 1
2274 * above are completed.
2276 * That is what dest is used for below.
2278 mp
->text_size
+= tp
->align
;
2279 mp
->data_size
+= dp
->align
;
2281 mp
->text
= kobj_text_alloc(text_arena
, mp
->text_size
);
2284 * a remap is taking place. Align the text ptr relative
2285 * to the secondary mapping. That is where the bits will
2288 if (kvseg
.s_base
!= NULL
&& !vmem_contains(heaptext_arena
,
2289 mp
->text
, mp
->text_size
)) {
2290 off_t off
= (uintptr_t)mp
->text
& PAGEOFFSET
;
2291 size_t size
= P2ROUNDUP(mp
->text_size
+ off
, PAGESIZE
);
2292 caddr_t map
= vmem_alloc(heap_arena
, size
, VM_SLEEP
);
2293 caddr_t orig
= mp
->text
- off
;
2294 pgcnt_t pages
= size
/ PAGESIZE
;
2296 dest
= (reloc_dest_t
)(map
+ off
);
2297 text
= ALIGN((uintptr_t)dest
, tp
->align
);
2300 hat_devload(kas
.a_hat
, map
, PAGESIZE
,
2301 hat_getpfnum(kas
.a_hat
, orig
),
2302 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
2303 HAT_LOAD_NOCONSIST
| HAT_LOAD_LOCK
);
2308 * Since we set up a non-cacheable mapping, we need
2309 * to flush any old entries in the cache that might
2310 * be left around from the read-only mapping.
2315 mp
->data
= vmem_alloc(data_arena
, mp
->data_size
,
2316 VM_SLEEP
| VM_BESTFIT
);
2318 textptr
= (uintptr_t)mp
->text
;
2319 textptr
= ALIGN(textptr
, tp
->align
);
2320 mp
->destination
= dest
;
2323 * This is the case where a remap is not being done.
2326 text
= ALIGN((uintptr_t)mp
->text
, tp
->align
);
2327 data
= ALIGN((uintptr_t)mp
->data
, dp
->align
);
2329 /* now loop though sections assigning addresses and loading the data */
2330 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2331 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2332 if (!(shp
->sh_flags
& SHF_ALLOC
))
2335 if ((shp
->sh_flags
& SHF_WRITE
) == 0)
2340 bits_ptr
= ALIGN(bits_ptr
, shp
->sh_addralign
);
2342 if (shp
->sh_type
== SHT_NOBITS
) {
2346 bzero((caddr_t
)bits_ptr
, shp
->sh_size
);
2347 shp
->sh_type
= SHT_PROGBITS
;
2349 if (kobj_read_file(file
, (char *)bits_ptr
,
2350 shp
->sh_size
, shp
->sh_offset
) < 0)
2354 if (shp
->sh_flags
& SHF_WRITE
) {
2355 shp
->sh_addr
= bits_ptr
;
2357 textptr
= ALIGN(textptr
, shp
->sh_addralign
);
2358 shp
->sh_addr
= textptr
;
2359 textptr
+= shp
->sh_size
;
2362 bits_ptr
+= shp
->sh_size
;
2363 if ((shp
->sh_flags
& SHF_WRITE
) == 0)
2372 * Free and mark as freed the section headers here so that
2373 * free_module_data() does not have to worry about this buffer.
2375 * This buffer is freed here because one of the possible reasons
2376 * for error is a section with non-zero sh_addr and in that case
2377 * free_module_data() would have no way of recognizing that this
2378 * buffer was unallocated.
2381 kobj_free(mp
->shdrs
, mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
);
2385 (void) kobj_free(tp
, sizeof (struct proginfo
));
2386 (void) kobj_free(dp
, sizeof (struct proginfo
));
2387 (void) kobj_free(sdp
, sizeof (struct proginfo
));
2393 * Go through suppress_sym_list to see if "multiply defined"
2394 * warning of this symbol should be suppressed. Return 1 if
2395 * warning should be suppressed, 0 otherwise.
2398 kobj_suppress_warning(char *symname
)
2402 for (i
= 0; suppress_sym_list
[i
] != NULL
; i
++) {
2403 if (strcmp(suppress_sym_list
[i
], symname
) == 0)
2411 get_syms(struct module
*mp
, struct _buf
*file
)
2421 * Find the interesting sections.
2423 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2424 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2425 switch (shp
->sh_type
) {
2427 mp
->symtbl_section
= shn
;
2440 /* KM_TMP since kobj_free'd in do_relocations */
2441 shp
->sh_addr
= (Addr
)
2442 kobj_alloc(shp
->sh_size
, KM_WAIT
|KM_TMP
);
2444 if (kobj_read_file(file
, (char *)shp
->sh_addr
,
2445 shp
->sh_size
, shp
->sh_offset
) < 0) {
2446 _kobj_printf(ops
, "krtld: get_syms: %s, ",
2448 _kobj_printf(ops
, "error reading section %d\n",
2457 * This is true for a stripped executable. In the case of
2458 * 'unix' it can be stripped but it still contains the SHT_DYNSYM,
2459 * and since that symbol information is still present everything
2463 if (mp
->flags
& KOBJ_EXEC
)
2465 _kobj_printf(ops
, "krtld: get_syms: %s ",
2467 _kobj_printf(ops
, "no SHT_SYMTAB symbol table found\n");
2472 * get the associated string table header
2474 if ((mp
->symhdr
== 0) || (mp
->symhdr
->sh_link
>= mp
->hdr
.e_shnum
))
2476 mp
->strhdr
= (Shdr
*)
2477 (mp
->shdrs
+ mp
->symhdr
->sh_link
* mp
->hdr
.e_shentsize
);
2479 mp
->nsyms
= mp
->symhdr
->sh_size
/ mp
->symhdr
->sh_entsize
;
2480 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
2483 * Allocate space for the symbol table, buckets, chains, and strings.
2485 mp
->symsize
= mp
->symhdr
->sh_size
+
2486 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
) + mp
->strhdr
->sh_size
;
2487 mp
->symspace
= kobj_zalloc(mp
->symsize
, KM_WAIT
|KM_SCRATCH
);
2489 mp
->symtbl
= mp
->symspace
;
2490 mp
->buckets
= (symid_t
*)(mp
->symtbl
+ mp
->symhdr
->sh_size
);
2491 mp
->chains
= mp
->buckets
+ mp
->hashsize
;
2492 mp
->strings
= (char *)(mp
->chains
+ mp
->nsyms
);
2494 if (kobj_read_file(file
, mp
->symtbl
,
2495 mp
->symhdr
->sh_size
, mp
->symhdr
->sh_offset
) < 0 ||
2496 kobj_read_file(file
, mp
->strings
,
2497 mp
->strhdr
->sh_size
, mp
->strhdr
->sh_offset
) < 0)
2501 * loop through the symbol table adjusting values to account
2502 * for where each section got loaded into memory. Also
2503 * fill in the hash table.
2505 for (i
= 1; i
< mp
->nsyms
; i
++) {
2506 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
2507 if (sp
->st_shndx
< SHN_LORESERVE
) {
2508 if (sp
->st_shndx
>= mp
->hdr
.e_shnum
) {
2509 _kobj_printf(ops
, "%s bad shndx ",
2511 _kobj_printf(ops
, "in symbol %d\n", i
);
2516 sp
->st_shndx
* mp
->hdr
.e_shentsize
);
2517 if (!(mp
->flags
& KOBJ_EXEC
))
2518 sp
->st_value
+= shp
->sh_addr
;
2521 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
2523 if (sp
->st_name
>= mp
->strhdr
->sh_size
)
2526 symname
= mp
->strings
+ sp
->st_name
;
2528 if (!(mp
->flags
& KOBJ_EXEC
) &&
2529 ELF_ST_BIND(sp
->st_info
) == STB_GLOBAL
) {
2530 ksp
= kobj_lookup_all(mp
, symname
, 0);
2532 if (ksp
&& ELF_ST_BIND(ksp
->st_info
) == STB_GLOBAL
&&
2533 !kobj_suppress_warning(symname
) &&
2534 sp
->st_shndx
!= SHN_UNDEF
&&
2535 sp
->st_shndx
!= SHN_COMMON
&&
2536 ksp
->st_shndx
!= SHN_UNDEF
&&
2537 ksp
->st_shndx
!= SHN_COMMON
) {
2539 * Unless this symbol is a stub, it's multiply
2540 * defined. Multiply-defined symbols are
2541 * usually bad, but some objects (kmdb) have
2542 * a legitimate need to have their own
2543 * copies of common functions.
2546 ksp
->st_value
< (uintptr_t)stubs_base
||
2547 ksp
->st_value
>= (uintptr_t)stubs_end
) &&
2548 !(mp
->flags
& KOBJ_IGNMULDEF
)) {
2550 "%s symbol ", file
->_name
);
2552 "%s multiply defined\n", symname
);
2557 sym_insert(mp
, symname
, i
);
2564 get_ctf(struct module
*mp
, struct _buf
*file
)
2566 char *shstrtab
, *ctfdata
;
2571 if (_moddebug
& MODDEBUG_NOCTF
)
2572 return (0); /* do not attempt to even load CTF data */
2574 if (mp
->hdr
.e_shstrndx
>= mp
->hdr
.e_shnum
) {
2575 _kobj_printf(ops
, "krtld: get_ctf: %s, ",
2577 _kobj_printf(ops
, "corrupt e_shstrndx %u\n",
2578 mp
->hdr
.e_shstrndx
);
2582 shp
= (Shdr
*)(mp
->shdrs
+ mp
->hdr
.e_shstrndx
* mp
->hdr
.e_shentsize
);
2583 shstrlen
= shp
->sh_size
;
2584 shstrtab
= kobj_alloc(shstrlen
, KM_WAIT
|KM_TMP
);
2586 if (kobj_read_file(file
, shstrtab
, shstrlen
, shp
->sh_offset
) < 0) {
2587 _kobj_printf(ops
, "krtld: get_ctf: %s, ",
2589 _kobj_printf(ops
, "error reading section %u\n",
2590 mp
->hdr
.e_shstrndx
);
2591 kobj_free(shstrtab
, shstrlen
);
2595 for (i
= 0; i
< mp
->hdr
.e_shnum
; i
++) {
2596 shp
= (Shdr
*)(mp
->shdrs
+ i
* mp
->hdr
.e_shentsize
);
2598 if (shp
->sh_size
!= 0 && shp
->sh_name
< shstrlen
&&
2599 strcmp(shstrtab
+ shp
->sh_name
, ".SUNW_ctf") == 0) {
2600 ctfdata
= kobj_alloc(shp
->sh_size
, KM_WAIT
|KM_SCRATCH
);
2602 if (kobj_read_file(file
, ctfdata
, shp
->sh_size
,
2603 shp
->sh_offset
) < 0) {
2604 _kobj_printf(ops
, "krtld: get_ctf: %s, error "
2605 "reading .SUNW_ctf data\n", mp
->filename
);
2606 kobj_free(ctfdata
, shp
->sh_size
);
2607 kobj_free(shstrtab
, shstrlen
);
2611 mp
->ctfdata
= ctfdata
;
2612 mp
->ctfsize
= shp
->sh_size
;
2617 kobj_free(shstrtab
, shstrlen
);
2621 #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */
2624 * Return the hash of the ELF sections that are memory resident.
2625 * i.e. text and data. We skip a SHT_NOBITS section since it occupies
2626 * no space in the file. We use SHA1 here since libelfsign uses
2627 * it and both places need to use the same algorithm.
2630 crypto_es_hash(struct module
*mp
, char *hash
, char *shstrtab
)
2638 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2639 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2640 if (!(shp
->sh_flags
& SHF_ALLOC
) || shp
->sh_size
== 0)
2644 * The check should ideally be shp->sh_type == SHT_NOBITS.
2645 * However, we can't do that check here as get_progbits()
2648 if (strcmp(shstrtab
+ shp
->sh_name
, ".bss") == 0)
2651 if (kobj_debug
& D_DEBUG
)
2653 "krtld: crypto_es_hash: updating hash with"
2654 " %s data size=%d\n", shstrtab
+ shp
->sh_name
,
2657 ASSERT(shp
->sh_addr
!= (uintptr_t)NULL
);
2658 SHA1Update(&ctx
, (const uint8_t *)shp
->sh_addr
, shp
->sh_size
);
2661 SHA1Final((uchar_t
*)hash
, &ctx
);
2665 * Get the .SUNW_signature section for the module, it it exists.
2667 * This section exists only for crypto modules. None of the
2668 * primary modules have this section currently.
2671 get_signature(struct module
*mp
, struct _buf
*file
)
2673 char *shstrtab
, *sigdata
= NULL
;
2678 if (mp
->hdr
.e_shstrndx
>= mp
->hdr
.e_shnum
) {
2679 _kobj_printf(ops
, "krtld: get_signature: %s, ",
2681 _kobj_printf(ops
, "corrupt e_shstrndx %u\n",
2682 mp
->hdr
.e_shstrndx
);
2686 shp
= (Shdr
*)(mp
->shdrs
+ mp
->hdr
.e_shstrndx
* mp
->hdr
.e_shentsize
);
2687 shstrlen
= shp
->sh_size
;
2688 shstrtab
= kobj_alloc(shstrlen
, KM_WAIT
|KM_TMP
);
2690 if (kobj_read_file(file
, shstrtab
, shstrlen
, shp
->sh_offset
) < 0) {
2691 _kobj_printf(ops
, "krtld: get_signature: %s, ",
2693 _kobj_printf(ops
, "error reading section %u\n",
2694 mp
->hdr
.e_shstrndx
);
2695 kobj_free(shstrtab
, shstrlen
);
2699 for (i
= 0; i
< mp
->hdr
.e_shnum
; i
++) {
2700 shp
= (Shdr
*)(mp
->shdrs
+ i
* mp
->hdr
.e_shentsize
);
2701 if (shp
->sh_size
!= 0 && shp
->sh_name
< shstrlen
&&
2702 strcmp(shstrtab
+ shp
->sh_name
,
2703 ELF_SIGNATURE_SECTION
) == 0) {
2704 filesig_vers_t filesig_version
;
2705 size_t sigsize
= shp
->sh_size
+ SHA1_DIGEST_LENGTH
;
2706 sigdata
= kobj_alloc(sigsize
, KM_WAIT
|KM_SCRATCH
);
2708 if (kobj_read_file(file
, sigdata
, shp
->sh_size
,
2709 shp
->sh_offset
) < 0) {
2710 _kobj_printf(ops
, "krtld: get_signature: %s,"
2711 " error reading .SUNW_signature data\n",
2713 kobj_free(sigdata
, sigsize
);
2714 kobj_free(shstrtab
, shstrlen
);
2717 filesig_version
= ((struct filesignatures
*)sigdata
)->
2718 filesig_sig
.filesig_version
;
2719 if (!(filesig_version
== FILESIG_VERSION1
||
2720 filesig_version
== FILESIG_VERSION3
)) {
2721 /* skip versions we don't understand */
2722 kobj_free(sigdata
, sigsize
);
2723 kobj_free(shstrtab
, shstrlen
);
2727 mp
->sigdata
= sigdata
;
2728 mp
->sigsize
= sigsize
;
2733 if (sigdata
!= NULL
) {
2734 crypto_es_hash(mp
, sigdata
+ shp
->sh_size
, shstrtab
);
2737 kobj_free(shstrtab
, shstrlen
);
2741 add_dependent(struct module
*mp
, struct module
*dep
)
2743 struct module_list
*lp
;
2745 for (lp
= mp
->head
; lp
; lp
= lp
->next
) {
2747 return; /* already on the list */
2751 lp
= kobj_zalloc(sizeof (*lp
), KM_WAIT
);
2756 mp
->tail
->next
= lp
;
2764 do_dependents(struct modctl
*modp
, char *modname
, size_t modnamelen
)
2770 char *err_modname
= NULL
;
2774 if ((p
= mp
->depends_on
) == NULL
)
2781 while (*p
&& (*p
== ' ' || *p
== '\t'))
2789 while (*p
&& *p
!= ' ' && *p
!= '\t') {
2790 if (c
< modnamelen
- 1) {
2800 if (c
== modnamelen
- 1) {
2801 char *dep
= kobj_alloc(p
- d
+ 1, KM_WAIT
|KM_TMP
);
2803 (void) strncpy(dep
, d
, p
- d
+ 1);
2806 _kobj_printf(ops
, "%s: dependency ", modp
->mod_modname
);
2807 _kobj_printf(ops
, "'%s' too long ", dep
);
2808 _kobj_printf(ops
, "(max %d chars)\n", modnamelen
);
2810 kobj_free(dep
, p
- d
+ 1);
2816 if ((req
= mod_load_requisite(modp
, modname
)) == NULL
) {
2818 if (_moddebug
& MODDEBUG_LOADMSG
) {
2819 #endif /* KOBJ_DEBUG */
2821 "%s: unable to resolve dependency, ",
2823 _kobj_printf(ops
, "cannot load module '%s'\n",
2827 #endif /* KOBJ_DEBUG */
2828 if (err_modname
== NULL
) {
2830 * This must be the same size as the modname
2833 err_modname
= kobj_zalloc(MODMAXNAMELEN
,
2837 * We can use strcpy() here without fearing
2838 * the NULL terminator because the size of
2839 * err_modname is the same as one of modname,
2840 * and it's filled with zeros.
2842 (void) strcpy(err_modname
, modname
);
2847 add_dependent(mp
, req
->mod_mp
);
2848 mod_release_mod(req
);
2852 if (err_modname
!= NULL
) {
2854 * Copy the first module name where you detect an error to keep
2855 * its behavior the same as before.
2856 * This way keeps minimizing the memory use for error
2857 * modules, and this might be important at boot time because
2858 * the memory usage is a crucial factor for booting in most
2859 * cases. You can expect more verbose messages when using
2860 * a debug kernel or setting a bit in moddebug.
2862 bzero(modname
, MODMAXNAMELEN
);
2863 (void) strcpy(modname
, err_modname
);
2864 kobj_free(err_modname
, MODMAXNAMELEN
);
2872 do_common(struct module
*mp
)
2877 * first time through, assign all symbols defined in other
2878 * modules, and count up how much common space will be needed
2879 * (bss_size and bss_align)
2881 if ((err
= do_symbols(mp
, 0)) < 0)
2884 * increase bss_size by the maximum delta that could be
2885 * computed by the ALIGN below
2887 mp
->bss_size
+= mp
->bss_align
;
2890 mp
->bss
= (uintptr_t)kobj_segbrk(&_edata
, mp
->bss_size
,
2893 mp
->bss
= (uintptr_t)vmem_alloc(data_arena
,
2894 mp
->bss_size
, VM_SLEEP
| VM_BESTFIT
);
2895 bzero((void *)mp
->bss
, mp
->bss_size
);
2896 /* now assign addresses to all common symbols */
2897 if ((err
= do_symbols(mp
, ALIGN(mp
->bss
, mp
->bss_align
))) < 0)
2904 do_symbols(struct module
*mp
, Elf64_Addr bss_base
)
2916 * Nothing left to do (optimization).
2918 if (mp
->flags
& KOBJ_RESOLVED
)
2921 assign
= (bss_base
) ? 1 : 0;
2926 for (i
= 1; i
< mp
->nsyms
; i
++) {
2927 sp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_entsize
* i
);
2929 * we know that st_name is in bounds, since get_sections
2930 * has already checked all of the symbols
2932 name
= mp
->strings
+ sp
->st_name
;
2933 if (sp
->st_shndx
!= SHN_UNDEF
&& sp
->st_shndx
!= SHN_COMMON
)
2936 * TLS symbols are ignored in the kernel
2938 if (ELF_ST_TYPE(sp
->st_info
) == STT_TLS
) {
2939 _kobj_printf(ops
, "%s: TLS symbol ",
2941 _kobj_printf(ops
, "not supported '%s'\n",
2947 if (ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
) {
2948 if ((sp1
= kobj_lookup_all(mp
, name
, 0)) != NULL
) {
2949 sp
->st_shndx
= SHN_ABS
;
2950 sp
->st_value
= sp1
->st_value
;
2955 if (sp
->st_shndx
== SHN_UNDEF
) {
2958 if (strncmp(name
, sdt_prefix
, strlen(sdt_prefix
)) == 0)
2962 * If it's not a weak reference and it's
2963 * not a primary object, it's an error.
2964 * (Primary objects may take more than
2965 * one pass to resolve)
2967 if (!(mp
->flags
& KOBJ_PRIM
) &&
2968 ELF_ST_BIND(sp
->st_info
) != STB_WEAK
) {
2969 _kobj_printf(ops
, "%s: undefined symbol",
2971 _kobj_printf(ops
, " '%s'\n", name
);
2973 * Try to determine whether this symbol
2974 * represents a dependency on obsolete
2975 * unsafe driver support. This is just
2976 * to make the warning more informative.
2978 if (strcmp(name
, "sleep") == 0 ||
2979 strcmp(name
, "unsleep") == 0 ||
2980 strcmp(name
, "wakeup") == 0 ||
2981 strcmp(name
, "bsd_compat_ioctl") == 0 ||
2982 strcmp(name
, "unsafe_driver") == 0 ||
2983 strncmp(name
, "spl", 3) == 0 ||
2984 strncmp(name
, "i_ddi_spl", 9) == 0)
2992 * It's a common symbol - st_value is the
2993 * required alignment.
2995 if (sp
->st_value
> bss_align
)
2996 bss_align
= sp
->st_value
;
2997 bss_ptr
= ALIGN(bss_ptr
, sp
->st_value
);
2999 sp
->st_shndx
= SHN_ABS
;
3000 sp
->st_value
= bss_ptr
;
3002 bss_ptr
+= sp
->st_size
;
3006 if (assign
== 0 && mp
->bss
== (uintptr_t)NULL
) {
3007 mp
->bss_align
= bss_align
;
3008 mp
->bss_size
= bss_ptr
;
3009 } else if (resolved
) {
3010 mp
->flags
|= KOBJ_RESOLVED
;
3017 kobj_hash_name(const char *p
)
3024 hval
= (hval
<< 4) + *p
++;
3025 if ((g
= (hval
& 0xf0000000)) != 0)
3032 /* look for name in all modules */
3034 kobj_getsymvalue(char *name
, int kernelonly
)
3037 struct modctl
*modp
;
3039 uintptr_t value
= 0;
3041 if ((sp
= kobj_lookup_kernel(name
)) != NULL
)
3042 return ((uintptr_t)sp
->st_value
);
3045 return (0); /* didn't find it in the kernel so give up */
3047 mutex_enter(&mod_lock
);
3050 mp
= (struct module
*)modp
->mod_mp
;
3051 if (mp
&& !(mp
->flags
& KOBJ_PRIM
) && modp
->mod_loaded
&&
3052 (sp
= lookup_one(mp
, name
))) {
3053 value
= (uintptr_t)sp
->st_value
;
3056 } while ((modp
= modp
->mod_next
) != &modules
);
3057 mutex_exit(&mod_lock
);
3061 /* look for a symbol near value. */
3063 kobj_getsymname(uintptr_t value
, ulong_t
*offset
)
3066 struct modctl
*modp
;
3068 struct modctl_list
*lp
;
3072 * Loop through the primary kernel modules.
3074 for (lp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); lp
; lp
= lp
->modl_next
) {
3077 if ((name
= kobj_searchsym(mp
, value
, offset
)) != NULL
)
3081 mutex_enter(&mod_lock
);
3084 mp
= (struct module
*)modp
->mod_mp
;
3085 if (mp
&& !(mp
->flags
& KOBJ_PRIM
) && modp
->mod_loaded
&&
3086 (name
= kobj_searchsym(mp
, value
, offset
)))
3088 } while ((modp
= modp
->mod_next
) != &modules
);
3089 mutex_exit(&mod_lock
);
3093 /* return address of symbol and size */
3096 kobj_getelfsym(char *name
, void *mp
, int *size
)
3101 sp
= kobj_lookup_kernel(name
);
3103 sp
= lookup_one(mp
, name
);
3108 *size
= (int)sp
->st_size
;
3109 return ((uintptr_t)sp
->st_value
);
3113 kobj_lookup(struct module
*mod
, const char *name
)
3117 sp
= lookup_one(mod
, name
);
3122 return ((uintptr_t)sp
->st_value
);
3126 kobj_searchsym(struct module
*mp
, uintptr_t value
, ulong_t
*offset
)
3135 *offset
= (ulong_t
)-1l; /* assume not found */
3138 if (kobj_addrcheck(mp
, (void *)value
) != 0)
3139 return (NULL
); /* not in this module */
3141 strtabptr
= mp
->strings
;
3142 symtabptr
= (Sym
*)mp
->symtbl
;
3145 * Scan the module's symbol table for a symbol <= value
3147 for (symnum
= 1, sym
= symtabptr
+ 1;
3148 symnum
< mp
->nsyms
; symnum
++, sym
= (Sym
*)
3149 ((uintptr_t)sym
+ mp
->symhdr
->sh_entsize
)) {
3150 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
) {
3151 if (ELF_ST_BIND(sym
->st_info
) != STB_LOCAL
)
3153 if (ELF_ST_TYPE(sym
->st_info
) != STT_OBJECT
&&
3154 ELF_ST_TYPE(sym
->st_info
) != STT_FUNC
)
3158 curval
= (uintptr_t)sym
->st_value
;
3164 * If one or both are functions...
3166 if (ELF_ST_TYPE(sym
->st_info
) == STT_FUNC
|| (cursym
!= NULL
&&
3167 ELF_ST_TYPE(cursym
->st_info
) == STT_FUNC
)) {
3168 /* Ignore if the address is out of the bounds */
3169 if (value
- sym
->st_value
>= sym
->st_size
)
3172 if (cursym
!= NULL
&&
3173 ELF_ST_TYPE(cursym
->st_info
) == STT_FUNC
) {
3174 /* Prefer the function to the non-function */
3175 if (ELF_ST_TYPE(sym
->st_info
) != STT_FUNC
)
3178 /* Prefer the larger of the two functions */
3179 if (sym
->st_size
<= cursym
->st_size
)
3182 } else if (value
- curval
>= *offset
) {
3186 *offset
= (ulong_t
)(value
- curval
);
3192 return (strtabptr
+ cursym
->st_name
);
3196 kobj_lookup_all(struct module
*mp
, char *name
, int include_self
)
3199 struct module_list
*mlp
;
3200 struct modctl_list
*clp
;
3203 if (include_self
&& (sp
= lookup_one(mp
, name
)) != NULL
)
3206 for (mlp
= mp
->head
; mlp
; mlp
= mlp
->next
) {
3207 if ((sp
= lookup_one(mlp
->mp
, name
)) != NULL
&&
3208 ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
)
3213 * Loop through the primary kernel modules.
3215 for (clp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); clp
; clp
= clp
->modl_next
) {
3218 if (mmp
== NULL
|| mp
== mmp
)
3221 if ((sp
= lookup_one(mmp
, name
)) != NULL
&&
3222 ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
)
3229 kobj_lookup_kernel(const char *name
)
3231 struct modctl_list
*lp
;
3236 * Loop through the primary kernel modules.
3238 for (lp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); lp
; lp
= lp
->modl_next
) {
3244 if ((sp
= lookup_one(mp
, name
)) != NULL
)
3251 lookup_one(struct module
*mp
, const char *name
)
3257 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3258 ip
= &mp
->chains
[*ip
]) {
3259 sp
= (Sym
*)(mp
->symtbl
+
3260 mp
->symhdr
->sh_entsize
* *ip
);
3261 name1
= mp
->strings
+ sp
->st_name
;
3262 if (strcmp(name
, name1
) == 0 &&
3263 ELF_ST_TYPE(sp
->st_info
) != STT_FILE
&&
3264 sp
->st_shndx
!= SHN_UNDEF
&&
3265 sp
->st_shndx
!= SHN_COMMON
)
3272 * Lookup a given symbol pointer in the module's symbol hash. If the symbol
3273 * is hashed, return the symbol pointer; otherwise return NULL.
3276 sym_lookup(struct module
*mp
, Sym
*ksp
)
3278 char *name
= mp
->strings
+ ksp
->st_name
;
3282 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3283 ip
= &mp
->chains
[*ip
]) {
3284 sp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_entsize
* *ip
);
3292 sym_insert(struct module
*mp
, char *name
, symid_t index
)
3297 if (kobj_debug
& D_SYMBOLS
) {
3298 static struct module
*lastmp
= NULL
;
3302 "krtld: symbol entry: file=%s\n",
3305 "krtld:\tsymndx\tvalue\t\t"
3309 sp
= (Sym
*)(mp
->symtbl
+
3310 index
* mp
->symhdr
->sh_entsize
);
3311 _kobj_printf(ops
, "krtld:\t[%3d]", index
);
3312 _kobj_printf(ops
, "\t0x%lx", sp
->st_value
);
3313 _kobj_printf(ops
, "\t%s\n", name
);
3317 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3318 ip
= &mp
->chains
[*ip
]) {
3325 kobj_boot_mod_lookup(const char *modname
)
3327 struct modctl
*mctl
= kobj_modules
;
3330 if (strcmp(modname
, mctl
->mod_modname
) == 0)
3332 } while ((mctl
= mctl
->mod_next
) != kobj_modules
);
3338 * Determine if the module exists.
3341 kobj_path_exists(char *name
, int use_path
)
3345 file
= kobj_open_path(name
, use_path
);
3346 if (file
== (struct _buf
*)-1)
3348 kobj_close_file(file
);
3353 * fullname is dynamically allocated to be able to hold the
3354 * maximum size string that can be constructed from name.
3355 * path is exactly like the shell PATH variable.
3358 kobj_open_path(char *name
, int use_path
)
3368 pathp
= ""; /* use name as specified */
3370 pathp
= kobj_module_path
;
3371 /* use configured default path */
3373 pathpsave
= pathp
; /* keep this for error reporting */
3376 * Allocate enough space for the largest possible fullname.
3377 * since path is of the form <directory> : <directory> : ...
3378 * we're potentially allocating a little more than we need to
3379 * but we'll allocate the exact amount when we find the right directory.
3380 * (The + 3 below is one for NULL terminator and one for the '/'
3381 * we might have to add at the beginning of path and one for
3382 * the '/' between path and name.)
3384 maxpathlen
= strlen(pathp
) + strlen(name
) + 3;
3385 fullname
= kobj_zalloc(maxpathlen
, KM_WAIT
);
3389 if (*pathp
!= '\0' && *pathp
!= '/')
3390 *p
++ = '/'; /* path must start with '/' */
3391 while (*pathp
&& *pathp
!= ':' && *pathp
!= ' ')
3393 if (p
!= fullname
&& p
[-1] != '/')
3399 if ((file
= kobj_open_file(fullname
)) != (struct _buf
*)-1) {
3400 kobj_free(fullname
, maxpathlen
);
3403 while (*pathp
== ' ' || *pathp
== ':')
3409 kobj_free(fullname
, maxpathlen
);
3410 if (_moddebug
& MODDEBUG_ERRMSG
) {
3411 _kobj_printf(ops
, "can't open %s,", name
);
3412 _kobj_printf(ops
, " path is %s\n", pathpsave
);
3414 return ((struct _buf
*)-1);
3418 kobj_open(char *filename
)
3423 if (_modrootloaded
) {
3424 struct kobjopen_tctl
*ltp
= kobjopen_alloc(filename
);
3428 * Hand off the open to a thread who has a
3429 * stack size capable handling the request.
3431 if (curthread
!= &t0
) {
3432 (void) thread_create(NULL
, DEFAULTSTKSZ
* 2,
3433 kobjopen_thread
, ltp
, 0, &p0
, TS_RUN
, maxclsyspri
);
3439 * 1098067: module creds should not be those of the
3442 cred_t
*saved_cred
= curthread
->t_cred
;
3443 curthread
->t_cred
= kcred
;
3444 Errno
= vn_openat(filename
, UIO_SYSSPACE
, FREAD
, 0, &vp
,
3446 curthread
->t_cred
= saved_cred
;
3451 if (_moddebug
& MODDEBUG_ERRMSG
) {
3453 "kobj_open: vn_open of %s fails, ",
3455 _kobj_printf(ops
, "Errno = %d\n", Errno
);
3459 if (_moddebug
& MODDEBUG_ERRMSG
) {
3460 _kobj_printf(ops
, "kobj_open: '%s'", filename
);
3461 _kobj_printf(ops
, " vp = %p\n", vp
);
3463 return ((intptr_t)vp
);
3466 fd
= kobj_boot_open(filename
, 0);
3468 if (_moddebug
& MODDEBUG_ERRMSG
) {
3471 "kobj_open: can't open %s\n", filename
);
3473 _kobj_printf(ops
, "kobj_open: '%s'", filename
);
3474 _kobj_printf(ops
, " descr = 0x%x\n", fd
);
3477 return ((intptr_t)fd
);
3482 * Calls to kobj_open() are handled off to this routine as a separate thread.
3485 kobjopen_thread(struct kobjopen_tctl
*ltp
)
3490 mutex_init(&cpr_lk
, NULL
, MUTEX_DEFAULT
, NULL
);
3491 CALLB_CPR_INIT(&cpr_i
, &cpr_lk
, callb_generic_cpr
, "kobjopen");
3492 ltp
->Errno
= vn_open(ltp
->name
, UIO_SYSSPACE
, FREAD
, 0, &(ltp
->vp
),
3495 mutex_enter(&cpr_lk
);
3496 CALLB_CPR_EXIT(&cpr_i
);
3497 mutex_destroy(&cpr_lk
);
3502 * allocate and initialize a kobjopen thread structure
3504 static struct kobjopen_tctl
*
3505 kobjopen_alloc(char *filename
)
3507 struct kobjopen_tctl
*ltp
= kmem_zalloc(sizeof (*ltp
), KM_SLEEP
);
3509 ASSERT(filename
!= NULL
);
3511 ltp
->name
= kmem_alloc(strlen(filename
) + 1, KM_SLEEP
);
3512 bcopy(filename
, ltp
->name
, strlen(filename
) + 1);
3513 sema_init(<p
->sema
, 0, NULL
, SEMA_DEFAULT
, NULL
);
3518 * free a kobjopen thread control structure
3521 kobjopen_free(struct kobjopen_tctl
*ltp
)
3523 sema_destroy(<p
->sema
);
3524 kmem_free(ltp
->name
, strlen(ltp
->name
) + 1);
3525 kmem_free(ltp
, sizeof (*ltp
));
3529 kobj_read(intptr_t descr
, char *buf
, uint_t size
, uint_t offset
)
3534 if (_modrootloaded
) {
3535 if ((stat
= vn_rdwr(UIO_READ
, (struct vnode
*)descr
, buf
, size
,
3536 (offset_t
)offset
, UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(),
3539 "vn_rdwr failed with error 0x%x\n", stat
);
3542 return (size
- resid
);
3546 if (kobj_boot_seek((int)descr
, (off_t
)0, offset
) != 0) {
3548 "kobj_read: seek 0x%x failed\n", offset
);
3552 count
= kobj_boot_read((int)descr
, buf
, size
);
3554 if (_moddebug
& MODDEBUG_ERRMSG
) {
3556 "kobj_read: req %d bytes, ", size
);
3557 _kobj_printf(ops
, "got %d\n", count
);
3565 kobj_close(intptr_t descr
)
3567 if (_moddebug
& MODDEBUG_ERRMSG
)
3568 _kobj_printf(ops
, "kobj_close: 0x%lx\n", descr
);
3570 if (_modrootloaded
) {
3571 struct vnode
*vp
= (struct vnode
*)descr
;
3572 (void) fop_close(vp
, FREAD
, 1, (offset_t
)0, CRED(), NULL
);
3575 (void) kobj_boot_close((int)descr
);
3579 kobj_fstat(intptr_t descr
, struct bootstat
*buf
)
3584 if (_modrootloaded
) {
3586 struct vnode
*vp
= (struct vnode
*)descr
;
3587 if (fop_getattr(vp
, &vattr
, 0, kcred
, NULL
) != 0)
3591 * The vattr and bootstat structures are similar, but not
3592 * identical. We do our best to fill in the bootstat structure
3593 * from the contents of vattr (transfering only the ones that
3597 buf
->st_mode
= (uint32_t)vattr
.va_mode
;
3598 buf
->st_nlink
= (uint32_t)vattr
.va_nlink
;
3599 buf
->st_uid
= (int32_t)vattr
.va_uid
;
3600 buf
->st_gid
= (int32_t)vattr
.va_gid
;
3601 buf
->st_rdev
= (uint64_t)vattr
.va_rdev
;
3602 buf
->st_size
= (uint64_t)vattr
.va_size
;
3603 buf
->st_atim
.tv_sec
= (int64_t)vattr
.va_atime
.tv_sec
;
3604 buf
->st_atim
.tv_nsec
= (int64_t)vattr
.va_atime
.tv_nsec
;
3605 buf
->st_mtim
.tv_sec
= (int64_t)vattr
.va_mtime
.tv_sec
;
3606 buf
->st_mtim
.tv_nsec
= (int64_t)vattr
.va_mtime
.tv_nsec
;
3607 buf
->st_ctim
.tv_sec
= (int64_t)vattr
.va_ctime
.tv_sec
;
3608 buf
->st_ctim
.tv_nsec
= (int64_t)vattr
.va_ctime
.tv_nsec
;
3609 buf
->st_blksize
= (int32_t)vattr
.va_blksize
;
3610 buf
->st_blocks
= (int64_t)vattr
.va_nblocks
;
3615 return (kobj_boot_fstat((int)descr
, buf
));
3620 kobj_open_file(char *name
)
3623 struct compinfo cbuf
;
3626 if ((fd
= kobj_open(name
)) == -1) {
3627 return ((struct _buf
*)-1);
3630 file
= kobj_zalloc(sizeof (struct _buf
), KM_WAIT
|KM_TMP
);
3632 file
->_name
= kobj_alloc(strlen(name
)+1, KM_WAIT
|KM_TMP
);
3633 file
->_cnt
= file
->_size
= file
->_off
= 0;
3635 file
->_ptr
= file
->_base
;
3636 (void) strcpy(file
->_name
, name
);
3639 * Before root is mounted, we must check
3640 * for a compressed file and do our own
3643 if (_modrootloaded
) {
3644 file
->_base
= kobj_zalloc(MAXBSIZE
, KM_WAIT
);
3645 file
->_bsize
= MAXBSIZE
;
3647 /* Check if the file is compressed */
3648 file
->_iscmp
= kobj_is_compressed(fd
);
3650 if (kobj_boot_compinfo(fd
, &cbuf
) != 0) {
3651 kobj_close_file(file
);
3652 return ((struct _buf
*)-1);
3654 file
->_iscmp
= cbuf
.iscmp
;
3656 if (kobj_comp_setup(file
, &cbuf
) != 0) {
3657 kobj_close_file(file
);
3658 return ((struct _buf
*)-1);
3661 file
->_base
= kobj_zalloc(cbuf
.blksize
, KM_WAIT
|KM_TMP
);
3662 file
->_bsize
= cbuf
.blksize
;
3669 kobj_comp_setup(struct _buf
*file
, struct compinfo
*cip
)
3671 struct comphdr
*hdr
;
3674 * read the compressed image into memory,
3675 * so we can deompress from there
3677 file
->_dsize
= cip
->fsize
;
3678 file
->_dbuf
= kobj_alloc(cip
->fsize
, KM_WAIT
|KM_TMP
);
3679 if (kobj_read(file
->_fd
, file
->_dbuf
, cip
->fsize
, 0) != cip
->fsize
) {
3680 kobj_free(file
->_dbuf
, cip
->fsize
);
3684 hdr
= kobj_comphdr(file
);
3685 if (hdr
->ch_magic
!= CH_MAGIC_ZLIB
|| hdr
->ch_version
!= CH_VERSION
||
3686 hdr
->ch_algorithm
!= CH_ALG_ZLIB
|| hdr
->ch_fsize
== 0 ||
3687 !ISP2(hdr
->ch_blksize
)) {
3688 kobj_free(file
->_dbuf
, cip
->fsize
);
3691 file
->_base
= kobj_alloc(hdr
->ch_blksize
, KM_WAIT
|KM_TMP
);
3692 file
->_bsize
= hdr
->ch_blksize
;
3697 kobj_close_file(struct _buf
*file
)
3699 kobj_close(file
->_fd
);
3700 if (file
->_base
!= NULL
)
3701 kobj_free(file
->_base
, file
->_bsize
);
3702 if (file
->_dbuf
!= NULL
)
3703 kobj_free(file
->_dbuf
, file
->_dsize
);
3704 kobj_free(file
->_name
, strlen(file
->_name
)+1);
3705 kobj_free(file
, sizeof (struct _buf
));
3709 kobj_read_file(struct _buf
*file
, char *buf
, uint_t size
, uint_t off
)
3712 int b_off
; /* Offset into buffer for start of bcopy */
3716 if (_moddebug
& MODDEBUG_ERRMSG
) {
3717 _kobj_printf(ops
, "kobj_read_file: size=%x,", size
);
3718 _kobj_printf(ops
, " offset=%x at", off
);
3719 _kobj_printf(ops
, " buf=%x\n", buf
);
3723 * Handle compressed (gzip for now) file here. First get the
3724 * compressed size, then read the image into memory and finally
3725 * call zlib to decompress the image at the supplied memory buffer.
3727 if (file
->_iscmp
== CH_MAGIC_GZIP
) {
3730 struct vnode
*vp
= (struct vnode
*)file
->_fd
;
3734 if (fop_getattr(vp
, &vattr
, 0, kcred
, NULL
) != 0)
3737 file
->_dbuf
= kobj_alloc(vattr
.va_size
, KM_WAIT
|KM_TMP
);
3738 file
->_dsize
= vattr
.va_size
;
3740 /* Read the compressed file into memory */
3741 if ((err
= vn_rdwr(UIO_READ
, vp
, file
->_dbuf
, vattr
.va_size
,
3742 (offset_t
)(0), UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(),
3745 _kobj_printf(ops
, "kobj_read_file :vn_rdwr() failed, "
3746 "error code 0x%x\n", err
);
3752 /* Decompress the image at the supplied memory buffer */
3753 if ((err
= z_uncompress(buf
, &dlen
, file
->_dbuf
,
3754 vattr
.va_size
)) != Z_OK
) {
3755 _kobj_printf(ops
, "kobj_read_file: z_uncompress "
3756 "failed, error code : 0x%x\n", err
);
3761 _kobj_printf(ops
, "kobj_read_file: z_uncompress "
3762 "failed to uncompress (size returned 0x%x , "
3763 "expected size: 0x%x)\n", dlen
, size
);
3771 page_addr
= F_PAGE(file
, off
);
3772 b_size
= file
->_size
;
3774 * If we have the filesystem page the caller's referring to
3775 * and we have something in the buffer,
3776 * satisfy as much of the request from the buffer as we can.
3778 if (page_addr
== file
->_off
&& b_size
> 0) {
3779 b_off
= B_OFFSET(file
, off
);
3780 c_size
= b_size
- b_off
;
3782 * If there's nothing to copy, we're at EOF.
3789 if (_moddebug
& MODDEBUG_ERRMSG
)
3790 _kobj_printf(ops
, "copying %x bytes\n",
3792 bcopy(file
->_base
+b_off
, buf
, c_size
);
3798 _kobj_printf(ops
, "kobj_read: system error");
3804 * If the caller's offset is page aligned and
3805 * the caller want's at least a filesystem page and
3806 * the caller provided a buffer,
3807 * read directly into the caller's buffer.
3809 if (page_addr
== off
&&
3810 (c_size
= F_BLKS(file
, size
)) && buf
) {
3811 c_size
= kobj_read_blks(file
, buf
, c_size
,
3818 if (c_size
!= F_BLKS(file
, size
))
3824 * Otherwise, read into our buffer and copy next time
3828 file
->_off
= page_addr
;
3829 c_size
= kobj_read_blks(file
, file
->_base
,
3830 file
->_bsize
, page_addr
);
3831 file
->_ptr
= file
->_base
;
3832 file
->_cnt
= c_size
;
3833 file
->_size
= c_size
;
3835 * If a _filbuf call or nothing read, break.
3837 if (buf
== NULL
|| c_size
<= 0) {
3842 if (_moddebug
& MODDEBUG_ERRMSG
)
3843 _kobj_printf(ops
, "read %x bytes\n", c_size
);
3846 if (_moddebug
& MODDEBUG_ERRMSG
)
3847 _kobj_printf(ops
, "count = %x\n", count
);
3853 kobj_read_blks(struct _buf
*file
, char *buf
, uint_t size
, uint_t off
)
3857 ASSERT(B_OFFSET(file
, size
) == 0 && B_OFFSET(file
, off
) == 0);
3863 for (blks
= size
/ file
->_bsize
; blks
!= 0; blks
--) {
3864 nret
= kobj_uncomp_blk(file
, buf
, off
);
3870 if (nret
< file
->_bsize
)
3874 ret
= kobj_read(file
->_fd
, buf
, size
, off
);
3879 kobj_uncomp_blk(struct _buf
*file
, char *buf
, uint_t off
)
3881 struct comphdr
*hdr
= kobj_comphdr(file
);
3886 dlen
= file
->_bsize
;
3887 i
= off
/ file
->_bsize
;
3888 src
= file
->_dbuf
+ hdr
->ch_blkmap
[i
];
3889 if (i
== hdr
->ch_fsize
/ file
->_bsize
)
3890 slen
= file
->_dsize
- hdr
->ch_blkmap
[i
];
3892 slen
= hdr
->ch_blkmap
[i
+ 1] - hdr
->ch_blkmap
[i
];
3893 if (z_uncompress(buf
, &dlen
, src
, slen
) != Z_OK
)
3899 kobj_filbuf(struct _buf
*f
)
3901 if (kobj_read_file(f
, NULL
, f
->_bsize
, f
->_off
+ f
->_size
) > 0)
3902 return (kobj_getc(f
));
3907 kobj_free(void *address
, size_t size
)
3912 kmem_free(address
, size
);
3913 kobj_stat
.nfree_calls
++;
3914 kobj_stat
.nfree
+= size
;
3918 kobj_zalloc(size_t size
, int flag
)
3922 if ((v
= kobj_alloc(size
, flag
)) != 0) {
3930 kobj_alloc(size_t size
, int flag
)
3933 * If we are running standalone in the
3934 * linker, we ask boot for memory.
3935 * Either it's temporary memory that we lose
3936 * once boot is mapped out or we allocate it
3937 * permanently using the dynamic data segment.
3941 if (flag
& (KM_TMP
| KM_SCRATCH
))
3942 return (bop_temp_alloc(size
, MINALIGN
));
3944 if (flag
& (KM_TMP
| KM_SCRATCH
))
3945 return (BOP_ALLOC(ops
, 0, size
, MINALIGN
));
3947 return (kobj_segbrk(&_edata
, size
, MINALIGN
, 0));
3950 kobj_stat
.nalloc_calls
++;
3951 kobj_stat
.nalloc
+= size
;
3953 return (kmem_alloc(size
, (flag
& KM_NOWAIT
) ? KM_NOSLEEP
: KM_SLEEP
));
3957 * Allow the "mod" system to sync up with the work
3958 * already done by kobj during the initial loading
3959 * of the kernel. This also gives us a chance
3960 * to reallocate memory that belongs to boot.
3965 struct modctl_list
*lp
, **lpp
;
3968 * The module path can be set in /etc/system via 'moddir' commands
3970 if (default_path
!= NULL
)
3971 kobj_module_path
= default_path
;
3973 default_path
= kobj_module_path
;
3975 ksyms_arena
= vmem_create("ksyms", NULL
, 0, sizeof (uint64_t),
3976 segkmem_alloc
, segkmem_free
, heap_arena
, 0, VM_SLEEP
);
3978 ctf_arena
= vmem_create("ctf", NULL
, 0, sizeof (uint_t
),
3979 segkmem_alloc
, segkmem_free
, heap_arena
, 0, VM_SLEEP
);
3982 * Move symbol tables from boot memory to ksyms_arena.
3984 for (lpp
= kobj_linkmaps
; *lpp
!= NULL
; lpp
++) {
3985 for (lp
= *lpp
; lp
!= NULL
; lp
= lp
->modl_next
)
3986 kobj_export_module(mod(lp
));
3991 kobj_segbrk(caddr_t
*spp
, size_t size
, size_t align
, caddr_t limit
)
3994 size_t alloc_pgsz
= kobj_mmu_pagesize
;
3995 size_t alloc_align
= BO_NO_ALIGN
;
3999 * If we are using "large" mappings for the kernel,
4000 * request aligned memory from boot using the
4004 alloc_align
= lg_pagesize
;
4005 alloc_pgsz
= lg_pagesize
;
4009 va
= ALIGN((uintptr_t)*spp
, align
);
4010 pva
= P2ROUNDUP((uintptr_t)*spp
, alloc_pgsz
);
4014 if (va
+ size
> pva
) {
4017 alloc_size
= P2ROUNDUP(size
- (pva
- va
), alloc_pgsz
);
4019 * Check for overlapping segments.
4021 if (limit
&& limit
<= *spp
+ alloc_size
) {
4022 return ((caddr_t
)0);
4025 npva
= (uintptr_t)BOP_ALLOC(ops
, (caddr_t
)pva
,
4026 alloc_size
, alloc_align
);
4028 if (npva
== (uintptr_t)NULL
) {
4029 _kobj_printf(ops
, "BOP_ALLOC failed, 0x%lx bytes",
4031 _kobj_printf(ops
, " aligned %lx", alloc_align
);
4032 _kobj_printf(ops
, " at 0x%lx\n", pva
);
4036 *spp
= (caddr_t
)(va
+ size
);
4038 return ((caddr_t
)va
);
4042 * Calculate the number of output hash buckets.
4043 * We use the next prime larger than n / 4,
4044 * so the average hash chain is about 4 entries.
4045 * More buckets would just be a waste of memory.
4048 kobj_gethashsize(uint_t n
)
4051 int hsize
= MAX(n
/ 4, 2);
4053 for (f
= 2; f
* f
<= hsize
; f
++)
4061 * Get the file size.
4063 * Before root is mounted, files are compressed in the boot_archive ramdisk
4064 * (in the memory). kobj_fstat would return the compressed file size.
4065 * In order to get the uncompressed file size, read the file to the end and
4069 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
4075 if (_modrootloaded
) {
4076 struct bootstat bst
;
4078 if (kobj_fstat(file
->_fd
, &bst
) != 0)
4080 *size
= bst
.st_size
;
4082 if (file
->_iscmp
== CH_MAGIC_GZIP
) {
4084 * Read the last 4 bytes of the compressed (gzip)
4085 * image to get the size of its uncompressed
4088 if ((err
= vn_rdwr(UIO_READ
, (struct vnode
*)file
->_fd
,
4089 (char *)(&buf
), 4, (offset_t
)(*size
- 4),
4090 UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(), &resid
))
4092 _kobj_printf(ops
, "kobj_get_filesize: "
4093 "vn_rdwr() failed with error 0x%x\n", err
);
4097 *size
= (uint64_t)buf
;
4102 struct bootstat bsb
;
4105 struct comphdr
*hdr
= kobj_comphdr(file
);
4107 *size
= hdr
->ch_fsize
;
4108 } else if (kobj_boot_fstat(file
->_fd
, &bsb
) != 0)
4111 *size
= bsb
.st_size
;
4115 uint64_t offset
= 0;
4117 buf
= kmem_alloc(MAXBSIZE
, KM_SLEEP
);
4119 count
= kobj_read_file(file
, buf
, MAXBSIZE
, offset
);
4121 kmem_free(buf
, MAXBSIZE
);
4125 } while (count
== MAXBSIZE
);
4126 kmem_free(buf
, MAXBSIZE
);
4146 return (q
? q
+ 1 : s
);
4150 kobj_stat_get(kobj_stat_t
*kp
)
4158 return (lg_pagesize
);
4162 kobj_textwin_alloc(struct module
*mp
)
4164 ASSERT(MUTEX_HELD(&mod_lock
));
4166 if (mp
->textwin
!= NULL
)
4170 * If the text is not contained in the heap, then it is not contained
4171 * by a writable mapping. (Specifically, it's on the nucleus page.)
4172 * We allocate a read/write mapping for this module's text to allow
4173 * the text to be patched without calling hot_patch_kernel_text()
4174 * (which is quite slow).
4176 if (!vmem_contains(heaptext_arena
, mp
->text
, mp
->text_size
)) {
4177 uintptr_t text
= (uintptr_t)mp
->text
;
4178 uintptr_t size
= (uintptr_t)mp
->text_size
;
4181 size_t sz
= ((text
+ size
+ PAGESIZE
- 1) & PAGEMASK
) -
4184 va
= mp
->textwin_base
= vmem_alloc(heap_arena
, sz
, VM_SLEEP
);
4186 for (i
= text
& PAGEMASK
; i
< text
+ size
; i
+= PAGESIZE
) {
4187 hat_devload(kas
.a_hat
, va
, PAGESIZE
,
4188 hat_getpfnum(kas
.a_hat
, (caddr_t
)i
),
4189 PROT_READ
| PROT_WRITE
,
4190 HAT_LOAD_LOCK
| HAT_LOAD_NOCONSIST
);
4194 mp
->textwin
= mp
->textwin_base
+ (text
& PAGEOFFSET
);
4196 mp
->textwin
= mp
->text
;
4201 kobj_textwin_free(struct module
*mp
)
4203 uintptr_t text
= (uintptr_t)mp
->text
;
4204 uintptr_t tsize
= (uintptr_t)mp
->text_size
;
4205 size_t size
= (((text
+ tsize
+ PAGESIZE
- 1) & PAGEMASK
) -
4210 if (mp
->textwin_base
== NULL
)
4213 hat_unload(kas
.a_hat
, mp
->textwin_base
, size
, HAT_UNLOAD_UNLOCK
);
4214 vmem_free(heap_arena
, mp
->textwin_base
, size
);
4215 mp
->textwin_base
= NULL
;
4219 find_libmacro(char *name
)
4223 for (lmi
= 0; lmi
< NLIBMACROS
; lmi
++) {
4224 if (strcmp(name
, libmacros
[lmi
].lmi_macroname
) == 0)
4225 return (libmacros
[lmi
].lmi_list
);
4231 * Check for $MACRO in tail (string to expand) and expand it in path at pathend
4232 * returns path if successful, else NULL
4233 * Support multiple $MACROs expansion and the first valid path will be returned
4234 * Caller's responsibility to provide enough space in path to expand
4237 expand_libmacro(char *tail
, char *path
, char *pathend
)
4239 char c
, *p
, *p1
, *p2
, *path2
, *endp
;
4240 int diff
, lmi
, macrolen
, valid_macro
, more_macro
;
4244 * check for $MACROS between nulls or slashes
4246 p
= strchr(tail
, '$');
4249 for (lmi
= 0; lmi
< NLIBMACROS
; lmi
++) {
4250 macrolen
= libmacros
[lmi
].lmi_macrolen
;
4251 if (strncmp(p
+ 1, libmacros
[lmi
].lmi_macroname
, macrolen
) == 0)
4256 if (lmi
< NLIBMACROS
) {
4258 * The following checks are used to restrict expansion of
4259 * macros to those that form a full directory/file name
4260 * and to keep the behavior same as before. If this
4261 * restriction is removed or no longer valid in the future,
4262 * the checks below can be deleted.
4264 if ((p
== tail
) || (*(p
- 1) == '/')) {
4265 c
= *(p
+ macrolen
+ 1);
4266 if (c
== '/' || c
== '\0')
4272 p2
= strchr(p
, '/');
4274 * if no more macro to expand, then just copy whatever left
4275 * and check whether it exists
4277 if (p2
== NULL
|| strchr(p2
, '$') == NULL
) {
4278 (void) strcpy(pathend
, tail
);
4279 if ((file
= kobj_open_path(path
, 1)) !=
4280 (struct _buf
*)-1) {
4281 kobj_close_file(file
);
4287 * copy all chars before '/' and call expand_libmacro()
4291 bcopy(tail
, pathend
, diff
);
4294 return (expand_libmacro(p2
, path
, pathend
));
4300 endp
= p
+ macrolen
+ 1;
4301 if (strchr(endp
, '$') != NULL
)
4307 * copy lmi_list and split it into components.
4308 * then put the part of tail before $MACRO into path
4313 bcopy(tail
, pathend
, diff
);
4314 path2
= pathend
+ diff
;
4315 p1
= libmacros
[lmi
].lmi_list
;
4316 while (p1
&& (*p1
!= '\0')) {
4317 p2
= strchr(p1
, ':');
4320 bcopy(p1
, path2
, diff
);
4321 *(path2
+ diff
) = '\0';
4324 bcopy(p1
, path2
, diff
+ 1);
4326 /* copy endp only if there isn't any more macro to expand */
4327 if (!more_macro
&& (endp
!= NULL
))
4328 (void) strcat(path2
, endp
);
4329 file
= kobj_open_path(path
, 1);
4330 if (file
!= (struct _buf
*)-1) {
4331 kobj_close_file(file
);
4333 * if more macros to expand then call expand_libmacro(),
4334 * else return path which has the whole path
4336 if (!more_macro
|| (expand_libmacro(endp
, path
,
4337 path2
+ diff
) != NULL
)) {
4350 tnf_add_notifyunload(kobj_notify_f
*fp
)
4352 kobj_notify_list_t
*entry
;
4354 entry
= kobj_alloc(sizeof (kobj_notify_list_t
), KM_WAIT
);
4355 entry
->kn_type
= KOBJ_NOTIFY_MODUNLOADING
;
4356 entry
->kn_func
= fp
;
4357 (void) kobj_notify_add(entry
);
4362 tnf_unsplice_probes(uint_t what
, struct modctl
*mod
)
4364 tnf_probe_control_t
**p
;
4366 struct module
*mp
= mod
->mod_mp
;
4368 if (!(mp
->flags
& KOBJ_TNF_PROBE
))
4371 for (p
= &__tnf_probe_list_head
; *p
; )
4372 if (kobj_addrcheck(mp
, (char *)*p
) == 0)
4377 for (q
= &__tnf_tag_list_head
; *q
; )
4378 if (kobj_addrcheck(mp
, (char *)*q
) == 0)
4379 *q
= (tnf_tag_data_t
*)(*q
)->tag_version
;
4381 q
= (tnf_tag_data_t
**)&(*q
)->tag_version
;
4383 tnf_changed_probe_list
= 1;
4387 tnf_splice_probes(int boot_load
, tnf_probe_control_t
*plist
,
4388 tnf_tag_data_t
*tlist
)
4391 static int add_notify
= 1;
4394 tnf_probe_control_t
*pl
;
4396 for (pl
= plist
; pl
->next
; )
4400 mutex_enter(&mod_lock
);
4401 tnf_changed_probe_list
= 1;
4402 pl
->next
= __tnf_probe_list_head
;
4403 __tnf_probe_list_head
= plist
;
4405 mutex_exit(&mod_lock
);
4412 for (tl
= tlist
; tl
->tag_version
; )
4413 tl
= (tnf_tag_data_t
*)tl
->tag_version
;
4416 mutex_enter(&mod_lock
);
4417 tl
->tag_version
= (tnf_tag_version_t
*)__tnf_tag_list_head
;
4418 __tnf_tag_list_head
= tlist
;
4420 mutex_exit(&mod_lock
);
4423 if (!boot_load
&& result
&& add_notify
) {
4424 tnf_add_notifyunload(tnf_unsplice_probes
);
4430 char *kobj_file_buf
;
4431 int kobj_file_bufsize
;
4434 * This code is for the purpose of manually recording which files
4435 * needs to go into the boot archive on any given system.
4437 * To enable the code, set kobj_file_bufsize in /etc/system
4438 * and reboot the system, then use mdb to look at kobj_file_buf.
4441 kobj_record_file(char *filename
)
4444 static int size
= 0;
4447 if (kobj_file_bufsize
== 0) /* don't bother */
4450 if (kobj_file_buf
== NULL
) { /* allocate buffer */
4451 size
= kobj_file_bufsize
;
4452 buf
= kobj_file_buf
= kobj_alloc(size
, KM_WAIT
|KM_TMP
);
4455 n
= snprintf(buf
, size
, "%s\n", filename
);
4463 kobj_boot_fstat(int fd
, struct bootstat
*stp
)
4466 if (!standalone
&& _ioquiesced
)
4468 return (BOP_FSTAT(ops
, fd
, stp
));
4470 return (BRD_FSTAT(bfs_ops
, fd
, stp
));
4475 kobj_boot_open(char *filename
, int flags
)
4480 * If io via bootops is quiesced, it means boot is no longer
4481 * available to us. We make it look as if we can't open the
4482 * named file - which is reasonably accurate.
4484 if (!standalone
&& _ioquiesced
)
4487 kobj_record_file(filename
);
4488 return (BOP_OPEN(filename
, flags
));
4490 kobj_record_file(filename
);
4491 return (BRD_OPEN(bfs_ops
, filename
, flags
));
4496 kobj_boot_close(int fd
)
4499 if (!standalone
&& _ioquiesced
)
4502 return (BOP_CLOSE(fd
));
4504 return (BRD_CLOSE(bfs_ops
, fd
));
4510 kobj_boot_seek(int fd
, off_t hi
, off_t lo
)
4513 return (BOP_SEEK(fd
, lo
) == -1 ? -1 : 0);
4515 return (BRD_SEEK(bfs_ops
, fd
, lo
, SEEK_SET
));
4520 kobj_boot_read(int fd
, caddr_t buf
, size_t size
)
4523 return (BOP_READ(fd
, buf
, size
));
4525 return (BRD_READ(bfs_ops
, fd
, buf
, size
));
4530 kobj_boot_compinfo(int fd
, struct compinfo
*cb
)
4532 return (boot_compinfo(fd
, cb
));
4536 * Check if the file is compressed (for now we handle only gzip).
4537 * It returns CH_MAGIC_GZIP if the file is compressed and 0 otherwise.
4540 kobj_is_compressed(intptr_t fd
)
4542 struct vnode
*vp
= (struct vnode
*)fd
;
4547 if ((err
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)((intptr_t)&magic_buf
),
4548 sizeof (magic_buf
), (offset_t
)(0),
4549 UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(), &resid
)) != 0) {
4551 _kobj_printf(ops
, "kobj_is_compressed: vn_rdwr() failed, "
4552 "error code 0x%x\n", err
);
4556 if (magic_buf
== CH_MAGIC_GZIP
)
4557 return (CH_MAGIC_GZIP
);