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