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