xref: /titanic_51/usr/src/cmd/sgs/libld/common/groups.c (revision 359db861fd14071f8a25831efe3bf3790980d071)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23bf994817SAli Bahrami  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include	<stdio.h>
277c478bd9Sstevel@tonic-gate #include	<string.h>
287c478bd9Sstevel@tonic-gate #include	<link.h>
295aefb655Srie #include	<debug.h>
307c478bd9Sstevel@tonic-gate #include	"msg.h"
317c478bd9Sstevel@tonic-gate #include	"_libld.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
34cc7efc4fSrie  * Determine whether a (COMDAT) group has already been encountered.  If so,
350e233487SRod Evans  * indicate that the group descriptor has an overriding group (gd_oisc).  This
360e233487SRod Evans  * indication triggers the ld_place_section() to discard this group, while the
370e233487SRod Evans  * gd_oisc information provides for complete diagnostics of the override.
380e233487SRod Evans  * Otherwise, this is the first occurrence of this group, therefore the group
390e233487SRod Evans  * descriptor is saved for future comparisons.
40cc7efc4fSrie  */
415aefb655Srie static uintptr_t
42cc7efc4fSrie gpavl_loaded(Ofl_desc *ofl, Group_desc *gdp)
43cc7efc4fSrie {
446b3ba5bdSAli Bahrami 	Isd_node	isd, *isdp;
45cc7efc4fSrie 	avl_tree_t	*avlt;
46cc7efc4fSrie 	avl_index_t	where;
47cc7efc4fSrie 
48cc7efc4fSrie 	/*
496b3ba5bdSAli Bahrami 	 * Create a groups avl tree if required.
50cc7efc4fSrie 	 */
516b3ba5bdSAli Bahrami 	if ((avlt = ofl->ofl_groups) == NULL) {
526b3ba5bdSAli Bahrami 		if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
53cc7efc4fSrie 			return (S_ERROR);
546b3ba5bdSAli Bahrami 		avl_create(avlt, isdavl_compare, sizeof (Isd_node),
556b3ba5bdSAli Bahrami 		    SGSOFFSETOF(Isd_node, isd_avl));
56cc7efc4fSrie 		ofl->ofl_groups = avlt;
57cc7efc4fSrie 	}
58cc7efc4fSrie 
59e64d0ff9SAli Bahrami 	/*
60e64d0ff9SAli Bahrami 	 * An SHT_GROUP section is identified by the name of its signature
61e64d0ff9SAli Bahrami 	 * symbol rather than section name. Although the section names are
62e64d0ff9SAli Bahrami 	 * often unique, this is not required, and some compilers set it to
63e64d0ff9SAli Bahrami 	 * a generic name like ".group".
64e64d0ff9SAli Bahrami 	 */
65e64d0ff9SAli Bahrami 	isd.isd_name = gdp->gd_name;
66e64d0ff9SAli Bahrami 	isd.isd_hash = sgs_str_hash(isd.isd_name);
67cc7efc4fSrie 
686b3ba5bdSAli Bahrami 	if ((isdp = avl_find(avlt, &isd, &where)) != NULL) {
696b3ba5bdSAli Bahrami 		gdp->gd_oisc = isdp->isd_isp;
70cc7efc4fSrie 		return (1);
71cc7efc4fSrie 	}
72cc7efc4fSrie 
73cc7efc4fSrie 	/*
746b3ba5bdSAli Bahrami 	 * This is a new group - so keep it.
75cc7efc4fSrie 	 */
766b3ba5bdSAli Bahrami 	if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL)
77cc7efc4fSrie 		return (S_ERROR);
78cc7efc4fSrie 
79e64d0ff9SAli Bahrami 	isdp->isd_name = isd.isd_name;
806b3ba5bdSAli Bahrami 	isdp->isd_hash = isd.isd_hash;
81e64d0ff9SAli Bahrami 	isdp->isd_isp = gdp->gd_isc;
82cc7efc4fSrie 
836b3ba5bdSAli Bahrami 	avl_insert(avlt, isdp, where);
84cc7efc4fSrie 	return (0);
85cc7efc4fSrie }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate Group_desc *
885aefb655Srie ld_get_group(Ofl_desc *ofl, Is_desc *isp)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = isp->is_file;
917c478bd9Sstevel@tonic-gate 	uint_t		scnndx = isp->is_scnndx;
92cc7efc4fSrie 	Group_desc	*gdp;
93cce0e03bSab196087 	Aliste		idx;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/*
96cc7efc4fSrie 	 * Scan the GROUP sections associated with this file to find the
97cc7efc4fSrie 	 * matching group section.
987c478bd9Sstevel@tonic-gate 	 */
99cce0e03bSab196087 	for (ALIST_TRAVERSE(ifl->ifl_groups, idx, gdp)) {
100cc7efc4fSrie 		size_t	ndx;
101cc7efc4fSrie 		Word	*data;
102cc7efc4fSrie 
1037c478bd9Sstevel@tonic-gate 		if (isp->is_shdr->sh_type == SHT_GROUP) {
1040e233487SRod Evans 			if (isp->is_scnndx == gdp->gd_isc->is_scnndx)
105cc7efc4fSrie 				return (gdp);
1067c478bd9Sstevel@tonic-gate 			continue;
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 
109cc7efc4fSrie 		data = gdp->gd_data;
110cc7efc4fSrie 		for (ndx = 1; ndx < gdp->gd_cnt; ndx++) {
111cc7efc4fSrie 			if (data[ndx] == scnndx)
112cc7efc4fSrie 				return (gdp);
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 	}
115cc7efc4fSrie 
1161007fd6fSAli Bahrami 	ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ELF_NOGROUPSECT),
1174a8d0ea7SAli Bahrami 	    ifl->ifl_name, EC_WORD(isp->is_scnndx), isp->is_name);
1180e233487SRod Evans 	return (NULL);
1190e233487SRod Evans }
1200e233487SRod Evans 
121*ef16f6b5SRichard Lowe /*
122*ef16f6b5SRichard Lowe  * When creating a .debug_macro section, in an attempt to make certain DWARF
123*ef16f6b5SRichard Lowe  * macro information shareable, the GNU compiler must construct group sections
124*ef16f6b5SRichard Lowe  * with a repeatable signature symbol while nevertheless having no actual
125*ef16f6b5SRichard Lowe  * symbol to refer to (because it relates to macros).
126*ef16f6b5SRichard Lowe  *
127*ef16f6b5SRichard Lowe  * We use this as yet another way to clue ourselves in that sloppy relocation
128*ef16f6b5SRichard Lowe  * will likely be required.
129*ef16f6b5SRichard Lowe  *
130*ef16f6b5SRichard Lowe  * The format of these gensym'd names is:
131*ef16f6b5SRichard Lowe  *    wm<offset size>.<encoded path name>.<lineno>.<32byte hash>
132*ef16f6b5SRichard Lowe  * Where the encoded file name may be absent.
133*ef16f6b5SRichard Lowe  */
134*ef16f6b5SRichard Lowe static boolean_t
135*ef16f6b5SRichard Lowe is_header_gensym(const char *name)
136*ef16f6b5SRichard Lowe {
137*ef16f6b5SRichard Lowe 	const char	*c = NULL;
138*ef16f6b5SRichard Lowe 	size_t		len = strlen(name);
139*ef16f6b5SRichard Lowe 
140*ef16f6b5SRichard Lowe 	/* No room for leader, hash, and periods */
141*ef16f6b5SRichard Lowe 	if (len < 37)
142*ef16f6b5SRichard Lowe 		return (B_FALSE);
143*ef16f6b5SRichard Lowe 
144*ef16f6b5SRichard Lowe 	if ((strncmp(name, "wm4.", 4) != 0) &&
145*ef16f6b5SRichard Lowe 	    strncmp(name, "wm8.", 4) != 0)
146*ef16f6b5SRichard Lowe 		return (B_FALSE);
147*ef16f6b5SRichard Lowe 
148*ef16f6b5SRichard Lowe 	c = &name[len - 33];
149*ef16f6b5SRichard Lowe 	if (*c++ != '.')
150*ef16f6b5SRichard Lowe 		return (B_FALSE);
151*ef16f6b5SRichard Lowe 
152*ef16f6b5SRichard Lowe 	for (; *c != '\0'; c++) {
153*ef16f6b5SRichard Lowe 		if (!(((*c >= 'a') && (*c <= 'f')) ||
154*ef16f6b5SRichard Lowe 		    ((*c >= '0') && (*c <= '9')))) {
155*ef16f6b5SRichard Lowe 			return (B_FALSE);
156*ef16f6b5SRichard Lowe 		}
157*ef16f6b5SRichard Lowe 	}
158*ef16f6b5SRichard Lowe 
159*ef16f6b5SRichard Lowe 	return (B_TRUE);
160*ef16f6b5SRichard Lowe }
161*ef16f6b5SRichard Lowe 
1620e233487SRod Evans uintptr_t
1630e233487SRod Evans ld_group_process(Is_desc *gisc, Ofl_desc *ofl)
1640e233487SRod Evans {
1650e233487SRod Evans 	Ifl_desc	*gifl = gisc->is_file;
1660e233487SRod Evans 	Shdr		*sshdr, *gshdr = gisc->is_shdr;
1670e233487SRod Evans 	Is_desc		*isc;
1680e233487SRod Evans 	Sym		*sym;
1694a8d0ea7SAli Bahrami 	const char	*str;
1700e233487SRod Evans 	Group_desc	gd;
1710e233487SRod Evans 	size_t		ndx;
172e64d0ff9SAli Bahrami 	int		gnu_stt_section;
1730e233487SRod Evans 
1740e233487SRod Evans 	/*
1750e233487SRod Evans 	 * Confirm that the sh_link points to a valid section.
1760e233487SRod Evans 	 */
1770e233487SRod Evans 	if ((gshdr->sh_link == SHN_UNDEF) ||
1780e233487SRod Evans 	    (gshdr->sh_link >= gifl->ifl_shnum) ||
1790e233487SRod Evans 	    ((isc = gifl->ifl_isdesc[gshdr->sh_link]) == NULL)) {
1801007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHLINK),
1814a8d0ea7SAli Bahrami 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx),
1824a8d0ea7SAli Bahrami 		    gisc->is_name, EC_XWORD(gshdr->sh_link));
183cc7efc4fSrie 		return (0);
1847c478bd9Sstevel@tonic-gate 	}
1850e233487SRod Evans 	if (gshdr->sh_entsize == 0) {
1861007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHENTSIZE),
1874a8d0ea7SAli Bahrami 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name,
1884a8d0ea7SAli Bahrami 		    EC_XWORD(gshdr->sh_entsize));
1890e233487SRod Evans 		return (0);
1900e233487SRod Evans 	}
1910e233487SRod Evans 
1920e233487SRod Evans 	/*
1930e233487SRod Evans 	 * Get the associated symbol table.  Sanity check the sh_info field
1940e233487SRod Evans 	 * (which points to the signature symbol table entry) against the size
1950e233487SRod Evans 	 * of the symbol table.
1960e233487SRod Evans 	 */
1970e233487SRod Evans 	sshdr = isc->is_shdr;
1980e233487SRod Evans 	sym = (Sym *)isc->is_indata->d_buf;
1990e233487SRod Evans 
2000e233487SRod Evans 	if ((sshdr->sh_info == SHN_UNDEF) ||
2010e233487SRod Evans 	    (gshdr->sh_info >= (Word)(sshdr->sh_size / sshdr->sh_entsize)) ||
2020e233487SRod Evans 	    ((isc = gifl->ifl_isdesc[sshdr->sh_link]) == NULL)) {
2031007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
2044a8d0ea7SAli Bahrami 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name,
2054a8d0ea7SAli Bahrami 		    EC_XWORD(gshdr->sh_info));
2060e233487SRod Evans 		return (0);
2070e233487SRod Evans 	}
2080e233487SRod Evans 
2090e233487SRod Evans 	sym += gshdr->sh_info;
2100e233487SRod Evans 
2110e233487SRod Evans 	/*
2120e233487SRod Evans 	 * Get the symbol name from the associated string table.
2130e233487SRod Evans 	 */
2140e233487SRod Evans 	str = (char *)isc->is_indata->d_buf;
2150e233487SRod Evans 	str += sym->st_name;
2160e233487SRod Evans 
2170e233487SRod Evans 	/*
218e64d0ff9SAli Bahrami 	 * The GNU assembler can use section symbols as the signature symbol
219e64d0ff9SAli Bahrami 	 * as described by this comment in the gold linker (found via google):
220e64d0ff9SAli Bahrami 	 *
221e64d0ff9SAli Bahrami 	 *	It seems that some versions of gas will create a section group
222e64d0ff9SAli Bahrami 	 *	associated with a section symbol, and then fail to give a name
223e64d0ff9SAli Bahrami 	 *	to the section symbol.  In such a case, use the name of the
224e64d0ff9SAli Bahrami 	 *	section.
225e64d0ff9SAli Bahrami 	 *
226e64d0ff9SAli Bahrami 	 * In order to support such objects, we do the same.
227e64d0ff9SAli Bahrami 	 */
228e64d0ff9SAli Bahrami 	gnu_stt_section = ((sym->st_name == 0) || (*str == '\0')) &&
229e64d0ff9SAli Bahrami 	    (ELF_ST_TYPE(sym->st_info) == STT_SECTION);
230e64d0ff9SAli Bahrami 	if (gnu_stt_section)
231e64d0ff9SAli Bahrami 		str = gisc->is_name;
232e64d0ff9SAli Bahrami 
233e64d0ff9SAli Bahrami 
234e64d0ff9SAli Bahrami 	/*
2350e233487SRod Evans 	 * Generate a group descriptor.
2360e233487SRod Evans 	 */
2370e233487SRod Evans 	gd.gd_isc = gisc;
2380e233487SRod Evans 	gd.gd_oisc = NULL;
2390e233487SRod Evans 	gd.gd_name = str;
2400e233487SRod Evans 	gd.gd_data = gisc->is_indata->d_buf;
2410e233487SRod Evans 	gd.gd_cnt = gisc->is_indata->d_size / sizeof (Word);
2420e233487SRod Evans 
2430e233487SRod Evans 	/*
244*ef16f6b5SRichard Lowe 	 * If the signature symbol is a name generated by the GNU compiler to
245*ef16f6b5SRichard Lowe 	 * refer to a header, we need sloppy relocation.
246*ef16f6b5SRichard Lowe 	 */
247*ef16f6b5SRichard Lowe 	if (is_header_gensym(str)) {
248*ef16f6b5SRichard Lowe 		if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
249*ef16f6b5SRichard Lowe 			ofl->ofl_flags1 |= FLG_OF1_RLXREL;
250*ef16f6b5SRichard Lowe 		DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, gisc, TRUE,
251*ef16f6b5SRichard Lowe 		    (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0));
252*ef16f6b5SRichard Lowe 	}
253*ef16f6b5SRichard Lowe 
254*ef16f6b5SRichard Lowe 	/*
2550e233487SRod Evans 	 * Validate the section indices within the group.  If this is a COMDAT
2560e233487SRod Evans 	 * group, mark each section as COMDAT.
2570e233487SRod Evans 	 */
2580e233487SRod Evans 	for (ndx = 1; ndx < gd.gd_cnt; ndx++) {
2590e233487SRod Evans 		Word	gndx;
2600e233487SRod Evans 
2610e233487SRod Evans 		if ((gndx = gd.gd_data[ndx]) >= gifl->ifl_shnum) {
2621007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL,
2630e233487SRod Evans 			    MSG_INTL(MSG_GRP_INVALNDX), gifl->ifl_name,
2644a8d0ea7SAli Bahrami 			    EC_WORD(gisc->is_scnndx), gisc->is_name, ndx, gndx);
2650e233487SRod Evans 			return (0);
2660e233487SRod Evans 		}
2670e233487SRod Evans 
2680e233487SRod Evans 		if (gd.gd_data[0] & GRP_COMDAT)
2690e233487SRod Evans 			gifl->ifl_isdesc[gndx]->is_flags |= FLG_IS_COMDAT;
2700e233487SRod Evans 	}
2710e233487SRod Evans 
2720e233487SRod Evans 	/*
2730e233487SRod Evans 	 * If this is a COMDAT group, determine whether this group has already
2740e233487SRod Evans 	 * been encountered, or whether this is the first instance of the group.
2750e233487SRod Evans 	 */
2760e233487SRod Evans 	if ((gd.gd_data[0] & GRP_COMDAT) &&
2770e233487SRod Evans 	    (gpavl_loaded(ofl, &gd) == S_ERROR))
2780e233487SRod Evans 		return (S_ERROR);
2790e233487SRod Evans 
2800e233487SRod Evans 	/*
2810e233487SRod Evans 	 * Associate the group descriptor with this input file.
2820e233487SRod Evans 	 */
2830e233487SRod Evans 	if (alist_append(&(gifl->ifl_groups), &gd, sizeof (Group_desc),
2840e233487SRod Evans 	    AL_CNT_IFL_GROUPS) == NULL)
2850e233487SRod Evans 		return (S_ERROR);
2860e233487SRod Evans 
2870e233487SRod Evans 	return (1);
2880e233487SRod Evans }
289