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