10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55648Ssetje * Common Development and Distribution License (the "License").
65648Ssetje * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11112SJerry.Gilliam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/sysmacros.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/exechdr.h>
290Sstevel@tonic-gate #include <sys/elf.h>
300Sstevel@tonic-gate #include <sys/elf_notes.h>
310Sstevel@tonic-gate #include <sys/bootconf.h>
320Sstevel@tonic-gate #include <sys/reboot.h>
330Sstevel@tonic-gate #include <sys/fcntl.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/modctl.h>
360Sstevel@tonic-gate #include <sys/link.h>
370Sstevel@tonic-gate #include <sys/auxv.h>
380Sstevel@tonic-gate #include <sys/salib.h>
390Sstevel@tonic-gate #include <sys/bootvfs.h>
400Sstevel@tonic-gate #include <sys/platnames.h>
410Sstevel@tonic-gate
42231Sjg #include "util.h"
43231Sjg
440Sstevel@tonic-gate #ifdef BOOTAMD64
450Sstevel@tonic-gate #include <amd64/amd64_page.h>
460Sstevel@tonic-gate #endif /* BOOTAMD64 */
470Sstevel@tonic-gate
480Sstevel@tonic-gate union {
490Sstevel@tonic-gate struct exec X;
500Sstevel@tonic-gate Elf32_Ehdr Elfhdr;
510Sstevel@tonic-gate Elf64_Ehdr Elfhdr64;
520Sstevel@tonic-gate } ex;
530Sstevel@tonic-gate
540Sstevel@tonic-gate #define x ex.X
550Sstevel@tonic-gate #define elfhdr ex.Elfhdr
560Sstevel@tonic-gate #define elfhdr64 ex.Elfhdr64
570Sstevel@tonic-gate
580Sstevel@tonic-gate typedef int (*func_t)();
590Sstevel@tonic-gate
600Sstevel@tonic-gate #define FAIL ((func_t)-1)
610Sstevel@tonic-gate #define ALIGN(x, a) \
620Sstevel@tonic-gate ((a) == 0 ? (uintptr_t)(x) : (((uintptr_t)(x) + (a) - 1) & ~((a) - 1)))
630Sstevel@tonic-gate
640Sstevel@tonic-gate #define __BOOT_NAUXV_IMPL 22
650Sstevel@tonic-gate
660Sstevel@tonic-gate int use_align = 0;
670Sstevel@tonic-gate int npagesize = 0;
680Sstevel@tonic-gate uint_t icache_flush = 0;
690Sstevel@tonic-gate char *cpulist = NULL;
700Sstevel@tonic-gate char *mmulist = NULL;
710Sstevel@tonic-gate char *module_path; /* path for kernel modules */
720Sstevel@tonic-gate
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * This file gets compiled in LP64 (for sun4u) and ILP32 models.
750Sstevel@tonic-gate * For LP64 compilation, the "client" file we load and run may be LP64 or ILP32,
760Sstevel@tonic-gate * and during bringup, the LP64 clients may have ELF32 headers.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate #ifdef _ELF64_SUPPORT
790Sstevel@tonic-gate #ifndef BOOTAMD64
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * Bootstrap vector for ELF32 LP64 client - neither supported nor needed for
820Sstevel@tonic-gate * AMD64
830Sstevel@tonic-gate */
840Sstevel@tonic-gate Elf32_Boot *elfbootvecELF32_64;
850Sstevel@tonic-gate #endif /* !BOOTAMD64 */
860Sstevel@tonic-gate
870Sstevel@tonic-gate Elf64_Boot *elfbootvecELF64; /* ELF bootstrap vector for Elf64 LP64 */
880Sstevel@tonic-gate
890Sstevel@tonic-gate #define OK ((func_t)0)
900Sstevel@tonic-gate
910Sstevel@tonic-gate #define FAIL_READELF64 ((uint64_t)0)
920Sstevel@tonic-gate #define FAIL_ILOAD64 ((Elf64_Addr)-1)
930Sstevel@tonic-gate #endif /* _ELF64_SUPPORT */
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * And by an ILP32 client. The non-sun4u/LP64 booters use these.
970Sstevel@tonic-gate * Also, the sun4u booter must create this for ILP32 clients.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate Elf32_Boot *elfbootvec; /* ELF bootstrap vector normal ILP32 */
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * Read in a Unix executable file and return its entry point.
1030Sstevel@tonic-gate * Handle the various a.out formats correctly.
1040Sstevel@tonic-gate * "fd" is the standalone file descriptor to read from.
1050Sstevel@tonic-gate * Print informative little messages if "print" is on.
1060Sstevel@tonic-gate * Returns -1 for errors.
1070Sstevel@tonic-gate */
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate #ifdef DEBUG
1100Sstevel@tonic-gate static int debug = 1;
1110Sstevel@tonic-gate #else /* DEBUG */
1120Sstevel@tonic-gate static int debug = 0;
1130Sstevel@tonic-gate #endif /* DEBUG */
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate #define dprintf if (debug) printf
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate #ifdef _ELF64_SUPPORT
1180Sstevel@tonic-gate typedef struct {
1190Sstevel@tonic-gate uint_t a_type;
1200Sstevel@tonic-gate #ifdef BOOTAMD64
1210Sstevel@tonic-gate uint_t a_pad; /* needed to 8-byte align uint64_ts below for AMD64 */
1220Sstevel@tonic-gate #endif /* BOOTAMD64 */
1230Sstevel@tonic-gate union {
1240Sstevel@tonic-gate uint64_t a_val;
1250Sstevel@tonic-gate uint64_t a_ptr;
1260Sstevel@tonic-gate #ifndef BOOTAMD64
1270Sstevel@tonic-gate void (*a_fcn)(); /* XXX - UNUSED? */
1280Sstevel@tonic-gate #endif /* !BOOTAMD64 */
1290Sstevel@tonic-gate } a_un;
1300Sstevel@tonic-gate } auxv64_t;
1310Sstevel@tonic-gate
132641Skalai #if defined(__sparcv9)
1330Sstevel@tonic-gate extern int client_isLP64;
134641Skalai #endif /* __sparcv9 */
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate static uint64_t read_elf64(int, int, Elf64_Ehdr *);
1370Sstevel@tonic-gate static Elf64_Addr iload64(char *, Elf64_Phdr *, Elf64_Phdr *, auxv64_t **);
1380Sstevel@tonic-gate #endif /* _ELF64_SUPPORT */
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate #if defined(i386) && !defined(_SYSCALL32)
1410Sstevel@tonic-gate typedef auxv_t auxv32_t;
1420Sstevel@tonic-gate #endif
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate static func_t read_elf32(int, int, Elf32_Ehdr *);
1450Sstevel@tonic-gate static func_t iload32(char *, Elf32_Phdr *, Elf32_Phdr *, auxv32_t **);
1460Sstevel@tonic-gate static caddr_t segbrk(caddr_t *, size_t, size_t);
1470Sstevel@tonic-gate static int openpath(char *, char *, int);
1480Sstevel@tonic-gate static char *getmodpath(char *);
1490Sstevel@tonic-gate extern void setup_aux(void);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate extern void *kmem_alloc(size_t, int);
1520Sstevel@tonic-gate extern void kmem_free(void *, size_t);
1530Sstevel@tonic-gate extern int cons_gets(char *, int);
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate #ifdef BOOTAMD64
1560Sstevel@tonic-gate extern const char *amd64_getmmulist(void);
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate extern int amd64_elf64;
1590Sstevel@tonic-gate extern int is_amd64;
1600Sstevel@tonic-gate #endif /* BOOTAMD64 */
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate #ifdef lint
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate * This function is currently inlined
1650Sstevel@tonic-gate */
1660Sstevel@tonic-gate /*ARGSUSED*/
1670Sstevel@tonic-gate void
sync_instruction_memory(caddr_t v,size_t len)1680Sstevel@tonic-gate sync_instruction_memory(caddr_t v, size_t len)
1690Sstevel@tonic-gate {}
1700Sstevel@tonic-gate #else /* lint */
1710Sstevel@tonic-gate extern void sync_instruction_memory(caddr_t v, size_t len);
1720Sstevel@tonic-gate #endif /* lint */
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate extern int verbosemode;
1750Sstevel@tonic-gate extern int boothowto;
1760Sstevel@tonic-gate extern int pagesize;
1770Sstevel@tonic-gate extern char filename[];
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate * repeat reads (forever) until size of request is satisfied
1810Sstevel@tonic-gate * (Thus, you don't want to use this cases where short reads are ok)
1820Sstevel@tonic-gate */
1835648Ssetje ssize_t
xread(int fd,char * p,size_t nbytes)1840Sstevel@tonic-gate xread(int fd, char *p, size_t nbytes)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate size_t bytesread = 0;
1870Sstevel@tonic-gate int errorcount = 0;
1880Sstevel@tonic-gate ssize_t i;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate while (bytesread < nbytes) {
1910Sstevel@tonic-gate i = read(fd, p, nbytes - bytesread);
1920Sstevel@tonic-gate if (i < 0) {
1930Sstevel@tonic-gate ++errorcount;
1940Sstevel@tonic-gate if (verbosemode)
1950Sstevel@tonic-gate printf("read error (0x%x times)\n", errorcount);
1960Sstevel@tonic-gate continue;
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate bytesread += i;
1990Sstevel@tonic-gate p += i;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate return (bytesread);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate func_t
readfile(int fd,int print)2050Sstevel@tonic-gate readfile(int fd, int print)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate #ifdef _ELF64_SUPPORT
2080Sstevel@tonic-gate #ifdef BOOTAMD64
209157Sjg extern int bsetprop(struct bootops *, char *, void *, int);
2100Sstevel@tonic-gate extern struct bootops *bop;
2110Sstevel@tonic-gate extern uint64_t elf64_go2;
2120Sstevel@tonic-gate #else /* !BOOTAMD64 */
2130Sstevel@tonic-gate uint64_t elf64_go2;
2140Sstevel@tonic-gate #endif /* BOOTAMD64 */
2150Sstevel@tonic-gate #endif /* _ELF64_SUPPORT */
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate ssize_t i;
2180Sstevel@tonic-gate int shared = 0;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if (verbosemode) {
2210Sstevel@tonic-gate dprintf("fd = %x\n", fd);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate i = xread(fd, (char *)&elfhdr, sizeof (Elf64_Ehdr));
2250Sstevel@tonic-gate if (x.a_magic == ZMAGIC || x.a_magic == NMAGIC)
2260Sstevel@tonic-gate shared = 1;
2270Sstevel@tonic-gate if (i != sizeof (Elf64_Ehdr)) {
2280Sstevel@tonic-gate printf("Error reading ELF header.\n");
2290Sstevel@tonic-gate return (FAIL);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate if (!shared && x.a_magic != OMAGIC) {
2320Sstevel@tonic-gate if (*(int *)&elfhdr.e_ident == *(int *)(ELFMAG)) {
2330Sstevel@tonic-gate if (verbosemode) {
2340Sstevel@tonic-gate int is64 = (elfhdr.e_ident[EI_CLASS] ==
2350Sstevel@tonic-gate ELFCLASS64);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate dprintf("calling readelf, elfheader is:\n");
2380Sstevel@tonic-gate dprintf("e_ident\t0x%x, 0x%x, 0x%x, 0x%x\n",
2390Sstevel@tonic-gate *(int *)&elfhdr.e_ident[0],
2400Sstevel@tonic-gate *(int *)&elfhdr.e_ident[4],
2410Sstevel@tonic-gate *(int *)&elfhdr.e_ident[8],
2420Sstevel@tonic-gate *(int *)&elfhdr.e_ident[12]);
2430Sstevel@tonic-gate dprintf("e_machine\t0x%x\n", elfhdr.e_machine);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate dprintf("e_entry\t\t0x%llx\n", (is64 ?
2460Sstevel@tonic-gate elfhdr64.e_entry :
2470Sstevel@tonic-gate (u_longlong_t)elfhdr.e_entry));
2480Sstevel@tonic-gate dprintf("e_shoff\t\t0x%llx\n", (is64 ?
2490Sstevel@tonic-gate elfhdr64.e_shoff :
2500Sstevel@tonic-gate (u_longlong_t)elfhdr.e_shoff));
2510Sstevel@tonic-gate dprintf("e_shnentsize\t%d\n", (is64 ?
2520Sstevel@tonic-gate elfhdr64.e_shentsize : elfhdr.e_shentsize));
2530Sstevel@tonic-gate dprintf("e_shnum\t\t%d\n", (is64 ?
2540Sstevel@tonic-gate elfhdr64.e_shnum : elfhdr.e_shnum));
2550Sstevel@tonic-gate dprintf("e_shstrndx\t%d\n", (is64 ?
2560Sstevel@tonic-gate elfhdr64.e_shstrndx : elfhdr.e_shstrndx));
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate #ifdef _ELF64_SUPPORT
2610Sstevel@tonic-gate dprintf("ELF file CLASS 0x%x 32 is %x 64 is %x\n",
2620Sstevel@tonic-gate elfhdr.e_ident[EI_CLASS], ELFCLASS32, ELFCLASS64);
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate if (elfhdr.e_ident[EI_CLASS] == ELFCLASS64) {
2650Sstevel@tonic-gate #ifdef BOOTAMD64
2660Sstevel@tonic-gate if (elfhdr.e_machine != EM_AMD64) {
2670Sstevel@tonic-gate printf("FATAL: 64-bit ELF executable "
2680Sstevel@tonic-gate "not for AMD64\n (e_machine "
2690Sstevel@tonic-gate "= %d).\n", elfhdr.e_machine);
2705648Ssetje return (FAIL);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate * OK, we know the executable is for an AMD64
2750Sstevel@tonic-gate * CPU. Make sure we ARE an AMD64 CPU before
2760Sstevel@tonic-gate * proceeding.
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate if (is_amd64 == 0) {
2790Sstevel@tonic-gate printf("FATAL: AMD64 executables not "
2800Sstevel@tonic-gate " supported on this CPU.\n");
2810Sstevel@tonic-gate return (FAIL);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate amd64_elf64 = (elfhdr.e_ident[EI_CLASS] ==
2850Sstevel@tonic-gate ELFCLASS64);
2860Sstevel@tonic-gate #endif /* BOOTAMD64 */
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate elf64_go2 = read_elf64(fd, print,
2890Sstevel@tonic-gate (Elf64_Ehdr *)&elfhdr);
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate #ifdef BOOTAMD64
2920Sstevel@tonic-gate if (elf64_go2 != FAIL_READELF64)
2930Sstevel@tonic-gate (void) bsetprop(bop, "mmu-modlist",
294157Sjg "mmu64", 0);
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate return ((elf64_go2 == FAIL_READELF64) ? FAIL :
2970Sstevel@tonic-gate OK);
2980Sstevel@tonic-gate #else /* !BOOTAMD64 */
2990Sstevel@tonic-gate return ((elf64_go2 == FAIL_READELF64) ? FAIL :
3000Sstevel@tonic-gate (func_t)elf64_go2);
3010Sstevel@tonic-gate #endif /* BOOTAMD64 */
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate } else
3040Sstevel@tonic-gate #endif /* _ELF64_SUPPORT */
3050Sstevel@tonic-gate return (read_elf32(fd, print, &elfhdr));
3060Sstevel@tonic-gate } else {
3070Sstevel@tonic-gate printf("File not executable.\n");
3080Sstevel@tonic-gate return (FAIL);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate return (FAIL);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /*
315785Seota * Macros to add attribute/values to the ELF bootstrap vector
316785Seota * and the aux vector. Use the type-cast to convert integers
317785Seota * to pointers first to suppress the gcc warning.
3180Sstevel@tonic-gate */
3190Sstevel@tonic-gate #define AUX(p, a, v) { (p)->a_type = (a); \
320785Seota ((p)++)->a_un.a_val = (int32_t)(uintptr_t)(v); }
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate #define EBV(p, a, v) { (p)->eb_tag = (a); \
323785Seota ((p)++)->eb_un.eb_val = (Elf32_Word)(uintptr_t)(v); }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate static func_t
read_elf32(int fd,int print,Elf32_Ehdr * elfhdrp)3260Sstevel@tonic-gate read_elf32(int fd, int print, Elf32_Ehdr *elfhdrp)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate Elf32_Phdr *phdr; /* program header */
3290Sstevel@tonic-gate Elf32_Nhdr *nhdr; /* note header */
3300Sstevel@tonic-gate int nphdrs, phdrsize;
3310Sstevel@tonic-gate caddr_t allphdrs;
3320Sstevel@tonic-gate caddr_t namep, descp;
3330Sstevel@tonic-gate Elf32_Addr loadaddr, base;
3340Sstevel@tonic-gate size_t offset = 0;
3350Sstevel@tonic-gate size_t size;
3360Sstevel@tonic-gate uintptr_t off;
3370Sstevel@tonic-gate int i;
3380Sstevel@tonic-gate int bss_seen = 0;
3390Sstevel@tonic-gate int interp = 0; /* interpreter required */
3400Sstevel@tonic-gate static char dlname[MAXPATHLEN]; /* name of interpeter */
3410Sstevel@tonic-gate uint_t dynamic; /* dynamic tags array */
3420Sstevel@tonic-gate Elf32_Phdr *thdr; /* "text" program header */
3430Sstevel@tonic-gate Elf32_Phdr *dhdr; /* "data" program header */
3440Sstevel@tonic-gate func_t entrypt; /* entry point of standalone */
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /* Initialize pointers so we won't free bogus ones on elferror */
3470Sstevel@tonic-gate allphdrs = NULL;
3480Sstevel@tonic-gate nhdr = NULL;
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate #ifdef _ELF64_SUPPORT
3510Sstevel@tonic-gate if (verbosemode)
3520Sstevel@tonic-gate printf("Elf32 client\n");
3530Sstevel@tonic-gate #endif /* _ELF64_SUPPORT */
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate if (elfhdrp->e_phnum == 0 || elfhdrp->e_phoff == 0)
3560Sstevel@tonic-gate goto elferror;
3570Sstevel@tonic-gate
358785Seota /* use uintptr_t to suppress the gcc warning */
359785Seota entrypt = (func_t)(uintptr_t)elfhdrp->e_entry;
3600Sstevel@tonic-gate if (verbosemode)
3610Sstevel@tonic-gate dprintf("Entry point: %p\n", (void *)entrypt);
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate * Allocate and read in all the program headers.
3650Sstevel@tonic-gate */
3660Sstevel@tonic-gate nphdrs = elfhdrp->e_phnum;
3670Sstevel@tonic-gate phdrsize = nphdrs * elfhdrp->e_phentsize;
3680Sstevel@tonic-gate allphdrs = (caddr_t)kmem_alloc(phdrsize, 0);
3690Sstevel@tonic-gate if (allphdrs == NULL)
3700Sstevel@tonic-gate goto elferror;
3710Sstevel@tonic-gate if (verbosemode)
3720Sstevel@tonic-gate dprintf("lseek: args = %x %x %x\n", fd, elfhdrp->e_phoff, 0);
3730Sstevel@tonic-gate if (lseek(fd, elfhdrp->e_phoff, 0) == -1)
3740Sstevel@tonic-gate goto elferror;
3750Sstevel@tonic-gate if (xread(fd, allphdrs, phdrsize) != phdrsize)
3760Sstevel@tonic-gate goto elferror;
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate * First look for PT_NOTE headers that tell us what pagesize to
3800Sstevel@tonic-gate * use in allocating program memory.
3810Sstevel@tonic-gate */
3820Sstevel@tonic-gate npagesize = 0;
3830Sstevel@tonic-gate for (i = 0; i < nphdrs; i++) {
3840Sstevel@tonic-gate void *note_buf;
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate phdr = (Elf32_Phdr *)(allphdrs + elfhdrp->e_phentsize * i);
3870Sstevel@tonic-gate if (phdr->p_type != PT_NOTE)
3880Sstevel@tonic-gate continue;
3890Sstevel@tonic-gate if (verbosemode) {
3900Sstevel@tonic-gate dprintf("allocating 0x%x bytes for note hdr\n",
3915648Ssetje phdr->p_filesz);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate if ((note_buf = kmem_alloc(phdr->p_filesz, 0)) == NULL)
3940Sstevel@tonic-gate goto elferror;
3950Sstevel@tonic-gate if (verbosemode)
3960Sstevel@tonic-gate dprintf("seeking to 0x%x\n", phdr->p_offset);
3970Sstevel@tonic-gate if (lseek(fd, phdr->p_offset, 0) == -1)
3980Sstevel@tonic-gate goto elferror;
3990Sstevel@tonic-gate if (verbosemode) {
4000Sstevel@tonic-gate dprintf("reading 0x%x bytes into %p\n",
4015648Ssetje phdr->p_filesz, (void *)nhdr);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate nhdr = (Elf32_Nhdr *)note_buf;
4040Sstevel@tonic-gate if (xread(fd, (caddr_t)nhdr, phdr->p_filesz) != phdr->p_filesz)
4050Sstevel@tonic-gate goto elferror;
4060Sstevel@tonic-gate if (verbosemode) {
4070Sstevel@tonic-gate dprintf("p_note namesz %x descsz %x type %x\n",
4085648Ssetje nhdr->n_namesz, nhdr->n_descsz, nhdr->n_type);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate * Iterate through all ELF PT_NOTE elements looking for
4130Sstevel@tonic-gate * ELF_NOTE_SOLARIS which, if present, will specify the
4140Sstevel@tonic-gate * executable's preferred pagesize.
4150Sstevel@tonic-gate */
4160Sstevel@tonic-gate do {
4170Sstevel@tonic-gate namep = (caddr_t)(nhdr + 1);
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate if (nhdr->n_namesz == strlen(ELF_NOTE_SOLARIS) + 1 &&
4200Sstevel@tonic-gate strcmp(namep, ELF_NOTE_SOLARIS) == 0 &&
4210Sstevel@tonic-gate nhdr->n_type == ELF_NOTE_PAGESIZE_HINT) {
4220Sstevel@tonic-gate descp = namep + roundup(nhdr->n_namesz, 4);
4230Sstevel@tonic-gate npagesize = *(int *)descp;
4240Sstevel@tonic-gate if (verbosemode)
4250Sstevel@tonic-gate dprintf("pagesize is %x\n", npagesize);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate offset += sizeof (Elf32_Nhdr) + roundup(nhdr->n_namesz,
4290Sstevel@tonic-gate 4) + roundup(nhdr->n_descsz, 4);
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate nhdr = (Elf32_Nhdr *)((char *)note_buf + offset);
4320Sstevel@tonic-gate } while (offset < phdr->p_filesz);
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate kmem_free(note_buf, phdr->p_filesz);
4350Sstevel@tonic-gate nhdr = NULL;
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate /*
4390Sstevel@tonic-gate * Next look for PT_LOAD headers to read in.
4400Sstevel@tonic-gate */
4410Sstevel@tonic-gate if (print)
4420Sstevel@tonic-gate printf("Size: ");
4430Sstevel@tonic-gate for (i = 0; i < nphdrs; i++) {
4440Sstevel@tonic-gate phdr = (Elf32_Phdr *)(allphdrs + elfhdrp->e_phentsize * i);
4450Sstevel@tonic-gate if (verbosemode) {
4460Sstevel@tonic-gate dprintf("Doing header 0x%x\n", i);
4470Sstevel@tonic-gate dprintf("phdr\n");
4480Sstevel@tonic-gate dprintf("\tp_offset = %x, p_vaddr = %x\n",
4495648Ssetje phdr->p_offset, phdr->p_vaddr);
4500Sstevel@tonic-gate dprintf("\tp_memsz = %x, p_filesz = %x\n",
4515648Ssetje phdr->p_memsz, phdr->p_filesz);
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate if (phdr->p_type == PT_LOAD) {
4540Sstevel@tonic-gate if (verbosemode)
4550Sstevel@tonic-gate dprintf("seeking to 0x%x\n", phdr->p_offset);
4560Sstevel@tonic-gate if (lseek(fd, phdr->p_offset, 0) == -1)
4570Sstevel@tonic-gate goto elferror;
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate if (phdr->p_flags == (PF_R | PF_W) &&
4605648Ssetje phdr->p_vaddr == 0) {
4610Sstevel@tonic-gate /*
4620Sstevel@tonic-gate * It's a PT_LOAD segment that is RW but
4630Sstevel@tonic-gate * not executable and has a vaddr
4640Sstevel@tonic-gate * of zero. This is relocation info that
4650Sstevel@tonic-gate * doesn't need to stick around after
4660Sstevel@tonic-gate * krtld is done with it. We allocate boot
4670Sstevel@tonic-gate * memory for this segment, since we don't want
4680Sstevel@tonic-gate * it mapped in permanently as part of
4690Sstevel@tonic-gate * the kernel image.
4700Sstevel@tonic-gate */
4710Sstevel@tonic-gate if ((loadaddr = (uintptr_t)
4720Sstevel@tonic-gate kmem_alloc(phdr->p_memsz, 0)) == NULL)
4730Sstevel@tonic-gate goto elferror;
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate * Save this to pass on
4760Sstevel@tonic-gate * to the interpreter.
4770Sstevel@tonic-gate */
4780Sstevel@tonic-gate phdr->p_vaddr = (Elf32_Addr)loadaddr;
4790Sstevel@tonic-gate } else {
4800Sstevel@tonic-gate if (print)
4810Sstevel@tonic-gate printf("0x%x+", phdr->p_filesz);
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate * If we found a new pagesize above, use it
4840Sstevel@tonic-gate * to adjust the memory allocation.
4850Sstevel@tonic-gate */
4860Sstevel@tonic-gate loadaddr = phdr->p_vaddr;
4870Sstevel@tonic-gate if (use_align && npagesize != 0) {
4880Sstevel@tonic-gate off = loadaddr & (npagesize - 1);
4890Sstevel@tonic-gate size = roundup(phdr->p_memsz + off,
4905648Ssetje npagesize);
4910Sstevel@tonic-gate base = loadaddr - off;
4920Sstevel@tonic-gate } else {
4930Sstevel@tonic-gate npagesize = 0;
4940Sstevel@tonic-gate size = phdr->p_memsz;
4950Sstevel@tonic-gate base = loadaddr;
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate * Check if it's text or data.
4990Sstevel@tonic-gate */
5000Sstevel@tonic-gate if (phdr->p_flags & PF_W)
5010Sstevel@tonic-gate dhdr = phdr;
5020Sstevel@tonic-gate else
5030Sstevel@tonic-gate thdr = phdr;
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate * If memory size is zero just ignore this
5070Sstevel@tonic-gate * header.
5080Sstevel@tonic-gate */
5090Sstevel@tonic-gate if (size == 0)
5100Sstevel@tonic-gate continue;
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate if (verbosemode)
5130Sstevel@tonic-gate dprintf("allocating memory: %x %lx "
5140Sstevel@tonic-gate "%x\n", base, size, npagesize);
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate * We're all set up to read.
5170Sstevel@tonic-gate * Now let's allocate some memory.
5180Sstevel@tonic-gate */
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate #ifdef i386
5210Sstevel@tonic-gate /*
5220Sstevel@tonic-gate * If vaddr == paddr and npagesize is 0, that
5230Sstevel@tonic-gate * means the executable needs to be identity
5240Sstevel@tonic-gate * mapped in memory (va == pa, mapped 1:1)
5250Sstevel@tonic-gate *
5260Sstevel@tonic-gate * Otherwise load as usual.
5270Sstevel@tonic-gate */
5280Sstevel@tonic-gate if ((phdr->p_vaddr == phdr->p_paddr) &&
5290Sstevel@tonic-gate (npagesize == 0)) {
5300Sstevel@tonic-gate extern caddr_t idmap_mem(uint32_t,
5310Sstevel@tonic-gate size_t, int);
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate uint_t n;
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate n = (uint_t)base & (pagesize - 1);
5360Sstevel@tonic-gate if (n) {
5370Sstevel@tonic-gate base -= n;
5380Sstevel@tonic-gate size += n;
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate if (!idmap_mem((uint32_t)base,
5420Sstevel@tonic-gate (size_t)size, pagesize))
5430Sstevel@tonic-gate goto elferror;
5440Sstevel@tonic-gate } else
5450Sstevel@tonic-gate #endif /* i386 */
546785Seota /* use uintptr_t to suppress the gcc warning */
547785Seota if (get_progmemory((caddr_t)(uintptr_t)base,
548785Seota size, npagesize))
5490Sstevel@tonic-gate goto elferror;
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate if (verbosemode) {
5530Sstevel@tonic-gate dprintf("reading 0x%x bytes into 0x%x\n",
5545648Ssetje phdr->p_filesz, loadaddr);
5550Sstevel@tonic-gate }
556785Seota /* use uintptr_t to suppress the gcc warning */
557785Seota if (xread(fd, (caddr_t)(uintptr_t)loadaddr,
558785Seota phdr->p_filesz) != phdr->p_filesz)
5590Sstevel@tonic-gate goto elferror;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate /* zero out BSS */
5620Sstevel@tonic-gate if (phdr->p_memsz > phdr->p_filesz) {
5630Sstevel@tonic-gate loadaddr += phdr->p_filesz;
5640Sstevel@tonic-gate if (verbosemode) {
5650Sstevel@tonic-gate dprintf("bss from 0x%x size 0x%x\n",
5660Sstevel@tonic-gate loadaddr,
5670Sstevel@tonic-gate phdr->p_memsz - phdr->p_filesz);
5680Sstevel@tonic-gate }
569785Seota /* use uintptr_t to suppress the gcc warning */
570785Seota bzero((void *)(uintptr_t)loadaddr,
5710Sstevel@tonic-gate phdr->p_memsz - phdr->p_filesz);
5720Sstevel@tonic-gate bss_seen++;
5730Sstevel@tonic-gate if (print)
5740Sstevel@tonic-gate printf("0x%x Bytes\n",
5750Sstevel@tonic-gate phdr->p_memsz - phdr->p_filesz);
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate /* force instructions to be visible to icache */
579785Seota if (phdr->p_flags & PF_X) {
580785Seota sync_instruction_memory(
581785Seota (caddr_t)(uintptr_t)phdr->p_vaddr,
5820Sstevel@tonic-gate phdr->p_memsz);
583785Seota }
5840Sstevel@tonic-gate } else if (phdr->p_type == PT_INTERP) {
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate * Dynamically-linked executable.
5870Sstevel@tonic-gate */
5880Sstevel@tonic-gate interp = 1;
5890Sstevel@tonic-gate if (lseek(fd, phdr->p_offset, 0) == -1) {
5900Sstevel@tonic-gate goto elferror;
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate * Get the name of the interpreter.
5940Sstevel@tonic-gate */
5950Sstevel@tonic-gate if (xread(fd, dlname, phdr->p_filesz) !=
5960Sstevel@tonic-gate phdr->p_filesz ||
5970Sstevel@tonic-gate dlname[phdr->p_filesz - 1] != '\0')
5980Sstevel@tonic-gate goto elferror;
5990Sstevel@tonic-gate } else if (phdr->p_type == PT_DYNAMIC) {
6000Sstevel@tonic-gate dynamic = phdr->p_vaddr;
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate if (!bss_seen && print)
6050Sstevel@tonic-gate printf("0 Bytes\n");
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate * Load the interpreter
6090Sstevel@tonic-gate * if there is one.
6100Sstevel@tonic-gate */
6110Sstevel@tonic-gate if (interp) {
6120Sstevel@tonic-gate Elf32_Boot bootv[EB_MAX]; /* Bootstrap vector */
6130Sstevel@tonic-gate auxv32_t auxv[__BOOT_NAUXV_IMPL]; /* Aux vector */
6140Sstevel@tonic-gate Elf32_Boot *bv = bootv;
6150Sstevel@tonic-gate auxv32_t *av = auxv;
6160Sstevel@tonic-gate size_t vsize;
6170Sstevel@tonic-gate
6180Sstevel@tonic-gate /*
6190Sstevel@tonic-gate * Load it.
6200Sstevel@tonic-gate */
6210Sstevel@tonic-gate if ((entrypt = iload32(dlname, thdr, dhdr, &av)) == FAIL)
6220Sstevel@tonic-gate goto elferror;
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate * Build bootstrap and aux vectors.
6250Sstevel@tonic-gate */
6260Sstevel@tonic-gate setup_aux();
6270Sstevel@tonic-gate EBV(bv, EB_AUXV, 0); /* fill in later */
6280Sstevel@tonic-gate EBV(bv, EB_PAGESIZE, pagesize);
6290Sstevel@tonic-gate EBV(bv, EB_DYNAMIC, dynamic);
6300Sstevel@tonic-gate EBV(bv, EB_NULL, 0);
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate AUX(av, AT_BASE, entrypt);
6330Sstevel@tonic-gate AUX(av, AT_ENTRY, elfhdrp->e_entry);
6340Sstevel@tonic-gate AUX(av, AT_PAGESZ, pagesize);
6350Sstevel@tonic-gate AUX(av, AT_PHDR, allphdrs);
6360Sstevel@tonic-gate AUX(av, AT_PHNUM, elfhdrp->e_phnum);
6370Sstevel@tonic-gate AUX(av, AT_PHENT, elfhdrp->e_phentsize);
6380Sstevel@tonic-gate if (use_align)
6390Sstevel@tonic-gate AUX(av, AT_SUN_LPAGESZ, npagesize);
6400Sstevel@tonic-gate AUX(av, AT_SUN_IFLUSH, icache_flush);
6410Sstevel@tonic-gate if (cpulist != NULL)
6420Sstevel@tonic-gate AUX(av, AT_SUN_CPU, cpulist);
6430Sstevel@tonic-gate if (mmulist != NULL)
6440Sstevel@tonic-gate AUX(av, AT_SUN_MMU, mmulist);
6450Sstevel@tonic-gate AUX(av, AT_NULL, 0);
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate * Realloc vectors and copy them.
6480Sstevel@tonic-gate */
6490Sstevel@tonic-gate vsize = (caddr_t)bv - (caddr_t)bootv;
6500Sstevel@tonic-gate if ((elfbootvec = (Elf32_Boot *)kmem_alloc(vsize, 0)) == NULL)
6510Sstevel@tonic-gate goto elferror;
6520Sstevel@tonic-gate bcopy((char *)bootv, (char *)elfbootvec, vsize);
6530Sstevel@tonic-gate
6540Sstevel@tonic-gate size = (caddr_t)av - (caddr_t)auxv;
6550Sstevel@tonic-gate if (size > sizeof (auxv)) {
6560Sstevel@tonic-gate printf("readelf: overrun of available aux vectors\n");
6570Sstevel@tonic-gate kmem_free(elfbootvec, vsize);
6580Sstevel@tonic-gate goto elferror;
6590Sstevel@tonic-gate }
660785Seota /* use uintptr_t to suppress the gcc warning */
6610Sstevel@tonic-gate if ((elfbootvec->eb_un.eb_ptr =
662785Seota (Elf32_Addr)(uintptr_t)kmem_alloc(size, 0)) == NULL) {
6630Sstevel@tonic-gate kmem_free(elfbootvec, vsize);
6640Sstevel@tonic-gate goto elferror;
6650Sstevel@tonic-gate }
666785Seota /* use uintptr_t to suppress the gcc warning */
667785Seota bcopy(auxv,
668785Seota (void *)(uintptr_t)(elfbootvec->eb_un.eb_ptr), size);
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate #if defined(_ELF64_SUPPORT) && !defined(BOOTAMD64)
6710Sstevel@tonic-gate /*
6720Sstevel@tonic-gate * Make an LP64 copy of the vector for use by 64-bit standalones
6730Sstevel@tonic-gate * even if they have ELF32.
6740Sstevel@tonic-gate */
6750Sstevel@tonic-gate if ((elfbootvecELF32_64 = (Elf32_Boot *)kmem_alloc(vsize, 0))
6760Sstevel@tonic-gate == NULL)
6770Sstevel@tonic-gate goto elferror;
6780Sstevel@tonic-gate bcopy(bootv, elfbootvecELF32_64, vsize);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate size = (av - auxv) * sizeof (auxv64_t);
681785Seota /* use uintptr_t to suppress the gcc warning */
6820Sstevel@tonic-gate if ((elfbootvecELF32_64->eb_un.eb_ptr =
683785Seota (Elf32_Addr)(uintptr_t)kmem_alloc(size, 0)) == NULL) {
6840Sstevel@tonic-gate kmem_free(elfbootvecELF32_64, vsize);
6850Sstevel@tonic-gate goto elferror;
6860Sstevel@tonic-gate } else {
6870Sstevel@tonic-gate auxv64_t *a64 =
688785Seota (auxv64_t *)(uintptr_t)
689785Seota elfbootvecELF32_64->eb_un.eb_ptr;
6900Sstevel@tonic-gate auxv32_t *a = auxv;
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate for (a = auxv; a < av; a++) {
6930Sstevel@tonic-gate a64->a_type = a->a_type;
6940Sstevel@tonic-gate a64->a_un.a_val = a->a_un.a_val;
6950Sstevel@tonic-gate a64++;
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate #endif /* _ELF64_SUPPORT && !BOOTAMD64 */
6990Sstevel@tonic-gate } else {
7000Sstevel@tonic-gate kmem_free(allphdrs, phdrsize);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate return (entrypt);
7030Sstevel@tonic-gate
7040Sstevel@tonic-gate elferror:
7050Sstevel@tonic-gate if (allphdrs != NULL)
7060Sstevel@tonic-gate kmem_free(allphdrs, phdrsize);
7070Sstevel@tonic-gate if (nhdr != NULL)
7080Sstevel@tonic-gate kmem_free(nhdr, phdr->p_filesz);
7090Sstevel@tonic-gate printf("Elf32 read error.\n");
7100Sstevel@tonic-gate return (FAIL);
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate
7130Sstevel@tonic-gate #ifdef _ELF64_SUPPORT
7140Sstevel@tonic-gate /*
7150Sstevel@tonic-gate * Macros to add attribute/values to the ELF bootstrap vector
7160Sstevel@tonic-gate * and the aux vector.
7170Sstevel@tonic-gate */
7180Sstevel@tonic-gate #define AUX64(p, a, v) { (p)->a_type = (a); \
7190Sstevel@tonic-gate ((p)++)->a_un.a_val = (uint64_t)(v); }
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate #define EBV64(p, a, v) { (p)->eb_tag = (a); \
7220Sstevel@tonic-gate ((p)++)->eb_un.eb_val = (Elf64_Xword)(v); }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate static uint64_t
read_elf64(int fd,int print,Elf64_Ehdr * elfhdrp)7250Sstevel@tonic-gate read_elf64(int fd, int print, Elf64_Ehdr *elfhdrp)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate Elf64_Phdr *phdr; /* program header */
7280Sstevel@tonic-gate Elf64_Nhdr *nhdr; /* note header */
7290Sstevel@tonic-gate int nphdrs, phdrsize;
7300Sstevel@tonic-gate caddr_t allphdrs;
7310Sstevel@tonic-gate caddr_t namep, descp;
7320Sstevel@tonic-gate Elf64_Addr loadaddr, base;
7330Sstevel@tonic-gate size_t offset = 0;
7340Sstevel@tonic-gate size_t size;
7350Sstevel@tonic-gate int i;
7360Sstevel@tonic-gate uintptr_t off;
7370Sstevel@tonic-gate int bss_seen = 0;
7380Sstevel@tonic-gate int interp = 0; /* interpreter required */
7390Sstevel@tonic-gate static char dlname[MAXPATHLEN]; /* name of interpeter */
7400Sstevel@tonic-gate uintptr_t dynamic; /* dynamic tags array */
7410Sstevel@tonic-gate Elf64_Phdr *thdr; /* "text" program header */
7420Sstevel@tonic-gate Elf64_Phdr *dhdr; /* "data" program header */
7430Sstevel@tonic-gate Elf64_Addr entrypt; /* entry point of standalone */
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate /* Initialize pointers so we won't free bogus ones on elf64error */
7460Sstevel@tonic-gate allphdrs = NULL;
7470Sstevel@tonic-gate nhdr = NULL;
748641Skalai #if defined(__sparcv9)
7490Sstevel@tonic-gate client_isLP64 = 1;
750641Skalai #endif /* __sparcv9 */
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate if (verbosemode)
7530Sstevel@tonic-gate printf("Elf64 client\n");
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate if (elfhdrp->e_phnum == 0 || elfhdrp->e_phoff == 0)
7560Sstevel@tonic-gate goto elf64error;
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate entrypt = elfhdrp->e_entry;
7590Sstevel@tonic-gate if (verbosemode)
7600Sstevel@tonic-gate dprintf("Entry point: 0x%llx\n", (u_longlong_t)entrypt);
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate /*
7630Sstevel@tonic-gate * Allocate and read in all the program headers.
7640Sstevel@tonic-gate */
7650Sstevel@tonic-gate nphdrs = elfhdrp->e_phnum;
7660Sstevel@tonic-gate phdrsize = nphdrs * elfhdrp->e_phentsize;
7670Sstevel@tonic-gate allphdrs = (caddr_t)kmem_alloc(phdrsize, 0);
7680Sstevel@tonic-gate if (allphdrs == NULL)
7690Sstevel@tonic-gate goto elf64error;
7700Sstevel@tonic-gate if (verbosemode)
7710Sstevel@tonic-gate dprintf("lseek: args = %x %llx %x\n", fd,
7720Sstevel@tonic-gate (u_longlong_t)elfhdrp->e_phoff, 0);
7730Sstevel@tonic-gate if (lseek(fd, elfhdrp->e_phoff, 0) == -1)
7740Sstevel@tonic-gate goto elf64error;
7750Sstevel@tonic-gate if (xread(fd, allphdrs, phdrsize) != phdrsize)
7760Sstevel@tonic-gate goto elf64error;
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate /*
7790Sstevel@tonic-gate * First look for PT_NOTE headers that tell us what pagesize to
7800Sstevel@tonic-gate * use in allocating program memory.
7810Sstevel@tonic-gate */
7820Sstevel@tonic-gate npagesize = 0;
7830Sstevel@tonic-gate for (i = 0; i < nphdrs; i++) {
7840Sstevel@tonic-gate void *note_buf;
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate phdr = (Elf64_Phdr *)(allphdrs + elfhdrp->e_phentsize * i);
7870Sstevel@tonic-gate if (phdr->p_type != PT_NOTE)
7880Sstevel@tonic-gate continue;
7890Sstevel@tonic-gate if (verbosemode) {
7900Sstevel@tonic-gate dprintf("allocating 0x%llx bytes for note hdr\n",
7915648Ssetje (u_longlong_t)phdr->p_filesz);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate if ((note_buf = kmem_alloc(phdr->p_filesz, 0)) == NULL)
7940Sstevel@tonic-gate goto elf64error;
7950Sstevel@tonic-gate if (verbosemode)
7960Sstevel@tonic-gate dprintf("seeking to 0x%llx\n",
7970Sstevel@tonic-gate (u_longlong_t)phdr->p_offset);
7980Sstevel@tonic-gate if (lseek(fd, phdr->p_offset, 0) == -1)
7990Sstevel@tonic-gate goto elf64error;
8000Sstevel@tonic-gate if (verbosemode) {
8010Sstevel@tonic-gate dprintf("reading 0x%llx bytes into 0x%p\n",
8025648Ssetje (u_longlong_t)phdr->p_filesz, (void *)nhdr);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate nhdr = (Elf64_Nhdr *)note_buf;
8050Sstevel@tonic-gate if (xread(fd, (caddr_t)nhdr, phdr->p_filesz) != phdr->p_filesz)
8060Sstevel@tonic-gate goto elf64error;
8070Sstevel@tonic-gate if (verbosemode) {
8080Sstevel@tonic-gate dprintf("p_note namesz %x descsz %x type %x\n",
8095648Ssetje nhdr->n_namesz, nhdr->n_descsz, nhdr->n_type);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate /*
8130Sstevel@tonic-gate * Iterate through all ELF PT_NOTE elements looking for
8140Sstevel@tonic-gate * ELF_NOTE_SOLARIS which, if present, will specify the
8150Sstevel@tonic-gate * executable's preferred pagesize.
8160Sstevel@tonic-gate */
8170Sstevel@tonic-gate do {
8180Sstevel@tonic-gate namep = (caddr_t)(nhdr + 1);
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate if (nhdr->n_namesz == strlen(ELF_NOTE_SOLARIS) + 1 &&
8210Sstevel@tonic-gate strcmp(namep, ELF_NOTE_SOLARIS) == 0 &&
8220Sstevel@tonic-gate nhdr->n_type == ELF_NOTE_PAGESIZE_HINT) {
8230Sstevel@tonic-gate descp = namep + roundup(nhdr->n_namesz, 4);
8240Sstevel@tonic-gate npagesize = *(int *)descp;
8250Sstevel@tonic-gate if (verbosemode)
8260Sstevel@tonic-gate dprintf("pagesize is %x\n", npagesize);
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate offset += sizeof (Elf64_Nhdr) + roundup(nhdr->n_namesz,
8300Sstevel@tonic-gate 4) + roundup(nhdr->n_descsz, 4);
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate nhdr = (Elf64_Nhdr *)((char *)note_buf + offset);
8330Sstevel@tonic-gate } while (offset < phdr->p_filesz);
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate kmem_free(note_buf, phdr->p_filesz);
8360Sstevel@tonic-gate nhdr = NULL;
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate /*
8400Sstevel@tonic-gate * Next look for PT_LOAD headers to read in.
8410Sstevel@tonic-gate */
8420Sstevel@tonic-gate if (print)
8430Sstevel@tonic-gate printf("Size: ");
8440Sstevel@tonic-gate for (i = 0; i < nphdrs; i++) {
8450Sstevel@tonic-gate phdr = (Elf64_Phdr *)(allphdrs + elfhdrp->e_phentsize * i);
8460Sstevel@tonic-gate if (verbosemode) {
8470Sstevel@tonic-gate dprintf("Doing header 0x%x\n", i);
8480Sstevel@tonic-gate dprintf("phdr\n");
8490Sstevel@tonic-gate dprintf("\tp_offset = %llx, p_vaddr = %llx\n",
8505648Ssetje (u_longlong_t)phdr->p_offset,
8515648Ssetje (u_longlong_t)phdr->p_vaddr);
8520Sstevel@tonic-gate dprintf("\tp_memsz = %llx, p_filesz = %llx\n",
8535648Ssetje (u_longlong_t)phdr->p_memsz,
8545648Ssetje (u_longlong_t)phdr->p_filesz);
8550Sstevel@tonic-gate dprintf("\tp_type = %x, p_flags = %x\n",
8565648Ssetje phdr->p_type, phdr->p_flags);
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate if (phdr->p_type == PT_LOAD) {
8590Sstevel@tonic-gate if (verbosemode)
8600Sstevel@tonic-gate dprintf("seeking to 0x%llx\n",
8610Sstevel@tonic-gate (u_longlong_t)phdr->p_offset);
8620Sstevel@tonic-gate if (lseek(fd, phdr->p_offset, 0) == -1)
8630Sstevel@tonic-gate goto elf64error;
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate if (phdr->p_flags == (PF_R | PF_W) &&
8665648Ssetje phdr->p_vaddr == 0) {
8670Sstevel@tonic-gate /*
8680Sstevel@tonic-gate * It's a PT_LOAD segment that is RW but
8690Sstevel@tonic-gate * not executable and has a vaddr
8700Sstevel@tonic-gate * of zero. This is relocation info that
8710Sstevel@tonic-gate * doesn't need to stick around after
8720Sstevel@tonic-gate * krtld is done with it. We allocate boot
8730Sstevel@tonic-gate * memory for this segment, since we don't want
8740Sstevel@tonic-gate * it mapped in permanently as part of
8750Sstevel@tonic-gate * the kernel image.
8760Sstevel@tonic-gate */
8770Sstevel@tonic-gate #ifdef BOOTAMD64
8780Sstevel@tonic-gate if ((loadaddr = (Elf64_Addr)
8790Sstevel@tonic-gate (ADDR_XTND(kmem_alloc(phdr->p_memsz, 0))))
8800Sstevel@tonic-gate == NULL)
8810Sstevel@tonic-gate #else /* !BOOTAMD64 */
8820Sstevel@tonic-gate if ((loadaddr = (Elf64_Addr)(uintptr_t)
8830Sstevel@tonic-gate kmem_alloc(phdr->p_memsz, 0)) == NULL)
8840Sstevel@tonic-gate #endif /* BOOTAMD64 */
8850Sstevel@tonic-gate goto elf64error;
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate /*
8880Sstevel@tonic-gate * Save this to pass on
8890Sstevel@tonic-gate * to the interpreter.
8900Sstevel@tonic-gate */
8910Sstevel@tonic-gate phdr->p_vaddr = loadaddr;
8920Sstevel@tonic-gate } else {
8930Sstevel@tonic-gate if (print)
8940Sstevel@tonic-gate printf("0x%llx+",
8950Sstevel@tonic-gate (u_longlong_t)phdr->p_filesz);
8960Sstevel@tonic-gate /*
8970Sstevel@tonic-gate * If we found a new pagesize above, use it
8980Sstevel@tonic-gate * to adjust the memory allocation.
8990Sstevel@tonic-gate */
9000Sstevel@tonic-gate loadaddr = phdr->p_vaddr;
9010Sstevel@tonic-gate if (use_align && npagesize != 0) {
9020Sstevel@tonic-gate off = loadaddr & (npagesize - 1);
9030Sstevel@tonic-gate size = roundup(phdr->p_memsz + off,
9045648Ssetje npagesize);
9050Sstevel@tonic-gate base = loadaddr - off;
9060Sstevel@tonic-gate } else {
9070Sstevel@tonic-gate npagesize = 0;
9080Sstevel@tonic-gate size = phdr->p_memsz;
9090Sstevel@tonic-gate base = loadaddr;
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate /*
9120Sstevel@tonic-gate * Check if it's text or data.
9130Sstevel@tonic-gate */
9140Sstevel@tonic-gate if (phdr->p_flags & PF_W)
9150Sstevel@tonic-gate dhdr = phdr;
9160Sstevel@tonic-gate else
9170Sstevel@tonic-gate thdr = phdr;
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate if (verbosemode)
9200Sstevel@tonic-gate dprintf(
9210Sstevel@tonic-gate "allocating memory: %llx %lx %x\n",
9220Sstevel@tonic-gate (u_longlong_t)base,
9230Sstevel@tonic-gate size, npagesize);
9240Sstevel@tonic-gate
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate * If memory size is zero just ignore this
9270Sstevel@tonic-gate * header.
9280Sstevel@tonic-gate */
9290Sstevel@tonic-gate if (size == 0)
9300Sstevel@tonic-gate continue;
9310Sstevel@tonic-gate
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate * We're all set up to read.
9340Sstevel@tonic-gate * Now let's allocate some memory.
9350Sstevel@tonic-gate */
936231Sjg if (get_progmemory((caddr_t)(uintptr_t)base,
937231Sjg size, npagesize))
9380Sstevel@tonic-gate goto elf64error;
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate if (verbosemode) {
9420Sstevel@tonic-gate dprintf("reading 0x%llx bytes into 0x%llx\n",
9435648Ssetje (u_longlong_t)phdr->p_filesz,
9445648Ssetje (u_longlong_t)loadaddr);
9450Sstevel@tonic-gate }
946231Sjg if (xread(fd, (caddr_t)(uintptr_t)
947231Sjg loadaddr, phdr->p_filesz) != phdr->p_filesz)
9480Sstevel@tonic-gate goto elf64error;
9490Sstevel@tonic-gate
9500Sstevel@tonic-gate /* zero out BSS */
9510Sstevel@tonic-gate if (phdr->p_memsz > phdr->p_filesz) {
9520Sstevel@tonic-gate loadaddr += phdr->p_filesz;
9530Sstevel@tonic-gate if (verbosemode) {
9540Sstevel@tonic-gate dprintf("bss from 0x%llx size 0x%llx\n",
9550Sstevel@tonic-gate (u_longlong_t)loadaddr,
9560Sstevel@tonic-gate (u_longlong_t)(phdr->p_memsz -
9570Sstevel@tonic-gate phdr->p_filesz));
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate
960231Sjg bzero((caddr_t)(uintptr_t)loadaddr,
9610Sstevel@tonic-gate phdr->p_memsz - phdr->p_filesz);
9620Sstevel@tonic-gate bss_seen++;
9630Sstevel@tonic-gate if (print)
9640Sstevel@tonic-gate printf("0x%llx Bytes\n",
9650Sstevel@tonic-gate (u_longlong_t)(phdr->p_memsz -
9660Sstevel@tonic-gate phdr->p_filesz));
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate /* force instructions to be visible to icache */
9700Sstevel@tonic-gate if (phdr->p_flags & PF_X)
971231Sjg sync_instruction_memory((caddr_t)(uintptr_t)
972231Sjg phdr->p_vaddr, phdr->p_memsz);
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate } else if (phdr->p_type == PT_INTERP) {
9750Sstevel@tonic-gate /*
9760Sstevel@tonic-gate * Dynamically-linked executable.
9770Sstevel@tonic-gate */
9780Sstevel@tonic-gate interp = 1;
9790Sstevel@tonic-gate if (lseek(fd, phdr->p_offset, 0) == -1) {
9800Sstevel@tonic-gate goto elf64error;
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate /*
9830Sstevel@tonic-gate * Get the name of the interpreter.
9840Sstevel@tonic-gate */
9850Sstevel@tonic-gate if (xread(fd, dlname, phdr->p_filesz) !=
9860Sstevel@tonic-gate phdr->p_filesz ||
9870Sstevel@tonic-gate dlname[phdr->p_filesz - 1] != '\0')
9880Sstevel@tonic-gate goto elf64error;
9890Sstevel@tonic-gate } else if (phdr->p_type == PT_DYNAMIC) {
9900Sstevel@tonic-gate dynamic = phdr->p_vaddr;
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate
9940Sstevel@tonic-gate if (!bss_seen && print)
9950Sstevel@tonic-gate printf("0 Bytes\n");
9960Sstevel@tonic-gate
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate * Load the interpreter
9990Sstevel@tonic-gate * if there is one.
10000Sstevel@tonic-gate */
10010Sstevel@tonic-gate if (interp) {
10020Sstevel@tonic-gate Elf64_Boot bootv[EB_MAX]; /* Bootstrap vector */
10030Sstevel@tonic-gate auxv64_t auxv[__BOOT_NAUXV_IMPL]; /* Aux vector */
10040Sstevel@tonic-gate Elf64_Boot *bv = bootv;
10050Sstevel@tonic-gate auxv64_t *av = auxv;
10060Sstevel@tonic-gate size_t vsize;
10070Sstevel@tonic-gate
10080Sstevel@tonic-gate /*
10090Sstevel@tonic-gate * Load it.
10100Sstevel@tonic-gate */
10110Sstevel@tonic-gate if ((entrypt = iload64(dlname, thdr, dhdr, &av)) ==
10120Sstevel@tonic-gate FAIL_ILOAD64)
10130Sstevel@tonic-gate goto elf64error;
10140Sstevel@tonic-gate /*
10150Sstevel@tonic-gate * Build bootstrap and aux vectors.
10160Sstevel@tonic-gate */
10170Sstevel@tonic-gate setup_aux();
10180Sstevel@tonic-gate EBV64(bv, EB_AUXV, 0); /* fill in later */
10190Sstevel@tonic-gate EBV64(bv, EB_PAGESIZE, pagesize);
10200Sstevel@tonic-gate EBV64(bv, EB_DYNAMIC, dynamic);
10210Sstevel@tonic-gate EBV64(bv, EB_NULL, 0);
10220Sstevel@tonic-gate
10230Sstevel@tonic-gate AUX64(av, AT_BASE, entrypt);
10240Sstevel@tonic-gate AUX64(av, AT_ENTRY, elfhdrp->e_entry);
10250Sstevel@tonic-gate AUX64(av, AT_PAGESZ, pagesize);
1026231Sjg AUX64(av, AT_PHDR, (uintptr_t)allphdrs);
10270Sstevel@tonic-gate AUX64(av, AT_PHNUM, elfhdrp->e_phnum);
10280Sstevel@tonic-gate AUX64(av, AT_PHENT, elfhdrp->e_phentsize);
10290Sstevel@tonic-gate if (npagesize)
10300Sstevel@tonic-gate AUX64(av, AT_SUN_LPAGESZ, npagesize);
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate #ifdef BOOTAMD64
10330Sstevel@tonic-gate vsize = strlen(amd64_getmmulist()) + 1;
10340Sstevel@tonic-gate if ((mmulist = kmem_alloc(vsize, 0)) == NULL)
10350Sstevel@tonic-gate goto elf64error;
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate bcopy(amd64_getmmulist(), mmulist, vsize);
10380Sstevel@tonic-gate AUX64(av, AT_SUN_MMU, (uintptr_t)mmulist);
10390Sstevel@tonic-gate #endif /* BOOTAMD64 */
10400Sstevel@tonic-gate
10410Sstevel@tonic-gate AUX64(av, AT_SUN_IFLUSH, icache_flush);
10420Sstevel@tonic-gate if (cpulist != NULL)
1043231Sjg AUX64(av, AT_SUN_CPU, (uintptr_t)cpulist);
10440Sstevel@tonic-gate AUX64(av, AT_NULL, 0);
10450Sstevel@tonic-gate /*
10460Sstevel@tonic-gate * Realloc vectors and copy them.
10470Sstevel@tonic-gate */
10480Sstevel@tonic-gate vsize = (caddr_t)bv - (caddr_t)bootv;
10490Sstevel@tonic-gate if ((elfbootvecELF64 =
10500Sstevel@tonic-gate (Elf64_Boot *)kmem_alloc(vsize, 0)) == NULL)
10510Sstevel@tonic-gate goto elf64error;
10520Sstevel@tonic-gate bcopy((char *)bootv, (char *)elfbootvecELF64, vsize);
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate size = (caddr_t)av - (caddr_t)auxv;
10550Sstevel@tonic-gate if (size > sizeof (auxv)) {
10560Sstevel@tonic-gate printf("readelf: overrun of available aux vectors\n");
10570Sstevel@tonic-gate kmem_free(elfbootvecELF64, vsize);
10580Sstevel@tonic-gate goto elf64error;
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate
10610Sstevel@tonic-gate #ifdef BOOTAMD64
10620Sstevel@tonic-gate if ((elfbootvecELF64->eb_un.eb_ptr =
10630Sstevel@tonic-gate ADDR_XTND(kmem_alloc(size, 0))) == NULL) {
10640Sstevel@tonic-gate kmem_free(elfbootvecELF64, vsize);
10650Sstevel@tonic-gate goto elf64error;
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate bcopy((char *)auxv,
10690Sstevel@tonic-gate (char *)ADDR_TRUNC((elfbootvecELF64->eb_un.eb_ptr)), size);
10700Sstevel@tonic-gate #else /* !BOOTAMD64 */
10710Sstevel@tonic-gate if ((elfbootvecELF64->eb_un.eb_ptr =
10720Sstevel@tonic-gate (Elf64_Addr)kmem_alloc(size, 0)) == NULL) {
10730Sstevel@tonic-gate kmem_free(elfbootvecELF64, vsize);
10740Sstevel@tonic-gate goto elf64error;
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate
10770Sstevel@tonic-gate bcopy((char *)auxv, (char *)(elfbootvecELF64->eb_un.eb_ptr),
10785648Ssetje size);
10790Sstevel@tonic-gate #endif /* BOOTAMD64 */
10800Sstevel@tonic-gate } else {
10810Sstevel@tonic-gate kmem_free(allphdrs, phdrsize);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate return ((uint64_t)entrypt);
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate elf64error:
10860Sstevel@tonic-gate if (allphdrs != NULL)
10870Sstevel@tonic-gate kmem_free(allphdrs, phdrsize);
10880Sstevel@tonic-gate if (nhdr != NULL)
10890Sstevel@tonic-gate kmem_free(nhdr, phdr->p_filesz);
10900Sstevel@tonic-gate printf("Elf64 read error.\n");
10910Sstevel@tonic-gate return (FAIL_READELF64);
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate #endif /* _ELF64_SUPPORT */
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate /*
10960Sstevel@tonic-gate * Load the interpreter. It expects a
10970Sstevel@tonic-gate * relocatable .o capable of bootstrapping
10980Sstevel@tonic-gate * itself.
10990Sstevel@tonic-gate */
11000Sstevel@tonic-gate static func_t
iload32(char * rtld,Elf32_Phdr * thdr,Elf32_Phdr * dhdr,auxv32_t ** avp)11010Sstevel@tonic-gate iload32(char *rtld, Elf32_Phdr *thdr, Elf32_Phdr *dhdr, auxv32_t **avp)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate Elf32_Ehdr *ehdr = NULL;
11040Sstevel@tonic-gate uintptr_t dl_entry = 0;
11050Sstevel@tonic-gate uint_t i;
11060Sstevel@tonic-gate int fd;
11070Sstevel@tonic-gate int size;
11080Sstevel@tonic-gate caddr_t shdrs = NULL;
11090Sstevel@tonic-gate caddr_t etext, edata;
11100Sstevel@tonic-gate
1111785Seota /* use uintptr_t to suppress the gcc warning */
1112785Seota etext = (caddr_t)(uintptr_t)thdr->p_vaddr + thdr->p_memsz;
1113785Seota edata = (caddr_t)(uintptr_t)dhdr->p_vaddr + dhdr->p_memsz;
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate * Get the module path.
11170Sstevel@tonic-gate */
11180Sstevel@tonic-gate module_path = getmodpath(filename);
11190Sstevel@tonic-gate
11200Sstevel@tonic-gate if ((fd = openpath(module_path, rtld, O_RDONLY)) < 0) {
11210Sstevel@tonic-gate printf("boot: cannot find %s\n", rtld);
11220Sstevel@tonic-gate goto errorx;
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate dprintf("Opened %s OK\n", rtld);
11250Sstevel@tonic-gate AUX(*avp, AT_SUN_LDNAME, rtld);
11260Sstevel@tonic-gate /*
11270Sstevel@tonic-gate * Allocate and read the ELF header.
11280Sstevel@tonic-gate */
11290Sstevel@tonic-gate if ((ehdr = (Elf32_Ehdr *)kmem_alloc(sizeof (Elf32_Ehdr), 0)) == NULL) {
11300Sstevel@tonic-gate printf("boot: alloc error reading ELF header (%s).\n", rtld);
11310Sstevel@tonic-gate goto error;
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate if (xread(fd, (char *)ehdr, sizeof (*ehdr)) != sizeof (*ehdr)) {
11350Sstevel@tonic-gate printf("boot: error reading ELF header (%s).\n", rtld);
11360Sstevel@tonic-gate goto error;
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate size = ehdr->e_shentsize * ehdr->e_shnum;
11400Sstevel@tonic-gate if ((shdrs = (caddr_t)kmem_alloc(size, 0)) == NULL) {
11410Sstevel@tonic-gate printf("boot: alloc error reading ELF header (%s).\n", rtld);
11420Sstevel@tonic-gate goto error;
11430Sstevel@tonic-gate }
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate * Read the section headers.
11460Sstevel@tonic-gate */
11470Sstevel@tonic-gate if (lseek(fd, ehdr->e_shoff, 0) == -1 ||
11480Sstevel@tonic-gate xread(fd, shdrs, size) != size) {
11490Sstevel@tonic-gate printf("boot: error reading section headers\n");
11500Sstevel@tonic-gate goto error;
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate AUX(*avp, AT_SUN_LDELF, ehdr);
11530Sstevel@tonic-gate AUX(*avp, AT_SUN_LDSHDR, shdrs);
11540Sstevel@tonic-gate /*
11550Sstevel@tonic-gate * Load sections into the appropriate dynamic segment.
11560Sstevel@tonic-gate */
11570Sstevel@tonic-gate for (i = 1; i < ehdr->e_shnum; i++) {
11580Sstevel@tonic-gate Elf32_Shdr *sp;
11590Sstevel@tonic-gate caddr_t *spp;
11600Sstevel@tonic-gate caddr_t load;
11610Sstevel@tonic-gate
11620Sstevel@tonic-gate sp = (Elf32_Shdr *)(shdrs + (i*ehdr->e_shentsize));
11630Sstevel@tonic-gate /*
11640Sstevel@tonic-gate * If it's not allocated and not required
11650Sstevel@tonic-gate * to do relocation, skip it.
11660Sstevel@tonic-gate */
11670Sstevel@tonic-gate if (!(sp->sh_flags & SHF_ALLOC) &&
11685648Ssetje #ifdef i386
11695648Ssetje sp->sh_type != SHT_REL &&
11705648Ssetje #else
11715648Ssetje sp->sh_type != SHT_RELA &&
11725648Ssetje #endif
11730Sstevel@tonic-gate sp->sh_type != SHT_SYMTAB &&
11745648Ssetje sp->sh_type != SHT_STRTAB)
11750Sstevel@tonic-gate continue;
11760Sstevel@tonic-gate /*
11770Sstevel@tonic-gate * If the section is read-only,
11780Sstevel@tonic-gate * it goes in as text.
11790Sstevel@tonic-gate */
11800Sstevel@tonic-gate spp = (sp->sh_flags & SHF_WRITE)? &edata: &etext;
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate * Make some room for it.
11830Sstevel@tonic-gate */
11840Sstevel@tonic-gate load = segbrk(spp, sp->sh_size, sp->sh_addralign);
11850Sstevel@tonic-gate if (load == NULL) {
11860Sstevel@tonic-gate printf("boot: allocating memory for sections failed\n");
11870Sstevel@tonic-gate goto error;
11880Sstevel@tonic-gate }
11890Sstevel@tonic-gate /*
11900Sstevel@tonic-gate * Compute the entry point of the linker.
11910Sstevel@tonic-gate */
11920Sstevel@tonic-gate if (dl_entry == 0 &&
11930Sstevel@tonic-gate !(sp->sh_flags & SHF_WRITE) &&
11940Sstevel@tonic-gate (sp->sh_flags & SHF_EXECINSTR)) {
11950Sstevel@tonic-gate dl_entry = (uintptr_t)load + ehdr->e_entry;
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate * If it's bss, just zero it out.
11990Sstevel@tonic-gate */
12000Sstevel@tonic-gate if (sp->sh_type == SHT_NOBITS) {
12010Sstevel@tonic-gate bzero(load, sp->sh_size);
12020Sstevel@tonic-gate } else {
12030Sstevel@tonic-gate /*
12040Sstevel@tonic-gate * Read the section contents.
12050Sstevel@tonic-gate */
12060Sstevel@tonic-gate if (lseek(fd, sp->sh_offset, 0) == -1 ||
12070Sstevel@tonic-gate xread(fd, load, sp->sh_size) != sp->sh_size) {
12080Sstevel@tonic-gate printf("boot: error reading sections\n");
12090Sstevel@tonic-gate goto error;
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate /*
1213785Seota * Assign the section's virtual addr. Use uintptr_t to
1214785Seota * suppress the gcc warning.
12150Sstevel@tonic-gate */
1216785Seota sp->sh_addr = (Elf32_Off)(uintptr_t)load;
1217785Seota /*
1218785Seota * Force instructions to be visible to icache. Use
1219785Seota * uintptr_t to suppress the gcc warning as well.
1220785Seota */
12210Sstevel@tonic-gate if (sp->sh_flags & SHF_EXECINSTR)
1222785Seota sync_instruction_memory((caddr_t)(uintptr_t)sp->sh_addr,
12230Sstevel@tonic-gate sp->sh_size);
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate /*
12260Sstevel@tonic-gate * Update sizes of segments.
12270Sstevel@tonic-gate */
12280Sstevel@tonic-gate thdr->p_memsz = (Elf32_Word)((uintptr_t)etext - thdr->p_vaddr);
12290Sstevel@tonic-gate dhdr->p_memsz = (Elf32_Word)((uintptr_t)edata - dhdr->p_vaddr);
12300Sstevel@tonic-gate
12310Sstevel@tonic-gate /* load and relocate symbol tables in SAS */
12320Sstevel@tonic-gate (void) close(fd);
12330Sstevel@tonic-gate return ((func_t)dl_entry);
12340Sstevel@tonic-gate
12350Sstevel@tonic-gate error:
12360Sstevel@tonic-gate (void) close(fd);
12370Sstevel@tonic-gate errorx:
12380Sstevel@tonic-gate if (ehdr)
12390Sstevel@tonic-gate kmem_free(ehdr, sizeof (Elf32_Ehdr));
12400Sstevel@tonic-gate if (shdrs)
12410Sstevel@tonic-gate kmem_free(shdrs, size);
12420Sstevel@tonic-gate printf("boot: error loading interpreter (%s)\n", rtld);
12430Sstevel@tonic-gate return (FAIL);
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate #ifdef _ELF64_SUPPORT
12470Sstevel@tonic-gate /*
12480Sstevel@tonic-gate * Load the interpreter. It expects a
12490Sstevel@tonic-gate * relocatable .o capable of bootstrapping
12500Sstevel@tonic-gate * itself.
12510Sstevel@tonic-gate */
12520Sstevel@tonic-gate static Elf64_Addr
iload64(char * rtld,Elf64_Phdr * thdr,Elf64_Phdr * dhdr,auxv64_t ** avp)12530Sstevel@tonic-gate iload64(char *rtld, Elf64_Phdr *thdr, Elf64_Phdr *dhdr, auxv64_t **avp)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate Elf64_Ehdr *ehdr = NULL;
12560Sstevel@tonic-gate Elf64_Addr dl_entry = (Elf64_Addr)0;
12570Sstevel@tonic-gate Elf64_Addr etext, edata;
12580Sstevel@tonic-gate uint_t i;
12590Sstevel@tonic-gate int fd;
12600Sstevel@tonic-gate int size;
12610Sstevel@tonic-gate caddr_t shdrs = NULL;
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate etext = thdr->p_vaddr + thdr->p_memsz;
12640Sstevel@tonic-gate edata = dhdr->p_vaddr + dhdr->p_memsz;
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate /*
12670Sstevel@tonic-gate * Get the module path.
12680Sstevel@tonic-gate */
12690Sstevel@tonic-gate module_path = getmodpath(filename);
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate if ((fd = openpath(module_path, rtld, O_RDONLY)) < 0) {
12720Sstevel@tonic-gate printf("boot: cannot find %s\n", rtld);
12730Sstevel@tonic-gate goto errorx;
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate dprintf("Opened %s OK\n", rtld);
1276231Sjg AUX64(*avp, AT_SUN_LDNAME, (uintptr_t)rtld);
12770Sstevel@tonic-gate /*
12780Sstevel@tonic-gate * Allocate and read the ELF header.
12790Sstevel@tonic-gate */
12800Sstevel@tonic-gate #ifdef BOOTAMD64
12810Sstevel@tonic-gate if ((ehdr = (Elf64_Ehdr *)(uintptr_t)kmem_alloc(sizeof (Elf64_Ehdr),
12820Sstevel@tonic-gate 0)) == NULL) {
12830Sstevel@tonic-gate #else /* !BOOTAMD64 */
12840Sstevel@tonic-gate if ((ehdr = (Elf64_Ehdr *)kmem_alloc(sizeof (Elf64_Ehdr), 0)) == NULL) {
12850Sstevel@tonic-gate #endif /* BOOTAMD64 */
12860Sstevel@tonic-gate printf("boot: alloc error reading ELF header (%s).\n", rtld);
12870Sstevel@tonic-gate goto error;
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate if (xread(fd, (char *)ehdr, sizeof (*ehdr)) != sizeof (*ehdr)) {
12910Sstevel@tonic-gate printf("boot: error reading ELF header (%s).\n", rtld);
12920Sstevel@tonic-gate goto error;
12930Sstevel@tonic-gate }
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate size = ehdr->e_shentsize * ehdr->e_shnum;
12960Sstevel@tonic-gate if ((shdrs = (caddr_t)kmem_alloc(size, 0)) == NULL) {
12970Sstevel@tonic-gate printf("boot: alloc error reading ELF header (%s).\n", rtld);
12980Sstevel@tonic-gate goto error;
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate /*
13010Sstevel@tonic-gate * Read the section headers.
13020Sstevel@tonic-gate */
13030Sstevel@tonic-gate if (lseek(fd, ehdr->e_shoff, 0) == -1 ||
13040Sstevel@tonic-gate xread(fd, shdrs, size) != size) {
13050Sstevel@tonic-gate printf("boot: error reading section headers\n");
13060Sstevel@tonic-gate goto error;
13070Sstevel@tonic-gate }
13080Sstevel@tonic-gate
13090Sstevel@tonic-gate #ifdef BOOTAMD64
13100Sstevel@tonic-gate AUX64(*avp, AT_SUN_LDELF, (uintptr_t)ehdr);
13110Sstevel@tonic-gate AUX64(*avp, AT_SUN_LDSHDR, (uintptr_t)shdrs);
13120Sstevel@tonic-gate #else /* !BOOTAMD64 */
13130Sstevel@tonic-gate AUX64(*avp, AT_SUN_LDELF, ehdr);
13140Sstevel@tonic-gate AUX64(*avp, AT_SUN_LDSHDR, shdrs);
13150Sstevel@tonic-gate #endif /* BOOTAMD64 */
13160Sstevel@tonic-gate
13170Sstevel@tonic-gate /*
13180Sstevel@tonic-gate * Load sections into the appropriate dynamic segment.
13190Sstevel@tonic-gate */
13200Sstevel@tonic-gate for (i = 1; i < ehdr->e_shnum; i++) {
13210Sstevel@tonic-gate Elf64_Shdr *sp;
13220Sstevel@tonic-gate Elf64_Addr *spp, load;
13230Sstevel@tonic-gate
13240Sstevel@tonic-gate sp = (Elf64_Shdr *)(shdrs + (i*ehdr->e_shentsize));
13250Sstevel@tonic-gate /*
13260Sstevel@tonic-gate * If it's not allocated and not required
13270Sstevel@tonic-gate * to do relocation, skip it.
13280Sstevel@tonic-gate */
13290Sstevel@tonic-gate if (!(sp->sh_flags & SHF_ALLOC) &&
13300Sstevel@tonic-gate sp->sh_type != SHT_SYMTAB &&
13310Sstevel@tonic-gate sp->sh_type != SHT_STRTAB &&
13320Sstevel@tonic-gate sp->sh_type != SHT_RELA)
13330Sstevel@tonic-gate continue;
13340Sstevel@tonic-gate /*
13350Sstevel@tonic-gate * If the section is read-only,
13360Sstevel@tonic-gate * it goes in as text.
13370Sstevel@tonic-gate */
13380Sstevel@tonic-gate spp = (sp->sh_flags & SHF_WRITE)? &edata: &etext;
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate /*
13410Sstevel@tonic-gate * Make some room for it.
13420Sstevel@tonic-gate */
13430Sstevel@tonic-gate #ifdef BOOTAMD64
13440Sstevel@tonic-gate load = ADDR_XTND(segbrk((caddr_t *)spp,
13450Sstevel@tonic-gate sp->sh_size, sp->sh_addralign));
13460Sstevel@tonic-gate #else /* !BOOTAMD64 */
13470Sstevel@tonic-gate load = (Elf64_Addr)segbrk((caddr_t *)spp, sp->sh_size,
13480Sstevel@tonic-gate sp->sh_addralign);
13490Sstevel@tonic-gate #endif /* BOOTAMD64 */
13500Sstevel@tonic-gate
13510Sstevel@tonic-gate if (load == NULL) {
13520Sstevel@tonic-gate printf("boot: allocating memory for section %d "
13530Sstevel@tonic-gate "failed\n", i);
13540Sstevel@tonic-gate goto error;
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate
13570Sstevel@tonic-gate /*
13580Sstevel@tonic-gate * Compute the entry point of the linker.
13590Sstevel@tonic-gate */
13600Sstevel@tonic-gate if (dl_entry == 0 &&
13610Sstevel@tonic-gate !(sp->sh_flags & SHF_WRITE) &&
13620Sstevel@tonic-gate (sp->sh_flags & SHF_EXECINSTR)) {
13630Sstevel@tonic-gate dl_entry = load + ehdr->e_entry;
13640Sstevel@tonic-gate if (verbosemode)
13650Sstevel@tonic-gate dprintf("boot: loading linker @ 0x%llx\n",
13660Sstevel@tonic-gate (u_longlong_t)dl_entry);
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate
13690Sstevel@tonic-gate /*
13700Sstevel@tonic-gate * If it's bss, just zero it out.
13710Sstevel@tonic-gate */
13720Sstevel@tonic-gate if (sp->sh_type == SHT_NOBITS) {
1373231Sjg bzero((caddr_t)(uintptr_t)load, sp->sh_size);
13740Sstevel@tonic-gate } else {
13750Sstevel@tonic-gate /*
13760Sstevel@tonic-gate * Read the section contents.
13770Sstevel@tonic-gate */
13780Sstevel@tonic-gate if (lseek(fd, sp->sh_offset, 0) == -1 ||
1379231Sjg xread(fd, (caddr_t)(uintptr_t)load, sp->sh_size) !=
13805648Ssetje sp->sh_size) {
13815648Ssetje printf("boot: error reading section %d\n", i);
13825648Ssetje goto error;
13830Sstevel@tonic-gate }
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate /*
13860Sstevel@tonic-gate * Assign the section's virtual addr.
13870Sstevel@tonic-gate */
13880Sstevel@tonic-gate
13890Sstevel@tonic-gate sp->sh_addr = load;
13900Sstevel@tonic-gate
13910Sstevel@tonic-gate if (verbosemode)
13920Sstevel@tonic-gate dprintf("boot: section %d, type %d, loaded @ 0x%llx, "
13930Sstevel@tonic-gate "size 0x%llx\n", i, sp->sh_type, (u_longlong_t)load,
13940Sstevel@tonic-gate (u_longlong_t)sp->sh_size);
13950Sstevel@tonic-gate
13960Sstevel@tonic-gate /* force instructions to be visible to icache */
13970Sstevel@tonic-gate if (sp->sh_flags & SHF_EXECINSTR)
1398231Sjg sync_instruction_memory((caddr_t)(uintptr_t)sp->sh_addr,
13990Sstevel@tonic-gate sp->sh_size);
14000Sstevel@tonic-gate }
14010Sstevel@tonic-gate /*
14020Sstevel@tonic-gate * Update sizes of segments.
14030Sstevel@tonic-gate */
14040Sstevel@tonic-gate thdr->p_memsz = etext - thdr->p_vaddr;
14050Sstevel@tonic-gate dhdr->p_memsz = edata - dhdr->p_vaddr;
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate /* load and relocate symbol tables in SAS */
14080Sstevel@tonic-gate (void) close(fd);
14090Sstevel@tonic-gate return (dl_entry);
14100Sstevel@tonic-gate
14110Sstevel@tonic-gate error:
14120Sstevel@tonic-gate (void) close(fd);
14130Sstevel@tonic-gate errorx:
14140Sstevel@tonic-gate if (ehdr)
14150Sstevel@tonic-gate kmem_free((caddr_t)ehdr, sizeof (Elf64_Ehdr));
14160Sstevel@tonic-gate if (shdrs)
14170Sstevel@tonic-gate kmem_free(shdrs, size);
14180Sstevel@tonic-gate printf("boot: error loading interpreter (%s)\n", rtld);
14190Sstevel@tonic-gate return (FAIL_ILOAD64);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate #endif /* _ELF64_SUPPORT */
14220Sstevel@tonic-gate
14230Sstevel@tonic-gate /*
14240Sstevel@tonic-gate * Extend the segment's "break" value by bytes.
14250Sstevel@tonic-gate */
14260Sstevel@tonic-gate static caddr_t
14270Sstevel@tonic-gate segbrk(caddr_t *spp, size_t bytes, size_t align)
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate caddr_t va, pva;
14300Sstevel@tonic-gate size_t size = 0;
14310Sstevel@tonic-gate unsigned int alloc_pagesize = pagesize;
14320Sstevel@tonic-gate unsigned int alloc_align = 0;
14330Sstevel@tonic-gate
14340Sstevel@tonic-gate if (npagesize) {
14350Sstevel@tonic-gate alloc_align = npagesize;
14360Sstevel@tonic-gate alloc_pagesize = npagesize;
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate
14390Sstevel@tonic-gate va = (caddr_t)ALIGN(*spp, align);
14400Sstevel@tonic-gate pva = (caddr_t)roundup((uintptr_t)*spp, alloc_pagesize);
14410Sstevel@tonic-gate /*
14420Sstevel@tonic-gate * Need more pages?
14430Sstevel@tonic-gate */
14440Sstevel@tonic-gate if (va + bytes > pva) {
14450Sstevel@tonic-gate size = roundup((bytes - (pva - va)), alloc_pagesize);
14460Sstevel@tonic-gate
14470Sstevel@tonic-gate if (get_progmemory(pva, size, alloc_align)) {
14480Sstevel@tonic-gate printf("boot: segbrk allocation failed, "
14490Sstevel@tonic-gate "0x%lx bytes @ %p\n", bytes, (void *)pva);
14500Sstevel@tonic-gate return (NULL);
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate *spp = va + bytes;
14540Sstevel@tonic-gate
14550Sstevel@tonic-gate return (va);
14560Sstevel@tonic-gate }
14570Sstevel@tonic-gate
14580Sstevel@tonic-gate /*
14590Sstevel@tonic-gate * Open the file using a search path and
14600Sstevel@tonic-gate * return the file descriptor (or -1 on failure).
14610Sstevel@tonic-gate */
14620Sstevel@tonic-gate static int
14630Sstevel@tonic-gate openpath(path, fname, flags)
14640Sstevel@tonic-gate char *path;
14650Sstevel@tonic-gate char *fname;
14660Sstevel@tonic-gate int flags;
14670Sstevel@tonic-gate {
14680Sstevel@tonic-gate register char *p, *q;
14690Sstevel@tonic-gate char buf[MAXPATHLEN];
14700Sstevel@tonic-gate int fd;
14710Sstevel@tonic-gate
14720Sstevel@tonic-gate /*
14730Sstevel@tonic-gate * If the file name is absolute,
14740Sstevel@tonic-gate * don't use the module search path.
14750Sstevel@tonic-gate */
14760Sstevel@tonic-gate if (fname[0] == '/')
14770Sstevel@tonic-gate return (open(fname, flags));
14780Sstevel@tonic-gate
14790Sstevel@tonic-gate q = NULL;
14800Sstevel@tonic-gate for (p = path; /* forever */; p = q) {
14810Sstevel@tonic-gate
14820Sstevel@tonic-gate while (*p == ' ' || *p == '\t' || *p == ':')
14830Sstevel@tonic-gate p++;
14840Sstevel@tonic-gate if (*p == '\0')
14850Sstevel@tonic-gate break;
14860Sstevel@tonic-gate q = p;
14870Sstevel@tonic-gate while (*q && *q != ' ' && *q != '\t' && *q != ':')
14880Sstevel@tonic-gate q++;
14890Sstevel@tonic-gate (void) strncpy(buf, p, q - p);
14900Sstevel@tonic-gate if (q[-1] != '/') {
14910Sstevel@tonic-gate buf[q - p] = '/';
14920Sstevel@tonic-gate (void) strcpy(&buf[q - p + 1], fname);
14930Sstevel@tonic-gate } else {
14940Sstevel@tonic-gate /*
14950Sstevel@tonic-gate * This checks for paths that end in '/'
14960Sstevel@tonic-gate */
14970Sstevel@tonic-gate (void) strcpy(&buf[q - p], fname);
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate
15000Sstevel@tonic-gate if ((fd = open(buf, flags)) > 0)
15010Sstevel@tonic-gate return (fd);
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate return (-1);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate
15060Sstevel@tonic-gate /*
15070Sstevel@tonic-gate * Get the module search path.
15080Sstevel@tonic-gate */
15090Sstevel@tonic-gate static char *
15100Sstevel@tonic-gate getmodpath(fname)
15110Sstevel@tonic-gate char *fname;
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate register char *p = strrchr(fname, '/');
15140Sstevel@tonic-gate static char mod_path[MOD_MAXPATH];
15150Sstevel@tonic-gate size_t len;
15160Sstevel@tonic-gate extern char *impl_arch_name;
1517641Skalai #if defined(__sparcv9) || defined(BOOTAMD64)
15180Sstevel@tonic-gate #ifdef __sparcv9
15190Sstevel@tonic-gate char *isastr = "/sparcv9";
15200Sstevel@tonic-gate #endif /* __sparcv9 */
15210Sstevel@tonic-gate #ifdef BOOTAMD64
15220Sstevel@tonic-gate char *isastr = "/amd64";
15230Sstevel@tonic-gate #endif /* BOOTAMD64 */
15240Sstevel@tonic-gate size_t isalen = strlen(isastr);
1525641Skalai #endif /* __sparcv9 || BOOTAMD64 */
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate if (p == NULL) {
15280Sstevel@tonic-gate /* strchr could not find a "/" */
15290Sstevel@tonic-gate printf("%s is not a legal kernel pathname", fname);
15300Sstevel@tonic-gate return (NULL);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate while (p > fname && *(p - 1) == '/')
15330Sstevel@tonic-gate p--; /* remove trailing "/"s */
15340Sstevel@tonic-gate if (p == fname)
15350Sstevel@tonic-gate p++; /* "/" is the modpath in this case */
15360Sstevel@tonic-gate
15370Sstevel@tonic-gate len = p - fname;
15380Sstevel@tonic-gate (void) strncpy(mod_path, fname, len);
15390Sstevel@tonic-gate mod_path[len] = 0;
15400Sstevel@tonic-gate
1541641Skalai #if defined(__sparcv9) || defined(BOOTAMD64)
15420Sstevel@tonic-gate len = strlen(mod_path);
15430Sstevel@tonic-gate if ((len > isalen) && (strcmp(&mod_path[len - isalen], isastr) == 0)) {
15440Sstevel@tonic-gate mod_path[len - isalen] = '\0';
1545641Skalai #if defined(__sparcv9)
15460Sstevel@tonic-gate if ((client_isLP64 == 0) && verbosemode)
15470Sstevel@tonic-gate printf("Assuming LP64 %s client.\n", isastr);
15480Sstevel@tonic-gate client_isLP64 = 1;
1549641Skalai #endif /* __sparcv9 */
15500Sstevel@tonic-gate }
1551641Skalai #endif /* __sparcv9 || BOOTAMD64 */
15520Sstevel@tonic-gate mod_path_uname_m(mod_path, impl_arch_name);
15530Sstevel@tonic-gate (void) strcat(mod_path, " ");
15540Sstevel@tonic-gate (void) strcat(mod_path, MOD_DEFPATH);
15550Sstevel@tonic-gate
15560Sstevel@tonic-gate if (boothowto & RB_ASKNAME) {
15570Sstevel@tonic-gate char buf[MOD_MAXPATH];
15580Sstevel@tonic-gate
15590Sstevel@tonic-gate printf("Enter default directory for modules [%s]: ", mod_path);
15600Sstevel@tonic-gate (void) cons_gets(buf, sizeof (buf));
15610Sstevel@tonic-gate if (buf[0] != '\0')
15620Sstevel@tonic-gate (void) strcpy(mod_path, buf);
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate if (verbosemode)
15650Sstevel@tonic-gate printf("modpath: %s\n", mod_path);
15660Sstevel@tonic-gate return (mod_path);
15670Sstevel@tonic-gate }
1568