xref: /titanic_52/usr/src/cmd/sgs/rtld/sparc/_setup.c (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
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 /*
28  *	Copyright (c) 1988 AT&T
29  *	  All Rights Reserved
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 /*
35  * SPARC specific setup routine  -  relocate ld.so's symbols, setup its
36  * environment, map in loadable sections of the executable.
37  *
38  * Takes base address ld.so was loaded at, address of ld.so's dynamic
39  * structure, address of process environment pointers, address of auxiliary
40  * vector and * argv[0] (process name).
41  * If errors occur, send process signal - otherwise
42  * return executable's entry point to the bootstrap routine.
43  */
44 
45 #include	<signal.h>
46 #include	<stdlib.h>
47 #include	<fcntl.h>
48 #include	<stdio.h>
49 #include	<sys/auxv.h>
50 #include	<sys/types.h>
51 #include	<sys/stat.h>
52 #include	<link.h>
53 #include	<dlfcn.h>
54 #include	"_rtld.h"
55 #include	"_audit.h"
56 #include	"msg.h"
57 #ifdef A_OUT
58 #include	"_a.out.h"
59 #endif /* A_OUT */
60 
61 extern int	_end;
62 extern int	_etext;
63 extern void	_init(void);
64 
65 
66 /* VARARGS */
67 unsigned long
68 _setup(Boot * ebp, Dyn * ld_dyn)
69 {
70 	unsigned long	reladdr, relacount, ld_base = 0;
71 	unsigned long	relaent = 0;
72 	unsigned long	strtab, soname, interp_base = 0;
73 	char		*_rt_name, **_envp, **_argv;
74 	int		_syspagsz = 0, fd = -1, dz_fd = FD_UNAVAIL;
75 	uint_t		_flags = 0, hwcap_1 = 0;
76 	Dyn *		dyn_ptr;
77 	Phdr *		phdr = 0;
78 	Rt_map *	lmp;
79 	auxv_t		*auxv, *_auxv;
80 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
81 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
82 	char		*_platform = 0, *_execname = 0, *_emulator = 0;
83 	int		auxflags = -1;
84 #ifdef	A_OUT
85 	void *		aoutdyn = 0;
86 #endif	/* A_OUT */
87 
88 	/*
89 	 * Scan the bootstrap structure to pick up the basics.
90 	 */
91 	for (; ebp->eb_tag != EB_NULL; ebp++)
92 		switch (ebp->eb_tag) {
93 		case EB_DYNAMIC:
94 #ifdef A_OUT
95 			aoutdyn = (struct link_dynamic *)ebp->eb_un.eb_val;
96 #endif /* A_OUT */
97 			break;
98 		case EB_LDSO_BASE:
99 			ld_base = (unsigned long)ebp->eb_un.eb_val;
100 			break;
101 		case EB_ARGV:
102 			_argv = (char **)ebp->eb_un.eb_ptr;
103 			break;
104 		case EB_ENVP:
105 			_envp = (char **)ebp->eb_un.eb_ptr;
106 			break;
107 		case EB_AUXV:
108 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
109 			break;
110 		case EB_DEVZERO:
111 			dz_fd = (int)ebp->eb_un.eb_val;
112 			break;
113 		case EB_PAGESIZE:
114 			_syspagsz = (int)ebp->eb_un.eb_val;
115 			break;
116 		}
117 
118 	/*
119 	 * Search the aux. vector for the information passed by exec.
120 	 */
121 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
122 		switch (auxv->a_type) {
123 		case AT_EXECFD:
124 			/* this is the old exec that passes a file descriptor */
125 			fd = (int)auxv->a_un.a_val;
126 			break;
127 		case AT_FLAGS:
128 			/* processor flags (MAU available, etc) */
129 			_flags = auxv->a_un.a_val;
130 			break;
131 		case AT_PAGESZ:
132 			/* system page size */
133 			_syspagsz = (int)auxv->a_un.a_val;
134 			break;
135 		case AT_PHDR:
136 			/* address of the segment table */
137 			phdr = (Phdr *)auxv->a_un.a_ptr;
138 			break;
139 		case AT_BASE:
140 			/* interpreter base address */
141 			if (ld_base == 0)
142 				ld_base = auxv->a_un.a_val;
143 			interp_base = auxv->a_un.a_val;
144 			break;
145 		case AT_SUN_UID:
146 			/* effective user id for the executable */
147 			euid = (uid_t)auxv->a_un.a_val;
148 			break;
149 		case AT_SUN_RUID:
150 			/* real user id for the executable */
151 			uid = (uid_t)auxv->a_un.a_val;
152 			break;
153 		case AT_SUN_GID:
154 			/* effective group id for the executable */
155 			egid = (gid_t)auxv->a_un.a_val;
156 			break;
157 		case AT_SUN_RGID:
158 			/* real group id for the executable */
159 			gid = (gid_t)auxv->a_un.a_val;
160 			break;
161 #ifdef	AT_SUN_PLATFORM			/* Defined on SunOS 5.5 & greater. */
162 		case AT_SUN_PLATFORM:
163 			/* platform name */
164 			_platform = auxv->a_un.a_ptr;
165 			break;
166 #endif
167 #ifdef	AT_SUN_EXECNAME			/* Defined on SunOS 5.6 & greater. */
168 		case AT_SUN_EXECNAME:
169 			/* full pathname of execed object */
170 			_execname = auxv->a_un.a_ptr;
171 			break;
172 #endif
173 #ifdef	AT_SUN_AUXFLAGS			/* At the behest of PSARC 2002/188 */
174 		case AT_SUN_AUXFLAGS:
175 			auxflags = (int)auxv->a_un.a_val;
176 			break;
177 #endif
178 #ifdef	AT_SUN_HWCAP			/* Hardware capabilities */
179 		case AT_SUN_HWCAP:
180 			hwcap_1 = (uint_t)auxv->a_un.a_val;
181 			break;
182 #endif
183 #ifdef  AT_SUN_EMULATOR			/* Emulation library name */
184 		case AT_SUN_EMULATOR:
185 			/* name of emulation library, if any */
186 			_emulator = auxv->a_un.a_ptr;
187 			break;
188 #endif
189 		}
190 	}
191 
192 	/*
193 	 * Get needed info from ld.so's dynamic structure.
194 	 */
195 	/* LINTED */
196 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
197 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
198 		switch (ld_dyn->d_tag) {
199 		case DT_RELA:
200 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
201 			break;
202 		case DT_RELACOUNT:
203 			relacount = ld_dyn->d_un.d_val;
204 			break;
205 		case DT_RELAENT:
206 			relaent = ld_dyn->d_un.d_val;
207 			break;
208 		case DT_STRTAB:
209 			strtab = ld_dyn->d_un.d_ptr + ld_base;
210 			break;
211 		case DT_SONAME:
212 			soname = ld_dyn->d_un.d_val;
213 			break;
214 		}
215 	}
216 	_rt_name = (char *)strtab + soname;
217 
218 	/*
219 	 * If we don't have a RELAENT, just assume
220 	 * the size.
221 	 */
222 	if (relaent == 0)
223 		relaent = sizeof (Rela);
224 
225 	/*
226 	 * Because ld.so.1 is built with -Bsymbolic there should only be
227 	 * RELATIVE and JMPSLOT relocations.  Process all relatives first.
228 	 */
229 	for (; relacount; relacount--) {
230 		ulong_t	roffset;
231 
232 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
233 		*((ulong_t *)roffset) = ld_base +
234 		    (long)(((Rela *)reladdr)->r_addend);
235 		reladdr += relaent;
236 	}
237 
238 	/*
239 	 * If an emulation library is being used, use that as the linker's
240 	 * effective executable name. The real executable is not linked by this
241 	 * linker.
242 	 */
243 	if (_emulator != NULL) {
244 		_execname = _emulator;
245 		rtld_flags2 |= RT_FL2_BRANDED;
246 	}
247 
248 	/*
249 	 * Continue with generic startup processing.
250 	 */
251 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
252 	    _syspagsz, _rt_name, dyn_ptr, ld_base, interp_base, fd, phdr,
253 	    _execname, _argv, dz_fd, uid, euid, gid, egid,
254 #ifdef	A_OUT
255 	    aoutdyn, auxflags, hwcap_1)) == (Rt_map *)0) {
256 #else
257 	    /* CSTYLED */
258 	    NULL, auxflags, hwcap_1)) == NULL) {
259 #endif	/* A_OUT */
260 		rtldexit(&lml_main, 1);
261 	}
262 
263 	return (LM_ENTRY_PT(lmp)());
264 }
265