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