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