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