xref: /illumos-gate/usr/src/cmd/sgs/libld/common/groups.c (revision d227ea68051b5404410ee1880d0bba1ebb33a4cc)
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) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include	<stdio.h>
27 #include	<string.h>
28 #include	<link.h>
29 #include	<debug.h>
30 #include	"msg.h"
31 #include	"_libld.h"
32 
33 /*
34  * Determine whether a (COMDAT) group has already been encountered.  If so,
35  * indicate that the group descriptor has an overriding group (gd_oisc).  This
36  * indication triggers the ld_place_section() to discard this group, while the
37  * gd_oisc information provides for complete diagnostics of the override.
38  * Otherwise, this is the first occurrence of this group, therefore the group
39  * descriptor is saved for future comparisons.
40  */
41 static uintptr_t
gpavl_loaded(Ofl_desc * ofl,Group_desc * gdp)42 gpavl_loaded(Ofl_desc *ofl, Group_desc *gdp)
43 {
44 	Isd_node	isd, *isdp;
45 	avl_tree_t	*avlt;
46 	avl_index_t	where;
47 
48 	/*
49 	 * Create a groups avl tree if required.
50 	 */
51 	if ((avlt = ofl->ofl_groups) == NULL) {
52 		if ((avlt = libld_calloc(1, sizeof (avl_tree_t))) == NULL)
53 			return (S_ERROR);
54 		avl_create(avlt, isdavl_compare, sizeof (Isd_node),
55 		    SGSOFFSETOF(Isd_node, isd_avl));
56 		ofl->ofl_groups = avlt;
57 	}
58 
59 	/*
60 	 * An SHT_GROUP section is identified by the name of its signature
61 	 * symbol rather than section name. Although the section names are
62 	 * often unique, this is not required, and some compilers set it to
63 	 * a generic name like ".group".
64 	 */
65 	isd.isd_name = gdp->gd_name;
66 	isd.isd_hash = sgs_str_hash(isd.isd_name);
67 
68 	if ((isdp = avl_find(avlt, &isd, &where)) != NULL) {
69 		gdp->gd_oisc = isdp->isd_isp;
70 		return (1);
71 	}
72 
73 	/*
74 	 * This is a new group - so keep it.
75 	 */
76 	if ((isdp = libld_calloc(1, sizeof (Isd_node))) == NULL)
77 		return (S_ERROR);
78 
79 	isdp->isd_name = isd.isd_name;
80 	isdp->isd_hash = isd.isd_hash;
81 	isdp->isd_isp = gdp->gd_isc;
82 
83 	avl_insert(avlt, isdp, where);
84 	return (0);
85 }
86 
87 Group_desc *
ld_get_group(Ofl_desc * ofl,Is_desc * isp)88 ld_get_group(Ofl_desc *ofl, Is_desc *isp)
89 {
90 	Ifl_desc	*ifl = isp->is_file;
91 	uint_t		scnndx = isp->is_scnndx;
92 	Group_desc	*gdp;
93 	Aliste		idx;
94 
95 	/*
96 	 * Scan the GROUP sections associated with this file to find the
97 	 * matching group section.
98 	 */
99 	for (ALIST_TRAVERSE(ifl->ifl_groups, idx, gdp)) {
100 		size_t	ndx;
101 		Word	*data;
102 
103 		if (isp->is_shdr->sh_type == SHT_GROUP) {
104 			if (isp->is_scnndx == gdp->gd_isc->is_scnndx)
105 				return (gdp);
106 			continue;
107 		}
108 
109 		data = gdp->gd_data;
110 		for (ndx = 1; ndx < gdp->gd_cnt; ndx++) {
111 			if (data[ndx] == scnndx)
112 				return (gdp);
113 		}
114 	}
115 
116 	ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ELF_NOGROUPSECT),
117 	    ifl->ifl_name, EC_WORD(isp->is_scnndx), isp->is_name);
118 	return (NULL);
119 }
120 
121 /*
122  * When creating a .debug_macro section, in an attempt to make certain DWARF
123  * macro information shareable, the GNU compiler must construct group sections
124  * with a repeatable signature symbol while nevertheless having no actual
125  * symbol to refer to (because it relates to macros).
126  *
127  * We use this as yet another way to clue ourselves in that sloppy relocation
128  * will likely be required.
129  *
130  * The format of these gensym'd names is:
131  *    wm<offset size>.<encoded path name>.<lineno>.<32byte hash>
132  * Where the encoded file name may be absent.
133  */
134 static boolean_t
is_header_gensym(const char * name)135 is_header_gensym(const char *name)
136 {
137 	const char	*c = NULL;
138 	size_t		len = strlen(name);
139 
140 	/* No room for leader, hash, and periods */
141 	if (len < 37)
142 		return (B_FALSE);
143 
144 	if ((strncmp(name, "wm4.", 4) != 0) &&
145 	    strncmp(name, "wm8.", 4) != 0)
146 		return (B_FALSE);
147 
148 	c = &name[len - 33];
149 	if (*c++ != '.')
150 		return (B_FALSE);
151 
152 	for (; *c != '\0'; c++) {
153 		if (!(((*c >= 'a') && (*c <= 'f')) ||
154 		    ((*c >= '0') && (*c <= '9')))) {
155 			return (B_FALSE);
156 		}
157 	}
158 
159 	return (B_TRUE);
160 }
161 
162 uintptr_t
ld_group_process(Is_desc * gisc,Ofl_desc * ofl)163 ld_group_process(Is_desc *gisc, Ofl_desc *ofl)
164 {
165 	Ifl_desc	*gifl = gisc->is_file;
166 	Shdr		*sshdr, *gshdr = gisc->is_shdr;
167 	Word		*new_data = NULL;
168 	Shdr		*new_shdr = NULL;
169 	Is_desc		*isc;
170 	Sym		*sym;
171 	const char	*str;
172 	Group_desc	gd;
173 	size_t		ndx;
174 	int		gnu_stt_section;
175 
176 	/*
177 	 * Confirm that the sh_link points to a valid section.
178 	 */
179 	if ((gshdr->sh_link == SHN_UNDEF) ||
180 	    (gshdr->sh_link >= gifl->ifl_shnum) ||
181 	    ((isc = gifl->ifl_isdesc[gshdr->sh_link]) == NULL)) {
182 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHLINK),
183 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx),
184 		    gisc->is_name, EC_XWORD(gshdr->sh_link));
185 		return (0);
186 	}
187 	if (gshdr->sh_entsize == 0) {
188 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHENTSIZE),
189 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name,
190 		    EC_XWORD(gshdr->sh_entsize));
191 		return (0);
192 	}
193 
194 	/*
195 	 * Get the associated symbol table.  Sanity check the sh_info field
196 	 * (which points to the signature symbol table entry) against the size
197 	 * of the symbol table.
198 	 */
199 	sshdr = isc->is_shdr;
200 	sym = (Sym *)isc->is_indata->d_buf;
201 
202 	if ((sshdr->sh_info == SHN_UNDEF) ||
203 	    (gshdr->sh_info >= (Word)(sshdr->sh_size / sshdr->sh_entsize)) ||
204 	    ((isc = gifl->ifl_isdesc[sshdr->sh_link]) == NULL)) {
205 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
206 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name,
207 		    EC_XWORD(gshdr->sh_info));
208 		return (0);
209 	}
210 
211 	sym += gshdr->sh_info;
212 
213 	/*
214 	 * Get the symbol name from the associated string table.
215 	 */
216 	str = (char *)isc->is_indata->d_buf;
217 	str += sym->st_name;
218 
219 	/*
220 	 * The GNU assembler can use section symbols as the signature symbol
221 	 * as described by this comment in the gold linker (found via google):
222 	 *
223 	 *	It seems that some versions of gas will create a section group
224 	 *	associated with a section symbol, and then fail to give a name
225 	 *	to the section symbol.  In such a case, use the name of the
226 	 *	section.
227 	 *
228 	 * In order to support such objects, we do the same.
229 	 */
230 	gnu_stt_section = ((sym->st_name == 0) || (*str == '\0')) &&
231 	    (ELF_ST_TYPE(sym->st_info) == STT_SECTION);
232 	if (gnu_stt_section)
233 		str = gisc->is_name;
234 
235 
236 	/*
237 	 * Generate a group descriptor.
238 	 */
239 	gd.gd_isc = gisc;
240 	gd.gd_oisc = NULL;
241 	gd.gd_name = str;
242 	gd.gd_data = gisc->is_indata->d_buf;
243 	gd.gd_cnt = gisc->is_indata->d_size / sizeof (Word);
244 
245 	/*
246 	 * If the signature symbol is a name generated by the GNU compiler to
247 	 * refer to a header, we need sloppy relocation.
248 	 */
249 	if (is_header_gensym(str)) {
250 		if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
251 			ofl->ofl_flags1 |= FLG_OF1_RLXREL;
252 		DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, gisc, TRUE,
253 		    (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0));
254 	}
255 
256 	/*
257 	 * Validate the section indices within the group.  If this is a COMDAT
258 	 * group, mark each section as COMDAT.
259 	 */
260 	for (ndx = (gd.gd_cnt - 1); ndx >= 1; ndx--) {
261 		Word	gndx = gd.gd_data[ndx];
262 
263 		if ((gndx == 0) || (gndx >= gifl->ifl_shnum)) {
264 			ld_eprintf(ofl, ERR_FATAL,
265 			    MSG_INTL(MSG_GRP_INVALNDX), gifl->ifl_name,
266 			    EC_WORD(gisc->is_scnndx), gisc->is_name, ndx, gndx);
267 			return (0);
268 		}
269 
270 		/*
271 		 * If we reach here the group was valid but has been damaged
272 		 * by FLG_OF_STRIP.  That is, this section in the group exists
273 		 * but was ignored during input processing.
274 		 *
275 		 * If this entry is NULL, remove it from the group.  You might
276 		 * think we can make consuming code careful to be aware that
277 		 * entries in a group may not exist, but if we are linking a
278 		 * relocatable object (-r -s), the group must survive, and a
279 		 * bogus entry must not be written out.
280 		 */
281 		if (gifl->ifl_isdesc[gndx] == NULL) {
282 			/*
283 			 * We need to allocate new data for the group and the
284 			 * shdr, the existing data is mapped from the file.
285 			 */
286 			if (new_data == NULL) {
287 				if ((new_data = libld_calloc(sizeof (Word),
288 				    gd.gd_cnt)) == NULL)
289 					return (S_ERROR);
290 
291 				/*
292 				 * Copy up the whole thing, we'll shrink it in
293 				 * a moment.
294 				 */
295 				memcpy(new_data, gd.gd_data,
296 				    sizeof (Word) * gd.gd_cnt);
297 				gisc->is_indata->d_buf = gd.gd_data = new_data;
298 
299 				new_shdr = libld_malloc(sizeof (Shdr));
300 				if (new_shdr == NULL)
301 					return (S_ERROR);
302 
303 				memcpy(new_shdr, gisc->is_shdr, sizeof (Shdr));
304 				gisc->is_shdr = new_shdr;
305 			}
306 
307 			/* If there're entries after us, copy them down */
308 			if (ndx < (gd.gd_cnt - 1)) {
309 				memmove(&gd.gd_data[ndx], &gd.gd_data[ndx + 1],
310 				    (gd.gd_cnt - (ndx + 1)) * sizeof (Word));
311 			}
312 
313 			gisc->is_indata->d_size -= sizeof (Word);
314 			gisc->is_shdr->sh_size -= sizeof (Word);
315 			gd.gd_cnt -= 1;
316 			continue;
317 		}
318 
319 		if (gd.gd_data[0] & GRP_COMDAT)
320 			gifl->ifl_isdesc[gndx]->is_flags |= FLG_IS_COMDAT;
321 	}
322 
323 	/*
324 	 * If we're left with only one item in the group -- the header --
325 	 * discard it.
326 	 */
327 	if (gd.gd_cnt == 1)
328 		gisc->is_flags |= FLG_IS_DISCARD;
329 
330 	/*
331 	 * If this is a COMDAT group, determine whether this group has already
332 	 * been encountered, or whether this is the first instance of the group.
333 	 */
334 	if ((gd.gd_data[0] & GRP_COMDAT) &&
335 	    (gpavl_loaded(ofl, &gd) == S_ERROR))
336 		return (S_ERROR);
337 
338 	/*
339 	 * Associate the group descriptor with this input file.
340 	 */
341 	if (alist_append(&(gifl->ifl_groups), &gd, sizeof (Group_desc),
342 	    AL_CNT_IFL_GROUPS) == NULL)
343 		return (S_ERROR);
344 
345 	return (1);
346 }
347