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