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) 2012, Joyent, Inc. All rights reserved.
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>
54 _setup(Boot
*ebp
, Dyn
*ld_dyn
)
56 ulong_t reladdr
, relacount
, ld_base
= 0;
57 ulong_t relaent
= 0, pltrelsz
= 0;
58 ulong_t strtab
, soname
, interp_base
= 0;
59 char *_rt_name
, **_envp
, **_argv
;
60 int _syspagsz
= 0, fd
= -1;
62 uint_t hwcap
[2] = { 0, 0 };
67 uid_t uid
= (uid_t
)-1, euid
= (uid_t
)-1;
68 gid_t gid
= (gid_t
)-1, egid
= (gid_t
)-1;
69 char *_platform
= NULL
, *_execname
= NULL
, *_emulator
= NULL
;
73 * Scan the bootstrap structure to pick up the basics.
75 for (; ebp
->eb_tag
!= EB_NULL
; ebp
++)
76 switch (ebp
->eb_tag
) {
78 ld_base
= (unsigned long)ebp
->eb_un
.eb_val
;
81 _argv
= (char **)ebp
->eb_un
.eb_ptr
;
84 _envp
= (char **)ebp
->eb_un
.eb_ptr
;
87 _auxv
= (auxv_t
*)ebp
->eb_un
.eb_ptr
;
90 _syspagsz
= (int)ebp
->eb_un
.eb_val
;
95 * Search the aux. vector for the information passed by exec.
97 for (auxv
= _auxv
; auxv
->a_type
!= AT_NULL
; auxv
++) {
98 switch (auxv
->a_type
) {
100 /* this is the old exec that passes a file descriptor */
101 fd
= (int)auxv
->a_un
.a_val
;
104 /* processor flags (MAU available, etc) */
105 _flags
= auxv
->a_un
.a_val
;
108 /* system page size */
109 _syspagsz
= (int)auxv
->a_un
.a_val
;
112 /* address of the segment table */
113 phdr
= (Phdr
*)auxv
->a_un
.a_ptr
;
116 /* interpreter base address */
118 ld_base
= auxv
->a_un
.a_val
;
119 interp_base
= auxv
->a_un
.a_val
;
122 /* effective user id for the executable */
123 euid
= (uid_t
)auxv
->a_un
.a_val
;
126 /* real user id for the executable */
127 uid
= (uid_t
)auxv
->a_un
.a_val
;
130 /* effective group id for the executable */
131 egid
= (gid_t
)auxv
->a_un
.a_val
;
134 /* real group id for the executable */
135 gid
= (gid_t
)auxv
->a_un
.a_val
;
137 case AT_SUN_PLATFORM
:
139 _platform
= auxv
->a_un
.a_ptr
;
141 case AT_SUN_EXECNAME
:
142 /* full pathname of execed object */
143 _execname
= auxv
->a_un
.a_ptr
;
145 case AT_SUN_AUXFLAGS
:
146 /* auxiliary flags */
147 auxflags
= (int)auxv
->a_un
.a_val
;
150 /* hardware capabilities */
151 hwcap
[0] = (uint_t
)auxv
->a_un
.a_val
;
154 /* hardware capabilities */
155 hwcap
[1] = (uint_t
)auxv
->a_un
.a_val
;
157 case AT_SUN_EMULATOR
:
158 /* name of emulation library, if any */
159 _emulator
= auxv
->a_un
.a_ptr
;
165 * Get needed info from ld.so's dynamic structure.
168 dyn_ptr
= (Dyn
*)((char *)ld_dyn
+ ld_base
);
169 for (ld_dyn
= dyn_ptr
; ld_dyn
->d_tag
!= DT_NULL
; ld_dyn
++) {
170 switch (ld_dyn
->d_tag
) {
172 reladdr
= ld_dyn
->d_un
.d_ptr
+ ld_base
;
175 relacount
= ld_dyn
->d_un
.d_val
;
178 relaent
= ld_dyn
->d_un
.d_val
;
181 pltrelsz
= ld_dyn
->d_un
.d_val
;
184 strtab
= ld_dyn
->d_un
.d_ptr
+ ld_base
;
187 soname
= ld_dyn
->d_un
.d_val
;
191 _rt_name
= (char *)strtab
+ soname
;
194 * If we don't have a RELAENT, just assume the size.
197 relaent
= sizeof (Rela
);
200 * As all global symbol references within ld.so.1 are protected
201 * (symbolic), only RELATIVE and JMPSLOT relocations should be left
202 * to process at runtime. Process all relocations now.
204 relacount
+= (pltrelsz
/ relaent
);
205 for (; relacount
; relacount
--) {
208 roffset
= ((Rela
*)reladdr
)->r_offset
+ ld_base
;
209 *((ulong_t
*)roffset
) += ld_base
+
210 ((Rela
*)reladdr
)->r_addend
;
215 * If an emulation library is being used, use that as the linker's
216 * effective executable name. The real executable is not linked by this
219 if (_emulator
!= NULL
) {
220 _execname
= _emulator
;
221 rtld_flags2
|= RT_FL2_BRANDED
;
225 * Initialize the dyn_plt_ent_size field. It currently contains the
226 * size of the dyn_plt_template. It still needs to be aligned and have
227 * space for the 'dyn_data' area added.
229 dyn_plt_ent_size
= ROUND(dyn_plt_ent_size
, M_WORD_ALIGN
) +
230 sizeof (uintptr_t) + sizeof (uintptr_t) + sizeof (ulong_t
) +
231 sizeof (ulong_t
) + sizeof (Sym
);
234 * Continue with generic startup processing.
236 if ((lmp
= setup((char **)_envp
, (auxv_t
*)_auxv
, _flags
, _platform
,
237 _syspagsz
, _rt_name
, ld_base
, interp_base
, fd
, phdr
,
238 _execname
, _argv
, uid
, euid
, gid
, egid
, NULL
, auxflags
,
240 rtldexit(&lml_main
, 1);
243 return (LM_ENTRY_PT(lmp
)());