xref: /titanic_52/usr/src/cmd/sgs/rtld/i386/_setup.c (revision 71269a2275bf5a143dad6461eee2710a344e7261)
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  * i386 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	<sys/auxv.h>
48 #include	<sys/types.h>
49 #include	<sys/stat.h>
50 #include	<link.h>
51 #include	<dlfcn.h>
52 #include	<debug.h>
53 #include	"_rtld.h"
54 #include	"_audit.h"
55 #include	"msg.h"
56 
57 extern int	_end;
58 extern int	_etext;
59 
60 
61 /*
62  * Stub routine to prevent atexit_init() being extracted from libc_pic.a on
63  * i386 and added to ld.so.1.  We don't need it.
64  */
65 void
66 atexit_init()
67 {
68 }
69 
70 /* VARARGS */
71 unsigned long
72 _setup(Boot * ebp, Dyn * ld_dyn)
73 {
74 	unsigned long	reladdr, relcount, ld_base = 0;
75 	unsigned long	relent = 0;
76 	unsigned long	strtab, soname, interp_base = 0;
77 	char		*_rt_name, **_envp, **_argv;
78 	int		_syspagsz = 0, fd = -1, dz_fd = FD_UNAVAIL;
79 	uint_t		_flags = 0, hwcap_1 = 0;
80 	Dyn *		dyn_ptr;
81 	Phdr *		phdr = 0;
82 	Rt_map *	lmp;
83 	auxv_t		*auxv, *_auxv;
84 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
85 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
86 	char		*_platform = 0, *_execname = 0, *_emulator = 0;
87 	int		auxflags = -1;
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_LDSO_BASE:
94 			ld_base = (unsigned long)ebp->eb_un.eb_val;
95 			break;
96 		case EB_ARGV:
97 			_argv = (char **)ebp->eb_un.eb_ptr;
98 			break;
99 		case EB_ENVP:
100 			_envp = (char **)ebp->eb_un.eb_ptr;
101 			break;
102 		case EB_AUXV:
103 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
104 			break;
105 		case EB_DEVZERO:
106 			dz_fd = (int)ebp->eb_un.eb_val;
107 			break;
108 		case EB_PAGESIZE:
109 			_syspagsz = (int)ebp->eb_un.eb_val;
110 			break;
111 		}
112 
113 	/*
114 	 * Search the aux. vector for the information passed by exec.
115 	 */
116 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
117 		switch (auxv->a_type) {
118 		case AT_EXECFD:
119 			/* this is the old exec that passes a file descriptor */
120 			fd = (int)auxv->a_un.a_val;
121 			break;
122 		case AT_FLAGS:
123 			/* processor flags (MAU available, etc) */
124 			_flags = auxv->a_un.a_val;
125 			break;
126 		case AT_PAGESZ:
127 			/* system page size */
128 			_syspagsz = (int)auxv->a_un.a_val;
129 			break;
130 		case AT_PHDR:
131 			/* address of the segment table */
132 			phdr = (Phdr *)auxv->a_un.a_ptr;
133 			break;
134 		case AT_BASE:
135 			/* interpreter base address */
136 			if (ld_base == 0)
137 				ld_base = auxv->a_un.a_val;
138 			interp_base = auxv->a_un.a_val;
139 			break;
140 		case AT_SUN_UID:
141 			/* effective user id for the executable */
142 			euid = (uid_t)auxv->a_un.a_val;
143 			break;
144 		case AT_SUN_RUID:
145 			/* real user id for the executable */
146 			uid = (uid_t)auxv->a_un.a_val;
147 			break;
148 		case AT_SUN_GID:
149 			/* effective group id for the executable */
150 			egid = (gid_t)auxv->a_un.a_val;
151 			break;
152 		case AT_SUN_RGID:
153 			/* real group id for the executable */
154 			gid = (gid_t)auxv->a_un.a_val;
155 			break;
156 #ifdef	AT_SUN_PLATFORM			/* Defined on SunOS 5.5 & greater. */
157 		case AT_SUN_PLATFORM:
158 			/* platform name */
159 			_platform = auxv->a_un.a_ptr;
160 			break;
161 #endif
162 #ifdef	AT_SUN_EXECNAME			/* Defined on SunOS 5.6 & greater. */
163 		case AT_SUN_EXECNAME:
164 			/* full pathname of execed object */
165 			_execname = auxv->a_un.a_ptr;
166 			break;
167 #endif
168 #ifdef	AT_SUN_AUXFLAGS			/* At the behest of PSARC 2002/188 */
169 		case AT_SUN_AUXFLAGS:
170 			auxflags = (int)auxv->a_un.a_val;
171 			break;
172 #endif
173 #ifdef	AT_SUN_HWCAP			/* Hardware capabilities */
174 		case AT_SUN_HWCAP:
175 			hwcap_1 = (uint_t)auxv->a_un.a_val;
176 			break;
177 #endif
178 #ifdef	AT_SUN_EMULATOR			/* Emulation library name */
179 		case AT_SUN_EMULATOR:
180 			/* name of emulation library, if any */
181 			_emulator = auxv->a_un.a_ptr;
182 			break;
183 #endif
184 		}
185 	}
186 
187 	/*
188 	 * Get needed info from ld.so's dynamic structure.
189 	 */
190 	/* LINTED */
191 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
192 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
193 		switch (ld_dyn->d_tag) {
194 		case DT_REL:
195 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
196 			break;
197 		case DT_RELCOUNT:
198 			relcount = ld_dyn->d_un.d_val;
199 			break;
200 		case DT_RELENT:
201 			relent = ld_dyn->d_un.d_val;
202 			break;
203 		case DT_STRTAB:
204 			strtab = ld_dyn->d_un.d_ptr + ld_base;
205 			break;
206 		case DT_SONAME:
207 			soname = ld_dyn->d_un.d_val;
208 			break;
209 		}
210 	}
211 	_rt_name = (char *)strtab + soname;
212 
213 	/*
214 	 * If we don't have a RELENT, just assume
215 	 * the size.
216 	 */
217 	if (relent == 0)
218 		relent = sizeof (Rel);
219 
220 	/*
221 	 * Relocate all symbols in ld.so.
222 	 *
223 	 * Because ld.so.1 is built with -Bsymbolic there should only be
224 	 * RELATIVE and JMPSLOT relocations, both of which get relative
225 	 * additions against them.
226 	 */
227 	for (; relcount; relcount--) {
228 		ulong_t	roffset;
229 
230 		roffset = ((Rel *)reladdr)->r_offset + ld_base;
231 		*((ulong_t *)roffset) += ld_base;
232 		reladdr += relent;
233 	}
234 
235 	/*
236 	 * If an emulation library is being used, use that as the linker's
237 	 * effective executable name. The real executable is not linked by this
238 	 * linker.
239 	 */
240 	if (_emulator != NULL) {
241 		_execname = _emulator;
242 		rtld_flags2 |= RT_FL2_BRANDED;
243 	}
244 
245 	/*
246 	 * Initialize the dyn_plt_ent_size field.  It currently contains the
247 	 * size of the dyn_plt_template.  It still needs to be aligned and have
248 	 * space for the 'dyn_data' area added.
249 	 */
250 	dyn_plt_ent_size = ROUND(dyn_plt_ent_size, M_WORD_ALIGN) +
251 	    sizeof (uintptr_t) + sizeof (uintptr_t) + sizeof (ulong_t) +
252 	    sizeof (ulong_t) + sizeof (Sym);
253 
254 	/*
255 	 * Continue with generic startup processing.
256 	 */
257 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
258 	    _syspagsz, _rt_name, dyn_ptr, ld_base, interp_base, fd, phdr,
259 	    _execname, _argv, dz_fd, uid, euid, gid, egid, NULL, auxflags,
260 	    hwcap_1)) == NULL) {
261 		rtldexit(&lml_main, 1);
262 	}
263 
264 	return (LM_ENTRY_PT(lmp)());
265 }
266