8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sgs / libld / common / entry.c
blob396075d09fbcdb720870dc40e875e389879c00d7
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 1988 AT&T
24 * All Rights Reserved
26 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
29 #define ELF_TARGET_AMD64
31 #include <stdio.h>
32 #include <memory.h>
33 #include <debug.h>
34 #include "msg.h"
35 #include "_libld.h"
38 * The link-editor uses a segment descriptor list to describe the program
39 * headers, and related output segments, it can potentially create. This
40 * list is initially seeded using the templates contained in the sg_desc
41 * array below. Additional segments may be added using a mapfile.
43 * The entries in sg_desc must be put in the order defined by the
44 * Segment_id enum.
46 * The entries in sg_desc are initialized using the SG_DESC_INIT macro
47 * for two reasons:
49 * 1) The first field of the Sg_desc struct is a program header
50 * entry. ELF32_Phdr and ELF64_Phdr have the same fields,
51 * but their order is different. Use of a macro allows us
52 * to handle this transparently.
53 * 2) Most of the fields in the Sg_desc entries are set to 0.
54 * Use of a macro allows us to hide the clutter.
56 * If a given program header can be referenced via an entrance criteria
57 * (i.e. can serve as a segment), then it must be given a unique sg_name.
58 * Program headers that cannot be a segment (PHDR, INTERP, DYNAMIC, etc)
59 * must have a NULL sg_name --- their program header type identifies them.
61 #ifdef _ELF64
62 #define SG_DESC_INIT(id, p_type, p_flags, sg_name, sg_flags) \
63 { id, { p_type, p_flags, 0, 0, 0, 0, 0, 0}, \
64 sg_name, 0, 0, NULL, NULL, NULL, sg_flags, NULL, 0, NULL}
65 #else
66 #define SG_DESC_INIT(id, p_type, p_flags, sg_name, sg_flags) \
67 { id, { p_type, 0, 0, 0, 0, 0, p_flags, 0}, \
68 sg_name, 0, 0, NULL, NULL, NULL, sg_flags, NULL, 0, NULL}
69 #endif
72 * Predefined segment descriptors:
74 * The C language guarantees that a structure containing only fields of
75 * identical type is indistinguishable from a simple array containing
76 * the same number of items of the same type. They will have the same
77 * size, alignment, and internal layout:
79 * - A pointer to one is equivalent to a pointer to the other, and you
80 * can cast safely between them.
82 * - You can put both into a union, and access the elements within
83 * either way (by index, or by name).
85 * We use this fact here to create an "array" of predefined segment
86 * descriptors, assigning each one a mnemonic name that can be used to point
87 * at it from a predefined entrance criteria descriptor (below). These
88 * segments are positioned in the default order that will result in the
89 * output object, unless a mapfile alters things.
91 typedef struct {
92 Sg_desc psg_phdr;
93 Sg_desc psg_interp;
94 Sg_desc psg_sunwcap;
95 Sg_desc psg_text;
96 Sg_desc psg_data;
97 Sg_desc psg_bss;
98 #if defined(_ELF64)
99 Sg_desc psg_lrodata; /* (amd64-only) */
100 Sg_desc psg_ldata; /* (amd64-only) */
101 #endif
102 Sg_desc psg_dynamic;
103 Sg_desc psg_sunwdtrace;
104 Sg_desc psg_tls;
105 Sg_desc psg_unwind;
106 Sg_desc psg_sunwstack;
107 Sg_desc psg_note;
108 Sg_desc psg_extra;
109 } predef_seg_t;
111 static const size_t predef_seg_nelts =
112 (sizeof (predef_seg_t) / sizeof (Sg_desc));
114 static predef_seg_t sg_desc = {
115 /* psg_phdr */
116 SG_DESC_INIT(SGID_PHDR, PT_PHDR, PF_R + PF_X, NULL,
117 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
119 /* psg_interp */
120 SG_DESC_INIT(SGID_INTERP, PT_INTERP, PF_R, NULL,
121 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
123 /* psg_sunwcap */
124 SG_DESC_INIT(SGID_SUNWCAP, PT_SUNWCAP, PF_R, NULL,
125 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
127 /* psg_text */
128 SG_DESC_INIT(SGID_TEXT, PT_LOAD, PF_R + PF_X, MSG_ORIG(MSG_ENT_TEXT),
129 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
131 /* psg_data */
132 SG_DESC_INIT(SGID_DATA, PT_LOAD, 0, MSG_ORIG(MSG_ENT_DATA),
133 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
135 /* psg_bss */
136 SG_DESC_INIT(SGID_BSS, PT_LOAD, 0, MSG_ORIG(MSG_ENT_BSS),
137 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS | FLG_SG_DISABLED)),
139 #if defined(_ELF64)
140 /* psg_lrodata (amd64-only ) */
141 SG_DESC_INIT(SGID_LRODATA, PT_LOAD, PF_R, MSG_ORIG(MSG_ENT_LRODATA),
142 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
144 /* psg_ldata (amd64-only ) */
145 SG_DESC_INIT(SGID_LDATA, PT_LOAD, 0, MSG_ORIG(MSG_ENT_LDATA),
146 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
147 #endif
148 /* psg_dynamic */
149 SG_DESC_INIT(SGID_DYN, PT_DYNAMIC, 0, NULL,
150 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
152 /* psg_sunwdtrace */
153 SG_DESC_INIT(SGID_DTRACE, PT_SUNWDTRACE, 0, NULL,
154 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
156 /* psg_tls */
157 SG_DESC_INIT(SGID_TLS, PT_TLS, PF_R, NULL,
158 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
160 /* psg_unwind */
161 SG_DESC_INIT(SGID_UNWIND, PT_SUNW_UNWIND, PF_R, NULL,
162 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS)),
164 /* psg_sunwstack */
165 SG_DESC_INIT(SGID_SUNWSTACK, PT_SUNWSTACK, 0, NULL,
166 (FLG_SG_P_TYPE | FLG_SG_P_FLAGS | FLG_SG_DISABLED)),
168 /* psg_note */
169 SG_DESC_INIT(SGID_NOTE, PT_NOTE, 0, MSG_ORIG(MSG_ENT_NOTE),
170 FLG_SG_P_TYPE),
173 * psg_extra
175 * This segment is referenced by the final entrance criteria descriptor
176 * to catch any segment not otherwise placed. It cannot be disabled
177 * via a mapfile.
179 SG_DESC_INIT(SGID_EXTRA, PT_NULL, 0, MSG_ORIG(MSG_ENT_EXTRA),
180 (FLG_SG_P_TYPE | FLG_SG_NODISABLE))
182 #undef SG_DESC_INIT
185 * The processing of input files by the link-editor involves matching the
186 * input file sections against an ordered list of entrance criteria
187 * descriptors. The following template defines the built in entrance criteria
188 * list. This list can be augmented using a mapfile. Each entrance criteria
189 * is associated with a segment descriptor, providing the means for mapping
190 * input sections to output segments.
192 * As with the segment descriptors, the EC_DESC_INIT macro is used
193 * to reduce boilerplate clutter.
195 #define EC_DESC_INIT(ec_is_name, ec_type, ec_attrmask, ec_attrbits, \
196 _seg_field, ec_flags) \
197 { NULL, NULL, ec_is_name, ec_type, ec_attrmask, ec_attrbits, \
198 &sg_desc.psg_ ## _seg_field, 0, FLG_EC_BUILTIN | ec_flags }
200 static const Ent_desc ent_desc[] = {
201 EC_DESC_INIT(NULL, SHT_NOTE, 0, 0, note, 0),
203 #if defined(_ELF64) /* (amd64-only) */
204 EC_DESC_INIT(NULL, 0, SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
205 SHF_ALLOC + SHF_AMD64_LARGE, lrodata, 0),
206 #endif
207 EC_DESC_INIT(NULL, 0, SHF_ALLOC + SHF_WRITE, SHF_ALLOC, text, 0),
210 * Explicitly assign the .tdata section to bss. The design of TLS
211 * provides for initialized data being assigned to a .tdata section,
212 * and uninitialized data being assigned to a .tbss section. These
213 * sections should be laid out adjacent to each other, with little or
214 * no gap between them. A PT_TLS program header is created that
215 * defines the address range of the two sections. This header is
216 * passed to libc to instantiate the appropriate thread allocation.
218 * By default a separate bss segment is disabled, however users can
219 * trigger the creation of a bss segment with a mapfile. By default,
220 * all bss sections are assigned to the data segment, and the section
221 * identifiers of .tdata and .tbss ensure that these two sections are
222 * adjacent to each other.
224 * However, if a bss segment is enabled, the adjacency of the .tdata
225 * and .tbss sections can only be retained by having an explicit .tdata
226 * entrance criteria.
228 EC_DESC_INIT(MSG_ORIG(MSG_SCN_TDATA), 0, SHF_ALLOC + SHF_WRITE,
229 SHF_ALLOC + SHF_WRITE, bss, 0),
231 EC_DESC_INIT(NULL, SHT_NOBITS, SHF_ALLOC + SHF_WRITE,
232 SHF_ALLOC + SHF_WRITE, bss, 0),
234 #if defined(_ELF64) /* (amd64-only) */
235 EC_DESC_INIT(NULL, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
236 SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE, data, 0),
238 EC_DESC_INIT(NULL, 0, SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
239 SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE, ldata, 0),
240 #endif
241 EC_DESC_INIT(NULL, 0, SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
242 data, 0),
245 * Final catchall rule sends remaining sections to "extra"
246 * NULL segment, which has been tagged as FLG_SG_NODISABLE,
247 * and which will therefore always accept them.
249 EC_DESC_INIT(NULL, 0, 0, 0, extra, FLG_EC_CATCHALL)
251 #undef EC_DESC_INIT
254 * AVL comparison function for Sg_desc items in ofl_segs_avl.
256 * entry:
257 * n1, n2 - pointers to nodes to be compared
259 * exit:
260 * Returns -1 if (n1 < n2), 0 if they are equal, and 1 if (n1 > n2)
262 static int
263 ofl_segs_avl_cmp(const void *n1, const void *n2)
265 int rc;
267 rc = strcmp(((Sg_desc *)n1)->sg_name, ((Sg_desc *)n2)->sg_name);
269 if (rc > 0)
270 return (1);
271 if (rc < 0)
272 return (-1);
273 return (0);
277 * AVL comparison function for Ent_desc items in ofl_ents_avl.
279 * entry:
280 * n1, n2 - pointers to nodes to be compared
282 * exit:
283 * Returns -1 if (n1 < n2), 0 if they are equal, and 1 if (n1 > n2)
285 static int
286 ofl_ents_avl_cmp(const void *n1, const void *n2)
288 int rc;
291 * There are entrance criteria nodes with NULL pointer names,
292 * but they are never entered into the AVL tree. Hence, we can
293 * assume that both nodes have names.
295 rc = strcmp(((Ent_desc *)n1)->ec_name, ((Ent_desc *)n2)->ec_name);
297 if (rc > 0)
298 return (1);
299 if (rc < 0)
300 return (-1);
301 return (0);
305 * Lookup a segment descriptor by name.
307 * entry:
308 * ofl - Output descriptor
309 * name - Name of desired segment
311 * exit:
312 * On success, returns pointer to descriptor. On failure, returns NULL.
314 Sg_desc *
315 ld_seg_lookup(Ofl_desc *ofl, const char *name, avl_index_t *where)
317 Sg_desc sg;
319 sg.sg_name = name;
320 return (avl_find(&ofl->ofl_segs_avl, &sg, where));
325 * Look up an entrance criteria record by name
327 * entry:
328 * mf - Mapfile descriptor
329 * name - Name of entrance criteria to locate
331 * exit:
332 * On success, a pointer to the entrace criteria record is
333 * returned. On failure, NULL is returned.
335 * note:
336 * Entrance criteria are not required to have names. Only
337 * named entrance criteria can be looked up via this method.
339 Ent_desc *
340 ld_ent_lookup(Ofl_desc *ofl, const char *name, avl_index_t *where)
342 Ent_desc en;
344 en.ec_name = name;
345 return (avl_find(&ofl->ofl_ents_avl, &en, where));
349 * Initialize new entrance and segment descriptors and add them as lists to
350 * the output file descriptor.
352 uintptr_t
353 ld_ent_setup(Ofl_desc *ofl, Xword segalign)
355 Ent_desc *enp;
356 predef_seg_t *psegs;
357 Sg_desc *sgp;
358 size_t idx;
361 * Initialize the elf library.
363 if (elf_version(EV_CURRENT) == EV_NONE) {
364 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ELF_LIBELF),
365 EV_CURRENT);
366 return (S_ERROR);
370 * Initialize internal Global Symbol Table AVL tree
372 avl_create(&ofl->ofl_symavl, &ld_sym_avl_comp, sizeof (Sym_avlnode),
373 SGSOFFSETOF(Sym_avlnode, sav_node));
375 /* Initialize segment AVL tree */
376 avl_create(&ofl->ofl_segs_avl, ofl_segs_avl_cmp,
377 sizeof (Sg_desc), SGSOFFSETOF(Sg_desc, sg_avlnode));
379 /* Initialize entrance criteria AVL tree */
380 avl_create(&ofl->ofl_ents_avl, ofl_ents_avl_cmp, sizeof (Ent_desc),
381 SGSOFFSETOF(Ent_desc, ec_avlnode));
385 * Allocate and initialize writable copies of both the entrance and
386 * segment descriptors.
388 * Note that on non-amd64 targets, this allocates a few more
389 * elements than are needed. For now, we are willing to overallocate
390 * a small amount to simplify the code.
392 if ((psegs = libld_malloc(sizeof (sg_desc))) == NULL)
393 return (S_ERROR);
394 (void) memcpy(psegs, &sg_desc, sizeof (sg_desc));
395 sgp = (Sg_desc *) psegs;
398 * The data segment and stack permissions can differ:
400 * - Architectural/ABI per-platform differences
401 * - Whether the object is built statically or dynamically
403 * Those segments so affected have their program header flags
404 * set here at runtime, rather than in the sg_desc templates above.
406 psegs->psg_data.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
407 psegs->psg_bss.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
408 psegs->psg_dynamic.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
409 psegs->psg_sunwdtrace.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
410 #if defined(_ELF64)
411 psegs->psg_ldata.sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm;
412 psegs->psg_sunwdtrace.sg_phdr.p_flags |= PF_X;
413 #endif
414 psegs->psg_sunwstack.sg_phdr.p_flags = ld_targ.t_m.m_stack_perm;
415 if ((ofl->ofl_flags & FLG_OF_DYNAMIC) == 0)
416 psegs->psg_data.sg_phdr.p_flags |= PF_X;
419 * Traverse the new entrance descriptor list converting the segment
420 * pointer entries to the absolute address within the new segment
421 * descriptor list. Add each entrance descriptor to the output file
422 * list.
424 if ((enp = libld_malloc(sizeof (ent_desc))) == NULL)
425 return (S_ERROR);
426 (void) memcpy(enp, ent_desc, sizeof (ent_desc));
427 for (idx = 0; idx < (sizeof (ent_desc) / sizeof (ent_desc[0])); idx++,
428 enp++) {
430 #if defined(_ELF64)
431 /* Don't use the amd64 entry conditions for non-amd64 targets */
432 if ((enp->ec_attrmask & SHF_AMD64_LARGE) &&
433 (ld_targ.t_m.m_mach != EM_AMD64))
434 continue;
435 #endif
436 if (aplist_append(&ofl->ofl_ents, enp,
437 AL_CNT_OFL_ENTRANCE) == NULL)
438 return (S_ERROR);
441 * The segment pointer is currently pointing at a template
442 * segment descriptor in sg_desc. Compute its array index,
443 * and then use that index to compute the address of the
444 * corresponding descriptor in the writable copy.
446 enp->ec_segment =
447 &sgp[(enp->ec_segment - (Sg_desc *) &sg_desc)];
451 * Add each segment descriptor to the segment descriptor list. The
452 * ones with non-NULL sg_name are also entered into the AVL tree.
453 * For each loadable segment initialize a default alignment. Note
454 * that ld(1) and ld.so.1 initialize this differently.
456 for (idx = 0; idx < predef_seg_nelts; idx++, sgp++) {
457 Phdr *phdr = &(sgp->sg_phdr);
459 #if defined(_ELF64)
460 /* Ignore amd64 segment templates for non-amd64 targets */
461 switch (sgp->sg_id) {
462 case SGID_LRODATA:
463 case SGID_LDATA:
464 if ((ld_targ.t_m.m_mach != EM_AMD64))
465 continue;
467 #endif
468 if (phdr->p_type == PT_LOAD)
469 phdr->p_align = segalign;
471 if ((aplist_append(&ofl->ofl_segs, sgp,
472 AL_CNT_SEGMENTS)) == NULL)
473 return (S_ERROR);
475 #ifdef NDEBUG /* assert() is enabled */
477 * Enforce the segment name rule: Any segment that can
478 * be referenced by an entrance descriptor must have
479 * a name. Any segment that cannot, must have a NULL
480 * name pointer.
482 switch (phdr->p_type) {
483 case PT_LOAD:
484 case PT_NOTE:
485 case PT_NULL:
486 assert(sgp->sg_name != NULL);
487 break;
488 default:
489 assert(sgp->sg_name == NULL);
490 break;
492 #endif
495 * Add named segment descriptors to the AVL tree to
496 * provide O(logN) lookups.
498 if (sgp->sg_name != NULL)
499 avl_add(&ofl->ofl_segs_avl, sgp);
502 return (1);