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