xref: /illumos-gate/usr/src/lib/libc/sparc/crt/_rtld.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Redirection ld.so.  Based on the 4.x binary compatibility ld.so, used
31*7c478bd9Sstevel@tonic-gate  * to redirect aliases for ld.so to the real one.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * Import data structures
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/sysconfig.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
44*7c478bd9Sstevel@tonic-gate #include <elf.h>
45*7c478bd9Sstevel@tonic-gate #include <link.h>
46*7c478bd9Sstevel@tonic-gate #include <string.h>
47*7c478bd9Sstevel@tonic-gate #include "alias_boot.h"
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate  * Local manifest constants and macros.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate #define	ALIGN(x, a)		((uintptr_t)(x) & ~((a) - 1))
53*7c478bd9Sstevel@tonic-gate #define	ROUND(x, a)		(((uintptr_t)(x) + ((a) - 1)) &  ~((a) - 1))
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #define	EMPTY	strings[EMPTY_S]
56*7c478bd9Sstevel@tonic-gate #define	LDSO	strings[LDSO_S]
57*7c478bd9Sstevel@tonic-gate #define	ZERO	strings[ZERO_S]
58*7c478bd9Sstevel@tonic-gate #define	CLOSE	(*(funcs[CLOSE_F]))
59*7c478bd9Sstevel@tonic-gate #define	FSTAT	(*(funcs[FSTAT_F]))
60*7c478bd9Sstevel@tonic-gate #define	MMAP	(*(funcs[MMAP_F]))
61*7c478bd9Sstevel@tonic-gate #define	MUNMAP	(*(funcs[MUNMAP_F]))
62*7c478bd9Sstevel@tonic-gate #define	OPEN	(*(funcs[OPEN_F]))
63*7c478bd9Sstevel@tonic-gate #define	PANIC	(*(funcs[PANIC_F]))
64*7c478bd9Sstevel@tonic-gate #define	SYSCONFIG (*(funcs[SYSCONFIG_F]))
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * Alias ld.so entry point -- receives a bootstrap structure and a vector
68*7c478bd9Sstevel@tonic-gate  * of strings.  The vector is "well-known" to us, and consists of pointers
69*7c478bd9Sstevel@tonic-gate  * to string constants.  This aliasing bootstrap requires no relocation in
70*7c478bd9Sstevel@tonic-gate  * order to run, save for the pointers of constant strings.  This second
71*7c478bd9Sstevel@tonic-gate  * parameter provides this.  Note that this program is carefully coded in
72*7c478bd9Sstevel@tonic-gate  * order to maintain the "no bootstrapping" requirement -- it calls only
73*7c478bd9Sstevel@tonic-gate  * local functions, uses no intrinsics, etc.
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate static void *
76*7c478bd9Sstevel@tonic-gate __rtld(Elf32_Boot *ebp, const char *strings[], int (*funcs[])())
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	int i, p;			/* working */
79*7c478bd9Sstevel@tonic-gate 	long j;				/* working */
80*7c478bd9Sstevel@tonic-gate 	long page_size = 0;		/* size of a page */
81*7c478bd9Sstevel@tonic-gate 	const char *program_name = EMPTY; /* our name */
82*7c478bd9Sstevel@tonic-gate 	int ldfd;			/* fd assigned to ld.so */
83*7c478bd9Sstevel@tonic-gate 	int dzfd = 0;			/* fd assigned to /dev/zero */
84*7c478bd9Sstevel@tonic-gate 	Elf32_Ehdr *ehdr;		/* ELF header of ld.so */
85*7c478bd9Sstevel@tonic-gate 	Elf32_Phdr *phdr;		/* first Phdr in file */
86*7c478bd9Sstevel@tonic-gate 	Elf32_Phdr *pptr;		/* working Phdr */
87*7c478bd9Sstevel@tonic-gate 	Elf32_Phdr *lph = NULL;		/* last loadable Phdr */
88*7c478bd9Sstevel@tonic-gate 	Elf32_Phdr *fph = NULL;		/* first loadable Phdr */
89*7c478bd9Sstevel@tonic-gate 	caddr_t	maddr;			/* pointer to mapping claim */
90*7c478bd9Sstevel@tonic-gate 	Elf32_Off mlen;			/* total mapping claim */
91*7c478bd9Sstevel@tonic-gate 	caddr_t faddr;			/* first program mapping of ld.so */
92*7c478bd9Sstevel@tonic-gate 	Elf32_Off foff;			/* file offset for segment mapping */
93*7c478bd9Sstevel@tonic-gate 	Elf32_Off flen;			/* file length for segment mapping */
94*7c478bd9Sstevel@tonic-gate 	caddr_t addr;			/* working mapping address */
95*7c478bd9Sstevel@tonic-gate 	caddr_t zaddr;			/* /dev/zero working mapping addr */
96*7c478bd9Sstevel@tonic-gate 	struct stat sb;			/* stat buffer for sizing */
97*7c478bd9Sstevel@tonic-gate 	auxv_t *ap;			/* working aux pointer */
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	/*
100*7c478bd9Sstevel@tonic-gate 	 * Discover things about our environment: auxiliary vector (if
101*7c478bd9Sstevel@tonic-gate 	 * any), arguments, program name, and the like.
102*7c478bd9Sstevel@tonic-gate 	 */
103*7c478bd9Sstevel@tonic-gate 	while (ebp->eb_tag != NULL) {
104*7c478bd9Sstevel@tonic-gate 		switch (ebp->eb_tag) {
105*7c478bd9Sstevel@tonic-gate 		case EB_ARGV:
106*7c478bd9Sstevel@tonic-gate 			program_name = *((char **)ebp->eb_un.eb_ptr);
107*7c478bd9Sstevel@tonic-gate 			break;
108*7c478bd9Sstevel@tonic-gate 		case EB_AUXV:
109*7c478bd9Sstevel@tonic-gate 			for (ap = (auxv_t *)ebp->eb_un.eb_ptr;
110*7c478bd9Sstevel@tonic-gate 			    ap->a_type != AT_NULL; ap++)
111*7c478bd9Sstevel@tonic-gate 				if (ap->a_type == AT_PAGESZ) {
112*7c478bd9Sstevel@tonic-gate 					page_size = ap->a_un.a_val;
113*7c478bd9Sstevel@tonic-gate 					break;
114*7c478bd9Sstevel@tonic-gate 				}
115*7c478bd9Sstevel@tonic-gate 			break;
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 		ebp++;
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/*
121*7c478bd9Sstevel@tonic-gate 	 * If we didn't get a page size from looking in the auxiliary
122*7c478bd9Sstevel@tonic-gate 	 * vector, we need to get one now.
123*7c478bd9Sstevel@tonic-gate 	 */
124*7c478bd9Sstevel@tonic-gate 	if (page_size == 0) {
125*7c478bd9Sstevel@tonic-gate 		page_size = SYSCONFIG(_CONFIG_PAGESIZE);
126*7c478bd9Sstevel@tonic-gate 		ebp->eb_tag = EB_PAGESIZE, (ebp++)->eb_un.eb_val =
127*7c478bd9Sstevel@tonic-gate 		    (Elf32_Word)page_size;
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	/*
131*7c478bd9Sstevel@tonic-gate 	 * Map in the real ld.so.  Note that we're mapping it as
132*7c478bd9Sstevel@tonic-gate 	 * an ELF database, not as a program -- we just want to walk it's
133*7c478bd9Sstevel@tonic-gate 	 * data structures.  Further mappings will actually establish the
134*7c478bd9Sstevel@tonic-gate 	 * program in the address space.
135*7c478bd9Sstevel@tonic-gate 	 */
136*7c478bd9Sstevel@tonic-gate 	if ((ldfd = OPEN(LDSO, O_RDONLY)) == -1)
137*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
138*7c478bd9Sstevel@tonic-gate 	if (FSTAT(ldfd, &sb) == -1)
139*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
140*7c478bd9Sstevel@tonic-gate 	ehdr = (Elf32_Ehdr *)MMAP(0, sb.st_size, PROT_READ | PROT_EXEC,
141*7c478bd9Sstevel@tonic-gate 	    MAP_SHARED, ldfd, 0);
142*7c478bd9Sstevel@tonic-gate 	if (ehdr == (Elf32_Ehdr *)-1)
143*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	/*
146*7c478bd9Sstevel@tonic-gate 	 * Validate the file we're looking at, ensure it has the correct
147*7c478bd9Sstevel@tonic-gate 	 * ELF structures, such as: ELF magic numbers, coded for SPARC,
148*7c478bd9Sstevel@tonic-gate 	 * is a ".so", etc.
149*7c478bd9Sstevel@tonic-gate 	 */
150*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
151*7c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
152*7c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
153*7c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3)
154*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
155*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
156*7c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB)
157*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
158*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_type != ET_DYN)
159*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
160*7c478bd9Sstevel@tonic-gate 	if ((ehdr->e_machine != EM_SPARC) &&
161*7c478bd9Sstevel@tonic-gate 	    (ehdr->e_machine != EM_SPARC32PLUS))
162*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
163*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_version > EV_CURRENT)
164*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	/*
167*7c478bd9Sstevel@tonic-gate 	 * Point at program headers and start figuring out what to load.
168*7c478bd9Sstevel@tonic-gate 	 */
169*7c478bd9Sstevel@tonic-gate 	phdr = (Elf32_Phdr *)((caddr_t)ehdr + ehdr->e_phoff);
170*7c478bd9Sstevel@tonic-gate 	for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++,
171*7c478bd9Sstevel@tonic-gate 	    pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize))
172*7c478bd9Sstevel@tonic-gate 		if (pptr->p_type == PT_LOAD) {
173*7c478bd9Sstevel@tonic-gate 			if (fph == 0) {
174*7c478bd9Sstevel@tonic-gate 				fph = pptr;
175*7c478bd9Sstevel@tonic-gate 			} else if (pptr->p_vaddr <= lph->p_vaddr)
176*7c478bd9Sstevel@tonic-gate 				PANIC(program_name);
177*7c478bd9Sstevel@tonic-gate 			lph = pptr;
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	/*
181*7c478bd9Sstevel@tonic-gate 	 * We'd better have at least one loadable segment.
182*7c478bd9Sstevel@tonic-gate 	 */
183*7c478bd9Sstevel@tonic-gate 	if (fph == 0)
184*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	/*
187*7c478bd9Sstevel@tonic-gate 	 * Map enough address space to hold the program (as opposed to the
188*7c478bd9Sstevel@tonic-gate 	 * file) represented by ld.so.  The amount to be assigned is the
189*7c478bd9Sstevel@tonic-gate 	 * range between the end of the last loadable segment and the
190*7c478bd9Sstevel@tonic-gate 	 * beginning of the first PLUS the alignment of the first segment.
191*7c478bd9Sstevel@tonic-gate 	 * mmap() can assign us any page-aligned address, but the relocations
192*7c478bd9Sstevel@tonic-gate 	 * assume the alignments included in the program header.  As an
193*7c478bd9Sstevel@tonic-gate 	 * optimization, however, let's assume that mmap() will actually
194*7c478bd9Sstevel@tonic-gate 	 * give us an aligned address -- since if it does, we can save
195*7c478bd9Sstevel@tonic-gate 	 * an munmap() later on.  If it doesn't -- then go try it again.
196*7c478bd9Sstevel@tonic-gate 	 */
197*7c478bd9Sstevel@tonic-gate 	mlen = ROUND((lph->p_vaddr + lph->p_memsz) -
198*7c478bd9Sstevel@tonic-gate 	    ALIGN(fph->p_vaddr, page_size), page_size);
199*7c478bd9Sstevel@tonic-gate 	maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC,
200*7c478bd9Sstevel@tonic-gate 	    MAP_SHARED, ldfd, 0);
201*7c478bd9Sstevel@tonic-gate 	if (maddr == (caddr_t)-1)
202*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
203*7c478bd9Sstevel@tonic-gate 	faddr = (caddr_t)ROUND(maddr, fph->p_align);
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	/*
206*7c478bd9Sstevel@tonic-gate 	 * Check to see whether alignment skew was really needed.
207*7c478bd9Sstevel@tonic-gate 	 */
208*7c478bd9Sstevel@tonic-gate 	if (faddr != maddr) {
209*7c478bd9Sstevel@tonic-gate 		(void) MUNMAP(maddr, mlen);
210*7c478bd9Sstevel@tonic-gate 		mlen = ROUND((lph->p_vaddr + lph->p_memsz) -
211*7c478bd9Sstevel@tonic-gate 		    ALIGN(fph->p_vaddr, fph->p_align) + fph->p_align,
212*7c478bd9Sstevel@tonic-gate 		    page_size);
213*7c478bd9Sstevel@tonic-gate 		maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC,
214*7c478bd9Sstevel@tonic-gate 		    MAP_SHARED, ldfd, 0);
215*7c478bd9Sstevel@tonic-gate 		if (maddr == (caddr_t)-1)
216*7c478bd9Sstevel@tonic-gate 			PANIC(program_name);
217*7c478bd9Sstevel@tonic-gate 		faddr = (caddr_t)ROUND(maddr, fph->p_align);
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	/*
221*7c478bd9Sstevel@tonic-gate 	 * We have the address space reserved, so map each loadable segment.
222*7c478bd9Sstevel@tonic-gate 	 */
223*7c478bd9Sstevel@tonic-gate 	for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++,
224*7c478bd9Sstevel@tonic-gate 	    pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize)) {
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 		/*
227*7c478bd9Sstevel@tonic-gate 		 * Skip non-loadable segments or segments that don't occupy
228*7c478bd9Sstevel@tonic-gate 		 * any memory.
229*7c478bd9Sstevel@tonic-gate 		 */
230*7c478bd9Sstevel@tonic-gate 		if ((pptr->p_type != PT_LOAD) || (pptr->p_memsz == 0))
231*7c478bd9Sstevel@tonic-gate 			continue;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 		/*
234*7c478bd9Sstevel@tonic-gate 		 * Determine the file offset to which the mapping will
235*7c478bd9Sstevel@tonic-gate 		 * directed (must be aligned) and how much to map (might
236*7c478bd9Sstevel@tonic-gate 		 * be more than the file in the case of .bss.)
237*7c478bd9Sstevel@tonic-gate 		 */
238*7c478bd9Sstevel@tonic-gate 		foff = ALIGN(pptr->p_offset, page_size);
239*7c478bd9Sstevel@tonic-gate 		flen = pptr->p_memsz + (pptr->p_offset - foff);
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 		/*
242*7c478bd9Sstevel@tonic-gate 		 * Set address of this segment relative to our base.
243*7c478bd9Sstevel@tonic-gate 		 */
244*7c478bd9Sstevel@tonic-gate 		addr = (caddr_t)ALIGN(faddr + pptr->p_vaddr, page_size);
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 		/*
247*7c478bd9Sstevel@tonic-gate 		 * If this is the first program header, record our base
248*7c478bd9Sstevel@tonic-gate 		 * address for later use.
249*7c478bd9Sstevel@tonic-gate 		 */
250*7c478bd9Sstevel@tonic-gate 		if (pptr == phdr) {
251*7c478bd9Sstevel@tonic-gate 			ebp->eb_tag = EB_LDSO_BASE;
252*7c478bd9Sstevel@tonic-gate 			(ebp++)->eb_un.eb_ptr = (Elf32_Addr)addr;
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 		/*
256*7c478bd9Sstevel@tonic-gate 		 * Unmap anything from the last mapping address to this
257*7c478bd9Sstevel@tonic-gate 		 * one.
258*7c478bd9Sstevel@tonic-gate 		 */
259*7c478bd9Sstevel@tonic-gate 		if (addr - maddr) {
260*7c478bd9Sstevel@tonic-gate 			(void) MUNMAP(maddr, addr - maddr);
261*7c478bd9Sstevel@tonic-gate 			mlen -= addr - maddr;
262*7c478bd9Sstevel@tonic-gate 		}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 		/*
265*7c478bd9Sstevel@tonic-gate 		 * Determine the mapping protection from the section
266*7c478bd9Sstevel@tonic-gate 		 * attributes.
267*7c478bd9Sstevel@tonic-gate 		 */
268*7c478bd9Sstevel@tonic-gate 		i = 0;
269*7c478bd9Sstevel@tonic-gate 		if (pptr->p_flags & PF_R)
270*7c478bd9Sstevel@tonic-gate 			i |= PROT_READ;
271*7c478bd9Sstevel@tonic-gate 		if (pptr->p_flags & PF_W)
272*7c478bd9Sstevel@tonic-gate 			i |= PROT_WRITE;
273*7c478bd9Sstevel@tonic-gate 		if (pptr->p_flags & PF_X)
274*7c478bd9Sstevel@tonic-gate 			i |= PROT_EXEC;
275*7c478bd9Sstevel@tonic-gate 		if ((caddr_t)MMAP((caddr_t)addr, flen, i,
276*7c478bd9Sstevel@tonic-gate 		    MAP_FIXED | MAP_PRIVATE, ldfd, foff) == (caddr_t)-1)
277*7c478bd9Sstevel@tonic-gate 			PANIC(program_name);
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 		/*
280*7c478bd9Sstevel@tonic-gate 		 * If the memory occupancy of the segment overflows the
281*7c478bd9Sstevel@tonic-gate 		 * definition in the file, we need to "zero out" the
282*7c478bd9Sstevel@tonic-gate 		 * end of the mapping we've established, and if necessary,
283*7c478bd9Sstevel@tonic-gate 		 * map some more space from /dev/zero.
284*7c478bd9Sstevel@tonic-gate 		 */
285*7c478bd9Sstevel@tonic-gate 		if (pptr->p_memsz > pptr->p_filesz) {
286*7c478bd9Sstevel@tonic-gate 			foff = (uintptr_t)faddr + pptr->p_vaddr +
287*7c478bd9Sstevel@tonic-gate 				pptr->p_filesz;
288*7c478bd9Sstevel@tonic-gate 			zaddr = (caddr_t)ROUND(foff, page_size);
289*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < (int)(zaddr - foff); j++)
290*7c478bd9Sstevel@tonic-gate 				*((char *)foff + j) = 0;
291*7c478bd9Sstevel@tonic-gate 			j = (faddr + pptr->p_vaddr + pptr->p_memsz) - zaddr;
292*7c478bd9Sstevel@tonic-gate 			if (j > 0) {
293*7c478bd9Sstevel@tonic-gate 				if (dzfd == 0) {
294*7c478bd9Sstevel@tonic-gate 					dzfd = OPEN(ZERO, O_RDWR);
295*7c478bd9Sstevel@tonic-gate 					if (dzfd == -1)
296*7c478bd9Sstevel@tonic-gate 						PANIC(program_name);
297*7c478bd9Sstevel@tonic-gate 				}
298*7c478bd9Sstevel@tonic-gate 				if ((caddr_t)MMAP((caddr_t)zaddr, j, i,
299*7c478bd9Sstevel@tonic-gate 				    MAP_FIXED | MAP_PRIVATE, dzfd,
300*7c478bd9Sstevel@tonic-gate 				    0) == (caddr_t)-1)
301*7c478bd9Sstevel@tonic-gate 					PANIC(program_name);
302*7c478bd9Sstevel@tonic-gate 			}
303*7c478bd9Sstevel@tonic-gate 		}
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 		/*
306*7c478bd9Sstevel@tonic-gate 		 * Update the mapping claim pointer.
307*7c478bd9Sstevel@tonic-gate 		 */
308*7c478bd9Sstevel@tonic-gate 		maddr = addr + ROUND(flen, page_size);
309*7c478bd9Sstevel@tonic-gate 		mlen -= maddr - addr;
310*7c478bd9Sstevel@tonic-gate 	}
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	/*
313*7c478bd9Sstevel@tonic-gate 	 * Unmap any final reservation.
314*7c478bd9Sstevel@tonic-gate 	 */
315*7c478bd9Sstevel@tonic-gate 	if (mlen != 0)
316*7c478bd9Sstevel@tonic-gate 		(void) MUNMAP(maddr, mlen);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	/*
319*7c478bd9Sstevel@tonic-gate 	 * Clean up file descriptor space we've consumed.  Pass along
320*7c478bd9Sstevel@tonic-gate 	 * the /dev/zero file descriptor we got -- every cycle counts.
321*7c478bd9Sstevel@tonic-gate 	 */
322*7c478bd9Sstevel@tonic-gate 	(void) CLOSE(ldfd);
323*7c478bd9Sstevel@tonic-gate 	if (dzfd != 0)
324*7c478bd9Sstevel@tonic-gate 		ebp->eb_tag = EB_DEVZERO, (ebp++)->eb_un.eb_val = dzfd;
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	/*
327*7c478bd9Sstevel@tonic-gate 	 * The call itself.  Note that we start 1 instruction word in.
328*7c478bd9Sstevel@tonic-gate 	 * The ELF ld.so contains an "entry vector" of branch instructions,
329*7c478bd9Sstevel@tonic-gate 	 * which, for our interest are:
330*7c478bd9Sstevel@tonic-gate 	 *	+0:	ba, a	<normal startup>
331*7c478bd9Sstevel@tonic-gate 	 *	+4:	ba, a	<compatibility startup>
332*7c478bd9Sstevel@tonic-gate 	 *	+8:	ba, a	<alias startup>
333*7c478bd9Sstevel@tonic-gate 	 * By starting at the alias startup, the ELF ld.so knows
334*7c478bd9Sstevel@tonic-gate 	 * that a pointer to "eb" is available to it and further knows
335*7c478bd9Sstevel@tonic-gate 	 * how to calculate the offset to the program's arguments and
336*7c478bd9Sstevel@tonic-gate 	 * other structures.  We do the "call" by returning to our
337*7c478bd9Sstevel@tonic-gate 	 * bootstrap and then jumping to the address that we return.
338*7c478bd9Sstevel@tonic-gate 	 */
339*7c478bd9Sstevel@tonic-gate 	ebp->eb_tag = EB_NULL, ebp->eb_un.eb_val = 0;
340*7c478bd9Sstevel@tonic-gate 	return ((void *)(ehdr->e_entry + faddr + 8));
341*7c478bd9Sstevel@tonic-gate }
342