summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/libld/common/entry.c
blob: f55e2cf72336cdbaa436d59d7018b13c99de46e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 *	Copyright (c) 1988 AT&T
 *	  All Rights Reserved
 *
 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#define	ELF_TARGET_AMD64

#include	<stdio.h>
#include	<memory.h>
#include	<debug.h>
#include	"msg.h"
#include	"_libld.h"

/*
 * The link-editor uses a segment descriptor list to describe the program
 * headers, and related output segments, it can potentially create. This
 * list is initially seeded using the templates contained in the sg_desc
 * array below. Additional segments may be added using a mapfile.
 *
 * The entries in sg_desc must be put in the order defined by the
 * Segment_id enum.
 *
 * The entries in sg_desc are initialized using the SG_DESC_INIT macro
 * for two reasons:
 *
 *	1) The first field of the Sg_desc struct is a program header
 *		entry. ELF32_Phdr and ELF64_Phdr have the same fields,
 *		but their order is different. Use of a macro allows us
 *		to handle this transparently.
 *	2) Most of the fields in the Sg_desc entries are set to 0.
 *		Use of a macro allows us to hide the clutter.
 *
 * If a given program header can be referenced via an entrance criteria
 * (i.e. can serve as a segment), then it must be given a unique sg_name.
 * Program headers that cannot be a segment (PHDR, INTERP, DYNAMIC, etc)
 * must have a NULL sg_name --- their program header type identifies them.
 */
#ifdef _ELF64
#define	SG_DESC_INIT(id, p_type, p_flags, sg_name, sg_flags) \
	{ id, { p_type, p_flags, 0, 0, 0, 0, 0, 0}, \
	    sg_name, 0, 0, NULL, NULL, NULL, sg_flags, NULL, 0, NULL}
#else
#define	SG_DESC_INIT(id, p_type, p_flags, sg_name, sg_flags) \
	{ id, { p_type, 0, 0, 0, 0, 0, p_flags, 0}, \
	    sg_name, 0, 0, NULL, NULL, NULL, sg_flags, NULL, 0, NULL}
#endif

/*
 * Predefined segment descriptors:
 *
 * The C language guarantees that a structure containing only fields of
 * identical type is indistinguishable from a simple array containing
 * the same number of items of the same type. They will have the same
 * size, alignment, and internal layout:
 *
 * -	A pointer to one is equivalent to a pointer to the other, and you
 *	can cast safely between them.
 *
 * -	You can put both into a union, and access the elements within
 *	either way (by index, or by name).
 *
 * We use this fact here to create an "array" of predefined segment
 * descriptors, assigning each one a mnemonic name that can be used to point
 * at it from a predefined entrance criteria descriptor (below). These
 * segments are positioned in the default order that will result in the
 * output object, unless a mapfile alters things.
 */
typedef struct {
	Sg_desc	psg_phdr;
	Sg_desc psg_interp;
	Sg_desc psg_sunwcap;
	Sg_desc psg_text;
	Sg_desc psg_data;
	Sg_desc psg_bss;
#if	defined(_ELF64)
	Sg_desc	psg_lrodata;	/* (amd64-only) */
	Sg_desc psg_ldata;	/* (amd64-only) */
#endif
	Sg_desc	psg_dynamic;
	Sg_desc	psg_sunwdtrace;
	Sg_desc	psg_tls;
	Sg_desc	psg_unwind;
	Sg_desc	psg_sunwstack;
	Sg_desc	psg_note;
	Sg_desc	psg_extra;
} predef_seg_t;

static const size_t predef_seg_nelts =
	(sizeof (predef_seg_t) / sizeof (Sg_desc));

static predef_seg_t sg_desc = {
	/* psg_phdr */
	SG_DESC_INIT(SGID_PHDR, PT_PHDR, PF_R + PF_X, NULL,
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_interp */
	SG_DESC_INIT(SGID_INTERP, PT_INTERP, PF_R, NULL,
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_sunwcap */
	SG_DESC_INIT(SGID_SUNWCAP, PT_SUNWCAP, PF_R, NULL,
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_text */
	SG_DESC_INIT(SGID_TEXT, PT_LOAD, PF_R + PF_X, MSG_ORIG(MSG_ENT_TEXT),
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_data */
	SG_DESC_INIT(SGID_DATA, PT_LOAD, 0, MSG_ORIG(MSG_ENT_DATA),
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_bss */
	SG_DESC_INIT(SGID_BSS, PT_LOAD, 0, MSG_ORIG(MSG_ENT_BSS),
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS | FLG_SG_DISABLED)),

#if	defined(_ELF64)
	/* psg_lrodata (amd64-only ) */
	SG_DESC_INIT(SGID_LRODATA, PT_LOAD, PF_R, MSG_ORIG(MSG_ENT_LRODATA),
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_ldata (amd64-only ) */
	SG_DESC_INIT(SGID_LDATA, PT_LOAD, 0, MSG_ORIG(MSG_ENT_LDATA),
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
#endif
	/* psg_dynamic */
	SG_DESC_INIT(SGID_DYN, PT_DYNAMIC, 0, NULL,
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_sunwdtrace */
	SG_DESC_INIT(SGID_DTRACE, PT_SUNWDTRACE, 0, NULL,
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_tls */
	SG_DESC_INIT(SGID_TLS, PT_TLS, PF_R, NULL,
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_unwind */
	SG_DESC_INIT(SGID_UNWIND, PT_SUNW_UNWIND, PF_R, NULL,
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),

	/* psg_sunwstack */
	SG_DESC_INIT(SGID_SUNWSTACK, PT_SUNWSTACK, 0, NULL,
	    (FLG_SG_P_TYPE | FLG_SG_P_FLAGS | FLG_SG_DISABLED)),

	/* psg_note */
	SG_DESC_INIT(SGID_NOTE, PT_NOTE, 0, MSG_ORIG(MSG_ENT_NOTE),
	    FLG_SG_P_TYPE),

	/*
	 * psg_extra
	 *
	 * This segment is referenced by the final entrance criteria descriptor
	 * to catch any segment not otherwise placed. It cannot be disabled
	 * via a mapfile.
	 */
	SG_DESC_INIT(SGID_EXTRA, PT_NULL, 0, MSG_ORIG(MSG_ENT_EXTRA),
	    (FLG_SG_P_TYPE | FLG_SG_NODISABLE))
};
#undef SG_DESC_INIT

/*
 * The processing of input files by the link-editor involves matching the
 * input file sections against an ordered list of entrance criteria
 * descriptors. The following template defines the built in entrance criteria
 * list. This list can be augmented using a mapfile. Each entrance criteria
 * is associated with a segment descriptor, providing the means for mapping
 * input sections to output segments.
 *
 * As with the segment descriptors, the EC_DESC_INIT macro is used
 * to reduce boilerplate clutter.
 */
#define	EC_DESC_INIT(ec_is_name, ec_type, ec_attrmask, ec_attrbits, \
    _seg_field, ec_flags) \
	{ NULL, NULL, ec_is_name, ec_type, ec_attrmask, ec_attrbits, \
	    &sg_desc.psg_ ## _seg_field, 0, FLG_EC_BUILTIN | ec_flags }

static const Ent_desc	ent_desc[] = {
	EC_DESC_INIT(NULL, SHT_NOTE, 0, 0, note, 0),

#if	defined(_ELF64)		/* (amd64-only) */
	EC_DESC_INIT(NULL, 0, SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
	    SHF_ALLOC + SHF_AMD64_LARGE, lrodata, 0),
#endif
	EC_DESC_INIT(NULL, 0, SHF_ALLOC + SHF_WRITE, SHF_ALLOC, text, 0),

	/*
	 * Explicitly assign the .tdata section to bss.  The design of TLS
	 * provides for initialized data being assigned to a .tdata section,
	 * and uninitialized data being assigned to a .tbss section.  These
	 * sections should be laid out adjacent to each other, with little or
	 * no gap between them.  A PT_TLS program header is created that
	 * defines the address range of the two sections.  This header is
	 * passed to libc to instantiate the appropriate thread allocation.
	 *
	 * By default a separate bss segment is disabled, however users can
	 * trigger the creation of a bss segment with a mapfile.  By default,
	 * all bss sections are assigned to the data segment, and the section
	 * identifiers of .tdata and .tbss ensure that these two sections are
	 * adjacent to each other.
	 *
	 * However, if a bss segment is enabled, the adjacency of the .tdata
	 * and .tbss sections can only be retained by having an explicit .tdata
	 * entrance criteria.
	 */
	EC_DESC_INIT(MSG_ORIG(MSG_SCN_TDATA), 0, SHF_ALLOC + SHF_WRITE,
	    SHF_ALLOC + SHF_WRITE, bss, 0),

	EC_DESC_INIT(NULL, SHT_NOBITS, SHF_ALLOC + SHF_WRITE,
	    SHF_ALLOC + SHF_WRITE, bss, 0),

#if	defined(_ELF64)		/* (amd64-only) */
	EC_DESC_INIT(NULL, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
	    SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE, data, 0),

	EC_DESC_INIT(NULL, 0, SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
	    SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE, ldata, 0),
#endif
	EC_DESC_INIT(NULL, 0, SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
	    data, 0),

	/*
	 * Final catchall rule sends remaining sections to "extra"
	 * NULL segment, which has been tagged as FLG_SG_NODISABLE,
	 * and which will therefore always accept them.
	 */
	EC_DESC_INIT(NULL, 0, 0, 0, extra, FLG_EC_CATCHALL)
};
#undef EC_DESC_INIT

/*
 * AVL comparison function for Sg_desc items in ofl_segs_avl.
 *
 * entry:
 *	n1, n2 - pointers to nodes to be compared
 *
 * exit:
 *	Returns -1 if (n1 < n2), 0 if they are equal, and 1 if (n1 > n2)
 */
static int
ofl_segs_avl_cmp(const void *n1, const void *n2)
{
	int		rc;

	rc = strcmp(((Sg_desc *)n1)->sg_name, ((Sg_desc *)n2)->sg_name);

	if (rc > 0)
		return (1);
	if (rc < 0)
		return (-1);
	return (0);
}

/*
 * AVL comparison function for Ent_desc items in ofl_ents_avl.
 *
 * entry:
 *	n1, n2 - pointers to nodes to be compared
 *
 * exit:
 *	Returns -1 if (n1 < n2), 0 if they are equal, and 1 if (n1 > n2)
 */
static int
ofl_ents_avl_cmp(const void *n1, const void *n2)
{
	int		rc;

	/*
	 * There are entrance criteria nodes with NULL pointer names,
	 * but they are never entered into the AVL tree. Hence, we can
	 * assume that both nodes have names.
	 */
	rc = strcmp(((Ent_desc *)n1)->ec_name, ((Ent_desc *)n2)->ec_name);

	if (rc > 0)
		return (1);
	if (rc < 0)
		return (-1);
	return (0);
}

/*
 * Lookup a segment descriptor by name.
 *
 * entry:
 *	ofl - Output descriptor
 *	name - Name of desired segment
 *
 * exit:
 *	On success, returns pointer to descriptor. On failure, returns NULL.
 */
Sg_desc *
ld_seg_lookup(Ofl_desc *ofl, const char *name, avl_index_t *where)
{
	Sg_desc		sg;

	sg.sg_name = name;
	return (avl_find(&ofl->ofl_segs_avl, &sg, where));
}


/*
 * Look up an entrance criteria record by name
 *
 * entry:
 *	mf - Mapfile descriptor
 *	name - Name of entrance criteria to locate
 *
 * exit:
 *	On success, a pointer to the entrace criteria record is
 *	returned. On failure, NULL is returned.
 *
 * note:
 *	Entrance criteria are not required to have names. Only
 *	named entrance criteria can be looked up via this method.
 */
Ent_desc *
ld_ent_lookup(Ofl_desc *ofl, const char *name, avl_index_t *where)
{
	Ent_desc	en;

	en.ec_name = name;
	return (avl_find(&ofl->ofl_ents_avl, &en, where));
}

/*
 * Initialize new entrance and segment descriptors and add them as lists to
 * the output file descriptor.
 */
uintptr_t
ld_ent_setup(Ofl_desc *ofl, Xword segalign)
{
	Ent_desc	*enp;
	predef_seg_t	*psegs;
	Sg_desc		*sgp;
	size_t		idx;

	/*
	 * Initialize the elf library.
	 */
	if (elf_version(EV_CURRENT) == EV_NONE) {
		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ELF_LIBELF),
		    EV_CURRENT);
		return (S_ERROR);
	}

	/*
	 * Initialize internal Global Symbol Table AVL tree
	 */
	avl_create(&ofl->ofl_symavl, &ld_sym_avl_comp, sizeof (Sym_avlnode),
	    SGSOFFSETOF(Sym_avlnode, sav_node));

	/* Initialize segment AVL tree */
	avl_create(&ofl->ofl_segs_avl, ofl_segs_avl_cmp,
	    sizeof (Sg_desc), SGSOFFSETOF(Sg_desc, sg_avlnode));

	/* Initialize entrance criteria AVL tree */
	avl_create(&ofl->ofl_ents_avl, ofl_ents_avl_cmp, sizeof (Ent_desc),
	    SGSOFFSETOF(Ent_desc, ec_avlnode));


	/*
	 * Allocate and initialize writable copies of both the entrance and
	 * segment descriptors.
	 *
	 * Note that on non-amd64 targets, this allocates a few more
	 * elements than are needed. For now, we are willing to overallocate
	 * a small amount to simplify the code.
	 */
	if ((psegs = libld_malloc(sizeof (sg_desc))) == NULL)
		return (S_ERROR);
	(void) memcpy(psegs, &sg_desc, sizeof (sg_desc));
	sgp = (Sg_desc *) psegs;

	/*
	 * The data segment and stack permissions can differ:
	 *
	 *	- Architectural/ABI per-platform differences
	 *	- Whether the object is built statically or dynamically
	 *
	 * Those segments so affected have their program header flags
	 * set here at runtime, rather than in the sg_desc templates above.
	 */
	psegs->psg_data.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
	psegs->psg_bss.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
	psegs->psg_dynamic.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
	psegs->psg_sunwdtrace.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
#if	defined(_ELF64)
	psegs->psg_ldata.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
	psegs->psg_sunwdtrace.sg_phdr.p_flags |= PF_X;
#endif
	psegs->psg_sunwstack.sg_phdr.p_flags = ld_targ.t_m.m_stack_perm;
	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) == 0)
		psegs->psg_data.sg_phdr.p_flags |= PF_X;

	/*
	 * Traverse the new entrance descriptor list converting the segment
	 * pointer entries to the absolute address within the new segment
	 * descriptor list.  Add each entrance descriptor to the output file
	 * list.
	 */
	if ((enp = libld_malloc(sizeof (ent_desc))) == NULL)
		return (S_ERROR);
	(void) memcpy(enp, ent_desc, sizeof (ent_desc));
	for (idx = 0; idx < (sizeof (ent_desc) / sizeof (ent_desc[0])); idx++,
	    enp++) {

#if	defined(_ELF64)
		/* Don't use the amd64 entry conditions for non-amd64 targets */
		if ((enp->ec_attrmask & SHF_AMD64_LARGE) &&
		    (ld_targ.t_m.m_mach != EM_AMD64))
			continue;
#endif
		if (aplist_append(&ofl->ofl_ents, enp,
		    AL_CNT_OFL_ENTRANCE) == NULL)
			return (S_ERROR);

		/*
		 * The segment pointer is currently pointing at a template
		 * segment descriptor in sg_desc. Compute its array index,
		 * and then use that index to compute the address of the
		 * corresponding descriptor in the writable copy.
		 */
		enp->ec_segment =
		    &sgp[(enp->ec_segment - (Sg_desc *) &sg_desc)];
	}

	/*
	 * Add each segment descriptor to the segment descriptor list. The
	 * ones with non-NULL sg_name are also entered into the AVL tree.
	 * For each loadable segment initialize a default alignment. Note
	 * that ld(1) and ld.so.1 initialize this differently.
	 */
	for (idx = 0; idx < predef_seg_nelts; idx++, sgp++) {
		Phdr	*phdr = &(sgp->sg_phdr);

#if	defined(_ELF64)
		/* Ignore amd64 segment templates for non-amd64 targets */
		switch (sgp->sg_id) {
		case SGID_LRODATA:
		case SGID_LDATA:
			if ((ld_targ.t_m.m_mach != EM_AMD64))
				continue;
		}
#endif
		if (phdr->p_type == PT_LOAD)
			phdr->p_align = segalign;

		if ((aplist_append(&ofl->ofl_segs, sgp,
		    AL_CNT_SEGMENTS)) == NULL)
			return (S_ERROR);

#ifndef NDEBUG			/* assert() is enabled */
		/*
		 * Enforce the segment name rule: Any segment that can
		 * be referenced by an entrance descriptor must have
		 * a name. Any segment that cannot, must have a NULL
		 * name pointer.
		 */
		switch (phdr->p_type) {
		case PT_LOAD:
		case PT_NOTE:
		case PT_NULL:
			assert(sgp->sg_name != NULL);
			break;
		default:
			assert(sgp->sg_name == NULL);
			break;
		}
#endif

		/*
		 * Add named segment descriptors to the AVL tree to
		 * provide O(logN) lookups.
		 */
		if (sgp->sg_name != NULL)
			avl_add(&ofl->ofl_segs_avl, sgp);
	}

	return (1);
}