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