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