xref: /titanic_44/usr/src/cmd/sgs/libld/common/entry.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  *
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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  * The loader uses a `segment descriptor' list to describe the output
41  * segments it can potentially create.   Additional segments may be added
42  * using a map file.
43  */
44 #ifdef _ELF64
45 /* Phdr packing changes under Elf64 */
46 static Sg_desc sg_desc[LD_NUM] = {
47 	{{PT_PHDR, PF_R + PF_X, 0, 0, 0, 0, 0, 0},
48 		MSG_ORIG(MSG_ENT_PHDR), 0, 0, {NULL, NULL}, {NULL, NULL},
49 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
50 	{{PT_INTERP, PF_R, 0, 0, 0, 0, 0, 0},
51 		MSG_ORIG(MSG_ENT_INTERP), 0, 0, {NULL, NULL}, {NULL, NULL},
52 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
53 	{{PT_SUNWCAP, PF_R, 0, 0, 0, 0, 0, 0},
54 		MSG_ORIG(MSG_ENT_SUNWCAP), 0, 0, {NULL, NULL}, {NULL, NULL},
55 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
56 	{{PT_LOAD, PF_R + PF_X, 0, 0, 0, 0, 0, 0},
57 		MSG_ORIG(MSG_ENT_TEXT), 0, 0, {NULL, NULL}, {NULL, NULL},
58 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
59 	{{PT_LOAD, M_DATASEG_PERM, 0, 0, 0, 0, 0, 0},
60 		MSG_ORIG(MSG_ENT_DATA), 0, 0, {NULL, NULL}, {NULL, NULL},
61 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
62 	{{PT_LOAD, M_DATASEG_PERM, 0, 0, 0, 0, 0, 0},
63 		MSG_ORIG(MSG_ENT_BSS), 0, 0, {NULL, NULL}, {NULL, NULL},
64 		(FLG_SG_TYPE | FLG_SG_FLAGS | FLG_SG_DISABLED), NULL, 0, 0},
65 	{{PT_DYNAMIC, M_DATASEG_PERM, 0, 0, 0, 0, 0, 0},
66 		MSG_ORIG(MSG_ENT_DYNAMIC), 0, 0, {NULL, NULL}, {NULL, NULL},
67 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
68 	{{PT_SUNWDTRACE, M_DATASEG_PERM | PF_X, 0, 0, 0, 0, 0, 0},
69 		MSG_ORIG(MSG_ENT_DTRACE), 0, 0, {NULL, NULL}, {NULL, NULL},
70 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
71 	{{PT_NOTE, 0, 0, 0, 0, 0, 0, 0},
72 		MSG_ORIG(MSG_ENT_NOTE), 0, 0, {NULL, NULL}, {NULL, NULL},
73 		FLG_SG_TYPE, NULL, 0, 0},
74 	{{PT_SUNWBSS, 0, 0, 0, 0, 0, 0, 0},
75 		MSG_ORIG(MSG_ENT_SUNWBSS), 0, 0, {NULL, NULL}, {NULL, NULL},
76 		FLG_SG_TYPE, NULL, 0, 0},
77 	{{PT_TLS, PF_R, 0, 0, 0, 0, 0, 0},
78 		MSG_ORIG(MSG_ENT_TLS), 0, 0, {NULL, NULL}, {NULL, NULL},
79 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
80 #if	defined(__i386) || defined(__amd64)
81 	{{PT_SUNW_UNWIND, PF_R, 0, 0, 0, 0, 0, 0},
82 		MSG_ORIG(MSG_ENT_UNWIND), 0, 0, {NULL, NULL}, {NULL, NULL},
83 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
84 #endif
85 	{{PT_NULL, 0, 0, 0, 0, 0, 0, 0},
86 		MSG_ORIG(MSG_STR_EMPTY), 0, 0, {NULL, NULL}, {NULL, NULL},
87 		FLG_SG_TYPE, NULL, 0, 0}
88 };
89 #else  /* Elf32 */
90 static Sg_desc sg_desc[LD_NUM] = {
91 	{{PT_PHDR, 0, 0, 0, 0, 0, PF_R + PF_X, 0},
92 		MSG_ORIG(MSG_ENT_PHDR), 0, 0, {NULL, NULL}, {NULL, NULL},
93 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
94 	{{PT_INTERP, 0, 0, 0, 0, 0, PF_R, 0},
95 		MSG_ORIG(MSG_ENT_INTERP), 0, 0, {NULL, NULL}, {NULL, NULL},
96 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
97 	{{PT_SUNWCAP, 0, 0, 0, 0, 0, PF_R, 0},
98 		MSG_ORIG(MSG_ENT_SUNWCAP), 0, 0, {NULL, NULL}, {NULL, NULL},
99 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
100 	{{PT_LOAD, 0, 0, 0, 0, 0, PF_R + PF_X, 0},
101 		MSG_ORIG(MSG_ENT_TEXT), 0, 0, {NULL, NULL}, {NULL, NULL},
102 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
103 	{{PT_LOAD, 0, 0, 0, 0, 0, M_DATASEG_PERM, 0},
104 		MSG_ORIG(MSG_ENT_DATA), 0, 0, {NULL, NULL}, {NULL, NULL},
105 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
106 	{{PT_LOAD, 0, 0, 0, 0, 0, M_DATASEG_PERM, 0},
107 		MSG_ORIG(MSG_ENT_BSS), 0, 0, {NULL, NULL}, {NULL, NULL},
108 		(FLG_SG_TYPE | FLG_SG_FLAGS | FLG_SG_DISABLED), NULL, 0, 0},
109 	{{PT_DYNAMIC, 0, 0, 0, 0, 0, M_DATASEG_PERM, 0},
110 		MSG_ORIG(MSG_ENT_DYNAMIC), 0, 0, {NULL, NULL}, {NULL, NULL},
111 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
112 	{{PT_SUNWDTRACE, 0, 0, 0, 0, 0, M_DATASEG_PERM, 0},
113 		MSG_ORIG(MSG_ENT_DTRACE), 0, 0, {NULL, NULL}, {NULL, NULL},
114 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
115 	{{PT_NOTE, 0, 0, 0, 0, 0, 0, 0},
116 		MSG_ORIG(MSG_ENT_NOTE), 0, 0, {NULL, NULL}, {NULL, NULL},
117 		FLG_SG_TYPE, NULL, 0, 0},
118 	{{PT_SUNWBSS, 0, 0, 0, 0, 0, 0, 0},
119 		MSG_ORIG(MSG_ENT_SUNWBSS), 0, 0, {NULL, NULL}, {NULL, NULL},
120 		FLG_SG_TYPE, NULL, 0, 0},
121 	{{PT_TLS, PF_R, 0, 0, 0, 0, 0, 0},
122 		MSG_ORIG(MSG_ENT_TLS), 0, 0, {NULL, NULL}, {NULL, NULL},
123 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
124 	{{PT_NULL, 0, 0, 0, 0, 0, 0, 0},
125 		MSG_ORIG(MSG_STR_EMPTY), 0, 0, {NULL, NULL}, {NULL, NULL},
126 		FLG_SG_TYPE, NULL, 0, 0}
127 };
128 #endif /* Elfxx */
129 
130 
131 /*
132  * The input processing of the loader involves matching the sections of its
133  * input files to an `entrance descriptor definition'.  The entrance criteria
134  * is different for either a static or dynamic linkage, and may even be
135  * modified further using a map file.  Each entrance criteria is associated
136  * with a segment descriptor, thus a mapping of input sections to output
137  * segments is maintained.
138  */
139 static const Ent_desc	ent_desc[] = {
140 	{{NULL, NULL}, MSG_ORIG(MSG_SCN_SUNWBSS), NULL,
141 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
142 		(Sg_desc *)LD_SUNWBSS, 0, FALSE},
143 	{{NULL, NULL}, NULL, SHT_NOTE, 0, 0,
144 		(Sg_desc *)LD_NOTE, 0, FALSE},
145 	{{NULL, NULL}, NULL, NULL,
146 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC,
147 		(Sg_desc *)LD_TEXT, 0, FALSE},
148 	{{NULL, NULL}, NULL, SHT_NOBITS,
149 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
150 		(Sg_desc *)LD_BSS, 0, FALSE},
151 	{{NULL, NULL}, NULL, NULL,
152 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
153 		(Sg_desc *)LD_DATA, 0, FALSE},
154 	{{NULL, NULL}, NULL, 0, 0, 0,
155 		(Sg_desc *)LD_EXTRA, 0, FALSE}
156 };
157 
158 /*
159  * Initialize new entrance and segment descriptors and add them as lists to
160  * the output file descriptor.
161  */
162 uintptr_t
163 ent_setup(Ofl_desc * ofl, Xword segalign)
164 {
165 	Ent_desc *	enp;
166 	Sg_desc *	sgp;
167 	size_t		size;
168 
169 	/*
170 	 * Initialize the elf library.
171 	 */
172 	if (elf_version(EV_CURRENT) == EV_NONE) {
173 		eprintf(ERR_FATAL, MSG_INTL(MSG_ELF_LIBELF), EV_CURRENT);
174 		return (S_ERROR);
175 	}
176 
177 	/*
178 	 * Initialize internal Global Symbol Table AVL tree
179 	 */
180 	avl_create(&ofl->ofl_symavl, &sym_avl_comp, sizeof (Sym_avlnode),
181 		SGSOFFSETOF(Sym_avlnode, sav_node));
182 	/*
183 	 * The datasegment permissions can differ depending on whether
184 	 * this object is built statically or dynamically.
185 	 */
186 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
187 		sg_desc[LD_DATA].sg_phdr.p_flags = M_DATASEG_PERM;
188 		sg_desc[LD_SUNWBSS].sg_phdr.p_flags = M_DATASEG_PERM;
189 	} else {
190 		sg_desc[LD_DATA].sg_phdr.p_flags = M_DATASEG_PERM | PF_X;
191 	}
192 
193 	/*
194 	 * Allocate and initialize writable copies of both the entrance and
195 	 * segment descriptors.
196 	 */
197 	if ((sgp = libld_malloc(sizeof (sg_desc))) == 0)
198 		return (S_ERROR);
199 	(void) memcpy(sgp, sg_desc, sizeof (sg_desc));
200 	if ((enp = libld_malloc(sizeof (ent_desc))) == 0)
201 		return (S_ERROR);
202 	(void) memcpy(enp, ent_desc, sizeof (ent_desc));
203 
204 	/*
205 	 * Traverse the new entrance descriptor list converting the segment
206 	 * pointer entries to the absolute address within the new segment
207 	 * descriptor list.  Add each entrance descriptor to the output file
208 	 * list.
209 	 */
210 	for (size = 0; size < sizeof (ent_desc); size += sizeof (Ent_desc)) {
211 		enp->ec_segment = &sgp[(long)enp->ec_segment];
212 		if ((list_appendc(&ofl->ofl_ents, enp)) == 0)
213 			return (S_ERROR);
214 		enp++;
215 	}
216 
217 	/*
218 	 * Traverse the new segment descriptor list adding each entry to the
219 	 * segment descriptor list.  For each loadable segment initialize
220 	 * a default alignment (ld(1) and ld.so.1 initialize this differently).
221 	 */
222 	for (size = 0; size < sizeof (sg_desc); size += sizeof (Sg_desc)) {
223 
224 		Phdr *	phdr = &(sgp->sg_phdr);
225 
226 		if ((list_appendc(&ofl->ofl_segs, sgp)) == 0)
227 			return (S_ERROR);
228 		if (phdr->p_type == PT_LOAD)
229 			phdr->p_align = segalign;
230 
231 		sgp++;
232 	}
233 	return (1);
234 }
235