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