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]
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.
44 #include <sys/types.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.
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
81 case AT_386_FPINFO_FXSAVE
:
82 _plt_fp_save
= _elf_rtbndr_fp_fxsave
;
83 _plt_fp_restore
= _elf_rtbndr_fp_fxrestore
;
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
;
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
109 _plt_save_size
= 64 * 8;
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;
124 uint_t hwcap
[2] = { 0, 0 };
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;
136 * Scan the bootstrap structure to pick up the basics.
138 for (; ebp
->eb_tag
!= EB_NULL
; ebp
++)
139 switch (ebp
->eb_tag
) {
141 ld_base
= (unsigned long)ebp
->eb_un
.eb_val
;
144 _argv
= (char **)ebp
->eb_un
.eb_ptr
;
147 _envp
= (char **)ebp
->eb_un
.eb_ptr
;
150 _auxv
= (auxv_t
*)ebp
->eb_un
.eb_ptr
;
153 _syspagsz
= (int)ebp
->eb_un
.eb_val
;
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
) {
163 /* this is the old exec that passes a file descriptor */
164 fd
= (int)auxv
->a_un
.a_val
;
167 /* processor flags (MAU available, etc) */
168 _flags
= auxv
->a_un
.a_val
;
171 /* system page size */
172 _syspagsz
= (int)auxv
->a_un
.a_val
;
175 /* address of the segment table */
176 phdr
= (Phdr
*)auxv
->a_un
.a_ptr
;
179 /* interpreter base address */
181 ld_base
= auxv
->a_un
.a_val
;
182 interp_base
= auxv
->a_un
.a_val
;
185 /* effective user id for the executable */
186 euid
= (uid_t
)auxv
->a_un
.a_val
;
189 /* real user id for the executable */
190 uid
= (uid_t
)auxv
->a_un
.a_val
;
193 /* effective group id for the executable */
194 egid
= (gid_t
)auxv
->a_un
.a_val
;
197 /* real group id for the executable */
198 gid
= (gid_t
)auxv
->a_un
.a_val
;
200 case AT_SUN_PLATFORM
:
202 _platform
= auxv
->a_un
.a_ptr
;
204 case AT_SUN_EXECNAME
:
205 /* full pathname of execed object */
206 _execname
= auxv
->a_un
.a_ptr
;
208 case AT_SUN_AUXFLAGS
:
209 /* auxiliary flags */
210 auxflags
= (int)auxv
->a_un
.a_val
;
213 /* hardware capabilities */
214 hwcap
[0] = (uint_t
)auxv
->a_un
.a_val
;
217 /* hardware capabilities */
218 hwcap
[1] = (uint_t
)auxv
->a_un
.a_val
;
220 case AT_SUN_EMULATOR
:
221 /* name of emulation library, if any */
222 _emulator
= auxv
->a_un
.a_ptr
;
225 fpkind
= (int)auxv
->a_un
.a_val
;
228 fpsize
= (size_t)auxv
->a_un
.a_val
;
234 * Get needed info from ld.so's dynamic structure.
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
) {
241 reladdr
= ld_dyn
->d_un
.d_ptr
+ ld_base
;
244 relacount
= ld_dyn
->d_un
.d_val
;
247 relaent
= ld_dyn
->d_un
.d_val
;
250 pltrelsz
= ld_dyn
->d_un
.d_val
;
253 strtab
= ld_dyn
->d_un
.d_ptr
+ ld_base
;
256 soname
= ld_dyn
->d_un
.d_val
;
260 _rt_name
= (char *)strtab
+ soname
;
263 * If we don't have a RELAENT, just assume the size.
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
--) {
277 roffset
= ((Rela
*)reladdr
)->r_offset
+ ld_base
;
278 *((ulong_t
*)roffset
) += ld_base
+
279 ((Rela
*)reladdr
)->r_addend
;
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
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
,
315 rtldexit(&lml_main
, 1);
318 return (LM_ENTRY_PT(lmp
)());