xref: /titanic_52/usr/src/cmd/sgs/rtld/amd64/_setup.c (revision 799823bbed51a695d01e13511bbb1369980bb714)
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  * Copyright (c) 2012, Joyent, Inc.  All rights reserved.
28  */
29 
30 /*
31  * amd64 specific setup routine  -  relocate ld.so's symbols, setup its
32  * environment, map in loadable sections of the executable.
33  *
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.
39  */
40 
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"
51 
52 /* VARARGS */
53 unsigned long
54 _setup(Boot *ebp, Dyn *ld_dyn)
55 {
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;
61 	uint_t		_flags = 0;
62 	uint_t		hwcap[2] = { 0, 0 };
63 	Dyn		*dyn_ptr;
64 	Phdr		*phdr = NULL;
65 	Rt_map		*lmp;
66 	auxv_t		*auxv, *_auxv;
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;
70 	int		auxflags = -1;
71 
72 	/*
73 	 * Scan the bootstrap structure to pick up the basics.
74 	 */
75 	for (; ebp->eb_tag != EB_NULL; ebp++)
76 		switch (ebp->eb_tag) {
77 		case EB_LDSO_BASE:
78 			ld_base = (unsigned long)ebp->eb_un.eb_val;
79 			break;
80 		case EB_ARGV:
81 			_argv = (char **)ebp->eb_un.eb_ptr;
82 			break;
83 		case EB_ENVP:
84 			_envp = (char **)ebp->eb_un.eb_ptr;
85 			break;
86 		case EB_AUXV:
87 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
88 			break;
89 		case EB_PAGESIZE:
90 			_syspagsz = (int)ebp->eb_un.eb_val;
91 			break;
92 		}
93 
94 	/*
95 	 * Search the aux. vector for the information passed by exec.
96 	 */
97 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
98 		switch (auxv->a_type) {
99 		case AT_EXECFD:
100 			/* this is the old exec that passes a file descriptor */
101 			fd = (int)auxv->a_un.a_val;
102 			break;
103 		case AT_FLAGS:
104 			/* processor flags (MAU available, etc) */
105 			_flags = auxv->a_un.a_val;
106 			break;
107 		case AT_PAGESZ:
108 			/* system page size */
109 			_syspagsz = (int)auxv->a_un.a_val;
110 			break;
111 		case AT_PHDR:
112 			/* address of the segment table */
113 			phdr = (Phdr *)auxv->a_un.a_ptr;
114 			break;
115 		case AT_BASE:
116 			/* interpreter base address */
117 			if (ld_base == 0)
118 				ld_base = auxv->a_un.a_val;
119 			interp_base = auxv->a_un.a_val;
120 			break;
121 		case AT_SUN_UID:
122 			/* effective user id for the executable */
123 			euid = (uid_t)auxv->a_un.a_val;
124 			break;
125 		case AT_SUN_RUID:
126 			/* real user id for the executable */
127 			uid = (uid_t)auxv->a_un.a_val;
128 			break;
129 		case AT_SUN_GID:
130 			/* effective group id for the executable */
131 			egid = (gid_t)auxv->a_un.a_val;
132 			break;
133 		case AT_SUN_RGID:
134 			/* real group id for the executable */
135 			gid = (gid_t)auxv->a_un.a_val;
136 			break;
137 		case AT_SUN_PLATFORM:
138 			/* platform name */
139 			_platform = auxv->a_un.a_ptr;
140 			break;
141 		case AT_SUN_EXECNAME:
142 			/* full pathname of execed object */
143 			_execname = auxv->a_un.a_ptr;
144 			break;
145 		case AT_SUN_AUXFLAGS:
146 			/* auxiliary flags */
147 			auxflags = (int)auxv->a_un.a_val;
148 			break;
149 		case AT_SUN_HWCAP:
150 			/* hardware capabilities */
151 			hwcap[0] = (uint_t)auxv->a_un.a_val;
152 			break;
153 		case AT_SUN_HWCAP2:
154 			/* hardware capabilities */
155 			hwcap[1] = (uint_t)auxv->a_un.a_val;
156 			break;
157 		case AT_SUN_EMULATOR:
158 			/* name of emulation library, if any */
159 			_emulator = auxv->a_un.a_ptr;
160 			break;
161 		}
162 	}
163 
164 	/*
165 	 * Get needed info from ld.so's dynamic structure.
166 	 */
167 	/* LINTED */
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) {
171 		case DT_RELA:
172 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
173 			break;
174 		case DT_RELACOUNT:
175 			relacount = ld_dyn->d_un.d_val;
176 			break;
177 		case DT_RELAENT:
178 			relaent = ld_dyn->d_un.d_val;
179 			break;
180 		case DT_PLTRELSZ:
181 			pltrelsz = ld_dyn->d_un.d_val;
182 			break;
183 		case DT_STRTAB:
184 			strtab = ld_dyn->d_un.d_ptr + ld_base;
185 			break;
186 		case DT_SONAME:
187 			soname = ld_dyn->d_un.d_val;
188 			break;
189 		}
190 	}
191 	_rt_name = (char *)strtab + soname;
192 
193 	/*
194 	 * If we don't have a RELAENT, just assume the size.
195 	 */
196 	if (relaent == 0)
197 		relaent = sizeof (Rela);
198 
199 	/*
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.
203 	 */
204 	relacount += (pltrelsz / relaent);
205 	for (; relacount; relacount--) {
206 		ulong_t	roffset;
207 
208 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
209 		*((ulong_t *)roffset) += ld_base +
210 		    ((Rela *)reladdr)->r_addend;
211 		reladdr += relaent;
212 	}
213 
214 	/*
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
217 	 * linker.
218 	 */
219 	if (_emulator != NULL) {
220 		_execname = _emulator;
221 		rtld_flags2 |= RT_FL2_BRANDED;
222 	}
223 
224 	/*
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.
228 	 */
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);
232 
233 	/*
234 	 * Continue with generic startup processing.
235 	 */
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,
239 	    hwcap)) == NULL) {
240 		rtldexit(&lml_main, 1);
241 	}
242 
243 	return (LM_ENTRY_PT(lmp)());
244 }
245