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
*);
124 static Sym
*lookup_one(struct module
*, const char *);
125 static void sym_insert(struct module
*, char *, symid_t
);
126 static Sym
*sym_lookup(struct module
*, Sym
*);
128 static struct kobjopen_tctl
*kobjopen_alloc(char *filename
);
129 static void kobjopen_free(struct kobjopen_tctl
*ltp
);
130 static void kobjopen_thread(struct kobjopen_tctl
*ltp
);
131 static int kobj_is_compressed(intptr_t);
133 extern int kcopy(const void *, void *, size_t);
134 extern int elf_mach_ok(Ehdr
*);
135 extern int alloc_gottable(struct module
*, caddr_t
*, caddr_t
*);
138 extern int kobj_boot_mountroot(void);
141 static void tnf_unsplice_probes(uint_t
, struct modctl
*);
142 extern tnf_probe_control_t
*__tnf_probe_list_head
;
143 extern tnf_tag_data_t
*__tnf_tag_list_head
;
145 extern int modrootloaded
;
146 extern int swaploaded
;
147 extern int bop_io_quiesced
;
148 extern int last_module_id
;
150 extern char stubs_base
[];
151 extern char stubs_end
[];
155 * Values that can be or'd in to kobj_debug and their effects:
157 * D_DEBUG - misc. debugging information.
158 * D_SYMBOLS - list symbols and their values as they are entered
159 * into the hash table
160 * D_RELOCATIONS - display relocation processing information
161 * D_LOADING - display information about each module as it
166 #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \
167 (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s))
169 #define KOBJ_MARK(s) /* discard */
172 #define MODPATH_PROPNAME "module-path"
175 static char slash_moddir_suffix_slash
[] = MODDIR_SUFFIX
"/";
177 #define slash_moddir_suffix_slash ""
180 #define _moddebug get_weakish_int(&moddebug)
181 #define _modrootloaded get_weakish_int(&modrootloaded)
182 #define _swaploaded get_weakish_int(&swaploaded)
183 #define _ioquiesced get_weakish_int(&bop_io_quiesced)
185 #define mod(X) (struct module *)((X)->modl_modp->mod_mp)
187 void *romp
; /* rom vector (opaque to us) */
188 struct bootops
*ops
; /* bootops vector */
189 void *dbvec
; /* debug vector */
192 * kobjopen thread control structure
194 struct kobjopen_tctl
{
196 char *name
; /* name of file */
197 struct vnode
*vp
; /* vnode return from vn_open() */
198 int Errno
; /* error return from vnopen */
202 * Structure for defining dynamically expandable library macros
205 struct lib_macro_info
{
206 char *lmi_list
; /* ptr to list of possible choices */
207 char *lmi_macroname
; /* pointer to macro name */
208 ushort_t lmi_ba_index
; /* index into bootaux vector */
209 ushort_t lmi_macrolen
; /* macro length */
211 { NULL
, "CPU", BA_CPU
, 0 },
212 { NULL
, "MMU", BA_MMU
, 0 }
215 #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info)
217 char *boot_cpu_compatible_list
; /* make $CPU available */
219 char *kobj_module_path
; /* module search path */
220 vmem_t
*text_arena
; /* module text arena */
221 static vmem_t
*data_arena
; /* module data & bss arena */
222 static vmem_t
*ctf_arena
; /* CTF debug data arena */
223 static struct modctl
*kobj_modules
= NULL
; /* modules loaded */
224 int kobj_mmu_pagesize
; /* system pagesize */
225 static int lg_pagesize
; /* "large" pagesize */
226 static int kobj_last_module_id
= 0; /* id assignment */
227 static kmutex_t kobj_lock
; /* protects mach memory list */
230 * The following functions have been implemented by the kernel.
231 * However, many 3rd party drivers provide their own implementations
232 * of these functions. When such drivers are loaded, messages
233 * indicating that these symbols have been multiply defined will be
234 * emitted to the console. To avoid alarming customers for no good
235 * reason, we simply suppress such warnings for the following set of
238 static char *suppress_sym_list
[] =
254 NULL
/* This entry must exist */
257 /* indexed by KOBJ_NOTIFY_* */
258 static kobj_notify_list_t
*kobj_notifiers
[KOBJ_NOTIFY_MAX
+ 1];
261 * TNF probe management globals
263 tnf_probe_control_t
*__tnf_probe_list_head
= NULL
;
264 tnf_tag_data_t
*__tnf_tag_list_head
= NULL
;
265 int tnf_changed_probe_list
= 0;
268 * Prefix for statically defined tracing (SDT) DTrace probes.
270 const char *sdt_prefix
= "__dtrace_probe_";
273 * Beginning and end of the kernel's dynamic text/data segments.
275 static caddr_t _text
;
276 static caddr_t _etext
;
277 static caddr_t _data
;
280 * The sparc linker doesn't create a memory location
281 * for a variable named _edata, so _edata can only be
282 * referred to, not modified. krtld needs a static
283 * variable to modify it - within krtld, of course -
284 * outside of krtld, e_data is used in all kernels.
287 static caddr_t _edata
;
289 extern caddr_t _edata
;
292 Addr dynseg
= 0; /* load address of "dynamic" segment */
293 size_t dynsize
; /* "dynamic" segment size */
296 int standalone
= 1; /* an unwholey kernel? */
297 int use_iflush
; /* iflush after relocations */
302 * Common printf function pointer. Can handle only one conversion
303 * specification in the format string. Some of the functions invoked
304 * through this function pointer cannot handle more that one conversion
305 * specification in the format string.
307 void (*_kobj_printf
)(void *, const char *, ...); /* printf routine */
310 * Standalone function pointers for use within krtld.
311 * Many platforms implement optimized platmod versions of
312 * utilities such as bcopy and any such are not yet available
313 * until the kernel is more completely stitched together.
316 void (*kobj_bcopy
)(const void *, void *, size_t);
317 void (*kobj_bzero
)(void *, size_t);
318 size_t (*kobj_strlcat
)(char *, const char *, size_t);
320 static kobj_stat_t kobj_stat
;
322 #define MINALIGN 8 /* at least a double-word */
325 get_weakish_int(int *ip
)
329 return (ip
== NULL
? 0 : *ip
);
333 get_weakish_pointer(void **ptrp
)
337 return (ptrp
== NULL
? 0 : *ptrp
);
341 * XXX fix dependencies on "kernel"; this should work
342 * for other standalone binaries as well.
344 * XXX Fix hashing code to use one pointer to
358 * Load, bind and relocate all modules that
359 * form the primary kernel. At this point, our
360 * externals have not been relocated.
366 struct bootops
*bootvec
,
372 char filename
[MAXPATHLEN
];
375 * Save these to pass on to
376 * the booted standalone.
382 kobj_setup_standalone_vectors();
384 KOBJ_MARK("Entered kobj_init()");
386 (void) BOP_GETPROP(ops
, "whoami", filename
);
389 * We don't support standalone debuggers anymore. The use of kadb
390 * will interfere with the later use of kmdb. Let the user mend
391 * their ways now. Users will reach this message if they still
392 * have the kadb binary on their system (perhaps they used an old
393 * bfu, or maybe they intentionally copied it there) and have
394 * specified its use in a way that eluded our checking in the boot
398 _kobj_printf(ops
, "\nWARNING: Standalone debuggers such as "
399 "kadb are no longer supported\n\n");
405 * OBP allows us to read both the ramdisk and
406 * the underlying root fs when root is a disk.
407 * This can lower incidences of unbootable systems
408 * when the archive is out-of-date with the /etc
411 if (BOP_MOUNTROOT() != BOOT_SVC_OK
) {
412 _kobj_printf(ops
, "can't mount boot fs\n");
417 /* on x86, we always boot with a ramdisk */
418 (void) kobj_boot_mountroot();
421 * Now that the ramdisk is mounted, finish boot property
427 #if !defined(_UNIX_KRTLD)
429 * 'unix' is linked together with 'krtld' into one executable and
430 * the early boot code does -not- hand us any of the dynamic metadata
431 * about the executable. In particular, it does not read in, map or
432 * otherwise look at the program headers. We fake all that up now.
434 * We do this early as DTrace static probes and tnf probes both call
435 * undefined references. We have to process those relocations before
436 * calling any of them.
438 * OBP tells kobj_start() where the ELF image is in memory, so it
439 * synthesized bootaux before kobj_init() was called
441 if (bootaux
[BA_PHDR
].ba_ptr
== NULL
)
442 synthetic_bootaux(filename
, bootaux
);
444 #endif /* !_UNIX_KRTLD */
448 * Save the interesting attribute-values
449 * (scanned by kobj_boot).
454 * Set the module search path.
456 kobj_module_path
= getmodpath(filename
);
458 boot_cpu_compatible_list
= find_libmacro("CPU");
461 * These two modules have actually been
462 * loaded by boot, but we finish the job
463 * by introducing them into the world of
467 mp
= load_exec(bootaux
, filename
);
468 load_linker(bootaux
);
471 * Load all the primary dependent modules.
473 if (load_primary(mp
, KOBJ_LM_PRIMARY
) == -1)
479 if (bind_primary(bootaux
, KOBJ_LM_PRIMARY
) == -1)
482 entry
= bootaux
[BA_ENTRY
].ba_val
;
489 if (boothowto
& RB_VERBOSE
)
490 kobj_lm_dump(KOBJ_LM_PRIMARY
);
494 if (boothowto
& RB_KMDB
) {
495 if (load_kmdb(bootaux
) < 0)
507 kobj_sync_instruction_memory(s_text
, e_text
- s_text
);
510 if (kobj_debug
& D_DEBUG
)
512 "krtld: transferring control to: 0x%p\n", entry
);
516 * Make sure the mod system knows about the modules already loaded.
518 last_module_id
= kobj_last_module_id
;
519 bcopy(kobj_modules
, &modules
, sizeof (modules
));
522 if (modp
->mod_next
== kobj_modules
)
523 modp
->mod_next
= &modules
;
524 if (modp
->mod_prev
== kobj_modules
)
525 modp
->mod_prev
= &modules
;
526 } while ((modp
= modp
->mod_next
) != &modules
);
531 if (kobj_debug
& D_DEBUG
)
533 "krtld: really transferring control to: 0x%p\n", entry
);
536 /* restore printf/bcopy/bzero vectors before returning */
537 kobj_restore_vectors();
541 * krtld was called from a dboot ELF section, the embedded
542 * dboot code contains the real entry via bootaux
544 exitto((caddr_t
)entry
);
547 * krtld was directly called from startup
554 _kobj_printf(ops
, "krtld: error during initial load/link phase\n");
556 #if !defined(_UNIX_KRTLD)
557 _kobj_printf(ops
, "\n");
558 _kobj_printf(ops
, "krtld could neither locate nor resolve symbols"
560 _kobj_printf(ops
, " %s\n", filename
);
561 _kobj_printf(ops
, "in the boot archive. Please verify that this"
563 _kobj_printf(ops
, "matches what is found in the boot archive.\n");
564 _kobj_printf(ops
, "You may need to boot using the Solaris failsafe to"
566 bop_panic("Unable to boot");
570 #if !defined(_UNIX_KRTLD) && !defined(_OBP)
572 * Synthesize additional metadata that describes the executable if
573 * krtld's caller didn't do it.
575 * (When the dynamic executable has an interpreter, the boot program
576 * does all this for us. Where we don't have an interpreter, (or a
577 * even a boot program, perhaps) we have to do this for ourselves.)
580 synthetic_bootaux(char *filename
, val_t
*bootaux
)
590 KOBJ_MARK("synthetic_bootaux()");
592 file
= kobj_open_file(filename
);
593 if (file
== (struct _buf
*)-1) {
594 _kobj_printf(ops
, "krtld: failed to open '%s'\n", filename
);
597 KOBJ_MARK("reading program headers");
598 if (kobj_read_file(file
, (char *)&ehdr
, sizeof (ehdr
), 0) < 0) {
599 _kobj_printf(ops
, "krtld: %s: failed to read ehder\n",
607 bootaux
[BA_PHNUM
].ba_val
= ehdr
.e_phnum
;
608 bootaux
[BA_PHENT
].ba_val
= ehdr
.e_phentsize
;
609 n
= ehdr
.e_phentsize
* ehdr
.e_phnum
;
611 phdrbase
= kobj_alloc(n
, KM_WAIT
| KM_TMP
);
613 if (kobj_read_file(file
, phdrbase
, n
, ehdr
.e_phoff
) < 0) {
614 _kobj_printf(ops
, "krtld: %s: failed to read phdrs\n",
618 bootaux
[BA_PHDR
].ba_ptr
= phdrbase
;
619 kobj_close_file(file
);
620 KOBJ_MARK("closed file");
623 * Find the dynamic section address
625 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
626 Phdr
*phdr
= (Phdr
*)(phdrbase
+ ehdr
.e_phentsize
* i
);
628 if (phdr
->p_type
== PT_DYNAMIC
) {
629 bootaux
[BA_DYNAMIC
].ba_ptr
= (void *)phdr
->p_vaddr
;
633 KOBJ_MARK("synthetic_bootaux() done");
635 #endif /* !_UNIX_KRTLD && !_OBP */
638 * Set up any global information derived
639 * from attribute/values in the boot or
643 attr_val(val_t
*bootaux
)
649 KOBJ_MARK("attr_val()");
650 kobj_mmu_pagesize
= bootaux
[BA_PAGESZ
].ba_val
;
651 lg_pagesize
= bootaux
[BA_LPAGESZ
].ba_val
;
652 use_iflush
= bootaux
[BA_IFLUSH
].ba_val
;
654 phdr
= (Phdr
*)bootaux
[BA_PHDR
].ba_ptr
;
655 phnum
= bootaux
[BA_PHNUM
].ba_val
;
656 phsize
= bootaux
[BA_PHENT
].ba_val
;
657 for (i
= 0; i
< phnum
; i
++) {
658 phdr
= (Phdr
*)(bootaux
[BA_PHDR
].ba_val
+ i
* phsize
);
660 if (phdr
->p_type
!= PT_LOAD
) {
664 * Bounds of the various segments.
666 if (!(phdr
->p_flags
& PF_X
)) {
669 * sparc kernel puts the dynamic info
670 * into a separate segment, which is
671 * free'd in bop_fini()
673 ASSERT(phdr
->p_vaddr
!= 0);
674 dynseg
= phdr
->p_vaddr
;
675 dynsize
= phdr
->p_memsz
;
677 ASSERT(phdr
->p_vaddr
== 0);
680 if (phdr
->p_flags
& PF_W
) {
681 _data
= (caddr_t
)phdr
->p_vaddr
;
682 _edata
= _data
+ phdr
->p_memsz
;
684 _text
= (caddr_t
)phdr
->p_vaddr
;
685 _etext
= _text
+ phdr
->p_memsz
;
690 /* To do the kobj_alloc, _edata needs to be set. */
691 for (i
= 0; i
< NLIBMACROS
; i
++) {
692 if (bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
!= NULL
) {
693 libmacros
[i
].lmi_list
= kobj_alloc(
694 strlen(bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
) +
696 (void) strcpy(libmacros
[i
].lmi_list
,
697 bootaux
[libmacros
[i
].lmi_ba_index
].ba_ptr
);
699 libmacros
[i
].lmi_macrolen
= strlen(libmacros
[i
].lmi_macroname
);
704 * Set up the booted executable.
706 static struct module
*
707 load_exec(val_t
*bootaux
, char *filename
)
713 int i
, lsize
, osize
, nsize
, allocsize
;
715 char path
[MAXPATHLEN
];
718 if (kobj_debug
& D_DEBUG
)
719 _kobj_printf(ops
, "module path '%s'\n", kobj_module_path
);
722 KOBJ_MARK("add_primary");
723 cp
= add_primary(filename
, KOBJ_LM_PRIMARY
);
725 KOBJ_MARK("struct module");
726 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
730 * We don't have the following information
731 * since this module is an executable and not
734 mp
->symtbl_section
= 0;
739 * Since this module is the only exception,
740 * we cons up some section headers.
743 mp
->symhdr
= kobj_zalloc(sizeof (Shdr
), KM_WAIT
);
746 mp
->strhdr
= kobj_zalloc(sizeof (Shdr
), KM_WAIT
);
748 mp
->symhdr
->sh_type
= SHT_SYMTAB
;
749 mp
->strhdr
->sh_type
= SHT_STRTAB
;
751 * Scan the dynamic structure.
753 for (dyn
= (Dyn
*) bootaux
[BA_DYNAMIC
].ba_ptr
;
754 dyn
->d_tag
!= DT_NULL
; dyn
++) {
755 switch (dyn
->d_tag
) {
757 mp
->symspace
= mp
->symtbl
= (char *)dyn
->d_un
.d_ptr
;
758 mp
->symhdr
->sh_addr
= dyn
->d_un
.d_ptr
;
761 mp
->nsyms
= *((uint_t
*)dyn
->d_un
.d_ptr
+ 1);
762 mp
->hashsize
= *(uint_t
*)dyn
->d_un
.d_ptr
;
765 mp
->strings
= (char *)dyn
->d_un
.d_ptr
;
766 mp
->strhdr
->sh_addr
= dyn
->d_un
.d_ptr
;
769 mp
->strhdr
->sh_size
= dyn
->d_un
.d_val
;
772 mp
->symhdr
->sh_entsize
= dyn
->d_un
.d_val
;
778 * Collapse any DT_NEEDED entries into one string.
781 allocsize
= MAXPATHLEN
;
783 KOBJ_MARK("depends_on");
784 mp
->depends_on
= kobj_alloc(allocsize
, KM_WAIT
);
786 for (dyn
= (Dyn
*) bootaux
[BA_DYNAMIC
].ba_ptr
;
787 dyn
->d_tag
!= DT_NULL
; dyn
++)
788 if (dyn
->d_tag
== DT_NEEDED
) {
791 libname
= mp
->strings
+ dyn
->d_un
.d_val
;
792 if (strchr(libname
, '$') != NULL
) {
793 if ((_lib
= expand_libmacro(libname
,
794 path
, path
)) != NULL
)
797 _kobj_printf(ops
, "krtld: "
798 "load_exec: fail to "
799 "expand %s\n", libname
);
801 lsize
= strlen(libname
);
803 if (nsize
+ 1 > allocsize
) {
804 KOBJ_MARK("grow depends_on");
805 tmp
= kobj_alloc(allocsize
+ MAXPATHLEN
,
807 bcopy(mp
->depends_on
, tmp
, osize
);
808 kobj_free(mp
->depends_on
, allocsize
);
809 mp
->depends_on
= tmp
;
810 allocsize
+= MAXPATHLEN
;
812 bcopy(libname
, mp
->depends_on
+ osize
, lsize
);
813 *(mp
->depends_on
+ nsize
) = ' '; /* separate */
818 mp
->depends_on
[nsize
- 1] = '\0'; /* terminate the string */
820 * alloc with exact size and copy whatever it got over
822 KOBJ_MARK("realloc depends_on");
823 tmp
= kobj_alloc(nsize
, KM_WAIT
);
824 bcopy(mp
->depends_on
, tmp
, nsize
);
825 kobj_free(mp
->depends_on
, allocsize
);
826 mp
->depends_on
= tmp
;
828 kobj_free(mp
->depends_on
, allocsize
);
829 mp
->depends_on
= NULL
;
832 mp
->flags
= KOBJ_EXEC
|KOBJ_PRIM
; /* NOT a relocatable .o */
833 mp
->symhdr
->sh_size
= mp
->nsyms
* mp
->symhdr
->sh_entsize
;
835 * We allocate our own table since we don't
836 * hash undefined references.
839 mp
->chains
= kobj_zalloc(mp
->nsyms
* sizeof (symid_t
), KM_WAIT
);
840 KOBJ_MARK("buckets");
841 mp
->buckets
= kobj_zalloc(mp
->hashsize
* sizeof (symid_t
), KM_WAIT
);
846 mp
->text_size
= _etext
- _text
;
847 mp
->data_size
= _edata
- _data
;
849 cp
->mod_text
= mp
->text
;
850 cp
->mod_text_size
= mp
->text_size
;
852 mp
->filename
= cp
->mod_filename
;
855 if (kobj_debug
& D_LOADING
) {
856 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
857 _kobj_printf(ops
, "\ttext: 0x%p", mp
->text
);
858 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
859 _kobj_printf(ops
, "\tdata: 0x%p", mp
->data
);
860 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
862 #endif /* KOBJ_DEBUG */
865 * Insert symbols into the hash table.
867 for (i
= 0; i
< mp
->nsyms
; i
++) {
868 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
870 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
874 * Register symbols are ignored in the kernel
876 if (ELF_ST_TYPE(sp
->st_info
) == STT_SPARC_REGISTER
)
880 sym_insert(mp
, mp
->strings
+ sp
->st_name
, i
);
883 KOBJ_MARK("load_exec done");
888 * Set up the linker module (if it's compiled in, LDNAME is NULL)
891 load_linker(val_t
*bootaux
)
893 struct module
*kmp
= (struct module
*)kobj_modules
->mod_mp
;
900 char *dlname
= (char *)bootaux
[BA_LDNAME
].ba_ptr
;
903 * On some architectures, krtld is compiled into the kernel.
908 cp
= add_primary(dlname
, KOBJ_LM_PRIMARY
);
910 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
913 mp
->hdr
= *(Ehdr
*)bootaux
[BA_LDELF
].ba_ptr
;
914 shsize
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
915 mp
->shdrs
= kobj_alloc(shsize
, KM_WAIT
);
916 bcopy(bootaux
[BA_LDSHDR
].ba_ptr
, mp
->shdrs
, shsize
);
918 for (i
= 1; i
< (int)mp
->hdr
.e_shnum
; i
++) {
919 shp
= (Shdr
*)(mp
->shdrs
+ (i
* mp
->hdr
.e_shentsize
));
921 if (shp
->sh_flags
& SHF_ALLOC
) {
922 if (shp
->sh_flags
& SHF_WRITE
) {
923 if (mp
->data
== NULL
)
924 mp
->data
= (char *)shp
->sh_addr
;
925 } else if (mp
->text
== NULL
) {
926 mp
->text
= (char *)shp
->sh_addr
;
929 if (shp
->sh_type
== SHT_SYMTAB
) {
930 mp
->symtbl_section
= i
;
932 mp
->symspace
= mp
->symtbl
= (char *)shp
->sh_addr
;
935 mp
->nsyms
= mp
->symhdr
->sh_size
/ mp
->symhdr
->sh_entsize
;
936 mp
->flags
= KOBJ_INTERP
|KOBJ_PRIM
;
937 mp
->strhdr
= (Shdr
*)
938 (mp
->shdrs
+ mp
->symhdr
->sh_link
* mp
->hdr
.e_shentsize
);
939 mp
->strings
= (char *)mp
->strhdr
->sh_addr
;
940 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
942 mp
->symsize
= mp
->symhdr
->sh_size
+ mp
->strhdr
->sh_size
+ sizeof (int) +
943 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
);
945 mp
->chains
= kobj_zalloc(mp
->nsyms
* sizeof (symid_t
), KM_WAIT
);
946 mp
->buckets
= kobj_zalloc(mp
->hashsize
* sizeof (symid_t
), KM_WAIT
);
948 mp
->bss
= bootaux
[BA_BSS
].ba_val
;
949 mp
->bss_align
= 0; /* pre-aligned during allocation */
950 mp
->bss_size
= (uintptr_t)_edata
- mp
->bss
;
951 mp
->text_size
= _etext
- mp
->text
;
952 mp
->data_size
= _edata
- mp
->data
;
953 mp
->filename
= cp
->mod_filename
;
954 cp
->mod_text
= mp
->text
;
955 cp
->mod_text_size
= mp
->text_size
;
958 * Now that we've figured out where the linker is,
959 * set the limits for the booted object.
961 kmp
->text_size
= (size_t)(mp
->text
- kmp
->text
);
962 kmp
->data_size
= (size_t)(mp
->data
- kmp
->data
);
963 kobj_modules
->mod_text_size
= kmp
->text_size
;
966 if (kobj_debug
& D_LOADING
) {
967 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
968 _kobj_printf(ops
, "\ttext:0x%p", mp
->text
);
969 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
970 _kobj_printf(ops
, "\tdata:0x%p", mp
->data
);
971 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
973 #endif /* KOBJ_DEBUG */
976 * Insert the symbols into the hash table.
978 for (i
= 0; i
< mp
->nsyms
; i
++) {
979 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
981 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
983 if (ELF_ST_BIND(sp
->st_info
) == STB_GLOBAL
) {
984 if (sp
->st_shndx
== SHN_COMMON
)
985 sp
->st_shndx
= SHN_ABS
;
987 sym_insert(mp
, mp
->strings
+ sp
->st_name
, i
);
992 static kobj_notify_list_t
**
993 kobj_notify_lookup(uint_t type
)
995 ASSERT(type
!= 0 && type
< sizeof (kobj_notifiers
) /
996 sizeof (kobj_notify_list_t
*));
998 return (&kobj_notifiers
[type
]);
1002 kobj_notify_add(kobj_notify_list_t
*knp
)
1004 kobj_notify_list_t
**knl
;
1006 knl
= kobj_notify_lookup(knp
->kn_type
);
1008 knp
->kn_next
= NULL
;
1009 knp
->kn_prev
= NULL
;
1011 mutex_enter(&kobj_lock
);
1014 (*knl
)->kn_prev
= knp
;
1015 knp
->kn_next
= *knl
;
1019 mutex_exit(&kobj_lock
);
1024 kobj_notify_remove(kobj_notify_list_t
*knp
)
1026 kobj_notify_list_t
**knl
= kobj_notify_lookup(knp
->kn_type
);
1027 kobj_notify_list_t
*tknp
;
1029 mutex_enter(&kobj_lock
);
1032 if (tknp
= knp
->kn_next
)
1033 tknp
->kn_prev
= knp
->kn_prev
;
1036 if (tknp
= knp
->kn_prev
)
1037 tknp
->kn_next
= knp
->kn_next
;
1039 *knl
= knp
->kn_next
;
1041 mutex_exit(&kobj_lock
);
1047 * Notify all interested callbacks of a specified change in module state.
1050 kobj_notify(int type
, struct modctl
*modp
)
1052 kobj_notify_list_t
*knp
;
1054 if (modp
->mod_loadflags
& MOD_NONOTIFY
|| standalone
)
1057 mutex_enter(&kobj_lock
);
1059 for (knp
= *(kobj_notify_lookup(type
)); knp
!= NULL
; knp
= knp
->kn_next
)
1060 knp
->kn_func(type
, modp
);
1063 * KDI notification must be last (it has to allow for work done by the
1064 * other notification callbacks), so we call it manually.
1066 kobj_kdi_mod_notify(type
, modp
);
1068 mutex_exit(&kobj_lock
);
1072 * Create the module path.
1075 getmodpath(const char *filename
)
1077 char *path
= kobj_zalloc(MAXPATHLEN
, KM_WAIT
);
1080 * Platform code gets first crack, then add
1081 * the default components
1083 mach_modpath(path
, filename
);
1085 (void) strcat(path
, " ");
1086 return (strcat(path
, MOD_DEFPATH
));
1089 static struct modctl
*
1090 add_primary(const char *filename
, int lmid
)
1094 cp
= kobj_zalloc(sizeof (struct modctl
), KM_WAIT
);
1096 cp
->mod_filename
= kobj_alloc(strlen(filename
) + 1, KM_WAIT
);
1099 * For symbol lookup, we assemble our own
1100 * modctl list of the primary modules.
1103 (void) strcpy(cp
->mod_filename
, filename
);
1104 cp
->mod_modname
= basename(cp
->mod_filename
);
1106 /* set values for modinfo assuming that the load will work */
1109 cp
->mod_installed
= 1;
1110 cp
->mod_loadcnt
= 1;
1111 cp
->mod_loadflags
= MOD_NOAUTOUNLOAD
;
1113 cp
->mod_id
= kobj_last_module_id
++;
1116 * Link the module in. We'll pass this info on
1117 * to the mod squad later.
1119 if (kobj_modules
== NULL
) {
1121 cp
->mod_prev
= cp
->mod_next
= cp
;
1123 cp
->mod_prev
= kobj_modules
->mod_prev
;
1124 cp
->mod_next
= kobj_modules
;
1125 kobj_modules
->mod_prev
->mod_next
= cp
;
1126 kobj_modules
->mod_prev
= cp
;
1129 kobj_lm_append(lmid
, cp
);
1135 bind_primary(val_t
*bootaux
, int lmid
)
1137 struct modctl_list
*linkmap
= kobj_lm_lookup(lmid
);
1138 struct modctl_list
*lp
;
1142 * Do common symbols.
1144 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1148 * Don't do common section relocations for modules that
1151 if (mp
->flags
& (KOBJ_EXEC
|KOBJ_INTERP
))
1154 if (do_common(mp
) < 0)
1161 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1164 if (do_symbols(mp
, 0) < 0)
1171 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1174 if (mp
->flags
& KOBJ_EXEC
) {
1176 Word relasz
= 0, relaent
= 0;
1180 for (dyn
= (Dyn
*)bootaux
[BA_DYNAMIC
].ba_ptr
;
1181 dyn
->d_tag
!= DT_NULL
; dyn
++) {
1182 switch (dyn
->d_tag
) {
1185 relasz
= dyn
->d_un
.d_val
;
1189 relaent
= dyn
->d_un
.d_val
;
1193 rela
= (char *)dyn
->d_un
.d_ptr
;
1197 rela
= (char *)dyn
->d_un
.d_ptr
;
1202 relaent
== 0 || rela
== NULL
) {
1203 _kobj_printf(ops
, "krtld: bind_primary(): "
1204 "no relocation information found for "
1205 "module %s\n", mp
->filename
);
1209 if (kobj_debug
& D_RELOCATIONS
)
1210 _kobj_printf(ops
, "krtld: relocating: file=%s "
1211 "KOBJ_EXEC\n", mp
->filename
);
1213 if (do_relocate(mp
, rela
, shtype
, relasz
/relaent
,
1214 relaent
, (Addr
)mp
->text
) < 0)
1217 if (do_relocations(mp
) < 0)
1221 kobj_sync_instruction_memory(mp
->text
, mp
->text_size
);
1224 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1228 * We need to re-read the full symbol table for the boot file,
1229 * since we couldn't use the full one before. We also need to
1230 * load the CTF sections of both the boot file and the
1233 if (mp
->flags
& KOBJ_EXEC
) {
1237 file
= kobj_open_file(mp
->filename
);
1238 if (file
== (struct _buf
*)-1)
1240 if (kobj_read_file(file
, (char *)&mp
->hdr
,
1241 sizeof (mp
->hdr
), 0) < 0)
1243 n
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
1244 mp
->shdrs
= kobj_alloc(n
, KM_WAIT
);
1245 if (kobj_read_file(file
, mp
->shdrs
, n
,
1246 mp
->hdr
.e_shoff
) < 0)
1248 if (get_syms(mp
, file
) < 0)
1250 if (get_ctf(mp
, file
) < 0)
1252 kobj_close_file(file
);
1253 mp
->flags
|= KOBJ_RELOCATED
;
1255 } else if (mp
->flags
& KOBJ_INTERP
) {
1259 * The interpreter path fragment in mp->filename
1260 * will already have the module directory suffix
1261 * in it (if appropriate).
1263 file
= kobj_open_path(mp
->filename
, 1, 0);
1264 if (file
== (struct _buf
*)-1)
1266 if (get_ctf(mp
, file
) < 0)
1268 kobj_close_file(file
);
1269 mp
->flags
|= KOBJ_RELOCATED
;
1276 static struct modctl
*
1277 mod_already_loaded(char *modname
)
1279 struct modctl
*mctl
= kobj_modules
;
1282 if (strcmp(modname
, mctl
->mod_filename
) == 0)
1284 mctl
= mctl
->mod_next
;
1286 } while (mctl
!= kobj_modules
);
1292 * Load all the primary dependent modules.
1295 load_primary(struct module
*mp
, int lmid
)
1300 char modname
[MODMAXNAMELEN
];
1302 if ((p
= mp
->depends_on
) == NULL
)
1305 /* CONSTANTCONDITION */
1310 while (*p
&& (*p
== ' ' || *p
== '\t'))
1316 while (*p
&& *p
!= ' ' && *p
!= '\t')
1324 * Check for dup dependencies.
1326 if (strcmp(modname
, "dtracestubs") == 0 ||
1327 mod_already_loaded(modname
) != NULL
)
1330 cp
= add_primary(modname
, lmid
);
1335 (void) kobj_load_module(cp
, 1);
1338 if ((dmp
= cp
->mod_mp
) == NULL
) {
1340 cp
->mod_installed
= 0;
1341 cp
->mod_loadcnt
= 0;
1345 add_dependent(mp
, dmp
);
1346 dmp
->flags
|= KOBJ_PRIM
;
1351 if (load_primary(dmp
, lmid
) == -1) {
1353 cp
->mod_installed
= 0;
1354 cp
->mod_loadcnt
= 0;
1362 console_is_usb_serial(void)
1367 if ((len
= BOP_GETPROPLEN(ops
, "console")) == -1)
1370 console
= kobj_zalloc(len
, KM_WAIT
|KM_TMP
);
1371 (void) BOP_GETPROP(ops
, "console", console
);
1372 ret
= (strcmp(console
, "usb-serial") == 0);
1373 kobj_free(console
, len
);
1379 load_kmdb(val_t
*bootaux
)
1381 struct modctl
*mctl
;
1385 if (console_is_usb_serial()) {
1386 _kobj_printf(ops
, "kmdb not loaded "
1387 "(unsupported on usb serial console)\n");
1391 _kobj_printf(ops
, "Loading kmdb...\n");
1393 if ((mctl
= add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER
)) == NULL
)
1397 (void) kobj_load_module(mctl
, 1);
1400 if ((mp
= mctl
->mod_mp
) == NULL
)
1403 mp
->flags
|= KOBJ_PRIM
;
1405 if (load_primary(mp
, KOBJ_LM_DEBUGGER
) < 0)
1408 if (boothowto
& RB_VERBOSE
)
1409 kobj_lm_dump(KOBJ_LM_DEBUGGER
);
1411 if (bind_primary(bootaux
, KOBJ_LM_DEBUGGER
) < 0)
1414 if ((sym
= lookup_one(mctl
->mod_mp
, "kctl_boot_activate")) == NULL
)
1418 if (kobj_debug
& D_DEBUG
) {
1419 _kobj_printf(ops
, "calling kctl_boot_activate() @ 0x%lx\n",
1421 _kobj_printf(ops
, "\tops 0x%p\n", ops
);
1422 _kobj_printf(ops
, "\tromp 0x%p\n", romp
);
1426 if (((kctl_boot_activate_f
*)sym
->st_value
)(ops
, romp
, 0,
1427 (const char **)kobj_kmdb_argv
) < 0)
1434 * Return a string listing module dependencies.
1437 depends_on(struct module
*mp
)
1443 * The module doesn't have a depends_on value, so let's try it the
1444 * old-fashioned way - via "_depends_on"
1446 if ((sp
= lookup_one(mp
, "_depends_on")) == NULL
)
1449 q
= (char *)sp
->st_value
;
1453 * _depends_on is a deprecated interface, so we warn about its use
1454 * irrespective of subsequent processing errors. How else are we going
1455 * to be able to deco this interface completely?
1456 * Changes initially limited to DEBUG because third-party modules
1457 * should be flagged to developers before general use base.
1460 "Warning: %s uses deprecated _depends_on interface.\n",
1462 _kobj_printf(ops
, "Please notify module developer or vendor.\n");
1466 * Idiot checks. Make sure it's
1467 * in-bounds and NULL terminated.
1469 if (kobj_addrcheck(mp
, q
) || q
[sp
->st_size
- 1] != '\0') {
1470 _kobj_printf(ops
, "Error processing dependency for %s\n",
1475 depstr
= (char *)kobj_alloc(strlen(q
) + 1, KM_WAIT
);
1476 (void) strcpy(depstr
, q
);
1482 kobj_getmodinfo(void *xmp
, struct modinfo
*modinfo
)
1485 mp
= (struct module
*)xmp
;
1487 modinfo
->mi_base
= mp
->text
;
1488 modinfo
->mi_size
= mp
->text_size
+ mp
->data_size
;
1492 * kobj_export_ksyms() performs the following services:
1494 * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena.
1495 * (2) Removes unneeded symbols to save space.
1496 * (3) Reduces memory footprint by using VM_BESTFIT allocations.
1497 * (4) Makes the symbol table visible to /dev/ksyms.
1500 kobj_export_ksyms(struct module
*mp
)
1502 Sym
*esp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_size
);
1508 size_t symsize
= mp
->symhdr
->sh_entsize
;
1513 * Make a copy of the original module structure.
1515 omp
= kobj_alloc(sizeof (struct module
), KM_WAIT
);
1516 bcopy(mp
, omp
, sizeof (struct module
));
1519 * Compute the sizes of the new symbol table sections.
1521 for (nsyms
= strsize
= 1, osp
= (Sym
*)omp
->symtbl
; osp
< esp
; osp
++) {
1522 if (osp
->st_value
== 0)
1524 if (sym_lookup(omp
, osp
) == NULL
)
1526 name
= omp
->strings
+ osp
->st_name
;
1527 namelen
= strlen(name
);
1528 if (ELF_ST_BIND(osp
->st_info
) == STB_LOCAL
)
1531 strsize
+= namelen
+ 1;
1535 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
1538 * ksyms_lock must be held as writer during any operation that
1539 * modifies ksyms_arena, including allocation from same, and
1540 * must not be dropped until the arena is vmem_walk()able.
1542 rw_enter(&ksyms_lock
, RW_WRITER
);
1545 * Allocate space for the new section headers (symtab and strtab),
1546 * symbol table, buckets, chains, and strings.
1548 mp
->symsize
= (2 * sizeof (Shdr
)) + (nsyms
* symsize
) +
1549 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
) + strsize
;
1551 if (mp
->flags
& KOBJ_NOKSYMS
) {
1552 mp
->symspace
= kobj_alloc(mp
->symsize
, KM_WAIT
);
1554 mp
->symspace
= vmem_alloc(ksyms_arena
, mp
->symsize
,
1555 VM_BESTFIT
| VM_SLEEP
);
1557 bzero(mp
->symspace
, mp
->symsize
);
1560 * Divvy up symspace.
1562 mp
->shdrs
= mp
->symspace
;
1563 mp
->symhdr
= (Shdr
*)mp
->shdrs
;
1564 mp
->strhdr
= (Shdr
*)(mp
->symhdr
+ 1);
1565 mp
->symtbl
= (char *)(mp
->strhdr
+ 1);
1566 mp
->buckets
= (symid_t
*)(mp
->symtbl
+ (nsyms
* symsize
));
1567 mp
->chains
= (symid_t
*)(mp
->buckets
+ mp
->hashsize
);
1568 mp
->strings
= (char *)(mp
->chains
+ nsyms
);
1571 * Fill in the new section headers (symtab and strtab).
1573 mp
->hdr
.e_shnum
= 2;
1574 mp
->symtbl_section
= 0;
1576 mp
->symhdr
->sh_type
= SHT_SYMTAB
;
1577 mp
->symhdr
->sh_addr
= (Addr
)mp
->symtbl
;
1578 mp
->symhdr
->sh_size
= nsyms
* symsize
;
1579 mp
->symhdr
->sh_link
= 1;
1580 mp
->symhdr
->sh_info
= locals
;
1581 mp
->symhdr
->sh_addralign
= sizeof (Addr
);
1582 mp
->symhdr
->sh_entsize
= symsize
;
1584 mp
->strhdr
->sh_type
= SHT_STRTAB
;
1585 mp
->strhdr
->sh_addr
= (Addr
)mp
->strings
;
1586 mp
->strhdr
->sh_size
= strsize
;
1587 mp
->strhdr
->sh_addralign
= 1;
1590 * Construct the new symbol table.
1592 for (nsyms
= strsize
= 1, osp
= (Sym
*)omp
->symtbl
; osp
< esp
; osp
++) {
1593 if (osp
->st_value
== 0)
1595 if (sym_lookup(omp
, osp
) == NULL
)
1597 name
= omp
->strings
+ osp
->st_name
;
1598 namelen
= strlen(name
);
1599 sp
= (Sym
*)(mp
->symtbl
+ symsize
* nsyms
);
1600 bcopy(osp
, sp
, symsize
);
1601 bcopy(name
, mp
->strings
+ strsize
, namelen
);
1602 sp
->st_name
= strsize
;
1603 sym_insert(mp
, name
, nsyms
);
1605 strsize
+= namelen
+ 1;
1608 rw_exit(&ksyms_lock
);
1611 * Free the old section headers -- we'll never need them again.
1613 if (!(mp
->flags
& KOBJ_PRIM
)) {
1617 for (shn
= 1; shn
< omp
->hdr
.e_shnum
; shn
++) {
1618 shp
= (Shdr
*)(omp
->shdrs
+ shn
* omp
->hdr
.e_shentsize
);
1619 switch (shp
->sh_type
) {
1622 if (shp
->sh_addr
!= 0) {
1623 kobj_free((void *)shp
->sh_addr
,
1629 kobj_free(omp
->shdrs
, omp
->hdr
.e_shentsize
* omp
->hdr
.e_shnum
);
1632 * Discard the old symbol table and our copy of the module strucure.
1634 if (!(mp
->flags
& KOBJ_PRIM
))
1635 kobj_free(omp
->symspace
, omp
->symsize
);
1636 kobj_free(omp
, sizeof (struct module
));
1640 kobj_export_ctf(struct module
*mp
)
1642 char *data
= mp
->ctfdata
;
1643 size_t size
= mp
->ctfsize
;
1646 if (_moddebug
& MODDEBUG_NOCTF
) {
1650 mp
->ctfdata
= vmem_alloc(ctf_arena
, size
,
1651 VM_BESTFIT
| VM_SLEEP
);
1652 bcopy(data
, mp
->ctfdata
, size
);
1655 if (!(mp
->flags
& KOBJ_PRIM
))
1656 kobj_free(data
, size
);
1661 kobj_export_module(struct module
*mp
)
1663 kobj_export_ksyms(mp
);
1664 kobj_export_ctf(mp
);
1666 mp
->flags
|= KOBJ_EXPORTED
;
1670 process_dynamic(struct module
*mp
, char *dyndata
, char *strdata
)
1672 char *path
= NULL
, *depstr
= NULL
;
1673 int allocsize
= 0, osize
= 0, nsize
= 0;
1674 char *libname
, *tmp
;
1678 for (dynp
= (Dyn
*)dyndata
; dynp
&& dynp
->d_tag
!= DT_NULL
; dynp
++) {
1679 switch (dynp
->d_tag
) {
1682 * Read the DT_NEEDED entries, expanding the macros they
1683 * contain (if any), and concatenating them into a
1684 * single space-separated dependency list.
1686 libname
= (ulong_t
)dynp
->d_un
.d_ptr
+ strdata
;
1688 if (strchr(libname
, '$') != NULL
) {
1692 path
= kobj_alloc(MAXPATHLEN
, KM_WAIT
);
1693 if ((_lib
= expand_libmacro(libname
, path
,
1697 _kobj_printf(ops
, "krtld: "
1698 "process_dynamic: failed to expand "
1703 lsize
= strlen(libname
);
1705 if (nsize
+ 1 > allocsize
) {
1706 tmp
= kobj_alloc(allocsize
+ MAXPATHLEN
,
1708 if (depstr
!= NULL
) {
1709 bcopy(depstr
, tmp
, osize
);
1710 kobj_free(depstr
, allocsize
);
1713 allocsize
+= MAXPATHLEN
;
1715 bcopy(libname
, depstr
+ osize
, lsize
);
1716 *(depstr
+ nsize
) = ' '; /* separator */
1722 if (dynp
->d_un
.d_val
& DF_1_IGNMULDEF
)
1723 mp
->flags
|= KOBJ_IGNMULDEF
;
1724 if (dynp
->d_un
.d_val
& DF_1_NOKSYMS
)
1725 mp
->flags
|= KOBJ_NOKSYMS
;
1732 * finish up the depends string (if any)
1734 if (depstr
!= NULL
) {
1735 *(depstr
+ nsize
- 1) = '\0'; /* overwrite separator w/term */
1737 kobj_free(path
, MAXPATHLEN
);
1739 tmp
= kobj_alloc(nsize
, KM_WAIT
);
1740 bcopy(depstr
, tmp
, nsize
);
1741 kobj_free(depstr
, allocsize
);
1744 mp
->depends_on
= depstr
;
1751 do_dynamic(struct module
*mp
, struct _buf
*file
)
1753 Shdr
*dshp
, *dstrp
, *shp
;
1754 char *dyndata
, *dstrdata
;
1757 /* find and validate the dynamic section (if any) */
1759 for (dshp
= NULL
, shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
1760 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
1761 switch (shp
->sh_type
) {
1764 _kobj_printf(ops
, "krtld: get_dynamic: %s, ",
1767 "multiple dynamic sections\n");
1780 if (dshp
->sh_link
> mp
->hdr
.e_shnum
) {
1781 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1782 _kobj_printf(ops
, "no section for sh_link %d\n", dshp
->sh_link
);
1785 dstrp
= (Shdr
*)(mp
->shdrs
+ dshp
->sh_link
* mp
->hdr
.e_shentsize
);
1787 if (dstrp
->sh_type
!= SHT_STRTAB
) {
1788 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1789 _kobj_printf(ops
, "sh_link not a string table for section %d\n",
1794 /* read it from disk */
1796 dyndata
= kobj_alloc(dshp
->sh_size
, KM_WAIT
|KM_TMP
);
1797 if (kobj_read_file(file
, dyndata
, dshp
->sh_size
, dshp
->sh_offset
) < 0) {
1798 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1799 _kobj_printf(ops
, "error reading section %d\n", dshn
);
1801 kobj_free(dyndata
, dshp
->sh_size
);
1805 dstrdata
= kobj_alloc(dstrp
->sh_size
, KM_WAIT
|KM_TMP
);
1806 if (kobj_read_file(file
, dstrdata
, dstrp
->sh_size
,
1807 dstrp
->sh_offset
) < 0) {
1808 _kobj_printf(ops
, "krtld: get_dynamic: %s, ", mp
->filename
);
1809 _kobj_printf(ops
, "error reading section %d\n", dshp
->sh_link
);
1811 kobj_free(dyndata
, dshp
->sh_size
);
1812 kobj_free(dstrdata
, dstrp
->sh_size
);
1816 /* pull the interesting pieces out */
1818 rc
= process_dynamic(mp
, dyndata
, dstrdata
);
1820 kobj_free(dyndata
, dshp
->sh_size
);
1821 kobj_free(dstrdata
, dstrp
->sh_size
);
1827 kobj_set_ctf(struct module
*mp
, caddr_t data
, size_t size
)
1830 if (mp
->ctfdata
!= NULL
) {
1831 if (vmem_contains(ctf_arena
, mp
->ctfdata
,
1833 vmem_free(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
);
1835 kobj_free(mp
->ctfdata
, mp
->ctfsize
);
1841 * The order is very important here. We need to make sure that
1842 * consumers, at any given instant, see a consistent state. We'd
1843 * rather they see no CTF data than the address of one buffer and the
1854 kobj_load_module(struct modctl
*modp
, int use_path
)
1856 char *filename
= modp
->mod_filename
;
1857 char *modname
= modp
->mod_modname
;
1861 struct module
*mp
= NULL
;
1862 #ifdef MODDIR_SUFFIX
1863 int no_suffixdir_drv
= 0;
1866 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
1869 * We need to prevent kmdb's symbols from leaking into /dev/ksyms.
1870 * kmdb contains a bunch of symbols with well-known names, symbols
1871 * which will mask the real versions, thus causing no end of trouble
1874 if (strcmp(modp
->mod_modname
, "kmdbmod") == 0)
1875 mp
->flags
|= KOBJ_NOKSYMS
;
1877 file
= kobj_open_path(filename
, use_path
, 1);
1878 if (file
== (struct _buf
*)-1) {
1879 #ifdef MODDIR_SUFFIX
1880 file
= kobj_open_path(filename
, use_path
, 0);
1882 if (file
== (struct _buf
*)-1) {
1883 kobj_free(mp
, sizeof (*mp
));
1886 #ifdef MODDIR_SUFFIX
1888 * There is no driver module in the ISA specific (suffix)
1889 * subdirectory but there is a module in the parent directory.
1891 if (strncmp(filename
, "drv/", 4) == 0) {
1892 no_suffixdir_drv
= 1;
1897 mp
->filename
= kobj_alloc(strlen(file
->_name
) + 1, KM_WAIT
);
1898 (void) strcpy(mp
->filename
, file
->_name
);
1900 if (kobj_read_file(file
, (char *)&mp
->hdr
, sizeof (mp
->hdr
), 0) < 0) {
1901 _kobj_printf(ops
, "kobj_load_module: %s read header failed\n",
1903 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1904 kobj_free(mp
, sizeof (*mp
));
1907 for (i
= 0; i
< SELFMAG
; i
++) {
1908 if (mp
->hdr
.e_ident
[i
] != ELFMAG
[i
]) {
1909 if (_moddebug
& MODDEBUG_ERRMSG
)
1910 _kobj_printf(ops
, "%s not an elf module\n",
1912 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1913 kobj_free(mp
, sizeof (*mp
));
1918 * It's ELF, but is it our ISA? Interpreting the header
1919 * from a file for a byte-swapped ISA could cause a huge
1920 * and unsatisfiable value to be passed to kobj_alloc below
1921 * and therefore hang booting.
1923 if (!elf_mach_ok(&mp
->hdr
)) {
1924 if (_moddebug
& MODDEBUG_ERRMSG
)
1925 _kobj_printf(ops
, "%s not an elf module for this ISA\n",
1927 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1928 kobj_free(mp
, sizeof (*mp
));
1929 #ifdef MODDIR_SUFFIX
1931 * The driver mod is not in the ISA specific subdirectory
1932 * and the module in the parent directory is not our ISA.
1933 * If it is our ISA, for now we will silently succeed.
1935 if (no_suffixdir_drv
== 1) {
1936 cmn_err(CE_CONT
, "?NOTICE: %s: 64-bit driver module"
1937 " not found\n", modname
);
1944 * All modules, save for unix, should be relocatable (as opposed to
1945 * dynamic). Dynamic modules come with PLTs and GOTs, which can't
1946 * currently be processed by krtld.
1948 if (mp
->hdr
.e_type
!= ET_REL
) {
1949 if (_moddebug
& MODDEBUG_ERRMSG
)
1950 _kobj_printf(ops
, "%s isn't a relocatable (ET_REL) "
1951 "module\n", modname
);
1952 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1953 kobj_free(mp
, sizeof (*mp
));
1957 n
= mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
;
1958 mp
->shdrs
= kobj_alloc(n
, KM_WAIT
);
1960 if (kobj_read_file(file
, mp
->shdrs
, n
, mp
->hdr
.e_shoff
) < 0) {
1961 _kobj_printf(ops
, "kobj_load_module: %s error reading "
1962 "section headers\n", modname
);
1963 kobj_free(mp
->shdrs
, n
);
1964 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1965 kobj_free(mp
, sizeof (*mp
));
1969 kobj_notify(KOBJ_NOTIFY_MODLOADING
, modp
);
1970 module_assign(modp
, mp
);
1972 /* read in sections */
1973 if (get_progbits(mp
, file
) < 0) {
1974 _kobj_printf(ops
, "%s error reading sections\n", modname
);
1978 if (do_dynamic(mp
, file
) < 0) {
1979 _kobj_printf(ops
, "%s error reading dynamic section\n",
1984 modp
->mod_text
= mp
->text
;
1985 modp
->mod_text_size
= mp
->text_size
;
1987 /* read in symbols; adjust values for each section's real address */
1988 if (get_syms(mp
, file
) < 0) {
1989 _kobj_printf(ops
, "%s error reading symbols\n",
1995 * If we didn't dependency information from the dynamic section, look
1996 * for it the old-fashioned way.
1998 if (mp
->depends_on
== NULL
)
1999 mp
->depends_on
= depends_on(mp
);
2001 if (get_ctf(mp
, file
) < 0) {
2002 _kobj_printf(ops
, "%s debug information will not "
2003 "be available\n", modname
);
2006 /* primary kernel modules do not have a signature section */
2007 if (!(mp
->flags
& KOBJ_PRIM
))
2008 get_signature(mp
, file
);
2011 if (kobj_debug
& D_LOADING
) {
2012 _kobj_printf(ops
, "krtld: file=%s\n", mp
->filename
);
2013 _kobj_printf(ops
, "\ttext:0x%p", mp
->text
);
2014 _kobj_printf(ops
, " size: 0x%x\n", mp
->text_size
);
2015 _kobj_printf(ops
, "\tdata:0x%p", mp
->data
);
2016 _kobj_printf(ops
, " dsize: 0x%x\n", mp
->data_size
);
2018 #endif /* KOBJ_DEBUG */
2021 * For primary kernel modules, we defer
2022 * symbol resolution and relocation until
2023 * all primary objects have been loaded.
2027 char *dependent_modname
;
2028 /* load all dependents */
2029 dependent_modname
= kobj_zalloc(MODMAXNAMELEN
, KM_WAIT
);
2030 ddrval
= do_dependents(modp
, dependent_modname
, MODMAXNAMELEN
);
2033 * resolve undefined and common symbols,
2034 * also allocates common space
2036 if ((dcrval
= do_common(mp
)) < 0) {
2039 _kobj_printf(ops
, "WARNING: mod_load: "
2040 "MT-unsafe module '%s' rejected\n",
2044 _kobj_printf(ops
, "WARNING: mod_load: "
2045 "cannot load module '%s'\n",
2048 _kobj_printf(ops
, "WARNING: %s: ",
2051 "unable to resolve dependency, "
2052 "module '%s' not found\n",
2058 kobj_free(dependent_modname
, MODMAXNAMELEN
);
2062 /* process relocation tables */
2063 if (do_relocations(mp
) < 0) {
2064 _kobj_printf(ops
, "%s error doing relocations\n",
2069 if (mp
->destination
) {
2070 off_t off
= (uintptr_t)mp
->destination
& PAGEOFFSET
;
2071 caddr_t base
= (caddr_t
)mp
->destination
- off
;
2072 size_t size
= P2ROUNDUP(mp
->text_size
+ off
, PAGESIZE
);
2074 hat_unload(kas
.a_hat
, base
, size
, HAT_UNLOAD_UNLOCK
);
2075 vmem_free(heap_arena
, base
, size
);
2078 /* sync_instruction_memory */
2079 kobj_sync_instruction_memory(mp
->text
, mp
->text_size
);
2080 kobj_export_module(mp
);
2081 kobj_notify(KOBJ_NOTIFY_MODLOADED
, modp
);
2083 kobj_close_file(file
);
2086 if (file
!= (struct _buf
*)-1)
2087 kobj_close_file(file
);
2088 if (modp
->mod_mp
!= NULL
)
2089 free_module_data(modp
->mod_mp
);
2091 module_assign(modp
, NULL
);
2092 return ((file
== (struct _buf
*)-1) ? ENOENT
: EINVAL
);
2096 kobj_load_primary_module(struct modctl
*modp
)
2101 if (kobj_load_module(modp
, 0) != 0)
2105 mp
->flags
|= KOBJ_PRIM
;
2107 /* Bind new module to its dependents */
2108 if (mp
->depends_on
!= NULL
&& (dep
=
2109 mod_already_loaded(mp
->depends_on
)) == NULL
) {
2111 if (kobj_debug
& D_DEBUG
) {
2112 _kobj_printf(ops
, "krtld: failed to resolve deps "
2113 "for primary %s\n", modp
->mod_modname
);
2119 add_dependent(mp
, dep
->mod_mp
);
2122 * Relocate it. This module may not be part of a link map, so we
2123 * can't use bind_primary.
2125 if (do_common(mp
) < 0 || do_symbols(mp
, 0) < 0 ||
2126 do_relocations(mp
) < 0) {
2128 if (kobj_debug
& D_DEBUG
) {
2129 _kobj_printf(ops
, "krtld: failed to relocate "
2130 "primary %s\n", modp
->mod_modname
);
2140 module_assign(struct modctl
*cp
, struct module
*mp
)
2146 mutex_enter(&mod_lock
);
2149 mutex_exit(&mod_lock
);
2153 kobj_unload_module(struct modctl
*modp
)
2155 struct module
*mp
= modp
->mod_mp
;
2157 if ((_moddebug
& MODDEBUG_KEEPTEXT
) && mp
) {
2158 _kobj_printf(ops
, "text for %s ", mp
->filename
);
2159 _kobj_printf(ops
, "was at %p\n", mp
->text
);
2160 mp
->text
= NULL
; /* don't actually free it */
2163 kobj_notify(KOBJ_NOTIFY_MODUNLOADING
, modp
);
2166 * Null out mod_mp first, so consumers (debuggers) know not to look
2167 * at the module structure any more.
2169 mutex_enter(&mod_lock
);
2170 modp
->mod_mp
= NULL
;
2171 mutex_exit(&mod_lock
);
2173 kobj_notify(KOBJ_NOTIFY_MODUNLOADED
, modp
);
2174 free_module_data(mp
);
2178 free_module_data(struct module
*mp
)
2180 struct module_list
*lp
, *tmp
;
2181 int ksyms_exported
= 0;
2187 kobj_free((char *)tmp
, sizeof (*tmp
));
2190 rw_enter(&ksyms_lock
, RW_WRITER
);
2192 if (vmem_contains(ksyms_arena
, mp
->symspace
, mp
->symsize
)) {
2193 vmem_free(ksyms_arena
, mp
->symspace
, mp
->symsize
);
2196 if (mp
->flags
& KOBJ_NOKSYMS
)
2198 kobj_free(mp
->symspace
, mp
->symsize
);
2201 rw_exit(&ksyms_lock
);
2204 if (vmem_contains(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
))
2205 vmem_free(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
);
2207 kobj_free(mp
->ctfdata
, mp
->ctfsize
);
2211 kobj_free(mp
->sigdata
, mp
->sigsize
);
2214 * We did not get far enough into kobj_export_ksyms() to free allocated
2215 * buffers because we encounted error conditions. Free the buffers.
2217 if ((ksyms_exported
== 0) && (mp
->shdrs
!= NULL
)) {
2221 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2222 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2223 switch (shp
->sh_type
) {
2226 if (shp
->sh_addr
!= 0)
2227 kobj_free((void *)shp
->sh_addr
,
2233 if (!(mp
->flags
& KOBJ_PRIM
)) {
2234 kobj_free(mp
->shdrs
,
2235 mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
);
2240 vmem_free(data_arena
, (void *)mp
->bss
, mp
->bss_size
);
2243 kobj_texthole_free(mp
->fbt_tab
, mp
->fbt_size
);
2245 if (mp
->textwin_base
)
2246 kobj_textwin_free(mp
);
2248 if (mp
->sdt_probes
!= NULL
) {
2249 sdt_probedesc_t
*sdp
= mp
->sdt_probes
, *next
;
2251 while (sdp
!= NULL
) {
2252 next
= sdp
->sdpd_next
;
2253 kobj_free(sdp
->sdpd_name
, strlen(sdp
->sdpd_name
) + 1);
2254 kobj_free(sdp
, sizeof (sdt_probedesc_t
));
2260 kobj_texthole_free(mp
->sdt_tab
, mp
->sdt_size
);
2262 vmem_free(text_arena
, mp
->text
, mp
->text_size
);
2264 vmem_free(data_arena
, mp
->data
, mp
->data_size
);
2266 kobj_free(mp
->depends_on
, strlen(mp
->depends_on
)+1);
2268 kobj_free(mp
->filename
, strlen(mp
->filename
)+1);
2270 kobj_free((char *)mp
, sizeof (*mp
));
2274 get_progbits(struct module
*mp
, struct _buf
*file
)
2276 struct proginfo
*tp
, *dp
, *sdp
;
2278 reloc_dest_t dest
= NULL
;
2280 uintptr_t text
= 0, data
, textptr
;
2284 tp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2285 dp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2286 sdp
= kobj_zalloc(sizeof (struct proginfo
), KM_WAIT
|KM_TMP
);
2288 * loop through sections to find out how much space we need
2289 * for text, data, (also bss that is already assigned)
2291 if (get_progbits_size(mp
, tp
, dp
, sdp
) < 0)
2294 mp
->text_size
= tp
->size
;
2295 mp
->data_size
= dp
->size
;
2298 caddr_t limit
= _data
;
2300 if (lg_pagesize
&& _text
+ lg_pagesize
< limit
)
2301 limit
= _text
+ lg_pagesize
;
2303 mp
->text
= kobj_segbrk(&_etext
, mp
->text_size
,
2306 * If we can't grow the text segment, try the
2307 * data segment before failing.
2309 if (mp
->text
== NULL
) {
2310 mp
->text
= kobj_segbrk(&_edata
, mp
->text_size
,
2314 mp
->data
= kobj_segbrk(&_edata
, mp
->data_size
, dp
->align
, 0);
2316 if (mp
->text
== NULL
|| mp
->data
== NULL
)
2320 if (text_arena
== NULL
)
2321 kobj_vmem_init(&text_arena
, &data_arena
);
2324 * some architectures may want to load the module on a
2325 * page that is currently read only. It may not be
2326 * possible for those architectures to remap their page
2327 * on the fly. So we provide a facility for them to hang
2328 * a private hook where the memory they assign the module
2329 * is not the actual place where the module loads.
2331 * In this case there are two addresses that deal with the
2333 * 1) the final destination of the module
2334 * 2) the address that is used to view the newly
2335 * loaded module until all the relocations relative to 1
2336 * above are completed.
2338 * That is what dest is used for below.
2340 mp
->text_size
+= tp
->align
;
2341 mp
->data_size
+= dp
->align
;
2343 mp
->text
= kobj_text_alloc(text_arena
, mp
->text_size
);
2346 * a remap is taking place. Align the text ptr relative
2347 * to the secondary mapping. That is where the bits will
2350 if (kvseg
.s_base
!= NULL
&& !vmem_contains(heaptext_arena
,
2351 mp
->text
, mp
->text_size
)) {
2352 off_t off
= (uintptr_t)mp
->text
& PAGEOFFSET
;
2353 size_t size
= P2ROUNDUP(mp
->text_size
+ off
, PAGESIZE
);
2354 caddr_t map
= vmem_alloc(heap_arena
, size
, VM_SLEEP
);
2355 caddr_t orig
= mp
->text
- off
;
2356 pgcnt_t pages
= size
/ PAGESIZE
;
2358 dest
= (reloc_dest_t
)(map
+ off
);
2359 text
= ALIGN((uintptr_t)dest
, tp
->align
);
2362 hat_devload(kas
.a_hat
, map
, PAGESIZE
,
2363 hat_getpfnum(kas
.a_hat
, orig
),
2364 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
2365 HAT_LOAD_NOCONSIST
| HAT_LOAD_LOCK
);
2370 * Since we set up a non-cacheable mapping, we need
2371 * to flush any old entries in the cache that might
2372 * be left around from the read-only mapping.
2377 mp
->data
= vmem_alloc(data_arena
, mp
->data_size
,
2378 VM_SLEEP
| VM_BESTFIT
);
2380 textptr
= (uintptr_t)mp
->text
;
2381 textptr
= ALIGN(textptr
, tp
->align
);
2382 mp
->destination
= dest
;
2385 * This is the case where a remap is not being done.
2388 text
= ALIGN((uintptr_t)mp
->text
, tp
->align
);
2389 data
= ALIGN((uintptr_t)mp
->data
, dp
->align
);
2391 /* now loop though sections assigning addresses and loading the data */
2392 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2393 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2394 if (!(shp
->sh_flags
& SHF_ALLOC
))
2397 if ((shp
->sh_flags
& SHF_WRITE
) == 0)
2402 bits_ptr
= ALIGN(bits_ptr
, shp
->sh_addralign
);
2404 if (shp
->sh_type
== SHT_NOBITS
) {
2408 bzero((caddr_t
)bits_ptr
, shp
->sh_size
);
2409 shp
->sh_type
= SHT_PROGBITS
;
2411 if (kobj_read_file(file
, (char *)bits_ptr
,
2412 shp
->sh_size
, shp
->sh_offset
) < 0)
2416 if (shp
->sh_flags
& SHF_WRITE
) {
2417 shp
->sh_addr
= bits_ptr
;
2419 textptr
= ALIGN(textptr
, shp
->sh_addralign
);
2420 shp
->sh_addr
= textptr
;
2421 textptr
+= shp
->sh_size
;
2424 bits_ptr
+= shp
->sh_size
;
2425 if ((shp
->sh_flags
& SHF_WRITE
) == 0)
2434 * Free and mark as freed the section headers here so that
2435 * free_module_data() does not have to worry about this buffer.
2437 * This buffer is freed here because one of the possible reasons
2438 * for error is a section with non-zero sh_addr and in that case
2439 * free_module_data() would have no way of recognizing that this
2440 * buffer was unallocated.
2443 kobj_free(mp
->shdrs
, mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
);
2447 (void) kobj_free(tp
, sizeof (struct proginfo
));
2448 (void) kobj_free(dp
, sizeof (struct proginfo
));
2449 (void) kobj_free(sdp
, sizeof (struct proginfo
));
2455 * Go through suppress_sym_list to see if "multiply defined"
2456 * warning of this symbol should be suppressed. Return 1 if
2457 * warning should be suppressed, 0 otherwise.
2460 kobj_suppress_warning(char *symname
)
2464 for (i
= 0; suppress_sym_list
[i
] != NULL
; i
++) {
2465 if (strcmp(suppress_sym_list
[i
], symname
) == 0)
2473 get_syms(struct module
*mp
, struct _buf
*file
)
2483 * Find the interesting sections.
2485 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2486 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2487 switch (shp
->sh_type
) {
2489 mp
->symtbl_section
= shn
;
2502 /* KM_TMP since kobj_free'd in do_relocations */
2503 shp
->sh_addr
= (Addr
)
2504 kobj_alloc(shp
->sh_size
, KM_WAIT
|KM_TMP
);
2506 if (kobj_read_file(file
, (char *)shp
->sh_addr
,
2507 shp
->sh_size
, shp
->sh_offset
) < 0) {
2508 _kobj_printf(ops
, "krtld: get_syms: %s, ",
2510 _kobj_printf(ops
, "error reading section %d\n",
2519 * This is true for a stripped executable. In the case of
2520 * 'unix' it can be stripped but it still contains the SHT_DYNSYM,
2521 * and since that symbol information is still present everything
2525 if (mp
->flags
& KOBJ_EXEC
)
2527 _kobj_printf(ops
, "krtld: get_syms: %s ",
2529 _kobj_printf(ops
, "no SHT_SYMTAB symbol table found\n");
2534 * get the associated string table header
2536 if ((mp
->symhdr
== 0) || (mp
->symhdr
->sh_link
>= mp
->hdr
.e_shnum
))
2538 mp
->strhdr
= (Shdr
*)
2539 (mp
->shdrs
+ mp
->symhdr
->sh_link
* mp
->hdr
.e_shentsize
);
2541 mp
->nsyms
= mp
->symhdr
->sh_size
/ mp
->symhdr
->sh_entsize
;
2542 mp
->hashsize
= kobj_gethashsize(mp
->nsyms
);
2545 * Allocate space for the symbol table, buckets, chains, and strings.
2547 mp
->symsize
= mp
->symhdr
->sh_size
+
2548 (mp
->hashsize
+ mp
->nsyms
) * sizeof (symid_t
) + mp
->strhdr
->sh_size
;
2549 mp
->symspace
= kobj_zalloc(mp
->symsize
, KM_WAIT
|KM_SCRATCH
);
2551 mp
->symtbl
= mp
->symspace
;
2552 mp
->buckets
= (symid_t
*)(mp
->symtbl
+ mp
->symhdr
->sh_size
);
2553 mp
->chains
= mp
->buckets
+ mp
->hashsize
;
2554 mp
->strings
= (char *)(mp
->chains
+ mp
->nsyms
);
2556 if (kobj_read_file(file
, mp
->symtbl
,
2557 mp
->symhdr
->sh_size
, mp
->symhdr
->sh_offset
) < 0 ||
2558 kobj_read_file(file
, mp
->strings
,
2559 mp
->strhdr
->sh_size
, mp
->strhdr
->sh_offset
) < 0)
2563 * loop through the symbol table adjusting values to account
2564 * for where each section got loaded into memory. Also
2565 * fill in the hash table.
2567 for (i
= 1; i
< mp
->nsyms
; i
++) {
2568 sp
= (Sym
*)(mp
->symtbl
+ i
* mp
->symhdr
->sh_entsize
);
2569 if (sp
->st_shndx
< SHN_LORESERVE
) {
2570 if (sp
->st_shndx
>= mp
->hdr
.e_shnum
) {
2571 _kobj_printf(ops
, "%s bad shndx ",
2573 _kobj_printf(ops
, "in symbol %d\n", i
);
2578 sp
->st_shndx
* mp
->hdr
.e_shentsize
);
2579 if (!(mp
->flags
& KOBJ_EXEC
))
2580 sp
->st_value
+= shp
->sh_addr
;
2583 if (sp
->st_name
== 0 || sp
->st_shndx
== SHN_UNDEF
)
2585 if (sp
->st_name
>= mp
->strhdr
->sh_size
)
2588 symname
= mp
->strings
+ sp
->st_name
;
2590 if (!(mp
->flags
& KOBJ_EXEC
) &&
2591 ELF_ST_BIND(sp
->st_info
) == STB_GLOBAL
) {
2592 ksp
= kobj_lookup_all(mp
, symname
, 0);
2594 if (ksp
&& ELF_ST_BIND(ksp
->st_info
) == STB_GLOBAL
&&
2595 !kobj_suppress_warning(symname
) &&
2596 sp
->st_shndx
!= SHN_UNDEF
&&
2597 sp
->st_shndx
!= SHN_COMMON
&&
2598 ksp
->st_shndx
!= SHN_UNDEF
&&
2599 ksp
->st_shndx
!= SHN_COMMON
) {
2601 * Unless this symbol is a stub, it's multiply
2602 * defined. Multiply-defined symbols are
2603 * usually bad, but some objects (kmdb) have
2604 * a legitimate need to have their own
2605 * copies of common functions.
2608 ksp
->st_value
< (uintptr_t)stubs_base
||
2609 ksp
->st_value
>= (uintptr_t)stubs_end
) &&
2610 !(mp
->flags
& KOBJ_IGNMULDEF
)) {
2612 "%s symbol ", file
->_name
);
2614 "%s multiply defined\n", symname
);
2619 sym_insert(mp
, symname
, i
);
2626 get_ctf(struct module
*mp
, struct _buf
*file
)
2628 char *shstrtab
, *ctfdata
;
2633 if (_moddebug
& MODDEBUG_NOCTF
)
2634 return (0); /* do not attempt to even load CTF data */
2636 if (mp
->hdr
.e_shstrndx
>= mp
->hdr
.e_shnum
) {
2637 _kobj_printf(ops
, "krtld: get_ctf: %s, ",
2639 _kobj_printf(ops
, "corrupt e_shstrndx %u\n",
2640 mp
->hdr
.e_shstrndx
);
2644 shp
= (Shdr
*)(mp
->shdrs
+ mp
->hdr
.e_shstrndx
* mp
->hdr
.e_shentsize
);
2645 shstrlen
= shp
->sh_size
;
2646 shstrtab
= kobj_alloc(shstrlen
, KM_WAIT
|KM_TMP
);
2648 if (kobj_read_file(file
, shstrtab
, shstrlen
, shp
->sh_offset
) < 0) {
2649 _kobj_printf(ops
, "krtld: get_ctf: %s, ",
2651 _kobj_printf(ops
, "error reading section %u\n",
2652 mp
->hdr
.e_shstrndx
);
2653 kobj_free(shstrtab
, shstrlen
);
2657 for (i
= 0; i
< mp
->hdr
.e_shnum
; i
++) {
2658 shp
= (Shdr
*)(mp
->shdrs
+ i
* mp
->hdr
.e_shentsize
);
2660 if (shp
->sh_size
!= 0 && shp
->sh_name
< shstrlen
&&
2661 strcmp(shstrtab
+ shp
->sh_name
, ".SUNW_ctf") == 0) {
2662 ctfdata
= kobj_alloc(shp
->sh_size
, KM_WAIT
|KM_SCRATCH
);
2664 if (kobj_read_file(file
, ctfdata
, shp
->sh_size
,
2665 shp
->sh_offset
) < 0) {
2666 _kobj_printf(ops
, "krtld: get_ctf: %s, error "
2667 "reading .SUNW_ctf data\n", mp
->filename
);
2668 kobj_free(ctfdata
, shp
->sh_size
);
2669 kobj_free(shstrtab
, shstrlen
);
2673 mp
->ctfdata
= ctfdata
;
2674 mp
->ctfsize
= shp
->sh_size
;
2679 kobj_free(shstrtab
, shstrlen
);
2683 #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */
2686 * Return the hash of the ELF sections that are memory resident.
2687 * i.e. text and data. We skip a SHT_NOBITS section since it occupies
2688 * no space in the file. We use SHA1 here since libelfsign uses
2689 * it and both places need to use the same algorithm.
2692 crypto_es_hash(struct module
*mp
, char *hash
, char *shstrtab
)
2700 for (shn
= 1; shn
< mp
->hdr
.e_shnum
; shn
++) {
2701 shp
= (Shdr
*)(mp
->shdrs
+ shn
* mp
->hdr
.e_shentsize
);
2702 if (!(shp
->sh_flags
& SHF_ALLOC
) || shp
->sh_size
== 0)
2706 * The check should ideally be shp->sh_type == SHT_NOBITS.
2707 * However, we can't do that check here as get_progbits()
2710 if (strcmp(shstrtab
+ shp
->sh_name
, ".bss") == 0)
2713 if (kobj_debug
& D_DEBUG
)
2715 "krtld: crypto_es_hash: updating hash with"
2716 " %s data size=%d\n", shstrtab
+ shp
->sh_name
,
2719 ASSERT(shp
->sh_addr
!= NULL
);
2720 SHA1Update(&ctx
, (const uint8_t *)shp
->sh_addr
, shp
->sh_size
);
2723 SHA1Final((uchar_t
*)hash
, &ctx
);
2727 * Get the .SUNW_signature section for the module, it it exists.
2729 * This section exists only for crypto modules. None of the
2730 * primary modules have this section currently.
2733 get_signature(struct module
*mp
, struct _buf
*file
)
2735 char *shstrtab
, *sigdata
= NULL
;
2740 if (mp
->hdr
.e_shstrndx
>= mp
->hdr
.e_shnum
) {
2741 _kobj_printf(ops
, "krtld: get_signature: %s, ",
2743 _kobj_printf(ops
, "corrupt e_shstrndx %u\n",
2744 mp
->hdr
.e_shstrndx
);
2748 shp
= (Shdr
*)(mp
->shdrs
+ mp
->hdr
.e_shstrndx
* mp
->hdr
.e_shentsize
);
2749 shstrlen
= shp
->sh_size
;
2750 shstrtab
= kobj_alloc(shstrlen
, KM_WAIT
|KM_TMP
);
2752 if (kobj_read_file(file
, shstrtab
, shstrlen
, shp
->sh_offset
) < 0) {
2753 _kobj_printf(ops
, "krtld: get_signature: %s, ",
2755 _kobj_printf(ops
, "error reading section %u\n",
2756 mp
->hdr
.e_shstrndx
);
2757 kobj_free(shstrtab
, shstrlen
);
2761 for (i
= 0; i
< mp
->hdr
.e_shnum
; i
++) {
2762 shp
= (Shdr
*)(mp
->shdrs
+ i
* mp
->hdr
.e_shentsize
);
2763 if (shp
->sh_size
!= 0 && shp
->sh_name
< shstrlen
&&
2764 strcmp(shstrtab
+ shp
->sh_name
,
2765 ELF_SIGNATURE_SECTION
) == 0) {
2766 filesig_vers_t filesig_version
;
2767 size_t sigsize
= shp
->sh_size
+ SHA1_DIGEST_LENGTH
;
2768 sigdata
= kobj_alloc(sigsize
, KM_WAIT
|KM_SCRATCH
);
2770 if (kobj_read_file(file
, sigdata
, shp
->sh_size
,
2771 shp
->sh_offset
) < 0) {
2772 _kobj_printf(ops
, "krtld: get_signature: %s,"
2773 " error reading .SUNW_signature data\n",
2775 kobj_free(sigdata
, sigsize
);
2776 kobj_free(shstrtab
, shstrlen
);
2779 filesig_version
= ((struct filesignatures
*)sigdata
)->
2780 filesig_sig
.filesig_version
;
2781 if (!(filesig_version
== FILESIG_VERSION1
||
2782 filesig_version
== FILESIG_VERSION3
)) {
2783 /* skip versions we don't understand */
2784 kobj_free(sigdata
, sigsize
);
2785 kobj_free(shstrtab
, shstrlen
);
2789 mp
->sigdata
= sigdata
;
2790 mp
->sigsize
= sigsize
;
2795 if (sigdata
!= NULL
) {
2796 crypto_es_hash(mp
, sigdata
+ shp
->sh_size
, shstrtab
);
2799 kobj_free(shstrtab
, shstrlen
);
2803 add_dependent(struct module
*mp
, struct module
*dep
)
2805 struct module_list
*lp
;
2807 for (lp
= mp
->head
; lp
; lp
= lp
->next
) {
2809 return; /* already on the list */
2813 lp
= kobj_zalloc(sizeof (*lp
), KM_WAIT
);
2818 mp
->tail
->next
= lp
;
2826 do_dependents(struct modctl
*modp
, char *modname
, size_t modnamelen
)
2832 char *err_modname
= NULL
;
2836 if ((p
= mp
->depends_on
) == NULL
)
2843 while (*p
&& (*p
== ' ' || *p
== '\t'))
2851 while (*p
&& *p
!= ' ' && *p
!= '\t') {
2852 if (c
< modnamelen
- 1) {
2862 if (c
== modnamelen
- 1) {
2863 char *dep
= kobj_alloc(p
- d
+ 1, KM_WAIT
|KM_TMP
);
2865 (void) strncpy(dep
, d
, p
- d
+ 1);
2868 _kobj_printf(ops
, "%s: dependency ", modp
->mod_modname
);
2869 _kobj_printf(ops
, "'%s' too long ", dep
);
2870 _kobj_printf(ops
, "(max %d chars)\n", modnamelen
);
2872 kobj_free(dep
, p
- d
+ 1);
2878 if ((req
= mod_load_requisite(modp
, modname
)) == NULL
) {
2880 if (_moddebug
& MODDEBUG_LOADMSG
) {
2881 #endif /* KOBJ_DEBUG */
2883 "%s: unable to resolve dependency, ",
2885 _kobj_printf(ops
, "cannot load module '%s'\n",
2889 #endif /* KOBJ_DEBUG */
2890 if (err_modname
== NULL
) {
2892 * This must be the same size as the modname
2895 err_modname
= kobj_zalloc(MODMAXNAMELEN
,
2899 * We can use strcpy() here without fearing
2900 * the NULL terminator because the size of
2901 * err_modname is the same as one of modname,
2902 * and it's filled with zeros.
2904 (void) strcpy(err_modname
, modname
);
2909 add_dependent(mp
, req
->mod_mp
);
2910 mod_release_mod(req
);
2914 if (err_modname
!= NULL
) {
2916 * Copy the first module name where you detect an error to keep
2917 * its behavior the same as before.
2918 * This way keeps minimizing the memory use for error
2919 * modules, and this might be important at boot time because
2920 * the memory usage is a crucial factor for booting in most
2921 * cases. You can expect more verbose messages when using
2922 * a debug kernel or setting a bit in moddebug.
2924 bzero(modname
, MODMAXNAMELEN
);
2925 (void) strcpy(modname
, err_modname
);
2926 kobj_free(err_modname
, MODMAXNAMELEN
);
2934 do_common(struct module
*mp
)
2939 * first time through, assign all symbols defined in other
2940 * modules, and count up how much common space will be needed
2941 * (bss_size and bss_align)
2943 if ((err
= do_symbols(mp
, 0)) < 0)
2946 * increase bss_size by the maximum delta that could be
2947 * computed by the ALIGN below
2949 mp
->bss_size
+= mp
->bss_align
;
2952 mp
->bss
= (uintptr_t)kobj_segbrk(&_edata
, mp
->bss_size
,
2955 mp
->bss
= (uintptr_t)vmem_alloc(data_arena
,
2956 mp
->bss_size
, VM_SLEEP
| VM_BESTFIT
);
2957 bzero((void *)mp
->bss
, mp
->bss_size
);
2958 /* now assign addresses to all common symbols */
2959 if ((err
= do_symbols(mp
, ALIGN(mp
->bss
, mp
->bss_align
))) < 0)
2966 do_symbols(struct module
*mp
, Elf64_Addr bss_base
)
2978 * Nothing left to do (optimization).
2980 if (mp
->flags
& KOBJ_RESOLVED
)
2983 assign
= (bss_base
) ? 1 : 0;
2988 for (i
= 1; i
< mp
->nsyms
; i
++) {
2989 sp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_entsize
* i
);
2991 * we know that st_name is in bounds, since get_sections
2992 * has already checked all of the symbols
2994 name
= mp
->strings
+ sp
->st_name
;
2995 if (sp
->st_shndx
!= SHN_UNDEF
&& sp
->st_shndx
!= SHN_COMMON
)
2997 #if defined(__sparc)
2999 * Register symbols are ignored in the kernel
3001 if (ELF_ST_TYPE(sp
->st_info
) == STT_SPARC_REGISTER
) {
3002 if (*name
!= '\0') {
3003 _kobj_printf(ops
, "%s: named REGISTER symbol ",
3005 _kobj_printf(ops
, "not supported '%s'\n",
3011 #endif /* __sparc */
3013 * TLS symbols are ignored in the kernel
3015 if (ELF_ST_TYPE(sp
->st_info
) == STT_TLS
) {
3016 _kobj_printf(ops
, "%s: TLS symbol ",
3018 _kobj_printf(ops
, "not supported '%s'\n",
3024 if (ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
) {
3025 if ((sp1
= kobj_lookup_all(mp
, name
, 0)) != NULL
) {
3026 sp
->st_shndx
= SHN_ABS
;
3027 sp
->st_value
= sp1
->st_value
;
3032 if (sp
->st_shndx
== SHN_UNDEF
) {
3035 if (strncmp(name
, sdt_prefix
, strlen(sdt_prefix
)) == 0)
3039 * If it's not a weak reference and it's
3040 * not a primary object, it's an error.
3041 * (Primary objects may take more than
3042 * one pass to resolve)
3044 if (!(mp
->flags
& KOBJ_PRIM
) &&
3045 ELF_ST_BIND(sp
->st_info
) != STB_WEAK
) {
3046 _kobj_printf(ops
, "%s: undefined symbol",
3048 _kobj_printf(ops
, " '%s'\n", name
);
3050 * Try to determine whether this symbol
3051 * represents a dependency on obsolete
3052 * unsafe driver support. This is just
3053 * to make the warning more informative.
3055 if (strcmp(name
, "sleep") == 0 ||
3056 strcmp(name
, "unsleep") == 0 ||
3057 strcmp(name
, "wakeup") == 0 ||
3058 strcmp(name
, "bsd_compat_ioctl") == 0 ||
3059 strcmp(name
, "unsafe_driver") == 0 ||
3060 strncmp(name
, "spl", 3) == 0 ||
3061 strncmp(name
, "i_ddi_spl", 9) == 0)
3069 * It's a common symbol - st_value is the
3070 * required alignment.
3072 if (sp
->st_value
> bss_align
)
3073 bss_align
= sp
->st_value
;
3074 bss_ptr
= ALIGN(bss_ptr
, sp
->st_value
);
3076 sp
->st_shndx
= SHN_ABS
;
3077 sp
->st_value
= bss_ptr
;
3079 bss_ptr
+= sp
->st_size
;
3083 if (assign
== 0 && mp
->bss
== NULL
) {
3084 mp
->bss_align
= bss_align
;
3085 mp
->bss_size
= bss_ptr
;
3086 } else if (resolved
) {
3087 mp
->flags
|= KOBJ_RESOLVED
;
3094 kobj_hash_name(const char *p
)
3101 hval
= (hval
<< 4) + *p
++;
3102 if ((g
= (hval
& 0xf0000000)) != 0)
3109 /* look for name in all modules */
3111 kobj_getsymvalue(char *name
, int kernelonly
)
3114 struct modctl
*modp
;
3116 uintptr_t value
= 0;
3118 if ((sp
= kobj_lookup_kernel(name
)) != NULL
)
3119 return ((uintptr_t)sp
->st_value
);
3122 return (0); /* didn't find it in the kernel so give up */
3124 mutex_enter(&mod_lock
);
3127 mp
= (struct module
*)modp
->mod_mp
;
3128 if (mp
&& !(mp
->flags
& KOBJ_PRIM
) && modp
->mod_loaded
&&
3129 (sp
= lookup_one(mp
, name
))) {
3130 value
= (uintptr_t)sp
->st_value
;
3133 } while ((modp
= modp
->mod_next
) != &modules
);
3134 mutex_exit(&mod_lock
);
3138 /* look for a symbol near value. */
3140 kobj_getsymname(uintptr_t value
, ulong_t
*offset
)
3143 struct modctl
*modp
;
3145 struct modctl_list
*lp
;
3149 * Loop through the primary kernel modules.
3151 for (lp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); lp
; lp
= lp
->modl_next
) {
3154 if ((name
= kobj_searchsym(mp
, value
, offset
)) != NULL
)
3158 mutex_enter(&mod_lock
);
3161 mp
= (struct module
*)modp
->mod_mp
;
3162 if (mp
&& !(mp
->flags
& KOBJ_PRIM
) && modp
->mod_loaded
&&
3163 (name
= kobj_searchsym(mp
, value
, offset
)))
3165 } while ((modp
= modp
->mod_next
) != &modules
);
3166 mutex_exit(&mod_lock
);
3170 /* return address of symbol and size */
3173 kobj_getelfsym(char *name
, void *mp
, int *size
)
3178 sp
= kobj_lookup_kernel(name
);
3180 sp
= lookup_one(mp
, name
);
3185 *size
= (int)sp
->st_size
;
3186 return ((uintptr_t)sp
->st_value
);
3190 kobj_lookup(struct module
*mod
, const char *name
)
3194 sp
= lookup_one(mod
, name
);
3199 return ((uintptr_t)sp
->st_value
);
3203 kobj_searchsym(struct module
*mp
, uintptr_t value
, ulong_t
*offset
)
3212 *offset
= (ulong_t
)-1l; /* assume not found */
3215 if (kobj_addrcheck(mp
, (void *)value
) != 0)
3216 return (NULL
); /* not in this module */
3218 strtabptr
= mp
->strings
;
3219 symtabptr
= (Sym
*)mp
->symtbl
;
3222 * Scan the module's symbol table for a symbol <= value
3224 for (symnum
= 1, sym
= symtabptr
+ 1;
3225 symnum
< mp
->nsyms
; symnum
++, sym
= (Sym
*)
3226 ((uintptr_t)sym
+ mp
->symhdr
->sh_entsize
)) {
3227 if (ELF_ST_BIND(sym
->st_info
) != STB_GLOBAL
) {
3228 if (ELF_ST_BIND(sym
->st_info
) != STB_LOCAL
)
3230 if (ELF_ST_TYPE(sym
->st_info
) != STT_OBJECT
&&
3231 ELF_ST_TYPE(sym
->st_info
) != STT_FUNC
)
3235 curval
= (uintptr_t)sym
->st_value
;
3241 * If one or both are functions...
3243 if (ELF_ST_TYPE(sym
->st_info
) == STT_FUNC
|| (cursym
!= NULL
&&
3244 ELF_ST_TYPE(cursym
->st_info
) == STT_FUNC
)) {
3245 /* Ignore if the address is out of the bounds */
3246 if (value
- sym
->st_value
>= sym
->st_size
)
3249 if (cursym
!= NULL
&&
3250 ELF_ST_TYPE(cursym
->st_info
) == STT_FUNC
) {
3251 /* Prefer the function to the non-function */
3252 if (ELF_ST_TYPE(sym
->st_info
) != STT_FUNC
)
3255 /* Prefer the larger of the two functions */
3256 if (sym
->st_size
<= cursym
->st_size
)
3259 } else if (value
- curval
>= *offset
) {
3263 *offset
= (ulong_t
)(value
- curval
);
3269 return (strtabptr
+ cursym
->st_name
);
3273 kobj_lookup_all(struct module
*mp
, char *name
, int include_self
)
3276 struct module_list
*mlp
;
3277 struct modctl_list
*clp
;
3280 if (include_self
&& (sp
= lookup_one(mp
, name
)) != NULL
)
3283 for (mlp
= mp
->head
; mlp
; mlp
= mlp
->next
) {
3284 if ((sp
= lookup_one(mlp
->mp
, name
)) != NULL
&&
3285 ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
)
3290 * Loop through the primary kernel modules.
3292 for (clp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); clp
; clp
= clp
->modl_next
) {
3295 if (mmp
== NULL
|| mp
== mmp
)
3298 if ((sp
= lookup_one(mmp
, name
)) != NULL
&&
3299 ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
)
3306 kobj_lookup_kernel(const char *name
)
3308 struct modctl_list
*lp
;
3313 * Loop through the primary kernel modules.
3315 for (lp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); lp
; lp
= lp
->modl_next
) {
3321 if ((sp
= lookup_one(mp
, name
)) != NULL
)
3328 lookup_one(struct module
*mp
, const char *name
)
3334 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3335 ip
= &mp
->chains
[*ip
]) {
3336 sp
= (Sym
*)(mp
->symtbl
+
3337 mp
->symhdr
->sh_entsize
* *ip
);
3338 name1
= mp
->strings
+ sp
->st_name
;
3339 if (strcmp(name
, name1
) == 0 &&
3340 ELF_ST_TYPE(sp
->st_info
) != STT_FILE
&&
3341 sp
->st_shndx
!= SHN_UNDEF
&&
3342 sp
->st_shndx
!= SHN_COMMON
)
3349 * Lookup a given symbol pointer in the module's symbol hash. If the symbol
3350 * is hashed, return the symbol pointer; otherwise return NULL.
3353 sym_lookup(struct module
*mp
, Sym
*ksp
)
3355 char *name
= mp
->strings
+ ksp
->st_name
;
3359 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3360 ip
= &mp
->chains
[*ip
]) {
3361 sp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_entsize
* *ip
);
3369 sym_insert(struct module
*mp
, char *name
, symid_t index
)
3374 if (kobj_debug
& D_SYMBOLS
) {
3375 static struct module
*lastmp
= NULL
;
3379 "krtld: symbol entry: file=%s\n",
3382 "krtld:\tsymndx\tvalue\t\t"
3386 sp
= (Sym
*)(mp
->symtbl
+
3387 index
* mp
->symhdr
->sh_entsize
);
3388 _kobj_printf(ops
, "krtld:\t[%3d]", index
);
3389 _kobj_printf(ops
, "\t0x%lx", sp
->st_value
);
3390 _kobj_printf(ops
, "\t%s\n", name
);
3394 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3395 ip
= &mp
->chains
[*ip
]) {
3402 kobj_boot_mod_lookup(const char *modname
)
3404 struct modctl
*mctl
= kobj_modules
;
3407 if (strcmp(modname
, mctl
->mod_modname
) == 0)
3409 } while ((mctl
= mctl
->mod_next
) != kobj_modules
);
3415 * Determine if the module exists.
3418 kobj_path_exists(char *name
, int use_path
)
3422 file
= kobj_open_path(name
, use_path
, 1);
3423 #ifdef MODDIR_SUFFIX
3424 if (file
== (struct _buf
*)-1)
3425 file
= kobj_open_path(name
, use_path
, 0);
3426 #endif /* MODDIR_SUFFIX */
3427 if (file
== (struct _buf
*)-1)
3429 kobj_close_file(file
);
3434 * fullname is dynamically allocated to be able to hold the
3435 * maximum size string that can be constructed from name.
3436 * path is exactly like the shell PATH variable.
3439 kobj_open_path(char *name
, int use_path
, int use_moddir_suffix
)
3448 #if !defined(MODDIR_SUFFIX)
3449 use_moddir_suffix
= B_FALSE
;
3453 pathp
= ""; /* use name as specified */
3455 pathp
= kobj_module_path
;
3456 /* use configured default path */
3458 pathpsave
= pathp
; /* keep this for error reporting */
3461 * Allocate enough space for the largest possible fullname.
3462 * since path is of the form <directory> : <directory> : ...
3463 * we're potentially allocating a little more than we need to
3464 * but we'll allocate the exact amount when we find the right directory.
3465 * (The + 3 below is one for NULL terminator and one for the '/'
3466 * we might have to add at the beginning of path and one for
3467 * the '/' between path and name.)
3469 maxpathlen
= strlen(pathp
) + strlen(name
) + 3;
3470 /* sizeof includes null */
3471 maxpathlen
+= sizeof (slash_moddir_suffix_slash
) - 1;
3472 fullname
= kobj_zalloc(maxpathlen
, KM_WAIT
);
3476 if (*pathp
!= '\0' && *pathp
!= '/')
3477 *p
++ = '/'; /* path must start with '/' */
3478 while (*pathp
&& *pathp
!= ':' && *pathp
!= ' ')
3480 if (p
!= fullname
&& p
[-1] != '/')
3482 if (use_moddir_suffix
) {
3483 char *b
= basename(name
);
3486 /* copy everything up to the base name */
3488 while (q
!= b
&& *q
)
3490 s
= slash_moddir_suffix_slash
;
3502 if ((file
= kobj_open_file(fullname
)) != (struct _buf
*)-1) {
3503 kobj_free(fullname
, maxpathlen
);
3506 while (*pathp
== ' ' || *pathp
== ':')
3512 kobj_free(fullname
, maxpathlen
);
3513 if (_moddebug
& MODDEBUG_ERRMSG
) {
3514 _kobj_printf(ops
, "can't open %s,", name
);
3515 _kobj_printf(ops
, " path is %s\n", pathpsave
);
3517 return ((struct _buf
*)-1);
3521 kobj_open(char *filename
)
3526 if (_modrootloaded
) {
3527 struct kobjopen_tctl
*ltp
= kobjopen_alloc(filename
);
3531 * Hand off the open to a thread who has a
3532 * stack size capable handling the request.
3534 if (curthread
!= &t0
) {
3535 (void) thread_create(NULL
, DEFAULTSTKSZ
* 2,
3536 kobjopen_thread
, ltp
, 0, &p0
, TS_RUN
, maxclsyspri
);
3542 * 1098067: module creds should not be those of the
3545 cred_t
*saved_cred
= curthread
->t_cred
;
3546 curthread
->t_cred
= kcred
;
3547 Errno
= vn_openat(filename
, UIO_SYSSPACE
, FREAD
, 0, &vp
,
3549 curthread
->t_cred
= saved_cred
;
3554 if (_moddebug
& MODDEBUG_ERRMSG
) {
3556 "kobj_open: vn_open of %s fails, ",
3558 _kobj_printf(ops
, "Errno = %d\n", Errno
);
3562 if (_moddebug
& MODDEBUG_ERRMSG
) {
3563 _kobj_printf(ops
, "kobj_open: '%s'", filename
);
3564 _kobj_printf(ops
, " vp = %p\n", vp
);
3566 return ((intptr_t)vp
);
3569 fd
= kobj_boot_open(filename
, 0);
3571 if (_moddebug
& MODDEBUG_ERRMSG
) {
3574 "kobj_open: can't open %s\n", filename
);
3576 _kobj_printf(ops
, "kobj_open: '%s'", filename
);
3577 _kobj_printf(ops
, " descr = 0x%x\n", fd
);
3580 return ((intptr_t)fd
);
3585 * Calls to kobj_open() are handled off to this routine as a separate thread.
3588 kobjopen_thread(struct kobjopen_tctl
*ltp
)
3593 mutex_init(&cpr_lk
, NULL
, MUTEX_DEFAULT
, NULL
);
3594 CALLB_CPR_INIT(&cpr_i
, &cpr_lk
, callb_generic_cpr
, "kobjopen");
3595 ltp
->Errno
= vn_open(ltp
->name
, UIO_SYSSPACE
, FREAD
, 0, &(ltp
->vp
),
3598 mutex_enter(&cpr_lk
);
3599 CALLB_CPR_EXIT(&cpr_i
);
3600 mutex_destroy(&cpr_lk
);
3605 * allocate and initialize a kobjopen thread structure
3607 static struct kobjopen_tctl
*
3608 kobjopen_alloc(char *filename
)
3610 struct kobjopen_tctl
*ltp
= kmem_zalloc(sizeof (*ltp
), KM_SLEEP
);
3612 ASSERT(filename
!= NULL
);
3614 ltp
->name
= kmem_alloc(strlen(filename
) + 1, KM_SLEEP
);
3615 bcopy(filename
, ltp
->name
, strlen(filename
) + 1);
3616 sema_init(<p
->sema
, 0, NULL
, SEMA_DEFAULT
, NULL
);
3621 * free a kobjopen thread control structure
3624 kobjopen_free(struct kobjopen_tctl
*ltp
)
3626 sema_destroy(<p
->sema
);
3627 kmem_free(ltp
->name
, strlen(ltp
->name
) + 1);
3628 kmem_free(ltp
, sizeof (*ltp
));
3632 kobj_read(intptr_t descr
, char *buf
, uint_t size
, uint_t offset
)
3637 if (_modrootloaded
) {
3638 if ((stat
= vn_rdwr(UIO_READ
, (struct vnode
*)descr
, buf
, size
,
3639 (offset_t
)offset
, UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(),
3642 "vn_rdwr failed with error 0x%x\n", stat
);
3645 return (size
- resid
);
3649 if (kobj_boot_seek((int)descr
, (off_t
)0, offset
) != 0) {
3651 "kobj_read: seek 0x%x failed\n", offset
);
3655 count
= kobj_boot_read((int)descr
, buf
, size
);
3657 if (_moddebug
& MODDEBUG_ERRMSG
) {
3659 "kobj_read: req %d bytes, ", size
);
3660 _kobj_printf(ops
, "got %d\n", count
);
3668 kobj_close(intptr_t descr
)
3670 if (_moddebug
& MODDEBUG_ERRMSG
)
3671 _kobj_printf(ops
, "kobj_close: 0x%lx\n", descr
);
3673 if (_modrootloaded
) {
3674 struct vnode
*vp
= (struct vnode
*)descr
;
3675 (void) VOP_CLOSE(vp
, FREAD
, 1, (offset_t
)0, CRED(), NULL
);
3678 (void) kobj_boot_close((int)descr
);
3682 kobj_fstat(intptr_t descr
, struct bootstat
*buf
)
3687 if (_modrootloaded
) {
3689 struct vnode
*vp
= (struct vnode
*)descr
;
3690 if (VOP_GETATTR(vp
, &vattr
, 0, kcred
, NULL
) != 0)
3694 * The vattr and bootstat structures are similar, but not
3695 * identical. We do our best to fill in the bootstat structure
3696 * from the contents of vattr (transfering only the ones that
3700 buf
->st_mode
= (uint32_t)vattr
.va_mode
;
3701 buf
->st_nlink
= (uint32_t)vattr
.va_nlink
;
3702 buf
->st_uid
= (int32_t)vattr
.va_uid
;
3703 buf
->st_gid
= (int32_t)vattr
.va_gid
;
3704 buf
->st_rdev
= (uint64_t)vattr
.va_rdev
;
3705 buf
->st_size
= (uint64_t)vattr
.va_size
;
3706 buf
->st_atim
.tv_sec
= (int64_t)vattr
.va_atime
.tv_sec
;
3707 buf
->st_atim
.tv_nsec
= (int64_t)vattr
.va_atime
.tv_nsec
;
3708 buf
->st_mtim
.tv_sec
= (int64_t)vattr
.va_mtime
.tv_sec
;
3709 buf
->st_mtim
.tv_nsec
= (int64_t)vattr
.va_mtime
.tv_nsec
;
3710 buf
->st_ctim
.tv_sec
= (int64_t)vattr
.va_ctime
.tv_sec
;
3711 buf
->st_ctim
.tv_nsec
= (int64_t)vattr
.va_ctime
.tv_nsec
;
3712 buf
->st_blksize
= (int32_t)vattr
.va_blksize
;
3713 buf
->st_blocks
= (int64_t)vattr
.va_nblocks
;
3718 return (kobj_boot_fstat((int)descr
, buf
));
3723 kobj_open_file(char *name
)
3726 struct compinfo cbuf
;
3729 if ((fd
= kobj_open(name
)) == -1) {
3730 return ((struct _buf
*)-1);
3733 file
= kobj_zalloc(sizeof (struct _buf
), KM_WAIT
|KM_TMP
);
3735 file
->_name
= kobj_alloc(strlen(name
)+1, KM_WAIT
|KM_TMP
);
3736 file
->_cnt
= file
->_size
= file
->_off
= 0;
3738 file
->_ptr
= file
->_base
;
3739 (void) strcpy(file
->_name
, name
);
3742 * Before root is mounted, we must check
3743 * for a compressed file and do our own
3746 if (_modrootloaded
) {
3747 file
->_base
= kobj_zalloc(MAXBSIZE
, KM_WAIT
);
3748 file
->_bsize
= MAXBSIZE
;
3750 /* Check if the file is compressed */
3751 file
->_iscmp
= kobj_is_compressed(fd
);
3753 if (kobj_boot_compinfo(fd
, &cbuf
) != 0) {
3754 kobj_close_file(file
);
3755 return ((struct _buf
*)-1);
3757 file
->_iscmp
= cbuf
.iscmp
;
3759 if (kobj_comp_setup(file
, &cbuf
) != 0) {
3760 kobj_close_file(file
);
3761 return ((struct _buf
*)-1);
3764 file
->_base
= kobj_zalloc(cbuf
.blksize
, KM_WAIT
|KM_TMP
);
3765 file
->_bsize
= cbuf
.blksize
;
3772 kobj_comp_setup(struct _buf
*file
, struct compinfo
*cip
)
3774 struct comphdr
*hdr
;
3777 * read the compressed image into memory,
3778 * so we can deompress from there
3780 file
->_dsize
= cip
->fsize
;
3781 file
->_dbuf
= kobj_alloc(cip
->fsize
, KM_WAIT
|KM_TMP
);
3782 if (kobj_read(file
->_fd
, file
->_dbuf
, cip
->fsize
, 0) != cip
->fsize
) {
3783 kobj_free(file
->_dbuf
, cip
->fsize
);
3787 hdr
= kobj_comphdr(file
);
3788 if (hdr
->ch_magic
!= CH_MAGIC_ZLIB
|| hdr
->ch_version
!= CH_VERSION
||
3789 hdr
->ch_algorithm
!= CH_ALG_ZLIB
|| hdr
->ch_fsize
== 0 ||
3790 !ISP2(hdr
->ch_blksize
)) {
3791 kobj_free(file
->_dbuf
, cip
->fsize
);
3794 file
->_base
= kobj_alloc(hdr
->ch_blksize
, KM_WAIT
|KM_TMP
);
3795 file
->_bsize
= hdr
->ch_blksize
;
3800 kobj_close_file(struct _buf
*file
)
3802 kobj_close(file
->_fd
);
3803 if (file
->_base
!= NULL
)
3804 kobj_free(file
->_base
, file
->_bsize
);
3805 if (file
->_dbuf
!= NULL
)
3806 kobj_free(file
->_dbuf
, file
->_dsize
);
3807 kobj_free(file
->_name
, strlen(file
->_name
)+1);
3808 kobj_free(file
, sizeof (struct _buf
));
3812 kobj_read_file(struct _buf
*file
, char *buf
, uint_t size
, uint_t off
)
3815 int b_off
; /* Offset into buffer for start of bcopy */
3819 if (_moddebug
& MODDEBUG_ERRMSG
) {
3820 _kobj_printf(ops
, "kobj_read_file: size=%x,", size
);
3821 _kobj_printf(ops
, " offset=%x at", off
);
3822 _kobj_printf(ops
, " buf=%x\n", buf
);
3826 * Handle compressed (gzip for now) file here. First get the
3827 * compressed size, then read the image into memory and finally
3828 * call zlib to decompress the image at the supplied memory buffer.
3830 if (file
->_iscmp
== CH_MAGIC_GZIP
) {
3833 struct vnode
*vp
= (struct vnode
*)file
->_fd
;
3837 if (VOP_GETATTR(vp
, &vattr
, 0, kcred
, NULL
) != 0)
3840 file
->_dbuf
= kobj_alloc(vattr
.va_size
, KM_WAIT
|KM_TMP
);
3841 file
->_dsize
= vattr
.va_size
;
3843 /* Read the compressed file into memory */
3844 if ((err
= vn_rdwr(UIO_READ
, vp
, file
->_dbuf
, vattr
.va_size
,
3845 (offset_t
)(0), UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(),
3848 _kobj_printf(ops
, "kobj_read_file :vn_rdwr() failed, "
3849 "error code 0x%x\n", err
);
3855 /* Decompress the image at the supplied memory buffer */
3856 if ((err
= z_uncompress(buf
, &dlen
, file
->_dbuf
,
3857 vattr
.va_size
)) != Z_OK
) {
3858 _kobj_printf(ops
, "kobj_read_file: z_uncompress "
3859 "failed, error code : 0x%x\n", err
);
3864 _kobj_printf(ops
, "kobj_read_file: z_uncompress "
3865 "failed to uncompress (size returned 0x%x , "
3866 "expected size: 0x%x)\n", dlen
, size
);
3874 page_addr
= F_PAGE(file
, off
);
3875 b_size
= file
->_size
;
3877 * If we have the filesystem page the caller's referring to
3878 * and we have something in the buffer,
3879 * satisfy as much of the request from the buffer as we can.
3881 if (page_addr
== file
->_off
&& b_size
> 0) {
3882 b_off
= B_OFFSET(file
, off
);
3883 c_size
= b_size
- b_off
;
3885 * If there's nothing to copy, we're at EOF.
3892 if (_moddebug
& MODDEBUG_ERRMSG
)
3893 _kobj_printf(ops
, "copying %x bytes\n",
3895 bcopy(file
->_base
+b_off
, buf
, c_size
);
3901 _kobj_printf(ops
, "kobj_read: system error");
3907 * If the caller's offset is page aligned and
3908 * the caller want's at least a filesystem page and
3909 * the caller provided a buffer,
3910 * read directly into the caller's buffer.
3912 if (page_addr
== off
&&
3913 (c_size
= F_BLKS(file
, size
)) && buf
) {
3914 c_size
= kobj_read_blks(file
, buf
, c_size
,
3921 if (c_size
!= F_BLKS(file
, size
))
3927 * Otherwise, read into our buffer and copy next time
3931 file
->_off
= page_addr
;
3932 c_size
= kobj_read_blks(file
, file
->_base
,
3933 file
->_bsize
, page_addr
);
3934 file
->_ptr
= file
->_base
;
3935 file
->_cnt
= c_size
;
3936 file
->_size
= c_size
;
3938 * If a _filbuf call or nothing read, break.
3940 if (buf
== NULL
|| c_size
<= 0) {
3945 if (_moddebug
& MODDEBUG_ERRMSG
)
3946 _kobj_printf(ops
, "read %x bytes\n", c_size
);
3949 if (_moddebug
& MODDEBUG_ERRMSG
)
3950 _kobj_printf(ops
, "count = %x\n", count
);
3956 kobj_read_blks(struct _buf
*file
, char *buf
, uint_t size
, uint_t off
)
3960 ASSERT(B_OFFSET(file
, size
) == 0 && B_OFFSET(file
, off
) == 0);
3966 for (blks
= size
/ file
->_bsize
; blks
!= 0; blks
--) {
3967 nret
= kobj_uncomp_blk(file
, buf
, off
);
3973 if (nret
< file
->_bsize
)
3977 ret
= kobj_read(file
->_fd
, buf
, size
, off
);
3982 kobj_uncomp_blk(struct _buf
*file
, char *buf
, uint_t off
)
3984 struct comphdr
*hdr
= kobj_comphdr(file
);
3989 dlen
= file
->_bsize
;
3990 i
= off
/ file
->_bsize
;
3991 src
= file
->_dbuf
+ hdr
->ch_blkmap
[i
];
3992 if (i
== hdr
->ch_fsize
/ file
->_bsize
)
3993 slen
= file
->_dsize
- hdr
->ch_blkmap
[i
];
3995 slen
= hdr
->ch_blkmap
[i
+ 1] - hdr
->ch_blkmap
[i
];
3996 if (z_uncompress(buf
, &dlen
, src
, slen
) != Z_OK
)
4002 kobj_filbuf(struct _buf
*f
)
4004 if (kobj_read_file(f
, NULL
, f
->_bsize
, f
->_off
+ f
->_size
) > 0)
4005 return (kobj_getc(f
));
4010 kobj_free(void *address
, size_t size
)
4015 kmem_free(address
, size
);
4016 kobj_stat
.nfree_calls
++;
4017 kobj_stat
.nfree
+= size
;
4021 kobj_zalloc(size_t size
, int flag
)
4025 if ((v
= kobj_alloc(size
, flag
)) != 0) {
4033 kobj_alloc(size_t size
, int flag
)
4036 * If we are running standalone in the
4037 * linker, we ask boot for memory.
4038 * Either it's temporary memory that we lose
4039 * once boot is mapped out or we allocate it
4040 * permanently using the dynamic data segment.
4044 if (flag
& (KM_TMP
| KM_SCRATCH
))
4045 return (bop_temp_alloc(size
, MINALIGN
));
4047 if (flag
& (KM_TMP
| KM_SCRATCH
))
4048 return (BOP_ALLOC(ops
, 0, size
, MINALIGN
));
4050 return (kobj_segbrk(&_edata
, size
, MINALIGN
, 0));
4053 kobj_stat
.nalloc_calls
++;
4054 kobj_stat
.nalloc
+= size
;
4056 return (kmem_alloc(size
, (flag
& KM_NOWAIT
) ? KM_NOSLEEP
: KM_SLEEP
));
4060 * Allow the "mod" system to sync up with the work
4061 * already done by kobj during the initial loading
4062 * of the kernel. This also gives us a chance
4063 * to reallocate memory that belongs to boot.
4068 struct modctl_list
*lp
, **lpp
;
4071 * The module path can be set in /etc/system via 'moddir' commands
4073 if (default_path
!= NULL
)
4074 kobj_module_path
= default_path
;
4076 default_path
= kobj_module_path
;
4078 ksyms_arena
= vmem_create("ksyms", NULL
, 0, sizeof (uint64_t),
4079 segkmem_alloc
, segkmem_free
, heap_arena
, 0, VM_SLEEP
);
4081 ctf_arena
= vmem_create("ctf", NULL
, 0, sizeof (uint_t
),
4082 segkmem_alloc
, segkmem_free
, heap_arena
, 0, VM_SLEEP
);
4085 * Move symbol tables from boot memory to ksyms_arena.
4087 for (lpp
= kobj_linkmaps
; *lpp
!= NULL
; lpp
++) {
4088 for (lp
= *lpp
; lp
!= NULL
; lp
= lp
->modl_next
)
4089 kobj_export_module(mod(lp
));
4094 kobj_segbrk(caddr_t
*spp
, size_t size
, size_t align
, caddr_t limit
)
4097 size_t alloc_pgsz
= kobj_mmu_pagesize
;
4098 size_t alloc_align
= BO_NO_ALIGN
;
4102 * If we are using "large" mappings for the kernel,
4103 * request aligned memory from boot using the
4107 alloc_align
= lg_pagesize
;
4108 alloc_pgsz
= lg_pagesize
;
4111 #if defined(__sparc)
4112 /* account for redzone */
4114 limit
-= alloc_pgsz
;
4115 #endif /* __sparc */
4117 va
= ALIGN((uintptr_t)*spp
, align
);
4118 pva
= P2ROUNDUP((uintptr_t)*spp
, alloc_pgsz
);
4122 if (va
+ size
> pva
) {
4125 alloc_size
= P2ROUNDUP(size
- (pva
- va
), alloc_pgsz
);
4127 * Check for overlapping segments.
4129 if (limit
&& limit
<= *spp
+ alloc_size
) {
4130 return ((caddr_t
)0);
4133 npva
= (uintptr_t)BOP_ALLOC(ops
, (caddr_t
)pva
,
4134 alloc_size
, alloc_align
);
4137 _kobj_printf(ops
, "BOP_ALLOC failed, 0x%lx bytes",
4139 _kobj_printf(ops
, " aligned %lx", alloc_align
);
4140 _kobj_printf(ops
, " at 0x%lx\n", pva
);
4144 *spp
= (caddr_t
)(va
+ size
);
4146 return ((caddr_t
)va
);
4150 * Calculate the number of output hash buckets.
4151 * We use the next prime larger than n / 4,
4152 * so the average hash chain is about 4 entries.
4153 * More buckets would just be a waste of memory.
4156 kobj_gethashsize(uint_t n
)
4159 int hsize
= MAX(n
/ 4, 2);
4161 for (f
= 2; f
* f
<= hsize
; f
++)
4169 * Get the file size.
4171 * Before root is mounted, files are compressed in the boot_archive ramdisk
4172 * (in the memory). kobj_fstat would return the compressed file size.
4173 * In order to get the uncompressed file size, read the file to the end and
4177 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
4183 if (_modrootloaded
) {
4184 struct bootstat bst
;
4186 if (kobj_fstat(file
->_fd
, &bst
) != 0)
4188 *size
= bst
.st_size
;
4190 if (file
->_iscmp
== CH_MAGIC_GZIP
) {
4192 * Read the last 4 bytes of the compressed (gzip)
4193 * image to get the size of its uncompressed
4196 if ((err
= vn_rdwr(UIO_READ
, (struct vnode
*)file
->_fd
,
4197 (char *)(&buf
), 4, (offset_t
)(*size
- 4),
4198 UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(), &resid
))
4200 _kobj_printf(ops
, "kobj_get_filesize: "
4201 "vn_rdwr() failed with error 0x%x\n", err
);
4205 *size
= (uint64_t)buf
;
4210 struct bootstat bsb
;
4213 struct comphdr
*hdr
= kobj_comphdr(file
);
4215 *size
= hdr
->ch_fsize
;
4216 } else if (kobj_boot_fstat(file
->_fd
, &bsb
) != 0)
4219 *size
= bsb
.st_size
;
4223 uint64_t offset
= 0;
4225 buf
= kmem_alloc(MAXBSIZE
, KM_SLEEP
);
4227 count
= kobj_read_file(file
, buf
, MAXBSIZE
, offset
);
4229 kmem_free(buf
, MAXBSIZE
);
4233 } while (count
== MAXBSIZE
);
4234 kmem_free(buf
, MAXBSIZE
);
4254 return (q
? q
+ 1 : s
);
4258 kobj_stat_get(kobj_stat_t
*kp
)
4266 return (lg_pagesize
);
4270 kobj_textwin_alloc(struct module
*mp
)
4272 ASSERT(MUTEX_HELD(&mod_lock
));
4274 if (mp
->textwin
!= NULL
)
4278 * If the text is not contained in the heap, then it is not contained
4279 * by a writable mapping. (Specifically, it's on the nucleus page.)
4280 * We allocate a read/write mapping for this module's text to allow
4281 * the text to be patched without calling hot_patch_kernel_text()
4282 * (which is quite slow).
4284 if (!vmem_contains(heaptext_arena
, mp
->text
, mp
->text_size
)) {
4285 uintptr_t text
= (uintptr_t)mp
->text
;
4286 uintptr_t size
= (uintptr_t)mp
->text_size
;
4289 size_t sz
= ((text
+ size
+ PAGESIZE
- 1) & PAGEMASK
) -
4292 va
= mp
->textwin_base
= vmem_alloc(heap_arena
, sz
, VM_SLEEP
);
4294 for (i
= text
& PAGEMASK
; i
< text
+ size
; i
+= PAGESIZE
) {
4295 hat_devload(kas
.a_hat
, va
, PAGESIZE
,
4296 hat_getpfnum(kas
.a_hat
, (caddr_t
)i
),
4297 PROT_READ
| PROT_WRITE
,
4298 HAT_LOAD_LOCK
| HAT_LOAD_NOCONSIST
);
4302 mp
->textwin
= mp
->textwin_base
+ (text
& PAGEOFFSET
);
4304 mp
->textwin
= mp
->text
;
4309 kobj_textwin_free(struct module
*mp
)
4311 uintptr_t text
= (uintptr_t)mp
->text
;
4312 uintptr_t tsize
= (uintptr_t)mp
->text_size
;
4313 size_t size
= (((text
+ tsize
+ PAGESIZE
- 1) & PAGEMASK
) -
4318 if (mp
->textwin_base
== NULL
)
4321 hat_unload(kas
.a_hat
, mp
->textwin_base
, size
, HAT_UNLOAD_UNLOCK
);
4322 vmem_free(heap_arena
, mp
->textwin_base
, size
);
4323 mp
->textwin_base
= NULL
;
4327 find_libmacro(char *name
)
4331 for (lmi
= 0; lmi
< NLIBMACROS
; lmi
++) {
4332 if (strcmp(name
, libmacros
[lmi
].lmi_macroname
) == 0)
4333 return (libmacros
[lmi
].lmi_list
);
4339 * Check for $MACRO in tail (string to expand) and expand it in path at pathend
4340 * returns path if successful, else NULL
4341 * Support multiple $MACROs expansion and the first valid path will be returned
4342 * Caller's responsibility to provide enough space in path to expand
4345 expand_libmacro(char *tail
, char *path
, char *pathend
)
4347 char c
, *p
, *p1
, *p2
, *path2
, *endp
;
4348 int diff
, lmi
, macrolen
, valid_macro
, more_macro
;
4352 * check for $MACROS between nulls or slashes
4354 p
= strchr(tail
, '$');
4357 for (lmi
= 0; lmi
< NLIBMACROS
; lmi
++) {
4358 macrolen
= libmacros
[lmi
].lmi_macrolen
;
4359 if (strncmp(p
+ 1, libmacros
[lmi
].lmi_macroname
, macrolen
) == 0)
4364 if (lmi
< NLIBMACROS
) {
4366 * The following checks are used to restrict expansion of
4367 * macros to those that form a full directory/file name
4368 * and to keep the behavior same as before. If this
4369 * restriction is removed or no longer valid in the future,
4370 * the checks below can be deleted.
4372 if ((p
== tail
) || (*(p
- 1) == '/')) {
4373 c
= *(p
+ macrolen
+ 1);
4374 if (c
== '/' || c
== '\0')
4380 p2
= strchr(p
, '/');
4382 * if no more macro to expand, then just copy whatever left
4383 * and check whether it exists
4385 if (p2
== NULL
|| strchr(p2
, '$') == NULL
) {
4386 (void) strcpy(pathend
, tail
);
4387 if ((file
= kobj_open_path(path
, 1, 1)) !=
4388 (struct _buf
*)-1) {
4389 kobj_close_file(file
);
4395 * copy all chars before '/' and call expand_libmacro()
4399 bcopy(tail
, pathend
, diff
);
4402 return (expand_libmacro(p2
, path
, pathend
));
4408 endp
= p
+ macrolen
+ 1;
4409 if (strchr(endp
, '$') != NULL
)
4415 * copy lmi_list and split it into components.
4416 * then put the part of tail before $MACRO into path
4421 bcopy(tail
, pathend
, diff
);
4422 path2
= pathend
+ diff
;
4423 p1
= libmacros
[lmi
].lmi_list
;
4424 while (p1
&& (*p1
!= '\0')) {
4425 p2
= strchr(p1
, ':');
4428 bcopy(p1
, path2
, diff
);
4429 *(path2
+ diff
) = '\0';
4432 bcopy(p1
, path2
, diff
+ 1);
4434 /* copy endp only if there isn't any more macro to expand */
4435 if (!more_macro
&& (endp
!= NULL
))
4436 (void) strcat(path2
, endp
);
4437 file
= kobj_open_path(path
, 1, 1);
4438 if (file
!= (struct _buf
*)-1) {
4439 kobj_close_file(file
);
4441 * if more macros to expand then call expand_libmacro(),
4442 * else return path which has the whole path
4444 if (!more_macro
|| (expand_libmacro(endp
, path
,
4445 path2
+ diff
) != NULL
)) {
4458 tnf_add_notifyunload(kobj_notify_f
*fp
)
4460 kobj_notify_list_t
*entry
;
4462 entry
= kobj_alloc(sizeof (kobj_notify_list_t
), KM_WAIT
);
4463 entry
->kn_type
= KOBJ_NOTIFY_MODUNLOADING
;
4464 entry
->kn_func
= fp
;
4465 (void) kobj_notify_add(entry
);
4470 tnf_unsplice_probes(uint_t what
, struct modctl
*mod
)
4472 tnf_probe_control_t
**p
;
4474 struct module
*mp
= mod
->mod_mp
;
4476 if (!(mp
->flags
& KOBJ_TNF_PROBE
))
4479 for (p
= &__tnf_probe_list_head
; *p
; )
4480 if (kobj_addrcheck(mp
, (char *)*p
) == 0)
4485 for (q
= &__tnf_tag_list_head
; *q
; )
4486 if (kobj_addrcheck(mp
, (char *)*q
) == 0)
4487 *q
= (tnf_tag_data_t
*)(*q
)->tag_version
;
4489 q
= (tnf_tag_data_t
**)&(*q
)->tag_version
;
4491 tnf_changed_probe_list
= 1;
4495 tnf_splice_probes(int boot_load
, tnf_probe_control_t
*plist
,
4496 tnf_tag_data_t
*tlist
)
4499 static int add_notify
= 1;
4502 tnf_probe_control_t
*pl
;
4504 for (pl
= plist
; pl
->next
; )
4508 mutex_enter(&mod_lock
);
4509 tnf_changed_probe_list
= 1;
4510 pl
->next
= __tnf_probe_list_head
;
4511 __tnf_probe_list_head
= plist
;
4513 mutex_exit(&mod_lock
);
4520 for (tl
= tlist
; tl
->tag_version
; )
4521 tl
= (tnf_tag_data_t
*)tl
->tag_version
;
4524 mutex_enter(&mod_lock
);
4525 tl
->tag_version
= (tnf_tag_version_t
*)__tnf_tag_list_head
;
4526 __tnf_tag_list_head
= tlist
;
4528 mutex_exit(&mod_lock
);
4531 if (!boot_load
&& result
&& add_notify
) {
4532 tnf_add_notifyunload(tnf_unsplice_probes
);
4538 char *kobj_file_buf
;
4539 int kobj_file_bufsize
;
4542 * This code is for the purpose of manually recording which files
4543 * needs to go into the boot archive on any given system.
4545 * To enable the code, set kobj_file_bufsize in /etc/system
4546 * and reboot the system, then use mdb to look at kobj_file_buf.
4549 kobj_record_file(char *filename
)
4552 static int size
= 0;
4555 if (kobj_file_bufsize
== 0) /* don't bother */
4558 if (kobj_file_buf
== NULL
) { /* allocate buffer */
4559 size
= kobj_file_bufsize
;
4560 buf
= kobj_file_buf
= kobj_alloc(size
, KM_WAIT
|KM_TMP
);
4563 n
= snprintf(buf
, size
, "%s\n", filename
);
4571 kobj_boot_fstat(int fd
, struct bootstat
*stp
)
4574 if (!standalone
&& _ioquiesced
)
4576 return (BOP_FSTAT(ops
, fd
, stp
));
4578 return (BRD_FSTAT(bfs_ops
, fd
, stp
));
4583 kobj_boot_open(char *filename
, int flags
)
4588 * If io via bootops is quiesced, it means boot is no longer
4589 * available to us. We make it look as if we can't open the
4590 * named file - which is reasonably accurate.
4592 if (!standalone
&& _ioquiesced
)
4595 kobj_record_file(filename
);
4596 return (BOP_OPEN(filename
, flags
));
4598 kobj_record_file(filename
);
4599 return (BRD_OPEN(bfs_ops
, filename
, flags
));
4604 kobj_boot_close(int fd
)
4607 if (!standalone
&& _ioquiesced
)
4610 return (BOP_CLOSE(fd
));
4612 return (BRD_CLOSE(bfs_ops
, fd
));
4618 kobj_boot_seek(int fd
, off_t hi
, off_t lo
)
4621 return (BOP_SEEK(fd
, lo
) == -1 ? -1 : 0);
4623 return (BRD_SEEK(bfs_ops
, fd
, lo
, SEEK_SET
));
4628 kobj_boot_read(int fd
, caddr_t buf
, size_t size
)
4631 return (BOP_READ(fd
, buf
, size
));
4633 return (BRD_READ(bfs_ops
, fd
, buf
, size
));
4638 kobj_boot_compinfo(int fd
, struct compinfo
*cb
)
4640 return (boot_compinfo(fd
, cb
));
4644 * Check if the file is compressed (for now we handle only gzip).
4645 * It returns CH_MAGIC_GZIP if the file is compressed and 0 otherwise.
4648 kobj_is_compressed(intptr_t fd
)
4650 struct vnode
*vp
= (struct vnode
*)fd
;
4655 if ((err
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)((intptr_t)&magic_buf
),
4656 sizeof (magic_buf
), (offset_t
)(0),
4657 UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(), &resid
)) != 0) {
4659 _kobj_printf(ops
, "kobj_is_compressed: vn_rdwr() failed, "
4660 "error code 0x%x\n", err
);
4664 if (magic_buf
== CH_MAGIC_GZIP
)
4665 return (CH_MAGIC_GZIP
);