xref: /titanic_44/usr/src/cmd/sgs/rtld/amd64/_setup.c (revision 4bc0a2ef2b7ba50a7a717e7ddbf31472ad28e358)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * amd64 specific setup routine  -  relocate ld.so's symbols, setup its
30  * environment, map in loadable sections of the executable.
31  *
32  * Takes base address ld.so was loaded at, address of ld.so's dynamic
33  * structure, address of process environment pointers, address of auxiliary
34  * vector and * argv[0] (process name).
35  * If errors occur, send process signal - otherwise
36  * return executable's entry point to the bootstrap routine.
37  */
38 #include	"_synonyms.h"
39 
40 #include	<signal.h>
41 #include	<stdlib.h>
42 #include	<sys/auxv.h>
43 #include	<sys/types.h>
44 #include	<sys/stat.h>
45 #include	<link.h>
46 #include	<dlfcn.h>
47 #include	"_rtld.h"
48 #include	"_audit.h"
49 #include	"msg.h"
50 #include	"debug.h"
51 
52 extern int	_end;
53 extern int	_etext;
54 
55 
56 /* VARARGS */
57 unsigned long
58 _setup(Boot * ebp, Dyn * ld_dyn)
59 {
60 	unsigned long	reladdr, relacount, ld_base = 0;
61 	unsigned long	relaent = 0, pltrelsz = 0;
62 	unsigned long	strtab, soname, interp_base = 0;
63 	char		*_rt_name, **_envp, **_argv;
64 	int		_syspagsz = 0, fd = -1, dz_fd = FD_UNAVAIL;
65 	uint_t		_flags = 0, hwcap_1 = 0;
66 	Dyn *		dyn_ptr;
67 	Phdr *		phdr = 0;
68 	Rt_map *	lmp;
69 	auxv_t		*auxv, *_auxv;
70 	uid_t		uid = -1, euid = -1;
71 	gid_t		gid = -1, egid = -1;
72 	char		*_platform = 0, *_execname = 0;
73 	int		auxflags = -1;
74 	/*
75 	 * Scan the bootstrap structure to pick up the basics.
76 	 */
77 	for (; ebp->eb_tag != EB_NULL; ebp++)
78 		switch (ebp->eb_tag) {
79 		case EB_LDSO_BASE:
80 			ld_base = (unsigned long)ebp->eb_un.eb_val;
81 			break;
82 		case EB_ARGV:
83 			_argv = (char **)ebp->eb_un.eb_ptr;
84 			break;
85 		case EB_ENVP:
86 			_envp = (char **)ebp->eb_un.eb_ptr;
87 			break;
88 		case EB_AUXV:
89 			_auxv = (auxv_t *)ebp->eb_un.eb_ptr;
90 			break;
91 		case EB_DEVZERO:
92 			dz_fd = (int)ebp->eb_un.eb_val;
93 			break;
94 		case EB_PAGESIZE:
95 			_syspagsz = (int)ebp->eb_un.eb_val;
96 			break;
97 		}
98 
99 	/*
100 	 * Search the aux. vector for the information passed by exec.
101 	 */
102 	for (auxv = _auxv; auxv->a_type != AT_NULL; auxv++) {
103 		switch (auxv->a_type) {
104 		case AT_EXECFD:
105 			/* this is the old exec that passes a file descriptor */
106 			fd = (int)auxv->a_un.a_val;
107 			break;
108 		case AT_FLAGS:
109 			/* processor flags (MAU available, etc) */
110 			_flags = auxv->a_un.a_val;
111 			break;
112 		case AT_PAGESZ:
113 			/* system page size */
114 			_syspagsz = (int)auxv->a_un.a_val;
115 			break;
116 		case AT_PHDR:
117 			/* address of the segment table */
118 			phdr = (Phdr *)auxv->a_un.a_ptr;
119 			break;
120 		case AT_BASE:
121 			/* interpreter base address */
122 			if (ld_base == 0)
123 				ld_base = auxv->a_un.a_val;
124 			interp_base = auxv->a_un.a_val;
125 			break;
126 		case AT_SUN_UID:
127 			/* effective user id for the executable */
128 			euid = (uid_t)auxv->a_un.a_val;
129 			break;
130 		case AT_SUN_RUID:
131 			/* real user id for the executable */
132 			uid = (uid_t)auxv->a_un.a_val;
133 			break;
134 		case AT_SUN_GID:
135 			/* effective group id for the executable */
136 			egid = (gid_t)auxv->a_un.a_val;
137 			break;
138 		case AT_SUN_RGID:
139 			/* real group id for the executable */
140 			gid = (gid_t)auxv->a_un.a_val;
141 			break;
142 		case AT_SUN_PLATFORM:
143 			/* platform name */
144 			_platform = auxv->a_un.a_ptr;
145 			break;
146 		case AT_SUN_EXECNAME:
147 			/* full pathname of execed object */
148 			_execname = auxv->a_un.a_ptr;
149 			break;
150 		case AT_SUN_AUXFLAGS:
151 			auxflags = (int)auxv->a_un.a_val;
152 			break;
153 		case AT_SUN_HWCAP:
154 			hwcap_1 = (uint_t)auxv->a_un.a_val;
155 			break;
156 		}
157 	}
158 
159 	/*
160 	 * Get needed info from ld.so's dynamic structure.
161 	 */
162 	/* LINTED */
163 	dyn_ptr = (Dyn *)((char *)ld_dyn + ld_base);
164 	for (ld_dyn = dyn_ptr; ld_dyn->d_tag != DT_NULL; ld_dyn++) {
165 		switch (ld_dyn->d_tag) {
166 		case DT_RELA:
167 			reladdr = ld_dyn->d_un.d_ptr + ld_base;
168 			break;
169 		case DT_RELACOUNT:
170 			relacount = ld_dyn->d_un.d_val;
171 			break;
172 		case DT_RELAENT:
173 			relaent = ld_dyn->d_un.d_val;
174 			break;
175 		case DT_PLTRELSZ:
176 			pltrelsz = ld_dyn->d_un.d_val;
177 			break;
178 		case DT_STRTAB:
179 			strtab = ld_dyn->d_un.d_ptr + ld_base;
180 			break;
181 		case DT_SONAME:
182 			soname = ld_dyn->d_un.d_val;
183 			break;
184 		}
185 	}
186 	_rt_name = (char *)strtab + soname;
187 
188 	/*
189 	 * If we don't have a RELAENT, just assume
190 	 * the size.
191 	 */
192 	if (relaent == 0)
193 		relaent = sizeof (Rela);
194 	/*
195 	 * Relocate all symbols in ld.so.
196 	 *
197 	 * Because ld.so.1 is built with -Bsymbolic there should only be
198 	 * RELATIVE and JMPSLOT relocations, both of which get relative
199 	 * additions against them.
200 	 */
201 	relacount += (pltrelsz / relaent);
202 	for (; relacount; relacount--) {
203 		ulong_t	roffset;
204 
205 		roffset = ((Rela *)reladdr)->r_offset + ld_base;
206 		*((ulong_t *)roffset) += ld_base +
207 		    ((Rela *)reladdr)->r_addend;
208 		reladdr += relaent;
209 	}
210 
211 	/*
212 	 * Initialize the dyn_plt_ent_size field.  It currently contains the
213 	 * size of the dyn_plt_template.  It still needs to be aligned and have
214 	 * space for the 'dyn_data' area added.
215 	 */
216 	dyn_plt_ent_size = ROUND(dyn_plt_ent_size, M_WORD_ALIGN) +
217 	    sizeof (uintptr_t) + sizeof (uintptr_t) + sizeof (ulong_t) +
218 	    sizeof (ulong_t) + sizeof (Sym);
219 
220 	/*
221 	 * Continue with generic startup processing.
222 	 */
223 	if ((lmp = setup((char **)_envp, (auxv_t *)_auxv, _flags, _platform,
224 	    _syspagsz, _rt_name, dyn_ptr, ld_base, interp_base, fd, phdr,
225 	    _execname, _argv, dz_fd, uid, euid, gid, egid, NULL, auxflags,
226 	    hwcap_1)) == NULL) {
227 		rtldexit(&lml_main, 1);
228 	}
229 
230 	return (LM_ENTRY_PT(lmp)());
231 }
232