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