xref: /titanic_52/usr/src/cmd/sgs/libld/common/entry.c (revision 6e375c8351497b82ffa4f33cbf61d712999b4605)
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 (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #define	ELF_TARGET_AMD64
31 
32 #include	<stdio.h>
33 #include	<memory.h>
34 #include	<debug.h>
35 #include	"msg.h"
36 #include	"_libld.h"
37 
38 
39 /*
40  * Types of segment index.
41  */
42 typedef enum {
43 	LD_PHDR,
44 	LD_INTERP,
45 	LD_SUNWCAP,
46 	LD_TEXT,
47 	LD_DATA,
48 	LD_BSS,
49 #if	defined(_ELF64)
50 	LD_LRODATA,		/* (amd64-only) */
51 	LD_LDATA,		/* (amd64-only) */
52 #endif
53 	LD_DYN,
54 	LD_DTRACE,
55 	LD_TLS,
56 #if	defined(_ELF64)
57 	LD_UNWIND,		/* (amd64-only) */
58 #endif
59 	LD_NOTE,
60 	LD_EXTRA,
61 	LD_NUM
62 } Segment_ndx;
63 
64 /*
65  * The loader uses a `segment descriptor' list to describe the output
66  * segments it can potentially create. This list is initially seeded
67  * using the templates contained in the sg_desc[] array below. Additional
68  * segments may be added using a map file.
69  *
70  * The entries in sg_desc[] must be put in the order defined by the
71  * Segment_ndx enum, such that a given LD_XXX value can serve as
72  * an index into sg_desc[] for the corresponding descriptor.
73  *
74  * The entries in sg_desc[] are initialized using the SG_DESC_INIT macro
75  * for two reasons:
76  *
77  *	1) The first field of the Sg_desc struct is a program header
78  *		entry. ELF32_Phdr and ELF64_Phdr have the same fields,
79  *		but their order is different. Use of a macro allows us
80  *		to handle this transparently.
81  *	2) Most of the fields in the Sg_desc entries are set to 0.
82  *		Use of a macro allows us to hide the clutter.
83  */
84 #ifdef _ELF64
85 #define	SG_DESC_INIT(p_type, p_flags, sg_name, sg_flags) \
86 	{ { p_type, p_flags, 0, 0, 0, 0, 0, 0}, \
87 	    sg_name, 0, 0, NULL, NULL, sg_flags, NULL, 0, 0}
88 #else
89 #define	SG_DESC_INIT(p_type, p_flags, sg_name, sg_flags) \
90 	{ { p_type, 0, 0, 0, 0, 0, p_flags, 0}, \
91 	    sg_name, 0, 0, NULL, NULL, sg_flags, NULL, 0, 0}
92 #endif
93 
94 static const Sg_desc sg_desc[LD_NUM] = {
95 	/* LD_PHDR */
96 	SG_DESC_INIT(PT_PHDR, PF_R + PF_X, MSG_ORIG(MSG_ENT_PHDR),
97 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
98 
99 	/* LD_INTERP */
100 	SG_DESC_INIT(PT_INTERP, PF_R, MSG_ORIG(MSG_ENT_INTERP),
101 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
102 
103 	/* LD_SUNWCAP */
104 	SG_DESC_INIT(PT_SUNWCAP, PF_R, MSG_ORIG(MSG_ENT_SUNWCAP),
105 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
106 
107 	/* LD_TEXT */
108 	SG_DESC_INIT(PT_LOAD, PF_R + PF_X, MSG_ORIG(MSG_ENT_TEXT),
109 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
110 
111 	/* LD_DATA */
112 	SG_DESC_INIT(PT_LOAD, 0, MSG_ORIG(MSG_ENT_DATA),
113 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
114 
115 	/* LD_BSS */
116 	SG_DESC_INIT(PT_LOAD, 0, MSG_ORIG(MSG_ENT_BSS),
117 	    (FLG_SG_TYPE | FLG_SG_FLAGS | FLG_SG_DISABLED)),
118 
119 #if	defined(_ELF64)
120 	/* LD_LRODATA (amd64-only) */
121 	SG_DESC_INIT(PT_LOAD, PF_R, MSG_ORIG(MSG_ENT_LRODATA),
122 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
123 
124 	/* LD_LDATA (amd64-only) */
125 	SG_DESC_INIT(PT_LOAD, 0, MSG_ORIG(MSG_ENT_LDATA),
126 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
127 #endif
128 
129 	/* LD_DYN */
130 	SG_DESC_INIT(PT_DYNAMIC, 0, MSG_ORIG(MSG_ENT_DYNAMIC),
131 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
132 
133 	/* LD_DTRACE */
134 	SG_DESC_INIT(PT_SUNWDTRACE, 0,
135 		MSG_ORIG(MSG_ENT_DTRACE), (FLG_SG_TYPE | FLG_SG_FLAGS)),
136 
137 	/* LD_TLS */
138 	SG_DESC_INIT(PT_TLS, PF_R, MSG_ORIG(MSG_ENT_TLS),
139 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
140 
141 #if	defined(_ELF64)
142 	/* LD_UNWIND (amd64-only) */
143 	SG_DESC_INIT(PT_SUNW_UNWIND, PF_R, MSG_ORIG(MSG_ENT_UNWIND),
144 	    (FLG_SG_TYPE | FLG_SG_FLAGS)),
145 #endif
146 
147 	/* LD_NOTE */
148 	SG_DESC_INIT(PT_NOTE, 0, MSG_ORIG(MSG_ENT_NOTE), FLG_SG_TYPE),
149 
150 	/* LD_EXTRA */
151 	SG_DESC_INIT(PT_NULL, 0, MSG_ORIG(MSG_STR_EMPTY), FLG_SG_TYPE)
152 };
153 
154 
155 
156 /*
157  * The input processing of the loader involves matching the sections of its
158  * input files to an `entrance descriptor definition'.  The entrance criteria
159  * is different for either a static or dynamic linkage, and may even be
160  * modified further using a map file.  Each entrance criteria is associated
161  * with a segment descriptor, thus a mapping of input sections to output
162  * segments is maintained.
163  *
164  * Note the trick used for the ec_segment field, which is supposed to
165  * be a pointer to a segment descriptor. We initialize this with the
166  * index of the descriptor, and then turn it into an actual pointer
167  * at runtime, once memory has been allocated and the templates copied.
168  */
169 static const Ent_desc	ent_desc[] = {
170 	{{NULL, NULL}, NULL, SHT_NOTE, 0, 0,
171 		(Sg_desc *)LD_NOTE, 0, FALSE},
172 
173 #if	defined(_ELF64)		/* (amd64-only) */
174 	{{NULL, NULL}, MSG_ORIG(MSG_SCN_LRODATA), NULL,
175 		SHF_ALLOC + SHF_AMD64_LARGE, SHF_ALLOC + SHF_AMD64_LARGE,
176 		(Sg_desc *)LD_LRODATA, 0, FALSE},
177 #endif
178 
179 	{{NULL, NULL}, NULL, NULL,
180 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC,
181 		(Sg_desc *)LD_TEXT, 0, FALSE},
182 
183 	{{NULL, NULL}, NULL, SHT_NOBITS,
184 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
185 		(Sg_desc *)LD_BSS, 0, FALSE},
186 
187 #if	defined(_ELF64)		/* (amd64-only) */
188 	{{NULL, NULL}, NULL, SHT_NOBITS,
189 		SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
190 		SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
191 		(Sg_desc *)LD_DATA, 0, FALSE},
192 
193 	{{NULL, NULL}, NULL, NULL,
194 		SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
195 		SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
196 		(Sg_desc *)LD_LDATA, 0, FALSE},
197 #endif
198 
199 	{{NULL, NULL}, NULL, NULL,
200 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
201 		(Sg_desc *)LD_DATA, 0, FALSE},
202 
203 	{{NULL, NULL}, NULL, 0, 0, 0,
204 		(Sg_desc *)LD_EXTRA, 0, FALSE}
205 };
206 
207 /*
208  * Initialize new entrance and segment descriptors and add them as lists to
209  * the output file descriptor.
210  */
211 uintptr_t
212 ld_ent_setup(Ofl_desc *ofl, Xword segalign)
213 {
214 	Ent_desc	*enp;
215 	Sg_desc		*sgp;
216 	size_t		idx;
217 
218 	/*
219 	 * Initialize the elf library.
220 	 */
221 	if (elf_version(EV_CURRENT) == EV_NONE) {
222 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ELF_LIBELF),
223 		    EV_CURRENT);
224 		return (S_ERROR);
225 	}
226 
227 	/*
228 	 * Initialize internal Global Symbol Table AVL tree
229 	 */
230 	avl_create(&ofl->ofl_symavl, &ld_sym_avl_comp, sizeof (Sym_avlnode),
231 	    SGSOFFSETOF(Sym_avlnode, sav_node));
232 
233 	/*
234 	 * Allocate and initialize writable copies of both the entrance and
235 	 * segment descriptors.
236 	 *
237 	 * Note that on non-amd64 targets, this allocates a few more
238 	 * elements than are needed. For now, we are willing to overallocate
239 	 * a small amount to simplify the code.
240 	 */
241 	if ((sgp = libld_malloc(sizeof (sg_desc))) == 0)
242 		return (S_ERROR);
243 	(void) memcpy(sgp, sg_desc, sizeof (sg_desc));
244 	if ((enp = libld_malloc(sizeof (ent_desc))) == 0)
245 		return (S_ERROR);
246 	(void) memcpy(enp, ent_desc, sizeof (ent_desc));
247 
248 	/*
249 	 * The data segment permissions can differ:
250 	 *
251 	 *	- Architecural/ABI per-platform differences
252 	 *	- Whether the object is built statically or dynamically
253 	 *
254 	 * Those segments so affected have their program header flags
255 	 * set here at runtime, rather than in the sg_desc templates above.
256 	 */
257 	sgp[LD_DATA].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
258 	sgp[LD_BSS].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
259 	sgp[LD_DYN].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
260 	sgp[LD_DTRACE].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
261 #if	defined(_ELF64)
262 	sgp[LD_LDATA].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
263 	sgp[LD_DTRACE].sg_phdr.p_flags |= PF_X;
264 #endif
265 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) == 0)
266 		sgp[LD_DATA].sg_phdr.p_flags |= PF_X;
267 
268 	/*
269 	 * Traverse the new entrance descriptor list converting the segment
270 	 * pointer entries to the absolute address within the new segment
271 	 * descriptor list.  Add each entrance descriptor to the output file
272 	 * list.
273 	 */
274 	for (idx = 0; idx < (sizeof (ent_desc) / sizeof (ent_desc[0]));
275 	    idx++, enp++) {
276 #if	defined(_ELF64)
277 		/* Don't use the amd64 entry conditions for non-amd64 targets */
278 		if ((enp->ec_attrmask & SHF_AMD64_LARGE) &&
279 		    (ld_targ.t_m.m_mach != EM_AMD64))
280 			continue;
281 #endif
282 		enp->ec_segment = &sgp[(long)enp->ec_segment];
283 		if ((list_appendc(&ofl->ofl_ents, enp)) == 0)
284 			return (S_ERROR);
285 	}
286 
287 	/*
288 	 * Traverse the new segment descriptor list adding each entry to the
289 	 * segment descriptor list.  For each loadable segment initialize
290 	 * a default alignment (ld(1) and ld.so.1 initialize this differently).
291 	 */
292 	for (idx = 0; idx < LD_NUM; idx++, sgp++) {
293 		Phdr	*phdr = &(sgp->sg_phdr);
294 
295 #if	defined(_ELF64)
296 		/* Ignore amd64 segment templates for non-amd64 targets */
297 		switch (idx) {
298 		case LD_LRODATA:
299 		case LD_LDATA:
300 		case LD_UNWIND:
301 			if ((ld_targ.t_m.m_mach != EM_AMD64))
302 				continue;
303 		}
304 #endif
305 
306 		if ((list_appendc(&ofl->ofl_segs, sgp)) == 0)
307 			return (S_ERROR);
308 		if (phdr->p_type == PT_LOAD)
309 			phdr->p_align = segalign;
310 	}
311 
312 	return (1);
313 }
314