loader: remove devdesc comments and assertions
[unleashed.git] / kernel / krtld / kobj.c
blobd3d986f8d990f6a8ca92b49dde4f83b5a0c4e6b8
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2011 Bayard G. Bell <buffer.g.overflow@gmail.com>.
27 * All rights reserved. Use is subject to license terms.
31 * Kernel's linker/loader
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/sysmacros.h>
37 #include <sys/systm.h>
38 #include <sys/user.h>
39 #include <sys/kmem.h>
40 #include <sys/reboot.h>
41 #include <sys/bootconf.h>
42 #include <sys/debug.h>
43 #include <sys/uio.h>
44 #include <sys/file.h>
45 #include <sys/vnode.h>
46 #include <sys/user.h>
47 #include <sys/mman.h>
48 #include <vm/as.h>
49 #include <vm/seg_kp.h>
50 #include <vm/seg_kmem.h>
51 #include <sys/elf.h>
52 #include <sys/elf_notes.h>
53 #include <sys/vmsystm.h>
54 #include <sys/kdi.h>
55 #include <sys/atomic.h>
56 #include <sys/kmdb.h>
58 #include <sys/link.h>
59 #include <sys/kobj.h>
60 #include <sys/ksyms.h>
61 #include <sys/disp.h>
62 #include <sys/modctl.h>
63 #include <sys/varargs.h>
64 #include <sys/kstat.h>
65 #include <sys/kobj_impl.h>
66 #include <sys/fs/decomp.h>
67 #include <sys/callb.h>
68 #include <sys/cmn_err.h>
69 #include <sys/tnf_probe.h>
70 #include <sys/zmod.h>
72 #include <krtld/reloc.h>
73 #include <krtld/kobj_kdi.h>
74 #include <sys/sha1.h>
75 #include <sys/crypto/elfsign.h>
77 #if !defined(_OBP)
78 #include <sys/bootvfs.h>
79 #endif
82 * do_symbols() error codes
84 #define DOSYM_UNDEF -1 /* undefined symbol */
85 #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */
87 #if !defined(_OBP)
88 static void synthetic_bootaux(char *, val_t *);
89 #endif
91 static struct module *load_exec(val_t *, char *);
92 static void load_linker(val_t *);
93 static struct modctl *add_primary(const char *filename, int);
94 static int bind_primary(val_t *, int);
95 static int load_primary(struct module *, int);
96 static int load_kmdb(val_t *);
97 static int get_progbits(struct module *, struct _buf *);
98 static int get_syms(struct module *, struct _buf *);
99 static int get_ctf(struct module *, struct _buf *);
100 static void get_signature(struct module *, struct _buf *);
101 static int do_common(struct module *);
102 static void add_dependent(struct module *, struct module *);
103 static int do_dependents(struct modctl *, char *, size_t);
104 static int do_symbols(struct module *, Elf64_Addr);
105 static void module_assign(struct modctl *, struct module *);
106 static void free_module_data(struct module *);
107 static char *depends_on(struct module *);
108 static char *getmodpath(const char *);
109 static char *basename(char *);
110 static void attr_val(val_t *);
111 static char *find_libmacro(char *);
112 static char *expand_libmacro(char *, char *, char *);
113 static int read_bootflags(void);
114 static int kobj_comp_setup(struct _buf *, struct compinfo *);
115 static int kobj_uncomp_blk(struct _buf *, caddr_t, uint_t);
116 static int kobj_read_blks(struct _buf *, caddr_t, uint_t, uint_t);
117 static int kobj_boot_open(char *, int);
118 static int kobj_boot_close(int);
119 static int kobj_boot_seek(int, off_t, off_t);
120 static int kobj_boot_read(int, caddr_t, size_t);
121 static int kobj_boot_fstat(int, struct bootstat *);
122 static int kobj_boot_compinfo(int, struct compinfo *);
123 extern int kobj_boot_mountroot(void);
125 static Sym *lookup_one(struct module *, const char *);
126 static void sym_insert(struct module *, char *, symid_t);
127 static Sym *sym_lookup(struct module *, Sym *);
129 static struct kobjopen_tctl *kobjopen_alloc(char *filename);
130 static void kobjopen_free(struct kobjopen_tctl *ltp);
131 static void kobjopen_thread(struct kobjopen_tctl *ltp);
132 static int kobj_is_compressed(intptr_t);
134 extern int kcopy(const void *, void *, size_t);
135 extern int elf_mach_ok(Ehdr *);
136 extern int alloc_gottable(struct module *, caddr_t *, caddr_t *);
138 static void tnf_unsplice_probes(uint_t, struct modctl *);
139 extern tnf_probe_control_t *__tnf_probe_list_head;
140 extern tnf_tag_data_t *__tnf_tag_list_head;
142 extern int modrootloaded;
143 extern int swaploaded;
144 extern int bop_io_quiesced;
145 extern int last_module_id;
147 extern char stubs_base[];
148 extern char stubs_end[];
150 #ifdef KOBJ_DEBUG
152 * Values that can be or'd in to kobj_debug and their effects:
154 * D_DEBUG - misc. debugging information.
155 * D_SYMBOLS - list symbols and their values as they are entered
156 * into the hash table
157 * D_RELOCATIONS - display relocation processing information
158 * D_LOADING - display information about each module as it
159 * is loaded.
161 int kobj_debug = 0;
163 #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \
164 (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s))
165 #else
166 #define KOBJ_MARK(s) /* discard */
167 #endif
169 #define MODPATH_PROPNAME "module-path"
171 #define _moddebug get_weakish_int(&moddebug)
172 #define _modrootloaded get_weakish_int(&modrootloaded)
173 #define _swaploaded get_weakish_int(&swaploaded)
174 #define _ioquiesced get_weakish_int(&bop_io_quiesced)
176 #define mod(X) (struct module *)((X)->modl_modp->mod_mp)
178 void *romp; /* rom vector (opaque to us) */
179 struct bootops *ops; /* bootops vector */
180 void *dbvec; /* debug vector */
183 * kobjopen thread control structure
185 struct kobjopen_tctl {
186 ksema_t sema;
187 char *name; /* name of file */
188 struct vnode *vp; /* vnode return from vn_open() */
189 int Errno; /* error return from vnopen */
193 * Structure for defining dynamically expandable library macros
196 struct lib_macro_info {
197 char *lmi_list; /* ptr to list of possible choices */
198 char *lmi_macroname; /* pointer to macro name */
199 ushort_t lmi_ba_index; /* index into bootaux vector */
200 ushort_t lmi_macrolen; /* macro length */
201 } libmacros[] = {
202 { NULL, "CPU", BA_CPU, 0 },
203 { NULL, "MMU", BA_MMU, 0 }
206 #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info)
208 char *boot_cpu_compatible_list; /* make $CPU available */
210 char *kobj_module_path; /* module search path */
211 vmem_t *text_arena; /* module text arena */
212 static vmem_t *data_arena; /* module data & bss arena */
213 static vmem_t *ctf_arena; /* CTF debug data arena */
214 static struct modctl *kobj_modules = NULL; /* modules loaded */
215 int kobj_mmu_pagesize; /* system pagesize */
216 static int lg_pagesize; /* "large" pagesize */
217 static int kobj_last_module_id = 0; /* id assignment */
218 static kmutex_t kobj_lock; /* protects mach memory list */
221 * The following functions have been implemented by the kernel.
222 * However, many 3rd party drivers provide their own implementations
223 * of these functions. When such drivers are loaded, messages
224 * indicating that these symbols have been multiply defined will be
225 * emitted to the console. To avoid alarming customers for no good
226 * reason, we simply suppress such warnings for the following set of
227 * functions.
229 static char *suppress_sym_list[] =
231 "strstr",
232 "strncat",
233 "strlcat",
234 "strlcpy",
235 "strspn",
236 "memcpy",
237 "memset",
238 "memmove",
239 "memcmp",
240 "memchr",
241 "__udivdi3",
242 "__divdi3",
243 "__umoddi3",
244 "__moddi3",
245 NULL /* This entry must exist */
248 /* indexed by KOBJ_NOTIFY_* */
249 static kobj_notify_list_t *kobj_notifiers[KOBJ_NOTIFY_MAX + 1];
252 * TNF probe management globals
254 tnf_probe_control_t *__tnf_probe_list_head = NULL;
255 tnf_tag_data_t *__tnf_tag_list_head = NULL;
256 int tnf_changed_probe_list = 0;
259 * Prefix for statically defined tracing (SDT) DTrace probes.
261 const char *sdt_prefix = "__dtrace_probe_";
264 * Beginning and end of the kernel's dynamic text/data segments.
266 static caddr_t _text;
267 static caddr_t _etext;
268 static caddr_t _data;
271 * The sparc linker doesn't create a memory location
272 * for a variable named _edata, so _edata can only be
273 * referred to, not modified. krtld needs a static
274 * variable to modify it - within krtld, of course -
275 * outside of krtld, e_data is used in all kernels.
277 extern caddr_t _edata;
279 Addr dynseg = 0; /* load address of "dynamic" segment */
280 size_t dynsize; /* "dynamic" segment size */
283 int standalone = 1; /* an unwholey kernel? */
284 int use_iflush; /* iflush after relocations */
287 * _kobj_printf()
289 * Common printf function pointer. Can handle only one conversion
290 * specification in the format string. Some of the functions invoked
291 * through this function pointer cannot handle more that one conversion
292 * specification in the format string.
294 void (*_kobj_printf)(void *, const char *, ...); /* printf routine */
297 * Standalone function pointers for use within krtld.
298 * Many platforms implement optimized platmod versions of
299 * utilities such as bcopy and any such are not yet available
300 * until the kernel is more completely stitched together.
301 * See kobj_impl.h
303 void (*kobj_bcopy)(const void *, void *, size_t);
304 void (*kobj_bzero)(void *, size_t);
305 size_t (*kobj_strlcat)(char *, const char *, size_t);
307 static kobj_stat_t kobj_stat;
309 #define MINALIGN 8 /* at least a double-word */
312 get_weakish_int(int *ip)
314 if (standalone)
315 return (0);
316 return (ip == NULL ? 0 : *ip);
319 static void *
320 get_weakish_pointer(void **ptrp)
322 if (standalone)
323 return (0);
324 return (ptrp == NULL ? 0 : *ptrp);
328 * XXX fix dependencies on "kernel"; this should work
329 * for other standalone binaries as well.
331 * XXX Fix hashing code to use one pointer to
332 * hash entries.
333 * |----------|
334 * | nbuckets |
335 * |----------|
336 * | nchains |
337 * |----------|
338 * | bucket[] |
339 * |----------|
340 * | chain[] |
341 * |----------|
345 * Load, bind and relocate all modules that
346 * form the primary kernel. At this point, our
347 * externals have not been relocated.
349 void
350 kobj_init(
351 void *romvec,
352 void *dvec,
353 struct bootops *bootvec,
354 val_t *bootaux)
356 struct module *mp;
357 struct modctl *modp;
358 Addr entry;
359 char filename[MAXPATHLEN];
362 * Save these to pass on to
363 * the booted standalone.
365 romp = romvec;
366 dbvec = dvec;
368 ops = bootvec;
369 kobj_setup_standalone_vectors();
371 KOBJ_MARK("Entered kobj_init()");
373 (void) BOP_GETPROP(ops, "whoami", filename);
376 * We don't support standalone debuggers anymore. The use of kadb
377 * will interfere with the later use of kmdb. Let the user mend
378 * their ways now. Users will reach this message if they still
379 * have the kadb binary on their system (perhaps they used an old
380 * bfu, or maybe they intentionally copied it there) and have
381 * specified its use in a way that eluded our checking in the boot
382 * program.
384 if (dvec != NULL) {
385 _kobj_printf(ops, "\nWARNING: Standalone debuggers such as "
386 "kadb are no longer supported\n\n");
387 goto fail;
390 #if defined(_OBP)
392 * OBP allows us to read both the ramdisk and
393 * the underlying root fs when root is a disk.
394 * This can lower incidences of unbootable systems
395 * when the archive is out-of-date with the /etc
396 * state files.
398 if (BOP_MOUNTROOT() != BOOT_SVC_OK) {
399 _kobj_printf(ops, "can't mount boot fs\n");
400 goto fail;
402 #else
404 /* on x86, we always boot with a ramdisk */
405 (void) kobj_boot_mountroot();
408 * Now that the ramdisk is mounted, finish boot property
409 * initialization.
411 boot_prop_finish();
415 * The early boot code does -not- hand us any of the dynamic
416 * metadata about the executable. In particular, it does not read
417 * in, map or otherwise look at the program headers. We fake all
418 * that up now.
420 * We do this early as DTrace static probes both call undefined
421 * references. We have to process those relocations before calling
422 * any of them.
424 * OBP tells kobj_start() where the ELF image is in memory, so it
425 * synthesized bootaux before kobj_init() was called
427 if (bootaux[BA_PHDR].ba_ptr == NULL)
428 synthetic_bootaux(filename, bootaux);
430 #endif /* _OBP */
433 * Save the interesting attribute-values
434 * (scanned by kobj_boot).
436 attr_val(bootaux);
439 * Set the module search path.
441 kobj_module_path = getmodpath(filename);
443 boot_cpu_compatible_list = find_libmacro("CPU");
446 * These two modules have actually been
447 * loaded by boot, but we finish the job
448 * by introducing them into the world of
449 * loadable modules.
452 mp = load_exec(bootaux, filename);
453 load_linker(bootaux);
456 * Load all the primary dependent modules.
458 if (load_primary(mp, KOBJ_LM_PRIMARY) == -1)
459 goto fail;
462 * Glue it together.
464 if (bind_primary(bootaux, KOBJ_LM_PRIMARY) == -1)
465 goto fail;
467 entry = bootaux[BA_ENTRY].ba_val;
470 * Get the boot flags
472 bootflags(ops);
474 if (boothowto & RB_VERBOSE)
475 kobj_lm_dump(KOBJ_LM_PRIMARY);
477 kobj_kdi_init();
479 if (boothowto & RB_KMDB) {
480 if (load_kmdb(bootaux) < 0)
481 goto fail;
485 * Post setup.
487 s_text = _text;
488 e_text = _etext;
489 s_data = _data;
490 e_data = _edata;
492 kobj_sync_instruction_memory(s_text, e_text - s_text);
494 #ifdef KOBJ_DEBUG
495 if (kobj_debug & D_DEBUG)
496 _kobj_printf(ops,
497 "krtld: transferring control to: 0x%p\n", entry);
498 #endif
501 * Make sure the mod system knows about the modules already loaded.
503 last_module_id = kobj_last_module_id;
504 bcopy(kobj_modules, &modules, sizeof (modules));
505 modp = &modules;
506 do {
507 if (modp->mod_next == kobj_modules)
508 modp->mod_next = &modules;
509 if (modp->mod_prev == kobj_modules)
510 modp->mod_prev = &modules;
511 } while ((modp = modp->mod_next) != &modules);
513 standalone = 0;
515 #ifdef KOBJ_DEBUG
516 if (kobj_debug & D_DEBUG)
517 _kobj_printf(ops,
518 "krtld: really transferring control to: 0x%p\n", entry);
519 #endif
521 /* restore printf/bcopy/bzero vectors before returning */
522 kobj_restore_vectors();
524 #if defined(_DBOOT)
526 * krtld was called from a dboot ELF section, the embedded
527 * dboot code contains the real entry via bootaux
529 exitto((caddr_t)entry);
530 #else
532 * krtld was directly called from startup
534 return;
535 #endif
537 fail:
539 _kobj_printf(ops, "krtld: error during initial load/link phase\n");
540 _kobj_printf(ops, "\n");
541 _kobj_printf(ops, "krtld could neither locate nor resolve symbols"
542 " for:\n");
543 _kobj_printf(ops, " %s\n", filename);
544 _kobj_printf(ops, "in the boot archive. Please verify that this"
545 " file\n");
546 _kobj_printf(ops, "matches what is found in the boot archive.\n");
547 bop_panic("Unable to boot");
550 #if !defined(_OBP)
552 * Synthesize additional metadata that describes the executable as
553 * krtld's caller didn't do it.
555 static void
556 synthetic_bootaux(char *filename, val_t *bootaux)
558 Ehdr ehdr;
559 caddr_t phdrbase;
560 struct _buf *file;
561 int i, n;
564 * Elf header
566 KOBJ_MARK("synthetic_bootaux()");
567 KOBJ_MARK(filename);
568 file = kobj_open_file(filename);
569 if (file == (struct _buf *)-1) {
570 _kobj_printf(ops, "krtld: failed to open '%s'\n", filename);
571 return;
573 KOBJ_MARK("reading program headers");
574 if (kobj_read_file(file, (char *)&ehdr, sizeof (ehdr), 0) < 0) {
575 _kobj_printf(ops, "krtld: %s: failed to read ehder\n",
576 filename);
577 return;
581 * Program headers
583 bootaux[BA_PHNUM].ba_val = ehdr.e_phnum;
584 bootaux[BA_PHENT].ba_val = ehdr.e_phentsize;
585 n = ehdr.e_phentsize * ehdr.e_phnum;
587 phdrbase = kobj_alloc(n, KM_WAIT | KM_TMP);
589 if (kobj_read_file(file, phdrbase, n, ehdr.e_phoff) < 0) {
590 _kobj_printf(ops, "krtld: %s: failed to read phdrs\n",
591 filename);
592 return;
594 bootaux[BA_PHDR].ba_ptr = phdrbase;
595 kobj_close_file(file);
596 KOBJ_MARK("closed file");
599 * Find the dynamic section address
601 for (i = 0; i < ehdr.e_phnum; i++) {
602 Phdr *phdr = (Phdr *)(phdrbase + ehdr.e_phentsize * i);
604 if (phdr->p_type == PT_DYNAMIC) {
605 bootaux[BA_DYNAMIC].ba_ptr = (void *)phdr->p_vaddr;
606 break;
609 KOBJ_MARK("synthetic_bootaux() done");
611 #endif /* !_OBP */
614 * Set up any global information derived
615 * from attribute/values in the boot or
616 * aux vector.
618 static void
619 attr_val(val_t *bootaux)
621 Phdr *phdr;
622 int phnum, phsize;
623 int i;
625 KOBJ_MARK("attr_val()");
626 kobj_mmu_pagesize = bootaux[BA_PAGESZ].ba_val;
627 lg_pagesize = bootaux[BA_LPAGESZ].ba_val;
628 use_iflush = bootaux[BA_IFLUSH].ba_val;
630 phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr;
631 phnum = bootaux[BA_PHNUM].ba_val;
632 phsize = bootaux[BA_PHENT].ba_val;
633 for (i = 0; i < phnum; i++) {
634 phdr = (Phdr *)(bootaux[BA_PHDR].ba_val + i * phsize);
636 if (phdr->p_type != PT_LOAD) {
637 continue;
640 * Bounds of the various segments.
642 if (!(phdr->p_flags & PF_X)) {
643 #if defined(_RELSEG)
645 * sparc kernel puts the dynamic info
646 * into a separate segment, which is
647 * free'd in bop_fini()
649 ASSERT(phdr->p_vaddr != 0);
650 dynseg = phdr->p_vaddr;
651 dynsize = phdr->p_memsz;
652 #else
653 ASSERT(phdr->p_vaddr == 0);
654 #endif
655 } else {
656 if (phdr->p_flags & PF_W) {
657 _data = (caddr_t)phdr->p_vaddr;
658 _edata = _data + phdr->p_memsz;
659 } else {
660 _text = (caddr_t)phdr->p_vaddr;
661 _etext = _text + phdr->p_memsz;
666 /* To do the kobj_alloc, _edata needs to be set. */
667 for (i = 0; i < NLIBMACROS; i++) {
668 if (bootaux[libmacros[i].lmi_ba_index].ba_ptr != NULL) {
669 libmacros[i].lmi_list = kobj_alloc(
670 strlen(bootaux[libmacros[i].lmi_ba_index].ba_ptr) +
671 1, KM_WAIT);
672 (void) strcpy(libmacros[i].lmi_list,
673 bootaux[libmacros[i].lmi_ba_index].ba_ptr);
675 libmacros[i].lmi_macrolen = strlen(libmacros[i].lmi_macroname);
680 * Set up the booted executable.
682 static struct module *
683 load_exec(val_t *bootaux, char *filename)
685 struct modctl *cp;
686 struct module *mp;
687 Dyn *dyn;
688 Sym *sp;
689 int i, lsize, osize, nsize, allocsize;
690 char *libname, *tmp;
691 char path[MAXPATHLEN];
693 #ifdef KOBJ_DEBUG
694 if (kobj_debug & D_DEBUG)
695 _kobj_printf(ops, "module path '%s'\n", kobj_module_path);
696 #endif
698 KOBJ_MARK("add_primary");
699 cp = add_primary(filename, KOBJ_LM_PRIMARY);
701 KOBJ_MARK("struct module");
702 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
703 cp->mod_mp = mp;
706 * We don't have the following information
707 * since this module is an executable and not
708 * a relocatable .o.
710 mp->symtbl_section = 0;
711 mp->shdrs = NULL;
712 mp->strhdr = NULL;
715 * Since this module is the only exception,
716 * we cons up some section headers.
718 KOBJ_MARK("symhdr");
719 mp->symhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT);
721 KOBJ_MARK("strhdr");
722 mp->strhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT);
724 mp->symhdr->sh_type = SHT_SYMTAB;
725 mp->strhdr->sh_type = SHT_STRTAB;
727 * Scan the dynamic structure.
729 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr;
730 dyn->d_tag != DT_NULL; dyn++) {
731 switch (dyn->d_tag) {
732 case DT_SYMTAB:
733 mp->symspace = mp->symtbl = (char *)dyn->d_un.d_ptr;
734 mp->symhdr->sh_addr = dyn->d_un.d_ptr;
735 break;
736 case DT_HASH:
737 mp->nsyms = *((uint_t *)dyn->d_un.d_ptr + 1);
738 mp->hashsize = *(uint_t *)dyn->d_un.d_ptr;
739 break;
740 case DT_STRTAB:
741 mp->strings = (char *)dyn->d_un.d_ptr;
742 mp->strhdr->sh_addr = dyn->d_un.d_ptr;
743 break;
744 case DT_STRSZ:
745 mp->strhdr->sh_size = dyn->d_un.d_val;
746 break;
747 case DT_SYMENT:
748 mp->symhdr->sh_entsize = dyn->d_un.d_val;
749 break;
754 * Collapse any DT_NEEDED entries into one string.
756 nsize = osize = 0;
757 allocsize = MAXPATHLEN;
759 KOBJ_MARK("depends_on");
760 mp->depends_on = kobj_alloc(allocsize, KM_WAIT);
762 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr;
763 dyn->d_tag != DT_NULL; dyn++)
764 if (dyn->d_tag == DT_NEEDED) {
765 char *_lib;
767 libname = mp->strings + dyn->d_un.d_val;
768 if (strchr(libname, '$') != NULL) {
769 if ((_lib = expand_libmacro(libname,
770 path, path)) != NULL)
771 libname = _lib;
772 else
773 _kobj_printf(ops, "krtld: "
774 "load_exec: fail to "
775 "expand %s\n", libname);
777 lsize = strlen(libname);
778 nsize += lsize;
779 if (nsize + 1 > allocsize) {
780 KOBJ_MARK("grow depends_on");
781 tmp = kobj_alloc(allocsize + MAXPATHLEN,
782 KM_WAIT);
783 bcopy(mp->depends_on, tmp, osize);
784 kobj_free(mp->depends_on, allocsize);
785 mp->depends_on = tmp;
786 allocsize += MAXPATHLEN;
788 bcopy(libname, mp->depends_on + osize, lsize);
789 *(mp->depends_on + nsize) = ' '; /* separate */
790 nsize++;
791 osize = nsize;
793 if (nsize) {
794 mp->depends_on[nsize - 1] = '\0'; /* terminate the string */
796 * alloc with exact size and copy whatever it got over
798 KOBJ_MARK("realloc depends_on");
799 tmp = kobj_alloc(nsize, KM_WAIT);
800 bcopy(mp->depends_on, tmp, nsize);
801 kobj_free(mp->depends_on, allocsize);
802 mp->depends_on = tmp;
803 } else {
804 kobj_free(mp->depends_on, allocsize);
805 mp->depends_on = NULL;
808 mp->flags = KOBJ_EXEC|KOBJ_PRIM; /* NOT a relocatable .o */
809 mp->symhdr->sh_size = mp->nsyms * mp->symhdr->sh_entsize;
811 * We allocate our own table since we don't
812 * hash undefined references.
814 KOBJ_MARK("chains");
815 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT);
816 KOBJ_MARK("buckets");
817 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT);
819 mp->text = _text;
820 mp->data = _data;
822 mp->text_size = _etext - _text;
823 mp->data_size = _edata - _data;
825 cp->mod_text = mp->text;
826 cp->mod_text_size = mp->text_size;
828 mp->filename = cp->mod_filename;
830 #ifdef KOBJ_DEBUG
831 if (kobj_debug & D_LOADING) {
832 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
833 _kobj_printf(ops, "\ttext: 0x%p", mp->text);
834 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
835 _kobj_printf(ops, "\tdata: 0x%p", mp->data);
836 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
838 #endif /* KOBJ_DEBUG */
841 * Insert symbols into the hash table.
843 for (i = 0; i < mp->nsyms; i++) {
844 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
846 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
847 continue;
849 sym_insert(mp, mp->strings + sp->st_name, i);
852 KOBJ_MARK("load_exec done");
853 return (mp);
857 * Set up the linker module (if it's compiled in, LDNAME is NULL)
859 static void
860 load_linker(val_t *bootaux)
862 struct module *kmp = (struct module *)kobj_modules->mod_mp;
863 struct module *mp;
864 struct modctl *cp;
865 int i;
866 Shdr *shp;
867 Sym *sp;
868 int shsize;
869 char *dlname = (char *)bootaux[BA_LDNAME].ba_ptr;
872 * On some architectures, krtld is compiled into the kernel.
874 if (dlname == NULL)
875 return;
877 cp = add_primary(dlname, KOBJ_LM_PRIMARY);
879 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
881 cp->mod_mp = mp;
882 mp->hdr = *(Ehdr *)bootaux[BA_LDELF].ba_ptr;
883 shsize = mp->hdr.e_shentsize * mp->hdr.e_shnum;
884 mp->shdrs = kobj_alloc(shsize, KM_WAIT);
885 bcopy(bootaux[BA_LDSHDR].ba_ptr, mp->shdrs, shsize);
887 for (i = 1; i < (int)mp->hdr.e_shnum; i++) {
888 shp = (Shdr *)(mp->shdrs + (i * mp->hdr.e_shentsize));
890 if (shp->sh_flags & SHF_ALLOC) {
891 if (shp->sh_flags & SHF_WRITE) {
892 if (mp->data == NULL)
893 mp->data = (char *)shp->sh_addr;
894 } else if (mp->text == NULL) {
895 mp->text = (char *)shp->sh_addr;
898 if (shp->sh_type == SHT_SYMTAB) {
899 mp->symtbl_section = i;
900 mp->symhdr = shp;
901 mp->symspace = mp->symtbl = (char *)shp->sh_addr;
904 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize;
905 mp->flags = KOBJ_INTERP|KOBJ_PRIM;
906 mp->strhdr = (Shdr *)
907 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize);
908 mp->strings = (char *)mp->strhdr->sh_addr;
909 mp->hashsize = kobj_gethashsize(mp->nsyms);
911 mp->symsize = mp->symhdr->sh_size + mp->strhdr->sh_size + sizeof (int) +
912 (mp->hashsize + mp->nsyms) * sizeof (symid_t);
914 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT);
915 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT);
917 mp->bss = bootaux[BA_BSS].ba_val;
918 mp->bss_align = 0; /* pre-aligned during allocation */
919 mp->bss_size = (uintptr_t)_edata - mp->bss;
920 mp->text_size = _etext - mp->text;
921 mp->data_size = _edata - mp->data;
922 mp->filename = cp->mod_filename;
923 cp->mod_text = mp->text;
924 cp->mod_text_size = mp->text_size;
927 * Now that we've figured out where the linker is,
928 * set the limits for the booted object.
930 kmp->text_size = (size_t)(mp->text - kmp->text);
931 kmp->data_size = (size_t)(mp->data - kmp->data);
932 kobj_modules->mod_text_size = kmp->text_size;
934 #ifdef KOBJ_DEBUG
935 if (kobj_debug & D_LOADING) {
936 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
937 _kobj_printf(ops, "\ttext:0x%p", mp->text);
938 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
939 _kobj_printf(ops, "\tdata:0x%p", mp->data);
940 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
942 #endif /* KOBJ_DEBUG */
945 * Insert the symbols into the hash table.
947 for (i = 0; i < mp->nsyms; i++) {
948 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
950 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
951 continue;
952 if (ELF_ST_BIND(sp->st_info) == STB_GLOBAL) {
953 if (sp->st_shndx == SHN_COMMON)
954 sp->st_shndx = SHN_ABS;
956 sym_insert(mp, mp->strings + sp->st_name, i);
961 static kobj_notify_list_t **
962 kobj_notify_lookup(uint_t type)
964 ASSERT(type != 0 && type < sizeof (kobj_notifiers) /
965 sizeof (kobj_notify_list_t *));
967 return (&kobj_notifiers[type]);
971 kobj_notify_add(kobj_notify_list_t *knp)
973 kobj_notify_list_t **knl;
975 knl = kobj_notify_lookup(knp->kn_type);
977 knp->kn_next = NULL;
978 knp->kn_prev = NULL;
980 mutex_enter(&kobj_lock);
982 if (*knl != NULL) {
983 (*knl)->kn_prev = knp;
984 knp->kn_next = *knl;
986 (*knl) = knp;
988 mutex_exit(&kobj_lock);
989 return (0);
993 kobj_notify_remove(kobj_notify_list_t *knp)
995 kobj_notify_list_t **knl = kobj_notify_lookup(knp->kn_type);
996 kobj_notify_list_t *tknp;
998 mutex_enter(&kobj_lock);
1000 /* LINTED */
1001 if (tknp = knp->kn_next)
1002 tknp->kn_prev = knp->kn_prev;
1004 /* LINTED */
1005 if (tknp = knp->kn_prev)
1006 tknp->kn_next = knp->kn_next;
1007 else
1008 *knl = knp->kn_next;
1010 mutex_exit(&kobj_lock);
1012 return (0);
1016 * Notify all interested callbacks of a specified change in module state.
1018 static void
1019 kobj_notify(int type, struct modctl *modp)
1021 kobj_notify_list_t *knp;
1023 if (modp->mod_loadflags & MOD_NONOTIFY || standalone)
1024 return;
1026 mutex_enter(&kobj_lock);
1028 for (knp = *(kobj_notify_lookup(type)); knp != NULL; knp = knp->kn_next)
1029 knp->kn_func(type, modp);
1032 * KDI notification must be last (it has to allow for work done by the
1033 * other notification callbacks), so we call it manually.
1035 kobj_kdi_mod_notify(type, modp);
1037 mutex_exit(&kobj_lock);
1041 * Create the module path.
1043 static char *
1044 getmodpath(const char *filename)
1046 char *path = kobj_zalloc(MAXPATHLEN, KM_WAIT);
1049 * Platform code gets first crack, then add
1050 * the default components
1052 mach_modpath(path, filename);
1053 if (*path != '\0')
1054 (void) strcat(path, " ");
1055 return (strcat(path, MOD_DEFPATH));
1058 static struct modctl *
1059 add_primary(const char *filename, int lmid)
1061 struct modctl *cp;
1063 cp = kobj_zalloc(sizeof (struct modctl), KM_WAIT);
1065 cp->mod_filename = kobj_alloc(strlen(filename) + 1, KM_WAIT);
1068 * For symbol lookup, we assemble our own
1069 * modctl list of the primary modules.
1072 (void) strcpy(cp->mod_filename, filename);
1073 cp->mod_modname = basename(cp->mod_filename);
1075 /* set values for modinfo assuming that the load will work */
1076 cp->mod_prim = 1;
1077 cp->mod_loaded = 1;
1078 cp->mod_installed = 1;
1079 cp->mod_loadcnt = 1;
1080 cp->mod_loadflags = MOD_NOAUTOUNLOAD;
1082 cp->mod_id = kobj_last_module_id++;
1085 * Link the module in. We'll pass this info on
1086 * to the mod squad later.
1088 if (kobj_modules == NULL) {
1089 kobj_modules = cp;
1090 cp->mod_prev = cp->mod_next = cp;
1091 } else {
1092 cp->mod_prev = kobj_modules->mod_prev;
1093 cp->mod_next = kobj_modules;
1094 kobj_modules->mod_prev->mod_next = cp;
1095 kobj_modules->mod_prev = cp;
1098 kobj_lm_append(lmid, cp);
1100 return (cp);
1103 static int
1104 bind_primary(val_t *bootaux, int lmid)
1106 struct modctl_list *linkmap = kobj_lm_lookup(lmid);
1107 struct modctl_list *lp;
1108 struct module *mp;
1111 * Do common symbols.
1113 for (lp = linkmap; lp; lp = lp->modl_next) {
1114 mp = mod(lp);
1117 * Don't do common section relocations for modules that
1118 * don't need it.
1120 if (mp->flags & (KOBJ_EXEC|KOBJ_INTERP))
1121 continue;
1123 if (do_common(mp) < 0)
1124 return (-1);
1128 * Resolve symbols.
1130 for (lp = linkmap; lp; lp = lp->modl_next) {
1131 mp = mod(lp);
1133 if (do_symbols(mp, 0) < 0)
1134 return (-1);
1138 * Do relocations.
1140 for (lp = linkmap; lp; lp = lp->modl_next) {
1141 mp = mod(lp);
1143 if (mp->flags & KOBJ_EXEC) {
1144 Dyn *dyn;
1145 Word relasz = 0, relaent = 0;
1146 Word shtype;
1147 char *rela = NULL;
1149 for (dyn = (Dyn *)bootaux[BA_DYNAMIC].ba_ptr;
1150 dyn->d_tag != DT_NULL; dyn++) {
1151 switch (dyn->d_tag) {
1152 case DT_RELASZ:
1153 case DT_RELSZ:
1154 relasz = dyn->d_un.d_val;
1155 break;
1156 case DT_RELAENT:
1157 case DT_RELENT:
1158 relaent = dyn->d_un.d_val;
1159 break;
1160 case DT_RELA:
1161 shtype = SHT_RELA;
1162 rela = (char *)dyn->d_un.d_ptr;
1163 break;
1164 case DT_REL:
1165 shtype = SHT_REL;
1166 rela = (char *)dyn->d_un.d_ptr;
1167 break;
1170 if (relasz == 0 ||
1171 relaent == 0 || rela == NULL) {
1172 _kobj_printf(ops, "krtld: bind_primary(): "
1173 "no relocation information found for "
1174 "module %s\n", mp->filename);
1175 return (-1);
1177 #ifdef KOBJ_DEBUG
1178 if (kobj_debug & D_RELOCATIONS)
1179 _kobj_printf(ops, "krtld: relocating: file=%s "
1180 "KOBJ_EXEC\n", mp->filename);
1181 #endif
1182 if (do_relocate(mp, rela, shtype, relasz/relaent,
1183 relaent, (Addr)mp->text) < 0)
1184 return (-1);
1185 } else {
1186 if (do_relocations(mp) < 0)
1187 return (-1);
1190 kobj_sync_instruction_memory(mp->text, mp->text_size);
1193 for (lp = linkmap; lp; lp = lp->modl_next) {
1194 mp = mod(lp);
1197 * We need to re-read the full symbol table for the boot file,
1198 * since we couldn't use the full one before. We also need to
1199 * load the CTF sections of both the boot file and the
1200 * interpreter (us).
1202 if (mp->flags & KOBJ_EXEC) {
1203 struct _buf *file;
1204 int n;
1206 file = kobj_open_file(mp->filename);
1207 if (file == (struct _buf *)-1)
1208 return (-1);
1209 if (kobj_read_file(file, (char *)&mp->hdr,
1210 sizeof (mp->hdr), 0) < 0)
1211 return (-1);
1212 n = mp->hdr.e_shentsize * mp->hdr.e_shnum;
1213 mp->shdrs = kobj_alloc(n, KM_WAIT);
1214 if (kobj_read_file(file, mp->shdrs, n,
1215 mp->hdr.e_shoff) < 0)
1216 return (-1);
1217 if (get_syms(mp, file) < 0)
1218 return (-1);
1219 if (get_ctf(mp, file) < 0)
1220 return (-1);
1221 kobj_close_file(file);
1222 mp->flags |= KOBJ_RELOCATED;
1224 } else if (mp->flags & KOBJ_INTERP) {
1225 struct _buf *file;
1227 file = kobj_open_path(mp->filename, 1);
1228 if (file == (struct _buf *)-1)
1229 return (-1);
1230 if (get_ctf(mp, file) < 0)
1231 return (-1);
1232 kobj_close_file(file);
1233 mp->flags |= KOBJ_RELOCATED;
1237 return (0);
1240 static struct modctl *
1241 mod_already_loaded(char *modname)
1243 struct modctl *mctl = kobj_modules;
1245 do {
1246 if (strcmp(modname, mctl->mod_filename) == 0)
1247 return (mctl);
1248 mctl = mctl->mod_next;
1250 } while (mctl != kobj_modules);
1252 return (NULL);
1256 * Load all the primary dependent modules.
1258 static int
1259 load_primary(struct module *mp, int lmid)
1261 struct modctl *cp;
1262 struct module *dmp;
1263 char *p, *q;
1264 char modname[MODMAXNAMELEN];
1266 if ((p = mp->depends_on) == NULL)
1267 return (0);
1269 /* CONSTANTCONDITION */
1270 while (1) {
1272 * Skip space.
1274 while (*p && (*p == ' ' || *p == '\t'))
1275 p++;
1277 * Get module name.
1279 q = modname;
1280 while (*p && *p != ' ' && *p != '\t')
1281 *q++ = *p++;
1283 if (q == modname)
1284 break;
1286 *q = '\0';
1288 * Check for dup dependencies.
1290 if (strcmp(modname, "dtracestubs") == 0 ||
1291 mod_already_loaded(modname) != NULL)
1292 continue;
1294 cp = add_primary(modname, lmid);
1295 cp->mod_busy = 1;
1297 * Load it.
1299 (void) kobj_load_module(cp, 1);
1300 cp->mod_busy = 0;
1302 if ((dmp = cp->mod_mp) == NULL) {
1303 cp->mod_loaded = 0;
1304 cp->mod_installed = 0;
1305 cp->mod_loadcnt = 0;
1306 return (-1);
1309 add_dependent(mp, dmp);
1310 dmp->flags |= KOBJ_PRIM;
1313 * Recurse.
1315 if (load_primary(dmp, lmid) == -1) {
1316 cp->mod_loaded = 0;
1317 cp->mod_installed = 0;
1318 cp->mod_loadcnt = 0;
1319 return (-1);
1322 return (0);
1325 static int
1326 console_is_usb_serial(void)
1328 char *console;
1329 int len, ret;
1331 if ((len = BOP_GETPROPLEN(ops, "console")) == -1)
1332 return (0);
1334 console = kobj_zalloc(len, KM_WAIT|KM_TMP);
1335 (void) BOP_GETPROP(ops, "console", console);
1336 ret = (strcmp(console, "usb-serial") == 0);
1337 kobj_free(console, len);
1339 return (ret);
1342 static int
1343 load_kmdb(val_t *bootaux)
1345 struct modctl *mctl;
1346 struct module *mp;
1347 Sym *sym;
1349 if (console_is_usb_serial()) {
1350 _kobj_printf(ops, "kmdb not loaded "
1351 "(unsupported on usb serial console)\n");
1352 return (0);
1355 _kobj_printf(ops, "Loading kmdb...\n");
1357 if ((mctl = add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER)) == NULL)
1358 return (-1);
1360 mctl->mod_busy = 1;
1361 (void) kobj_load_module(mctl, 1);
1362 mctl->mod_busy = 0;
1364 if ((mp = mctl->mod_mp) == NULL)
1365 return (-1);
1367 mp->flags |= KOBJ_PRIM;
1369 if (load_primary(mp, KOBJ_LM_DEBUGGER) < 0)
1370 return (-1);
1372 if (boothowto & RB_VERBOSE)
1373 kobj_lm_dump(KOBJ_LM_DEBUGGER);
1375 if (bind_primary(bootaux, KOBJ_LM_DEBUGGER) < 0)
1376 return (-1);
1378 if ((sym = lookup_one(mctl->mod_mp, "kctl_boot_activate")) == NULL)
1379 return (-1);
1381 #ifdef KOBJ_DEBUG
1382 if (kobj_debug & D_DEBUG) {
1383 _kobj_printf(ops, "calling kctl_boot_activate() @ 0x%lx\n",
1384 sym->st_value);
1385 _kobj_printf(ops, "\tops 0x%p\n", ops);
1386 _kobj_printf(ops, "\tromp 0x%p\n", romp);
1388 #endif
1390 if (((kctl_boot_activate_f *)sym->st_value)(ops, romp, 0,
1391 (const char **)kobj_kmdb_argv) < 0)
1392 return (-1);
1394 return (0);
1398 * Return a string listing module dependencies.
1400 static char *
1401 depends_on(struct module *mp)
1403 Sym *sp;
1404 char *depstr, *q;
1407 * The module doesn't have a depends_on value, so let's try it the
1408 * old-fashioned way - via "_depends_on"
1410 if ((sp = lookup_one(mp, "_depends_on")) == NULL)
1411 return (NULL);
1413 q = (char *)sp->st_value;
1415 #ifdef KOBJ_DEBUG
1417 * _depends_on is a deprecated interface, so we warn about its use
1418 * irrespective of subsequent processing errors. How else are we going
1419 * to be able to deco this interface completely?
1420 * Changes initially limited to DEBUG because third-party modules
1421 * should be flagged to developers before general use base.
1423 _kobj_printf(ops,
1424 "Warning: %s uses deprecated _depends_on interface.\n",
1425 mp->filename);
1426 _kobj_printf(ops, "Please notify module developer or vendor.\n");
1427 #endif
1430 * Idiot checks. Make sure it's
1431 * in-bounds and NULL terminated.
1433 if (kobj_addrcheck(mp, q) || q[sp->st_size - 1] != '\0') {
1434 _kobj_printf(ops, "Error processing dependency for %s\n",
1435 mp->filename);
1436 return (NULL);
1439 depstr = (char *)kobj_alloc(strlen(q) + 1, KM_WAIT);
1440 (void) strcpy(depstr, q);
1442 return (depstr);
1445 void
1446 kobj_getmodinfo(void *xmp, struct modinfo *modinfo)
1448 struct module *mp;
1449 mp = (struct module *)xmp;
1451 modinfo->mi_base = mp->text;
1452 modinfo->mi_size = mp->text_size + mp->data_size;
1456 * kobj_export_ksyms() performs the following services:
1458 * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena.
1459 * (2) Removes unneeded symbols to save space.
1460 * (3) Reduces memory footprint by using VM_BESTFIT allocations.
1461 * (4) Makes the symbol table visible to /dev/ksyms.
1463 static void
1464 kobj_export_ksyms(struct module *mp)
1466 Sym *esp = (Sym *)(mp->symtbl + mp->symhdr->sh_size);
1467 Sym *sp, *osp;
1468 char *name;
1469 size_t namelen;
1470 struct module *omp;
1471 uint_t nsyms;
1472 size_t symsize = mp->symhdr->sh_entsize;
1473 size_t locals = 1;
1474 size_t strsize;
1477 * Make a copy of the original module structure.
1479 omp = kobj_alloc(sizeof (struct module), KM_WAIT);
1480 bcopy(mp, omp, sizeof (struct module));
1483 * Compute the sizes of the new symbol table sections.
1485 for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) {
1486 if (osp->st_value == 0)
1487 continue;
1488 if (sym_lookup(omp, osp) == NULL)
1489 continue;
1490 name = omp->strings + osp->st_name;
1491 namelen = strlen(name);
1492 if (ELF_ST_BIND(osp->st_info) == STB_LOCAL)
1493 locals++;
1494 nsyms++;
1495 strsize += namelen + 1;
1498 mp->nsyms = nsyms;
1499 mp->hashsize = kobj_gethashsize(mp->nsyms);
1502 * ksyms_lock must be held as writer during any operation that
1503 * modifies ksyms_arena, including allocation from same, and
1504 * must not be dropped until the arena is vmem_walk()able.
1506 rw_enter(&ksyms_lock, RW_WRITER);
1509 * Allocate space for the new section headers (symtab and strtab),
1510 * symbol table, buckets, chains, and strings.
1512 mp->symsize = (2 * sizeof (Shdr)) + (nsyms * symsize) +
1513 (mp->hashsize + mp->nsyms) * sizeof (symid_t) + strsize;
1515 if (mp->flags & KOBJ_NOKSYMS) {
1516 mp->symspace = kobj_alloc(mp->symsize, KM_WAIT);
1517 } else {
1518 mp->symspace = vmem_alloc(ksyms_arena, mp->symsize,
1519 VM_BESTFIT | VM_SLEEP);
1521 bzero(mp->symspace, mp->symsize);
1524 * Divvy up symspace.
1526 mp->shdrs = mp->symspace;
1527 mp->symhdr = (Shdr *)mp->shdrs;
1528 mp->strhdr = (Shdr *)(mp->symhdr + 1);
1529 mp->symtbl = (char *)(mp->strhdr + 1);
1530 mp->buckets = (symid_t *)(mp->symtbl + (nsyms * symsize));
1531 mp->chains = (symid_t *)(mp->buckets + mp->hashsize);
1532 mp->strings = (char *)(mp->chains + nsyms);
1535 * Fill in the new section headers (symtab and strtab).
1537 mp->hdr.e_shnum = 2;
1538 mp->symtbl_section = 0;
1540 mp->symhdr->sh_type = SHT_SYMTAB;
1541 mp->symhdr->sh_addr = (Addr)mp->symtbl;
1542 mp->symhdr->sh_size = nsyms * symsize;
1543 mp->symhdr->sh_link = 1;
1544 mp->symhdr->sh_info = locals;
1545 mp->symhdr->sh_addralign = sizeof (Addr);
1546 mp->symhdr->sh_entsize = symsize;
1548 mp->strhdr->sh_type = SHT_STRTAB;
1549 mp->strhdr->sh_addr = (Addr)mp->strings;
1550 mp->strhdr->sh_size = strsize;
1551 mp->strhdr->sh_addralign = 1;
1554 * Construct the new symbol table.
1556 for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) {
1557 if (osp->st_value == 0)
1558 continue;
1559 if (sym_lookup(omp, osp) == NULL)
1560 continue;
1561 name = omp->strings + osp->st_name;
1562 namelen = strlen(name);
1563 sp = (Sym *)(mp->symtbl + symsize * nsyms);
1564 bcopy(osp, sp, symsize);
1565 bcopy(name, mp->strings + strsize, namelen);
1566 sp->st_name = strsize;
1567 sym_insert(mp, name, nsyms);
1568 nsyms++;
1569 strsize += namelen + 1;
1572 rw_exit(&ksyms_lock);
1575 * Free the old section headers -- we'll never need them again.
1577 if (!(mp->flags & KOBJ_PRIM)) {
1578 uint_t shn;
1579 Shdr *shp;
1581 for (shn = 1; shn < omp->hdr.e_shnum; shn++) {
1582 shp = (Shdr *)(omp->shdrs + shn * omp->hdr.e_shentsize);
1583 switch (shp->sh_type) {
1584 case SHT_RELA:
1585 case SHT_REL:
1586 if (shp->sh_addr != (uintptr_t)NULL) {
1587 kobj_free((void *)shp->sh_addr,
1588 shp->sh_size);
1590 break;
1593 kobj_free(omp->shdrs, omp->hdr.e_shentsize * omp->hdr.e_shnum);
1596 * Discard the old symbol table and our copy of the module strucure.
1598 if (!(mp->flags & KOBJ_PRIM))
1599 kobj_free(omp->symspace, omp->symsize);
1600 kobj_free(omp, sizeof (struct module));
1603 static void
1604 kobj_export_ctf(struct module *mp)
1606 char *data = mp->ctfdata;
1607 size_t size = mp->ctfsize;
1609 if (data != NULL) {
1610 if (_moddebug & MODDEBUG_NOCTF) {
1611 mp->ctfdata = NULL;
1612 mp->ctfsize = 0;
1613 } else {
1614 mp->ctfdata = vmem_alloc(ctf_arena, size,
1615 VM_BESTFIT | VM_SLEEP);
1616 bcopy(data, mp->ctfdata, size);
1619 if (!(mp->flags & KOBJ_PRIM))
1620 kobj_free(data, size);
1624 void
1625 kobj_export_module(struct module *mp)
1627 kobj_export_ksyms(mp);
1628 kobj_export_ctf(mp);
1630 mp->flags |= KOBJ_EXPORTED;
1633 static int
1634 process_dynamic(struct module *mp, char *dyndata, char *strdata)
1636 char *path = NULL, *depstr = NULL;
1637 int allocsize = 0, osize = 0, nsize = 0;
1638 char *libname, *tmp;
1639 int lsize;
1640 Dyn *dynp;
1642 for (dynp = (Dyn *)dyndata; dynp && dynp->d_tag != DT_NULL; dynp++) {
1643 switch (dynp->d_tag) {
1644 case DT_NEEDED:
1646 * Read the DT_NEEDED entries, expanding the macros they
1647 * contain (if any), and concatenating them into a
1648 * single space-separated dependency list.
1650 libname = (ulong_t)dynp->d_un.d_ptr + strdata;
1652 if (strchr(libname, '$') != NULL) {
1653 char *_lib;
1655 if (path == NULL)
1656 path = kobj_alloc(MAXPATHLEN, KM_WAIT);
1657 if ((_lib = expand_libmacro(libname, path,
1658 path)) != NULL)
1659 libname = _lib;
1660 else {
1661 _kobj_printf(ops, "krtld: "
1662 "process_dynamic: failed to expand "
1663 "%s\n", libname);
1667 lsize = strlen(libname);
1668 nsize += lsize;
1669 if (nsize + 1 > allocsize) {
1670 tmp = kobj_alloc(allocsize + MAXPATHLEN,
1671 KM_WAIT);
1672 if (depstr != NULL) {
1673 bcopy(depstr, tmp, osize);
1674 kobj_free(depstr, allocsize);
1676 depstr = tmp;
1677 allocsize += MAXPATHLEN;
1679 bcopy(libname, depstr + osize, lsize);
1680 *(depstr + nsize) = ' '; /* separator */
1681 nsize++;
1682 osize = nsize;
1683 break;
1685 case DT_FLAGS_1:
1686 if (dynp->d_un.d_val & DF_1_IGNMULDEF)
1687 mp->flags |= KOBJ_IGNMULDEF;
1688 if (dynp->d_un.d_val & DF_1_NOKSYMS)
1689 mp->flags |= KOBJ_NOKSYMS;
1691 break;
1696 * finish up the depends string (if any)
1698 if (depstr != NULL) {
1699 *(depstr + nsize - 1) = '\0'; /* overwrite separator w/term */
1700 if (path != NULL)
1701 kobj_free(path, MAXPATHLEN);
1703 tmp = kobj_alloc(nsize, KM_WAIT);
1704 bcopy(depstr, tmp, nsize);
1705 kobj_free(depstr, allocsize);
1706 depstr = tmp;
1708 mp->depends_on = depstr;
1711 return (0);
1714 static int
1715 do_dynamic(struct module *mp, struct _buf *file)
1717 Shdr *dshp, *dstrp, *shp;
1718 char *dyndata, *dstrdata;
1719 int dshn, shn, rc;
1721 /* find and validate the dynamic section (if any) */
1723 for (dshp = NULL, shn = 1; shn < mp->hdr.e_shnum; shn++) {
1724 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
1725 switch (shp->sh_type) {
1726 case SHT_DYNAMIC:
1727 if (dshp != NULL) {
1728 _kobj_printf(ops, "krtld: get_dynamic: %s, ",
1729 mp->filename);
1730 _kobj_printf(ops,
1731 "multiple dynamic sections\n");
1732 return (-1);
1733 } else {
1734 dshp = shp;
1735 dshn = shn;
1737 break;
1741 if (dshp == NULL)
1742 return (0);
1744 if (dshp->sh_link > mp->hdr.e_shnum) {
1745 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1746 _kobj_printf(ops, "no section for sh_link %d\n", dshp->sh_link);
1747 return (-1);
1749 dstrp = (Shdr *)(mp->shdrs + dshp->sh_link * mp->hdr.e_shentsize);
1751 if (dstrp->sh_type != SHT_STRTAB) {
1752 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1753 _kobj_printf(ops, "sh_link not a string table for section %d\n",
1754 dshn);
1755 return (-1);
1758 /* read it from disk */
1760 dyndata = kobj_alloc(dshp->sh_size, KM_WAIT|KM_TMP);
1761 if (kobj_read_file(file, dyndata, dshp->sh_size, dshp->sh_offset) < 0) {
1762 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1763 _kobj_printf(ops, "error reading section %d\n", dshn);
1765 kobj_free(dyndata, dshp->sh_size);
1766 return (-1);
1769 dstrdata = kobj_alloc(dstrp->sh_size, KM_WAIT|KM_TMP);
1770 if (kobj_read_file(file, dstrdata, dstrp->sh_size,
1771 dstrp->sh_offset) < 0) {
1772 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1773 _kobj_printf(ops, "error reading section %d\n", dshp->sh_link);
1775 kobj_free(dyndata, dshp->sh_size);
1776 kobj_free(dstrdata, dstrp->sh_size);
1777 return (-1);
1780 /* pull the interesting pieces out */
1782 rc = process_dynamic(mp, dyndata, dstrdata);
1784 kobj_free(dyndata, dshp->sh_size);
1785 kobj_free(dstrdata, dstrp->sh_size);
1787 return (rc);
1790 void
1791 kobj_set_ctf(struct module *mp, caddr_t data, size_t size)
1793 if (!standalone) {
1794 if (mp->ctfdata != NULL) {
1795 if (vmem_contains(ctf_arena, mp->ctfdata,
1796 mp->ctfsize)) {
1797 vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize);
1798 } else {
1799 kobj_free(mp->ctfdata, mp->ctfsize);
1805 * The order is very important here. We need to make sure that
1806 * consumers, at any given instant, see a consistent state. We'd
1807 * rather they see no CTF data than the address of one buffer and the
1808 * size of another.
1810 mp->ctfdata = NULL;
1811 membar_producer();
1812 mp->ctfsize = size;
1813 mp->ctfdata = data;
1814 membar_producer();
1818 kobj_load_module(struct modctl *modp, int use_path)
1820 char *filename = modp->mod_filename;
1821 char *modname = modp->mod_modname;
1822 int i;
1823 int n;
1824 struct _buf *file;
1825 struct module *mp = NULL;
1827 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
1830 * We need to prevent kmdb's symbols from leaking into /dev/ksyms.
1831 * kmdb contains a bunch of symbols with well-known names, symbols
1832 * which will mask the real versions, thus causing no end of trouble
1833 * for mdb.
1835 if (strcmp(modp->mod_modname, "kmdbmod") == 0)
1836 mp->flags |= KOBJ_NOKSYMS;
1838 file = kobj_open_path(filename, use_path);
1839 if (file == (struct _buf *)-1) {
1840 if (file == (struct _buf *)-1) {
1841 kobj_free(mp, sizeof (*mp));
1842 goto bad;
1846 mp->filename = kobj_alloc(strlen(file->_name) + 1, KM_WAIT);
1847 (void) strcpy(mp->filename, file->_name);
1849 if (kobj_read_file(file, (char *)&mp->hdr, sizeof (mp->hdr), 0) < 0) {
1850 _kobj_printf(ops, "kobj_load_module: %s read header failed\n",
1851 modname);
1852 kobj_free(mp->filename, strlen(file->_name) + 1);
1853 kobj_free(mp, sizeof (*mp));
1854 goto bad;
1856 for (i = 0; i < SELFMAG; i++) {
1857 if (mp->hdr.e_ident[i] != ELFMAG[i]) {
1858 if (_moddebug & MODDEBUG_ERRMSG)
1859 _kobj_printf(ops, "%s not an elf module\n",
1860 modname);
1861 kobj_free(mp->filename, strlen(file->_name) + 1);
1862 kobj_free(mp, sizeof (*mp));
1863 goto bad;
1867 * It's ELF, but is it our ISA? Interpreting the header
1868 * from a file for a byte-swapped ISA could cause a huge
1869 * and unsatisfiable value to be passed to kobj_alloc below
1870 * and therefore hang booting.
1872 if (!elf_mach_ok(&mp->hdr)) {
1873 if (_moddebug & MODDEBUG_ERRMSG)
1874 _kobj_printf(ops, "%s not an elf module for this ISA\n",
1875 modname);
1876 kobj_free(mp->filename, strlen(file->_name) + 1);
1877 kobj_free(mp, sizeof (*mp));
1878 goto bad;
1882 * All modules, save for unix, should be relocatable (as opposed to
1883 * dynamic). Dynamic modules come with PLTs and GOTs, which can't
1884 * currently be processed by krtld.
1886 if (mp->hdr.e_type != ET_REL) {
1887 if (_moddebug & MODDEBUG_ERRMSG)
1888 _kobj_printf(ops, "%s isn't a relocatable (ET_REL) "
1889 "module\n", modname);
1890 kobj_free(mp->filename, strlen(file->_name) + 1);
1891 kobj_free(mp, sizeof (*mp));
1892 goto bad;
1895 n = mp->hdr.e_shentsize * mp->hdr.e_shnum;
1896 mp->shdrs = kobj_alloc(n, KM_WAIT);
1898 if (kobj_read_file(file, mp->shdrs, n, mp->hdr.e_shoff) < 0) {
1899 _kobj_printf(ops, "kobj_load_module: %s error reading "
1900 "section headers\n", modname);
1901 kobj_free(mp->shdrs, n);
1902 kobj_free(mp->filename, strlen(file->_name) + 1);
1903 kobj_free(mp, sizeof (*mp));
1904 goto bad;
1907 kobj_notify(KOBJ_NOTIFY_MODLOADING, modp);
1908 module_assign(modp, mp);
1910 /* read in sections */
1911 if (get_progbits(mp, file) < 0) {
1912 _kobj_printf(ops, "%s error reading sections\n", modname);
1913 goto bad;
1916 if (do_dynamic(mp, file) < 0) {
1917 _kobj_printf(ops, "%s error reading dynamic section\n",
1918 modname);
1919 goto bad;
1922 modp->mod_text = mp->text;
1923 modp->mod_text_size = mp->text_size;
1925 /* read in symbols; adjust values for each section's real address */
1926 if (get_syms(mp, file) < 0) {
1927 _kobj_printf(ops, "%s error reading symbols\n",
1928 modname);
1929 goto bad;
1933 * If we didn't dependency information from the dynamic section, look
1934 * for it the old-fashioned way.
1936 if (mp->depends_on == NULL)
1937 mp->depends_on = depends_on(mp);
1939 if (get_ctf(mp, file) < 0) {
1940 _kobj_printf(ops, "%s debug information will not "
1941 "be available\n", modname);
1944 /* primary kernel modules do not have a signature section */
1945 if (!(mp->flags & KOBJ_PRIM))
1946 get_signature(mp, file);
1948 #ifdef KOBJ_DEBUG
1949 if (kobj_debug & D_LOADING) {
1950 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
1951 _kobj_printf(ops, "\ttext:0x%p", mp->text);
1952 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
1953 _kobj_printf(ops, "\tdata:0x%p", mp->data);
1954 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
1956 #endif /* KOBJ_DEBUG */
1959 * For primary kernel modules, we defer
1960 * symbol resolution and relocation until
1961 * all primary objects have been loaded.
1963 if (!standalone) {
1964 int ddrval, dcrval;
1965 char *dependent_modname;
1966 /* load all dependents */
1967 dependent_modname = kobj_zalloc(MODMAXNAMELEN, KM_WAIT);
1968 ddrval = do_dependents(modp, dependent_modname, MODMAXNAMELEN);
1971 * resolve undefined and common symbols,
1972 * also allocates common space
1974 if ((dcrval = do_common(mp)) < 0) {
1975 switch (dcrval) {
1976 case DOSYM_UNSAFE:
1977 _kobj_printf(ops, "WARNING: mod_load: "
1978 "MT-unsafe module '%s' rejected\n",
1979 modname);
1980 break;
1981 case DOSYM_UNDEF:
1982 _kobj_printf(ops, "WARNING: mod_load: "
1983 "cannot load module '%s'\n",
1984 modname);
1985 if (ddrval == -1) {
1986 _kobj_printf(ops, "WARNING: %s: ",
1987 modname);
1988 _kobj_printf(ops,
1989 "unable to resolve dependency, "
1990 "module '%s' not found\n",
1991 dependent_modname);
1993 break;
1996 kobj_free(dependent_modname, MODMAXNAMELEN);
1997 if (dcrval < 0)
1998 goto bad;
2000 /* process relocation tables */
2001 if (do_relocations(mp) < 0) {
2002 _kobj_printf(ops, "%s error doing relocations\n",
2003 modname);
2004 goto bad;
2007 if (mp->destination) {
2008 off_t off = (uintptr_t)mp->destination & PAGEOFFSET;
2009 caddr_t base = (caddr_t)mp->destination - off;
2010 size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE);
2012 hat_unload(kas.a_hat, base, size, HAT_UNLOAD_UNLOCK);
2013 vmem_free(heap_arena, base, size);
2016 /* sync_instruction_memory */
2017 kobj_sync_instruction_memory(mp->text, mp->text_size);
2018 kobj_export_module(mp);
2019 kobj_notify(KOBJ_NOTIFY_MODLOADED, modp);
2021 kobj_close_file(file);
2022 return (0);
2023 bad:
2024 if (file != (struct _buf *)-1)
2025 kobj_close_file(file);
2026 if (modp->mod_mp != NULL)
2027 free_module_data(modp->mod_mp);
2029 module_assign(modp, NULL);
2030 return ((file == (struct _buf *)-1) ? ENOENT : EINVAL);
2034 kobj_load_primary_module(struct modctl *modp)
2036 struct modctl *dep;
2037 struct module *mp;
2039 if (kobj_load_module(modp, 0) != 0)
2040 return (-1);
2042 mp = modp->mod_mp;
2043 mp->flags |= KOBJ_PRIM;
2045 /* Bind new module to its dependents */
2046 if (mp->depends_on != NULL && (dep =
2047 mod_already_loaded(mp->depends_on)) == NULL) {
2048 #ifdef KOBJ_DEBUG
2049 if (kobj_debug & D_DEBUG) {
2050 _kobj_printf(ops, "krtld: failed to resolve deps "
2051 "for primary %s\n", modp->mod_modname);
2053 #endif
2054 return (-1);
2057 add_dependent(mp, dep->mod_mp);
2060 * Relocate it. This module may not be part of a link map, so we
2061 * can't use bind_primary.
2063 if (do_common(mp) < 0 || do_symbols(mp, 0) < 0 ||
2064 do_relocations(mp) < 0) {
2065 #ifdef KOBJ_DEBUG
2066 if (kobj_debug & D_DEBUG) {
2067 _kobj_printf(ops, "krtld: failed to relocate "
2068 "primary %s\n", modp->mod_modname);
2070 #endif
2071 return (-1);
2074 return (0);
2077 static void
2078 module_assign(struct modctl *cp, struct module *mp)
2080 if (standalone) {
2081 cp->mod_mp = mp;
2082 return;
2084 mutex_enter(&mod_lock);
2085 cp->mod_mp = mp;
2086 cp->mod_gencount++;
2087 mutex_exit(&mod_lock);
2090 void
2091 kobj_unload_module(struct modctl *modp)
2093 struct module *mp = modp->mod_mp;
2095 if ((_moddebug & MODDEBUG_KEEPTEXT) && mp) {
2096 _kobj_printf(ops, "text for %s ", mp->filename);
2097 _kobj_printf(ops, "was at %p\n", mp->text);
2098 mp->text = NULL; /* don't actually free it */
2101 kobj_notify(KOBJ_NOTIFY_MODUNLOADING, modp);
2104 * Null out mod_mp first, so consumers (debuggers) know not to look
2105 * at the module structure any more.
2107 mutex_enter(&mod_lock);
2108 modp->mod_mp = NULL;
2109 mutex_exit(&mod_lock);
2111 kobj_notify(KOBJ_NOTIFY_MODUNLOADED, modp);
2112 free_module_data(mp);
2115 static void
2116 free_module_data(struct module *mp)
2118 struct module_list *lp, *tmp;
2119 int ksyms_exported = 0;
2121 lp = mp->head;
2122 while (lp) {
2123 tmp = lp;
2124 lp = lp->next;
2125 kobj_free((char *)tmp, sizeof (*tmp));
2128 rw_enter(&ksyms_lock, RW_WRITER);
2129 if (mp->symspace) {
2130 if (vmem_contains(ksyms_arena, mp->symspace, mp->symsize)) {
2131 vmem_free(ksyms_arena, mp->symspace, mp->symsize);
2132 ksyms_exported = 1;
2133 } else {
2134 if (mp->flags & KOBJ_NOKSYMS)
2135 ksyms_exported = 1;
2136 kobj_free(mp->symspace, mp->symsize);
2139 rw_exit(&ksyms_lock);
2141 if (mp->ctfdata) {
2142 if (vmem_contains(ctf_arena, mp->ctfdata, mp->ctfsize))
2143 vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize);
2144 else
2145 kobj_free(mp->ctfdata, mp->ctfsize);
2148 if (mp->sigdata)
2149 kobj_free(mp->sigdata, mp->sigsize);
2152 * We did not get far enough into kobj_export_ksyms() to free allocated
2153 * buffers because we encounted error conditions. Free the buffers.
2155 if ((ksyms_exported == 0) && (mp->shdrs != NULL)) {
2156 uint_t shn;
2157 Shdr *shp;
2159 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2160 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2161 switch (shp->sh_type) {
2162 case SHT_RELA:
2163 case SHT_REL:
2164 if (shp->sh_addr != 0)
2165 kobj_free((void *)shp->sh_addr,
2166 shp->sh_size);
2167 break;
2170 err_free_done:
2171 if (!(mp->flags & KOBJ_PRIM)) {
2172 kobj_free(mp->shdrs,
2173 mp->hdr.e_shentsize * mp->hdr.e_shnum);
2177 if (mp->bss)
2178 vmem_free(data_arena, (void *)mp->bss, mp->bss_size);
2180 if (mp->fbt_tab)
2181 kobj_texthole_free(mp->fbt_tab, mp->fbt_size);
2183 if (mp->textwin_base)
2184 kobj_textwin_free(mp);
2186 if (mp->sdt_probes != NULL) {
2187 sdt_probedesc_t *sdp = mp->sdt_probes, *next;
2189 while (sdp != NULL) {
2190 next = sdp->sdpd_next;
2191 kobj_free(sdp->sdpd_name, strlen(sdp->sdpd_name) + 1);
2192 kobj_free(sdp, sizeof (sdt_probedesc_t));
2193 sdp = next;
2197 if (mp->sdt_tab)
2198 kobj_texthole_free(mp->sdt_tab, mp->sdt_size);
2199 if (mp->text)
2200 vmem_free(text_arena, mp->text, mp->text_size);
2201 if (mp->data)
2202 vmem_free(data_arena, mp->data, mp->data_size);
2203 if (mp->depends_on)
2204 kobj_free(mp->depends_on, strlen(mp->depends_on)+1);
2205 if (mp->filename)
2206 kobj_free(mp->filename, strlen(mp->filename)+1);
2208 kobj_free((char *)mp, sizeof (*mp));
2211 static int
2212 get_progbits(struct module *mp, struct _buf *file)
2214 struct proginfo *tp, *dp, *sdp;
2215 Shdr *shp;
2216 reloc_dest_t dest = NULL;
2217 uintptr_t bits_ptr;
2218 uintptr_t text = 0, data, textptr;
2219 uint_t shn;
2220 int err = -1;
2222 tp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2223 dp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2224 sdp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2226 * loop through sections to find out how much space we need
2227 * for text, data, (also bss that is already assigned)
2229 if (get_progbits_size(mp, tp, dp, sdp) < 0)
2230 goto done;
2232 mp->text_size = tp->size;
2233 mp->data_size = dp->size;
2235 if (standalone) {
2236 caddr_t limit = _data;
2238 if (lg_pagesize && _text + lg_pagesize < limit)
2239 limit = _text + lg_pagesize;
2241 mp->text = kobj_segbrk(&_etext, mp->text_size,
2242 tp->align, limit);
2244 * If we can't grow the text segment, try the
2245 * data segment before failing.
2247 if (mp->text == NULL) {
2248 mp->text = kobj_segbrk(&_edata, mp->text_size,
2249 tp->align, 0);
2252 mp->data = kobj_segbrk(&_edata, mp->data_size, dp->align, 0);
2254 if (mp->text == NULL || mp->data == NULL)
2255 goto done;
2257 } else {
2258 if (text_arena == NULL)
2259 kobj_vmem_init(&text_arena, &data_arena);
2262 * some architectures may want to load the module on a
2263 * page that is currently read only. It may not be
2264 * possible for those architectures to remap their page
2265 * on the fly. So we provide a facility for them to hang
2266 * a private hook where the memory they assign the module
2267 * is not the actual place where the module loads.
2269 * In this case there are two addresses that deal with the
2270 * modload.
2271 * 1) the final destination of the module
2272 * 2) the address that is used to view the newly
2273 * loaded module until all the relocations relative to 1
2274 * above are completed.
2276 * That is what dest is used for below.
2278 mp->text_size += tp->align;
2279 mp->data_size += dp->align;
2281 mp->text = kobj_text_alloc(text_arena, mp->text_size);
2284 * a remap is taking place. Align the text ptr relative
2285 * to the secondary mapping. That is where the bits will
2286 * be read in.
2288 if (kvseg.s_base != NULL && !vmem_contains(heaptext_arena,
2289 mp->text, mp->text_size)) {
2290 off_t off = (uintptr_t)mp->text & PAGEOFFSET;
2291 size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE);
2292 caddr_t map = vmem_alloc(heap_arena, size, VM_SLEEP);
2293 caddr_t orig = mp->text - off;
2294 pgcnt_t pages = size / PAGESIZE;
2296 dest = (reloc_dest_t)(map + off);
2297 text = ALIGN((uintptr_t)dest, tp->align);
2299 while (pages--) {
2300 hat_devload(kas.a_hat, map, PAGESIZE,
2301 hat_getpfnum(kas.a_hat, orig),
2302 PROT_READ | PROT_WRITE | PROT_EXEC,
2303 HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
2304 map += PAGESIZE;
2305 orig += PAGESIZE;
2308 * Since we set up a non-cacheable mapping, we need
2309 * to flush any old entries in the cache that might
2310 * be left around from the read-only mapping.
2312 dcache_flushall();
2314 if (mp->data_size)
2315 mp->data = vmem_alloc(data_arena, mp->data_size,
2316 VM_SLEEP | VM_BESTFIT);
2318 textptr = (uintptr_t)mp->text;
2319 textptr = ALIGN(textptr, tp->align);
2320 mp->destination = dest;
2323 * This is the case where a remap is not being done.
2325 if (text == 0)
2326 text = ALIGN((uintptr_t)mp->text, tp->align);
2327 data = ALIGN((uintptr_t)mp->data, dp->align);
2329 /* now loop though sections assigning addresses and loading the data */
2330 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2331 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2332 if (!(shp->sh_flags & SHF_ALLOC))
2333 continue;
2335 if ((shp->sh_flags & SHF_WRITE) == 0)
2336 bits_ptr = text;
2337 else
2338 bits_ptr = data;
2340 bits_ptr = ALIGN(bits_ptr, shp->sh_addralign);
2342 if (shp->sh_type == SHT_NOBITS) {
2344 * Zero bss.
2346 bzero((caddr_t)bits_ptr, shp->sh_size);
2347 shp->sh_type = SHT_PROGBITS;
2348 } else {
2349 if (kobj_read_file(file, (char *)bits_ptr,
2350 shp->sh_size, shp->sh_offset) < 0)
2351 goto done;
2354 if (shp->sh_flags & SHF_WRITE) {
2355 shp->sh_addr = bits_ptr;
2356 } else {
2357 textptr = ALIGN(textptr, shp->sh_addralign);
2358 shp->sh_addr = textptr;
2359 textptr += shp->sh_size;
2362 bits_ptr += shp->sh_size;
2363 if ((shp->sh_flags & SHF_WRITE) == 0)
2364 text = bits_ptr;
2365 else
2366 data = bits_ptr;
2369 err = 0;
2370 done:
2372 * Free and mark as freed the section headers here so that
2373 * free_module_data() does not have to worry about this buffer.
2375 * This buffer is freed here because one of the possible reasons
2376 * for error is a section with non-zero sh_addr and in that case
2377 * free_module_data() would have no way of recognizing that this
2378 * buffer was unallocated.
2380 if (err != 0) {
2381 kobj_free(mp->shdrs, mp->hdr.e_shentsize * mp->hdr.e_shnum);
2382 mp->shdrs = NULL;
2385 (void) kobj_free(tp, sizeof (struct proginfo));
2386 (void) kobj_free(dp, sizeof (struct proginfo));
2387 (void) kobj_free(sdp, sizeof (struct proginfo));
2389 return (err);
2393 * Go through suppress_sym_list to see if "multiply defined"
2394 * warning of this symbol should be suppressed. Return 1 if
2395 * warning should be suppressed, 0 otherwise.
2397 static int
2398 kobj_suppress_warning(char *symname)
2400 int i;
2402 for (i = 0; suppress_sym_list[i] != NULL; i++) {
2403 if (strcmp(suppress_sym_list[i], symname) == 0)
2404 return (1);
2407 return (0);
2410 static int
2411 get_syms(struct module *mp, struct _buf *file)
2413 uint_t shn;
2414 Shdr *shp;
2415 uint_t i;
2416 Sym *sp, *ksp;
2417 char *symname;
2418 int dosymtab = 0;
2421 * Find the interesting sections.
2423 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2424 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2425 switch (shp->sh_type) {
2426 case SHT_SYMTAB:
2427 mp->symtbl_section = shn;
2428 mp->symhdr = shp;
2429 dosymtab++;
2430 break;
2432 case SHT_RELA:
2433 case SHT_REL:
2435 * Already loaded.
2437 if (shp->sh_addr)
2438 continue;
2440 /* KM_TMP since kobj_free'd in do_relocations */
2441 shp->sh_addr = (Addr)
2442 kobj_alloc(shp->sh_size, KM_WAIT|KM_TMP);
2444 if (kobj_read_file(file, (char *)shp->sh_addr,
2445 shp->sh_size, shp->sh_offset) < 0) {
2446 _kobj_printf(ops, "krtld: get_syms: %s, ",
2447 mp->filename);
2448 _kobj_printf(ops, "error reading section %d\n",
2449 shn);
2450 return (-1);
2452 break;
2457 * This is true for a stripped executable. In the case of
2458 * 'unix' it can be stripped but it still contains the SHT_DYNSYM,
2459 * and since that symbol information is still present everything
2460 * is just fine.
2462 if (!dosymtab) {
2463 if (mp->flags & KOBJ_EXEC)
2464 return (0);
2465 _kobj_printf(ops, "krtld: get_syms: %s ",
2466 mp->filename);
2467 _kobj_printf(ops, "no SHT_SYMTAB symbol table found\n");
2468 return (-1);
2472 * get the associated string table header
2474 if ((mp->symhdr == 0) || (mp->symhdr->sh_link >= mp->hdr.e_shnum))
2475 return (-1);
2476 mp->strhdr = (Shdr *)
2477 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize);
2479 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize;
2480 mp->hashsize = kobj_gethashsize(mp->nsyms);
2483 * Allocate space for the symbol table, buckets, chains, and strings.
2485 mp->symsize = mp->symhdr->sh_size +
2486 (mp->hashsize + mp->nsyms) * sizeof (symid_t) + mp->strhdr->sh_size;
2487 mp->symspace = kobj_zalloc(mp->symsize, KM_WAIT|KM_SCRATCH);
2489 mp->symtbl = mp->symspace;
2490 mp->buckets = (symid_t *)(mp->symtbl + mp->symhdr->sh_size);
2491 mp->chains = mp->buckets + mp->hashsize;
2492 mp->strings = (char *)(mp->chains + mp->nsyms);
2494 if (kobj_read_file(file, mp->symtbl,
2495 mp->symhdr->sh_size, mp->symhdr->sh_offset) < 0 ||
2496 kobj_read_file(file, mp->strings,
2497 mp->strhdr->sh_size, mp->strhdr->sh_offset) < 0)
2498 return (-1);
2501 * loop through the symbol table adjusting values to account
2502 * for where each section got loaded into memory. Also
2503 * fill in the hash table.
2505 for (i = 1; i < mp->nsyms; i++) {
2506 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
2507 if (sp->st_shndx < SHN_LORESERVE) {
2508 if (sp->st_shndx >= mp->hdr.e_shnum) {
2509 _kobj_printf(ops, "%s bad shndx ",
2510 file->_name);
2511 _kobj_printf(ops, "in symbol %d\n", i);
2512 return (-1);
2514 shp = (Shdr *)
2515 (mp->shdrs +
2516 sp->st_shndx * mp->hdr.e_shentsize);
2517 if (!(mp->flags & KOBJ_EXEC))
2518 sp->st_value += shp->sh_addr;
2521 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
2522 continue;
2523 if (sp->st_name >= mp->strhdr->sh_size)
2524 return (-1);
2526 symname = mp->strings + sp->st_name;
2528 if (!(mp->flags & KOBJ_EXEC) &&
2529 ELF_ST_BIND(sp->st_info) == STB_GLOBAL) {
2530 ksp = kobj_lookup_all(mp, symname, 0);
2532 if (ksp && ELF_ST_BIND(ksp->st_info) == STB_GLOBAL &&
2533 !kobj_suppress_warning(symname) &&
2534 sp->st_shndx != SHN_UNDEF &&
2535 sp->st_shndx != SHN_COMMON &&
2536 ksp->st_shndx != SHN_UNDEF &&
2537 ksp->st_shndx != SHN_COMMON) {
2539 * Unless this symbol is a stub, it's multiply
2540 * defined. Multiply-defined symbols are
2541 * usually bad, but some objects (kmdb) have
2542 * a legitimate need to have their own
2543 * copies of common functions.
2545 if ((standalone ||
2546 ksp->st_value < (uintptr_t)stubs_base ||
2547 ksp->st_value >= (uintptr_t)stubs_end) &&
2548 !(mp->flags & KOBJ_IGNMULDEF)) {
2549 _kobj_printf(ops,
2550 "%s symbol ", file->_name);
2551 _kobj_printf(ops,
2552 "%s multiply defined\n", symname);
2557 sym_insert(mp, symname, i);
2560 return (0);
2563 static int
2564 get_ctf(struct module *mp, struct _buf *file)
2566 char *shstrtab, *ctfdata;
2567 size_t shstrlen;
2568 Shdr *shp;
2569 uint_t i;
2571 if (_moddebug & MODDEBUG_NOCTF)
2572 return (0); /* do not attempt to even load CTF data */
2574 if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) {
2575 _kobj_printf(ops, "krtld: get_ctf: %s, ",
2576 mp->filename);
2577 _kobj_printf(ops, "corrupt e_shstrndx %u\n",
2578 mp->hdr.e_shstrndx);
2579 return (-1);
2582 shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize);
2583 shstrlen = shp->sh_size;
2584 shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP);
2586 if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) {
2587 _kobj_printf(ops, "krtld: get_ctf: %s, ",
2588 mp->filename);
2589 _kobj_printf(ops, "error reading section %u\n",
2590 mp->hdr.e_shstrndx);
2591 kobj_free(shstrtab, shstrlen);
2592 return (-1);
2595 for (i = 0; i < mp->hdr.e_shnum; i++) {
2596 shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize);
2598 if (shp->sh_size != 0 && shp->sh_name < shstrlen &&
2599 strcmp(shstrtab + shp->sh_name, ".SUNW_ctf") == 0) {
2600 ctfdata = kobj_alloc(shp->sh_size, KM_WAIT|KM_SCRATCH);
2602 if (kobj_read_file(file, ctfdata, shp->sh_size,
2603 shp->sh_offset) < 0) {
2604 _kobj_printf(ops, "krtld: get_ctf: %s, error "
2605 "reading .SUNW_ctf data\n", mp->filename);
2606 kobj_free(ctfdata, shp->sh_size);
2607 kobj_free(shstrtab, shstrlen);
2608 return (-1);
2611 mp->ctfdata = ctfdata;
2612 mp->ctfsize = shp->sh_size;
2613 break;
2617 kobj_free(shstrtab, shstrlen);
2618 return (0);
2621 #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */
2624 * Return the hash of the ELF sections that are memory resident.
2625 * i.e. text and data. We skip a SHT_NOBITS section since it occupies
2626 * no space in the file. We use SHA1 here since libelfsign uses
2627 * it and both places need to use the same algorithm.
2629 static void
2630 crypto_es_hash(struct module *mp, char *hash, char *shstrtab)
2632 uint_t shn;
2633 Shdr *shp;
2634 SHA1_CTX ctx;
2636 SHA1Init(&ctx);
2638 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2639 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2640 if (!(shp->sh_flags & SHF_ALLOC) || shp->sh_size == 0)
2641 continue;
2644 * The check should ideally be shp->sh_type == SHT_NOBITS.
2645 * However, we can't do that check here as get_progbits()
2646 * resets the type.
2648 if (strcmp(shstrtab + shp->sh_name, ".bss") == 0)
2649 continue;
2650 #ifdef KOBJ_DEBUG
2651 if (kobj_debug & D_DEBUG)
2652 _kobj_printf(ops,
2653 "krtld: crypto_es_hash: updating hash with"
2654 " %s data size=%d\n", shstrtab + shp->sh_name,
2655 shp->sh_size);
2656 #endif
2657 ASSERT(shp->sh_addr != (uintptr_t)NULL);
2658 SHA1Update(&ctx, (const uint8_t *)shp->sh_addr, shp->sh_size);
2661 SHA1Final((uchar_t *)hash, &ctx);
2665 * Get the .SUNW_signature section for the module, it it exists.
2667 * This section exists only for crypto modules. None of the
2668 * primary modules have this section currently.
2670 static void
2671 get_signature(struct module *mp, struct _buf *file)
2673 char *shstrtab, *sigdata = NULL;
2674 size_t shstrlen;
2675 Shdr *shp;
2676 uint_t i;
2678 if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) {
2679 _kobj_printf(ops, "krtld: get_signature: %s, ",
2680 mp->filename);
2681 _kobj_printf(ops, "corrupt e_shstrndx %u\n",
2682 mp->hdr.e_shstrndx);
2683 return;
2686 shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize);
2687 shstrlen = shp->sh_size;
2688 shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP);
2690 if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) {
2691 _kobj_printf(ops, "krtld: get_signature: %s, ",
2692 mp->filename);
2693 _kobj_printf(ops, "error reading section %u\n",
2694 mp->hdr.e_shstrndx);
2695 kobj_free(shstrtab, shstrlen);
2696 return;
2699 for (i = 0; i < mp->hdr.e_shnum; i++) {
2700 shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize);
2701 if (shp->sh_size != 0 && shp->sh_name < shstrlen &&
2702 strcmp(shstrtab + shp->sh_name,
2703 ELF_SIGNATURE_SECTION) == 0) {
2704 filesig_vers_t filesig_version;
2705 size_t sigsize = shp->sh_size + SHA1_DIGEST_LENGTH;
2706 sigdata = kobj_alloc(sigsize, KM_WAIT|KM_SCRATCH);
2708 if (kobj_read_file(file, sigdata, shp->sh_size,
2709 shp->sh_offset) < 0) {
2710 _kobj_printf(ops, "krtld: get_signature: %s,"
2711 " error reading .SUNW_signature data\n",
2712 mp->filename);
2713 kobj_free(sigdata, sigsize);
2714 kobj_free(shstrtab, shstrlen);
2715 return;
2717 filesig_version = ((struct filesignatures *)sigdata)->
2718 filesig_sig.filesig_version;
2719 if (!(filesig_version == FILESIG_VERSION1 ||
2720 filesig_version == FILESIG_VERSION3)) {
2721 /* skip versions we don't understand */
2722 kobj_free(sigdata, sigsize);
2723 kobj_free(shstrtab, shstrlen);
2724 return;
2727 mp->sigdata = sigdata;
2728 mp->sigsize = sigsize;
2729 break;
2733 if (sigdata != NULL) {
2734 crypto_es_hash(mp, sigdata + shp->sh_size, shstrtab);
2737 kobj_free(shstrtab, shstrlen);
2740 static void
2741 add_dependent(struct module *mp, struct module *dep)
2743 struct module_list *lp;
2745 for (lp = mp->head; lp; lp = lp->next) {
2746 if (lp->mp == dep)
2747 return; /* already on the list */
2750 if (lp == NULL) {
2751 lp = kobj_zalloc(sizeof (*lp), KM_WAIT);
2753 lp->mp = dep;
2754 lp->next = NULL;
2755 if (mp->tail)
2756 mp->tail->next = lp;
2757 else
2758 mp->head = lp;
2759 mp->tail = lp;
2763 static int
2764 do_dependents(struct modctl *modp, char *modname, size_t modnamelen)
2766 struct module *mp;
2767 struct modctl *req;
2768 char *d, *p, *q;
2769 int c;
2770 char *err_modname = NULL;
2772 mp = modp->mod_mp;
2774 if ((p = mp->depends_on) == NULL)
2775 return (0);
2777 for (;;) {
2779 * Skip space.
2781 while (*p && (*p == ' ' || *p == '\t'))
2782 p++;
2784 * Get module name.
2786 d = p;
2787 q = modname;
2788 c = 0;
2789 while (*p && *p != ' ' && *p != '\t') {
2790 if (c < modnamelen - 1) {
2791 *q++ = *p;
2792 c++;
2794 p++;
2797 if (q == modname)
2798 break;
2800 if (c == modnamelen - 1) {
2801 char *dep = kobj_alloc(p - d + 1, KM_WAIT|KM_TMP);
2803 (void) strncpy(dep, d, p - d + 1);
2804 dep[p - d] = '\0';
2806 _kobj_printf(ops, "%s: dependency ", modp->mod_modname);
2807 _kobj_printf(ops, "'%s' too long ", dep);
2808 _kobj_printf(ops, "(max %d chars)\n", modnamelen);
2810 kobj_free(dep, p - d + 1);
2812 return (-1);
2815 *q = '\0';
2816 if ((req = mod_load_requisite(modp, modname)) == NULL) {
2817 #ifndef KOBJ_DEBUG
2818 if (_moddebug & MODDEBUG_LOADMSG) {
2819 #endif /* KOBJ_DEBUG */
2820 _kobj_printf(ops,
2821 "%s: unable to resolve dependency, ",
2822 modp->mod_modname);
2823 _kobj_printf(ops, "cannot load module '%s'\n",
2824 modname);
2825 #ifndef KOBJ_DEBUG
2827 #endif /* KOBJ_DEBUG */
2828 if (err_modname == NULL) {
2830 * This must be the same size as the modname
2831 * one.
2833 err_modname = kobj_zalloc(MODMAXNAMELEN,
2834 KM_WAIT);
2837 * We can use strcpy() here without fearing
2838 * the NULL terminator because the size of
2839 * err_modname is the same as one of modname,
2840 * and it's filled with zeros.
2842 (void) strcpy(err_modname, modname);
2844 continue;
2847 add_dependent(mp, req->mod_mp);
2848 mod_release_mod(req);
2852 if (err_modname != NULL) {
2854 * Copy the first module name where you detect an error to keep
2855 * its behavior the same as before.
2856 * This way keeps minimizing the memory use for error
2857 * modules, and this might be important at boot time because
2858 * the memory usage is a crucial factor for booting in most
2859 * cases. You can expect more verbose messages when using
2860 * a debug kernel or setting a bit in moddebug.
2862 bzero(modname, MODMAXNAMELEN);
2863 (void) strcpy(modname, err_modname);
2864 kobj_free(err_modname, MODMAXNAMELEN);
2865 return (-1);
2868 return (0);
2871 static int
2872 do_common(struct module *mp)
2874 int err;
2877 * first time through, assign all symbols defined in other
2878 * modules, and count up how much common space will be needed
2879 * (bss_size and bss_align)
2881 if ((err = do_symbols(mp, 0)) < 0)
2882 return (err);
2884 * increase bss_size by the maximum delta that could be
2885 * computed by the ALIGN below
2887 mp->bss_size += mp->bss_align;
2888 if (mp->bss_size) {
2889 if (standalone)
2890 mp->bss = (uintptr_t)kobj_segbrk(&_edata, mp->bss_size,
2891 MINALIGN, 0);
2892 else
2893 mp->bss = (uintptr_t)vmem_alloc(data_arena,
2894 mp->bss_size, VM_SLEEP | VM_BESTFIT);
2895 bzero((void *)mp->bss, mp->bss_size);
2896 /* now assign addresses to all common symbols */
2897 if ((err = do_symbols(mp, ALIGN(mp->bss, mp->bss_align))) < 0)
2898 return (err);
2900 return (0);
2903 static int
2904 do_symbols(struct module *mp, Elf64_Addr bss_base)
2906 int bss_align;
2907 uintptr_t bss_ptr;
2908 int err;
2909 int i;
2910 Sym *sp, *sp1;
2911 char *name;
2912 int assign;
2913 int resolved = 1;
2916 * Nothing left to do (optimization).
2918 if (mp->flags & KOBJ_RESOLVED)
2919 return (0);
2921 assign = (bss_base) ? 1 : 0;
2922 bss_ptr = bss_base;
2923 bss_align = 0;
2924 err = 0;
2926 for (i = 1; i < mp->nsyms; i++) {
2927 sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * i);
2929 * we know that st_name is in bounds, since get_sections
2930 * has already checked all of the symbols
2932 name = mp->strings + sp->st_name;
2933 if (sp->st_shndx != SHN_UNDEF && sp->st_shndx != SHN_COMMON)
2934 continue;
2936 * TLS symbols are ignored in the kernel
2938 if (ELF_ST_TYPE(sp->st_info) == STT_TLS) {
2939 _kobj_printf(ops, "%s: TLS symbol ",
2940 mp->filename);
2941 _kobj_printf(ops, "not supported '%s'\n",
2942 name);
2943 err = DOSYM_UNDEF;
2944 continue;
2947 if (ELF_ST_BIND(sp->st_info) != STB_LOCAL) {
2948 if ((sp1 = kobj_lookup_all(mp, name, 0)) != NULL) {
2949 sp->st_shndx = SHN_ABS;
2950 sp->st_value = sp1->st_value;
2951 continue;
2955 if (sp->st_shndx == SHN_UNDEF) {
2956 resolved = 0;
2958 if (strncmp(name, sdt_prefix, strlen(sdt_prefix)) == 0)
2959 continue;
2962 * If it's not a weak reference and it's
2963 * not a primary object, it's an error.
2964 * (Primary objects may take more than
2965 * one pass to resolve)
2967 if (!(mp->flags & KOBJ_PRIM) &&
2968 ELF_ST_BIND(sp->st_info) != STB_WEAK) {
2969 _kobj_printf(ops, "%s: undefined symbol",
2970 mp->filename);
2971 _kobj_printf(ops, " '%s'\n", name);
2973 * Try to determine whether this symbol
2974 * represents a dependency on obsolete
2975 * unsafe driver support. This is just
2976 * to make the warning more informative.
2978 if (strcmp(name, "sleep") == 0 ||
2979 strcmp(name, "unsleep") == 0 ||
2980 strcmp(name, "wakeup") == 0 ||
2981 strcmp(name, "bsd_compat_ioctl") == 0 ||
2982 strcmp(name, "unsafe_driver") == 0 ||
2983 strncmp(name, "spl", 3) == 0 ||
2984 strncmp(name, "i_ddi_spl", 9) == 0)
2985 err = DOSYM_UNSAFE;
2986 if (err == 0)
2987 err = DOSYM_UNDEF;
2989 continue;
2992 * It's a common symbol - st_value is the
2993 * required alignment.
2995 if (sp->st_value > bss_align)
2996 bss_align = sp->st_value;
2997 bss_ptr = ALIGN(bss_ptr, sp->st_value);
2998 if (assign) {
2999 sp->st_shndx = SHN_ABS;
3000 sp->st_value = bss_ptr;
3002 bss_ptr += sp->st_size;
3004 if (err)
3005 return (err);
3006 if (assign == 0 && mp->bss == (uintptr_t)NULL) {
3007 mp->bss_align = bss_align;
3008 mp->bss_size = bss_ptr;
3009 } else if (resolved) {
3010 mp->flags |= KOBJ_RESOLVED;
3013 return (0);
3016 uint_t
3017 kobj_hash_name(const char *p)
3019 uint_t g;
3020 uint_t hval;
3022 hval = 0;
3023 while (*p) {
3024 hval = (hval << 4) + *p++;
3025 if ((g = (hval & 0xf0000000)) != 0)
3026 hval ^= g >> 24;
3027 hval &= ~g;
3029 return (hval);
3032 /* look for name in all modules */
3033 uintptr_t
3034 kobj_getsymvalue(char *name, int kernelonly)
3036 Sym *sp;
3037 struct modctl *modp;
3038 struct module *mp;
3039 uintptr_t value = 0;
3041 if ((sp = kobj_lookup_kernel(name)) != NULL)
3042 return ((uintptr_t)sp->st_value);
3044 if (kernelonly)
3045 return (0); /* didn't find it in the kernel so give up */
3047 mutex_enter(&mod_lock);
3048 modp = &modules;
3049 do {
3050 mp = (struct module *)modp->mod_mp;
3051 if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded &&
3052 (sp = lookup_one(mp, name))) {
3053 value = (uintptr_t)sp->st_value;
3054 break;
3056 } while ((modp = modp->mod_next) != &modules);
3057 mutex_exit(&mod_lock);
3058 return (value);
3061 /* look for a symbol near value. */
3062 char *
3063 kobj_getsymname(uintptr_t value, ulong_t *offset)
3065 char *name = NULL;
3066 struct modctl *modp;
3068 struct modctl_list *lp;
3069 struct module *mp;
3072 * Loop through the primary kernel modules.
3074 for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) {
3075 mp = mod(lp);
3077 if ((name = kobj_searchsym(mp, value, offset)) != NULL)
3078 return (name);
3081 mutex_enter(&mod_lock);
3082 modp = &modules;
3083 do {
3084 mp = (struct module *)modp->mod_mp;
3085 if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded &&
3086 (name = kobj_searchsym(mp, value, offset)))
3087 break;
3088 } while ((modp = modp->mod_next) != &modules);
3089 mutex_exit(&mod_lock);
3090 return (name);
3093 /* return address of symbol and size */
3095 uintptr_t
3096 kobj_getelfsym(char *name, void *mp, int *size)
3098 Sym *sp;
3100 if (mp == NULL)
3101 sp = kobj_lookup_kernel(name);
3102 else
3103 sp = lookup_one(mp, name);
3105 if (sp == NULL)
3106 return (0);
3108 *size = (int)sp->st_size;
3109 return ((uintptr_t)sp->st_value);
3112 uintptr_t
3113 kobj_lookup(struct module *mod, const char *name)
3115 Sym *sp;
3117 sp = lookup_one(mod, name);
3119 if (sp == NULL)
3120 return (0);
3122 return ((uintptr_t)sp->st_value);
3125 char *
3126 kobj_searchsym(struct module *mp, uintptr_t value, ulong_t *offset)
3128 Sym *symtabptr;
3129 char *strtabptr;
3130 int symnum;
3131 Sym *sym;
3132 Sym *cursym;
3133 uintptr_t curval;
3135 *offset = (ulong_t)-1l; /* assume not found */
3136 cursym = NULL;
3138 if (kobj_addrcheck(mp, (void *)value) != 0)
3139 return (NULL); /* not in this module */
3141 strtabptr = mp->strings;
3142 symtabptr = (Sym *)mp->symtbl;
3145 * Scan the module's symbol table for a symbol <= value
3147 for (symnum = 1, sym = symtabptr + 1;
3148 symnum < mp->nsyms; symnum++, sym = (Sym *)
3149 ((uintptr_t)sym + mp->symhdr->sh_entsize)) {
3150 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
3151 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
3152 continue;
3153 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
3154 ELF_ST_TYPE(sym->st_info) != STT_FUNC)
3155 continue;
3158 curval = (uintptr_t)sym->st_value;
3160 if (curval > value)
3161 continue;
3164 * If one or both are functions...
3166 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC || (cursym != NULL &&
3167 ELF_ST_TYPE(cursym->st_info) == STT_FUNC)) {
3168 /* Ignore if the address is out of the bounds */
3169 if (value - sym->st_value >= sym->st_size)
3170 continue;
3172 if (cursym != NULL &&
3173 ELF_ST_TYPE(cursym->st_info) == STT_FUNC) {
3174 /* Prefer the function to the non-function */
3175 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
3176 continue;
3178 /* Prefer the larger of the two functions */
3179 if (sym->st_size <= cursym->st_size)
3180 continue;
3182 } else if (value - curval >= *offset) {
3183 continue;
3186 *offset = (ulong_t)(value - curval);
3187 cursym = sym;
3189 if (cursym == NULL)
3190 return (NULL);
3192 return (strtabptr + cursym->st_name);
3195 Sym *
3196 kobj_lookup_all(struct module *mp, char *name, int include_self)
3198 Sym *sp;
3199 struct module_list *mlp;
3200 struct modctl_list *clp;
3201 struct module *mmp;
3203 if (include_self && (sp = lookup_one(mp, name)) != NULL)
3204 return (sp);
3206 for (mlp = mp->head; mlp; mlp = mlp->next) {
3207 if ((sp = lookup_one(mlp->mp, name)) != NULL &&
3208 ELF_ST_BIND(sp->st_info) != STB_LOCAL)
3209 return (sp);
3213 * Loop through the primary kernel modules.
3215 for (clp = kobj_lm_lookup(KOBJ_LM_PRIMARY); clp; clp = clp->modl_next) {
3216 mmp = mod(clp);
3218 if (mmp == NULL || mp == mmp)
3219 continue;
3221 if ((sp = lookup_one(mmp, name)) != NULL &&
3222 ELF_ST_BIND(sp->st_info) != STB_LOCAL)
3223 return (sp);
3225 return (NULL);
3228 Sym *
3229 kobj_lookup_kernel(const char *name)
3231 struct modctl_list *lp;
3232 struct module *mp;
3233 Sym *sp;
3236 * Loop through the primary kernel modules.
3238 for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) {
3239 mp = mod(lp);
3241 if (mp == NULL)
3242 continue;
3244 if ((sp = lookup_one(mp, name)) != NULL)
3245 return (sp);
3247 return (NULL);
3250 static Sym *
3251 lookup_one(struct module *mp, const char *name)
3253 symid_t *ip;
3254 char *name1;
3255 Sym *sp;
3257 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3258 ip = &mp->chains[*ip]) {
3259 sp = (Sym *)(mp->symtbl +
3260 mp->symhdr->sh_entsize * *ip);
3261 name1 = mp->strings + sp->st_name;
3262 if (strcmp(name, name1) == 0 &&
3263 ELF_ST_TYPE(sp->st_info) != STT_FILE &&
3264 sp->st_shndx != SHN_UNDEF &&
3265 sp->st_shndx != SHN_COMMON)
3266 return (sp);
3268 return (NULL);
3272 * Lookup a given symbol pointer in the module's symbol hash. If the symbol
3273 * is hashed, return the symbol pointer; otherwise return NULL.
3275 static Sym *
3276 sym_lookup(struct module *mp, Sym *ksp)
3278 char *name = mp->strings + ksp->st_name;
3279 symid_t *ip;
3280 Sym *sp;
3282 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3283 ip = &mp->chains[*ip]) {
3284 sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * *ip);
3285 if (sp == ksp)
3286 return (ksp);
3288 return (NULL);
3291 static void
3292 sym_insert(struct module *mp, char *name, symid_t index)
3294 symid_t *ip;
3296 #ifdef KOBJ_DEBUG
3297 if (kobj_debug & D_SYMBOLS) {
3298 static struct module *lastmp = NULL;
3299 Sym *sp;
3300 if (lastmp != mp) {
3301 _kobj_printf(ops,
3302 "krtld: symbol entry: file=%s\n",
3303 mp->filename);
3304 _kobj_printf(ops,
3305 "krtld:\tsymndx\tvalue\t\t"
3306 "symbol name\n");
3307 lastmp = mp;
3309 sp = (Sym *)(mp->symtbl +
3310 index * mp->symhdr->sh_entsize);
3311 _kobj_printf(ops, "krtld:\t[%3d]", index);
3312 _kobj_printf(ops, "\t0x%lx", sp->st_value);
3313 _kobj_printf(ops, "\t%s\n", name);
3316 #endif
3317 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3318 ip = &mp->chains[*ip]) {
3321 *ip = index;
3324 struct modctl *
3325 kobj_boot_mod_lookup(const char *modname)
3327 struct modctl *mctl = kobj_modules;
3329 do {
3330 if (strcmp(modname, mctl->mod_modname) == 0)
3331 return (mctl);
3332 } while ((mctl = mctl->mod_next) != kobj_modules);
3334 return (NULL);
3338 * Determine if the module exists.
3341 kobj_path_exists(char *name, int use_path)
3343 struct _buf *file;
3345 file = kobj_open_path(name, use_path);
3346 if (file == (struct _buf *)-1)
3347 return (0);
3348 kobj_close_file(file);
3349 return (1);
3353 * fullname is dynamically allocated to be able to hold the
3354 * maximum size string that can be constructed from name.
3355 * path is exactly like the shell PATH variable.
3357 struct _buf *
3358 kobj_open_path(char *name, int use_path)
3360 char *p, *q;
3361 char *pathp;
3362 char *pathpsave;
3363 char *fullname;
3364 int maxpathlen;
3365 struct _buf *file;
3367 if (!use_path)
3368 pathp = ""; /* use name as specified */
3369 else
3370 pathp = kobj_module_path;
3371 /* use configured default path */
3373 pathpsave = pathp; /* keep this for error reporting */
3376 * Allocate enough space for the largest possible fullname.
3377 * since path is of the form <directory> : <directory> : ...
3378 * we're potentially allocating a little more than we need to
3379 * but we'll allocate the exact amount when we find the right directory.
3380 * (The + 3 below is one for NULL terminator and one for the '/'
3381 * we might have to add at the beginning of path and one for
3382 * the '/' between path and name.)
3384 maxpathlen = strlen(pathp) + strlen(name) + 3;
3385 fullname = kobj_zalloc(maxpathlen, KM_WAIT);
3387 for (;;) {
3388 p = fullname;
3389 if (*pathp != '\0' && *pathp != '/')
3390 *p++ = '/'; /* path must start with '/' */
3391 while (*pathp && *pathp != ':' && *pathp != ' ')
3392 *p++ = *pathp++;
3393 if (p != fullname && p[-1] != '/')
3394 *p++ = '/';
3395 q = name;
3396 while (*q)
3397 *p++ = *q++;
3398 *p = 0;
3399 if ((file = kobj_open_file(fullname)) != (struct _buf *)-1) {
3400 kobj_free(fullname, maxpathlen);
3401 return (file);
3403 while (*pathp == ' ' || *pathp == ':')
3404 pathp++;
3405 if (*pathp == 0)
3406 break;
3409 kobj_free(fullname, maxpathlen);
3410 if (_moddebug & MODDEBUG_ERRMSG) {
3411 _kobj_printf(ops, "can't open %s,", name);
3412 _kobj_printf(ops, " path is %s\n", pathpsave);
3414 return ((struct _buf *)-1);
3417 intptr_t
3418 kobj_open(char *filename)
3420 struct vnode *vp;
3421 int fd;
3423 if (_modrootloaded) {
3424 struct kobjopen_tctl *ltp = kobjopen_alloc(filename);
3425 int Errno;
3428 * Hand off the open to a thread who has a
3429 * stack size capable handling the request.
3431 if (curthread != &t0) {
3432 (void) thread_create(NULL, DEFAULTSTKSZ * 2,
3433 kobjopen_thread, ltp, 0, &p0, TS_RUN, maxclsyspri);
3434 sema_p(&ltp->sema);
3435 Errno = ltp->Errno;
3436 vp = ltp->vp;
3437 } else {
3439 * 1098067: module creds should not be those of the
3440 * caller
3442 cred_t *saved_cred = curthread->t_cred;
3443 curthread->t_cred = kcred;
3444 Errno = vn_openat(filename, UIO_SYSSPACE, FREAD, 0, &vp,
3445 0, 0, rootdir, -1);
3446 curthread->t_cred = saved_cred;
3448 kobjopen_free(ltp);
3450 if (Errno) {
3451 if (_moddebug & MODDEBUG_ERRMSG) {
3452 _kobj_printf(ops,
3453 "kobj_open: vn_open of %s fails, ",
3454 filename);
3455 _kobj_printf(ops, "Errno = %d\n", Errno);
3457 return (-1);
3458 } else {
3459 if (_moddebug & MODDEBUG_ERRMSG) {
3460 _kobj_printf(ops, "kobj_open: '%s'", filename);
3461 _kobj_printf(ops, " vp = %p\n", vp);
3463 return ((intptr_t)vp);
3465 } else {
3466 fd = kobj_boot_open(filename, 0);
3468 if (_moddebug & MODDEBUG_ERRMSG) {
3469 if (fd < 0)
3470 _kobj_printf(ops,
3471 "kobj_open: can't open %s\n", filename);
3472 else {
3473 _kobj_printf(ops, "kobj_open: '%s'", filename);
3474 _kobj_printf(ops, " descr = 0x%x\n", fd);
3477 return ((intptr_t)fd);
3482 * Calls to kobj_open() are handled off to this routine as a separate thread.
3484 static void
3485 kobjopen_thread(struct kobjopen_tctl *ltp)
3487 kmutex_t cpr_lk;
3488 callb_cpr_t cpr_i;
3490 mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
3491 CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "kobjopen");
3492 ltp->Errno = vn_open(ltp->name, UIO_SYSSPACE, FREAD, 0, &(ltp->vp),
3493 0, 0);
3494 sema_v(&ltp->sema);
3495 mutex_enter(&cpr_lk);
3496 CALLB_CPR_EXIT(&cpr_i);
3497 mutex_destroy(&cpr_lk);
3498 thread_exit();
3502 * allocate and initialize a kobjopen thread structure
3504 static struct kobjopen_tctl *
3505 kobjopen_alloc(char *filename)
3507 struct kobjopen_tctl *ltp = kmem_zalloc(sizeof (*ltp), KM_SLEEP);
3509 ASSERT(filename != NULL);
3511 ltp->name = kmem_alloc(strlen(filename) + 1, KM_SLEEP);
3512 bcopy(filename, ltp->name, strlen(filename) + 1);
3513 sema_init(&ltp->sema, 0, NULL, SEMA_DEFAULT, NULL);
3514 return (ltp);
3518 * free a kobjopen thread control structure
3520 static void
3521 kobjopen_free(struct kobjopen_tctl *ltp)
3523 sema_destroy(&ltp->sema);
3524 kmem_free(ltp->name, strlen(ltp->name) + 1);
3525 kmem_free(ltp, sizeof (*ltp));
3529 kobj_read(intptr_t descr, char *buf, uint_t size, uint_t offset)
3531 int stat;
3532 ssize_t resid;
3534 if (_modrootloaded) {
3535 if ((stat = vn_rdwr(UIO_READ, (struct vnode *)descr, buf, size,
3536 (offset_t)offset, UIO_SYSSPACE, 0, 0, CRED(),
3537 &resid)) != 0) {
3538 _kobj_printf(ops,
3539 "vn_rdwr failed with error 0x%x\n", stat);
3540 return (-1);
3542 return (size - resid);
3543 } else {
3544 int count = 0;
3546 if (kobj_boot_seek((int)descr, (off_t)0, offset) != 0) {
3547 _kobj_printf(ops,
3548 "kobj_read: seek 0x%x failed\n", offset);
3549 return (-1);
3552 count = kobj_boot_read((int)descr, buf, size);
3553 if (count < size) {
3554 if (_moddebug & MODDEBUG_ERRMSG) {
3555 _kobj_printf(ops,
3556 "kobj_read: req %d bytes, ", size);
3557 _kobj_printf(ops, "got %d\n", count);
3560 return (count);
3564 void
3565 kobj_close(intptr_t descr)
3567 if (_moddebug & MODDEBUG_ERRMSG)
3568 _kobj_printf(ops, "kobj_close: 0x%lx\n", descr);
3570 if (_modrootloaded) {
3571 struct vnode *vp = (struct vnode *)descr;
3572 (void) fop_close(vp, FREAD, 1, 0, CRED(), NULL);
3573 VN_RELE(vp);
3574 } else
3575 (void) kobj_boot_close((int)descr);
3579 kobj_fstat(intptr_t descr, struct bootstat *buf)
3581 if (buf == NULL)
3582 return (-1);
3584 if (_modrootloaded) {
3585 vattr_t vattr;
3586 struct vnode *vp = (struct vnode *)descr;
3587 if (fop_getattr(vp, &vattr, 0, kcred, NULL) != 0)
3588 return (-1);
3591 * The vattr and bootstat structures are similar, but not
3592 * identical. We do our best to fill in the bootstat structure
3593 * from the contents of vattr (transfering only the ones that
3594 * are obvious.
3597 buf->st_mode = (uint32_t)vattr.va_mode;
3598 buf->st_nlink = (uint32_t)vattr.va_nlink;
3599 buf->st_uid = (int32_t)vattr.va_uid;
3600 buf->st_gid = (int32_t)vattr.va_gid;
3601 buf->st_rdev = (uint64_t)vattr.va_rdev;
3602 buf->st_size = (uint64_t)vattr.va_size;
3603 buf->st_atim.tv_sec = (int64_t)vattr.va_atime.tv_sec;
3604 buf->st_atim.tv_nsec = (int64_t)vattr.va_atime.tv_nsec;
3605 buf->st_mtim.tv_sec = (int64_t)vattr.va_mtime.tv_sec;
3606 buf->st_mtim.tv_nsec = (int64_t)vattr.va_mtime.tv_nsec;
3607 buf->st_ctim.tv_sec = (int64_t)vattr.va_ctime.tv_sec;
3608 buf->st_ctim.tv_nsec = (int64_t)vattr.va_ctime.tv_nsec;
3609 buf->st_blksize = (int32_t)vattr.va_blksize;
3610 buf->st_blocks = (int64_t)vattr.va_nblocks;
3612 return (0);
3615 return (kobj_boot_fstat((int)descr, buf));
3619 struct _buf *
3620 kobj_open_file(char *name)
3622 struct _buf *file;
3623 struct compinfo cbuf;
3624 intptr_t fd;
3626 if ((fd = kobj_open(name)) == -1) {
3627 return ((struct _buf *)-1);
3630 file = kobj_zalloc(sizeof (struct _buf), KM_WAIT|KM_TMP);
3631 file->_fd = fd;
3632 file->_name = kobj_alloc(strlen(name)+1, KM_WAIT|KM_TMP);
3633 file->_cnt = file->_size = file->_off = 0;
3634 file->_ln = 1;
3635 file->_ptr = file->_base;
3636 (void) strcpy(file->_name, name);
3639 * Before root is mounted, we must check
3640 * for a compressed file and do our own
3641 * buffering.
3643 if (_modrootloaded) {
3644 file->_base = kobj_zalloc(MAXBSIZE, KM_WAIT);
3645 file->_bsize = MAXBSIZE;
3647 /* Check if the file is compressed */
3648 file->_iscmp = kobj_is_compressed(fd);
3649 } else {
3650 if (kobj_boot_compinfo(fd, &cbuf) != 0) {
3651 kobj_close_file(file);
3652 return ((struct _buf *)-1);
3654 file->_iscmp = cbuf.iscmp;
3655 if (file->_iscmp) {
3656 if (kobj_comp_setup(file, &cbuf) != 0) {
3657 kobj_close_file(file);
3658 return ((struct _buf *)-1);
3660 } else {
3661 file->_base = kobj_zalloc(cbuf.blksize, KM_WAIT|KM_TMP);
3662 file->_bsize = cbuf.blksize;
3665 return (file);
3668 static int
3669 kobj_comp_setup(struct _buf *file, struct compinfo *cip)
3671 struct comphdr *hdr;
3674 * read the compressed image into memory,
3675 * so we can deompress from there
3677 file->_dsize = cip->fsize;
3678 file->_dbuf = kobj_alloc(cip->fsize, KM_WAIT|KM_TMP);
3679 if (kobj_read(file->_fd, file->_dbuf, cip->fsize, 0) != cip->fsize) {
3680 kobj_free(file->_dbuf, cip->fsize);
3681 return (-1);
3684 hdr = kobj_comphdr(file);
3685 if (hdr->ch_magic != CH_MAGIC_ZLIB || hdr->ch_version != CH_VERSION ||
3686 hdr->ch_algorithm != CH_ALG_ZLIB || hdr->ch_fsize == 0 ||
3687 !ISP2(hdr->ch_blksize)) {
3688 kobj_free(file->_dbuf, cip->fsize);
3689 return (-1);
3691 file->_base = kobj_alloc(hdr->ch_blksize, KM_WAIT|KM_TMP);
3692 file->_bsize = hdr->ch_blksize;
3693 return (0);
3696 void
3697 kobj_close_file(struct _buf *file)
3699 kobj_close(file->_fd);
3700 if (file->_base != NULL)
3701 kobj_free(file->_base, file->_bsize);
3702 if (file->_dbuf != NULL)
3703 kobj_free(file->_dbuf, file->_dsize);
3704 kobj_free(file->_name, strlen(file->_name)+1);
3705 kobj_free(file, sizeof (struct _buf));
3709 kobj_read_file(struct _buf *file, char *buf, uint_t size, uint_t off)
3711 int b_size, c_size;
3712 int b_off; /* Offset into buffer for start of bcopy */
3713 int count = 0;
3714 int page_addr;
3716 if (_moddebug & MODDEBUG_ERRMSG) {
3717 _kobj_printf(ops, "kobj_read_file: size=%x,", size);
3718 _kobj_printf(ops, " offset=%x at", off);
3719 _kobj_printf(ops, " buf=%x\n", buf);
3723 * Handle compressed (gzip for now) file here. First get the
3724 * compressed size, then read the image into memory and finally
3725 * call zlib to decompress the image at the supplied memory buffer.
3727 if (file->_iscmp == CH_MAGIC_GZIP) {
3728 ulong_t dlen;
3729 vattr_t vattr;
3730 struct vnode *vp = (struct vnode *)file->_fd;
3731 ssize_t resid;
3732 int err = 0;
3734 if (fop_getattr(vp, &vattr, 0, kcred, NULL) != 0)
3735 return (-1);
3737 file->_dbuf = kobj_alloc(vattr.va_size, KM_WAIT|KM_TMP);
3738 file->_dsize = vattr.va_size;
3740 /* Read the compressed file into memory */
3741 if ((err = vn_rdwr(UIO_READ, vp, file->_dbuf, vattr.va_size,
3742 (offset_t)(0), UIO_SYSSPACE, 0, 0, CRED(),
3743 &resid)) != 0) {
3745 _kobj_printf(ops, "kobj_read_file :vn_rdwr() failed, "
3746 "error code 0x%x\n", err);
3747 return (-1);
3750 dlen = size;
3752 /* Decompress the image at the supplied memory buffer */
3753 if ((err = z_uncompress(buf, &dlen, file->_dbuf,
3754 vattr.va_size)) != Z_OK) {
3755 _kobj_printf(ops, "kobj_read_file: z_uncompress "
3756 "failed, error code : 0x%x\n", err);
3757 return (-1);
3760 if (dlen != size) {
3761 _kobj_printf(ops, "kobj_read_file: z_uncompress "
3762 "failed to uncompress (size returned 0x%x , "
3763 "expected size: 0x%x)\n", dlen, size);
3764 return (-1);
3767 return (0);
3770 while (size) {
3771 page_addr = F_PAGE(file, off);
3772 b_size = file->_size;
3774 * If we have the filesystem page the caller's referring to
3775 * and we have something in the buffer,
3776 * satisfy as much of the request from the buffer as we can.
3778 if (page_addr == file->_off && b_size > 0) {
3779 b_off = B_OFFSET(file, off);
3780 c_size = b_size - b_off;
3782 * If there's nothing to copy, we're at EOF.
3784 if (c_size <= 0)
3785 break;
3786 if (c_size > size)
3787 c_size = size;
3788 if (buf) {
3789 if (_moddebug & MODDEBUG_ERRMSG)
3790 _kobj_printf(ops, "copying %x bytes\n",
3791 c_size);
3792 bcopy(file->_base+b_off, buf, c_size);
3793 size -= c_size;
3794 off += c_size;
3795 buf += c_size;
3796 count += c_size;
3797 } else {
3798 _kobj_printf(ops, "kobj_read: system error");
3799 count = -1;
3800 break;
3802 } else {
3804 * If the caller's offset is page aligned and
3805 * the caller want's at least a filesystem page and
3806 * the caller provided a buffer,
3807 * read directly into the caller's buffer.
3809 if (page_addr == off &&
3810 (c_size = F_BLKS(file, size)) && buf) {
3811 c_size = kobj_read_blks(file, buf, c_size,
3812 page_addr);
3813 if (c_size < 0) {
3814 count = -1;
3815 break;
3817 count += c_size;
3818 if (c_size != F_BLKS(file, size))
3819 break;
3820 size -= c_size;
3821 off += c_size;
3822 buf += c_size;
3824 * Otherwise, read into our buffer and copy next time
3825 * around the loop.
3827 } else {
3828 file->_off = page_addr;
3829 c_size = kobj_read_blks(file, file->_base,
3830 file->_bsize, page_addr);
3831 file->_ptr = file->_base;
3832 file->_cnt = c_size;
3833 file->_size = c_size;
3835 * If a _filbuf call or nothing read, break.
3837 if (buf == NULL || c_size <= 0) {
3838 count = c_size;
3839 break;
3842 if (_moddebug & MODDEBUG_ERRMSG)
3843 _kobj_printf(ops, "read %x bytes\n", c_size);
3846 if (_moddebug & MODDEBUG_ERRMSG)
3847 _kobj_printf(ops, "count = %x\n", count);
3849 return (count);
3852 static int
3853 kobj_read_blks(struct _buf *file, char *buf, uint_t size, uint_t off)
3855 int ret;
3857 ASSERT(B_OFFSET(file, size) == 0 && B_OFFSET(file, off) == 0);
3858 if (file->_iscmp) {
3859 uint_t blks;
3860 int nret;
3862 ret = 0;
3863 for (blks = size / file->_bsize; blks != 0; blks--) {
3864 nret = kobj_uncomp_blk(file, buf, off);
3865 if (nret == -1)
3866 return (-1);
3867 buf += nret;
3868 off += nret;
3869 ret += nret;
3870 if (nret < file->_bsize)
3871 break;
3873 } else
3874 ret = kobj_read(file->_fd, buf, size, off);
3875 return (ret);
3878 static int
3879 kobj_uncomp_blk(struct _buf *file, char *buf, uint_t off)
3881 struct comphdr *hdr = kobj_comphdr(file);
3882 ulong_t dlen, slen;
3883 caddr_t src;
3884 int i;
3886 dlen = file->_bsize;
3887 i = off / file->_bsize;
3888 src = file->_dbuf + hdr->ch_blkmap[i];
3889 if (i == hdr->ch_fsize / file->_bsize)
3890 slen = file->_dsize - hdr->ch_blkmap[i];
3891 else
3892 slen = hdr->ch_blkmap[i + 1] - hdr->ch_blkmap[i];
3893 if (z_uncompress(buf, &dlen, src, slen) != Z_OK)
3894 return (-1);
3895 return (dlen);
3899 kobj_filbuf(struct _buf *f)
3901 if (kobj_read_file(f, NULL, f->_bsize, f->_off + f->_size) > 0)
3902 return (kobj_getc(f));
3903 return (-1);
3906 void
3907 kobj_free(void *address, size_t size)
3909 if (standalone)
3910 return;
3912 kmem_free(address, size);
3913 kobj_stat.nfree_calls++;
3914 kobj_stat.nfree += size;
3917 void *
3918 kobj_zalloc(size_t size, int flag)
3920 void *v;
3922 if ((v = kobj_alloc(size, flag)) != 0) {
3923 bzero(v, size);
3926 return (v);
3929 void *
3930 kobj_alloc(size_t size, int flag)
3933 * If we are running standalone in the
3934 * linker, we ask boot for memory.
3935 * Either it's temporary memory that we lose
3936 * once boot is mapped out or we allocate it
3937 * permanently using the dynamic data segment.
3939 if (standalone) {
3940 #if defined(_OBP)
3941 if (flag & (KM_TMP | KM_SCRATCH))
3942 return (bop_temp_alloc(size, MINALIGN));
3943 #else
3944 if (flag & (KM_TMP | KM_SCRATCH))
3945 return (BOP_ALLOC(ops, 0, size, MINALIGN));
3946 #endif
3947 return (kobj_segbrk(&_edata, size, MINALIGN, 0));
3950 kobj_stat.nalloc_calls++;
3951 kobj_stat.nalloc += size;
3953 return (kmem_alloc(size, (flag & KM_NOWAIT) ? KM_NOSLEEP : KM_SLEEP));
3957 * Allow the "mod" system to sync up with the work
3958 * already done by kobj during the initial loading
3959 * of the kernel. This also gives us a chance
3960 * to reallocate memory that belongs to boot.
3962 void
3963 kobj_sync(void)
3965 struct modctl_list *lp, **lpp;
3968 * The module path can be set in /etc/system via 'moddir' commands
3970 if (default_path != NULL)
3971 kobj_module_path = default_path;
3972 else
3973 default_path = kobj_module_path;
3975 ksyms_arena = vmem_create("ksyms", NULL, 0, sizeof (uint64_t),
3976 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
3978 ctf_arena = vmem_create("ctf", NULL, 0, sizeof (uint_t),
3979 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
3982 * Move symbol tables from boot memory to ksyms_arena.
3984 for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) {
3985 for (lp = *lpp; lp != NULL; lp = lp->modl_next)
3986 kobj_export_module(mod(lp));
3990 caddr_t
3991 kobj_segbrk(caddr_t *spp, size_t size, size_t align, caddr_t limit)
3993 uintptr_t va, pva;
3994 size_t alloc_pgsz = kobj_mmu_pagesize;
3995 size_t alloc_align = BO_NO_ALIGN;
3996 size_t alloc_size;
3999 * If we are using "large" mappings for the kernel,
4000 * request aligned memory from boot using the
4001 * "large" pagesize.
4003 if (lg_pagesize) {
4004 alloc_align = lg_pagesize;
4005 alloc_pgsz = lg_pagesize;
4009 va = ALIGN((uintptr_t)*spp, align);
4010 pva = P2ROUNDUP((uintptr_t)*spp, alloc_pgsz);
4012 * Need more pages?
4014 if (va + size > pva) {
4015 uintptr_t npva;
4017 alloc_size = P2ROUNDUP(size - (pva - va), alloc_pgsz);
4019 * Check for overlapping segments.
4021 if (limit && limit <= *spp + alloc_size) {
4022 return ((caddr_t)0);
4025 npva = (uintptr_t)BOP_ALLOC(ops, (caddr_t)pva,
4026 alloc_size, alloc_align);
4028 if (npva == (uintptr_t)NULL) {
4029 _kobj_printf(ops, "BOP_ALLOC failed, 0x%lx bytes",
4030 alloc_size);
4031 _kobj_printf(ops, " aligned %lx", alloc_align);
4032 _kobj_printf(ops, " at 0x%lx\n", pva);
4033 return (NULL);
4036 *spp = (caddr_t)(va + size);
4038 return ((caddr_t)va);
4042 * Calculate the number of output hash buckets.
4043 * We use the next prime larger than n / 4,
4044 * so the average hash chain is about 4 entries.
4045 * More buckets would just be a waste of memory.
4047 uint_t
4048 kobj_gethashsize(uint_t n)
4050 int f;
4051 int hsize = MAX(n / 4, 2);
4053 for (f = 2; f * f <= hsize; f++)
4054 if (hsize % f == 0)
4055 hsize += f = 1;
4057 return (hsize);
4061 * Get the file size.
4063 * Before root is mounted, files are compressed in the boot_archive ramdisk
4064 * (in the memory). kobj_fstat would return the compressed file size.
4065 * In order to get the uncompressed file size, read the file to the end and
4066 * count its size.
4069 kobj_get_filesize(struct _buf *file, uint64_t *size)
4071 int err = 0;
4072 ssize_t resid;
4073 uint32_t buf;
4075 if (_modrootloaded) {
4076 struct bootstat bst;
4078 if (kobj_fstat(file->_fd, &bst) != 0)
4079 return (EIO);
4080 *size = bst.st_size;
4082 if (file->_iscmp == CH_MAGIC_GZIP) {
4084 * Read the last 4 bytes of the compressed (gzip)
4085 * image to get the size of its uncompressed
4086 * version.
4088 if ((err = vn_rdwr(UIO_READ, (struct vnode *)file->_fd,
4089 (char *)(&buf), 4, (offset_t)(*size - 4),
4090 UIO_SYSSPACE, 0, 0, CRED(), &resid))
4091 != 0) {
4092 _kobj_printf(ops, "kobj_get_filesize: "
4093 "vn_rdwr() failed with error 0x%x\n", err);
4094 return (-1);
4097 *size = (uint64_t)buf;
4099 } else {
4101 #if defined(_OBP)
4102 struct bootstat bsb;
4104 if (file->_iscmp) {
4105 struct comphdr *hdr = kobj_comphdr(file);
4107 *size = hdr->ch_fsize;
4108 } else if (kobj_boot_fstat(file->_fd, &bsb) != 0)
4109 return (EIO);
4110 else
4111 *size = bsb.st_size;
4112 #else
4113 char *buf;
4114 int count;
4115 uint64_t offset = 0;
4117 buf = kmem_alloc(MAXBSIZE, KM_SLEEP);
4118 do {
4119 count = kobj_read_file(file, buf, MAXBSIZE, offset);
4120 if (count < 0) {
4121 kmem_free(buf, MAXBSIZE);
4122 return (EIO);
4124 offset += count;
4125 } while (count == MAXBSIZE);
4126 kmem_free(buf, MAXBSIZE);
4128 *size = offset;
4129 #endif
4132 return (0);
4135 static char *
4136 basename(char *s)
4138 char *p, *q;
4140 q = NULL;
4141 p = s;
4142 do {
4143 if (*p == '/')
4144 q = p;
4145 } while (*p++);
4146 return (q ? q + 1 : s);
4149 void
4150 kobj_stat_get(kobj_stat_t *kp)
4152 *kp = kobj_stat;
4156 kobj_getpagesize()
4158 return (lg_pagesize);
4161 void
4162 kobj_textwin_alloc(struct module *mp)
4164 ASSERT(MUTEX_HELD(&mod_lock));
4166 if (mp->textwin != NULL)
4167 return;
4170 * If the text is not contained in the heap, then it is not contained
4171 * by a writable mapping. (Specifically, it's on the nucleus page.)
4172 * We allocate a read/write mapping for this module's text to allow
4173 * the text to be patched without calling hot_patch_kernel_text()
4174 * (which is quite slow).
4176 if (!vmem_contains(heaptext_arena, mp->text, mp->text_size)) {
4177 uintptr_t text = (uintptr_t)mp->text;
4178 uintptr_t size = (uintptr_t)mp->text_size;
4179 uintptr_t i;
4180 caddr_t va;
4181 size_t sz = ((text + size + PAGESIZE - 1) & PAGEMASK) -
4182 (text & PAGEMASK);
4184 va = mp->textwin_base = vmem_alloc(heap_arena, sz, VM_SLEEP);
4186 for (i = text & PAGEMASK; i < text + size; i += PAGESIZE) {
4187 hat_devload(kas.a_hat, va, PAGESIZE,
4188 hat_getpfnum(kas.a_hat, (caddr_t)i),
4189 PROT_READ | PROT_WRITE,
4190 HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
4191 va += PAGESIZE;
4194 mp->textwin = mp->textwin_base + (text & PAGEOFFSET);
4195 } else {
4196 mp->textwin = mp->text;
4200 void
4201 kobj_textwin_free(struct module *mp)
4203 uintptr_t text = (uintptr_t)mp->text;
4204 uintptr_t tsize = (uintptr_t)mp->text_size;
4205 size_t size = (((text + tsize + PAGESIZE - 1) & PAGEMASK) -
4206 (text & PAGEMASK));
4208 mp->textwin = NULL;
4210 if (mp->textwin_base == NULL)
4211 return;
4213 hat_unload(kas.a_hat, mp->textwin_base, size, HAT_UNLOAD_UNLOCK);
4214 vmem_free(heap_arena, mp->textwin_base, size);
4215 mp->textwin_base = NULL;
4218 static char *
4219 find_libmacro(char *name)
4221 int lmi;
4223 for (lmi = 0; lmi < NLIBMACROS; lmi++) {
4224 if (strcmp(name, libmacros[lmi].lmi_macroname) == 0)
4225 return (libmacros[lmi].lmi_list);
4227 return (NULL);
4231 * Check for $MACRO in tail (string to expand) and expand it in path at pathend
4232 * returns path if successful, else NULL
4233 * Support multiple $MACROs expansion and the first valid path will be returned
4234 * Caller's responsibility to provide enough space in path to expand
4236 char *
4237 expand_libmacro(char *tail, char *path, char *pathend)
4239 char c, *p, *p1, *p2, *path2, *endp;
4240 int diff, lmi, macrolen, valid_macro, more_macro;
4241 struct _buf *file;
4244 * check for $MACROS between nulls or slashes
4246 p = strchr(tail, '$');
4247 if (p == NULL)
4248 return (NULL);
4249 for (lmi = 0; lmi < NLIBMACROS; lmi++) {
4250 macrolen = libmacros[lmi].lmi_macrolen;
4251 if (strncmp(p + 1, libmacros[lmi].lmi_macroname, macrolen) == 0)
4252 break;
4255 valid_macro = 0;
4256 if (lmi < NLIBMACROS) {
4258 * The following checks are used to restrict expansion of
4259 * macros to those that form a full directory/file name
4260 * and to keep the behavior same as before. If this
4261 * restriction is removed or no longer valid in the future,
4262 * the checks below can be deleted.
4264 if ((p == tail) || (*(p - 1) == '/')) {
4265 c = *(p + macrolen + 1);
4266 if (c == '/' || c == '\0')
4267 valid_macro = 1;
4271 if (!valid_macro) {
4272 p2 = strchr(p, '/');
4274 * if no more macro to expand, then just copy whatever left
4275 * and check whether it exists
4277 if (p2 == NULL || strchr(p2, '$') == NULL) {
4278 (void) strcpy(pathend, tail);
4279 if ((file = kobj_open_path(path, 1)) !=
4280 (struct _buf *)-1) {
4281 kobj_close_file(file);
4282 return (path);
4283 } else
4284 return (NULL);
4285 } else {
4287 * copy all chars before '/' and call expand_libmacro()
4288 * again
4290 diff = p2 - tail;
4291 bcopy(tail, pathend, diff);
4292 pathend += diff;
4293 *(pathend) = '\0';
4294 return (expand_libmacro(p2, path, pathend));
4298 more_macro = 0;
4299 if (c != '\0') {
4300 endp = p + macrolen + 1;
4301 if (strchr(endp, '$') != NULL)
4302 more_macro = 1;
4303 } else
4304 endp = NULL;
4307 * copy lmi_list and split it into components.
4308 * then put the part of tail before $MACRO into path
4309 * at pathend
4311 diff = p - tail;
4312 if (diff > 0)
4313 bcopy(tail, pathend, diff);
4314 path2 = pathend + diff;
4315 p1 = libmacros[lmi].lmi_list;
4316 while (p1 && (*p1 != '\0')) {
4317 p2 = strchr(p1, ':');
4318 if (p2) {
4319 diff = p2 - p1;
4320 bcopy(p1, path2, diff);
4321 *(path2 + diff) = '\0';
4322 } else {
4323 diff = strlen(p1);
4324 bcopy(p1, path2, diff + 1);
4326 /* copy endp only if there isn't any more macro to expand */
4327 if (!more_macro && (endp != NULL))
4328 (void) strcat(path2, endp);
4329 file = kobj_open_path(path, 1);
4330 if (file != (struct _buf *)-1) {
4331 kobj_close_file(file);
4333 * if more macros to expand then call expand_libmacro(),
4334 * else return path which has the whole path
4336 if (!more_macro || (expand_libmacro(endp, path,
4337 path2 + diff) != NULL)) {
4338 return (path);
4341 if (p2)
4342 p1 = ++p2;
4343 else
4344 return (NULL);
4346 return (NULL);
4349 static void
4350 tnf_add_notifyunload(kobj_notify_f *fp)
4352 kobj_notify_list_t *entry;
4354 entry = kobj_alloc(sizeof (kobj_notify_list_t), KM_WAIT);
4355 entry->kn_type = KOBJ_NOTIFY_MODUNLOADING;
4356 entry->kn_func = fp;
4357 (void) kobj_notify_add(entry);
4360 /* ARGSUSED */
4361 static void
4362 tnf_unsplice_probes(uint_t what, struct modctl *mod)
4364 tnf_probe_control_t **p;
4365 tnf_tag_data_t **q;
4366 struct module *mp = mod->mod_mp;
4368 if (!(mp->flags & KOBJ_TNF_PROBE))
4369 return;
4371 for (p = &__tnf_probe_list_head; *p; )
4372 if (kobj_addrcheck(mp, (char *)*p) == 0)
4373 *p = (*p)->next;
4374 else
4375 p = &(*p)->next;
4377 for (q = &__tnf_tag_list_head; *q; )
4378 if (kobj_addrcheck(mp, (char *)*q) == 0)
4379 *q = (tnf_tag_data_t *)(*q)->tag_version;
4380 else
4381 q = (tnf_tag_data_t **)&(*q)->tag_version;
4383 tnf_changed_probe_list = 1;
4387 tnf_splice_probes(int boot_load, tnf_probe_control_t *plist,
4388 tnf_tag_data_t *tlist)
4390 int result = 0;
4391 static int add_notify = 1;
4393 if (plist) {
4394 tnf_probe_control_t *pl;
4396 for (pl = plist; pl->next; )
4397 pl = pl->next;
4399 if (!boot_load)
4400 mutex_enter(&mod_lock);
4401 tnf_changed_probe_list = 1;
4402 pl->next = __tnf_probe_list_head;
4403 __tnf_probe_list_head = plist;
4404 if (!boot_load)
4405 mutex_exit(&mod_lock);
4406 result = 1;
4409 if (tlist) {
4410 tnf_tag_data_t *tl;
4412 for (tl = tlist; tl->tag_version; )
4413 tl = (tnf_tag_data_t *)tl->tag_version;
4415 if (!boot_load)
4416 mutex_enter(&mod_lock);
4417 tl->tag_version = (tnf_tag_version_t *)__tnf_tag_list_head;
4418 __tnf_tag_list_head = tlist;
4419 if (!boot_load)
4420 mutex_exit(&mod_lock);
4421 result = 1;
4423 if (!boot_load && result && add_notify) {
4424 tnf_add_notifyunload(tnf_unsplice_probes);
4425 add_notify = 0;
4427 return (result);
4430 char *kobj_file_buf;
4431 int kobj_file_bufsize;
4434 * This code is for the purpose of manually recording which files
4435 * needs to go into the boot archive on any given system.
4437 * To enable the code, set kobj_file_bufsize in /etc/system
4438 * and reboot the system, then use mdb to look at kobj_file_buf.
4440 static void
4441 kobj_record_file(char *filename)
4443 static char *buf;
4444 static int size = 0;
4445 int n;
4447 if (kobj_file_bufsize == 0) /* don't bother */
4448 return;
4450 if (kobj_file_buf == NULL) { /* allocate buffer */
4451 size = kobj_file_bufsize;
4452 buf = kobj_file_buf = kobj_alloc(size, KM_WAIT|KM_TMP);
4455 n = snprintf(buf, size, "%s\n", filename);
4456 if (n > size)
4457 n = size;
4458 size -= n;
4459 buf += n;
4462 static int
4463 kobj_boot_fstat(int fd, struct bootstat *stp)
4465 #if defined(_OBP)
4466 if (!standalone && _ioquiesced)
4467 return (-1);
4468 return (BOP_FSTAT(ops, fd, stp));
4469 #else
4470 return (BRD_FSTAT(bfs_ops, fd, stp));
4471 #endif
4474 static int
4475 kobj_boot_open(char *filename, int flags)
4477 #if defined(_OBP)
4480 * If io via bootops is quiesced, it means boot is no longer
4481 * available to us. We make it look as if we can't open the
4482 * named file - which is reasonably accurate.
4484 if (!standalone && _ioquiesced)
4485 return (-1);
4487 kobj_record_file(filename);
4488 return (BOP_OPEN(filename, flags));
4489 #else /* x86 */
4490 kobj_record_file(filename);
4491 return (BRD_OPEN(bfs_ops, filename, flags));
4492 #endif
4495 static int
4496 kobj_boot_close(int fd)
4498 #if defined(_OBP)
4499 if (!standalone && _ioquiesced)
4500 return (-1);
4502 return (BOP_CLOSE(fd));
4503 #else /* x86 */
4504 return (BRD_CLOSE(bfs_ops, fd));
4505 #endif
4508 /*ARGSUSED*/
4509 static int
4510 kobj_boot_seek(int fd, off_t hi, off_t lo)
4512 #if defined(_OBP)
4513 return (BOP_SEEK(fd, lo) == -1 ? -1 : 0);
4514 #else
4515 return (BRD_SEEK(bfs_ops, fd, lo, SEEK_SET));
4516 #endif
4519 static int
4520 kobj_boot_read(int fd, caddr_t buf, size_t size)
4522 #if defined(_OBP)
4523 return (BOP_READ(fd, buf, size));
4524 #else
4525 return (BRD_READ(bfs_ops, fd, buf, size));
4526 #endif
4529 static int
4530 kobj_boot_compinfo(int fd, struct compinfo *cb)
4532 return (boot_compinfo(fd, cb));
4536 * Check if the file is compressed (for now we handle only gzip).
4537 * It returns CH_MAGIC_GZIP if the file is compressed and 0 otherwise.
4539 static int
4540 kobj_is_compressed(intptr_t fd)
4542 struct vnode *vp = (struct vnode *)fd;
4543 ssize_t resid;
4544 uint16_t magic_buf;
4545 int err = 0;
4547 if ((err = vn_rdwr(UIO_READ, vp, (caddr_t)((intptr_t)&magic_buf),
4548 sizeof (magic_buf), (offset_t)(0),
4549 UIO_SYSSPACE, 0, 0, CRED(), &resid)) != 0) {
4551 _kobj_printf(ops, "kobj_is_compressed: vn_rdwr() failed, "
4552 "error code 0x%x\n", err);
4553 return (0);
4556 if (magic_buf == CH_MAGIC_GZIP)
4557 return (CH_MAGIC_GZIP);
4559 return (0);