17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5986fd29aSsetje  * Common Development and Distribution License (the "License").
6986fd29aSsetje  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22db9ce1c9SJerry Gilliam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/exechdr.h>
297c478bd9Sstevel@tonic-gate #include <sys/elf.h>
307c478bd9Sstevel@tonic-gate #include <sys/elf_notes.h>
317c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
327c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
337c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/link.h>
377c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
387c478bd9Sstevel@tonic-gate #include <sys/salib.h>
397c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
407c478bd9Sstevel@tonic-gate #include <sys/platnames.h>
417c478bd9Sstevel@tonic-gate 
42bcbe9155Sjg #include "util.h"
43bcbe9155Sjg 
447c478bd9Sstevel@tonic-gate union {
457c478bd9Sstevel@tonic-gate 	struct exec X;
467c478bd9Sstevel@tonic-gate 	Elf32_Ehdr Elfhdr;
477c478bd9Sstevel@tonic-gate 	Elf64_Ehdr Elfhdr64;
487c478bd9Sstevel@tonic-gate } ex;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	x ex.X
517c478bd9Sstevel@tonic-gate #define	elfhdr ex.Elfhdr
527c478bd9Sstevel@tonic-gate #define	elfhdr64 ex.Elfhdr64
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate typedef int	(*func_t)();
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	FAIL	((func_t)-1)
577c478bd9Sstevel@tonic-gate #define	ALIGN(x, a)	\
587c478bd9Sstevel@tonic-gate 	((a) == 0 ? (uintptr_t)(x) : (((uintptr_t)(x) + (a) - 1) & ~((a) - 1)))
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	__BOOT_NAUXV_IMPL	22
617c478bd9Sstevel@tonic-gate 
62622d9a34SRichard Lowe int	use_align = 0;
63622d9a34SRichard Lowe int	npagesize = 0;
64622d9a34SRichard Lowe uint_t	icache_flush = 0;
65622d9a34SRichard Lowe char	*cpulist = NULL;
667c478bd9Sstevel@tonic-gate char	*mmulist = NULL;
67*a2cd9e18SToomas Soome extern char *module_path;		/* path for kernel modules */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * This file gets compiled in LP64 (for sun4u) and ILP32 models.
717c478bd9Sstevel@tonic-gate  * For LP64 compilation, the "client" file we load and run may be LP64 or ILP32,
727c478bd9Sstevel@tonic-gate  * and during bringup, the LP64 clients may have ELF32 headers.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate #ifdef	_ELF64_SUPPORT
757c478bd9Sstevel@tonic-gate /*
76622d9a34SRichard Lowe  * Bootstrap vector for ELF32 LP64 client
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate Elf32_Boot *elfbootvecELF32_64;
797c478bd9Sstevel@tonic-gate Elf64_Boot *elfbootvecELF64;	/* ELF bootstrap vector for Elf64 LP64 */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	OK		((func_t)0)
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	FAIL_READELF64	((uint64_t)0)
847c478bd9Sstevel@tonic-gate #define	FAIL_ILOAD64	((Elf64_Addr)-1)
857c478bd9Sstevel@tonic-gate #endif	/* _ELF64_SUPPORT */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * And by an ILP32 client. The non-sun4u/LP64 booters use these.
897c478bd9Sstevel@tonic-gate  * Also, the sun4u booter must create this for ILP32 clients.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate Elf32_Boot *elfbootvec;		/* ELF bootstrap vector normal ILP32 */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Read in a Unix executable file and return its entry point.
957c478bd9Sstevel@tonic-gate  * Handle the various a.out formats correctly.
967c478bd9Sstevel@tonic-gate  * "fd" is the standalone file descriptor to read from.
977c478bd9Sstevel@tonic-gate  * Print informative little messages if "print" is on.
987c478bd9Sstevel@tonic-gate  * Returns -1 for errors.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #ifdef DEBUG
1027c478bd9Sstevel@tonic-gate static int debug = 1;
1037c478bd9Sstevel@tonic-gate #else /* DEBUG */
1047c478bd9Sstevel@tonic-gate static int debug = 0;
1057c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	dprintf		if (debug) printf
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #ifdef	_ELF64_SUPPORT
1107c478bd9Sstevel@tonic-gate typedef struct {
1117c478bd9Sstevel@tonic-gate 	uint_t	a_type;
1127c478bd9Sstevel@tonic-gate 	union {
1137c478bd9Sstevel@tonic-gate 		uint64_t a_val;
1147c478bd9Sstevel@tonic-gate 		uint64_t a_ptr;
1157c478bd9Sstevel@tonic-gate 		void	(*a_fcn)();	/* XXX - UNUSED? */
1167c478bd9Sstevel@tonic-gate 	} a_un;
1177c478bd9Sstevel@tonic-gate } auxv64_t;
1187c478bd9Sstevel@tonic-gate 
119c2e7b48dSkalai #if defined(__sparcv9)
1207c478bd9Sstevel@tonic-gate extern int client_isLP64;
121c2e7b48dSkalai #endif	/* __sparcv9 */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static uint64_t read_elf64(int, int, Elf64_Ehdr *);
1247c478bd9Sstevel@tonic-gate static Elf64_Addr iload64(char *, Elf64_Phdr *, Elf64_Phdr *, auxv64_t **);
1257c478bd9Sstevel@tonic-gate #endif	/* _ELF64_SUPPORT */
1267c478bd9Sstevel@tonic-gate 
127622d9a34SRichard Lowe static func_t	read_elf32(int, int, Elf32_Ehdr *);
1287c478bd9Sstevel@tonic-gate static func_t	iload32(char *, Elf32_Phdr *, Elf32_Phdr *, auxv32_t **);
1297c478bd9Sstevel@tonic-gate static caddr_t	segbrk(caddr_t *, size_t, size_t);
1307c478bd9Sstevel@tonic-gate static int	openpath(char *, char *, int);
1317c478bd9Sstevel@tonic-gate static char	*getmodpath(char *);
1327c478bd9Sstevel@tonic-gate extern void	setup_aux(void);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate extern void	*kmem_alloc(size_t, int);
1357c478bd9Sstevel@tonic-gate extern void	kmem_free(void *, size_t);
1367c478bd9Sstevel@tonic-gate extern int	cons_gets(char *, int);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate extern void sync_instruction_memory(caddr_t v, size_t len);
1397c478bd9Sstevel@tonic-gate 
140622d9a34SRichard Lowe extern int	verbosemode;
1417c478bd9Sstevel@tonic-gate extern int	boothowto;
1427c478bd9Sstevel@tonic-gate extern int	pagesize;
1437c478bd9Sstevel@tonic-gate extern char	filename[];
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * repeat reads (forever) until size of request is satisfied
1477c478bd9Sstevel@tonic-gate  * (Thus, you don't want to use this cases where short reads are ok)
1487c478bd9Sstevel@tonic-gate  */
149986fd29aSsetje ssize_t
xread(int fd,char * p,size_t nbytes)1507c478bd9Sstevel@tonic-gate xread(int fd, char *p, size_t nbytes)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	size_t bytesread = 0;
1537c478bd9Sstevel@tonic-gate 	int errorcount = 0;
1547c478bd9Sstevel@tonic-gate 	ssize_t i;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	while (bytesread < nbytes) {
1577c478bd9Sstevel@tonic-gate 		i = read(fd, p, nbytes - bytesread);
1587c478bd9Sstevel@tonic-gate 		if (i < 0) {
1597c478bd9Sstevel@tonic-gate 			++errorcount;
1607c478bd9Sstevel@tonic-gate 			if (verbosemode)
1617c478bd9Sstevel@tonic-gate 				printf("read error (0x%x times)\n", errorcount);
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 		bytesread += i;
1657c478bd9Sstevel@tonic-gate 		p += i;
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	return (bytesread);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate func_t
readfile(int fd,int print)1717c478bd9Sstevel@tonic-gate readfile(int fd, int print)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate #ifdef	_ELF64_SUPPORT
1747c478bd9Sstevel@tonic-gate 	uint64_t elf64_go2;
1757c478bd9Sstevel@tonic-gate #endif	/* _ELF64_SUPPORT */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	ssize_t i;
1787c478bd9Sstevel@tonic-gate 	int shared = 0;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (verbosemode) {
1817c478bd9Sstevel@tonic-gate 		dprintf("fd = %x\n", fd);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	i = xread(fd, (char *)&elfhdr, sizeof (Elf64_Ehdr));
1857c478bd9Sstevel@tonic-gate 	if (x.a_magic == ZMAGIC || x.a_magic == NMAGIC)
1867c478bd9Sstevel@tonic-gate 		shared = 1;
1877c478bd9Sstevel@tonic-gate 	if (i != sizeof (Elf64_Ehdr)) {
1887c478bd9Sstevel@tonic-gate 		printf("Error reading ELF header.\n");
1897c478bd9Sstevel@tonic-gate 		return (FAIL);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	if (!shared && x.a_magic != OMAGIC) {
1927c478bd9Sstevel@tonic-gate 		if (*(int *)&elfhdr.e_ident == *(int *)(ELFMAG)) {
1937c478bd9Sstevel@tonic-gate 			if (verbosemode) {
1947c478bd9Sstevel@tonic-gate 				int is64 = (elfhdr.e_ident[EI_CLASS] ==
1957c478bd9Sstevel@tonic-gate 				    ELFCLASS64);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 				dprintf("calling readelf, elfheader is:\n");
1987c478bd9Sstevel@tonic-gate 				dprintf("e_ident\t0x%x, 0x%x, 0x%x, 0x%x\n",
1997c478bd9Sstevel@tonic-gate 				    *(int *)&elfhdr.e_ident[0],
2007c478bd9Sstevel@tonic-gate 				    *(int *)&elfhdr.e_ident[4],
2017c478bd9Sstevel@tonic-gate 				    *(int *)&elfhdr.e_ident[8],
2027c478bd9Sstevel@tonic-gate 				    *(int *)&elfhdr.e_ident[12]);
2037c478bd9Sstevel@tonic-gate 				dprintf("e_machine\t0x%x\n", elfhdr.e_machine);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 				dprintf("e_entry\t\t0x%llx\n", (is64 ?
2067c478bd9Sstevel@tonic-gate 				    elfhdr64.e_entry :
2077c478bd9Sstevel@tonic-gate 				    (u_longlong_t)elfhdr.e_entry));
2087c478bd9Sstevel@tonic-gate 				dprintf("e_shoff\t\t0x%llx\n", (is64 ?
2097c478bd9Sstevel@tonic-gate 				    elfhdr64.e_shoff :
2107c478bd9Sstevel@tonic-gate 				    (u_longlong_t)elfhdr.e_shoff));
2117c478bd9Sstevel@tonic-gate 				dprintf("e_shnentsize\t%d\n", (is64 ?
2127c478bd9Sstevel@tonic-gate 				    elfhdr64.e_shentsize : elfhdr.e_shentsize));
2137c478bd9Sstevel@tonic-gate 				dprintf("e_shnum\t\t%d\n", (is64 ?
2147c478bd9Sstevel@tonic-gate 				    elfhdr64.e_shnum : elfhdr.e_shnum));
2157c478bd9Sstevel@tonic-gate 				dprintf("e_shstrndx\t%d\n", (is64 ?
2167c478bd9Sstevel@tonic-gate 				    elfhdr64.e_shstrndx : elfhdr.e_shstrndx));
2177c478bd9Sstevel@tonic-gate 			}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #ifdef	_ELF64_SUPPORT
2217c478bd9Sstevel@tonic-gate 			dprintf("ELF file CLASS 0x%x 32 is %x 64 is %x\n",
2227c478bd9Sstevel@tonic-gate 			    elfhdr.e_ident[EI_CLASS], ELFCLASS32, ELFCLASS64);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 			if (elfhdr.e_ident[EI_CLASS] == ELFCLASS64) {
2257c478bd9Sstevel@tonic-gate 				elf64_go2 = read_elf64(fd, print,
2267c478bd9Sstevel@tonic-gate 				    (Elf64_Ehdr *)&elfhdr);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 				return ((elf64_go2 == FAIL_READELF64) ? FAIL :
2297c478bd9Sstevel@tonic-gate 				    (func_t)elf64_go2);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			} else
2327c478bd9Sstevel@tonic-gate #endif	/* _ELF64_SUPPORT */
2337c478bd9Sstevel@tonic-gate 				return (read_elf32(fd, print, &elfhdr));
2347c478bd9Sstevel@tonic-gate 		} else {
2357c478bd9Sstevel@tonic-gate 			printf("File not executable.\n");
2367c478bd9Sstevel@tonic-gate 			return (FAIL);
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 	return (FAIL);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
24353391bafSeota  * Macros to add attribute/values to the ELF bootstrap vector
24453391bafSeota  * and the aux vector. Use the type-cast to convert integers
24553391bafSeota  * to pointers first to suppress the gcc warning.
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate #define	AUX(p, a, v)	{ (p)->a_type = (a); \
24853391bafSeota 			((p)++)->a_un.a_val = (int32_t)(uintptr_t)(v); }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate #define	EBV(p, a, v)	{ (p)->eb_tag = (a); \
25153391bafSeota 			((p)++)->eb_un.eb_val = (Elf32_Word)(uintptr_t)(v); }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate static func_t
read_elf32(int fd,int print,Elf32_Ehdr * elfhdrp)2547c478bd9Sstevel@tonic-gate read_elf32(int fd, int print, Elf32_Ehdr *elfhdrp)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	Elf32_Phdr *phdr;	/* program header */
2577c478bd9Sstevel@tonic-gate 	Elf32_Nhdr *nhdr;	/* note header */
2587c478bd9Sstevel@tonic-gate 	int nphdrs, phdrsize;
2597c478bd9Sstevel@tonic-gate 	caddr_t allphdrs;
2607c478bd9Sstevel@tonic-gate 	caddr_t	namep, descp;
2617c478bd9Sstevel@tonic-gate 	Elf32_Addr loadaddr, base;
2627c478bd9Sstevel@tonic-gate 	size_t offset = 0;
2637c478bd9Sstevel@tonic-gate 	size_t size;
2647c478bd9Sstevel@tonic-gate 	uintptr_t off;
2657c478bd9Sstevel@tonic-gate 	int	i;
2667c478bd9Sstevel@tonic-gate 	int	bss_seen = 0;
2677c478bd9Sstevel@tonic-gate 	int interp = 0;				/* interpreter required */
2687c478bd9Sstevel@tonic-gate 	static char dlname[MAXPATHLEN];		/* name of interpeter */
2697c478bd9Sstevel@tonic-gate 	uint_t dynamic;				/* dynamic tags array */
2707c478bd9Sstevel@tonic-gate 	Elf32_Phdr *thdr;			/* "text" program header */
2717c478bd9Sstevel@tonic-gate 	Elf32_Phdr *dhdr;			/* "data" program header */
2727c478bd9Sstevel@tonic-gate 	func_t entrypt;				/* entry point of standalone */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* Initialize pointers so we won't free bogus ones on elferror */
2757c478bd9Sstevel@tonic-gate 	allphdrs = NULL;
2767c478bd9Sstevel@tonic-gate 	nhdr = NULL;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate #ifdef _ELF64_SUPPORT
2797c478bd9Sstevel@tonic-gate 	if (verbosemode)
2807c478bd9Sstevel@tonic-gate 		printf("Elf32 client\n");
2817c478bd9Sstevel@tonic-gate #endif	/* _ELF64_SUPPORT */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (elfhdrp->e_phnum == 0 || elfhdrp->e_phoff == 0)
2847c478bd9Sstevel@tonic-gate 		goto elferror;
2857c478bd9Sstevel@tonic-gate 
28653391bafSeota 	/* use uintptr_t to suppress the gcc warning */
28753391bafSeota 	entrypt = (func_t)(uintptr_t)elfhdrp->e_entry;
2887c478bd9Sstevel@tonic-gate 	if (verbosemode)
2897c478bd9Sstevel@tonic-gate 		dprintf("Entry point: %p\n", (void *)entrypt);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/*
2927c478bd9Sstevel@tonic-gate 	 * Allocate and read in all the program headers.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	nphdrs = elfhdrp->e_phnum;
2957c478bd9Sstevel@tonic-gate 	phdrsize = nphdrs * elfhdrp->e_phentsize;
2967c478bd9Sstevel@tonic-gate 	allphdrs = (caddr_t)kmem_alloc(phdrsize, 0);
2977c478bd9Sstevel@tonic-gate 	if (allphdrs == NULL)
2987c478bd9Sstevel@tonic-gate 		goto elferror;
2997c478bd9Sstevel@tonic-gate 	if (verbosemode)
3007c478bd9Sstevel@tonic-gate 		dprintf("lseek: args = %x %x %x\n", fd, elfhdrp->e_phoff, 0);
3017c478bd9Sstevel@tonic-gate 	if (lseek(fd, elfhdrp->e_phoff, 0) == -1)
3027c478bd9Sstevel@tonic-gate 		goto elferror;
3037c478bd9Sstevel@tonic-gate 	if (xread(fd, allphdrs, phdrsize) != phdrsize)
3047c478bd9Sstevel@tonic-gate 		goto elferror;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * First look for PT_NOTE headers that tell us what pagesize to
3087c478bd9Sstevel@tonic-gate 	 * use in allocating program memory.
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 	npagesize = 0;
3117c478bd9Sstevel@tonic-gate 	for (i = 0; i < nphdrs; i++) {
3127c478bd9Sstevel@tonic-gate 		void *note_buf;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		phdr = (Elf32_Phdr *)(allphdrs + elfhdrp->e_phentsize * i);
3157c478bd9Sstevel@tonic-gate 		if (phdr->p_type != PT_NOTE)
3167c478bd9Sstevel@tonic-gate 			continue;
3177c478bd9Sstevel@tonic-gate 		if (verbosemode) {
3187c478bd9Sstevel@tonic-gate 			dprintf("allocating 0x%x bytes for note hdr\n",
319986fd29aSsetje 			    phdr->p_filesz);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 		if ((note_buf = kmem_alloc(phdr->p_filesz, 0)) == NULL)
3227c478bd9Sstevel@tonic-gate 			goto elferror;
3237c478bd9Sstevel@tonic-gate 		if (verbosemode)
3247c478bd9Sstevel@tonic-gate 			dprintf("seeking to 0x%x\n", phdr->p_offset);
3257c478bd9Sstevel@tonic-gate 		if (lseek(fd, phdr->p_offset, 0) == -1)
3267c478bd9Sstevel@tonic-gate 			goto elferror;
3277c478bd9Sstevel@tonic-gate 		if (verbosemode) {
3287c478bd9Sstevel@tonic-gate 			dprintf("reading 0x%x bytes into %p\n",
329986fd29aSsetje 			    phdr->p_filesz, (void *)nhdr);
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		nhdr = (Elf32_Nhdr *)note_buf;
3327c478bd9Sstevel@tonic-gate 		if (xread(fd, (caddr_t)nhdr, phdr->p_filesz) != phdr->p_filesz)
3337c478bd9Sstevel@tonic-gate 			goto elferror;
3347c478bd9Sstevel@tonic-gate 		if (verbosemode) {
3357c478bd9Sstevel@tonic-gate 			dprintf("p_note namesz %x descsz %x type %x\n",
336986fd29aSsetje 			    nhdr->n_namesz, nhdr->n_descsz, nhdr->n_type);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		/*
3407c478bd9Sstevel@tonic-gate 		 * Iterate through all ELF PT_NOTE elements looking for
3417c478bd9Sstevel@tonic-gate 		 * ELF_NOTE_SOLARIS which, if present, will specify the
3427c478bd9Sstevel@tonic-gate 		 * executable's preferred pagesize.
3437c478bd9Sstevel@tonic-gate 		 */
3447c478bd9Sstevel@tonic-gate 		do {
3457c478bd9Sstevel@tonic-gate 			namep = (caddr_t)(nhdr + 1);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 			if (nhdr->n_namesz == strlen(ELF_NOTE_SOLARIS) + 1 &&
3487c478bd9Sstevel@tonic-gate 			    strcmp(namep, ELF_NOTE_SOLARIS) == 0 &&
3497c478bd9Sstevel@tonic-gate 			    nhdr->n_type == ELF_NOTE_PAGESIZE_HINT) {
3507c478bd9Sstevel@tonic-gate 				descp = namep + roundup(nhdr->n_namesz, 4);
3517c478bd9Sstevel@tonic-gate 				npagesize = *(int *)descp;
3527c478bd9Sstevel@tonic-gate 				if (verbosemode)
3537c478bd9Sstevel@tonic-gate 					dprintf("pagesize is %x\n", npagesize);
3547c478bd9Sstevel@tonic-gate 			}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 			offset += sizeof (Elf32_Nhdr) + roundup(nhdr->n_namesz,
3577c478bd9Sstevel@tonic-gate 			    4) + roundup(nhdr->n_descsz, 4);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 			nhdr = (Elf32_Nhdr *)((char *)note_buf + offset);
3607c478bd9Sstevel@tonic-gate 		} while (offset < phdr->p_filesz);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		kmem_free(note_buf, phdr->p_filesz);
3637c478bd9Sstevel@tonic-gate 		nhdr = NULL;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Next look for PT_LOAD headers to read in.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	if (print)
3707c478bd9Sstevel@tonic-gate 		printf("Size: ");
3717c478bd9Sstevel@tonic-gate 	for (i = 0; i < nphdrs; i++) {
3727c478bd9Sstevel@tonic-gate 		phdr = (Elf32_Phdr *)(allphdrs + elfhdrp->e_phentsize * i);
3737c478bd9Sstevel@tonic-gate 		if (verbosemode) {
3747c478bd9Sstevel@tonic-gate 			dprintf("Doing header 0x%x\n", i);
3757c478bd9Sstevel@tonic-gate 			dprintf("phdr\n");
3767c478bd9Sstevel@tonic-gate 			dprintf("\tp_offset = %x, p_vaddr = %x\n",
377986fd29aSsetje 			    phdr->p_offset, phdr->p_vaddr);
3787c478bd9Sstevel@tonic-gate 			dprintf("\tp_memsz = %x, p_filesz = %x\n",
379986fd29aSsetje 			    phdr->p_memsz, phdr->p_filesz);
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_LOAD) {
3827c478bd9Sstevel@tonic-gate 			if (verbosemode)
3837c478bd9Sstevel@tonic-gate 				dprintf("seeking to 0x%x\n", phdr->p_offset);
3847c478bd9Sstevel@tonic-gate 			if (lseek(fd, phdr->p_offset, 0) == -1)
3857c478bd9Sstevel@tonic-gate 				goto elferror;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 			if (phdr->p_flags == (PF_R | PF_W) &&
388986fd29aSsetje 			    phdr->p_vaddr == 0) {
3897c478bd9Sstevel@tonic-gate 				/*
3907c478bd9Sstevel@tonic-gate 				 * It's a PT_LOAD segment that is RW but
3917c478bd9Sstevel@tonic-gate 				 * not executable and has a vaddr
3927c478bd9Sstevel@tonic-gate 				 * of zero.  This is relocation info that
3937c478bd9Sstevel@tonic-gate 				 * doesn't need to stick around after
3947c478bd9Sstevel@tonic-gate 				 * krtld is done with it.  We allocate boot
3957c478bd9Sstevel@tonic-gate 				 * memory for this segment, since we don't want
3967c478bd9Sstevel@tonic-gate 				 * it mapped in permanently as part of
3977c478bd9Sstevel@tonic-gate 				 * the kernel image.
3987c478bd9Sstevel@tonic-gate 				 */
3997c478bd9Sstevel@tonic-gate 				if ((loadaddr = (uintptr_t)
400b531f6d1SToomas Soome 				    kmem_alloc(phdr->p_memsz, 0)) == 0)
4017c478bd9Sstevel@tonic-gate 					goto elferror;
4027c478bd9Sstevel@tonic-gate 				/*
4037c478bd9Sstevel@tonic-gate 				 * Save this to pass on
4047c478bd9Sstevel@tonic-gate 				 * to the interpreter.
4057c478bd9Sstevel@tonic-gate 				 */
4067c478bd9Sstevel@tonic-gate 				phdr->p_vaddr = (Elf32_Addr)loadaddr;
4077c478bd9Sstevel@tonic-gate 			} else {
4087c478bd9Sstevel@tonic-gate 				if (print)
4097c478bd9Sstevel@tonic-gate 					printf("0x%x+", phdr->p_filesz);
4107c478bd9Sstevel@tonic-gate 				/*
4117c478bd9Sstevel@tonic-gate 				 * If we found a new pagesize above, use it
4127c478bd9Sstevel@tonic-gate 				 * to adjust the memory allocation.
4137c478bd9Sstevel@tonic-gate 				 */
4147c478bd9Sstevel@tonic-gate 				loadaddr = phdr->p_vaddr;
4157c478bd9Sstevel@tonic-gate 				if (use_align && npagesize != 0) {
4167c478bd9Sstevel@tonic-gate 					off = loadaddr & (npagesize - 1);
4177c478bd9Sstevel@tonic-gate 					size = roundup(phdr->p_memsz + off,
418986fd29aSsetje 					    npagesize);
4197c478bd9Sstevel@tonic-gate 					base = loadaddr - off;
4207c478bd9Sstevel@tonic-gate 				} else {
4217c478bd9Sstevel@tonic-gate 					npagesize = 0;
4227c478bd9Sstevel@tonic-gate 					size = phdr->p_memsz;
4237c478bd9Sstevel@tonic-gate 					base = loadaddr;
4247c478bd9Sstevel@tonic-gate 				}
4257c478bd9Sstevel@tonic-gate 				/*
4267c478bd9Sstevel@tonic-gate 				 *  Check if it's text or data.
4277c478bd9Sstevel@tonic-gate 				 */
4287c478bd9Sstevel@tonic-gate 				if (phdr->p_flags & PF_W)
4297c478bd9Sstevel@tonic-gate 					dhdr = phdr;
4307c478bd9Sstevel@tonic-gate 				else
4317c478bd9Sstevel@tonic-gate 					thdr = phdr;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 				/*
4347c478bd9Sstevel@tonic-gate 				 * If memory size is zero just ignore this
4357c478bd9Sstevel@tonic-gate 				 * header.
4367c478bd9Sstevel@tonic-gate 				 */
4377c478bd9Sstevel@tonic-gate 				if (size == 0)
4387c478bd9Sstevel@tonic-gate 					continue;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 				if (verbosemode)
4417c478bd9Sstevel@tonic-gate 					dprintf("allocating memory: %x %lx "
4427c478bd9Sstevel@tonic-gate 					    "%x\n", base, size, npagesize);
4437c478bd9Sstevel@tonic-gate 				/*
4447c478bd9Sstevel@tonic-gate 				 * We're all set up to read.
4457c478bd9Sstevel@tonic-gate 				 * Now let's allocate some memory.
4467c478bd9Sstevel@tonic-gate 				 */
4477c478bd9Sstevel@tonic-gate 
44853391bafSeota 				/* use uintptr_t to suppress the gcc warning */
44953391bafSeota 				if (get_progmemory((caddr_t)(uintptr_t)base,
45053391bafSeota 				    size, npagesize))
4517c478bd9Sstevel@tonic-gate 					goto elferror;
4527c478bd9Sstevel@tonic-gate 			}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 			if (verbosemode) {
4557c478bd9Sstevel@tonic-gate 				dprintf("reading 0x%x bytes into 0x%x\n",
456986fd29aSsetje 				    phdr->p_filesz, loadaddr);
4577c478bd9Sstevel@tonic-gate 			}
45853391bafSeota 			/* use uintptr_t to suppress the gcc warning */
45953391bafSeota 			if (xread(fd, (caddr_t)(uintptr_t)loadaddr,
46053391bafSeota 			    phdr->p_filesz) != phdr->p_filesz)
4617c478bd9Sstevel@tonic-gate 				goto elferror;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 			/* zero out BSS */
4647c478bd9Sstevel@tonic-gate 			if (phdr->p_memsz > phdr->p_filesz) {
4657c478bd9Sstevel@tonic-gate 				loadaddr += phdr->p_filesz;
4667c478bd9Sstevel@tonic-gate 				if (verbosemode) {
4677c478bd9Sstevel@tonic-gate 					dprintf("bss from 0x%x size 0x%x\n",
4687c478bd9Sstevel@tonic-gate 					    loadaddr,
4697c478bd9Sstevel@tonic-gate 					    phdr->p_memsz - phdr->p_filesz);
4707c478bd9Sstevel@tonic-gate 				}
47153391bafSeota 				/* use uintptr_t to suppress the gcc warning */
47253391bafSeota 				bzero((void *)(uintptr_t)loadaddr,
4737c478bd9Sstevel@tonic-gate 				    phdr->p_memsz - phdr->p_filesz);
4747c478bd9Sstevel@tonic-gate 				bss_seen++;
4757c478bd9Sstevel@tonic-gate 				if (print)
4767c478bd9Sstevel@tonic-gate 					printf("0x%x Bytes\n",
4777c478bd9Sstevel@tonic-gate 					    phdr->p_memsz - phdr->p_filesz);
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 			/* force instructions to be visible to icache */
48153391bafSeota 			if (phdr->p_flags & PF_X) {
48253391bafSeota 				sync_instruction_memory(
48353391bafSeota 				    (caddr_t)(uintptr_t)phdr->p_vaddr,
4847c478bd9Sstevel@tonic-gate 				    phdr->p_memsz);
48553391bafSeota 			}
4867c478bd9Sstevel@tonic-gate 		} else if (phdr->p_type == PT_INTERP) {
4877c478bd9Sstevel@tonic-gate 			/*
4887c478bd9Sstevel@tonic-gate 			 * Dynamically-linked executable.
4897c478bd9Sstevel@tonic-gate 			 */
4907c478bd9Sstevel@tonic-gate 			interp = 1;
4917c478bd9Sstevel@tonic-gate 			if (lseek(fd, phdr->p_offset, 0) == -1) {
4927c478bd9Sstevel@tonic-gate 				goto elferror;
4937c478bd9Sstevel@tonic-gate 			}
4947c478bd9Sstevel@tonic-gate 			/*
4957c478bd9Sstevel@tonic-gate 			 * Get the name of the interpreter.
4967c478bd9Sstevel@tonic-gate 			 */
4977c478bd9Sstevel@tonic-gate 			if (xread(fd, dlname, phdr->p_filesz) !=
4987c478bd9Sstevel@tonic-gate 			    phdr->p_filesz ||
4997c478bd9Sstevel@tonic-gate 			    dlname[phdr->p_filesz - 1] != '\0')
5007c478bd9Sstevel@tonic-gate 				goto elferror;
5017c478bd9Sstevel@tonic-gate 		} else if (phdr->p_type == PT_DYNAMIC) {
5027c478bd9Sstevel@tonic-gate 			dynamic = phdr->p_vaddr;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (!bss_seen && print)
5077c478bd9Sstevel@tonic-gate 		printf("0 Bytes\n");
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Load the interpreter
5117c478bd9Sstevel@tonic-gate 	 * if there is one.
5127c478bd9Sstevel@tonic-gate 	 */
5137c478bd9Sstevel@tonic-gate 	if (interp) {
5147c478bd9Sstevel@tonic-gate 		Elf32_Boot bootv[EB_MAX];		/* Bootstrap vector */
5157c478bd9Sstevel@tonic-gate 		auxv32_t auxv[__BOOT_NAUXV_IMPL];	/* Aux vector */
5167c478bd9Sstevel@tonic-gate 		Elf32_Boot *bv = bootv;
5177c478bd9Sstevel@tonic-gate 		auxv32_t *av = auxv;
5187c478bd9Sstevel@tonic-gate 		size_t vsize;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		/*
5217c478bd9Sstevel@tonic-gate 		 * Load it.
5227c478bd9Sstevel@tonic-gate 		 */
5237c478bd9Sstevel@tonic-gate 		if ((entrypt = iload32(dlname, thdr, dhdr, &av)) == FAIL)
5247c478bd9Sstevel@tonic-gate 			goto elferror;
5257c478bd9Sstevel@tonic-gate 		/*
5267c478bd9Sstevel@tonic-gate 		 * Build bootstrap and aux vectors.
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 		setup_aux();
5297c478bd9Sstevel@tonic-gate 		EBV(bv, EB_AUXV, 0); /* fill in later */
5307c478bd9Sstevel@tonic-gate 		EBV(bv, EB_PAGESIZE, pagesize);
5317c478bd9Sstevel@tonic-gate 		EBV(bv, EB_DYNAMIC, dynamic);
5327c478bd9Sstevel@tonic-gate 		EBV(bv, EB_NULL, 0);
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		AUX(av, AT_BASE, entrypt);
5357c478bd9Sstevel@tonic-gate 		AUX(av, AT_ENTRY, elfhdrp->e_entry);
5367c478bd9Sstevel@tonic-gate 		AUX(av, AT_PAGESZ, pagesize);
5377c478bd9Sstevel@tonic-gate 		AUX(av, AT_PHDR, allphdrs);
5387c478bd9Sstevel@tonic-gate 		AUX(av, AT_PHNUM, elfhdrp->e_phnum);
5397c478bd9Sstevel@tonic-gate 		AUX(av, AT_PHENT, elfhdrp->e_phentsize);
5407c478bd9Sstevel@tonic-gate 		if (use_align)
5417c478bd9Sstevel@tonic-gate 			AUX(av, AT_SUN_LPAGESZ, npagesize);
5427c478bd9Sstevel@tonic-gate 		AUX(av, AT_SUN_IFLUSH, icache_flush);
5437c478bd9Sstevel@tonic-gate 		if (cpulist != NULL)
5447c478bd9Sstevel@tonic-gate 			AUX(av, AT_SUN_CPU, cpulist);
5457c478bd9Sstevel@tonic-gate 		if (mmulist != NULL)
5467c478bd9Sstevel@tonic-gate 			AUX(av, AT_SUN_MMU, mmulist);
5477c478bd9Sstevel@tonic-gate 		AUX(av, AT_NULL, 0);
5487c478bd9Sstevel@tonic-gate 		/*
5497c478bd9Sstevel@tonic-gate 		 * Realloc vectors and copy them.
5507c478bd9Sstevel@tonic-gate 		 */
5517c478bd9Sstevel@tonic-gate 		vsize = (caddr_t)bv - (caddr_t)bootv;
5527c478bd9Sstevel@tonic-gate 		if ((elfbootvec = (Elf32_Boot *)kmem_alloc(vsize, 0)) == NULL)
5537c478bd9Sstevel@tonic-gate 			goto elferror;
5547c478bd9Sstevel@tonic-gate 		bcopy((char *)bootv, (char *)elfbootvec, vsize);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		size = (caddr_t)av - (caddr_t)auxv;
5577c478bd9Sstevel@tonic-gate 		if (size > sizeof (auxv)) {
5587c478bd9Sstevel@tonic-gate 			printf("readelf: overrun of available aux vectors\n");
5597c478bd9Sstevel@tonic-gate 			kmem_free(elfbootvec, vsize);
5607c478bd9Sstevel@tonic-gate 			goto elferror;
5617c478bd9Sstevel@tonic-gate 		}
56253391bafSeota 		/* use uintptr_t to suppress the gcc warning */
5637c478bd9Sstevel@tonic-gate 		if ((elfbootvec->eb_un.eb_ptr =
564b531f6d1SToomas Soome 		    (Elf32_Addr)(uintptr_t)kmem_alloc(size, 0)) == 0) {
5657c478bd9Sstevel@tonic-gate 			kmem_free(elfbootvec, vsize);
5667c478bd9Sstevel@tonic-gate 			goto elferror;
5677c478bd9Sstevel@tonic-gate 		}
56853391bafSeota 		/* use uintptr_t to suppress the gcc warning */
56953391bafSeota 		bcopy(auxv,
57053391bafSeota 		    (void *)(uintptr_t)(elfbootvec->eb_un.eb_ptr), size);
5717c478bd9Sstevel@tonic-gate 
572622d9a34SRichard Lowe #if defined(_ELF64_SUPPORT)
5737c478bd9Sstevel@tonic-gate 		/*
5747c478bd9Sstevel@tonic-gate 		 * Make an LP64 copy of the vector for use by 64-bit standalones
5757c478bd9Sstevel@tonic-gate 		 * even if they have ELF32.
5767c478bd9Sstevel@tonic-gate 		 */
5777c478bd9Sstevel@tonic-gate 		if ((elfbootvecELF32_64 = (Elf32_Boot *)kmem_alloc(vsize, 0))
5787c478bd9Sstevel@tonic-gate 		    == NULL)
5797c478bd9Sstevel@tonic-gate 			goto elferror;
5807c478bd9Sstevel@tonic-gate 		bcopy(bootv, elfbootvecELF32_64, vsize);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		size = (av - auxv) * sizeof (auxv64_t);
58353391bafSeota 		/* use uintptr_t to suppress the gcc warning */
5847c478bd9Sstevel@tonic-gate 		if ((elfbootvecELF32_64->eb_un.eb_ptr =
585b531f6d1SToomas Soome 		    (Elf32_Addr)(uintptr_t)kmem_alloc(size, 0)) == 0) {
5867c478bd9Sstevel@tonic-gate 			kmem_free(elfbootvecELF32_64, vsize);
5877c478bd9Sstevel@tonic-gate 			goto elferror;
5887c478bd9Sstevel@tonic-gate 		} else {
5897c478bd9Sstevel@tonic-gate 			auxv64_t *a64 =
59053391bafSeota 			    (auxv64_t *)(uintptr_t)
59153391bafSeota 			    elfbootvecELF32_64->eb_un.eb_ptr;
5927c478bd9Sstevel@tonic-gate 			auxv32_t *a = auxv;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 			for (a = auxv; a < av; a++) {
5957c478bd9Sstevel@tonic-gate 				a64->a_type = a->a_type;
5967c478bd9Sstevel@tonic-gate 				a64->a_un.a_val = a->a_un.a_val;
5977c478bd9Sstevel@tonic-gate 				a64++;
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 		}
600622d9a34SRichard Lowe #endif	/* _ELF64_SUPPORT */
6017c478bd9Sstevel@tonic-gate 	} else {
6027c478bd9Sstevel@tonic-gate 		kmem_free(allphdrs, phdrsize);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	return (entrypt);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate elferror:
6077c478bd9Sstevel@tonic-gate 	if (allphdrs != NULL)
6087c478bd9Sstevel@tonic-gate 		kmem_free(allphdrs, phdrsize);
6097c478bd9Sstevel@tonic-gate 	if (nhdr != NULL)
6107c478bd9Sstevel@tonic-gate 		kmem_free(nhdr, phdr->p_filesz);
6117c478bd9Sstevel@tonic-gate 	printf("Elf32 read error.\n");
6127c478bd9Sstevel@tonic-gate 	return (FAIL);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate #ifdef	_ELF64_SUPPORT
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * Macros to add attribute/values to the ELF bootstrap vector
6187c478bd9Sstevel@tonic-gate  * and the aux vector.
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate #define	AUX64(p, a, v)	{ (p)->a_type = (a); \
6217c478bd9Sstevel@tonic-gate 			((p)++)->a_un.a_val = (uint64_t)(v); }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate #define	EBV64(p, a, v)	{ (p)->eb_tag = (a); \
6247c478bd9Sstevel@tonic-gate 			((p)++)->eb_un.eb_val = (Elf64_Xword)(v); }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate static uint64_t
read_elf64(int fd,int print,Elf64_Ehdr * elfhdrp)6277c478bd9Sstevel@tonic-gate read_elf64(int fd, int print, Elf64_Ehdr *elfhdrp)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	Elf64_Phdr *phdr;	/* program header */
6307c478bd9Sstevel@tonic-gate 	Elf64_Nhdr *nhdr;	/* note header */
6317c478bd9Sstevel@tonic-gate 	int nphdrs, phdrsize;
6327c478bd9Sstevel@tonic-gate 	caddr_t allphdrs;
6337c478bd9Sstevel@tonic-gate 	caddr_t	namep, descp;
6347c478bd9Sstevel@tonic-gate 	Elf64_Addr loadaddr, base;
6357c478bd9Sstevel@tonic-gate 	size_t offset = 0;
6367c478bd9Sstevel@tonic-gate 	size_t size;
6377c478bd9Sstevel@tonic-gate 	int i;
6387c478bd9Sstevel@tonic-gate 	uintptr_t	off;
6397c478bd9Sstevel@tonic-gate 	int bss_seen = 0;
6407c478bd9Sstevel@tonic-gate 	int interp = 0;				/* interpreter required */
6417c478bd9Sstevel@tonic-gate 	static char dlname[MAXPATHLEN];		/* name of interpeter */
6427c478bd9Sstevel@tonic-gate 	uintptr_t dynamic;			/* dynamic tags array */
6437c478bd9Sstevel@tonic-gate 	Elf64_Phdr *thdr;			/* "text" program header */
6447c478bd9Sstevel@tonic-gate 	Elf64_Phdr *dhdr;			/* "data" program header */
6457c478bd9Sstevel@tonic-gate 	Elf64_Addr entrypt;			/* entry point of standalone */
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/* Initialize pointers so we won't free bogus ones on elf64error */
6487c478bd9Sstevel@tonic-gate 	allphdrs = NULL;
6497c478bd9Sstevel@tonic-gate 	nhdr = NULL;
650c2e7b48dSkalai #if defined(__sparcv9)
6517c478bd9Sstevel@tonic-gate 	client_isLP64 = 1;
652c2e7b48dSkalai #endif	/* __sparcv9 */
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	if (verbosemode)
6557c478bd9Sstevel@tonic-gate 		printf("Elf64 client\n");
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (elfhdrp->e_phnum == 0 || elfhdrp->e_phoff == 0)
6587c478bd9Sstevel@tonic-gate 		goto elf64error;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	entrypt = elfhdrp->e_entry;
6617c478bd9Sstevel@tonic-gate 	if (verbosemode)
6627c478bd9Sstevel@tonic-gate 		dprintf("Entry point: 0x%llx\n", (u_longlong_t)entrypt);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/*
6657c478bd9Sstevel@tonic-gate 	 * Allocate and read in all the program headers.
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	nphdrs = elfhdrp->e_phnum;
6687c478bd9Sstevel@tonic-gate 	phdrsize = nphdrs * elfhdrp->e_phentsize;
6697c478bd9Sstevel@tonic-gate 	allphdrs = (caddr_t)kmem_alloc(phdrsize, 0);
6707c478bd9Sstevel@tonic-gate 	if (allphdrs == NULL)
6717c478bd9Sstevel@tonic-gate 		goto elf64error;
6727c478bd9Sstevel@tonic-gate 	if (verbosemode)
6737c478bd9Sstevel@tonic-gate 		dprintf("lseek: args = %x %llx %x\n", fd,
6747c478bd9Sstevel@tonic-gate 		    (u_longlong_t)elfhdrp->e_phoff, 0);
6757c478bd9Sstevel@tonic-gate 	if (lseek(fd, elfhdrp->e_phoff, 0) == -1)
6767c478bd9Sstevel@tonic-gate 		goto elf64error;
6777c478bd9Sstevel@tonic-gate 	if (xread(fd, allphdrs, phdrsize) != phdrsize)
6787c478bd9Sstevel@tonic-gate 		goto elf64error;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * First look for PT_NOTE headers that tell us what pagesize to
6827c478bd9Sstevel@tonic-gate 	 * use in allocating program memory.
6837c478bd9Sstevel@tonic-gate 	 */
6847c478bd9Sstevel@tonic-gate 	npagesize = 0;
6857c478bd9Sstevel@tonic-gate 	for (i = 0; i < nphdrs; i++) {
6867c478bd9Sstevel@tonic-gate 		void *note_buf;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		phdr = (Elf64_Phdr *)(allphdrs + elfhdrp->e_phentsize * i);
6897c478bd9Sstevel@tonic-gate 		if (phdr->p_type != PT_NOTE)
6907c478bd9Sstevel@tonic-gate 			continue;
6917c478bd9Sstevel@tonic-gate 		if (verbosemode) {
6927c478bd9Sstevel@tonic-gate 			dprintf("allocating 0x%llx bytes for note hdr\n",
693986fd29aSsetje 			    (u_longlong_t)phdr->p_filesz);
6947c478bd9Sstevel@tonic-gate 		}
6957c478bd9Sstevel@tonic-gate 		if ((note_buf = kmem_alloc(phdr->p_filesz, 0)) == NULL)
6967c478bd9Sstevel@tonic-gate 			goto elf64error;
6977c478bd9Sstevel@tonic-gate 		if (verbosemode)
6987c478bd9Sstevel@tonic-gate 			dprintf("seeking to 0x%llx\n",
6997c478bd9Sstevel@tonic-gate 			    (u_longlong_t)phdr->p_offset);
7007c478bd9Sstevel@tonic-gate 		if (lseek(fd, phdr->p_offset, 0) == -1)
7017c478bd9Sstevel@tonic-gate 			goto elf64error;
7027c478bd9Sstevel@tonic-gate 		if (verbosemode) {
7037c478bd9Sstevel@tonic-gate 			dprintf("reading 0x%llx bytes into 0x%p\n",
704986fd29aSsetje 			    (u_longlong_t)phdr->p_filesz, (void *)nhdr);
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 		nhdr = (Elf64_Nhdr *)note_buf;
7077c478bd9Sstevel@tonic-gate 		if (xread(fd, (caddr_t)nhdr, phdr->p_filesz) != phdr->p_filesz)
7087c478bd9Sstevel@tonic-gate 			goto elf64error;
7097c478bd9Sstevel@tonic-gate 		if (verbosemode) {
7107c478bd9Sstevel@tonic-gate 			dprintf("p_note namesz %x descsz %x type %x\n",
711986fd29aSsetje 			    nhdr->n_namesz, nhdr->n_descsz, nhdr->n_type);
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		/*
7157c478bd9Sstevel@tonic-gate 		 * Iterate through all ELF PT_NOTE elements looking for
7167c478bd9Sstevel@tonic-gate 		 * ELF_NOTE_SOLARIS which, if present, will specify the
7177c478bd9Sstevel@tonic-gate 		 * executable's preferred pagesize.
7187c478bd9Sstevel@tonic-gate 		 */
7197c478bd9Sstevel@tonic-gate 		do {
7207c478bd9Sstevel@tonic-gate 			namep = (caddr_t)(nhdr + 1);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 			if (nhdr->n_namesz == strlen(ELF_NOTE_SOLARIS) + 1 &&
7237c478bd9Sstevel@tonic-gate 			    strcmp(namep, ELF_NOTE_SOLARIS) == 0 &&
7247c478bd9Sstevel@tonic-gate 			    nhdr->n_type == ELF_NOTE_PAGESIZE_HINT) {
7257c478bd9Sstevel@tonic-gate 				descp = namep + roundup(nhdr->n_namesz, 4);
7267c478bd9Sstevel@tonic-gate 				npagesize = *(int *)descp;
7277c478bd9Sstevel@tonic-gate 				if (verbosemode)
7287c478bd9Sstevel@tonic-gate 					dprintf("pagesize is %x\n", npagesize);
7297c478bd9Sstevel@tonic-gate 			}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 			offset += sizeof (Elf64_Nhdr) + roundup(nhdr->n_namesz,
7327c478bd9Sstevel@tonic-gate 			    4) + roundup(nhdr->n_descsz, 4);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 			nhdr = (Elf64_Nhdr *)((char *)note_buf + offset);
7357c478bd9Sstevel@tonic-gate 		} while (offset < phdr->p_filesz);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		kmem_free(note_buf, phdr->p_filesz);
7387c478bd9Sstevel@tonic-gate 		nhdr = NULL;
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/*
7427c478bd9Sstevel@tonic-gate 	 * Next look for PT_LOAD headers to read in.
7437c478bd9Sstevel@tonic-gate 	 */
7447c478bd9Sstevel@tonic-gate 	if (print)
7457c478bd9Sstevel@tonic-gate 		printf("Size: ");
7467c478bd9Sstevel@tonic-gate 	for (i = 0; i < nphdrs; i++) {
7477c478bd9Sstevel@tonic-gate 		phdr = (Elf64_Phdr *)(allphdrs + elfhdrp->e_phentsize * i);
7487c478bd9Sstevel@tonic-gate 		if (verbosemode) {
7497c478bd9Sstevel@tonic-gate 			dprintf("Doing header 0x%x\n", i);
7507c478bd9Sstevel@tonic-gate 			dprintf("phdr\n");
7517c478bd9Sstevel@tonic-gate 			dprintf("\tp_offset = %llx, p_vaddr = %llx\n",
752986fd29aSsetje 			    (u_longlong_t)phdr->p_offset,
753986fd29aSsetje 			    (u_longlong_t)phdr->p_vaddr);
7547c478bd9Sstevel@tonic-gate 			dprintf("\tp_memsz = %llx, p_filesz = %llx\n",
755986fd29aSsetje 			    (u_longlong_t)phdr->p_memsz,
756986fd29aSsetje 			    (u_longlong_t)phdr->p_filesz);
7577c478bd9Sstevel@tonic-gate 			dprintf("\tp_type = %x, p_flags = %x\n",
758986fd29aSsetje 			    phdr->p_type, phdr->p_flags);
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_LOAD) {
7617c478bd9Sstevel@tonic-gate 			if (verbosemode)
7627c478bd9Sstevel@tonic-gate 				dprintf("seeking to 0x%llx\n",
7637c478bd9Sstevel@tonic-gate 				    (u_longlong_t)phdr->p_offset);
7647c478bd9Sstevel@tonic-gate 			if (lseek(fd, phdr->p_offset, 0) == -1)
7657c478bd9Sstevel@tonic-gate 				goto elf64error;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 			if (phdr->p_flags == (PF_R | PF_W) &&
768986fd29aSsetje 			    phdr->p_vaddr == 0) {
7697c478bd9Sstevel@tonic-gate 				/*
7707c478bd9Sstevel@tonic-gate 				 * It's a PT_LOAD segment that is RW but
7717c478bd9Sstevel@tonic-gate 				 * not executable and has a vaddr
7727c478bd9Sstevel@tonic-gate 				 * of zero.  This is relocation info that
7737c478bd9Sstevel@tonic-gate 				 * doesn't need to stick around after
7747c478bd9Sstevel@tonic-gate 				 * krtld is done with it.  We allocate boot
7757c478bd9Sstevel@tonic-gate 				 * memory for this segment, since we don't want
7767c478bd9Sstevel@tonic-gate 				 * it mapped in permanently as part of
7777c478bd9Sstevel@tonic-gate 				 * the kernel image.
7787c478bd9Sstevel@tonic-gate 				 */
7797c478bd9Sstevel@tonic-gate 				if ((loadaddr = (Elf64_Addr)(uintptr_t)
780b531f6d1SToomas Soome 				    kmem_alloc(phdr->p_memsz, 0)) == 0)
7817c478bd9Sstevel@tonic-gate 					goto elf64error;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 				/*
7847c478bd9Sstevel@tonic-gate 				 * Save this to pass on
7857c478bd9Sstevel@tonic-gate 				 * to the interpreter.
7867c478bd9Sstevel@tonic-gate 				 */
7877c478bd9Sstevel@tonic-gate 				phdr->p_vaddr = loadaddr;
7887c478bd9Sstevel@tonic-gate 			} else {
7897c478bd9Sstevel@tonic-gate 				if (print)
7907c478bd9Sstevel@tonic-gate 					printf("0x%llx+",
7917c478bd9Sstevel@tonic-gate 					    (u_longlong_t)phdr->p_filesz);
7927c478bd9Sstevel@tonic-gate 				/*
7937c478bd9Sstevel@tonic-gate 				 * If we found a new pagesize above, use it
7947c478bd9Sstevel@tonic-gate 				 * to adjust the memory allocation.
7957c478bd9Sstevel@tonic-gate 				 */
7967c478bd9Sstevel@tonic-gate 				loadaddr = phdr->p_vaddr;
7977c478bd9Sstevel@tonic-gate 				if (use_align && npagesize != 0) {
7987c478bd9Sstevel@tonic-gate 					off = loadaddr & (npagesize - 1);
7997c478bd9Sstevel@tonic-gate 					size = roundup(phdr->p_memsz + off,
800986fd29aSsetje 					    npagesize);
8017c478bd9Sstevel@tonic-gate 					base = loadaddr - off;
8027c478bd9Sstevel@tonic-gate 				} else {
8037c478bd9Sstevel@tonic-gate 					npagesize = 0;
8047c478bd9Sstevel@tonic-gate 					size = phdr->p_memsz;
8057c478bd9Sstevel@tonic-gate 					base = loadaddr;
8067c478bd9Sstevel@tonic-gate 				}
8077c478bd9Sstevel@tonic-gate 				/*
8087c478bd9Sstevel@tonic-gate 				 *  Check if it's text or data.
8097c478bd9Sstevel@tonic-gate 				 */
8107c478bd9Sstevel@tonic-gate 				if (phdr->p_flags & PF_W)
8117c478bd9Sstevel@tonic-gate 					dhdr = phdr;
8127c478bd9Sstevel@tonic-gate 				else
8137c478bd9Sstevel@tonic-gate 					thdr = phdr;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 				if (verbosemode)
8167c478bd9Sstevel@tonic-gate 					dprintf(
8177c478bd9Sstevel@tonic-gate 					    "allocating memory: %llx %lx %x\n",
8187c478bd9Sstevel@tonic-gate 					    (u_longlong_t)base,
8197c478bd9Sstevel@tonic-gate 					    size, npagesize);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 				/*
8227c478bd9Sstevel@tonic-gate 				 * If memory size is zero just ignore this
8237c478bd9Sstevel@tonic-gate 				 * header.
8247c478bd9Sstevel@tonic-gate 				 */
8257c478bd9Sstevel@tonic-gate 				if (size == 0)
8267c478bd9Sstevel@tonic-gate 					continue;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 				/*
8297c478bd9Sstevel@tonic-gate 				 * We're all set up to read.
8307c478bd9Sstevel@tonic-gate 				 * Now let's allocate some memory.
8317c478bd9Sstevel@tonic-gate 				 */
832bcbe9155Sjg 				if (get_progmemory((caddr_t)(uintptr_t)base,
833bcbe9155Sjg 				    size, npagesize))
8347c478bd9Sstevel@tonic-gate 					goto elf64error;
8357c478bd9Sstevel@tonic-gate 			}
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 			if (verbosemode) {
8387c478bd9Sstevel@tonic-gate 				dprintf("reading 0x%llx bytes into 0x%llx\n",
839986fd29aSsetje 				    (u_longlong_t)phdr->p_filesz,
840986fd29aSsetje 				    (u_longlong_t)loadaddr);
8417c478bd9Sstevel@tonic-gate 			}
842bcbe9155Sjg 			if (xread(fd, (caddr_t)(uintptr_t)
843bcbe9155Sjg 			    loadaddr, phdr->p_filesz) != phdr->p_filesz)
8447c478bd9Sstevel@tonic-gate 				goto elf64error;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 			/* zero out BSS */
8477c478bd9Sstevel@tonic-gate 			if (phdr->p_memsz > phdr->p_filesz) {
8487c478bd9Sstevel@tonic-gate 				loadaddr += phdr->p_filesz;
8497c478bd9Sstevel@tonic-gate 				if (verbosemode) {
8507c478bd9Sstevel@tonic-gate 					dprintf("bss from 0x%llx size 0x%llx\n",
8517c478bd9Sstevel@tonic-gate 					    (u_longlong_t)loadaddr,
8527c478bd9Sstevel@tonic-gate 					    (u_longlong_t)(phdr->p_memsz -
8537c478bd9Sstevel@tonic-gate 					    phdr->p_filesz));
8547c478bd9Sstevel@tonic-gate 				}
8557c478bd9Sstevel@tonic-gate 
856bcbe9155Sjg 				bzero((caddr_t)(uintptr_t)loadaddr,
8577c478bd9Sstevel@tonic-gate 				    phdr->p_memsz - phdr->p_filesz);
8587c478bd9Sstevel@tonic-gate 				bss_seen++;
8597c478bd9Sstevel@tonic-gate 				if (print)
8607c478bd9Sstevel@tonic-gate 					printf("0x%llx Bytes\n",
8617c478bd9Sstevel@tonic-gate 					    (u_longlong_t)(phdr->p_memsz -
8627c478bd9Sstevel@tonic-gate 					    phdr->p_filesz));
8637c478bd9Sstevel@tonic-gate 			}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 			/* force instructions to be visible to icache */
8667c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_X)
867bcbe9155Sjg 				sync_instruction_memory((caddr_t)(uintptr_t)
868bcbe9155Sjg 				    phdr->p_vaddr, phdr->p_memsz);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		} else if (phdr->p_type == PT_INTERP) {
8717c478bd9Sstevel@tonic-gate 			/*
8727c478bd9Sstevel@tonic-gate 			 * Dynamically-linked executable.
8737c478bd9Sstevel@tonic-gate 			 */
8747c478bd9Sstevel@tonic-gate 			interp = 1;
8757c478bd9Sstevel@tonic-gate 			if (lseek(fd, phdr->p_offset, 0) == -1) {
8767c478bd9Sstevel@tonic-gate 				goto elf64error;
8777c478bd9Sstevel@tonic-gate 			}
8787c478bd9Sstevel@tonic-gate 			/*
8797c478bd9Sstevel@tonic-gate 			 * Get the name of the interpreter.
8807c478bd9Sstevel@tonic-gate 			 */
8817c478bd9Sstevel@tonic-gate 			if (xread(fd, dlname, phdr->p_filesz) !=
8827c478bd9Sstevel@tonic-gate 			    phdr->p_filesz ||
8837c478bd9Sstevel@tonic-gate 			    dlname[phdr->p_filesz - 1] != '\0')
8847c478bd9Sstevel@tonic-gate 				goto elf64error;
8857c478bd9Sstevel@tonic-gate 		} else if (phdr->p_type == PT_DYNAMIC) {
8867c478bd9Sstevel@tonic-gate 			dynamic = phdr->p_vaddr;
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (!bss_seen && print)
8917c478bd9Sstevel@tonic-gate 		printf("0 Bytes\n");
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * Load the interpreter
8957c478bd9Sstevel@tonic-gate 	 * if there is one.
8967c478bd9Sstevel@tonic-gate 	 */
8977c478bd9Sstevel@tonic-gate 	if (interp) {
8987c478bd9Sstevel@tonic-gate 		Elf64_Boot bootv[EB_MAX];		/* Bootstrap vector */
8997c478bd9Sstevel@tonic-gate 		auxv64_t auxv[__BOOT_NAUXV_IMPL];	/* Aux vector */
9007c478bd9Sstevel@tonic-gate 		Elf64_Boot *bv = bootv;
9017c478bd9Sstevel@tonic-gate 		auxv64_t *av = auxv;
9027c478bd9Sstevel@tonic-gate 		size_t vsize;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		/*
9057c478bd9Sstevel@tonic-gate 		 * Load it.
9067c478bd9Sstevel@tonic-gate 		 */
9077c478bd9Sstevel@tonic-gate 		if ((entrypt = iload64(dlname, thdr, dhdr, &av)) ==
9087c478bd9Sstevel@tonic-gate 		    FAIL_ILOAD64)
9097c478bd9Sstevel@tonic-gate 			goto elf64error;
9107c478bd9Sstevel@tonic-gate 		/*
9117c478bd9Sstevel@tonic-gate 		 * Build bootstrap and aux vectors.
9127c478bd9Sstevel@tonic-gate 		 */
9137c478bd9Sstevel@tonic-gate 		setup_aux();
9147c478bd9Sstevel@tonic-gate 		EBV64(bv, EB_AUXV, 0); /* fill in later */
9157c478bd9Sstevel@tonic-gate 		EBV64(bv, EB_PAGESIZE, pagesize);
9167c478bd9Sstevel@tonic-gate 		EBV64(bv, EB_DYNAMIC, dynamic);
9177c478bd9Sstevel@tonic-gate 		EBV64(bv, EB_NULL, 0);
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		AUX64(av, AT_BASE, entrypt);
9207c478bd9Sstevel@tonic-gate 		AUX64(av, AT_ENTRY, elfhdrp->e_entry);
9217c478bd9Sstevel@tonic-gate 		AUX64(av, AT_PAGESZ, pagesize);
922bcbe9155Sjg 		AUX64(av, AT_PHDR, (uintptr_t)allphdrs);
9237c478bd9Sstevel@tonic-gate 		AUX64(av, AT_PHNUM, elfhdrp->e_phnum);
9247c478bd9Sstevel@tonic-gate 		AUX64(av, AT_PHENT, elfhdrp->e_phentsize);
9257c478bd9Sstevel@tonic-gate 		if (npagesize)
9267c478bd9Sstevel@tonic-gate 			AUX64(av, AT_SUN_LPAGESZ, npagesize);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		AUX64(av, AT_SUN_IFLUSH, icache_flush);
9297c478bd9Sstevel@tonic-gate 		if (cpulist != NULL)
930bcbe9155Sjg 			AUX64(av, AT_SUN_CPU, (uintptr_t)cpulist);
9317c478bd9Sstevel@tonic-gate 		AUX64(av, AT_NULL, 0);
9327c478bd9Sstevel@tonic-gate 		/*
9337c478bd9Sstevel@tonic-gate 		 * Realloc vectors and copy them.
9347c478bd9Sstevel@tonic-gate 		 */
9357c478bd9Sstevel@tonic-gate 		vsize = (caddr_t)bv - (caddr_t)bootv;
9367c478bd9Sstevel@tonic-gate 		if ((elfbootvecELF64 =
9377c478bd9Sstevel@tonic-gate 		    (Elf64_Boot *)kmem_alloc(vsize, 0)) == NULL)
9387c478bd9Sstevel@tonic-gate 			goto elf64error;
9397c478bd9Sstevel@tonic-gate 		bcopy((char *)bootv, (char *)elfbootvecELF64, vsize);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		size = (caddr_t)av - (caddr_t)auxv;
9427c478bd9Sstevel@tonic-gate 		if (size > sizeof (auxv)) {
9437c478bd9Sstevel@tonic-gate 			printf("readelf: overrun of available aux vectors\n");
9447c478bd9Sstevel@tonic-gate 			kmem_free(elfbootvecELF64, vsize);
9457c478bd9Sstevel@tonic-gate 			goto elf64error;
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		if ((elfbootvecELF64->eb_un.eb_ptr =
949b531f6d1SToomas Soome 		    (Elf64_Addr)kmem_alloc(size, 0)) == 0) {
9507c478bd9Sstevel@tonic-gate 			kmem_free(elfbootvecELF64, vsize);
9517c478bd9Sstevel@tonic-gate 			goto elf64error;
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 		bcopy((char *)auxv, (char *)(elfbootvecELF64->eb_un.eb_ptr),
955986fd29aSsetje 		    size);
9567c478bd9Sstevel@tonic-gate 	} else {
9577c478bd9Sstevel@tonic-gate 		kmem_free(allphdrs, phdrsize);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	return ((uint64_t)entrypt);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate elf64error:
9627c478bd9Sstevel@tonic-gate 	if (allphdrs != NULL)
9637c478bd9Sstevel@tonic-gate 		kmem_free(allphdrs, phdrsize);
9647c478bd9Sstevel@tonic-gate 	if (nhdr != NULL)
9657c478bd9Sstevel@tonic-gate 		kmem_free(nhdr, phdr->p_filesz);
9667c478bd9Sstevel@tonic-gate 	printf("Elf64 read error.\n");
9677c478bd9Sstevel@tonic-gate 	return (FAIL_READELF64);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate #endif	/* _ELF64_SUPPORT */
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate /*
9727c478bd9Sstevel@tonic-gate  * Load the interpreter.  It expects a
9737c478bd9Sstevel@tonic-gate  * relocatable .o capable of bootstrapping
9747c478bd9Sstevel@tonic-gate  * itself.
9757c478bd9Sstevel@tonic-gate  */
9767c478bd9Sstevel@tonic-gate static func_t
iload32(char * rtld,Elf32_Phdr * thdr,Elf32_Phdr * dhdr,auxv32_t ** avp)9777c478bd9Sstevel@tonic-gate iload32(char *rtld, Elf32_Phdr *thdr, Elf32_Phdr *dhdr, auxv32_t **avp)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	Elf32_Ehdr *ehdr = NULL;
9807c478bd9Sstevel@tonic-gate 	uintptr_t dl_entry = 0;
9817c478bd9Sstevel@tonic-gate 	uint_t i;
9827c478bd9Sstevel@tonic-gate 	int fd;
9837c478bd9Sstevel@tonic-gate 	int size;
9847c478bd9Sstevel@tonic-gate 	caddr_t shdrs = NULL;
9857c478bd9Sstevel@tonic-gate 	caddr_t etext, edata;
9867c478bd9Sstevel@tonic-gate 
98753391bafSeota 	/* use uintptr_t to suppress the gcc warning */
98853391bafSeota 	etext = (caddr_t)(uintptr_t)thdr->p_vaddr + thdr->p_memsz;
98953391bafSeota 	edata = (caddr_t)(uintptr_t)dhdr->p_vaddr + dhdr->p_memsz;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	/*
9927c478bd9Sstevel@tonic-gate 	 * Get the module path.
9937c478bd9Sstevel@tonic-gate 	 */
9947c478bd9Sstevel@tonic-gate 	module_path = getmodpath(filename);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	if ((fd = openpath(module_path, rtld, O_RDONLY)) < 0) {
9977c478bd9Sstevel@tonic-gate 		printf("boot: cannot find %s\n", rtld);
9987c478bd9Sstevel@tonic-gate 		goto errorx;
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 	dprintf("Opened %s OK\n", rtld);
10017c478bd9Sstevel@tonic-gate 	AUX(*avp, AT_SUN_LDNAME, rtld);
10027c478bd9Sstevel@tonic-gate 	/*
10037c478bd9Sstevel@tonic-gate 	 * Allocate and read the ELF header.
10047c478bd9Sstevel@tonic-gate 	 */
10057c478bd9Sstevel@tonic-gate 	if ((ehdr = (Elf32_Ehdr *)kmem_alloc(sizeof (Elf32_Ehdr), 0)) == NULL) {
10067c478bd9Sstevel@tonic-gate 		printf("boot: alloc error reading ELF header (%s).\n", rtld);
10077c478bd9Sstevel@tonic-gate 		goto error;
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (xread(fd, (char *)ehdr, sizeof (*ehdr)) != sizeof (*ehdr)) {
10117c478bd9Sstevel@tonic-gate 		printf("boot: error reading ELF header (%s).\n", rtld);
10127c478bd9Sstevel@tonic-gate 		goto error;
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	size = ehdr->e_shentsize * ehdr->e_shnum;
10167c478bd9Sstevel@tonic-gate 	if ((shdrs = (caddr_t)kmem_alloc(size, 0)) == NULL) {
10177c478bd9Sstevel@tonic-gate 		printf("boot: alloc error reading ELF header (%s).\n", rtld);
10187c478bd9Sstevel@tonic-gate 		goto error;
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 	/*
10217c478bd9Sstevel@tonic-gate 	 * Read the section headers.
10227c478bd9Sstevel@tonic-gate 	 */
10237c478bd9Sstevel@tonic-gate 	if (lseek(fd, ehdr->e_shoff, 0) == -1 ||
10247c478bd9Sstevel@tonic-gate 	    xread(fd, shdrs, size) != size) {
10257c478bd9Sstevel@tonic-gate 		printf("boot: error reading section headers\n");
10267c478bd9Sstevel@tonic-gate 		goto error;
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 	AUX(*avp, AT_SUN_LDELF, ehdr);
10297c478bd9Sstevel@tonic-gate 	AUX(*avp, AT_SUN_LDSHDR, shdrs);
10307c478bd9Sstevel@tonic-gate 	/*
10317c478bd9Sstevel@tonic-gate 	 * Load sections into the appropriate dynamic segment.
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	for (i = 1; i < ehdr->e_shnum; i++) {
10347c478bd9Sstevel@tonic-gate 		Elf32_Shdr *sp;
10357c478bd9Sstevel@tonic-gate 		caddr_t *spp;
10367c478bd9Sstevel@tonic-gate 		caddr_t load;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		sp = (Elf32_Shdr *)(shdrs + (i*ehdr->e_shentsize));
10397c478bd9Sstevel@tonic-gate 		/*
10407c478bd9Sstevel@tonic-gate 		 * If it's not allocated and not required
10417c478bd9Sstevel@tonic-gate 		 * to do relocation, skip it.
10427c478bd9Sstevel@tonic-gate 		 */
10437c478bd9Sstevel@tonic-gate 		if (!(sp->sh_flags & SHF_ALLOC) &&
1044986fd29aSsetje 		    sp->sh_type != SHT_RELA &&
1045986fd29aSsetje 		    sp->sh_type != SHT_SYMTAB &&
1046986fd29aSsetje 		    sp->sh_type != SHT_STRTAB)
10477c478bd9Sstevel@tonic-gate 			continue;
10487c478bd9Sstevel@tonic-gate 		/*
10497c478bd9Sstevel@tonic-gate 		 * If the section is read-only,
10507c478bd9Sstevel@tonic-gate 		 * it goes in as text.
10517c478bd9Sstevel@tonic-gate 		 */
10527c478bd9Sstevel@tonic-gate 		spp = (sp->sh_flags & SHF_WRITE)? &edata: &etext;
10537c478bd9Sstevel@tonic-gate 		/*
10547c478bd9Sstevel@tonic-gate 		 * Make some room for it.
10557c478bd9Sstevel@tonic-gate 		 */
10567c478bd9Sstevel@tonic-gate 		load = segbrk(spp, sp->sh_size, sp->sh_addralign);
10577c478bd9Sstevel@tonic-gate 		if (load == NULL) {
10587c478bd9Sstevel@tonic-gate 			printf("boot: allocating memory for sections failed\n");
10597c478bd9Sstevel@tonic-gate 			goto error;
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 		/*
10627c478bd9Sstevel@tonic-gate 		 * Compute the entry point of the linker.
10637c478bd9Sstevel@tonic-gate 		 */
10647c478bd9Sstevel@tonic-gate 		if (dl_entry == 0 &&
10657c478bd9Sstevel@tonic-gate 		    !(sp->sh_flags & SHF_WRITE) &&
10667c478bd9Sstevel@tonic-gate 		    (sp->sh_flags & SHF_EXECINSTR)) {
10677c478bd9Sstevel@tonic-gate 			dl_entry = (uintptr_t)load + ehdr->e_entry;
10687c478bd9Sstevel@tonic-gate 		}
10697c478bd9Sstevel@tonic-gate 		/*
10707c478bd9Sstevel@tonic-gate 		 * If it's bss, just zero it out.
10717c478bd9Sstevel@tonic-gate 		 */
10727c478bd9Sstevel@tonic-gate 		if (sp->sh_type == SHT_NOBITS) {
10737c478bd9Sstevel@tonic-gate 			bzero(load, sp->sh_size);
10747c478bd9Sstevel@tonic-gate 		} else {
10757c478bd9Sstevel@tonic-gate 			/*
10767c478bd9Sstevel@tonic-gate 			 * Read the section contents.
10777c478bd9Sstevel@tonic-gate 			 */
10787c478bd9Sstevel@tonic-gate 			if (lseek(fd, sp->sh_offset, 0) == -1 ||
10797c478bd9Sstevel@tonic-gate 			    xread(fd, load, sp->sh_size) != sp->sh_size) {
10807c478bd9Sstevel@tonic-gate 				printf("boot: error reading sections\n");
10817c478bd9Sstevel@tonic-gate 				goto error;
10827c478bd9Sstevel@tonic-gate 			}
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 		/*
108553391bafSeota 		 * Assign the section's virtual addr. Use uintptr_t to
108653391bafSeota 		 * suppress the gcc warning.
108753391bafSeota 		 */
108853391bafSeota 		sp->sh_addr = (Elf32_Off)(uintptr_t)load;
108953391bafSeota 		/*
109053391bafSeota 		 * Force instructions to be visible to icache. Use
109153391bafSeota 		 * uintptr_t to suppress the gcc warning as well.
10927c478bd9Sstevel@tonic-gate 		 */
10937c478bd9Sstevel@tonic-gate 		if (sp->sh_flags & SHF_EXECINSTR)
109453391bafSeota 			sync_instruction_memory((caddr_t)(uintptr_t)sp->sh_addr,
10957c478bd9Sstevel@tonic-gate 			    sp->sh_size);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 	/*
10987c478bd9Sstevel@tonic-gate 	 * Update sizes of segments.
10997c478bd9Sstevel@tonic-gate 	 */
11007c478bd9Sstevel@tonic-gate 	thdr->p_memsz = (Elf32_Word)((uintptr_t)etext - thdr->p_vaddr);
11017c478bd9Sstevel@tonic-gate 	dhdr->p_memsz = (Elf32_Word)((uintptr_t)edata - dhdr->p_vaddr);
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	/* load and relocate symbol tables in SAS */
11047c478bd9Sstevel@tonic-gate 	(void) close(fd);
11057c478bd9Sstevel@tonic-gate 	return ((func_t)dl_entry);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate error:
11087c478bd9Sstevel@tonic-gate 	(void) close(fd);
11097c478bd9Sstevel@tonic-gate errorx:
11107c478bd9Sstevel@tonic-gate 	if (ehdr)
11117c478bd9Sstevel@tonic-gate 		kmem_free(ehdr, sizeof (Elf32_Ehdr));
11127c478bd9Sstevel@tonic-gate 	if (shdrs)
11137c478bd9Sstevel@tonic-gate 		kmem_free(shdrs, size);
11147c478bd9Sstevel@tonic-gate 	printf("boot: error loading interpreter (%s)\n", rtld);
11157c478bd9Sstevel@tonic-gate 	return (FAIL);
11167c478bd9Sstevel@tonic-gate }
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate #ifdef	_ELF64_SUPPORT
11197c478bd9Sstevel@tonic-gate /*
11207c478bd9Sstevel@tonic-gate  * Load the interpreter.  It expects a
11217c478bd9Sstevel@tonic-gate  * relocatable .o capable of bootstrapping
11227c478bd9Sstevel@tonic-gate  * itself.
11237c478bd9Sstevel@tonic-gate  */
11247c478bd9Sstevel@tonic-gate static Elf64_Addr
iload64(char * rtld,Elf64_Phdr * thdr,Elf64_Phdr * dhdr,auxv64_t ** avp)11257c478bd9Sstevel@tonic-gate iload64(char *rtld, Elf64_Phdr *thdr, Elf64_Phdr *dhdr, auxv64_t **avp)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 	Elf64_Ehdr *ehdr = NULL;
11287c478bd9Sstevel@tonic-gate 	Elf64_Addr dl_entry = (Elf64_Addr)0;
11297c478bd9Sstevel@tonic-gate 	Elf64_Addr etext, edata;
11307c478bd9Sstevel@tonic-gate 	uint_t i;
11317c478bd9Sstevel@tonic-gate 	int fd;
11327c478bd9Sstevel@tonic-gate 	int size;
11337c478bd9Sstevel@tonic-gate 	caddr_t shdrs = NULL;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	etext = thdr->p_vaddr + thdr->p_memsz;
11367c478bd9Sstevel@tonic-gate 	edata = dhdr->p_vaddr + dhdr->p_memsz;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	/*
11397c478bd9Sstevel@tonic-gate 	 * Get the module path.
11407c478bd9Sstevel@tonic-gate 	 */
11417c478bd9Sstevel@tonic-gate 	module_path = getmodpath(filename);
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	if ((fd = openpath(module_path, rtld, O_RDONLY)) < 0) {
11447c478bd9Sstevel@tonic-gate 		printf("boot: cannot find %s\n", rtld);
11457c478bd9Sstevel@tonic-gate 		goto errorx;
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 	dprintf("Opened %s OK\n", rtld);
1148bcbe9155Sjg 	AUX64(*avp, AT_SUN_LDNAME, (uintptr_t)rtld);
11497c478bd9Sstevel@tonic-gate 	/*
11507c478bd9Sstevel@tonic-gate 	 * Allocate and read the ELF header.
11517c478bd9Sstevel@tonic-gate 	 */
11527c478bd9Sstevel@tonic-gate 	if ((ehdr = (Elf64_Ehdr *)kmem_alloc(sizeof (Elf64_Ehdr), 0)) == NULL) {
11537c478bd9Sstevel@tonic-gate 		printf("boot: alloc error reading ELF header (%s).\n", rtld);
11547c478bd9Sstevel@tonic-gate 		goto error;
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (xread(fd, (char *)ehdr, sizeof (*ehdr)) != sizeof (*ehdr)) {
11587c478bd9Sstevel@tonic-gate 		printf("boot: error reading ELF header (%s).\n", rtld);
11597c478bd9Sstevel@tonic-gate 		goto error;
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	size = ehdr->e_shentsize * ehdr->e_shnum;
11637c478bd9Sstevel@tonic-gate 	if ((shdrs = (caddr_t)kmem_alloc(size, 0)) == NULL) {
11647c478bd9Sstevel@tonic-gate 		printf("boot: alloc error reading ELF header (%s).\n", rtld);
11657c478bd9Sstevel@tonic-gate 		goto error;
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 	/*
11687c478bd9Sstevel@tonic-gate 	 * Read the section headers.
11697c478bd9Sstevel@tonic-gate 	 */
11707c478bd9Sstevel@tonic-gate 	if (lseek(fd, ehdr->e_shoff, 0) == -1 ||
11717c478bd9Sstevel@tonic-gate 	    xread(fd, shdrs, size) != size) {
11727c478bd9Sstevel@tonic-gate 		printf("boot: error reading section headers\n");
11737c478bd9Sstevel@tonic-gate 		goto error;
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	AUX64(*avp, AT_SUN_LDELF, ehdr);
11777c478bd9Sstevel@tonic-gate 	AUX64(*avp, AT_SUN_LDSHDR, shdrs);
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	/*
11807c478bd9Sstevel@tonic-gate 	 * Load sections into the appropriate dynamic segment.
11817c478bd9Sstevel@tonic-gate 	 */
11827c478bd9Sstevel@tonic-gate 	for (i = 1; i < ehdr->e_shnum; i++) {
11837c478bd9Sstevel@tonic-gate 		Elf64_Shdr *sp;
11847c478bd9Sstevel@tonic-gate 		Elf64_Addr *spp, load;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		sp = (Elf64_Shdr *)(shdrs + (i*ehdr->e_shentsize));
11877c478bd9Sstevel@tonic-gate 		/*
11887c478bd9Sstevel@tonic-gate 		 * If it's not allocated and not required
11897c478bd9Sstevel@tonic-gate 		 * to do relocation, skip it.
11907c478bd9Sstevel@tonic-gate 		 */
11917c478bd9Sstevel@tonic-gate 		if (!(sp->sh_flags & SHF_ALLOC) &&
11927c478bd9Sstevel@tonic-gate 		    sp->sh_type != SHT_SYMTAB &&
11937c478bd9Sstevel@tonic-gate 		    sp->sh_type != SHT_STRTAB &&
11947c478bd9Sstevel@tonic-gate 		    sp->sh_type != SHT_RELA)
11957c478bd9Sstevel@tonic-gate 			continue;
11967c478bd9Sstevel@tonic-gate 		/*
11977c478bd9Sstevel@tonic-gate 		 * If the section is read-only,
11987c478bd9Sstevel@tonic-gate 		 * it goes in as text.
11997c478bd9Sstevel@tonic-gate 		 */
12007c478bd9Sstevel@tonic-gate 		spp = (sp->sh_flags & SHF_WRITE)? &edata: &etext;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 		/*
12037c478bd9Sstevel@tonic-gate 		 * Make some room for it.
12047c478bd9Sstevel@tonic-gate 		 */
12057c478bd9Sstevel@tonic-gate 		load = (Elf64_Addr)segbrk((caddr_t *)spp, sp->sh_size,
12067c478bd9Sstevel@tonic-gate 		    sp->sh_addralign);
12077c478bd9Sstevel@tonic-gate 
1208b531f6d1SToomas Soome 		if (load == 0) {
12097c478bd9Sstevel@tonic-gate 			printf("boot: allocating memory for section %d "
12107c478bd9Sstevel@tonic-gate 			    "failed\n", i);
12117c478bd9Sstevel@tonic-gate 			goto error;
12127c478bd9Sstevel@tonic-gate 		}
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 		/*
12157c478bd9Sstevel@tonic-gate 		 * Compute the entry point of the linker.
12167c478bd9Sstevel@tonic-gate 		 */
12177c478bd9Sstevel@tonic-gate 		if (dl_entry == 0 &&
12187c478bd9Sstevel@tonic-gate 		    !(sp->sh_flags & SHF_WRITE) &&
12197c478bd9Sstevel@tonic-gate 		    (sp->sh_flags & SHF_EXECINSTR)) {
12207c478bd9Sstevel@tonic-gate 			dl_entry = load + ehdr->e_entry;
12217c478bd9Sstevel@tonic-gate 			if (verbosemode)
12227c478bd9Sstevel@tonic-gate 				dprintf("boot: loading linker @ 0x%llx\n",
12237c478bd9Sstevel@tonic-gate 				    (u_longlong_t)dl_entry);
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 		/*
12277c478bd9Sstevel@tonic-gate 		 * If it's bss, just zero it out.
12287c478bd9Sstevel@tonic-gate 		 */
12297c478bd9Sstevel@tonic-gate 		if (sp->sh_type == SHT_NOBITS) {
1230bcbe9155Sjg 			bzero((caddr_t)(uintptr_t)load, sp->sh_size);
12317c478bd9Sstevel@tonic-gate 		} else {
12327c478bd9Sstevel@tonic-gate 			/*
12337c478bd9Sstevel@tonic-gate 			 * Read the section contents.
12347c478bd9Sstevel@tonic-gate 			 */
12357c478bd9Sstevel@tonic-gate 			if (lseek(fd, sp->sh_offset, 0) == -1 ||
1236bcbe9155Sjg 			    xread(fd, (caddr_t)(uintptr_t)load, sp->sh_size) !=
1237986fd29aSsetje 			    sp->sh_size) {
1238986fd29aSsetje 				printf("boot: error reading section %d\n", i);
1239986fd29aSsetje 				goto error;
12407c478bd9Sstevel@tonic-gate 			}
12417c478bd9Sstevel@tonic-gate 		}
12427c478bd9Sstevel@tonic-gate 		/*
12437c478bd9Sstevel@tonic-gate 		 * Assign the section's virtual addr.
12447c478bd9Sstevel@tonic-gate 		 */
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 		sp->sh_addr = load;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		if (verbosemode)
12497c478bd9Sstevel@tonic-gate 			dprintf("boot: section %d, type %d, loaded @ 0x%llx, "
12507c478bd9Sstevel@tonic-gate 			    "size 0x%llx\n", i, sp->sh_type, (u_longlong_t)load,
12517c478bd9Sstevel@tonic-gate 			    (u_longlong_t)sp->sh_size);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 		/* force instructions to be visible to icache */
12547c478bd9Sstevel@tonic-gate 		if (sp->sh_flags & SHF_EXECINSTR)
1255bcbe9155Sjg 			sync_instruction_memory((caddr_t)(uintptr_t)sp->sh_addr,
12567c478bd9Sstevel@tonic-gate 			    sp->sh_size);
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 	/*
12597c478bd9Sstevel@tonic-gate 	 * Update sizes of segments.
12607c478bd9Sstevel@tonic-gate 	 */
12617c478bd9Sstevel@tonic-gate 	thdr->p_memsz = etext - thdr->p_vaddr;
12627c478bd9Sstevel@tonic-gate 	dhdr->p_memsz = edata - dhdr->p_vaddr;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	/* load and relocate symbol tables in SAS */
12657c478bd9Sstevel@tonic-gate 	(void) close(fd);
12667c478bd9Sstevel@tonic-gate 	return (dl_entry);
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate error:
12697c478bd9Sstevel@tonic-gate 	(void) close(fd);
12707c478bd9Sstevel@tonic-gate errorx:
12717c478bd9Sstevel@tonic-gate 	if (ehdr)
12727c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)ehdr, sizeof (Elf64_Ehdr));
12737c478bd9Sstevel@tonic-gate 	if (shdrs)
12747c478bd9Sstevel@tonic-gate 		kmem_free(shdrs, size);
12757c478bd9Sstevel@tonic-gate 	printf("boot: error loading interpreter (%s)\n", rtld);
12767c478bd9Sstevel@tonic-gate 	return (FAIL_ILOAD64);
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate #endif	/* _ELF64_SUPPORT */
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate /*
12817c478bd9Sstevel@tonic-gate  * Extend the segment's "break" value by bytes.
12827c478bd9Sstevel@tonic-gate  */
12837c478bd9Sstevel@tonic-gate static caddr_t
segbrk(caddr_t * spp,size_t bytes,size_t align)12847c478bd9Sstevel@tonic-gate segbrk(caddr_t *spp, size_t bytes, size_t align)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate 	caddr_t va, pva;
12877c478bd9Sstevel@tonic-gate 	size_t size = 0;
12887c478bd9Sstevel@tonic-gate 	unsigned int alloc_pagesize = pagesize;
12897c478bd9Sstevel@tonic-gate 	unsigned int alloc_align = 0;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	if (npagesize) {
12927c478bd9Sstevel@tonic-gate 		alloc_align = npagesize;
12937c478bd9Sstevel@tonic-gate 		alloc_pagesize = npagesize;
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	va = (caddr_t)ALIGN(*spp, align);
12977c478bd9Sstevel@tonic-gate 	pva = (caddr_t)roundup((uintptr_t)*spp, alloc_pagesize);
12987c478bd9Sstevel@tonic-gate 	/*
12997c478bd9Sstevel@tonic-gate 	 * Need more pages?
13007c478bd9Sstevel@tonic-gate 	 */
13017c478bd9Sstevel@tonic-gate 	if (va + bytes > pva) {
13027c478bd9Sstevel@tonic-gate 		size = roundup((bytes - (pva - va)), alloc_pagesize);
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 		if (get_progmemory(pva, size, alloc_align)) {
13057c478bd9Sstevel@tonic-gate 			printf("boot: segbrk allocation failed, "
13067c478bd9Sstevel@tonic-gate 			    "0x%lx bytes @ %p\n", bytes, (void *)pva);
13077c478bd9Sstevel@tonic-gate 			return (NULL);
13087c478bd9Sstevel@tonic-gate 		}
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 	*spp = va + bytes;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	return (va);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate /*
13167c478bd9Sstevel@tonic-gate  * Open the file using a search path and
13177c478bd9Sstevel@tonic-gate  * return the file descriptor (or -1 on failure).
13187c478bd9Sstevel@tonic-gate  */
13197c478bd9Sstevel@tonic-gate static int
openpath(char * path,char * fname,int flags)1320622d9a34SRichard Lowe openpath(char *path, char *fname, int flags)
13217c478bd9Sstevel@tonic-gate {
13227c478bd9Sstevel@tonic-gate 	register char *p, *q;
13237c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
13247c478bd9Sstevel@tonic-gate 	int fd;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/*
13277c478bd9Sstevel@tonic-gate 	 * If the file name is absolute,
13287c478bd9Sstevel@tonic-gate 	 * don't use the module search path.
13297c478bd9Sstevel@tonic-gate 	 */
13307c478bd9Sstevel@tonic-gate 	if (fname[0] == '/')
13317c478bd9Sstevel@tonic-gate 		return (open(fname, flags));
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	q = NULL;
13347c478bd9Sstevel@tonic-gate 	for (p = path;  /* forever */;  p = q) {
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 		while (*p == ' ' || *p == '\t' || *p == ':')
13377c478bd9Sstevel@tonic-gate 			p++;
13387c478bd9Sstevel@tonic-gate 		if (*p == '\0')
13397c478bd9Sstevel@tonic-gate 			break;
13407c478bd9Sstevel@tonic-gate 		q = p;
13417c478bd9Sstevel@tonic-gate 		while (*q && *q != ' ' && *q != '\t' && *q != ':')
13427c478bd9Sstevel@tonic-gate 			q++;
13437c478bd9Sstevel@tonic-gate 		(void) strncpy(buf, p, q - p);
13447c478bd9Sstevel@tonic-gate 		if (q[-1] != '/') {
13457c478bd9Sstevel@tonic-gate 			buf[q - p] = '/';
13467c478bd9Sstevel@tonic-gate 			(void) strcpy(&buf[q - p + 1], fname);
13477c478bd9Sstevel@tonic-gate 		} else {
13487c478bd9Sstevel@tonic-gate 			/*
13497c478bd9Sstevel@tonic-gate 			 * This checks for paths that end in '/'
13507c478bd9Sstevel@tonic-gate 			 */
13517c478bd9Sstevel@tonic-gate 			(void) strcpy(&buf[q - p], fname);
13527c478bd9Sstevel@tonic-gate 		}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 		if ((fd = open(buf, flags)) > 0)
13557c478bd9Sstevel@tonic-gate 			return (fd);
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 	return (-1);
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate /*
13617c478bd9Sstevel@tonic-gate  * Get the module search path.
13627c478bd9Sstevel@tonic-gate  */
13637c478bd9Sstevel@tonic-gate static char *
getmodpath(char * fname)1364622d9a34SRichard Lowe getmodpath(char *fname)
13657c478bd9Sstevel@tonic-gate {
13667c478bd9Sstevel@tonic-gate 	register char *p = strrchr(fname, '/');
13677c478bd9Sstevel@tonic-gate 	static char mod_path[MOD_MAXPATH];
13687c478bd9Sstevel@tonic-gate 	size_t len;
13697c478bd9Sstevel@tonic-gate 	extern char *impl_arch_name;
1370622d9a34SRichard Lowe #if defined(__sparcv9)
13717c478bd9Sstevel@tonic-gate 	char    *isastr = "/sparcv9";
13727c478bd9Sstevel@tonic-gate 	size_t	isalen = strlen(isastr);
1373622d9a34SRichard Lowe #endif	/* __sparcv9 */
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	if (p == NULL) {
13767c478bd9Sstevel@tonic-gate 		/* strchr could not find a "/" */
13777c478bd9Sstevel@tonic-gate 		printf("%s is not a legal kernel pathname", fname);
13787c478bd9Sstevel@tonic-gate 		return (NULL);
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 	while (p > fname && *(p - 1) == '/')
13817c478bd9Sstevel@tonic-gate 		p--;		/* remove trailing "/"s */
13827c478bd9Sstevel@tonic-gate 	if (p == fname)
13837c478bd9Sstevel@tonic-gate 		p++;		/* "/" is the modpath in this case */
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	len = p - fname;
13867c478bd9Sstevel@tonic-gate 	(void) strncpy(mod_path, fname, len);
13877c478bd9Sstevel@tonic-gate 	mod_path[len] = 0;
13887c478bd9Sstevel@tonic-gate 
1389622d9a34SRichard Lowe #if defined(__sparcv9)
13907c478bd9Sstevel@tonic-gate 	len = strlen(mod_path);
13917c478bd9Sstevel@tonic-gate 	if ((len > isalen) && (strcmp(&mod_path[len - isalen], isastr) == 0)) {
13927c478bd9Sstevel@tonic-gate 		mod_path[len - isalen] = '\0';
13937c478bd9Sstevel@tonic-gate 		if ((client_isLP64 == 0) && verbosemode)
13947c478bd9Sstevel@tonic-gate 			printf("Assuming LP64 %s client.\n", isastr);
13957c478bd9Sstevel@tonic-gate 		client_isLP64 = 1;
13967c478bd9Sstevel@tonic-gate 	}
1397622d9a34SRichard Lowe #endif	/* __sparcv9 */
13987c478bd9Sstevel@tonic-gate 	mod_path_uname_m(mod_path, impl_arch_name);
13997c478bd9Sstevel@tonic-gate 	(void) strcat(mod_path, " ");
14007c478bd9Sstevel@tonic-gate 	(void) strcat(mod_path, MOD_DEFPATH);
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	if (boothowto & RB_ASKNAME) {
14037c478bd9Sstevel@tonic-gate 		char buf[MOD_MAXPATH];
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 		printf("Enter default directory for modules [%s]: ", mod_path);
14067c478bd9Sstevel@tonic-gate 		(void) cons_gets(buf, sizeof (buf));
14077c478bd9Sstevel@tonic-gate 		if (buf[0] != '\0')
14087c478bd9Sstevel@tonic-gate 			(void) strcpy(mod_path, buf);
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate 	if (verbosemode)
14117c478bd9Sstevel@tonic-gate 		printf("modpath: %s\n", mod_path);
14127c478bd9Sstevel@tonic-gate 	return (mod_path);
14137c478bd9Sstevel@tonic-gate }
1414