xref: /titanic_50/usr/src/cmd/sgs/rtld/sparc/_setup.c (revision 13237b7e1e5bd293e466307b2e06f8e0e2321a0a)
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 2009 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 /*
33  * SPARC 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 
43 #include	<signal.h>
44 #include	<stdlib.h>
45 #include	<sys/auxv.h>
46 #include	<sys/types.h>
47 #include	<sys/stat.h>
48 #include	<link.h>
49 #include	<dlfcn.h>
50 #include	"_rtld.h"
51 #include	"_audit.h"
52 #include	"msg.h"
53 #ifdef A_OUT
54 #include	"_a.out.h"
55 #endif /* A_OUT */
56 
57 /* VARARGS */
58 unsigned long
59 _setup(Boot *ebp, Dyn *ld_dyn)
60 {
61 	ulong_t		reladdr, relacount, ld_base = 0;
62 	ulong_t		relaent = 0;
63 	ulong_t		strtab, soname, interp_base = 0;
64 	char		*_rt_name, **_envp, **_argv;
65 	int		_syspagsz = 0, fd = -1;
66 	uint_t		_flags = 0, hwcap_1 = 0;
67 	Dyn		*dyn_ptr;
68 	Phdr		*phdr = NULL;
69 	Rt_map		*lmp;
70 	auxv_t		*auxv, *_auxv;
71 	uid_t		uid = (uid_t)-1, euid = (uid_t)-1;
72 	gid_t		gid = (gid_t)-1, egid = (gid_t)-1;
73 	char		*_platform = NULL, *_execname = NULL, *_emulator = NULL;
74 	int		auxflags = -1;
75 #ifdef	A_OUT
76 	void		*aoutdyn = NULL;
77 #endif	/* A_OUT */
78 
79 	/*
80 	 * Scan the bootstrap structure to pick up the basics.
81 	 */
82 	for (; ebp->eb_tag != EB_NULL; ebp++)
83 		switch (ebp->eb_tag) {
84 		case EB_DYNAMIC:
85 #ifdef A_OUT
86 			aoutdyn = (Link_dynamic *)ebp->eb_un.eb_val;
87 #endif /* A_OUT */
88 			break;
89 		case EB_LDSO_BASE:
90 			ld_base = (unsigned long)ebp->eb_un.eb_val;
91 			break;
92 		case EB_ARGV:
93 			_argv = (char **)ebp->eb_un.eb_ptr;
94 			break;
95 		case EB_ENVP:
96 			_envp = (char **)ebp->eb_un.eb_ptr;
97 			break;
98 		case EB_AUXV:
99 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
100 			break;
101 		case EB_PAGESIZE:
102 			_syspagsz = (int)ebp->eb_un.eb_val;
103 			break;
104 		}
105 
106 	/*
107 	 * Search the aux. vector for the information passed by exec.
108 	 */
109 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
110 		switch (auxv->a_type) {
111 		case AT_EXECFD:
112 			/* this is the old exec that passes a file descriptor */
113 			fd = (int)auxv->a_un.a_val;
114 			break;
115 		case AT_FLAGS:
116 			/* processor flags (MAU available, etc) */
117 			_flags = auxv->a_un.a_val;
118 			break;
119 		case AT_PAGESZ:
120 			/* system page size */
121 			_syspagsz = (int)auxv->a_un.a_val;
122 			break;
123 		case AT_PHDR:
124 			/* address of the segment table */
125 			phdr = (Phdr *)auxv->a_un.a_ptr;
126 			break;
127 		case AT_BASE:
128 			/* interpreter base address */
129 			if (ld_base == 0)
130 				ld_base = auxv->a_un.a_val;
131 			interp_base = auxv->a_un.a_val;
132 			break;
133 		case AT_SUN_UID:
134 			/* effective user id for the executable */
135 			euid = (uid_t)auxv->a_un.a_val;
136 			break;
137 		case AT_SUN_RUID:
138 			/* real user id for the executable */
139 			uid = (uid_t)auxv->a_un.a_val;
140 			break;
141 		case AT_SUN_GID:
142 			/* effective group id for the executable */
143 			egid = (gid_t)auxv->a_un.a_val;
144 			break;
145 		case AT_SUN_RGID:
146 			/* real group id for the executable */
147 			gid = (gid_t)auxv->a_un.a_val;
148 			break;
149 		case AT_SUN_PLATFORM:
150 			/* platform name */
151 			_platform = auxv->a_un.a_ptr;
152 			break;
153 		case AT_SUN_EXECNAME:
154 			/* full pathname of execed object */
155 			_execname = auxv->a_un.a_ptr;
156 			break;
157 		case AT_SUN_AUXFLAGS:
158 			/* auxiliary flags */
159 			auxflags = (int)auxv->a_un.a_val;
160 			break;
161 		case AT_SUN_HWCAP:
162 			/* hardware capabilities */
163 			hwcap_1 = (uint_t)auxv->a_un.a_val;
164 			break;
165 		case AT_SUN_EMULATOR:
166 			/* name of emulation library, if any */
167 			_emulator = auxv->a_un.a_ptr;
168 			break;
169 		}
170 	}
171 
172 	/*
173 	 * Get needed info from ld.so's dynamic structure.
174 	 */
175 	/* LINTED */
176 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
177 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
178 		switch (ld_dyn->d_tag) {
179 		case DT_RELA:
180 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
181 			break;
182 		case DT_RELACOUNT:
183 			relacount = ld_dyn->d_un.d_val;
184 			break;
185 		case DT_RELAENT:
186 			relaent = ld_dyn->d_un.d_val;
187 			break;
188 		case DT_STRTAB:
189 			strtab = ld_dyn->d_un.d_ptr + ld_base;
190 			break;
191 		case DT_SONAME:
192 			soname = ld_dyn->d_un.d_val;
193 			break;
194 		}
195 	}
196 	_rt_name = (char *)strtab + soname;
197 
198 	/*
199 	 * If we don't have a RELAENT, just assume the size.
200 	 */
201 	if (relaent == 0)
202 		relaent = sizeof (Rela);
203 
204 	/*
205 	 * As all global symbol references within ld.so.1 are protected
206 	 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
207 	 * to process at runtime.  Process all relative relocations now.
208 	 */
209 	for (; relacount; relacount--) {
210 		ulong_t	roffset;
211 
212 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
213 		*((ulong_t *)roffset) = ld_base +
214 		    ((Rela *)reladdr)->r_addend;
215 		reladdr += relaent;
216 	}
217 
218 	/*
219 	 * If an emulation library is being used, use that as the linker's
220 	 * effective executable name. The real executable is not linked by this
221 	 * linker.
222 	 */
223 	if (_emulator != NULL) {
224 		_execname = _emulator;
225 		rtld_flags2 |= RT_FL2_BRANDED;
226 	}
227 
228 	/*
229 	 * Continue with generic startup processing.
230 	 */
231 	/* BEGIN CSTYLED */
232 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
233 	    _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
234 	    _execname, _argv, uid, euid, gid, egid,
235 #ifdef	A_OUT
236 	    aoutdyn, auxflags, hwcap_1)) == NULL) {
237 #else
238 	    /* CSTYLED */
239 	    NULL, auxflags, hwcap_1)) == NULL) {
240 #endif	/* A_OUT */
241 		rtldexit(&lml_main, 1);
242 	}
243 	/* END CSTYLED */
244 
245 	return (LM_ENTRY_PT(lmp)());
246 }
247