9596 Initial xsave xstate_bv should not include all features
[unleashed.git] / usr / src / cmd / sgs / rtld / amd64 / _setup.c
blob2a92d72565b778274eda651c519e17e759b3a07a
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright (c) 2018, Joyent, Inc.
31 * amd64 specific setup routine - relocate ld.so's symbols, setup its
32 * environment, map in loadable sections of the executable.
34 * Takes base address ld.so was loaded at, address of ld.so's dynamic
35 * structure, address of process environment pointers, address of auxiliary
36 * vector and * argv[0] (process name).
37 * If errors occur, send process signal - otherwise
38 * return executable's entry point to the bootstrap routine.
41 #include <signal.h>
42 #include <stdlib.h>
43 #include <sys/auxv.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <link.h>
47 #include <dlfcn.h>
48 #include "_rtld.h"
49 #include "_audit.h"
50 #include "msg.h"
53 * Number of bytes to save for register usage.
55 uint_t _plt_save_size;
56 void (*_plt_fp_save)(void *);
57 void (*_plt_fp_restore)(void *);
59 extern void _elf_rtbndr_fp_save_orig(void *);
60 extern void _elf_rtbndr_fp_restore_orig(void *);
61 extern void _elf_rtbndr_fp_fxsave(void *);
62 extern void _elf_rtbndr_fp_fxrestore(void *);
63 extern void _elf_rtbndr_fp_xsave(void *);
64 extern void _elf_rtbndr_fp_xrestore(void *);
67 * Based on what the kernel has told us, go through and set up the various
68 * pointers that we'll need for elf_rtbndr for the FPU.
70 static void
71 _setup_plt_fpu(int kind, size_t len)
74 * If we didn't get a length for some reason, fall back to the old
75 * implementation.
77 if (len == 0)
78 kind = -1;
80 switch (kind) {
81 case AT_386_FPINFO_FXSAVE:
82 _plt_fp_save = _elf_rtbndr_fp_fxsave;
83 _plt_fp_restore = _elf_rtbndr_fp_fxrestore;
84 _plt_save_size = len;
85 break;
87 * We can treat processors that don't correctly handle the exception
88 * information in xsave the same way we do others. The information
89 * that may or may not be properly saved and restored should not be
90 * relevant to us because of the ABI.
92 case AT_386_FPINFO_XSAVE:
93 case AT_386_FPINFO_XSAVE_AMD:
94 _plt_fp_save = _elf_rtbndr_fp_xsave;
95 _plt_fp_restore = _elf_rtbndr_fp_xrestore;
96 _plt_save_size = len;
97 break;
98 default:
99 _plt_fp_save = _elf_rtbndr_fp_save_orig;
100 _plt_fp_restore = _elf_rtbndr_fp_restore_orig;
102 * The ABI says that 8 floating point registers are used for
103 * passing arguments (%xmm0 through %xmm7). Because these
104 * registers on some platforms may shadow the %ymm and %zmm
105 * registers, we end up needing to size this for the maximally
106 * sized register we care about, a 512-bit (64-byte) zmm
107 * register.
109 _plt_save_size = 64 * 8;
110 break;
114 /* VARARGS */
115 unsigned long
116 _setup(Boot *ebp, Dyn *ld_dyn)
118 ulong_t reladdr, relacount, ld_base = 0;
119 ulong_t relaent = 0, pltrelsz = 0;
120 ulong_t strtab, soname, interp_base = 0;
121 char *_rt_name, **_envp, **_argv;
122 int _syspagsz = 0, fd = -1;
123 uint_t _flags = 0;
124 uint_t hwcap[2] = { 0, 0 };
125 Dyn *dyn_ptr;
126 Phdr *phdr = NULL;
127 Rt_map *lmp;
128 auxv_t *auxv, *_auxv;
129 uid_t uid = (uid_t)-1, euid = (uid_t)-1;
130 gid_t gid = (gid_t)-1, egid = (gid_t)-1;
131 char *_platform = NULL, *_execname = NULL, *_emulator = NULL;
132 int auxflags = -1, fpkind = -1;
133 size_t fpsize = 0;
136 * Scan the bootstrap structure to pick up the basics.
138 for (; ebp->eb_tag != EB_NULL; ebp++)
139 switch (ebp->eb_tag) {
140 case EB_LDSO_BASE:
141 ld_base = (unsigned long)ebp->eb_un.eb_val;
142 break;
143 case EB_ARGV:
144 _argv = (char **)ebp->eb_un.eb_ptr;
145 break;
146 case EB_ENVP:
147 _envp = (char **)ebp->eb_un.eb_ptr;
148 break;
149 case EB_AUXV:
150 _auxv = (auxv_t *)ebp->eb_un.eb_ptr;
151 break;
152 case EB_PAGESIZE:
153 _syspagsz = (int)ebp->eb_un.eb_val;
154 break;
158 * Search the aux. vector for the information passed by exec.
160 for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
161 switch (auxv->a_type) {
162 case AT_EXECFD:
163 /* this is the old exec that passes a file descriptor */
164 fd = (int)auxv->a_un.a_val;
165 break;
166 case AT_FLAGS:
167 /* processor flags (MAU available, etc) */
168 _flags = auxv->a_un.a_val;
169 break;
170 case AT_PAGESZ:
171 /* system page size */
172 _syspagsz = (int)auxv->a_un.a_val;
173 break;
174 case AT_PHDR:
175 /* address of the segment table */
176 phdr = (Phdr *)auxv->a_un.a_ptr;
177 break;
178 case AT_BASE:
179 /* interpreter base address */
180 if (ld_base == 0)
181 ld_base = auxv->a_un.a_val;
182 interp_base = auxv->a_un.a_val;
183 break;
184 case AT_SUN_UID:
185 /* effective user id for the executable */
186 euid = (uid_t)auxv->a_un.a_val;
187 break;
188 case AT_SUN_RUID:
189 /* real user id for the executable */
190 uid = (uid_t)auxv->a_un.a_val;
191 break;
192 case AT_SUN_GID:
193 /* effective group id for the executable */
194 egid = (gid_t)auxv->a_un.a_val;
195 break;
196 case AT_SUN_RGID:
197 /* real group id for the executable */
198 gid = (gid_t)auxv->a_un.a_val;
199 break;
200 case AT_SUN_PLATFORM:
201 /* platform name */
202 _platform = auxv->a_un.a_ptr;
203 break;
204 case AT_SUN_EXECNAME:
205 /* full pathname of execed object */
206 _execname = auxv->a_un.a_ptr;
207 break;
208 case AT_SUN_AUXFLAGS:
209 /* auxiliary flags */
210 auxflags = (int)auxv->a_un.a_val;
211 break;
212 case AT_SUN_HWCAP:
213 /* hardware capabilities */
214 hwcap[0] = (uint_t)auxv->a_un.a_val;
215 break;
216 case AT_SUN_HWCAP2:
217 /* hardware capabilities */
218 hwcap[1] = (uint_t)auxv->a_un.a_val;
219 break;
220 case AT_SUN_EMULATOR:
221 /* name of emulation library, if any */
222 _emulator = auxv->a_un.a_ptr;
223 break;
224 case AT_SUN_FPTYPE:
225 fpkind = (int)auxv->a_un.a_val;
226 break;
227 case AT_SUN_FPSIZE:
228 fpsize = (size_t)auxv->a_un.a_val;
229 break;
234 * Get needed info from ld.so's dynamic structure.
236 /* LINTED */
237 dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
238 for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
239 switch (ld_dyn->d_tag) {
240 case DT_RELA:
241 reladdr = ld_dyn->d_un.d_ptr + ld_base;
242 break;
243 case DT_RELACOUNT:
244 relacount = ld_dyn->d_un.d_val;
245 break;
246 case DT_RELAENT:
247 relaent = ld_dyn->d_un.d_val;
248 break;
249 case DT_PLTRELSZ:
250 pltrelsz = ld_dyn->d_un.d_val;
251 break;
252 case DT_STRTAB:
253 strtab = ld_dyn->d_un.d_ptr + ld_base;
254 break;
255 case DT_SONAME:
256 soname = ld_dyn->d_un.d_val;
257 break;
260 _rt_name = (char *)strtab + soname;
263 * If we don't have a RELAENT, just assume the size.
265 if (relaent == 0)
266 relaent = sizeof (Rela);
269 * As all global symbol references within ld.so.1 are protected
270 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
271 * to process at runtime. Process all relocations now.
273 relacount += (pltrelsz / relaent);
274 for (; relacount; relacount--) {
275 ulong_t roffset;
277 roffset = ((Rela *)reladdr)->r_offset + ld_base;
278 *((ulong_t *)roffset) += ld_base +
279 ((Rela *)reladdr)->r_addend;
280 reladdr += relaent;
284 * If an emulation library is being used, use that as the linker's
285 * effective executable name. The real executable is not linked by this
286 * linker.
288 if (_emulator != NULL) {
289 _execname = _emulator;
290 rtld_flags2 |= RT_FL2_BRANDED;
294 * Initialize the dyn_plt_ent_size field. It currently contains the
295 * size of the dyn_plt_template. It still needs to be aligned and have
296 * space for the 'dyn_data' area added.
298 dyn_plt_ent_size = ROUND(dyn_plt_ent_size, M_WORD_ALIGN) +
299 sizeof (uintptr_t) + sizeof (uintptr_t) + sizeof (ulong_t) +
300 sizeof (ulong_t) + sizeof (Sym);
303 * Initialize the amd64 specific PLT relocation constants based on the
304 * FP information that we have.
306 _setup_plt_fpu(fpkind, fpsize);
309 * Continue with generic startup processing.
311 if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
312 _syspagsz, _rt_name, ld_base, interp_base, fd, phdr,
313 _execname, _argv, uid, euid, gid, egid, NULL, auxflags,
314 hwcap)) == NULL) {
315 rtldexit(&lml_main, 1);
318 return (LM_ENTRY_PT(lmp)());