1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25 /*
26 * Copyright 2011 Bayard G. Bell <buffer.g.overflow@gmail.com>.
27 * All rights reserved. Use is subject to license terms.
28 * Copyright (c) 2018, Joyent, Inc.
29 */
30
31 /*
32 * Kernel's linker/loader
33 */
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/sysmacros.h>
38 #include <sys/systm.h>
39 #include <sys/user.h>
40 #include <sys/kmem.h>
41 #include <sys/reboot.h>
42 #include <sys/bootconf.h>
43 #include <sys/debug.h>
44 #include <sys/uio.h>
45 #include <sys/file.h>
46 #include <sys/vnode.h>
47 #include <sys/user.h>
48 #include <sys/mman.h>
49 #include <vm/as.h>
50 #include <vm/seg_kp.h>
51 #include <vm/seg_kmem.h>
52 #include <sys/elf.h>
53 #include <sys/elf_notes.h>
54 #include <sys/vmsystm.h>
55 #include <sys/kdi.h>
56 #include <sys/atomic.h>
57 #include <sys/kmdb.h>
58
59 #include <sys/link.h>
60 #include <sys/kobj.h>
61 #include <sys/ksyms.h>
62 #include <sys/disp.h>
63 #include <sys/modctl.h>
64 #include <sys/varargs.h>
65 #include <sys/kstat.h>
66 #include <sys/kobj_impl.h>
67 #include <sys/fs/decomp.h>
68 #include <sys/callb.h>
69 #include <sys/cmn_err.h>
70 #include <sys/tnf_probe.h>
71 #include <sys/zmod.h>
72
73 #include <krtld/reloc.h>
74 #include <krtld/kobj_kdi.h>
75 #include <sys/sha1.h>
76 #include <sys/crypto/elfsign.h>
77
78 #if !defined(_OBP)
79 #include <sys/bootvfs.h>
80 #endif
81
82 /*
83 * do_symbols() error codes
84 */
85 #define DOSYM_UNDEF -1 /* undefined symbol */
86 #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */
87
88 #if !defined(_OBP)
89 static void synthetic_bootaux(char *, val_t *);
90 #endif
91
92 static struct module *load_exec(val_t *, char *);
93 static void load_linker(val_t *);
94 static struct modctl *add_primary(const char *filename, int);
95 static int bind_primary(val_t *, int);
96 static int load_primary(struct module *, int);
97 static int load_kmdb(val_t *);
98 static int get_progbits(struct module *, struct _buf *);
99 static int get_syms(struct module *, struct _buf *);
100 static int get_ctf(struct module *, struct _buf *);
101 static void get_signature(struct module *, struct _buf *);
102 static int do_common(struct module *);
103 static void add_dependent(struct module *, struct module *);
104 static int do_dependents(struct modctl *, char *, size_t);
105 static int do_symbols(struct module *, Elf64_Addr);
106 static void module_assign(struct modctl *, struct module *);
107 static void free_module_data(struct module *);
108 static char *depends_on(struct module *);
109 static char *getmodpath(const char *);
110 static char *basename(char *);
111 static void attr_val(val_t *);
112 static char *find_libmacro(char *);
113 static char *expand_libmacro(char *, char *, char *);
114 static int read_bootflags(void);
115 static int kobj_comp_setup(struct _buf *, struct compinfo *);
116 static int kobj_uncomp_blk(struct _buf *, caddr_t, uint_t);
117 static int kobj_read_blks(struct _buf *, caddr_t, uint_t, uint_t);
118 static int kobj_boot_open(char *, int);
119 static int kobj_boot_close(int);
120 static int kobj_boot_seek(int, off_t, off_t);
121 static int kobj_boot_read(int, caddr_t, size_t);
122 static int kobj_boot_fstat(int, struct bootstat *);
123 static int kobj_boot_compinfo(int, struct compinfo *);
124
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 *);
128
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);
133
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 *);
137
138 #if !defined(_OBP)
139 extern int kobj_boot_mountroot(void);
140 #endif
141
142 static void tnf_unsplice_probes(uint_t, struct modctl *);
143 extern tnf_probe_control_t *__tnf_probe_list_head;
144 extern tnf_tag_data_t *__tnf_tag_list_head;
145
146 extern int modrootloaded;
147 extern int swaploaded;
148 extern int bop_io_quiesced;
149 extern int last_module_id;
150
151 extern char stubs_base[];
152 extern char stubs_end[];
153
154 #ifdef KOBJ_DEBUG
155 /*
156 * Values that can be or'd in to kobj_debug and their effects:
157 *
158 * D_DEBUG - misc. debugging information.
159 * D_SYMBOLS - list symbols and their values as they are entered
160 * into the hash table
161 * D_RELOCATIONS - display relocation processing information
162 * D_LOADING - display information about each module as it
163 * is loaded.
164 */
165 int kobj_debug = 0;
166
167 #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \
168 (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s))
169 #else
170 #define KOBJ_MARK(s) /* discard */
171 #endif
172
173 #define MODPATH_PROPNAME "module-path"
174
175 #ifdef MODDIR_SUFFIX
176 static char slash_moddir_suffix_slash[] = MODDIR_SUFFIX "/";
177 #else
178 #define slash_moddir_suffix_slash ""
179 #endif
180
181 #define _moddebug get_weakish_int(&moddebug)
182 #define _modrootloaded get_weakish_int(&modrootloaded)
183 #define _swaploaded get_weakish_int(&swaploaded)
184 #define _ioquiesced get_weakish_int(&bop_io_quiesced)
185
186 #define mod(X) (struct module *)((X)->modl_modp->mod_mp)
187
188 void *romp; /* rom vector (opaque to us) */
189 struct bootops *ops; /* bootops vector */
190 void *dbvec; /* debug vector */
191
192 /*
193 * kobjopen thread control structure
194 */
195 struct kobjopen_tctl {
196 ksema_t sema;
197 char *name; /* name of file */
198 struct vnode *vp; /* vnode return from vn_open() */
199 int Errno; /* error return from vnopen */
200 };
201
202 /*
203 * Structure for defining dynamically expandable library macros
204 */
205
206 struct lib_macro_info {
207 char *lmi_list; /* ptr to list of possible choices */
208 char *lmi_macroname; /* pointer to macro name */
209 ushort_t lmi_ba_index; /* index into bootaux vector */
210 ushort_t lmi_macrolen; /* macro length */
211 } libmacros[] = {
212 { NULL, "CPU", BA_CPU, 0 },
213 { NULL, "MMU", BA_MMU, 0 }
214 };
215
216 #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info)
217
218 char *boot_cpu_compatible_list; /* make $CPU available */
219
220 char *kobj_module_path; /* module search path */
221 vmem_t *text_arena; /* module text arena */
222 static vmem_t *data_arena; /* module data & bss arena */
223 static vmem_t *ctf_arena; /* CTF debug data arena */
224 static struct modctl *kobj_modules = NULL; /* modules loaded */
225 int kobj_mmu_pagesize; /* system pagesize */
226 static int lg_pagesize; /* "large" pagesize */
227 static int kobj_last_module_id = 0; /* id assignment */
228 static kmutex_t kobj_lock; /* protects mach memory list */
229
230 /*
231 * The following functions have been implemented by the kernel.
232 * However, many 3rd party drivers provide their own implementations
233 * of these functions. When such drivers are loaded, messages
234 * indicating that these symbols have been multiply defined will be
235 * emitted to the console. To avoid alarming customers for no good
236 * reason, we simply suppress such warnings for the following set of
237 * functions.
238 */
239 static char *suppress_sym_list[] =
240 {
241 "strstr",
242 "strncat",
243 "strlcat",
244 "strlcpy",
245 "strspn",
246 "memcpy",
247 "memset",
248 "memmove",
249 "memcmp",
250 "memchr",
251 "__udivdi3",
252 "__divdi3",
253 "__umoddi3",
254 "__moddi3",
255 NULL /* This entry must exist */
256 };
257
258 /* indexed by KOBJ_NOTIFY_* */
259 static kobj_notify_list_t *kobj_notifiers[KOBJ_NOTIFY_MAX + 1];
260
261 /*
262 * TNF probe management globals
263 */
264 tnf_probe_control_t *__tnf_probe_list_head = NULL;
265 tnf_tag_data_t *__tnf_tag_list_head = NULL;
266 int tnf_changed_probe_list = 0;
267
268 /*
269 * Prefix for statically defined tracing (SDT) DTrace probes.
270 */
271 const char *sdt_prefix = "__dtrace_probe_";
272
273 /*
274 * Beginning and end of the kernel's dynamic text/data segments.
275 */
276 static caddr_t _text;
277 static caddr_t _etext;
278 static caddr_t _data;
279
280 /*
281 * The sparc linker doesn't create a memory location
282 * for a variable named _edata, so _edata can only be
283 * referred to, not modified. krtld needs a static
284 * variable to modify it - within krtld, of course -
285 * outside of krtld, e_data is used in all kernels.
286 */
287 #if defined(__sparc)
288 static caddr_t _edata;
289 #else
290 extern caddr_t _edata;
291 #endif
292
293 Addr dynseg = 0; /* load address of "dynamic" segment */
294 size_t dynsize; /* "dynamic" segment size */
295
296
297 int standalone = 1; /* an unwholey kernel? */
298 int use_iflush; /* iflush after relocations */
299
300 /*
301 * _kobj_printf() and _vkobj_printf()
302 *
303 * Common printf function pointer. Can handle only one conversion
304 * specification in the format string. Some of the functions invoked
305 * through this function pointer cannot handle more that one conversion
306 * specification in the format string.
307 */
308 void (*_kobj_printf)(void *, const char *, ...); /* printf routine */
309 void (*_vkobj_printf)(void *, const char *, va_list); /* vprintf routine */
310
311 /*
312 * Standalone function pointers for use within krtld.
313 * Many platforms implement optimized platmod versions of
314 * utilities such as bcopy and any such are not yet available
315 * until the kernel is more completely stitched together.
316 * See kobj_impl.h
317 */
318 void (*kobj_bcopy)(const void *, void *, size_t);
319 void (*kobj_bzero)(void *, size_t);
320 size_t (*kobj_strlcat)(char *, const char *, size_t);
321
322 static kobj_stat_t kobj_stat;
323
324 #define MINALIGN 8 /* at least a double-word */
325
326 int
327 get_weakish_int(int *ip)
328 {
329 if (standalone)
330 return (0);
331 return (ip == NULL ? 0 : *ip);
332 }
333
334 static void *
335 get_weakish_pointer(void **ptrp)
336 {
337 if (standalone)
338 return (0);
339 return (ptrp == NULL ? 0 : *ptrp);
340 }
341
342 /*
343 * XXX fix dependencies on "kernel"; this should work
344 * for other standalone binaries as well.
345 *
346 * XXX Fix hashing code to use one pointer to
347 * hash entries.
348 * |----------|
349 * | nbuckets |
350 * |----------|
351 * | nchains |
352 * |----------|
353 * | bucket[] |
354 * |----------|
355 * | chain[] |
356 * |----------|
357 */
358
359 /*
360 * Load, bind and relocate all modules that
361 * form the primary kernel. At this point, our
362 * externals have not been relocated.
363 */
364 void
365 kobj_init(
366 void *romvec,
367 void *dvec,
368 struct bootops *bootvec,
369 val_t *bootaux)
370 {
371 struct module *mp;
372 struct modctl *modp;
373 Addr entry;
374 char filename[MAXPATHLEN];
375
376 /*
377 * Save these to pass on to
378 * the booted standalone.
379 */
380 romp = romvec;
381 dbvec = dvec;
382
383 ops = bootvec;
384 kobj_setup_standalone_vectors();
385
386 KOBJ_MARK("Entered kobj_init()");
387
388 (void) BOP_GETPROP(ops, "whoami", filename);
389
390 /*
391 * We don't support standalone debuggers anymore. The use of kadb
392 * will interfere with the later use of kmdb. Let the user mend
393 * their ways now. Users will reach this message if they still
394 * have the kadb binary on their system (perhaps they used an old
395 * bfu, or maybe they intentionally copied it there) and have
396 * specified its use in a way that eluded our checking in the boot
397 * program.
398 */
399 if (dvec != NULL) {
400 _kobj_printf(ops, "\nWARNING: Standalone debuggers such as "
401 "kadb are no longer supported\n\n");
402 goto fail;
403 }
404
405 #if defined(_OBP)
406 /*
407 * OBP allows us to read both the ramdisk and
408 * the underlying root fs when root is a disk.
409 * This can lower incidences of unbootable systems
410 * when the archive is out-of-date with the /etc
411 * state files.
412 */
413 if (BOP_MOUNTROOT() != BOOT_SVC_OK) {
414 _kobj_printf(ops, "can't mount boot fs\n");
415 goto fail;
416 }
417 #else
418 {
419 /* on x86, we always boot with a ramdisk */
420 (void) kobj_boot_mountroot();
421
422 /*
423 * Now that the ramdisk is mounted, finish boot property
424 * initialization.
425 */
426 boot_prop_finish();
427 }
428
429 #if !defined(_UNIX_KRTLD)
430 /*
431 * 'unix' is linked together with 'krtld' into one executable and
432 * the early boot code does -not- hand us any of the dynamic metadata
433 * about the executable. In particular, it does not read in, map or
434 * otherwise look at the program headers. We fake all that up now.
435 *
436 * We do this early as DTrace static probes and tnf probes both call
437 * undefined references. We have to process those relocations before
438 * calling any of them.
439 *
440 * OBP tells kobj_start() where the ELF image is in memory, so it
441 * synthesized bootaux before kobj_init() was called
442 */
443 if (bootaux[BA_PHDR].ba_ptr == NULL)
444 synthetic_bootaux(filename, bootaux);
445
446 #endif /* !_UNIX_KRTLD */
447 #endif /* _OBP */
448
449 /*
450 * Save the interesting attribute-values
451 * (scanned by kobj_boot).
452 */
453 attr_val(bootaux);
454
455 /*
456 * Set the module search path.
457 */
458 kobj_module_path = getmodpath(filename);
459
460 boot_cpu_compatible_list = find_libmacro("CPU");
461
462 /*
463 * These two modules have actually been
464 * loaded by boot, but we finish the job
465 * by introducing them into the world of
466 * loadable modules.
467 */
468
469 mp = load_exec(bootaux, filename);
470 load_linker(bootaux);
471
472 /*
473 * Load all the primary dependent modules.
474 */
475 if (load_primary(mp, KOBJ_LM_PRIMARY) == -1)
476 goto fail;
477
478 /*
479 * Glue it together.
480 */
481 if (bind_primary(bootaux, KOBJ_LM_PRIMARY) == -1)
482 goto fail;
483
484 entry = bootaux[BA_ENTRY].ba_val;
485
486 /*
487 * Get the boot flags
488 */
489 bootflags(ops);
490
491 if (boothowto & RB_VERBOSE)
492 kobj_lm_dump(KOBJ_LM_PRIMARY);
493
494 kobj_kdi_init();
495
496 if (boothowto & RB_KMDB) {
497 if (load_kmdb(bootaux) < 0)
498 goto fail;
499 }
500
501 /*
502 * Post setup.
503 */
504 s_text = _text;
505 e_text = _etext;
506 s_data = _data;
507 e_data = _edata;
508
509 kobj_sync_instruction_memory(s_text, e_text - s_text);
510
511 #ifdef KOBJ_DEBUG
512 if (kobj_debug & D_DEBUG)
513 _kobj_printf(ops,
514 "krtld: transferring control to: 0x%p\n", entry);
515 #endif
516
517 /*
518 * Make sure the mod system knows about the modules already loaded.
519 */
520 last_module_id = kobj_last_module_id;
521 bcopy(kobj_modules, &modules, sizeof (modules));
522 modp = &modules;
523 do {
524 if (modp->mod_next == kobj_modules)
525 modp->mod_next = &modules;
526 if (modp->mod_prev == kobj_modules)
527 modp->mod_prev = &modules;
528 } while ((modp = modp->mod_next) != &modules);
529
530 standalone = 0;
531
532 #ifdef KOBJ_DEBUG
533 if (kobj_debug & D_DEBUG)
534 _kobj_printf(ops,
535 "krtld: really transferring control to: 0x%p\n", entry);
536 #endif
537
538 /* restore printf/bcopy/bzero vectors before returning */
539 kobj_restore_vectors();
540
541 #if defined(_DBOOT)
542 /*
543 * krtld was called from a dboot ELF section, the embedded
544 * dboot code contains the real entry via bootaux
545 */
546 exitto((caddr_t)entry);
547 #else
548 /*
549 * krtld was directly called from startup
550 */
551 return;
552 #endif
553
554 fail:
555
556 _kobj_printf(ops, "krtld: error during initial load/link phase\n");
557
558 #if !defined(_UNIX_KRTLD)
559 _kobj_printf(ops, "\n");
560 _kobj_printf(ops, "krtld could neither locate nor resolve symbols"
561 " for:\n");
562 _kobj_printf(ops, " %s\n", filename);
563 _kobj_printf(ops, "in the boot archive. Please verify that this"
564 " file\n");
565 _kobj_printf(ops, "matches what is found in the boot archive.\n");
566 _kobj_printf(ops, "You may need to boot using the Solaris failsafe to"
567 " fix this.\n");
568 bop_panic("Unable to boot");
569 #endif
570 }
571
572 #if !defined(_UNIX_KRTLD) && !defined(_OBP)
573 /*
574 * Synthesize additional metadata that describes the executable if
575 * krtld's caller didn't do it.
576 *
577 * (When the dynamic executable has an interpreter, the boot program
578 * does all this for us. Where we don't have an interpreter, (or a
579 * even a boot program, perhaps) we have to do this for ourselves.)
580 */
581 static void
582 synthetic_bootaux(char *filename, val_t *bootaux)
583 {
584 Ehdr ehdr;
585 caddr_t phdrbase;
586 struct _buf *file;
587 int i, n;
588
589 /*
590 * Elf header
591 */
592 KOBJ_MARK("synthetic_bootaux()");
593 KOBJ_MARK(filename);
594 file = kobj_open_file(filename);
595 if (file == (struct _buf *)-1) {
596 _kobj_printf(ops, "krtld: failed to open '%s'\n", filename);
597 return;
598 }
599 KOBJ_MARK("reading program headers");
600 if (kobj_read_file(file, (char *)&ehdr, sizeof (ehdr), 0) < 0) {
601 _kobj_printf(ops, "krtld: %s: failed to read ehder\n",
602 filename);
603 return;
604 }
605
606 /*
607 * Program headers
608 */
609 bootaux[BA_PHNUM].ba_val = ehdr.e_phnum;
610 bootaux[BA_PHENT].ba_val = ehdr.e_phentsize;
611 n = ehdr.e_phentsize * ehdr.e_phnum;
612
613 phdrbase = kobj_alloc(n, KM_WAIT | KM_TMP);
614
615 if (kobj_read_file(file, phdrbase, n, ehdr.e_phoff) < 0) {
616 _kobj_printf(ops, "krtld: %s: failed to read phdrs\n",
617 filename);
618 return;
619 }
620 bootaux[BA_PHDR].ba_ptr = phdrbase;
621 kobj_close_file(file);
622 KOBJ_MARK("closed file");
623
624 /*
625 * Find the dynamic section address
626 */
627 for (i = 0; i < ehdr.e_phnum; i++) {
628 Phdr *phdr = (Phdr *)(phdrbase + ehdr.e_phentsize * i);
629
630 if (phdr->p_type == PT_DYNAMIC) {
631 bootaux[BA_DYNAMIC].ba_ptr = (void *)phdr->p_vaddr;
632 break;
633 }
634 }
635 KOBJ_MARK("synthetic_bootaux() done");
636 }
637 #endif /* !_UNIX_KRTLD && !_OBP */
638
639 /*
640 * Set up any global information derived
641 * from attribute/values in the boot or
642 * aux vector.
643 */
644 static void
645 attr_val(val_t *bootaux)
646 {
647 Phdr *phdr;
648 int phnum, phsize;
649 int i;
650
651 KOBJ_MARK("attr_val()");
652 kobj_mmu_pagesize = bootaux[BA_PAGESZ].ba_val;
653 lg_pagesize = bootaux[BA_LPAGESZ].ba_val;
654 use_iflush = bootaux[BA_IFLUSH].ba_val;
655
656 phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr;
657 phnum = bootaux[BA_PHNUM].ba_val;
658 phsize = bootaux[BA_PHENT].ba_val;
659 for (i = 0; i < phnum; i++) {
660 phdr = (Phdr *)(bootaux[BA_PHDR].ba_val + i * phsize);
661
662 if (phdr->p_type != PT_LOAD) {
663 continue;
664 }
665 /*
666 * Bounds of the various segments.
667 */
668 if (!(phdr->p_flags & PF_X)) {
669 #if defined(_RELSEG)
670 /*
671 * sparc kernel puts the dynamic info
672 * into a separate segment, which is
673 * free'd in bop_fini()
674 */
675 ASSERT(phdr->p_vaddr != 0);
676 dynseg = phdr->p_vaddr;
677 dynsize = phdr->p_memsz;
678 #else
679 ASSERT(phdr->p_vaddr == 0);
680 #endif
681 } else {
682 if (phdr->p_flags & PF_W) {
683 _data = (caddr_t)phdr->p_vaddr;
684 _edata = _data + phdr->p_memsz;
685 } else {
686 _text = (caddr_t)phdr->p_vaddr;
687 _etext = _text + phdr->p_memsz;
688 }
689 }
690 }
691
692 /* To do the kobj_alloc, _edata needs to be set. */
693 for (i = 0; i < NLIBMACROS; i++) {
694 if (bootaux[libmacros[i].lmi_ba_index].ba_ptr != NULL) {
695 libmacros[i].lmi_list = kobj_alloc(
696 strlen(bootaux[libmacros[i].lmi_ba_index].ba_ptr) +
697 1, KM_WAIT);
698 (void) strcpy(libmacros[i].lmi_list,
699 bootaux[libmacros[i].lmi_ba_index].ba_ptr);
700 }
701 libmacros[i].lmi_macrolen = strlen(libmacros[i].lmi_macroname);
702 }
703 }
704
705 /*
706 * Set up the booted executable.
707 */
708 static struct module *
709 load_exec(val_t *bootaux, char *filename)
710 {
711 struct modctl *cp;
712 struct module *mp;
713 Dyn *dyn;
714 Sym *sp;
715 int i, lsize, osize, nsize, allocsize;
716 char *libname, *tmp;
717 char path[MAXPATHLEN];
718
719 #ifdef KOBJ_DEBUG
720 if (kobj_debug & D_DEBUG)
721 _kobj_printf(ops, "module path '%s'\n", kobj_module_path);
722 #endif
723
724 KOBJ_MARK("add_primary");
725 cp = add_primary(filename, KOBJ_LM_PRIMARY);
726
727 KOBJ_MARK("struct module");
728 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
729 cp->mod_mp = mp;
730
731 /*
732 * We don't have the following information
733 * since this module is an executable and not
734 * a relocatable .o.
735 */
736 mp->symtbl_section = 0;
737 mp->shdrs = NULL;
738 mp->strhdr = NULL;
739
740 /*
741 * Since this module is the only exception,
742 * we cons up some section headers.
743 */
744 KOBJ_MARK("symhdr");
745 mp->symhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT);
746
747 KOBJ_MARK("strhdr");
748 mp->strhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT);
749
750 mp->symhdr->sh_type = SHT_SYMTAB;
751 mp->strhdr->sh_type = SHT_STRTAB;
752 /*
753 * Scan the dynamic structure.
754 */
755 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr;
756 dyn->d_tag != DT_NULL; dyn++) {
757 switch (dyn->d_tag) {
758 case DT_SYMTAB:
759 mp->symspace = mp->symtbl = (char *)dyn->d_un.d_ptr;
760 mp->symhdr->sh_addr = dyn->d_un.d_ptr;
761 break;
762 case DT_HASH:
763 mp->nsyms = *((uint_t *)dyn->d_un.d_ptr + 1);
764 mp->hashsize = *(uint_t *)dyn->d_un.d_ptr;
765 break;
766 case DT_STRTAB:
767 mp->strings = (char *)dyn->d_un.d_ptr;
768 mp->strhdr->sh_addr = dyn->d_un.d_ptr;
769 break;
770 case DT_STRSZ:
771 mp->strhdr->sh_size = dyn->d_un.d_val;
772 break;
773 case DT_SYMENT:
774 mp->symhdr->sh_entsize = dyn->d_un.d_val;
775 break;
776 }
777 }
778
779 /*
780 * Collapse any DT_NEEDED entries into one string.
781 */
782 nsize = osize = 0;
783 allocsize = MAXPATHLEN;
784
785 KOBJ_MARK("depends_on");
786 mp->depends_on = kobj_alloc(allocsize, KM_WAIT);
787
788 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr;
789 dyn->d_tag != DT_NULL; dyn++)
790 if (dyn->d_tag == DT_NEEDED) {
791 char *_lib;
792
793 libname = mp->strings + dyn->d_un.d_val;
794 if (strchr(libname, '$') != NULL) {
795 if ((_lib = expand_libmacro(libname,
796 path, path)) != NULL)
797 libname = _lib;
798 else
799 _kobj_printf(ops, "krtld: "
800 "load_exec: fail to "
801 "expand %s\n", libname);
802 }
803 lsize = strlen(libname);
804 nsize += lsize;
805 if (nsize + 1 > allocsize) {
806 KOBJ_MARK("grow depends_on");
807 tmp = kobj_alloc(allocsize + MAXPATHLEN,
808 KM_WAIT);
809 bcopy(mp->depends_on, tmp, osize);
810 kobj_free(mp->depends_on, allocsize);
811 mp->depends_on = tmp;
812 allocsize += MAXPATHLEN;
813 }
814 bcopy(libname, mp->depends_on + osize, lsize);
815 *(mp->depends_on + nsize) = ' '; /* separate */
816 nsize++;
817 osize = nsize;
818 }
819 if (nsize) {
820 mp->depends_on[nsize - 1] = '\0'; /* terminate the string */
821 /*
822 * alloc with exact size and copy whatever it got over
823 */
824 KOBJ_MARK("realloc depends_on");
825 tmp = kobj_alloc(nsize, KM_WAIT);
826 bcopy(mp->depends_on, tmp, nsize);
827 kobj_free(mp->depends_on, allocsize);
828 mp->depends_on = tmp;
829 } else {
830 kobj_free(mp->depends_on, allocsize);
831 mp->depends_on = NULL;
832 }
833
834 mp->flags = KOBJ_EXEC|KOBJ_PRIM; /* NOT a relocatable .o */
835 mp->symhdr->sh_size = mp->nsyms * mp->symhdr->sh_entsize;
836 /*
837 * We allocate our own table since we don't
838 * hash undefined references.
839 */
840 KOBJ_MARK("chains");
841 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT);
842 KOBJ_MARK("buckets");
843 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT);
844
845 mp->text = _text;
846 mp->data = _data;
847
848 mp->text_size = _etext - _text;
849 mp->data_size = _edata - _data;
850
851 cp->mod_text = mp->text;
852 cp->mod_text_size = mp->text_size;
853
854 mp->filename = cp->mod_filename;
855
856 #ifdef KOBJ_DEBUG
857 if (kobj_debug & D_LOADING) {
858 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
859 _kobj_printf(ops, "\ttext: 0x%p", mp->text);
860 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
861 _kobj_printf(ops, "\tdata: 0x%p", mp->data);
862 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
863 }
864 #endif /* KOBJ_DEBUG */
865
866 /*
867 * Insert symbols into the hash table.
868 */
869 for (i = 0; i < mp->nsyms; i++) {
870 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
871
872 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
873 continue;
874 #if defined(__sparc)
875 /*
876 * Register symbols are ignored in the kernel
877 */
878 if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER)
879 continue;
880 #endif /* __sparc */
881
882 sym_insert(mp, mp->strings + sp->st_name, i);
883 }
884
885 KOBJ_MARK("load_exec done");
886 return (mp);
887 }
888
889 /*
890 * Set up the linker module (if it's compiled in, LDNAME is NULL)
891 */
892 static void
893 load_linker(val_t *bootaux)
894 {
895 struct module *kmp = (struct module *)kobj_modules->mod_mp;
896 struct module *mp;
897 struct modctl *cp;
898 int i;
899 Shdr *shp;
900 Sym *sp;
901 int shsize;
902 char *dlname = (char *)bootaux[BA_LDNAME].ba_ptr;
903
904 /*
905 * On some architectures, krtld is compiled into the kernel.
906 */
907 if (dlname == NULL)
908 return;
909
910 cp = add_primary(dlname, KOBJ_LM_PRIMARY);
911
912 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
913
914 cp->mod_mp = mp;
915 mp->hdr = *(Ehdr *)bootaux[BA_LDELF].ba_ptr;
916 shsize = mp->hdr.e_shentsize * mp->hdr.e_shnum;
917 mp->shdrs = kobj_alloc(shsize, KM_WAIT);
918 bcopy(bootaux[BA_LDSHDR].ba_ptr, mp->shdrs, shsize);
919
920 for (i = 1; i < (int)mp->hdr.e_shnum; i++) {
921 shp = (Shdr *)(mp->shdrs + (i * mp->hdr.e_shentsize));
922
923 if (shp->sh_flags & SHF_ALLOC) {
924 if (shp->sh_flags & SHF_WRITE) {
925 if (mp->data == NULL)
926 mp->data = (char *)shp->sh_addr;
927 } else if (mp->text == NULL) {
928 mp->text = (char *)shp->sh_addr;
929 }
930 }
931 if (shp->sh_type == SHT_SYMTAB) {
932 mp->symtbl_section = i;
933 mp->symhdr = shp;
934 mp->symspace = mp->symtbl = (char *)shp->sh_addr;
935 }
936 }
937 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize;
938 mp->flags = KOBJ_INTERP|KOBJ_PRIM;
939 mp->strhdr = (Shdr *)
940 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize);
941 mp->strings = (char *)mp->strhdr->sh_addr;
942 mp->hashsize = kobj_gethashsize(mp->nsyms);
943
944 mp->symsize = mp->symhdr->sh_size + mp->strhdr->sh_size + sizeof (int) +
945 (mp->hashsize + mp->nsyms) * sizeof (symid_t);
946
947 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT);
948 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT);
949
950 mp->bss = bootaux[BA_BSS].ba_val;
951 mp->bss_align = 0; /* pre-aligned during allocation */
952 mp->bss_size = (uintptr_t)_edata - mp->bss;
953 mp->text_size = _etext - mp->text;
954 mp->data_size = _edata - mp->data;
955 mp->filename = cp->mod_filename;
956 cp->mod_text = mp->text;
957 cp->mod_text_size = mp->text_size;
958
959 /*
960 * Now that we've figured out where the linker is,
961 * set the limits for the booted object.
962 */
963 kmp->text_size = (size_t)(mp->text - kmp->text);
964 kmp->data_size = (size_t)(mp->data - kmp->data);
965 kobj_modules->mod_text_size = kmp->text_size;
966
967 #ifdef KOBJ_DEBUG
968 if (kobj_debug & D_LOADING) {
969 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
970 _kobj_printf(ops, "\ttext:0x%p", mp->text);
971 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
972 _kobj_printf(ops, "\tdata:0x%p", mp->data);
973 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
974 }
975 #endif /* KOBJ_DEBUG */
976
977 /*
978 * Insert the symbols into the hash table.
979 */
980 for (i = 0; i < mp->nsyms; i++) {
981 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
982
983 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
984 continue;
985 if (ELF_ST_BIND(sp->st_info) == STB_GLOBAL) {
986 if (sp->st_shndx == SHN_COMMON)
987 sp->st_shndx = SHN_ABS;
988 }
989 sym_insert(mp, mp->strings + sp->st_name, i);
990 }
991
992 }
993
994 static kobj_notify_list_t **
995 kobj_notify_lookup(uint_t type)
996 {
997 ASSERT(type != 0 && type < sizeof (kobj_notifiers) /
998 sizeof (kobj_notify_list_t *));
999
1000 return (&kobj_notifiers[type]);
1001 }
1002
1003 int
1004 kobj_notify_add(kobj_notify_list_t *knp)
1005 {
1006 kobj_notify_list_t **knl;
1007
1008 knl = kobj_notify_lookup(knp->kn_type);
1009
1010 knp->kn_next = NULL;
1011 knp->kn_prev = NULL;
1012
1013 mutex_enter(&kobj_lock);
1014
1015 if (*knl != NULL) {
1016 (*knl)->kn_prev = knp;
1017 knp->kn_next = *knl;
1018 }
1019 (*knl) = knp;
1020
1021 mutex_exit(&kobj_lock);
1022 return (0);
1023 }
1024
1025 int
1026 kobj_notify_remove(kobj_notify_list_t *knp)
1027 {
1028 kobj_notify_list_t **knl = kobj_notify_lookup(knp->kn_type);
1029 kobj_notify_list_t *tknp;
1030
1031 mutex_enter(&kobj_lock);
1032
1033 /* LINTED */
1034 if (tknp = knp->kn_next)
1035 tknp->kn_prev = knp->kn_prev;
1036
1037 /* LINTED */
1038 if (tknp = knp->kn_prev)
1039 tknp->kn_next = knp->kn_next;
1040 else
1041 *knl = knp->kn_next;
1042
1043 mutex_exit(&kobj_lock);
1044
1045 return (0);
1046 }
1047
1048 /*
1049 * Notify all interested callbacks of a specified change in module state.
1050 */
1051 static void
1052 kobj_notify(int type, struct modctl *modp)
1053 {
1054 kobj_notify_list_t *knp;
1055
1056 if (modp->mod_loadflags & MOD_NONOTIFY || standalone)
1057 return;
1058
1059 mutex_enter(&kobj_lock);
1060
1061 for (knp = *(kobj_notify_lookup(type)); knp != NULL; knp = knp->kn_next)
1062 knp->kn_func(type, modp);
1063
1064 /*
1065 * KDI notification must be last (it has to allow for work done by the
1066 * other notification callbacks), so we call it manually.
1067 */
1068 kobj_kdi_mod_notify(type, modp);
1069
1070 mutex_exit(&kobj_lock);
1071 }
1072
1073 /*
1074 * Create the module path.
1075 */
1076 static char *
1077 getmodpath(const char *filename)
1078 {
1079 char *path = kobj_zalloc(MAXPATHLEN, KM_WAIT);
1080
1081 /*
1082 * Platform code gets first crack, then add
1083 * the default components
1084 */
1085 mach_modpath(path, filename);
1086 if (*path != '\0')
1087 (void) strcat(path, " ");
1088 return (strcat(path, MOD_DEFPATH));
1089 }
1090
1091 static struct modctl *
1092 add_primary(const char *filename, int lmid)
1093 {
1094 struct modctl *cp;
1095
1096 cp = kobj_zalloc(sizeof (struct modctl), KM_WAIT);
1097
1098 cp->mod_filename = kobj_alloc(strlen(filename) + 1, KM_WAIT);
1099
1100 /*
1101 * For symbol lookup, we assemble our own
1102 * modctl list of the primary modules.
1103 */
1104
1105 (void) strcpy(cp->mod_filename, filename);
1106 cp->mod_modname = basename(cp->mod_filename);
1107
1108 /* set values for modinfo assuming that the load will work */
1109 cp->mod_prim = 1;
1110 cp->mod_loaded = 1;
1111 cp->mod_installed = 1;
1112 cp->mod_loadcnt = 1;
1113 cp->mod_loadflags = MOD_NOAUTOUNLOAD;
1114
1115 cp->mod_id = kobj_last_module_id++;
1116
1117 /*
1118 * Link the module in. We'll pass this info on
1119 * to the mod squad later.
1120 */
1121 if (kobj_modules == NULL) {
1122 kobj_modules = cp;
1123 cp->mod_prev = cp->mod_next = cp;
1124 } else {
1125 cp->mod_prev = kobj_modules->mod_prev;
1126 cp->mod_next = kobj_modules;
1127 kobj_modules->mod_prev->mod_next = cp;
1128 kobj_modules->mod_prev = cp;
1129 }
1130
1131 kobj_lm_append(lmid, cp);
1132
1133 return (cp);
1134 }
1135
1136 static int
1137 bind_primary(val_t *bootaux, int lmid)
1138 {
1139 struct modctl_list *linkmap = kobj_lm_lookup(lmid);
1140 struct modctl_list *lp;
1141 struct module *mp;
1142
1143 /*
1144 * Do common symbols.
1145 */
1146 for (lp = linkmap; lp; lp = lp->modl_next) {
1147 mp = mod(lp);
1148
1149 /*
1150 * Don't do common section relocations for modules that
1151 * don't need it.
1152 */
1153 if (mp->flags & (KOBJ_EXEC|KOBJ_INTERP))
1154 continue;
1155
1156 if (do_common(mp) < 0)
1157 return (-1);
1158 }
1159
1160 /*
1161 * Resolve symbols.
1162 */
1163 for (lp = linkmap; lp; lp = lp->modl_next) {
1164 mp = mod(lp);
1165
1166 if (do_symbols(mp, 0) < 0)
1167 return (-1);
1168 }
1169
1170 /*
1171 * Do relocations.
1172 */
1173 for (lp = linkmap; lp; lp = lp->modl_next) {
1174 mp = mod(lp);
1175
1176 if (mp->flags & KOBJ_EXEC) {
1177 Dyn *dyn;
1178 Word relasz = 0, relaent = 0;
1179 Word shtype;
1180 char *rela = NULL;
1181
1182 for (dyn = (Dyn *)bootaux[BA_DYNAMIC].ba_ptr;
1183 dyn->d_tag != DT_NULL; dyn++) {
1184 switch (dyn->d_tag) {
1185 case DT_RELASZ:
1186 case DT_RELSZ:
1187 relasz = dyn->d_un.d_val;
1188 break;
1189 case DT_RELAENT:
1190 case DT_RELENT:
1191 relaent = dyn->d_un.d_val;
1192 break;
1193 case DT_RELA:
1194 shtype = SHT_RELA;
1195 rela = (char *)dyn->d_un.d_ptr;
1196 break;
1197 case DT_REL:
1198 shtype = SHT_REL;
1199 rela = (char *)dyn->d_un.d_ptr;
1200 break;
1201 }
1202 }
1203 if (relasz == 0 ||
1204 relaent == 0 || rela == NULL) {
1205 _kobj_printf(ops, "krtld: bind_primary(): "
1206 "no relocation information found for "
1207 "module %s\n", mp->filename);
1208 return (-1);
1209 }
1210 #ifdef KOBJ_DEBUG
1211 if (kobj_debug & D_RELOCATIONS)
1212 _kobj_printf(ops, "krtld: relocating: file=%s "
1213 "KOBJ_EXEC\n", mp->filename);
1214 #endif
1215 if (do_relocate(mp, rela, shtype, relasz/relaent,
1216 relaent, (Addr)mp->text) < 0)
1217 return (-1);
1218 } else {
1219 if (do_relocations(mp) < 0)
1220 return (-1);
1221 }
1222
1223 kobj_sync_instruction_memory(mp->text, mp->text_size);
1224 }
1225
1226 for (lp = linkmap; lp; lp = lp->modl_next) {
1227 mp = mod(lp);
1228
1229 /*
1230 * We need to re-read the full symbol table for the boot file,
1231 * since we couldn't use the full one before. We also need to
1232 * load the CTF sections of both the boot file and the
1233 * interpreter (us).
1234 */
1235 if (mp->flags & KOBJ_EXEC) {
1236 struct _buf *file;
1237 int n;
1238
1239 file = kobj_open_file(mp->filename);
1240 if (file == (struct _buf *)-1)
1241 return (-1);
1242 if (kobj_read_file(file, (char *)&mp->hdr,
1243 sizeof (mp->hdr), 0) < 0)
1244 return (-1);
1245 n = mp->hdr.e_shentsize * mp->hdr.e_shnum;
1246 mp->shdrs = kobj_alloc(n, KM_WAIT);
1247 if (kobj_read_file(file, mp->shdrs, n,
1248 mp->hdr.e_shoff) < 0)
1249 return (-1);
1250 if (get_syms(mp, file) < 0)
1251 return (-1);
1252 if (get_ctf(mp, file) < 0)
1253 return (-1);
1254 kobj_close_file(file);
1255 mp->flags |= KOBJ_RELOCATED;
1256
1257 } else if (mp->flags & KOBJ_INTERP) {
1258 struct _buf *file;
1259
1260 /*
1261 * The interpreter path fragment in mp->filename
1262 * will already have the module directory suffix
1263 * in it (if appropriate).
1264 */
1265 file = kobj_open_path(mp->filename, 1, 0);
1266 if (file == (struct _buf *)-1)
1267 return (-1);
1268 if (get_ctf(mp, file) < 0)
1269 return (-1);
1270 kobj_close_file(file);
1271 mp->flags |= KOBJ_RELOCATED;
1272 }
1273 }
1274
1275 return (0);
1276 }
1277
1278 static struct modctl *
1279 mod_already_loaded(char *modname)
1280 {
1281 struct modctl *mctl = kobj_modules;
1282
1283 do {
1284 if (strcmp(modname, mctl->mod_filename) == 0)
1285 return (mctl);
1286 mctl = mctl->mod_next;
1287
1288 } while (mctl != kobj_modules);
1289
1290 return (NULL);
1291 }
1292
1293 /*
1294 * Load all the primary dependent modules.
1295 */
1296 static int
1297 load_primary(struct module *mp, int lmid)
1298 {
1299 struct modctl *cp;
1300 struct module *dmp;
1301 char *p, *q;
1302 char modname[MODMAXNAMELEN];
1303
1304 if ((p = mp->depends_on) == NULL)
1305 return (0);
1306
1307 /* CONSTANTCONDITION */
1308 while (1) {
1309 /*
1310 * Skip space.
1311 */
1312 while (*p && (*p == ' ' || *p == '\t'))
1313 p++;
1314 /*
1315 * Get module name.
1316 */
1317 q = modname;
1318 while (*p && *p != ' ' && *p != '\t')
1319 *q++ = *p++;
1320
1321 if (q == modname)
1322 break;
1323
1324 *q = '\0';
1325 /*
1326 * Check for dup dependencies.
1327 */
1328 if (strcmp(modname, "dtracestubs") == 0 ||
1329 mod_already_loaded(modname) != NULL)
1330 continue;
1331
1332 cp = add_primary(modname, lmid);
1333 cp->mod_busy = 1;
1334 /*
1335 * Load it.
1336 */
1337 (void) kobj_load_module(cp, 1);
1338 cp->mod_busy = 0;
1339
1340 if ((dmp = cp->mod_mp) == NULL) {
1341 cp->mod_loaded = 0;
1342 cp->mod_installed = 0;
1343 cp->mod_loadcnt = 0;
1344 return (-1);
1345 }
1346
1347 add_dependent(mp, dmp);
1348 dmp->flags |= KOBJ_PRIM;
1349
1350 /*
1351 * Recurse.
1352 */
1353 if (load_primary(dmp, lmid) == -1) {
1354 cp->mod_loaded = 0;
1355 cp->mod_installed = 0;
1356 cp->mod_loadcnt = 0;
1357 return (-1);
1358 }
1359 }
1360 return (0);
1361 }
1362
1363 static int
1364 console_is_usb_serial(void)
1365 {
1366 char *console;
1367 int len, ret;
1368
1369 if ((len = BOP_GETPROPLEN(ops, "console")) == -1)
1370 return (0);
1371
1372 console = kobj_zalloc(len, KM_WAIT|KM_TMP);
1373 (void) BOP_GETPROP(ops, "console", console);
1374 ret = (strcmp(console, "usb-serial") == 0);
1375 kobj_free(console, len);
1376
1377 return (ret);
1378 }
1379
1380 static int
1381 load_kmdb(val_t *bootaux)
1382 {
1383 struct modctl *mctl;
1384 struct module *mp;
1385 Sym *sym;
1386
1387 if (console_is_usb_serial()) {
1388 _kobj_printf(ops, "kmdb not loaded "
1389 "(unsupported on usb serial console)\n");
1390 return (0);
1391 }
1392
1393 _kobj_printf(ops, "Loading kmdb...\n");
1394
1395 if ((mctl = add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER)) == NULL)
1396 return (-1);
1397
1398 mctl->mod_busy = 1;
1399 (void) kobj_load_module(mctl, 1);
1400 mctl->mod_busy = 0;
1401
1402 if ((mp = mctl->mod_mp) == NULL)
1403 return (-1);
1404
1405 mp->flags |= KOBJ_PRIM;
1406
1407 if (load_primary(mp, KOBJ_LM_DEBUGGER) < 0)
1408 return (-1);
1409
1410 if (boothowto & RB_VERBOSE)
1411 kobj_lm_dump(KOBJ_LM_DEBUGGER);
1412
1413 if (bind_primary(bootaux, KOBJ_LM_DEBUGGER) < 0)
1414 return (-1);
1415
1416 if ((sym = lookup_one(mctl->mod_mp, "kctl_boot_activate")) == NULL)
1417 return (-1);
1418
1419 #ifdef KOBJ_DEBUG
1420 if (kobj_debug & D_DEBUG) {
1421 _kobj_printf(ops, "calling kctl_boot_activate() @ 0x%lx\n",
1422 sym->st_value);
1423 _kobj_printf(ops, "\tops 0x%p\n", ops);
1424 _kobj_printf(ops, "\tromp 0x%p\n", romp);
1425 }
1426 #endif
1427
1428 if (((kctl_boot_activate_f *)sym->st_value)(ops, romp, 0,
1429 (const char **)kobj_kmdb_argv) < 0)
1430 return (-1);
1431
1432 return (0);
1433 }
1434
1435 /*
1436 * Return a string listing module dependencies.
1437 */
1438 static char *
1439 depends_on(struct module *mp)
1440 {
1441 Sym *sp;
1442 char *depstr, *q;
1443
1444 /*
1445 * The module doesn't have a depends_on value, so let's try it the
1446 * old-fashioned way - via "_depends_on"
1447 */
1448 if ((sp = lookup_one(mp, "_depends_on")) == NULL)
1449 return (NULL);
1450
1451 q = (char *)sp->st_value;
1452
1453 #ifdef KOBJ_DEBUG
1454 /*
1455 * _depends_on is a deprecated interface, so we warn about its use
1456 * irrespective of subsequent processing errors. How else are we going
1457 * to be able to deco this interface completely?
1458 * Changes initially limited to DEBUG because third-party modules
1459 * should be flagged to developers before general use base.
1460 */
1461 _kobj_printf(ops,
1462 "Warning: %s uses deprecated _depends_on interface.\n",
1463 mp->filename);
1464 _kobj_printf(ops, "Please notify module developer or vendor.\n");
1465 #endif
1466
1467 /*
1468 * Idiot checks. Make sure it's
1469 * in-bounds and NULL terminated.
1470 */
1471 if (kobj_addrcheck(mp, q) || q[sp->st_size - 1] != '\0') {
1472 _kobj_printf(ops, "Error processing dependency for %s\n",
1473 mp->filename);
1474 return (NULL);
1475 }
1476
1477 depstr = (char *)kobj_alloc(strlen(q) + 1, KM_WAIT);
1478 (void) strcpy(depstr, q);
1479
1480 return (depstr);
1481 }
1482
1483 void
1484 kobj_getmodinfo(void *xmp, struct modinfo *modinfo)
1485 {
1486 struct module *mp;
1487 mp = (struct module *)xmp;
1488
1489 modinfo->mi_base = mp->text;
1490 modinfo->mi_size = mp->text_size + mp->data_size;
1491 }
1492
1493 /*
1494 * kobj_export_ksyms() performs the following services:
1495 *
1496 * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena.
1497 * (2) Removes unneeded symbols to save space.
1498 * (3) Reduces memory footprint by using VM_BESTFIT allocations.
1499 * (4) Makes the symbol table visible to /dev/ksyms.
1500 */
1501 static void
1502 kobj_export_ksyms(struct module *mp)
1503 {
1504 Sym *esp = (Sym *)(mp->symtbl + mp->symhdr->sh_size);
1505 Sym *sp, *osp;
1506 char *name;
1507 size_t namelen;
1508 struct module *omp;
1509 uint_t nsyms;
1510 size_t symsize = mp->symhdr->sh_entsize;
1511 size_t locals = 1;
1512 size_t strsize;
1513
1514 /*
1515 * Make a copy of the original module structure.
1516 */
1517 omp = kobj_alloc(sizeof (struct module), KM_WAIT);
1518 bcopy(mp, omp, sizeof (struct module));
1519
1520 /*
1521 * Compute the sizes of the new symbol table sections.
1522 */
1523 for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) {
1524 if (osp->st_value == 0)
1525 continue;
1526 if (sym_lookup(omp, osp) == NULL)
1527 continue;
1528 name = omp->strings + osp->st_name;
1529 namelen = strlen(name);
1530 if (ELF_ST_BIND(osp->st_info) == STB_LOCAL)
1531 locals++;
1532 nsyms++;
1533 strsize += namelen + 1;
1534 }
1535
1536 mp->nsyms = nsyms;
1537 mp->hashsize = kobj_gethashsize(mp->nsyms);
1538
1539 /*
1540 * ksyms_lock must be held as writer during any operation that
1541 * modifies ksyms_arena, including allocation from same, and
1542 * must not be dropped until the arena is vmem_walk()able.
1543 */
1544 rw_enter(&ksyms_lock, RW_WRITER);
1545
1546 /*
1547 * Allocate space for the new section headers (symtab and strtab),
1548 * symbol table, buckets, chains, and strings.
1549 */
1550 mp->symsize = (2 * sizeof (Shdr)) + (nsyms * symsize) +
1551 (mp->hashsize + mp->nsyms) * sizeof (symid_t) + strsize;
1552
1553 if (mp->flags & KOBJ_NOKSYMS) {
1554 mp->symspace = kobj_alloc(mp->symsize, KM_WAIT);
1555 } else {
1556 mp->symspace = vmem_alloc(ksyms_arena, mp->symsize,
1557 VM_BESTFIT | VM_SLEEP);
1558 }
1559 bzero(mp->symspace, mp->symsize);
1560
1561 /*
1562 * Divvy up symspace.
1563 */
1564 mp->shdrs = mp->symspace;
1565 mp->symhdr = (Shdr *)mp->shdrs;
1566 mp->strhdr = (Shdr *)(mp->symhdr + 1);
1567 mp->symtbl = (char *)(mp->strhdr + 1);
1568 mp->buckets = (symid_t *)(mp->symtbl + (nsyms * symsize));
1569 mp->chains = (symid_t *)(mp->buckets + mp->hashsize);
1570 mp->strings = (char *)(mp->chains + nsyms);
1571
1572 /*
1573 * Fill in the new section headers (symtab and strtab).
1574 */
1575 mp->hdr.e_shnum = 2;
1576 mp->symtbl_section = 0;
1577
1578 mp->symhdr->sh_type = SHT_SYMTAB;
1579 mp->symhdr->sh_addr = (Addr)mp->symtbl;
1580 mp->symhdr->sh_size = nsyms * symsize;
1581 mp->symhdr->sh_link = 1;
1582 mp->symhdr->sh_info = locals;
1583 mp->symhdr->sh_addralign = sizeof (Addr);
1584 mp->symhdr->sh_entsize = symsize;
1585
1586 mp->strhdr->sh_type = SHT_STRTAB;
1587 mp->strhdr->sh_addr = (Addr)mp->strings;
1588 mp->strhdr->sh_size = strsize;
1589 mp->strhdr->sh_addralign = 1;
1590
1591 /*
1592 * Construct the new symbol table.
1593 */
1594 for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) {
1595 if (osp->st_value == 0)
1596 continue;
1597 if (sym_lookup(omp, osp) == NULL)
1598 continue;
1599 name = omp->strings + osp->st_name;
1600 namelen = strlen(name);
1601 sp = (Sym *)(mp->symtbl + symsize * nsyms);
1602 bcopy(osp, sp, symsize);
1603 bcopy(name, mp->strings + strsize, namelen);
1604 sp->st_name = strsize;
1605 sym_insert(mp, name, nsyms);
1606 nsyms++;
1607 strsize += namelen + 1;
1608 }
1609
1610 rw_exit(&ksyms_lock);
1611
1612 /*
1613 * Free the old section headers -- we'll never need them again.
1614 */
1615 if (!(mp->flags & KOBJ_PRIM)) {
1616 uint_t shn;
1617 Shdr *shp;
1618
1619 for (shn = 1; shn < omp->hdr.e_shnum; shn++) {
1620 shp = (Shdr *)(omp->shdrs + shn * omp->hdr.e_shentsize);
1621 switch (shp->sh_type) {
1622 case SHT_RELA:
1623 case SHT_REL:
1624 if (shp->sh_addr != 0) {
1625 kobj_free((void *)shp->sh_addr,
1626 shp->sh_size);
1627 }
1628 break;
1629 }
1630 }
1631 kobj_free(omp->shdrs, omp->hdr.e_shentsize * omp->hdr.e_shnum);
1632 }
1633 /*
1634 * Discard the old symbol table and our copy of the module strucure.
1635 */
1636 if (!(mp->flags & KOBJ_PRIM))
1637 kobj_free(omp->symspace, omp->symsize);
1638 kobj_free(omp, sizeof (struct module));
1639 }
1640
1641 static void
1642 kobj_export_ctf(struct module *mp)
1643 {
1644 char *data = mp->ctfdata;
1645 size_t size = mp->ctfsize;
1646
1647 if (data != NULL) {
1648 if (_moddebug & MODDEBUG_NOCTF) {
1649 mp->ctfdata = NULL;
1650 mp->ctfsize = 0;
1651 } else {
1652 mp->ctfdata = vmem_alloc(ctf_arena, size,
1653 VM_BESTFIT | VM_SLEEP);
1654 bcopy(data, mp->ctfdata, size);
1655 }
1656
1657 if (!(mp->flags & KOBJ_PRIM))
1658 kobj_free(data, size);
1659 }
1660 }
1661
1662 void
1663 kobj_export_module(struct module *mp)
1664 {
1665 kobj_export_ksyms(mp);
1666 kobj_export_ctf(mp);
1667
1668 mp->flags |= KOBJ_EXPORTED;
1669 }
1670
1671 static int
1672 process_dynamic(struct module *mp, char *dyndata, char *strdata)
1673 {
1674 char *path = NULL, *depstr = NULL;
1675 int allocsize = 0, osize = 0, nsize = 0;
1676 char *libname, *tmp;
1677 int lsize;
1678 Dyn *dynp;
1679
1680 for (dynp = (Dyn *)dyndata; dynp && dynp->d_tag != DT_NULL; dynp++) {
1681 switch (dynp->d_tag) {
1682 case DT_NEEDED:
1683 /*
1684 * Read the DT_NEEDED entries, expanding the macros they
1685 * contain (if any), and concatenating them into a
1686 * single space-separated dependency list.
1687 */
1688 libname = (ulong_t)dynp->d_un.d_ptr + strdata;
1689
1690 if (strchr(libname, '$') != NULL) {
1691 char *_lib;
1692
1693 if (path == NULL)
1694 path = kobj_alloc(MAXPATHLEN, KM_WAIT);
1695 if ((_lib = expand_libmacro(libname, path,
1696 path)) != NULL)
1697 libname = _lib;
1698 else {
1699 _kobj_printf(ops, "krtld: "
1700 "process_dynamic: failed to expand "
1701 "%s\n", libname);
1702 }
1703 }
1704
1705 lsize = strlen(libname);
1706 nsize += lsize;
1707 if (nsize + 1 > allocsize) {
1708 tmp = kobj_alloc(allocsize + MAXPATHLEN,
1709 KM_WAIT);
1710 if (depstr != NULL) {
1711 bcopy(depstr, tmp, osize);
1712 kobj_free(depstr, allocsize);
1713 }
1714 depstr = tmp;
1715 allocsize += MAXPATHLEN;
1716 }
1717 bcopy(libname, depstr + osize, lsize);
1718 *(depstr + nsize) = ' '; /* separator */
1719 nsize++;
1720 osize = nsize;
1721 break;
1722
1723 case DT_FLAGS_1:
1724 if (dynp->d_un.d_val & DF_1_IGNMULDEF)
1725 mp->flags |= KOBJ_IGNMULDEF;
1726 if (dynp->d_un.d_val & DF_1_NOKSYMS)
1727 mp->flags |= KOBJ_NOKSYMS;
1728
1729 break;
1730 }
1731 }
1732
1733 /*
1734 * finish up the depends string (if any)
1735 */
1736 if (depstr != NULL) {
1737 *(depstr + nsize - 1) = '\0'; /* overwrite separator w/term */
1738 if (path != NULL)
1739 kobj_free(path, MAXPATHLEN);
1740
1741 tmp = kobj_alloc(nsize, KM_WAIT);
1742 bcopy(depstr, tmp, nsize);
1743 kobj_free(depstr, allocsize);
1744 depstr = tmp;
1745
1746 mp->depends_on = depstr;
1747 }
1748
1749 return (0);
1750 }
1751
1752 static int
1753 do_dynamic(struct module *mp, struct _buf *file)
1754 {
1755 Shdr *dshp, *dstrp, *shp;
1756 char *dyndata, *dstrdata;
1757 int dshn, shn, rc;
1758
1759 /* find and validate the dynamic section (if any) */
1760
1761 for (dshp = NULL, shn = 1; shn < mp->hdr.e_shnum; shn++) {
1762 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
1763 switch (shp->sh_type) {
1764 case SHT_DYNAMIC:
1765 if (dshp != NULL) {
1766 _kobj_printf(ops, "krtld: get_dynamic: %s, ",
1767 mp->filename);
1768 _kobj_printf(ops,
1769 "multiple dynamic sections\n");
1770 return (-1);
1771 } else {
1772 dshp = shp;
1773 dshn = shn;
1774 }
1775 break;
1776 }
1777 }
1778
1779 if (dshp == NULL)
1780 return (0);
1781
1782 if (dshp->sh_link > mp->hdr.e_shnum) {
1783 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1784 _kobj_printf(ops, "no section for sh_link %d\n", dshp->sh_link);
1785 return (-1);
1786 }
1787 dstrp = (Shdr *)(mp->shdrs + dshp->sh_link * mp->hdr.e_shentsize);
1788
1789 if (dstrp->sh_type != SHT_STRTAB) {
1790 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1791 _kobj_printf(ops, "sh_link not a string table for section %d\n",
1792 dshn);
1793 return (-1);
1794 }
1795
1796 /* read it from disk */
1797
1798 dyndata = kobj_alloc(dshp->sh_size, KM_WAIT|KM_TMP);
1799 if (kobj_read_file(file, dyndata, dshp->sh_size, dshp->sh_offset) < 0) {
1800 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1801 _kobj_printf(ops, "error reading section %d\n", dshn);
1802
1803 kobj_free(dyndata, dshp->sh_size);
1804 return (-1);
1805 }
1806
1807 dstrdata = kobj_alloc(dstrp->sh_size, KM_WAIT|KM_TMP);
1808 if (kobj_read_file(file, dstrdata, dstrp->sh_size,
1809 dstrp->sh_offset) < 0) {
1810 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1811 _kobj_printf(ops, "error reading section %d\n", dshp->sh_link);
1812
1813 kobj_free(dyndata, dshp->sh_size);
1814 kobj_free(dstrdata, dstrp->sh_size);
1815 return (-1);
1816 }
1817
1818 /* pull the interesting pieces out */
1819
1820 rc = process_dynamic(mp, dyndata, dstrdata);
1821
1822 kobj_free(dyndata, dshp->sh_size);
1823 kobj_free(dstrdata, dstrp->sh_size);
1824
1825 return (rc);
1826 }
1827
1828 void
1829 kobj_set_ctf(struct module *mp, caddr_t data, size_t size)
1830 {
1831 if (!standalone) {
1832 if (mp->ctfdata != NULL) {
1833 if (vmem_contains(ctf_arena, mp->ctfdata,
1834 mp->ctfsize)) {
1835 vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize);
1836 } else {
1837 kobj_free(mp->ctfdata, mp->ctfsize);
1838 }
1839 }
1840 }
1841
1842 /*
1843 * The order is very important here. We need to make sure that
1844 * consumers, at any given instant, see a consistent state. We'd
1845 * rather they see no CTF data than the address of one buffer and the
1846 * size of another.
1847 */
1848 mp->ctfdata = NULL;
1849 membar_producer();
1850 mp->ctfsize = size;
1851 mp->ctfdata = data;
1852 membar_producer();
1853 }
1854
1855 int
1856 kobj_load_module(struct modctl *modp, int use_path)
1857 {
1858 char *filename = modp->mod_filename;
1859 char *modname = modp->mod_modname;
1860 int i;
1861 int n;
1862 struct _buf *file;
1863 struct module *mp = NULL;
1864 #ifdef MODDIR_SUFFIX
1865 int no_suffixdir_drv = 0;
1866 #endif
1867
1868 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
1869
1870 /*
1871 * We need to prevent kmdb's symbols from leaking into /dev/ksyms.
1872 * kmdb contains a bunch of symbols with well-known names, symbols
1873 * which will mask the real versions, thus causing no end of trouble
1874 * for mdb.
1875 */
1876 if (strcmp(modp->mod_modname, "kmdbmod") == 0)
1877 mp->flags |= KOBJ_NOKSYMS;
1878
1879 file = kobj_open_path(filename, use_path, 1);
1880 if (file == (struct _buf *)-1) {
1881 #ifdef MODDIR_SUFFIX
1882 file = kobj_open_path(filename, use_path, 0);
1883 #endif
1884 if (file == (struct _buf *)-1) {
1885 kobj_free(mp, sizeof (*mp));
1886 goto bad;
1887 }
1888 #ifdef MODDIR_SUFFIX
1889 /*
1890 * There is no driver module in the ISA specific (suffix)
1891 * subdirectory but there is a module in the parent directory.
1892 */
1893 if (strncmp(filename, "drv/", 4) == 0) {
1894 no_suffixdir_drv = 1;
1895 }
1896 #endif
1897 }
1898
1899 mp->filename = kobj_alloc(strlen(file->_name) + 1, KM_WAIT);
1900 (void) strcpy(mp->filename, file->_name);
1901
1902 if (kobj_read_file(file, (char *)&mp->hdr, sizeof (mp->hdr), 0) < 0) {
1903 _kobj_printf(ops, "kobj_load_module: %s read header failed\n",
1904 modname);
1905 kobj_free(mp->filename, strlen(file->_name) + 1);
1906 kobj_free(mp, sizeof (*mp));
1907 goto bad;
1908 }
1909 for (i = 0; i < SELFMAG; i++) {
1910 if (mp->hdr.e_ident[i] != ELFMAG[i]) {
1911 if (_moddebug & MODDEBUG_ERRMSG)
1912 _kobj_printf(ops, "%s not an elf module\n",
1913 modname);
1914 kobj_free(mp->filename, strlen(file->_name) + 1);
1915 kobj_free(mp, sizeof (*mp));
1916 goto bad;
1917 }
1918 }
1919 /*
1920 * It's ELF, but is it our ISA? Interpreting the header
1921 * from a file for a byte-swapped ISA could cause a huge
1922 * and unsatisfiable value to be passed to kobj_alloc below
1923 * and therefore hang booting.
1924 */
1925 if (!elf_mach_ok(&mp->hdr)) {
1926 if (_moddebug & MODDEBUG_ERRMSG)
1927 _kobj_printf(ops, "%s not an elf module for this ISA\n",
1928 modname);
1929 kobj_free(mp->filename, strlen(file->_name) + 1);
1930 kobj_free(mp, sizeof (*mp));
1931 #ifdef MODDIR_SUFFIX
1932 /*
1933 * The driver mod is not in the ISA specific subdirectory
1934 * and the module in the parent directory is not our ISA.
1935 * If it is our ISA, for now we will silently succeed.
1936 */
1937 if (no_suffixdir_drv == 1) {
1938 cmn_err(CE_CONT, "?NOTICE: %s: 64-bit driver module"
1939 " not found\n", modname);
1940 }
1941 #endif
1942 goto bad;
1943 }
1944
1945 /*
1946 * All modules, save for unix, should be relocatable (as opposed to
1947 * dynamic). Dynamic modules come with PLTs and GOTs, which can't
1948 * currently be processed by krtld.
1949 */
1950 if (mp->hdr.e_type != ET_REL) {
1951 if (_moddebug & MODDEBUG_ERRMSG)
1952 _kobj_printf(ops, "%s isn't a relocatable (ET_REL) "
1953 "module\n", modname);
1954 kobj_free(mp->filename, strlen(file->_name) + 1);
1955 kobj_free(mp, sizeof (*mp));
1956 goto bad;
1957 }
1958
1959 n = mp->hdr.e_shentsize * mp->hdr.e_shnum;
1960 mp->shdrs = kobj_alloc(n, KM_WAIT);
1961
1962 if (kobj_read_file(file, mp->shdrs, n, mp->hdr.e_shoff) < 0) {
1963 _kobj_printf(ops, "kobj_load_module: %s error reading "
1964 "section headers\n", modname);
1965 kobj_free(mp->shdrs, n);
1966 kobj_free(mp->filename, strlen(file->_name) + 1);
1967 kobj_free(mp, sizeof (*mp));
1968 goto bad;
1969 }
1970
1971 kobj_notify(KOBJ_NOTIFY_MODLOADING, modp);
1972 module_assign(modp, mp);
1973
1974 /* read in sections */
1975 if (get_progbits(mp, file) < 0) {
1976 _kobj_printf(ops, "%s error reading sections\n", modname);
1977 goto bad;
1978 }
1979
1980 if (do_dynamic(mp, file) < 0) {
1981 _kobj_printf(ops, "%s error reading dynamic section\n",
1982 modname);
1983 goto bad;
1984 }
1985
1986 modp->mod_text = mp->text;
1987 modp->mod_text_size = mp->text_size;
1988
1989 /* read in symbols; adjust values for each section's real address */
1990 if (get_syms(mp, file) < 0) {
1991 _kobj_printf(ops, "%s error reading symbols\n",
1992 modname);
1993 goto bad;
1994 }
1995
1996 /*
1997 * If we didn't dependency information from the dynamic section, look
1998 * for it the old-fashioned way.
1999 */
2000 if (mp->depends_on == NULL)
2001 mp->depends_on = depends_on(mp);
2002
2003 if (get_ctf(mp, file) < 0) {
2004 _kobj_printf(ops, "%s debug information will not "
2005 "be available\n", modname);
2006 }
2007
2008 /* primary kernel modules do not have a signature section */
2009 if (!(mp->flags & KOBJ_PRIM))
2010 get_signature(mp, file);
2011
2012 #ifdef KOBJ_DEBUG
2013 if (kobj_debug & D_LOADING) {
2014 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
2015 _kobj_printf(ops, "\ttext:0x%p", mp->text);
2016 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
2017 _kobj_printf(ops, "\tdata:0x%p", mp->data);
2018 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
2019 }
2020 #endif /* KOBJ_DEBUG */
2021
2022 /*
2023 * For primary kernel modules, we defer
2024 * symbol resolution and relocation until
2025 * all primary objects have been loaded.
2026 */
2027 if (!standalone) {
2028 int ddrval, dcrval;
2029 char *dependent_modname;
2030 /* load all dependents */
2031 dependent_modname = kobj_zalloc(MODMAXNAMELEN, KM_WAIT);
2032 ddrval = do_dependents(modp, dependent_modname, MODMAXNAMELEN);
2033
2034 /*
2035 * resolve undefined and common symbols,
2036 * also allocates common space
2037 */
2038 if ((dcrval = do_common(mp)) < 0) {
2039 switch (dcrval) {
2040 case DOSYM_UNSAFE:
2041 _kobj_printf(ops, "WARNING: mod_load: "
2042 "MT-unsafe module '%s' rejected\n",
2043 modname);
2044 break;
2045 case DOSYM_UNDEF:
2046 _kobj_printf(ops, "WARNING: mod_load: "
2047 "cannot load module '%s'\n",
2048 modname);
2049 if (ddrval == -1) {
2050 _kobj_printf(ops, "WARNING: %s: ",
2051 modname);
2052 _kobj_printf(ops,
2053 "unable to resolve dependency, "
2054 "module '%s' not found\n",
2055 dependent_modname);
2056 }
2057 break;
2058 }
2059 }
2060 kobj_free(dependent_modname, MODMAXNAMELEN);
2061 if (dcrval < 0)
2062 goto bad;
2063
2064 /* process relocation tables */
2065 if (do_relocations(mp) < 0) {
2066 _kobj_printf(ops, "%s error doing relocations\n",
2067 modname);
2068 goto bad;
2069 }
2070
2071 if (mp->destination) {
2072 off_t off = (uintptr_t)mp->destination & PAGEOFFSET;
2073 caddr_t base = (caddr_t)mp->destination - off;
2074 size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE);
2075
2076 hat_unload(kas.a_hat, base, size, HAT_UNLOAD_UNLOCK);
2077 vmem_free(heap_arena, base, size);
2078 }
2079
2080 /* sync_instruction_memory */
2081 kobj_sync_instruction_memory(mp->text, mp->text_size);
2082 kobj_export_module(mp);
2083 kobj_notify(KOBJ_NOTIFY_MODLOADED, modp);
2084 }
2085 kobj_close_file(file);
2086 return (0);
2087 bad:
2088 if (file != (struct _buf *)-1)
2089 kobj_close_file(file);
2090 if (modp->mod_mp != NULL)
2091 free_module_data(modp->mod_mp);
2092
2093 module_assign(modp, NULL);
2094 return ((file == (struct _buf *)-1) ? ENOENT : EINVAL);
2095 }
2096
2097 int
2098 kobj_load_primary_module(struct modctl *modp)
2099 {
2100 struct modctl *dep;
2101 struct module *mp;
2102
2103 if (kobj_load_module(modp, 0) != 0)
2104 return (-1);
2105
2106 mp = modp->mod_mp;
2107 mp->flags |= KOBJ_PRIM;
2108
2109 /* Bind new module to its dependents */
2110 if (mp->depends_on != NULL && (dep =
2111 mod_already_loaded(mp->depends_on)) == NULL) {
2112 #ifdef KOBJ_DEBUG
2113 if (kobj_debug & D_DEBUG) {
2114 _kobj_printf(ops, "krtld: failed to resolve deps "
2115 "for primary %s\n", modp->mod_modname);
2116 }
2117 #endif
2118 return (-1);
2119 }
2120
2121 add_dependent(mp, dep->mod_mp);
2122
2123 /*
2124 * Relocate it. This module may not be part of a link map, so we
2125 * can't use bind_primary.
2126 */
2127 if (do_common(mp) < 0 || do_symbols(mp, 0) < 0 ||
2128 do_relocations(mp) < 0) {
2129 #ifdef KOBJ_DEBUG
2130 if (kobj_debug & D_DEBUG) {
2131 _kobj_printf(ops, "krtld: failed to relocate "
2132 "primary %s\n", modp->mod_modname);
2133 }
2134 #endif
2135 return (-1);
2136 }
2137
2138 return (0);
2139 }
2140
2141 static void
2142 module_assign(struct modctl *cp, struct module *mp)
2143 {
2144 if (standalone) {
2145 cp->mod_mp = mp;
2146 return;
2147 }
2148 mutex_enter(&mod_lock);
2149 cp->mod_mp = mp;
2150 cp->mod_gencount++;
2151 mutex_exit(&mod_lock);
2152 }
2153
2154 void
2155 kobj_unload_module(struct modctl *modp)
2156 {
2157 struct module *mp = modp->mod_mp;
2158
2159 if ((_moddebug & MODDEBUG_KEEPTEXT) && mp) {
2160 _kobj_printf(ops, "text for %s ", mp->filename);
2161 _kobj_printf(ops, "was at %p\n", mp->text);
2162 mp->text = NULL; /* don't actually free it */
2163 }
2164
2165 kobj_notify(KOBJ_NOTIFY_MODUNLOADING, modp);
2166
2167 /*
2168 * Null out mod_mp first, so consumers (debuggers) know not to look
2169 * at the module structure any more.
2170 */
2171 mutex_enter(&mod_lock);
2172 modp->mod_mp = NULL;
2173 mutex_exit(&mod_lock);
2174
2175 kobj_notify(KOBJ_NOTIFY_MODUNLOADED, modp);
2176 free_module_data(mp);
2177 }
2178
2179 static void
2180 free_module_data(struct module *mp)
2181 {
2182 struct module_list *lp, *tmp;
2183 hotinline_desc_t *hid, *next;
2184 int ksyms_exported = 0;
2185
2186 lp = mp->head;
2187 while (lp) {
2188 tmp = lp;
2189 lp = lp->next;
2190 kobj_free((char *)tmp, sizeof (*tmp));
2191 }
2192
2193 /* release hotinlines */
2194 hid = mp->hi_calls;
2195 while (hid != NULL) {
2196 next = hid->hid_next;
2197 kobj_free(hid->hid_symname, strlen(hid->hid_symname) + 1);
2198 kobj_free(hid, sizeof (hotinline_desc_t));
2199 hid = next;
2200 }
2201
2202 rw_enter(&ksyms_lock, RW_WRITER);
2203 if (mp->symspace) {
2204 if (vmem_contains(ksyms_arena, mp->symspace, mp->symsize)) {
2205 vmem_free(ksyms_arena, mp->symspace, mp->symsize);
2206 ksyms_exported = 1;
2207 } else {
2208 if (mp->flags & KOBJ_NOKSYMS)
2209 ksyms_exported = 1;
2210 kobj_free(mp->symspace, mp->symsize);
2211 }
2212 }
2213 rw_exit(&ksyms_lock);
2214
2215 if (mp->ctfdata) {
2216 if (vmem_contains(ctf_arena, mp->ctfdata, mp->ctfsize))
2217 vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize);
2218 else
2219 kobj_free(mp->ctfdata, mp->ctfsize);
2220 }
2221
2222 if (mp->sigdata)
2223 kobj_free(mp->sigdata, mp->sigsize);
2224
2225 /*
2226 * We did not get far enough into kobj_export_ksyms() to free allocated
2227 * buffers because we encounted error conditions. Free the buffers.
2228 */
2229 if ((ksyms_exported == 0) && (mp->shdrs != NULL)) {
2230 uint_t shn;
2231 Shdr *shp;
2232
2233 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2234 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2235 switch (shp->sh_type) {
2236 case SHT_RELA:
2237 case SHT_REL:
2238 if (shp->sh_addr != 0)
2239 kobj_free((void *)shp->sh_addr,
2240 shp->sh_size);
2241 break;
2242 }
2243 }
2244 err_free_done:
2245 if (!(mp->flags & KOBJ_PRIM)) {
2246 kobj_free(mp->shdrs,
2247 mp->hdr.e_shentsize * mp->hdr.e_shnum);
2248 }
2249 }
2250
2251 if (mp->bss)
2252 vmem_free(data_arena, (void *)mp->bss, mp->bss_size);
2253
2254 if (mp->fbt_tab)
2255 kobj_texthole_free(mp->fbt_tab, mp->fbt_size);
2256
2257 if (mp->textwin_base)
2258 kobj_textwin_free(mp);
2259
2260 if (mp->sdt_probes != NULL) {
2261 sdt_probedesc_t *sdp = mp->sdt_probes, *next;
2262
2263 while (sdp != NULL) {
2264 next = sdp->sdpd_next;
2265 kobj_free(sdp->sdpd_name, strlen(sdp->sdpd_name) + 1);
2266 kobj_free(sdp, sizeof (sdt_probedesc_t));
2267 sdp = next;
2268 }
2269 }
2270
2271 if (mp->sdt_tab)
2272 kobj_texthole_free(mp->sdt_tab, mp->sdt_size);
2273 if (mp->text)
2274 vmem_free(text_arena, mp->text, mp->text_size);
2275 if (mp->data)
2276 vmem_free(data_arena, mp->data, mp->data_size);
2277 if (mp->depends_on)
2278 kobj_free(mp->depends_on, strlen(mp->depends_on)+1);
2279 if (mp->filename)
2280 kobj_free(mp->filename, strlen(mp->filename)+1);
2281
2282 kobj_free((char *)mp, sizeof (*mp));
2283 }
2284
2285 static int
2286 get_progbits(struct module *mp, struct _buf *file)
2287 {
2288 struct proginfo *tp, *dp, *sdp;
2289 Shdr *shp;
2290 reloc_dest_t dest = NULL;
2291 uintptr_t bits_ptr;
2292 uintptr_t text = 0, data, textptr;
2293 uint_t shn;
2294 int err = -1;
2295
2296 tp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2297 dp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2298 sdp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2299 /*
2300 * loop through sections to find out how much space we need
2301 * for text, data, (also bss that is already assigned)
2302 */
2303 if (get_progbits_size(mp, tp, dp, sdp) < 0)
2304 goto done;
2305
2306 mp->text_size = tp->size;
2307 mp->data_size = dp->size;
2308
2309 if (standalone) {
2310 caddr_t limit = _data;
2311
2312 if (lg_pagesize && _text + lg_pagesize < limit)
2313 limit = _text + lg_pagesize;
2314
2315 mp->text = kobj_segbrk(&_etext, mp->text_size,
2316 tp->align, limit);
2317 /*
2318 * If we can't grow the text segment, try the
2319 * data segment before failing.
2320 */
2321 if (mp->text == NULL) {
2322 mp->text = kobj_segbrk(&_edata, mp->text_size,
2323 tp->align, 0);
2324 }
2325
2326 mp->data = kobj_segbrk(&_edata, mp->data_size, dp->align, 0);
2327
2328 if (mp->text == NULL || mp->data == NULL)
2329 goto done;
2330
2331 } else {
2332 if (text_arena == NULL)
2333 kobj_vmem_init(&text_arena, &data_arena);
2334
2335 /*
2336 * some architectures may want to load the module on a
2337 * page that is currently read only. It may not be
2338 * possible for those architectures to remap their page
2339 * on the fly. So we provide a facility for them to hang
2340 * a private hook where the memory they assign the module
2341 * is not the actual place where the module loads.
2342 *
2343 * In this case there are two addresses that deal with the
2344 * modload.
2345 * 1) the final destination of the module
2346 * 2) the address that is used to view the newly
2347 * loaded module until all the relocations relative to 1
2348 * above are completed.
2349 *
2350 * That is what dest is used for below.
2351 */
2352 mp->text_size += tp->align;
2353 mp->data_size += dp->align;
2354
2355 mp->text = kobj_text_alloc(text_arena, mp->text_size);
2356
2357 /*
2358 * a remap is taking place. Align the text ptr relative
2359 * to the secondary mapping. That is where the bits will
2360 * be read in.
2361 */
2362 if (kvseg.s_base != NULL && !vmem_contains(heaptext_arena,
2363 mp->text, mp->text_size)) {
2364 off_t off = (uintptr_t)mp->text & PAGEOFFSET;
2365 size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE);
2366 caddr_t map = vmem_alloc(heap_arena, size, VM_SLEEP);
2367 caddr_t orig = mp->text - off;
2368 pgcnt_t pages = size / PAGESIZE;
2369
2370 dest = (reloc_dest_t)(map + off);
2371 text = ALIGN((uintptr_t)dest, tp->align);
2372
2373 while (pages--) {
2374 hat_devload(kas.a_hat, map, PAGESIZE,
2375 hat_getpfnum(kas.a_hat, orig),
2376 PROT_READ | PROT_WRITE | PROT_EXEC,
2377 HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
2378 map += PAGESIZE;
2379 orig += PAGESIZE;
2380 }
2381 /*
2382 * Since we set up a non-cacheable mapping, we need
2383 * to flush any old entries in the cache that might
2384 * be left around from the read-only mapping.
2385 */
2386 dcache_flushall();
2387 }
2388 if (mp->data_size)
2389 mp->data = vmem_alloc(data_arena, mp->data_size,
2390 VM_SLEEP | VM_BESTFIT);
2391 }
2392 textptr = (uintptr_t)mp->text;
2393 textptr = ALIGN(textptr, tp->align);
2394 mp->destination = dest;
2395
2396 /*
2397 * This is the case where a remap is not being done.
2398 */
2399 if (text == 0)
2400 text = ALIGN((uintptr_t)mp->text, tp->align);
2401 data = ALIGN((uintptr_t)mp->data, dp->align);
2402
2403 /* now loop though sections assigning addresses and loading the data */
2404 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2405 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2406 if (!(shp->sh_flags & SHF_ALLOC))
2407 continue;
2408
2409 if ((shp->sh_flags & SHF_WRITE) == 0)
2410 bits_ptr = text;
2411 else
2412 bits_ptr = data;
2413
2414 bits_ptr = ALIGN(bits_ptr, shp->sh_addralign);
2415
2416 if (shp->sh_type == SHT_NOBITS) {
2417 /*
2418 * Zero bss.
2419 */
2420 bzero((caddr_t)bits_ptr, shp->sh_size);
2421 shp->sh_type = SHT_PROGBITS;
2422 } else {
2423 if (kobj_read_file(file, (char *)bits_ptr,
2424 shp->sh_size, shp->sh_offset) < 0)
2425 goto done;
2426 }
2427
2428 if (shp->sh_flags & SHF_WRITE) {
2429 shp->sh_addr = bits_ptr;
2430 } else {
2431 textptr = ALIGN(textptr, shp->sh_addralign);
2432 shp->sh_addr = textptr;
2433 textptr += shp->sh_size;
2434 }
2435
2436 bits_ptr += shp->sh_size;
2437 if ((shp->sh_flags & SHF_WRITE) == 0)
2438 text = bits_ptr;
2439 else
2440 data = bits_ptr;
2441 }
2442
2443 err = 0;
2444 done:
2445 /*
2446 * Free and mark as freed the section headers here so that
2447 * free_module_data() does not have to worry about this buffer.
2448 *
2449 * This buffer is freed here because one of the possible reasons
2450 * for error is a section with non-zero sh_addr and in that case
2451 * free_module_data() would have no way of recognizing that this
2452 * buffer was unallocated.
2453 */
2454 if (err != 0) {
2455 kobj_free(mp->shdrs, mp->hdr.e_shentsize * mp->hdr.e_shnum);
2456 mp->shdrs = NULL;
2457 }
2458
2459 (void) kobj_free(tp, sizeof (struct proginfo));
2460 (void) kobj_free(dp, sizeof (struct proginfo));
2461 (void) kobj_free(sdp, sizeof (struct proginfo));
2462
2463 return (err);
2464 }
2465
2466 /*
2467 * Go through suppress_sym_list to see if "multiply defined"
2468 * warning of this symbol should be suppressed. Return 1 if
2469 * warning should be suppressed, 0 otherwise.
2470 */
2471 static int
2472 kobj_suppress_warning(char *symname)
2473 {
2474 int i;
2475
2476 for (i = 0; suppress_sym_list[i] != NULL; i++) {
2477 if (strcmp(suppress_sym_list[i], symname) == 0)
2478 return (1);
2479 }
2480
2481 return (0);
2482 }
2483
2484 static int
2485 get_syms(struct module *mp, struct _buf *file)
2486 {
2487 uint_t shn;
2488 Shdr *shp;
2489 uint_t i;
2490 Sym *sp, *ksp;
2491 char *symname;
2492 int dosymtab = 0;
2493
2494 /*
2495 * Find the interesting sections.
2496 */
2497 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2498 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2499 switch (shp->sh_type) {
2500 case SHT_SYMTAB:
2501 mp->symtbl_section = shn;
2502 mp->symhdr = shp;
2503 dosymtab++;
2504 break;
2505
2506 case SHT_RELA:
2507 case SHT_REL:
2508 /*
2509 * Already loaded.
2510 */
2511 if (shp->sh_addr)
2512 continue;
2513
2514 /* KM_TMP since kobj_free'd in do_relocations */
2515 shp->sh_addr = (Addr)
2516 kobj_alloc(shp->sh_size, KM_WAIT|KM_TMP);
2517
2518 if (kobj_read_file(file, (char *)shp->sh_addr,
2519 shp->sh_size, shp->sh_offset) < 0) {
2520 _kobj_printf(ops, "krtld: get_syms: %s, ",
2521 mp->filename);
2522 _kobj_printf(ops, "error reading section %d\n",
2523 shn);
2524 return (-1);
2525 }
2526 break;
2527 }
2528 }
2529
2530 /*
2531 * This is true for a stripped executable. In the case of
2532 * 'unix' it can be stripped but it still contains the SHT_DYNSYM,
2533 * and since that symbol information is still present everything
2534 * is just fine.
2535 */
2536 if (!dosymtab) {
2537 if (mp->flags & KOBJ_EXEC)
2538 return (0);
2539 _kobj_printf(ops, "krtld: get_syms: %s ",
2540 mp->filename);
2541 _kobj_printf(ops, "no SHT_SYMTAB symbol table found\n");
2542 return (-1);
2543 }
2544
2545 /*
2546 * get the associated string table header
2547 */
2548 if ((mp->symhdr == 0) || (mp->symhdr->sh_link >= mp->hdr.e_shnum))
2549 return (-1);
2550 mp->strhdr = (Shdr *)
2551 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize);
2552
2553 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize;
2554 mp->hashsize = kobj_gethashsize(mp->nsyms);
2555
2556 /*
2557 * Allocate space for the symbol table, buckets, chains, and strings.
2558 */
2559 mp->symsize = mp->symhdr->sh_size +
2560 (mp->hashsize + mp->nsyms) * sizeof (symid_t) + mp->strhdr->sh_size;
2561 mp->symspace = kobj_zalloc(mp->symsize, KM_WAIT|KM_SCRATCH);
2562
2563 mp->symtbl = mp->symspace;
2564 mp->buckets = (symid_t *)(mp->symtbl + mp->symhdr->sh_size);
2565 mp->chains = mp->buckets + mp->hashsize;
2566 mp->strings = (char *)(mp->chains + mp->nsyms);
2567
2568 if (kobj_read_file(file, mp->symtbl,
2569 mp->symhdr->sh_size, mp->symhdr->sh_offset) < 0 ||
2570 kobj_read_file(file, mp->strings,
2571 mp->strhdr->sh_size, mp->strhdr->sh_offset) < 0)
2572 return (-1);
2573
2574 /*
2575 * loop through the symbol table adjusting values to account
2576 * for where each section got loaded into memory. Also
2577 * fill in the hash table.
2578 */
2579 for (i = 1; i < mp->nsyms; i++) {
2580 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
2581 if (sp->st_shndx < SHN_LORESERVE) {
2582 if (sp->st_shndx >= mp->hdr.e_shnum) {
2583 _kobj_printf(ops, "%s bad shndx ",
2584 file->_name);
2585 _kobj_printf(ops, "in symbol %d\n", i);
2586 return (-1);
2587 }
2588 shp = (Shdr *)
2589 (mp->shdrs +
2590 sp->st_shndx * mp->hdr.e_shentsize);
2591 if (!(mp->flags & KOBJ_EXEC))
2592 sp->st_value += shp->sh_addr;
2593 }
2594
2595 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
2596 continue;
2597 if (sp->st_name >= mp->strhdr->sh_size)
2598 return (-1);
2599
2600 symname = mp->strings + sp->st_name;
2601
2602 if (!(mp->flags & KOBJ_EXEC) &&
2603 ELF_ST_BIND(sp->st_info) == STB_GLOBAL) {
2604 ksp = kobj_lookup_all(mp, symname, 0);
2605
2606 if (ksp && ELF_ST_BIND(ksp->st_info) == STB_GLOBAL &&
2607 !kobj_suppress_warning(symname) &&
2608 sp->st_shndx != SHN_UNDEF &&
2609 sp->st_shndx != SHN_COMMON &&
2610 ksp->st_shndx != SHN_UNDEF &&
2611 ksp->st_shndx != SHN_COMMON) {
2612 /*
2613 * Unless this symbol is a stub, it's multiply
2614 * defined. Multiply-defined symbols are
2615 * usually bad, but some objects (kmdb) have
2616 * a legitimate need to have their own
2617 * copies of common functions.
2618 */
2619 if ((standalone ||
2620 ksp->st_value < (uintptr_t)stubs_base ||
2621 ksp->st_value >= (uintptr_t)stubs_end) &&
2622 !(mp->flags & KOBJ_IGNMULDEF)) {
2623 _kobj_printf(ops,
2624 "%s symbol ", file->_name);
2625 _kobj_printf(ops,
2626 "%s multiply defined\n", symname);
2627 }
2628 }
2629 }
2630
2631 sym_insert(mp, symname, i);
2632 }
2633
2634 return (0);
2635 }
2636
2637 static int
2638 get_ctf(struct module *mp, struct _buf *file)
2639 {
2640 char *shstrtab, *ctfdata;
2641 size_t shstrlen;
2642 Shdr *shp;
2643 uint_t i;
2644
2645 if (_moddebug & MODDEBUG_NOCTF)
2646 return (0); /* do not attempt to even load CTF data */
2647
2648 if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) {
2649 _kobj_printf(ops, "krtld: get_ctf: %s, ",
2650 mp->filename);
2651 _kobj_printf(ops, "corrupt e_shstrndx %u\n",
2652 mp->hdr.e_shstrndx);
2653 return (-1);
2654 }
2655
2656 shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize);
2657 shstrlen = shp->sh_size;
2658 shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP);
2659
2660 if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) {
2661 _kobj_printf(ops, "krtld: get_ctf: %s, ",
2662 mp->filename);
2663 _kobj_printf(ops, "error reading section %u\n",
2664 mp->hdr.e_shstrndx);
2665 kobj_free(shstrtab, shstrlen);
2666 return (-1);
2667 }
2668
2669 for (i = 0; i < mp->hdr.e_shnum; i++) {
2670 shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize);
2671
2672 if (shp->sh_size != 0 && shp->sh_name < shstrlen &&
2673 strcmp(shstrtab + shp->sh_name, ".SUNW_ctf") == 0) {
2674 ctfdata = kobj_alloc(shp->sh_size, KM_WAIT|KM_SCRATCH);
2675
2676 if (kobj_read_file(file, ctfdata, shp->sh_size,
2677 shp->sh_offset) < 0) {
2678 _kobj_printf(ops, "krtld: get_ctf: %s, error "
2679 "reading .SUNW_ctf data\n", mp->filename);
2680 kobj_free(ctfdata, shp->sh_size);
2681 kobj_free(shstrtab, shstrlen);
2682 return (-1);
2683 }
2684
2685 mp->ctfdata = ctfdata;
2686 mp->ctfsize = shp->sh_size;
2687 break;
2688 }
2689 }
2690
2691 kobj_free(shstrtab, shstrlen);
2692 return (0);
2693 }
2694
2695 #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */
2696
2697 /*
2698 * Return the hash of the ELF sections that are memory resident.
2699 * i.e. text and data. We skip a SHT_NOBITS section since it occupies
2700 * no space in the file. We use SHA1 here since libelfsign uses
2701 * it and both places need to use the same algorithm.
2702 */
2703 static void
2704 crypto_es_hash(struct module *mp, char *hash, char *shstrtab)
2705 {
2706 uint_t shn;
2707 Shdr *shp;
2708 SHA1_CTX ctx;
2709
2710 SHA1Init(&ctx);
2711
2712 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2713 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2714 if (!(shp->sh_flags & SHF_ALLOC) || shp->sh_size == 0)
2715 continue;
2716
2717 /*
2718 * The check should ideally be shp->sh_type == SHT_NOBITS.
2719 * However, we can't do that check here as get_progbits()
2720 * resets the type.
2721 */
2722 if (strcmp(shstrtab + shp->sh_name, ".bss") == 0)
2723 continue;
2724 #ifdef KOBJ_DEBUG
2725 if (kobj_debug & D_DEBUG)
2726 _kobj_printf(ops,
2727 "krtld: crypto_es_hash: updating hash with"
2728 " %s data size=%d\n", shstrtab + shp->sh_name,
2729 shp->sh_size);
2730 #endif
2731 ASSERT(shp->sh_addr != NULL);
2732 SHA1Update(&ctx, (const uint8_t *)shp->sh_addr, shp->sh_size);
2733 }
2734
2735 SHA1Final((uchar_t *)hash, &ctx);
2736 }
2737
2738 /*
2739 * Get the .SUNW_signature section for the module, it it exists.
2740 *
2741 * This section exists only for crypto modules. None of the
2742 * primary modules have this section currently.
2743 */
2744 static void
2745 get_signature(struct module *mp, struct _buf *file)
2746 {
2747 char *shstrtab, *sigdata = NULL;
2748 size_t shstrlen;
2749 Shdr *shp;
2750 uint_t i;
2751
2752 if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) {
2753 _kobj_printf(ops, "krtld: get_signature: %s, ",
2754 mp->filename);
2755 _kobj_printf(ops, "corrupt e_shstrndx %u\n",
2756 mp->hdr.e_shstrndx);
2757 return;
2758 }
2759
2760 shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize);
2761 shstrlen = shp->sh_size;
2762 shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP);
2763
2764 if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) {
2765 _kobj_printf(ops, "krtld: get_signature: %s, ",
2766 mp->filename);
2767 _kobj_printf(ops, "error reading section %u\n",
2768 mp->hdr.e_shstrndx);
2769 kobj_free(shstrtab, shstrlen);
2770 return;
2771 }
2772
2773 for (i = 0; i < mp->hdr.e_shnum; i++) {
2774 shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize);
2775 if (shp->sh_size != 0 && shp->sh_name < shstrlen &&
2776 strcmp(shstrtab + shp->sh_name,
2777 ELF_SIGNATURE_SECTION) == 0) {
2778 filesig_vers_t filesig_version;
2779 size_t sigsize = shp->sh_size + SHA1_DIGEST_LENGTH;
2780 sigdata = kobj_alloc(sigsize, KM_WAIT|KM_SCRATCH);
2781
2782 if (kobj_read_file(file, sigdata, shp->sh_size,
2783 shp->sh_offset) < 0) {
2784 _kobj_printf(ops, "krtld: get_signature: %s,"
2785 " error reading .SUNW_signature data\n",
2786 mp->filename);
2787 kobj_free(sigdata, sigsize);
2788 kobj_free(shstrtab, shstrlen);
2789 return;
2790 }
2791 filesig_version = ((struct filesignatures *)sigdata)->
2792 filesig_sig.filesig_version;
2793 if (!(filesig_version == FILESIG_VERSION1 ||
2794 filesig_version == FILESIG_VERSION3)) {
2795 /* skip versions we don't understand */
2796 kobj_free(sigdata, sigsize);
2797 kobj_free(shstrtab, shstrlen);
2798 return;
2799 }
2800
2801 mp->sigdata = sigdata;
2802 mp->sigsize = sigsize;
2803 break;
2804 }
2805 }
2806
2807 if (sigdata != NULL) {
2808 crypto_es_hash(mp, sigdata + shp->sh_size, shstrtab);
2809 }
2810
2811 kobj_free(shstrtab, shstrlen);
2812 }
2813
2814 static void
2815 add_dependent(struct module *mp, struct module *dep)
2816 {
2817 struct module_list *lp;
2818
2819 for (lp = mp->head; lp; lp = lp->next) {
2820 if (lp->mp == dep)
2821 return; /* already on the list */
2822 }
2823
2824 if (lp == NULL) {
2825 lp = kobj_zalloc(sizeof (*lp), KM_WAIT);
2826
2827 lp->mp = dep;
2828 lp->next = NULL;
2829 if (mp->tail)
2830 mp->tail->next = lp;
2831 else
2832 mp->head = lp;
2833 mp->tail = lp;
2834 }
2835 }
2836
2837 static int
2838 do_dependents(struct modctl *modp, char *modname, size_t modnamelen)
2839 {
2840 struct module *mp;
2841 struct modctl *req;
2842 char *d, *p, *q;
2843 int c;
2844 char *err_modname = NULL;
2845
2846 mp = modp->mod_mp;
2847
2848 if ((p = mp->depends_on) == NULL)
2849 return (0);
2850
2851 for (;;) {
2852 /*
2853 * Skip space.
2854 */
2855 while (*p && (*p == ' ' || *p == '\t'))
2856 p++;
2857 /*
2858 * Get module name.
2859 */
2860 d = p;
2861 q = modname;
2862 c = 0;
2863 while (*p && *p != ' ' && *p != '\t') {
2864 if (c < modnamelen - 1) {
2865 *q++ = *p;
2866 c++;
2867 }
2868 p++;
2869 }
2870
2871 if (q == modname)
2872 break;
2873
2874 if (c == modnamelen - 1) {
2875 char *dep = kobj_alloc(p - d + 1, KM_WAIT|KM_TMP);
2876
2877 (void) strncpy(dep, d, p - d + 1);
2878 dep[p - d] = '\0';
2879
2880 _kobj_printf(ops, "%s: dependency ", modp->mod_modname);
2881 _kobj_printf(ops, "'%s' too long ", dep);
2882 _kobj_printf(ops, "(max %d chars)\n", modnamelen);
2883
2884 kobj_free(dep, p - d + 1);
2885
2886 return (-1);
2887 }
2888
2889 *q = '\0';
2890 if ((req = mod_load_requisite(modp, modname)) == NULL) {
2891 #ifndef KOBJ_DEBUG
2892 if (_moddebug & MODDEBUG_LOADMSG) {
2893 #endif /* KOBJ_DEBUG */
2894 _kobj_printf(ops,
2895 "%s: unable to resolve dependency, ",
2896 modp->mod_modname);
2897 _kobj_printf(ops, "cannot load module '%s'\n",
2898 modname);
2899 #ifndef KOBJ_DEBUG
2900 }
2901 #endif /* KOBJ_DEBUG */
2902 if (err_modname == NULL) {
2903 /*
2904 * This must be the same size as the modname
2905 * one.
2906 */
2907 err_modname = kobj_zalloc(MODMAXNAMELEN,
2908 KM_WAIT);
2909
2910 /*
2911 * We can use strcpy() here without fearing
2912 * the NULL terminator because the size of
2913 * err_modname is the same as one of modname,
2914 * and it's filled with zeros.
2915 */
2916 (void) strcpy(err_modname, modname);
2917 }
2918 continue;
2919 }
2920
2921 add_dependent(mp, req->mod_mp);
2922 mod_release_mod(req);
2923
2924 }
2925
2926 if (err_modname != NULL) {
2927 /*
2928 * Copy the first module name where you detect an error to keep
2929 * its behavior the same as before.
2930 * This way keeps minimizing the memory use for error
2931 * modules, and this might be important at boot time because
2932 * the memory usage is a crucial factor for booting in most
2933 * cases. You can expect more verbose messages when using
2934 * a debug kernel or setting a bit in moddebug.
2935 */
2936 bzero(modname, MODMAXNAMELEN);
2937 (void) strcpy(modname, err_modname);
2938 kobj_free(err_modname, MODMAXNAMELEN);
2939 return (-1);
2940 }
2941
2942 return (0);
2943 }
2944
2945 static int
2946 do_common(struct module *mp)
2947 {
2948 int err;
2949
2950 /*
2951 * first time through, assign all symbols defined in other
2952 * modules, and count up how much common space will be needed
2953 * (bss_size and bss_align)
2954 */
2955 if ((err = do_symbols(mp, 0)) < 0)
2956 return (err);
2957 /*
2958 * increase bss_size by the maximum delta that could be
2959 * computed by the ALIGN below
2960 */
2961 mp->bss_size += mp->bss_align;
2962 if (mp->bss_size) {
2963 if (standalone)
2964 mp->bss = (uintptr_t)kobj_segbrk(&_edata, mp->bss_size,
2965 MINALIGN, 0);
2966 else
2967 mp->bss = (uintptr_t)vmem_alloc(data_arena,
2968 mp->bss_size, VM_SLEEP | VM_BESTFIT);
2969 bzero((void *)mp->bss, mp->bss_size);
2970 /* now assign addresses to all common symbols */
2971 if ((err = do_symbols(mp, ALIGN(mp->bss, mp->bss_align))) < 0)
2972 return (err);
2973 }
2974 return (0);
2975 }
2976
2977 static int
2978 do_symbols(struct module *mp, Elf64_Addr bss_base)
2979 {
2980 int bss_align;
2981 uintptr_t bss_ptr;
2982 int err;
2983 int i;
2984 Sym *sp, *sp1;
2985 char *name;
2986 int assign;
2987 int resolved = 1;
2988
2989 /*
2990 * Nothing left to do (optimization).
2991 */
2992 if (mp->flags & KOBJ_RESOLVED)
2993 return (0);
2994
2995 assign = (bss_base) ? 1 : 0;
2996 bss_ptr = bss_base;
2997 bss_align = 0;
2998 err = 0;
2999
3000 for (i = 1; i < mp->nsyms; i++) {
3001 sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * i);
3002 /*
3003 * we know that st_name is in bounds, since get_sections
3004 * has already checked all of the symbols
3005 */
3006 name = mp->strings + sp->st_name;
3007 if (sp->st_shndx != SHN_UNDEF && sp->st_shndx != SHN_COMMON)
3008 continue;
3009 #if defined(__sparc)
3010 /*
3011 * Register symbols are ignored in the kernel
3012 */
3013 if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) {
3014 if (*name != '\0') {
3015 _kobj_printf(ops, "%s: named REGISTER symbol ",
3016 mp->filename);
3017 _kobj_printf(ops, "not supported '%s'\n",
3018 name);
3019 err = DOSYM_UNDEF;
3020 }
3021 continue;
3022 }
3023 #endif /* __sparc */
3024 /*
3025 * TLS symbols are ignored in the kernel
3026 */
3027 if (ELF_ST_TYPE(sp->st_info) == STT_TLS) {
3028 _kobj_printf(ops, "%s: TLS symbol ",
3029 mp->filename);
3030 _kobj_printf(ops, "not supported '%s'\n",
3031 name);
3032 err = DOSYM_UNDEF;
3033 continue;
3034 }
3035
3036 if (ELF_ST_BIND(sp->st_info) != STB_LOCAL) {
3037 if ((sp1 = kobj_lookup_all(mp, name, 0)) != NULL) {
3038 sp->st_shndx = SHN_ABS;
3039 sp->st_value = sp1->st_value;
3040 continue;
3041 }
3042 }
3043
3044 if (sp->st_shndx == SHN_UNDEF) {
3045 resolved = 0;
3046
3047 /*
3048 * Skip over sdt probes and smap calls,
3049 * they're relocated later.
3050 */
3051 if (strncmp(name, sdt_prefix, strlen(sdt_prefix)) == 0)
3052 continue;
3053 #if defined(__x86)
3054 if (strcmp(name, "smap_enable") == 0 ||
3055 strcmp(name, "smap_disable") == 0)
3056 continue;
3057 #endif /* defined(__x86) */
3058
3059
3060 /*
3061 * If it's not a weak reference and it's
3062 * not a primary object, it's an error.
3063 * (Primary objects may take more than
3064 * one pass to resolve)
3065 */
3066 if (!(mp->flags & KOBJ_PRIM) &&
3067 ELF_ST_BIND(sp->st_info) != STB_WEAK) {
3068 _kobj_printf(ops, "%s: undefined symbol",
3069 mp->filename);
3070 _kobj_printf(ops, " '%s'\n", name);
3071 /*
3072 * Try to determine whether this symbol
3073 * represents a dependency on obsolete
3074 * unsafe driver support. This is just
3075 * to make the warning more informative.
3076 */
3077 if (strcmp(name, "sleep") == 0 ||
3078 strcmp(name, "unsleep") == 0 ||
3079 strcmp(name, "wakeup") == 0 ||
3080 strcmp(name, "bsd_compat_ioctl") == 0 ||
3081 strcmp(name, "unsafe_driver") == 0 ||
3082 strncmp(name, "spl", 3) == 0 ||
3083 strncmp(name, "i_ddi_spl", 9) == 0)
3084 err = DOSYM_UNSAFE;
3085 if (err == 0)
3086 err = DOSYM_UNDEF;
3087 }
3088 continue;
3089 }
3090 /*
3091 * It's a common symbol - st_value is the
3092 * required alignment.
3093 */
3094 if (sp->st_value > bss_align)
3095 bss_align = sp->st_value;
3096 bss_ptr = ALIGN(bss_ptr, sp->st_value);
3097 if (assign) {
3098 sp->st_shndx = SHN_ABS;
3099 sp->st_value = bss_ptr;
3100 }
3101 bss_ptr += sp->st_size;
3102 }
3103 if (err)
3104 return (err);
3105 if (assign == 0 && mp->bss == 0) {
3106 mp->bss_align = bss_align;
3107 mp->bss_size = bss_ptr;
3108 } else if (resolved) {
3109 mp->flags |= KOBJ_RESOLVED;
3110 }
3111
3112 return (0);
3113 }
3114
3115 uint_t
3116 kobj_hash_name(const char *p)
3117 {
3118 uint_t g;
3119 uint_t hval;
3120
3121 hval = 0;
3122 while (*p) {
3123 hval = (hval << 4) + *p++;
3124 if ((g = (hval & 0xf0000000)) != 0)
3125 hval ^= g >> 24;
3126 hval &= ~g;
3127 }
3128 return (hval);
3129 }
3130
3131 /* look for name in all modules */
3132 uintptr_t
3133 kobj_getsymvalue(char *name, int kernelonly)
3134 {
3135 Sym *sp;
3136 struct modctl *modp;
3137 struct module *mp;
3138 uintptr_t value = 0;
3139
3140 if ((sp = kobj_lookup_kernel(name)) != NULL)
3141 return ((uintptr_t)sp->st_value);
3142
3143 if (kernelonly)
3144 return (0); /* didn't find it in the kernel so give up */
3145
3146 mutex_enter(&mod_lock);
3147 modp = &modules;
3148 do {
3149 mp = (struct module *)modp->mod_mp;
3150 if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded &&
3151 (sp = lookup_one(mp, name))) {
3152 value = (uintptr_t)sp->st_value;
3153 break;
3154 }
3155 } while ((modp = modp->mod_next) != &modules);
3156 mutex_exit(&mod_lock);
3157 return (value);
3158 }
3159
3160 /* look for a symbol near value. */
3161 char *
3162 kobj_getsymname(uintptr_t value, ulong_t *offset)
3163 {
3164 char *name = NULL;
3165 struct modctl *modp;
3166
3167 struct modctl_list *lp;
3168 struct module *mp;
3169
3170 /*
3171 * Loop through the primary kernel modules.
3172 */
3173 for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) {
3174 mp = mod(lp);
3175
3176 if ((name = kobj_searchsym(mp, value, offset)) != NULL)
3177 return (name);
3178 }
3179
3180 mutex_enter(&mod_lock);
3181 modp = &modules;
3182 do {
3183 mp = (struct module *)modp->mod_mp;
3184 if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded &&
3185 (name = kobj_searchsym(mp, value, offset)))
3186 break;
3187 } while ((modp = modp->mod_next) != &modules);
3188 mutex_exit(&mod_lock);
3189 return (name);
3190 }
3191
3192 /* return address of symbol and size */
3193
3194 uintptr_t
3195 kobj_getelfsym(char *name, void *mp, int *size)
3196 {
3197 Sym *sp;
3198
3199 if (mp == NULL)
3200 sp = kobj_lookup_kernel(name);
3201 else
3202 sp = lookup_one(mp, name);
3203
3204 if (sp == NULL)
3205 return (0);
3206
3207 *size = (int)sp->st_size;
3208 return ((uintptr_t)sp->st_value);
3209 }
3210
3211 uintptr_t
3212 kobj_lookup(struct module *mod, const char *name)
3213 {
3214 Sym *sp;
3215
3216 sp = lookup_one(mod, name);
3217
3218 if (sp == NULL)
3219 return (0);
3220
3221 return ((uintptr_t)sp->st_value);
3222 }
3223
3224 char *
3225 kobj_searchsym(struct module *mp, uintptr_t value, ulong_t *offset)
3226 {
3227 Sym *symtabptr;
3228 char *strtabptr;
3229 int symnum;
3230 Sym *sym;
3231 Sym *cursym;
3232 uintptr_t curval;
3233
3234 *offset = (ulong_t)-1l; /* assume not found */
3235 cursym = NULL;
3236
3237 if (kobj_addrcheck(mp, (void *)value) != 0)
3238 return (NULL); /* not in this module */
3239
3240 strtabptr = mp->strings;
3241 symtabptr = (Sym *)mp->symtbl;
3242
3243 /*
3244 * Scan the module's symbol table for a symbol <= value
3245 */
3246 for (symnum = 1, sym = symtabptr + 1;
3247 symnum < mp->nsyms; symnum++, sym = (Sym *)
3248 ((uintptr_t)sym + mp->symhdr->sh_entsize)) {
3249 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
3250 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
3251 continue;
3252 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
3253 ELF_ST_TYPE(sym->st_info) != STT_FUNC)
3254 continue;
3255 }
3256
3257 curval = (uintptr_t)sym->st_value;
3258
3259 if (curval > value)
3260 continue;
3261
3262 /*
3263 * If one or both are functions...
3264 */
3265 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC || (cursym != NULL &&
3266 ELF_ST_TYPE(cursym->st_info) == STT_FUNC)) {
3267 /* Ignore if the address is out of the bounds */
3268 if (value - sym->st_value >= sym->st_size)
3269 continue;
3270
3271 if (cursym != NULL &&
3272 ELF_ST_TYPE(cursym->st_info) == STT_FUNC) {
3273 /* Prefer the function to the non-function */
3274 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
3275 continue;
3276
3277 /* Prefer the larger of the two functions */
3278 if (sym->st_size <= cursym->st_size)
3279 continue;
3280 }
3281 } else if (value - curval >= *offset) {
3282 continue;
3283 }
3284
3285 *offset = (ulong_t)(value - curval);
3286 cursym = sym;
3287 }
3288 if (cursym == NULL)
3289 return (NULL);
3290
3291 return (strtabptr + cursym->st_name);
3292 }
3293
3294 Sym *
3295 kobj_lookup_all(struct module *mp, char *name, int include_self)
3296 {
3297 Sym *sp;
3298 struct module_list *mlp;
3299 struct modctl_list *clp;
3300 struct module *mmp;
3301
3302 if (include_self && (sp = lookup_one(mp, name)) != NULL)
3303 return (sp);
3304
3305 for (mlp = mp->head; mlp; mlp = mlp->next) {
3306 if ((sp = lookup_one(mlp->mp, name)) != NULL &&
3307 ELF_ST_BIND(sp->st_info) != STB_LOCAL)
3308 return (sp);
3309 }
3310
3311 /*
3312 * Loop through the primary kernel modules.
3313 */
3314 for (clp = kobj_lm_lookup(KOBJ_LM_PRIMARY); clp; clp = clp->modl_next) {
3315 mmp = mod(clp);
3316
3317 if (mmp == NULL || mp == mmp)
3318 continue;
3319
3320 if ((sp = lookup_one(mmp, name)) != NULL &&
3321 ELF_ST_BIND(sp->st_info) != STB_LOCAL)
3322 return (sp);
3323 }
3324 return (NULL);
3325 }
3326
3327 Sym *
3328 kobj_lookup_kernel(const char *name)
3329 {
3330 struct modctl_list *lp;
3331 struct module *mp;
3332 Sym *sp;
3333
3334 /*
3335 * Loop through the primary kernel modules.
3336 */
3337 for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) {
3338 mp = mod(lp);
3339
3340 if (mp == NULL)
3341 continue;
3342
3343 if ((sp = lookup_one(mp, name)) != NULL)
3344 return (sp);
3345 }
3346 return (NULL);
3347 }
3348
3349 static Sym *
3350 lookup_one(struct module *mp, const char *name)
3351 {
3352 symid_t *ip;
3353 char *name1;
3354 Sym *sp;
3355
3356 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3357 ip = &mp->chains[*ip]) {
3358 sp = (Sym *)(mp->symtbl +
3359 mp->symhdr->sh_entsize * *ip);
3360 name1 = mp->strings + sp->st_name;
3361 if (strcmp(name, name1) == 0 &&
3362 ELF_ST_TYPE(sp->st_info) != STT_FILE &&
3363 sp->st_shndx != SHN_UNDEF &&
3364 sp->st_shndx != SHN_COMMON)
3365 return (sp);
3366 }
3367 return (NULL);
3368 }
3369
3370 /*
3371 * Lookup a given symbol pointer in the module's symbol hash. If the symbol
3372 * is hashed, return the symbol pointer; otherwise return NULL.
3373 */
3374 static Sym *
3375 sym_lookup(struct module *mp, Sym *ksp)
3376 {
3377 char *name = mp->strings + ksp->st_name;
3378 symid_t *ip;
3379 Sym *sp;
3380
3381 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3382 ip = &mp->chains[*ip]) {
3383 sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * *ip);
3384 if (sp == ksp)
3385 return (ksp);
3386 }
3387 return (NULL);
3388 }
3389
3390 static void
3391 sym_insert(struct module *mp, char *name, symid_t index)
3392 {
3393 symid_t *ip;
3394
3395 #ifdef KOBJ_DEBUG
3396 if (kobj_debug & D_SYMBOLS) {
3397 static struct module *lastmp = NULL;
3398 Sym *sp;
3399 if (lastmp != mp) {
3400 _kobj_printf(ops,
3401 "krtld: symbol entry: file=%s\n",
3402 mp->filename);
3403 _kobj_printf(ops,
3404 "krtld:\tsymndx\tvalue\t\t"
3405 "symbol name\n");
3406 lastmp = mp;
3407 }
3408 sp = (Sym *)(mp->symtbl +
3409 index * mp->symhdr->sh_entsize);
3410 _kobj_printf(ops, "krtld:\t[%3d]", index);
3411 _kobj_printf(ops, "\t0x%lx", sp->st_value);
3412 _kobj_printf(ops, "\t%s\n", name);
3413 }
3414 #endif
3415
3416 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3417 ip = &mp->chains[*ip]) {
3418 ;
3419 }
3420 *ip = index;
3421 }
3422
3423 struct modctl *
3424 kobj_boot_mod_lookup(const char *modname)
3425 {
3426 struct modctl *mctl = kobj_modules;
3427
3428 do {
3429 if (strcmp(modname, mctl->mod_modname) == 0)
3430 return (mctl);
3431 } while ((mctl = mctl->mod_next) != kobj_modules);
3432
3433 return (NULL);
3434 }
3435
3436 /*
3437 * Determine if the module exists.
3438 */
3439 int
3440 kobj_path_exists(char *name, int use_path)
3441 {
3442 struct _buf *file;
3443
3444 file = kobj_open_path(name, use_path, 1);
3445 #ifdef MODDIR_SUFFIX
3446 if (file == (struct _buf *)-1)
3447 file = kobj_open_path(name, use_path, 0);
3448 #endif /* MODDIR_SUFFIX */
3449 if (file == (struct _buf *)-1)
3450 return (0);
3451 kobj_close_file(file);
3452 return (1);
3453 }
3454
3455 /*
3456 * fullname is dynamically allocated to be able to hold the
3457 * maximum size string that can be constructed from name.
3458 * path is exactly like the shell PATH variable.
3459 */
3460 struct _buf *
3461 kobj_open_path(char *name, int use_path, int use_moddir_suffix)
3462 {
3463 char *p, *q;
3464 char *pathp;
3465 char *pathpsave;
3466 char *fullname;
3467 int maxpathlen;
3468 struct _buf *file;
3469
3470 #if !defined(MODDIR_SUFFIX)
3471 use_moddir_suffix = B_FALSE;
3472 #endif
3473
3474 if (!use_path)
3475 pathp = ""; /* use name as specified */
3476 else
3477 pathp = kobj_module_path;
3478 /* use configured default path */
3479
3480 pathpsave = pathp; /* keep this for error reporting */
3481
3482 /*
3483 * Allocate enough space for the largest possible fullname.
3484 * since path is of the form <directory> : <directory> : ...
3485 * we're potentially allocating a little more than we need to
3486 * but we'll allocate the exact amount when we find the right directory.
3487 * (The + 3 below is one for NULL terminator and one for the '/'
3488 * we might have to add at the beginning of path and one for
3489 * the '/' between path and name.)
3490 */
3491 maxpathlen = strlen(pathp) + strlen(name) + 3;
3492 /* sizeof includes null */
3493 maxpathlen += sizeof (slash_moddir_suffix_slash) - 1;
3494 fullname = kobj_zalloc(maxpathlen, KM_WAIT);
3495
3496 for (;;) {
3497 p = fullname;
3498 if (*pathp != '\0' && *pathp != '/')
3499 *p++ = '/'; /* path must start with '/' */
3500 while (*pathp && *pathp != ':' && *pathp != ' ')
3501 *p++ = *pathp++;
3502 if (p != fullname && p[-1] != '/')
3503 *p++ = '/';
3504 if (use_moddir_suffix) {
3505 char *b = basename(name);
3506 char *s;
3507
3508 /* copy everything up to the base name */
3509 q = name;
3510 while (q != b && *q)
3511 *p++ = *q++;
3512 s = slash_moddir_suffix_slash;
3513 while (*s)
3514 *p++ = *s++;
3515 /* copy the rest */
3516 while (*b)
3517 *p++ = *b++;
3518 } else {
3519 q = name;
3520 while (*q)
3521 *p++ = *q++;
3522 }
3523 *p = 0;
3524 if ((file = kobj_open_file(fullname)) != (struct _buf *)-1) {
3525 kobj_free(fullname, maxpathlen);
3526 return (file);
3527 }
3528 while (*pathp == ' ' || *pathp == ':')
3529 pathp++;
3530 if (*pathp == 0)
3531 break;
3532
3533 }
3534 kobj_free(fullname, maxpathlen);
3535 if (_moddebug & MODDEBUG_ERRMSG) {
3536 _kobj_printf(ops, "can't open %s,", name);
3537 _kobj_printf(ops, " path is %s\n", pathpsave);
3538 }
3539 return ((struct _buf *)-1);
3540 }
3541
3542 intptr_t
3543 kobj_open(char *filename)
3544 {
3545 struct vnode *vp;
3546 int fd;
3547
3548 if (_modrootloaded) {
3549 struct kobjopen_tctl *ltp = kobjopen_alloc(filename);
3550 int Errno;
3551
3552 /*
3553 * Hand off the open to a thread who has a
3554 * stack size capable handling the request.
3555 */
3556 if (curthread != &t0) {
3557 (void) thread_create(NULL, DEFAULTSTKSZ * 2,
3558 kobjopen_thread, ltp, 0, &p0, TS_RUN, maxclsyspri);
3559 sema_p(<p->sema);
3560 Errno = ltp->Errno;
3561 vp = ltp->vp;
3562 } else {
3563 /*
3564 * 1098067: module creds should not be those of the
3565 * caller
3566 */
3567 cred_t *saved_cred = curthread->t_cred;
3568 curthread->t_cred = kcred;
3569 Errno = vn_openat(filename, UIO_SYSSPACE, FREAD, 0, &vp,
3570 0, 0, rootdir, -1);
3571 curthread->t_cred = saved_cred;
3572 }
3573 kobjopen_free(ltp);
3574
3575 if (Errno) {
3576 if (_moddebug & MODDEBUG_ERRMSG) {
3577 _kobj_printf(ops,
3578 "kobj_open: vn_open of %s fails, ",
3579 filename);
3580 _kobj_printf(ops, "Errno = %d\n", Errno);
3581 }
3582 return (-1);
3583 } else {
3584 if (_moddebug & MODDEBUG_ERRMSG) {
3585 _kobj_printf(ops, "kobj_open: '%s'", filename);
3586 _kobj_printf(ops, " vp = %p\n", vp);
3587 }
3588 return ((intptr_t)vp);
3589 }
3590 } else {
3591 fd = kobj_boot_open(filename, 0);
3592
3593 if (_moddebug & MODDEBUG_ERRMSG) {
3594 if (fd < 0)
3595 _kobj_printf(ops,
3596 "kobj_open: can't open %s\n", filename);
3597 else {
3598 _kobj_printf(ops, "kobj_open: '%s'", filename);
3599 _kobj_printf(ops, " descr = 0x%x\n", fd);
3600 }
3601 }
3602 return ((intptr_t)fd);
3603 }
3604 }
3605
3606 /*
3607 * Calls to kobj_open() are handled off to this routine as a separate thread.
3608 */
3609 static void
3610 kobjopen_thread(struct kobjopen_tctl *ltp)
3611 {
3612 kmutex_t cpr_lk;
3613 callb_cpr_t cpr_i;
3614
3615 mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
3616 CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "kobjopen");
3617 ltp->Errno = vn_open(ltp->name, UIO_SYSSPACE, FREAD, 0, &(ltp->vp),
3618 0, 0);
3619 sema_v(<p->sema);
3620 mutex_enter(&cpr_lk);
3621 CALLB_CPR_EXIT(&cpr_i);
3622 mutex_destroy(&cpr_lk);
3623 thread_exit();
3624 }
3625
3626 /*
3627 * allocate and initialize a kobjopen thread structure
3628 */
3629 static struct kobjopen_tctl *
3630 kobjopen_alloc(char *filename)
3631 {
3632 struct kobjopen_tctl *ltp = kmem_zalloc(sizeof (*ltp), KM_SLEEP);
3633
3634 ASSERT(filename != NULL);
3635
3636 ltp->name = kmem_alloc(strlen(filename) + 1, KM_SLEEP);
3637 bcopy(filename, ltp->name, strlen(filename) + 1);
3638 sema_init(<p->sema, 0, NULL, SEMA_DEFAULT, NULL);
3639 return (ltp);
3640 }
3641
3642 /*
3643 * free a kobjopen thread control structure
3644 */
3645 static void
3646 kobjopen_free(struct kobjopen_tctl *ltp)
3647 {
3648 sema_destroy(<p->sema);
3649 kmem_free(ltp->name, strlen(ltp->name) + 1);
3650 kmem_free(ltp, sizeof (*ltp));
3651 }
3652
3653 int
3654 kobj_read(intptr_t descr, char *buf, uint_t size, uint_t offset)
3655 {
3656 int stat;
3657 ssize_t resid;
3658
3659 if (_modrootloaded) {
3660 if ((stat = vn_rdwr(UIO_READ, (struct vnode *)descr, buf, size,
3661 (offset_t)offset, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(),
3662 &resid)) != 0) {
3663 _kobj_printf(ops,
3664 "vn_rdwr failed with error 0x%x\n", stat);
3665 return (-1);
3666 }
3667 return (size - resid);
3668 } else {
3669 int count = 0;
3670
3671 if (kobj_boot_seek((int)descr, (off_t)0, offset) != 0) {
3672 _kobj_printf(ops,
3673 "kobj_read: seek 0x%x failed\n", offset);
3674 return (-1);
3675 }
3676
3677 count = kobj_boot_read((int)descr, buf, size);
3678 if (count < size) {
3679 if (_moddebug & MODDEBUG_ERRMSG) {
3680 _kobj_printf(ops,
3681 "kobj_read: req %d bytes, ", size);
3682 _kobj_printf(ops, "got %d\n", count);
3683 }
3684 }
3685 return (count);
3686 }
3687 }
3688
3689 void
3690 kobj_close(intptr_t descr)
3691 {
3692 if (_moddebug & MODDEBUG_ERRMSG)
3693 _kobj_printf(ops, "kobj_close: 0x%lx\n", descr);
3694
3695 if (_modrootloaded) {
3696 struct vnode *vp = (struct vnode *)descr;
3697 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
3698 VN_RELE(vp);
3699 } else
3700 (void) kobj_boot_close((int)descr);
3701 }
3702
3703 int
3704 kobj_fstat(intptr_t descr, struct bootstat *buf)
3705 {
3706 if (buf == NULL)
3707 return (-1);
3708
3709 if (_modrootloaded) {
3710 vattr_t vattr;
3711 struct vnode *vp = (struct vnode *)descr;
3712 if (VOP_GETATTR(vp, &vattr, 0, kcred, NULL) != 0)
3713 return (-1);
3714
3715 /*
3716 * The vattr and bootstat structures are similar, but not
3717 * identical. We do our best to fill in the bootstat structure
3718 * from the contents of vattr (transfering only the ones that
3719 * are obvious.
3720 */
3721
3722 buf->st_mode = (uint32_t)vattr.va_mode;
3723 buf->st_nlink = (uint32_t)vattr.va_nlink;
3724 buf->st_uid = (int32_t)vattr.va_uid;
3725 buf->st_gid = (int32_t)vattr.va_gid;
3726 buf->st_rdev = (uint64_t)vattr.va_rdev;
3727 buf->st_size = (uint64_t)vattr.va_size;
3728 buf->st_atim.tv_sec = (int64_t)vattr.va_atime.tv_sec;
3729 buf->st_atim.tv_nsec = (int64_t)vattr.va_atime.tv_nsec;
3730 buf->st_mtim.tv_sec = (int64_t)vattr.va_mtime.tv_sec;
3731 buf->st_mtim.tv_nsec = (int64_t)vattr.va_mtime.tv_nsec;
3732 buf->st_ctim.tv_sec = (int64_t)vattr.va_ctime.tv_sec;
3733 buf->st_ctim.tv_nsec = (int64_t)vattr.va_ctime.tv_nsec;
3734 buf->st_blksize = (int32_t)vattr.va_blksize;
3735 buf->st_blocks = (int64_t)vattr.va_nblocks;
3736
3737 return (0);
3738 }
3739
3740 return (kobj_boot_fstat((int)descr, buf));
3741 }
3742
3743
3744 struct _buf *
3745 kobj_open_file(char *name)
3746 {
3747 struct _buf *file;
3748 struct compinfo cbuf;
3749 intptr_t fd;
3750
3751 if ((fd = kobj_open(name)) == -1) {
3752 return ((struct _buf *)-1);
3753 }
3754
3755 file = kobj_zalloc(sizeof (struct _buf), KM_WAIT|KM_TMP);
3756 file->_fd = fd;
3757 file->_name = kobj_alloc(strlen(name)+1, KM_WAIT|KM_TMP);
3758 file->_cnt = file->_size = file->_off = 0;
3759 file->_ln = 1;
3760 file->_ptr = file->_base;
3761 (void) strcpy(file->_name, name);
3762
3763 /*
3764 * Before root is mounted, we must check
3765 * for a compressed file and do our own
3766 * buffering.
3767 */
3768 if (_modrootloaded) {
3769 file->_base = kobj_zalloc(MAXBSIZE, KM_WAIT);
3770 file->_bsize = MAXBSIZE;
3771
3772 /* Check if the file is compressed */
3773 file->_iscmp = kobj_is_compressed(fd);
3774 } else {
3775 if (kobj_boot_compinfo(fd, &cbuf) != 0) {
3776 kobj_close_file(file);
3777 return ((struct _buf *)-1);
3778 }
3779 file->_iscmp = cbuf.iscmp;
3780 if (file->_iscmp) {
3781 if (kobj_comp_setup(file, &cbuf) != 0) {
3782 kobj_close_file(file);
3783 return ((struct _buf *)-1);
3784 }
3785 } else {
3786 file->_base = kobj_zalloc(cbuf.blksize, KM_WAIT|KM_TMP);
3787 file->_bsize = cbuf.blksize;
3788 }
3789 }
3790 return (file);
3791 }
3792
3793 static int
3794 kobj_comp_setup(struct _buf *file, struct compinfo *cip)
3795 {
3796 struct comphdr *hdr;
3797
3798 /*
3799 * read the compressed image into memory,
3800 * so we can deompress from there
3801 */
3802 file->_dsize = cip->fsize;
3803 file->_dbuf = kobj_alloc(cip->fsize, KM_WAIT|KM_TMP);
3804 if (kobj_read(file->_fd, file->_dbuf, cip->fsize, 0) != cip->fsize) {
3805 kobj_free(file->_dbuf, cip->fsize);
3806 return (-1);
3807 }
3808
3809 hdr = kobj_comphdr(file);
3810 if (hdr->ch_magic != CH_MAGIC_ZLIB || hdr->ch_version != CH_VERSION ||
3811 hdr->ch_algorithm != CH_ALG_ZLIB || hdr->ch_fsize == 0 ||
3812 !ISP2(hdr->ch_blksize)) {
3813 kobj_free(file->_dbuf, cip->fsize);
3814 return (-1);
3815 }
3816 file->_base = kobj_alloc(hdr->ch_blksize, KM_WAIT|KM_TMP);
3817 file->_bsize = hdr->ch_blksize;
3818 return (0);
3819 }
3820
3821 void
3822 kobj_close_file(struct _buf *file)
3823 {
3824 kobj_close(file->_fd);
3825 if (file->_base != NULL)
3826 kobj_free(file->_base, file->_bsize);
3827 if (file->_dbuf != NULL)
3828 kobj_free(file->_dbuf, file->_dsize);
3829 kobj_free(file->_name, strlen(file->_name)+1);
3830 kobj_free(file, sizeof (struct _buf));
3831 }
3832
3833 int
3834 kobj_read_file(struct _buf *file, char *buf, uint_t size, uint_t off)
3835 {
3836 int b_size, c_size;
3837 int b_off; /* Offset into buffer for start of bcopy */
3838 int count = 0;
3839 int page_addr;
3840
3841 if (_moddebug & MODDEBUG_ERRMSG) {
3842 _kobj_printf(ops, "kobj_read_file: size=%x,", size);
3843 _kobj_printf(ops, " offset=%x at", off);
3844 _kobj_printf(ops, " buf=%x\n", buf);
3845 }
3846
3847 /*
3848 * Handle compressed (gzip for now) file here. First get the
3849 * compressed size, then read the image into memory and finally
3850 * call zlib to decompress the image at the supplied memory buffer.
3851 */
3852 if (file->_iscmp == CH_MAGIC_GZIP) {
3853 ulong_t dlen;
3854 vattr_t vattr;
3855 struct vnode *vp = (struct vnode *)file->_fd;
3856 ssize_t resid;
3857 int err = 0;
3858
3859 if (VOP_GETATTR(vp, &vattr, 0, kcred, NULL) != 0)
3860 return (-1);
3861
3862 file->_dbuf = kobj_alloc(vattr.va_size, KM_WAIT|KM_TMP);
3863 file->_dsize = vattr.va_size;
3864
3865 /* Read the compressed file into memory */
3866 if ((err = vn_rdwr(UIO_READ, vp, file->_dbuf, vattr.va_size,
3867 (offset_t)(0), UIO_SYSSPACE, 0, (rlim64_t)0, CRED(),
3868 &resid)) != 0) {
3869
3870 _kobj_printf(ops, "kobj_read_file :vn_rdwr() failed, "
3871 "error code 0x%x\n", err);
3872 return (-1);
3873 }
3874
3875 dlen = size;
3876
3877 /* Decompress the image at the supplied memory buffer */
3878 if ((err = z_uncompress(buf, &dlen, file->_dbuf,
3879 vattr.va_size)) != Z_OK) {
3880 _kobj_printf(ops, "kobj_read_file: z_uncompress "
3881 "failed, error code : 0x%x\n", err);
3882 return (-1);
3883 }
3884
3885 if (dlen != size) {
3886 _kobj_printf(ops, "kobj_read_file: z_uncompress "
3887 "failed to uncompress (size returned 0x%x , "
3888 "expected size: 0x%x)\n", dlen, size);
3889 return (-1);
3890 }
3891
3892 return (0);
3893 }
3894
3895 while (size) {
3896 page_addr = F_PAGE(file, off);
3897 b_size = file->_size;
3898 /*
3899 * If we have the filesystem page the caller's referring to
3900 * and we have something in the buffer,
3901 * satisfy as much of the request from the buffer as we can.
3902 */
3903 if (page_addr == file->_off && b_size > 0) {
3904 b_off = B_OFFSET(file, off);
3905 c_size = b_size - b_off;
3906 /*
3907 * If there's nothing to copy, we're at EOF.
3908 */
3909 if (c_size <= 0)
3910 break;
3911 if (c_size > size)
3912 c_size = size;
3913 if (buf) {
3914 if (_moddebug & MODDEBUG_ERRMSG)
3915 _kobj_printf(ops, "copying %x bytes\n",
3916 c_size);
3917 bcopy(file->_base+b_off, buf, c_size);
3918 size -= c_size;
3919 off += c_size;
3920 buf += c_size;
3921 count += c_size;
3922 } else {
3923 _kobj_printf(ops, "kobj_read: system error");
3924 count = -1;
3925 break;
3926 }
3927 } else {
3928 /*
3929 * If the caller's offset is page aligned and
3930 * the caller want's at least a filesystem page and
3931 * the caller provided a buffer,
3932 * read directly into the caller's buffer.
3933 */
3934 if (page_addr == off &&
3935 (c_size = F_BLKS(file, size)) && buf) {
3936 c_size = kobj_read_blks(file, buf, c_size,
3937 page_addr);
3938 if (c_size < 0) {
3939 count = -1;
3940 break;
3941 }
3942 count += c_size;
3943 if (c_size != F_BLKS(file, size))
3944 break;
3945 size -= c_size;
3946 off += c_size;
3947 buf += c_size;
3948 /*
3949 * Otherwise, read into our buffer and copy next time
3950 * around the loop.
3951 */
3952 } else {
3953 file->_off = page_addr;
3954 c_size = kobj_read_blks(file, file->_base,
3955 file->_bsize, page_addr);
3956 file->_ptr = file->_base;
3957 file->_cnt = c_size;
3958 file->_size = c_size;
3959 /*
3960 * If a _filbuf call or nothing read, break.
3961 */
3962 if (buf == NULL || c_size <= 0) {
3963 count = c_size;
3964 break;
3965 }
3966 }
3967 if (_moddebug & MODDEBUG_ERRMSG)
3968 _kobj_printf(ops, "read %x bytes\n", c_size);
3969 }
3970 }
3971 if (_moddebug & MODDEBUG_ERRMSG)
3972 _kobj_printf(ops, "count = %x\n", count);
3973
3974 return (count);
3975 }
3976
3977 static int
3978 kobj_read_blks(struct _buf *file, char *buf, uint_t size, uint_t off)
3979 {
3980 int ret;
3981
3982 ASSERT(B_OFFSET(file, size) == 0 && B_OFFSET(file, off) == 0);
3983 if (file->_iscmp) {
3984 uint_t blks;
3985 int nret;
3986
3987 ret = 0;
3988 for (blks = size / file->_bsize; blks != 0; blks--) {
3989 nret = kobj_uncomp_blk(file, buf, off);
3990 if (nret == -1)
3991 return (-1);
3992 buf += nret;
3993 off += nret;
3994 ret += nret;
3995 if (nret < file->_bsize)
3996 break;
3997 }
3998 } else
3999 ret = kobj_read(file->_fd, buf, size, off);
4000 return (ret);
4001 }
4002
4003 static int
4004 kobj_uncomp_blk(struct _buf *file, char *buf, uint_t off)
4005 {
4006 struct comphdr *hdr = kobj_comphdr(file);
4007 ulong_t dlen, slen;
4008 caddr_t src;
4009 int i;
4010
4011 dlen = file->_bsize;
4012 i = off / file->_bsize;
4013 src = file->_dbuf + hdr->ch_blkmap[i];
4014 if (i == hdr->ch_fsize / file->_bsize)
4015 slen = file->_dsize - hdr->ch_blkmap[i];
4016 else
4017 slen = hdr->ch_blkmap[i + 1] - hdr->ch_blkmap[i];
4018 if (z_uncompress(buf, &dlen, src, slen) != Z_OK)
4019 return (-1);
4020 return (dlen);
4021 }
4022
4023 int
4024 kobj_filbuf(struct _buf *f)
4025 {
4026 if (kobj_read_file(f, NULL, f->_bsize, f->_off + f->_size) > 0)
4027 return (kobj_getc(f));
4028 return (-1);
4029 }
4030
4031 void
4032 kobj_free(void *address, size_t size)
4033 {
4034 if (standalone)
4035 return;
4036
4037 kmem_free(address, size);
4038 kobj_stat.nfree_calls++;
4039 kobj_stat.nfree += size;
4040 }
4041
4042 void *
4043 kobj_zalloc(size_t size, int flag)
4044 {
4045 void *v;
4046
4047 if ((v = kobj_alloc(size, flag)) != 0) {
4048 bzero(v, size);
4049 }
4050
4051 return (v);
4052 }
4053
4054 void *
4055 kobj_alloc(size_t size, int flag)
4056 {
4057 /*
4058 * If we are running standalone in the
4059 * linker, we ask boot for memory.
4060 * Either it's temporary memory that we lose
4061 * once boot is mapped out or we allocate it
4062 * permanently using the dynamic data segment.
4063 */
4064 if (standalone) {
4065 #if defined(_OBP)
4066 if (flag & (KM_TMP | KM_SCRATCH))
4067 return (bop_temp_alloc(size, MINALIGN));
4068 #else
4069 if (flag & (KM_TMP | KM_SCRATCH))
4070 return (BOP_ALLOC(ops, 0, size, MINALIGN));
4071 #endif
4072 return (kobj_segbrk(&_edata, size, MINALIGN, 0));
4073 }
4074
4075 kobj_stat.nalloc_calls++;
4076 kobj_stat.nalloc += size;
4077
4078 return (kmem_alloc(size, (flag & KM_NOWAIT) ? KM_NOSLEEP : KM_SLEEP));
4079 }
4080
4081 /*
4082 * Allow the "mod" system to sync up with the work
4083 * already done by kobj during the initial loading
4084 * of the kernel. This also gives us a chance
4085 * to reallocate memory that belongs to boot.
4086 */
4087 void
4088 kobj_sync(void)
4089 {
4090 struct modctl_list *lp, **lpp;
4091
4092 /*
4093 * The module path can be set in /etc/system via 'moddir' commands
4094 */
4095 if (default_path != NULL)
4096 kobj_module_path = default_path;
4097 else
4098 default_path = kobj_module_path;
4099
4100 ksyms_arena = vmem_create("ksyms", NULL, 0, sizeof (uint64_t),
4101 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
4102
4103 ctf_arena = vmem_create("ctf", NULL, 0, sizeof (uint_t),
4104 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
4105
4106 /*
4107 * Move symbol tables from boot memory to ksyms_arena.
4108 */
4109 for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) {
4110 for (lp = *lpp; lp != NULL; lp = lp->modl_next)
4111 kobj_export_module(mod(lp));
4112 }
4113 }
4114
4115 caddr_t
4116 kobj_segbrk(caddr_t *spp, size_t size, size_t align, caddr_t limit)
4117 {
4118 uintptr_t va, pva;
4119 size_t alloc_pgsz = kobj_mmu_pagesize;
4120 size_t alloc_align = BO_NO_ALIGN;
4121 size_t alloc_size;
4122
4123 /*
4124 * If we are using "large" mappings for the kernel,
4125 * request aligned memory from boot using the
4126 * "large" pagesize.
4127 */
4128 if (lg_pagesize) {
4129 alloc_align = lg_pagesize;
4130 alloc_pgsz = lg_pagesize;
4131 }
4132
4133 #if defined(__sparc)
4134 /* account for redzone */
4135 if (limit)
4136 limit -= alloc_pgsz;
4137 #endif /* __sparc */
4138
4139 va = ALIGN((uintptr_t)*spp, align);
4140 pva = P2ROUNDUP((uintptr_t)*spp, alloc_pgsz);
4141 /*
4142 * Need more pages?
4143 */
4144 if (va + size > pva) {
4145 uintptr_t npva;
4146
4147 alloc_size = P2ROUNDUP(size - (pva - va), alloc_pgsz);
4148 /*
4149 * Check for overlapping segments.
4150 */
4151 if (limit && limit <= *spp + alloc_size) {
4152 return ((caddr_t)0);
4153 }
4154
4155 npva = (uintptr_t)BOP_ALLOC(ops, (caddr_t)pva,
4156 alloc_size, alloc_align);
4157
4158 if (npva == 0) {
4159 _kobj_printf(ops, "BOP_ALLOC failed, 0x%lx bytes",
4160 alloc_size);
4161 _kobj_printf(ops, " aligned %lx", alloc_align);
4162 _kobj_printf(ops, " at 0x%lx\n", pva);
4163 return (NULL);
4164 }
4165 }
4166 *spp = (caddr_t)(va + size);
4167
4168 return ((caddr_t)va);
4169 }
4170
4171 /*
4172 * Calculate the number of output hash buckets.
4173 * We use the next prime larger than n / 4,
4174 * so the average hash chain is about 4 entries.
4175 * More buckets would just be a waste of memory.
4176 */
4177 uint_t
4178 kobj_gethashsize(uint_t n)
4179 {
4180 int f;
4181 int hsize = MAX(n / 4, 2);
4182
4183 for (f = 2; f * f <= hsize; f++)
4184 if (hsize % f == 0)
4185 hsize += f = 1;
4186
4187 return (hsize);
4188 }
4189
4190 /*
4191 * Get the file size.
4192 *
4193 * Before root is mounted, files are compressed in the boot_archive ramdisk
4194 * (in the memory). kobj_fstat would return the compressed file size.
4195 * In order to get the uncompressed file size, read the file to the end and
4196 * count its size.
4197 */
4198 int
4199 kobj_get_filesize(struct _buf *file, uint64_t *size)
4200 {
4201 int err = 0;
4202 ssize_t resid;
4203 uint32_t buf;
4204
4205 if (_modrootloaded) {
4206 struct bootstat bst;
4207
4208 if (kobj_fstat(file->_fd, &bst) != 0)
4209 return (EIO);
4210 *size = bst.st_size;
4211
4212 if (file->_iscmp == CH_MAGIC_GZIP) {
4213 /*
4214 * Read the last 4 bytes of the compressed (gzip)
4215 * image to get the size of its uncompressed
4216 * version.
4217 */
4218 if ((err = vn_rdwr(UIO_READ, (struct vnode *)file->_fd,
4219 (char *)(&buf), 4, (offset_t)(*size - 4),
4220 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
4221 != 0) {
4222 _kobj_printf(ops, "kobj_get_filesize: "
4223 "vn_rdwr() failed with error 0x%x\n", err);
4224 return (-1);
4225 }
4226
4227 *size = (uint64_t)buf;
4228 }
4229 } else {
4230
4231 #if defined(_OBP)
4232 struct bootstat bsb;
4233
4234 if (file->_iscmp) {
4235 struct comphdr *hdr = kobj_comphdr(file);
4236
4237 *size = hdr->ch_fsize;
4238 } else if (kobj_boot_fstat(file->_fd, &bsb) != 0)
4239 return (EIO);
4240 else
4241 *size = bsb.st_size;
4242 #else
4243 char *buf;
4244 int count;
4245 uint64_t offset = 0;
4246
4247 buf = kmem_alloc(MAXBSIZE, KM_SLEEP);
4248 do {
4249 count = kobj_read_file(file, buf, MAXBSIZE, offset);
4250 if (count < 0) {
4251 kmem_free(buf, MAXBSIZE);
4252 return (EIO);
4253 }
4254 offset += count;
4255 } while (count == MAXBSIZE);
4256 kmem_free(buf, MAXBSIZE);
4257
4258 *size = offset;
4259 #endif
4260 }
4261
4262 return (0);
4263 }
4264
4265 static char *
4266 basename(char *s)
4267 {
4268 char *p, *q;
4269
4270 q = NULL;
4271 p = s;
4272 do {
4273 if (*p == '/')
4274 q = p;
4275 } while (*p++);
4276 return (q ? q + 1 : s);
4277 }
4278
4279 void
4280 kobj_stat_get(kobj_stat_t *kp)
4281 {
4282 *kp = kobj_stat;
4283 }
4284
4285 int
4286 kobj_getpagesize()
4287 {
4288 return (lg_pagesize);
4289 }
4290
4291 void
4292 kobj_textwin_alloc(struct module *mp)
4293 {
4294 ASSERT(MUTEX_HELD(&mod_lock));
4295
4296 if (mp->textwin != NULL)
4297 return;
4298
4299 /*
4300 * If the text is not contained in the heap, then it is not contained
4301 * by a writable mapping. (Specifically, it's on the nucleus page.)
4302 * We allocate a read/write mapping for this module's text to allow
4303 * the text to be patched without calling hot_patch_kernel_text()
4304 * (which is quite slow).
4305 */
4306 if (!vmem_contains(heaptext_arena, mp->text, mp->text_size)) {
4307 uintptr_t text = (uintptr_t)mp->text;
4308 uintptr_t size = (uintptr_t)mp->text_size;
4309 uintptr_t i;
4310 caddr_t va;
4311 size_t sz = ((text + size + PAGESIZE - 1) & PAGEMASK) -
4312 (text & PAGEMASK);
4313
4314 va = mp->textwin_base = vmem_alloc(heap_arena, sz, VM_SLEEP);
4315
4316 for (i = text & PAGEMASK; i < text + size; i += PAGESIZE) {
4317 hat_devload(kas.a_hat, va, PAGESIZE,
4318 hat_getpfnum(kas.a_hat, (caddr_t)i),
4319 PROT_READ | PROT_WRITE,
4320 HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
4321 va += PAGESIZE;
4322 }
4323
4324 mp->textwin = mp->textwin_base + (text & PAGEOFFSET);
4325 } else {
4326 mp->textwin = mp->text;
4327 }
4328 }
4329
4330 void
4331 kobj_textwin_free(struct module *mp)
4332 {
4333 uintptr_t text = (uintptr_t)mp->text;
4334 uintptr_t tsize = (uintptr_t)mp->text_size;
4335 size_t size = (((text + tsize + PAGESIZE - 1) & PAGEMASK) -
4336 (text & PAGEMASK));
4337
4338 mp->textwin = NULL;
4339
4340 if (mp->textwin_base == NULL)
4341 return;
4342
4343 hat_unload(kas.a_hat, mp->textwin_base, size, HAT_UNLOAD_UNLOCK);
4344 vmem_free(heap_arena, mp->textwin_base, size);
4345 mp->textwin_base = NULL;
4346 }
4347
4348 static char *
4349 find_libmacro(char *name)
4350 {
4351 int lmi;
4352
4353 for (lmi = 0; lmi < NLIBMACROS; lmi++) {
4354 if (strcmp(name, libmacros[lmi].lmi_macroname) == 0)
4355 return (libmacros[lmi].lmi_list);
4356 }
4357 return (NULL);
4358 }
4359
4360 /*
4361 * Check for $MACRO in tail (string to expand) and expand it in path at pathend
4362 * returns path if successful, else NULL
4363 * Support multiple $MACROs expansion and the first valid path will be returned
4364 * Caller's responsibility to provide enough space in path to expand
4365 */
4366 char *
4367 expand_libmacro(char *tail, char *path, char *pathend)
4368 {
4369 char c, *p, *p1, *p2, *path2, *endp;
4370 int diff, lmi, macrolen, valid_macro, more_macro;
4371 struct _buf *file;
4372
4373 /*
4374 * check for $MACROS between nulls or slashes
4375 */
4376 p = strchr(tail, '$');
4377 if (p == NULL)
4378 return (NULL);
4379 for (lmi = 0; lmi < NLIBMACROS; lmi++) {
4380 macrolen = libmacros[lmi].lmi_macrolen;
4381 if (strncmp(p + 1, libmacros[lmi].lmi_macroname, macrolen) == 0)
4382 break;
4383 }
4384
4385 valid_macro = 0;
4386 if (lmi < NLIBMACROS) {
4387 /*
4388 * The following checks are used to restrict expansion of
4389 * macros to those that form a full directory/file name
4390 * and to keep the behavior same as before. If this
4391 * restriction is removed or no longer valid in the future,
4392 * the checks below can be deleted.
4393 */
4394 if ((p == tail) || (*(p - 1) == '/')) {
4395 c = *(p + macrolen + 1);
4396 if (c == '/' || c == '\0')
4397 valid_macro = 1;
4398 }
4399 }
4400
4401 if (!valid_macro) {
4402 p2 = strchr(p, '/');
4403 /*
4404 * if no more macro to expand, then just copy whatever left
4405 * and check whether it exists
4406 */
4407 if (p2 == NULL || strchr(p2, '$') == NULL) {
4408 (void) strcpy(pathend, tail);
4409 if ((file = kobj_open_path(path, 1, 1)) !=
4410 (struct _buf *)-1) {
4411 kobj_close_file(file);
4412 return (path);
4413 } else
4414 return (NULL);
4415 } else {
4416 /*
4417 * copy all chars before '/' and call expand_libmacro()
4418 * again
4419 */
4420 diff = p2 - tail;
4421 bcopy(tail, pathend, diff);
4422 pathend += diff;
4423 *(pathend) = '\0';
4424 return (expand_libmacro(p2, path, pathend));
4425 }
4426 }
4427
4428 more_macro = 0;
4429 if (c != '\0') {
4430 endp = p + macrolen + 1;
4431 if (strchr(endp, '$') != NULL)
4432 more_macro = 1;
4433 } else
4434 endp = NULL;
4435
4436 /*
4437 * copy lmi_list and split it into components.
4438 * then put the part of tail before $MACRO into path
4439 * at pathend
4440 */
4441 diff = p - tail;
4442 if (diff > 0)
4443 bcopy(tail, pathend, diff);
4444 path2 = pathend + diff;
4445 p1 = libmacros[lmi].lmi_list;
4446 while (p1 && (*p1 != '\0')) {
4447 p2 = strchr(p1, ':');
4448 if (p2) {
4449 diff = p2 - p1;
4450 bcopy(p1, path2, diff);
4451 *(path2 + diff) = '\0';
4452 } else {
4453 diff = strlen(p1);
4454 bcopy(p1, path2, diff + 1);
4455 }
4456 /* copy endp only if there isn't any more macro to expand */
4457 if (!more_macro && (endp != NULL))
4458 (void) strcat(path2, endp);
4459 file = kobj_open_path(path, 1, 1);
4460 if (file != (struct _buf *)-1) {
4461 kobj_close_file(file);
4462 /*
4463 * if more macros to expand then call expand_libmacro(),
4464 * else return path which has the whole path
4465 */
4466 if (!more_macro || (expand_libmacro(endp, path,
4467 path2 + diff) != NULL)) {
4468 return (path);
4469 }
4470 }
4471 if (p2)
4472 p1 = ++p2;
4473 else
4474 return (NULL);
4475 }
4476 return (NULL);
4477 }
4478
4479 static void
4480 tnf_add_notifyunload(kobj_notify_f *fp)
4481 {
4482 kobj_notify_list_t *entry;
4483
4484 entry = kobj_alloc(sizeof (kobj_notify_list_t), KM_WAIT);
4485 entry->kn_type = KOBJ_NOTIFY_MODUNLOADING;
4486 entry->kn_func = fp;
4487 (void) kobj_notify_add(entry);
4488 }
4489
4490 /* ARGSUSED */
4491 static void
4492 tnf_unsplice_probes(uint_t what, struct modctl *mod)
4493 {
4494 tnf_probe_control_t **p;
4495 tnf_tag_data_t **q;
4496 struct module *mp = mod->mod_mp;
4497
4498 if (!(mp->flags & KOBJ_TNF_PROBE))
4499 return;
4500
4501 for (p = &__tnf_probe_list_head; *p; )
4502 if (kobj_addrcheck(mp, (char *)*p) == 0)
4503 *p = (*p)->next;
4504 else
4505 p = &(*p)->next;
4506
4507 for (q = &__tnf_tag_list_head; *q; )
4508 if (kobj_addrcheck(mp, (char *)*q) == 0)
4509 *q = (tnf_tag_data_t *)(*q)->tag_version;
4510 else
4511 q = (tnf_tag_data_t **)&(*q)->tag_version;
4512
4513 tnf_changed_probe_list = 1;
4514 }
4515
4516 int
4517 tnf_splice_probes(int boot_load, tnf_probe_control_t *plist,
4518 tnf_tag_data_t *tlist)
4519 {
4520 int result = 0;
4521 static int add_notify = 1;
4522
4523 if (plist) {
4524 tnf_probe_control_t *pl;
4525
4526 for (pl = plist; pl->next; )
4527 pl = pl->next;
4528
4529 if (!boot_load)
4530 mutex_enter(&mod_lock);
4531 tnf_changed_probe_list = 1;
4532 pl->next = __tnf_probe_list_head;
4533 __tnf_probe_list_head = plist;
4534 if (!boot_load)
4535 mutex_exit(&mod_lock);
4536 result = 1;
4537 }
4538
4539 if (tlist) {
4540 tnf_tag_data_t *tl;
4541
4542 for (tl = tlist; tl->tag_version; )
4543 tl = (tnf_tag_data_t *)tl->tag_version;
4544
4545 if (!boot_load)
4546 mutex_enter(&mod_lock);
4547 tl->tag_version = (tnf_tag_version_t *)__tnf_tag_list_head;
4548 __tnf_tag_list_head = tlist;
4549 if (!boot_load)
4550 mutex_exit(&mod_lock);
4551 result = 1;
4552 }
4553 if (!boot_load && result && add_notify) {
4554 tnf_add_notifyunload(tnf_unsplice_probes);
4555 add_notify = 0;
4556 }
4557 return (result);
4558 }
4559
4560 char *kobj_file_buf;
4561 int kobj_file_bufsize;
4562
4563 /*
4564 * This code is for the purpose of manually recording which files
4565 * needs to go into the boot archive on any given system.
4566 *
4567 * To enable the code, set kobj_file_bufsize in /etc/system
4568 * and reboot the system, then use mdb to look at kobj_file_buf.
4569 */
4570 static void
4571 kobj_record_file(char *filename)
4572 {
4573 static char *buf;
4574 static int size = 0;
4575 int n;
4576
4577 if (kobj_file_bufsize == 0) /* don't bother */
4578 return;
4579
4580 if (kobj_file_buf == NULL) { /* allocate buffer */
4581 size = kobj_file_bufsize;
4582 buf = kobj_file_buf = kobj_alloc(size, KM_WAIT|KM_TMP);
4583 }
4584
4585 n = snprintf(buf, size, "%s\n", filename);
4586 if (n > size)
4587 n = size;
4588 size -= n;
4589 buf += n;
4590 }
4591
4592 static int
4593 kobj_boot_fstat(int fd, struct bootstat *stp)
4594 {
4595 #if defined(_OBP)
4596 if (!standalone && _ioquiesced)
4597 return (-1);
4598 return (BOP_FSTAT(ops, fd, stp));
4599 #else
4600 return (BRD_FSTAT(bfs_ops, fd, stp));
4601 #endif
4602 }
4603
4604 static int
4605 kobj_boot_open(char *filename, int flags)
4606 {
4607 #if defined(_OBP)
4608
4609 /*
4610 * If io via bootops is quiesced, it means boot is no longer
4611 * available to us. We make it look as if we can't open the
4612 * named file - which is reasonably accurate.
4613 */
4614 if (!standalone && _ioquiesced)
4615 return (-1);
4616
4617 kobj_record_file(filename);
4618 return (BOP_OPEN(filename, flags));
4619 #else /* x86 */
4620 kobj_record_file(filename);
4621 return (BRD_OPEN(bfs_ops, filename, flags));
4622 #endif
4623 }
4624
4625 static int
4626 kobj_boot_close(int fd)
4627 {
4628 #if defined(_OBP)
4629 if (!standalone && _ioquiesced)
4630 return (-1);
4631
4632 return (BOP_CLOSE(fd));
4633 #else /* x86 */
4634 return (BRD_CLOSE(bfs_ops, fd));
4635 #endif
4636 }
4637
4638 /*ARGSUSED*/
4639 static int
4640 kobj_boot_seek(int fd, off_t hi, off_t lo)
4641 {
4642 #if defined(_OBP)
4643 return (BOP_SEEK(fd, lo) == -1 ? -1 : 0);
4644 #else
4645 return (BRD_SEEK(bfs_ops, fd, lo, SEEK_SET));
4646 #endif
4647 }
4648
4649 static int
4650 kobj_boot_read(int fd, caddr_t buf, size_t size)
4651 {
4652 #if defined(_OBP)
4653 return (BOP_READ(fd, buf, size));
4654 #else
4655 return (BRD_READ(bfs_ops, fd, buf, size));
4656 #endif
4657 }
4658
4659 static int
4660 kobj_boot_compinfo(int fd, struct compinfo *cb)
4661 {
4662 return (boot_compinfo(fd, cb));
4663 }
4664
4665 /*
4666 * Check if the file is compressed (for now we handle only gzip).
4667 * It returns CH_MAGIC_GZIP if the file is compressed and 0 otherwise.
4668 */
4669 static int
4670 kobj_is_compressed(intptr_t fd)
4671 {
4672 struct vnode *vp = (struct vnode *)fd;
4673 ssize_t resid;
4674 uint16_t magic_buf;
4675 int err = 0;
4676
4677 if ((err = vn_rdwr(UIO_READ, vp, (caddr_t)((intptr_t)&magic_buf),
4678 sizeof (magic_buf), (offset_t)(0),
4679 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) != 0) {
4680
4681 _kobj_printf(ops, "kobj_is_compressed: vn_rdwr() failed, "
4682 "error code 0x%x\n", err);
4683 return (0);
4684 }
4685
4686 if (magic_buf == CH_MAGIC_GZIP)
4687 return (CH_MAGIC_GZIP);
4688
4689 return (0);
4690 }