xref: /illumos-gate/usr/src/cmd/sgs/libld/common/entry.c (revision 65a89a64c60f3061bbe2381edaacc81660af9a95)
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 2005 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 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
66 	{{PT_LOAD, PF_R, 0, 0, 0, 0, 0, 0},
67 		MSG_ORIG(MSG_ENT_LRODATA), 0, 0, {NULL, NULL}, {NULL, NULL},
68 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
69 	{{PT_LOAD, M_DATASEG_PERM, 0, 0, 0, 0, 0, 0},
70 		MSG_ORIG(MSG_ENT_LDATA), 0, 0, {NULL, NULL}, {NULL, NULL},
71 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
72 #endif
73 	{{PT_DYNAMIC, M_DATASEG_PERM, 0, 0, 0, 0, 0, 0},
74 		MSG_ORIG(MSG_ENT_DYNAMIC), 0, 0, {NULL, NULL}, {NULL, NULL},
75 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
76 	{{PT_SUNWDTRACE, M_DATASEG_PERM | PF_X, 0, 0, 0, 0, 0, 0},
77 		MSG_ORIG(MSG_ENT_DTRACE), 0, 0, {NULL, NULL}, {NULL, NULL},
78 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
79 	{{PT_NOTE, 0, 0, 0, 0, 0, 0, 0},
80 		MSG_ORIG(MSG_ENT_NOTE), 0, 0, {NULL, NULL}, {NULL, NULL},
81 		FLG_SG_TYPE, NULL, 0, 0},
82 	{{PT_SUNWBSS, 0, 0, 0, 0, 0, 0, 0},
83 		MSG_ORIG(MSG_ENT_SUNWBSS), 0, 0, {NULL, NULL}, {NULL, NULL},
84 		FLG_SG_TYPE, NULL, 0, 0},
85 	{{PT_TLS, PF_R, 0, 0, 0, 0, 0, 0},
86 		MSG_ORIG(MSG_ENT_TLS), 0, 0, {NULL, NULL}, {NULL, NULL},
87 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
88 #if	defined(__i386) || defined(__amd64)
89 	{{PT_SUNW_UNWIND, PF_R, 0, 0, 0, 0, 0, 0},
90 		MSG_ORIG(MSG_ENT_UNWIND), 0, 0, {NULL, NULL}, {NULL, NULL},
91 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
92 #endif
93 	{{PT_NULL, 0, 0, 0, 0, 0, 0, 0},
94 		MSG_ORIG(MSG_STR_EMPTY), 0, 0, {NULL, NULL}, {NULL, NULL},
95 		FLG_SG_TYPE, NULL, 0, 0}
96 };
97 #else  /* Elf32 */
98 static Sg_desc sg_desc[LD_NUM] = {
99 	{{PT_PHDR, 0, 0, 0, 0, 0, PF_R + PF_X, 0},
100 		MSG_ORIG(MSG_ENT_PHDR), 0, 0, {NULL, NULL}, {NULL, NULL},
101 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
102 	{{PT_INTERP, 0, 0, 0, 0, 0, PF_R, 0},
103 		MSG_ORIG(MSG_ENT_INTERP), 0, 0, {NULL, NULL}, {NULL, NULL},
104 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
105 	{{PT_SUNWCAP, 0, 0, 0, 0, 0, PF_R, 0},
106 		MSG_ORIG(MSG_ENT_SUNWCAP), 0, 0, {NULL, NULL}, {NULL, NULL},
107 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
108 	{{PT_LOAD, 0, 0, 0, 0, 0, PF_R + PF_X, 0},
109 		MSG_ORIG(MSG_ENT_TEXT), 0, 0, {NULL, NULL}, {NULL, NULL},
110 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
111 	{{PT_LOAD, 0, 0, 0, 0, 0, M_DATASEG_PERM, 0},
112 		MSG_ORIG(MSG_ENT_DATA), 0, 0, {NULL, NULL}, {NULL, NULL},
113 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
114 	{{PT_LOAD, 0, 0, 0, 0, 0, M_DATASEG_PERM, 0},
115 		MSG_ORIG(MSG_ENT_BSS), 0, 0, {NULL, NULL}, {NULL, NULL},
116 		(FLG_SG_TYPE | FLG_SG_FLAGS | FLG_SG_DISABLED), NULL, 0, 0},
117 	{{PT_DYNAMIC, 0, 0, 0, 0, 0, M_DATASEG_PERM, 0},
118 		MSG_ORIG(MSG_ENT_DYNAMIC), 0, 0, {NULL, NULL}, {NULL, NULL},
119 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
120 	{{PT_SUNWDTRACE, 0, 0, 0, 0, 0, M_DATASEG_PERM, 0},
121 		MSG_ORIG(MSG_ENT_DTRACE), 0, 0, {NULL, NULL}, {NULL, NULL},
122 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
123 	{{PT_NOTE, 0, 0, 0, 0, 0, 0, 0},
124 		MSG_ORIG(MSG_ENT_NOTE), 0, 0, {NULL, NULL}, {NULL, NULL},
125 		FLG_SG_TYPE, NULL, 0, 0},
126 	{{PT_SUNWBSS, 0, 0, 0, 0, 0, 0, 0},
127 		MSG_ORIG(MSG_ENT_SUNWBSS), 0, 0, {NULL, NULL}, {NULL, NULL},
128 		FLG_SG_TYPE, NULL, 0, 0},
129 	{{PT_TLS, PF_R, 0, 0, 0, 0, 0, 0},
130 		MSG_ORIG(MSG_ENT_TLS), 0, 0, {NULL, NULL}, {NULL, NULL},
131 		(FLG_SG_TYPE | FLG_SG_FLAGS), NULL, 0, 0},
132 	{{PT_NULL, 0, 0, 0, 0, 0, 0, 0},
133 		MSG_ORIG(MSG_STR_EMPTY), 0, 0, {NULL, NULL}, {NULL, NULL},
134 		FLG_SG_TYPE, NULL, 0, 0}
135 };
136 #endif /* Elfxx */
137 
138 
139 /*
140  * The input processing of the loader involves matching the sections of its
141  * input files to an `entrance descriptor definition'.  The entrance criteria
142  * is different for either a static or dynamic linkage, and may even be
143  * modified further using a map file.  Each entrance criteria is associated
144  * with a segment descriptor, thus a mapping of input sections to output
145  * segments is maintained.
146  */
147 static const Ent_desc	ent_desc[] = {
148 	{{NULL, NULL}, MSG_ORIG(MSG_SCN_SUNWBSS), NULL,
149 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
150 		(Sg_desc *)LD_SUNWBSS, 0, FALSE},
151 	{{NULL, NULL}, NULL, SHT_NOTE, 0, 0,
152 		(Sg_desc *)LD_NOTE, 0, FALSE},
153 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
154 	{{NULL, NULL}, MSG_ORIG(MSG_SCN_LRODATA), NULL,
155 		SHF_ALLOC + SHF_AMD64_LARGE, SHF_ALLOC + SHF_AMD64_LARGE,
156 		(Sg_desc *)LD_LRODATA, 0, FALSE},
157 #endif
158 	{{NULL, NULL}, NULL, NULL,
159 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC,
160 		(Sg_desc *)LD_TEXT, 0, FALSE},
161 	{{NULL, NULL}, NULL, SHT_NOBITS,
162 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
163 		(Sg_desc *)LD_BSS, 0, FALSE},
164 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
165 	{{NULL, NULL}, NULL, SHT_NOBITS,
166 		SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
167 		SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
168 		(Sg_desc *)LD_DATA, 0, FALSE},
169 	{{NULL, NULL}, NULL, NULL,
170 		SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
171 		SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE,
172 		(Sg_desc *)LD_LDATA, 0, FALSE},
173 #endif
174 	{{NULL, NULL}, NULL, NULL,
175 		SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE,
176 		(Sg_desc *)LD_DATA, 0, FALSE},
177 	{{NULL, NULL}, NULL, 0, 0, 0,
178 		(Sg_desc *)LD_EXTRA, 0, FALSE}
179 };
180 
181 /*
182  * Initialize new entrance and segment descriptors and add them as lists to
183  * the output file descriptor.
184  */
185 uintptr_t
186 ent_setup(Ofl_desc * ofl, Xword segalign)
187 {
188 	Ent_desc *	enp;
189 	Sg_desc *	sgp;
190 	size_t		size;
191 
192 	/*
193 	 * Initialize the elf library.
194 	 */
195 	if (elf_version(EV_CURRENT) == EV_NONE) {
196 		eprintf(ERR_FATAL, MSG_INTL(MSG_ELF_LIBELF), EV_CURRENT);
197 		return (S_ERROR);
198 	}
199 
200 	/*
201 	 * Initialize internal Global Symbol Table AVL tree
202 	 */
203 	avl_create(&ofl->ofl_symavl, &sym_avl_comp, sizeof (Sym_avlnode),
204 		SGSOFFSETOF(Sym_avlnode, sav_node));
205 	/*
206 	 * The datasegment permissions can differ depending on whether
207 	 * this object is built statically or dynamically.
208 	 */
209 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
210 		sg_desc[LD_DATA].sg_phdr.p_flags = M_DATASEG_PERM;
211 		sg_desc[LD_SUNWBSS].sg_phdr.p_flags = M_DATASEG_PERM;
212 	} else {
213 		sg_desc[LD_DATA].sg_phdr.p_flags = M_DATASEG_PERM | PF_X;
214 	}
215 
216 	/*
217 	 * Allocate and initialize writable copies of both the entrance and
218 	 * segment descriptors.
219 	 */
220 	if ((sgp = libld_malloc(sizeof (sg_desc))) == 0)
221 		return (S_ERROR);
222 	(void) memcpy(sgp, sg_desc, sizeof (sg_desc));
223 	if ((enp = libld_malloc(sizeof (ent_desc))) == 0)
224 		return (S_ERROR);
225 	(void) memcpy(enp, ent_desc, sizeof (ent_desc));
226 
227 	/*
228 	 * Traverse the new entrance descriptor list converting the segment
229 	 * pointer entries to the absolute address within the new segment
230 	 * descriptor list.  Add each entrance descriptor to the output file
231 	 * list.
232 	 */
233 	for (size = 0; size < sizeof (ent_desc); size += sizeof (Ent_desc)) {
234 		enp->ec_segment = &sgp[(long)enp->ec_segment];
235 		if ((list_appendc(&ofl->ofl_ents, enp)) == 0)
236 			return (S_ERROR);
237 		enp++;
238 	}
239 
240 	/*
241 	 * Traverse the new segment descriptor list adding each entry to the
242 	 * segment descriptor list.  For each loadable segment initialize
243 	 * a default alignment (ld(1) and ld.so.1 initialize this differently).
244 	 */
245 	for (size = 0; size < sizeof (sg_desc); size += sizeof (Sg_desc)) {
246 
247 		Phdr *	phdr = &(sgp->sg_phdr);
248 
249 		if ((list_appendc(&ofl->ofl_segs, sgp)) == 0)
250 			return (S_ERROR);
251 		if (phdr->p_type == PT_LOAD)
252 			phdr->p_align = segalign;
253 
254 		sgp++;
255 	}
256 	return (1);
257 }
258