xref: /titanic_44/usr/src/cmd/sgs/rtld/sparcv9/_setup.c (revision b17f03d7d89b75b69b9b7db22f2316b700e3a5a8)
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  * SPARCV9-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_LDSO_BASE:
83 			ld_base = (unsigned long)ebp->eb_un.eb_val;
84 			break;
85 		case EB_ARGV:
86 			_argv = (char **)ebp->eb_un.eb_ptr;
87 			break;
88 		case EB_ENVP:
89 			_envp = (char **)ebp->eb_un.eb_ptr;
90 			break;
91 		case EB_AUXV:
92 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
93 			break;
94 		case EB_PAGESIZE:
95 			_syspagsz = (int)ebp->eb_un.eb_val;
96 			break;
97 		}
98 
99 	/*
100 	 * Search the aux. vector for the information passed by exec.
101 	 */
102 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
103 		switch (auxv->a_type) {
104 		case AT_EXECFD:
105 			/* this is the old exec that passes a file descriptor */
106 			fd = (int)auxv->a_un.a_val;
107 			break;
108 		case AT_FLAGS:
109 			/* processor flags (MAU available, etc) */
110 			_flags = auxv->a_un.a_val;
111 			break;
112 		case AT_PAGESZ:
113 			/* system page size */
114 			_syspagsz = (int)auxv->a_un.a_val;
115 			break;
116 		case AT_PHDR:
117 			/* address of the segment table */
118 			phdr = (Phdr *)auxv->a_un.a_ptr;
119 			break;
120 		case AT_BASE:
121 			/* interpreter base address */
122 			if (ld_base == 0)
123 				ld_base = auxv->a_un.a_val;
124 			interp_base = auxv->a_un.a_val;
125 			break;
126 		case AT_SUN_UID:
127 			/* effective user id for the executable */
128 			euid = (uid_t)auxv->a_un.a_val;
129 			break;
130 		case AT_SUN_RUID:
131 			/* real user id for the executable */
132 			uid = (uid_t)auxv->a_un.a_val;
133 			break;
134 		case AT_SUN_GID:
135 			/* effective group id for the executable */
136 			egid = (gid_t)auxv->a_un.a_val;
137 			break;
138 		case AT_SUN_RGID:
139 			/* real group id for the executable */
140 			gid = (gid_t)auxv->a_un.a_val;
141 			break;
142 		case AT_SUN_PLATFORM:
143 			/* platform name */
144 			_platform = auxv->a_un.a_ptr;
145 			break;
146 		case AT_SUN_EXECNAME:
147 			/* full pathname of execed object */
148 			_execname = auxv->a_un.a_ptr;
149 			break;
150 		case AT_SUN_AUXFLAGS:
151 			/* auxiliary flags */
152 			auxflags = (int)auxv->a_un.a_val;
153 			break;
154 		case AT_SUN_HWCAP:
155 			/* hardware capabilities */
156 			hwcap[0] = (uint_t)auxv->a_un.a_val;
157 			break;
158 		case AT_SUN_HWCAP2:
159 			/* hardware capabilities */
160 			hwcap[1] = (uint_t)auxv->a_un.a_val;
161 			break;
162 		case AT_SUN_EMULATOR:
163 			/* name of emulation library, if any */
164 			_emulator = auxv->a_un.a_ptr;
165 			break;
166 		}
167 	}
168 
169 	/*
170 	 * Get needed info from ld.so's dynamic structure.
171 	 */
172 	/* LINTED */
173 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
174 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
175 		switch (ld_dyn->d_tag) {
176 		case DT_RELA:
177 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
178 			break;
179 		case DT_RELACOUNT:
180 			relacount = ld_dyn->d_un.d_val;
181 			break;
182 		case DT_RELAENT:
183 			relaent = ld_dyn->d_un.d_val;
184 			break;
185 		case DT_STRTAB:
186 			strtab = ld_dyn->d_un.d_ptr + ld_base;
187 			break;
188 		case DT_SONAME:
189 			soname = ld_dyn->d_un.d_val;
190 			break;
191 		}
192 	}
193 	_rt_name = (char *)strtab + soname;
194 
195 	/*
196 	 * If we don't have a RELAENT, just assume the size.
197 	 */
198 	if (relaent == 0)
199 		relaent = sizeof (Rela);
200 
201 	/*
202 	 * As all global symbol references within ld.so.1 are protected
203 	 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
204 	 * to process at runtime.  Process all relative relocations now.
205 	 */
206 	for (; relacount; relacount--) {
207 		ulong_t	roffset;
208 
209 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
210 		*((ulong_t *)roffset) = ld_base +
211 		    ((Rela *)reladdr)->r_addend;
212 		reladdr += relaent;
213 	}
214 
215 	/*
216 	 * If an emulation library is being used, use that as the linker's
217 	 * effective executable name. The real executable is not linked by this
218 	 * linker.
219 	 */
220 	if (_emulator != NULL) {
221 		_execname = _emulator;
222 		rtld_flags2 |= RT_FL2_BRANDED;
223 	}
224 
225 	/*
226 	 * Continue with generic startup processing.
227 	 */
228 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
229 	    _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
230 	    _execname, _argv, uid, euid, gid, egid, NULL, auxflags,
231 	    hwcap)) == NULL) {
232 		rtldexit(&lml_main, 1);
233 	}
234 
235 	return (LM_ENTRY_PT(lmp)());
236 }
237