xref: /illumos-gate/usr/src/lib/libc/i386/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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate  * Redirection ld.so.  Based on the 4.x binary compatibility ld.so, used
26*7c478bd9Sstevel@tonic-gate  * to redirect aliases for ld.so to the real one.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1991, 2001 by Sun Microsystems, Inc.
31*7c478bd9Sstevel@tonic-gate  * All rights reserved.
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 <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/sysconfig.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
43*7c478bd9Sstevel@tonic-gate #include <elf.h>
44*7c478bd9Sstevel@tonic-gate #include <link.h>
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate #include "alias_boot.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * Local manifest constants and macros.
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate #define	ALIGN(x, a)		((int)(x) & ~((int)(a) - 1))
52*7c478bd9Sstevel@tonic-gate #define	ROUND(x, a)		(((int)(x) + ((int)(a) - 1)) & \
53*7c478bd9Sstevel@tonic-gate 				    ~((int)(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 #include <link.h>
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * Alias ld.so entry point -- receives a bootstrap structure and a vector
70*7c478bd9Sstevel@tonic-gate  * of strings.  The vector is "well-known" to us, and consists of pointers
71*7c478bd9Sstevel@tonic-gate  * to string constants.  This aliasing bootstrap requires no relocation in
72*7c478bd9Sstevel@tonic-gate  * order to run, save for the pointers of constant strings.  This second
73*7c478bd9Sstevel@tonic-gate  * parameter provides this.  Note that this program is carefully coded in
74*7c478bd9Sstevel@tonic-gate  * order to maintain the "no bootstrapping" requirement -- it calls only
75*7c478bd9Sstevel@tonic-gate  * local functions, uses no intrinsics, etc.
76*7c478bd9Sstevel@tonic-gate  */
77*7c478bd9Sstevel@tonic-gate void *
78*7c478bd9Sstevel@tonic-gate __rtld(Elf32_Boot *ebp, const char *strings[], int (*funcs[])())
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 	int i, j, p;			/* working */
81*7c478bd9Sstevel@tonic-gate 	int page_size = 0;		/* size of a page */
82*7c478bd9Sstevel@tonic-gate 	const char *program_name = EMPTY; /* our name */
83*7c478bd9Sstevel@tonic-gate 	int ldfd;			/* fd assigned to ld.so */
84*7c478bd9Sstevel@tonic-gate 	int dzfd = 0;			/* fd assigned to /dev/zero */
85*7c478bd9Sstevel@tonic-gate 	Elf32_Ehdr *ehdr;		/* ELF header of ld.so */
86*7c478bd9Sstevel@tonic-gate 	Elf32_Phdr *phdr;		/* first Phdr in file */
87*7c478bd9Sstevel@tonic-gate 	Elf32_Phdr *pptr;		/* working Phdr */
88*7c478bd9Sstevel@tonic-gate 	Elf32_Phdr *lph;		/* last loadable Phdr */
89*7c478bd9Sstevel@tonic-gate 	Elf32_Phdr *fph = 0;		/* first loadable Phdr */
90*7c478bd9Sstevel@tonic-gate 	caddr_t	maddr;			/* pointer to mapping claim */
91*7c478bd9Sstevel@tonic-gate 	Elf32_Off mlen;			/* total mapping claim */
92*7c478bd9Sstevel@tonic-gate 	caddr_t faddr;			/* first program mapping of ld.so */
93*7c478bd9Sstevel@tonic-gate 	Elf32_Off foff;			/* file offset for segment mapping */
94*7c478bd9Sstevel@tonic-gate 	Elf32_Off flen;			/* file length for segment mapping */
95*7c478bd9Sstevel@tonic-gate 	caddr_t addr;			/* working mapping address */
96*7c478bd9Sstevel@tonic-gate 	caddr_t zaddr;			/* /dev/zero working mapping addr */
97*7c478bd9Sstevel@tonic-gate 	struct stat sb;			/* stat buffer for sizing */
98*7c478bd9Sstevel@tonic-gate 	auxv_t *ap;			/* working aux pointer */
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	/*
101*7c478bd9Sstevel@tonic-gate 	 * Discover things about our environment: auxiliary vector (if
102*7c478bd9Sstevel@tonic-gate 	 * any), arguments, program name, and the like.
103*7c478bd9Sstevel@tonic-gate 	 */
104*7c478bd9Sstevel@tonic-gate 	while (ebp->eb_tag != NULL) {
105*7c478bd9Sstevel@tonic-gate 		switch (ebp->eb_tag) {
106*7c478bd9Sstevel@tonic-gate 		case EB_ARGV:
107*7c478bd9Sstevel@tonic-gate 			program_name = *((char **)ebp->eb_un.eb_ptr);
108*7c478bd9Sstevel@tonic-gate 			break;
109*7c478bd9Sstevel@tonic-gate 		case EB_AUXV:
110*7c478bd9Sstevel@tonic-gate 			for (ap = (auxv_t *)ebp->eb_un.eb_ptr;
111*7c478bd9Sstevel@tonic-gate 			    ap->a_type != AT_NULL; ap++)
112*7c478bd9Sstevel@tonic-gate 				if (ap->a_type == AT_PAGESZ) {
113*7c478bd9Sstevel@tonic-gate 					page_size = ap->a_un.a_val;
114*7c478bd9Sstevel@tonic-gate 					break;
115*7c478bd9Sstevel@tonic-gate 				}
116*7c478bd9Sstevel@tonic-gate 			break;
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 		ebp++;
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	/*
122*7c478bd9Sstevel@tonic-gate 	 * If we didn't get a page size from looking in the auxiliary
123*7c478bd9Sstevel@tonic-gate 	 * vector, we need to get one now.
124*7c478bd9Sstevel@tonic-gate 	 */
125*7c478bd9Sstevel@tonic-gate 	if (page_size == 0) {
126*7c478bd9Sstevel@tonic-gate 		page_size = SYSCONFIG(_CONFIG_PAGESIZE);
127*7c478bd9Sstevel@tonic-gate 		ebp->eb_tag = EB_PAGESIZE, (ebp++)->eb_un.eb_val =
128*7c478bd9Sstevel@tonic-gate 		    (Elf32_Word)page_size;
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	/*
132*7c478bd9Sstevel@tonic-gate 	 * Map in the real ld.so.  Note that we're mapping it as
133*7c478bd9Sstevel@tonic-gate 	 * an ELF database, not as a program -- we just want to walk it's
134*7c478bd9Sstevel@tonic-gate 	 * data structures.  Further mappings will actually establish the
135*7c478bd9Sstevel@tonic-gate 	 * program in the address space.
136*7c478bd9Sstevel@tonic-gate 	 */
137*7c478bd9Sstevel@tonic-gate 	if ((ldfd = OPEN(LDSO, O_RDONLY)) == -1)
138*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
139*7c478bd9Sstevel@tonic-gate /* NEEDSWORK (temp kludge to use xstat so we can run on G6) */
140*7c478bd9Sstevel@tonic-gate 	if (FSTAT(2, ldfd, &sb) == -1)
141*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
142*7c478bd9Sstevel@tonic-gate 	ehdr = (Elf32_Ehdr *)MMAP(0, sb.st_size, PROT_READ | PROT_EXEC,
143*7c478bd9Sstevel@tonic-gate 	    MAP_SHARED, ldfd, 0);
144*7c478bd9Sstevel@tonic-gate 	if (ehdr == (Elf32_Ehdr *)-1)
145*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	/*
148*7c478bd9Sstevel@tonic-gate 	 * Validate the file we're looking at, ensure it has the correct
149*7c478bd9Sstevel@tonic-gate 	 * ELF structures, such as: ELF magic numbers, coded for 386,
150*7c478bd9Sstevel@tonic-gate 	 * is a ".so", etc.
151*7c478bd9Sstevel@tonic-gate 	 */
152*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
153*7c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
154*7c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
155*7c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3)
156*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
157*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
158*7c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
159*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
160*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_type != ET_DYN)
161*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
162*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_machine != EM_386)
163*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
164*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_version > EV_CURRENT)
165*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	/*
168*7c478bd9Sstevel@tonic-gate 	 * Point at program headers and start figuring out what to load.
169*7c478bd9Sstevel@tonic-gate 	 */
170*7c478bd9Sstevel@tonic-gate 	phdr = (Elf32_Phdr *)((caddr_t)ehdr + ehdr->e_phoff);
171*7c478bd9Sstevel@tonic-gate 	for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++,
172*7c478bd9Sstevel@tonic-gate 	    pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize))
173*7c478bd9Sstevel@tonic-gate 		if (pptr->p_type == PT_LOAD) {
174*7c478bd9Sstevel@tonic-gate 			if (fph == 0) {
175*7c478bd9Sstevel@tonic-gate 				fph = pptr;
176*7c478bd9Sstevel@tonic-gate 			} else if (pptr->p_vaddr <= lph->p_vaddr)
177*7c478bd9Sstevel@tonic-gate 				PANIC(program_name);
178*7c478bd9Sstevel@tonic-gate 			lph = pptr;
179*7c478bd9Sstevel@tonic-gate 		}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	/*
182*7c478bd9Sstevel@tonic-gate 	 * We'd better have at least one loadable segment.
183*7c478bd9Sstevel@tonic-gate 	 */
184*7c478bd9Sstevel@tonic-gate 	if (fph == 0)
185*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	/*
188*7c478bd9Sstevel@tonic-gate 	 * Map enough address space to hold the program (as opposed to the
189*7c478bd9Sstevel@tonic-gate 	 * file) represented by ld.so.  The amount to be assigned is the
190*7c478bd9Sstevel@tonic-gate 	 * range between the end of the last loadable segment and the
191*7c478bd9Sstevel@tonic-gate 	 * beginning of the first PLUS the alignment of the first segment.
192*7c478bd9Sstevel@tonic-gate 	 * mmap() can assign us any page-aligned address, but the relocations
193*7c478bd9Sstevel@tonic-gate 	 * assume the alignments included in the program header.  As an
194*7c478bd9Sstevel@tonic-gate 	 * optimization, however, let's assume that mmap() will actually
195*7c478bd9Sstevel@tonic-gate 	 * give us an aligned address -- since if it does, we can save
196*7c478bd9Sstevel@tonic-gate 	 * an munmap() later on.  If it doesn't -- then go try it again.
197*7c478bd9Sstevel@tonic-gate 	 */
198*7c478bd9Sstevel@tonic-gate 	mlen = ROUND((lph->p_vaddr + lph->p_memsz) -
199*7c478bd9Sstevel@tonic-gate 	    ALIGN(fph->p_vaddr, page_size), page_size);
200*7c478bd9Sstevel@tonic-gate 	maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC,
201*7c478bd9Sstevel@tonic-gate 	    MAP_SHARED, ldfd, 0);
202*7c478bd9Sstevel@tonic-gate 	if (maddr == (caddr_t)-1)
203*7c478bd9Sstevel@tonic-gate 		PANIC(program_name);
204*7c478bd9Sstevel@tonic-gate 	faddr = (caddr_t)ROUND(maddr, fph->p_align);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	/*
207*7c478bd9Sstevel@tonic-gate 	 * Check to see whether alignment skew was really needed.
208*7c478bd9Sstevel@tonic-gate 	 */
209*7c478bd9Sstevel@tonic-gate 	if (faddr != maddr) {
210*7c478bd9Sstevel@tonic-gate 		(void) MUNMAP(maddr, mlen);
211*7c478bd9Sstevel@tonic-gate 		mlen = ROUND((lph->p_vaddr + lph->p_memsz) -
212*7c478bd9Sstevel@tonic-gate 		    ALIGN(fph->p_vaddr, fph->p_align) + fph->p_align,
213*7c478bd9Sstevel@tonic-gate 		    page_size);
214*7c478bd9Sstevel@tonic-gate 		maddr = (caddr_t)MMAP(0, mlen, PROT_READ | PROT_EXEC,
215*7c478bd9Sstevel@tonic-gate 		    MAP_SHARED, ldfd, 0);
216*7c478bd9Sstevel@tonic-gate 		if (maddr == (caddr_t)-1)
217*7c478bd9Sstevel@tonic-gate 			PANIC(program_name);
218*7c478bd9Sstevel@tonic-gate 		faddr = (caddr_t)ROUND(maddr, fph->p_align);
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	/*
222*7c478bd9Sstevel@tonic-gate 	 * We have the address space reserved, so map each loadable segment.
223*7c478bd9Sstevel@tonic-gate 	 */
224*7c478bd9Sstevel@tonic-gate 	for (p = 0, pptr = phdr; p < (int)ehdr->e_phnum; p++,
225*7c478bd9Sstevel@tonic-gate 	    pptr = (Elf32_Phdr *)((caddr_t)pptr + ehdr->e_phentsize)) {
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 		/*
228*7c478bd9Sstevel@tonic-gate 		 * Skip non-loadable segments or segments that don't occupy
229*7c478bd9Sstevel@tonic-gate 		 * any memory.
230*7c478bd9Sstevel@tonic-gate 		 */
231*7c478bd9Sstevel@tonic-gate 		if ((pptr->p_type != PT_LOAD) || (pptr->p_memsz == 0))
232*7c478bd9Sstevel@tonic-gate 			continue;
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 		/*
235*7c478bd9Sstevel@tonic-gate 		 * Determine the file offset to which the mapping will
236*7c478bd9Sstevel@tonic-gate 		 * directed (must be aligned) and how much to map (might
237*7c478bd9Sstevel@tonic-gate 		 * be more than the file in the case of .bss.)
238*7c478bd9Sstevel@tonic-gate 		 */
239*7c478bd9Sstevel@tonic-gate 		foff = ALIGN(pptr->p_offset, page_size);
240*7c478bd9Sstevel@tonic-gate 		flen = pptr->p_memsz + (pptr->p_offset - foff);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		/*
243*7c478bd9Sstevel@tonic-gate 		 * Set address of this segment relative to our base.
244*7c478bd9Sstevel@tonic-gate 		 */
245*7c478bd9Sstevel@tonic-gate 		addr = (caddr_t)ALIGN(faddr + pptr->p_vaddr, page_size);
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 		/*
248*7c478bd9Sstevel@tonic-gate 		 * If this is the first program header, record our base
249*7c478bd9Sstevel@tonic-gate 		 * address for later use.
250*7c478bd9Sstevel@tonic-gate 		 */
251*7c478bd9Sstevel@tonic-gate 		if (pptr == phdr) {
252*7c478bd9Sstevel@tonic-gate 			ebp->eb_tag = EB_LDSO_BASE;
253*7c478bd9Sstevel@tonic-gate 			(ebp++)->eb_un.eb_ptr = (Elf32_Addr)addr;
254*7c478bd9Sstevel@tonic-gate 		}
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 		/*
257*7c478bd9Sstevel@tonic-gate 		 * Unmap anything from the last mapping address to this
258*7c478bd9Sstevel@tonic-gate 		 * one.
259*7c478bd9Sstevel@tonic-gate 		 */
260*7c478bd9Sstevel@tonic-gate 		if (addr - maddr) {
261*7c478bd9Sstevel@tonic-gate 			(void) MUNMAP(maddr, addr - maddr);
262*7c478bd9Sstevel@tonic-gate 			mlen -= addr - maddr;
263*7c478bd9Sstevel@tonic-gate 		}
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 		/*
266*7c478bd9Sstevel@tonic-gate 		 * Determine the mapping protection from the section
267*7c478bd9Sstevel@tonic-gate 		 * attributes.
268*7c478bd9Sstevel@tonic-gate 		 */
269*7c478bd9Sstevel@tonic-gate 		i = 0;
270*7c478bd9Sstevel@tonic-gate 		if (pptr->p_flags & PF_R)
271*7c478bd9Sstevel@tonic-gate 			i |= PROT_READ;
272*7c478bd9Sstevel@tonic-gate 		if (pptr->p_flags & PF_W)
273*7c478bd9Sstevel@tonic-gate 			i |= PROT_WRITE;
274*7c478bd9Sstevel@tonic-gate 		if (pptr->p_flags & PF_X)
275*7c478bd9Sstevel@tonic-gate 			i |= PROT_EXEC;
276*7c478bd9Sstevel@tonic-gate 		if ((caddr_t)MMAP((caddr_t)addr, flen, i,
277*7c478bd9Sstevel@tonic-gate 		    MAP_FIXED | MAP_PRIVATE, ldfd, foff) == (caddr_t)-1)
278*7c478bd9Sstevel@tonic-gate 			PANIC(program_name);
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 		/*
281*7c478bd9Sstevel@tonic-gate 		 * If the memory occupancy of the segment overflows the
282*7c478bd9Sstevel@tonic-gate 		 * definition in the file, we need to "zero out" the
283*7c478bd9Sstevel@tonic-gate 		 * end of the mapping we've established, and if necessary,
284*7c478bd9Sstevel@tonic-gate 		 * map some more space from /dev/zero.
285*7c478bd9Sstevel@tonic-gate 		 */
286*7c478bd9Sstevel@tonic-gate 		if (pptr->p_memsz > pptr->p_filesz) {
287*7c478bd9Sstevel@tonic-gate 			foff = (int)faddr + pptr->p_vaddr + 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 	ebp->eb_tag = EB_NULL, ebp->eb_un.eb_val = 0;
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	/* The two bytes before _rt_boot is for the alias entry point */
329*7c478bd9Sstevel@tonic-gate 	return (void *) (ehdr->e_entry + faddr - 2);
330*7c478bd9Sstevel@tonic-gate }
331