xref: /titanic_41/usr/src/cmd/sgs/libld/common/groups.c (revision 72f0806acd90f56fb47a8087c33cfeaec527fdda)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<stdio.h>
28 #include	<string.h>
29 #include	<link.h>
30 #include	<debug.h>
31 #include	"msg.h"
32 #include	"_libld.h"
33 
34 /*
35  * Determine whether a (COMDAT) group has already been encountered.  If so,
36  * indicate that the group descriptor has an overriding group (gd_oisc).  This
37  * indication triggers the ld_place_section() to discard this group, while the
38  * gd_oisc information provides for complete diagnostics of the override.
39  * Otherwise, this is the first occurrence of this group, therefore the group
40  * descriptor is saved for future comparisons.
41  */
42 static uintptr_t
43 gpavl_loaded(Ofl_desc *ofl, Group_desc *gdp)
44 {
45 	Isd_node	isd, *isdp;
46 	avl_tree_t	*avlt;
47 	avl_index_t	where;
48 
49 	/*
50 	 * Create a groups avl tree if required.
51 	 */
52 	if ((avlt = ofl->ofl_groups) == NULL) {
53 		if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
54 			return (S_ERROR);
55 		avl_create(avlt, isdavl_compare, sizeof (Isd_node),
56 		    SGSOFFSETOF(Isd_node, isd_avl));
57 		ofl->ofl_groups = avlt;
58 	}
59 
60 	isd.isd_hash = sgs_str_hash(gdp->gd_isc->is_name);
61 	isd.isd_isp = gdp->gd_isc;
62 
63 	if ((isdp = avl_find(avlt, &isd, &where)) != NULL) {
64 		gdp->gd_oisc = isdp->isd_isp;
65 		return (1);
66 	}
67 
68 	/*
69 	 * This is a new group - so keep it.
70 	 */
71 	if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL)
72 		return (S_ERROR);
73 
74 	isdp->isd_hash = isd.isd_hash;
75 	isdp->isd_isp = isd.isd_isp;
76 
77 	avl_insert(avlt, isdp, where);
78 	return (0);
79 }
80 
81 Group_desc *
82 ld_get_group(Ofl_desc *ofl, Is_desc *isp)
83 {
84 	Ifl_desc	*ifl = isp->is_file;
85 	uint_t		scnndx = isp->is_scnndx;
86 	Group_desc	*gdp;
87 	Aliste		idx;
88 
89 	/*
90 	 * Scan the GROUP sections associated with this file to find the
91 	 * matching group section.
92 	 */
93 	for (ALIST_TRAVERSE(ifl->ifl_groups, idx, gdp)) {
94 		size_t	ndx;
95 		Word	*data;
96 
97 		if (isp->is_shdr->sh_type == SHT_GROUP) {
98 			if (isp->is_scnndx == gdp->gd_isc->is_scnndx)
99 				return (gdp);
100 			continue;
101 		}
102 
103 		data = gdp->gd_data;
104 		for (ndx = 1; ndx < gdp->gd_cnt; ndx++) {
105 			if (data[ndx] == scnndx)
106 				return (gdp);
107 		}
108 	}
109 
110 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ELF_NOGROUPSECT),
111 	    ifl->ifl_name, EC_WORD(isp->is_scnndx), isp->is_name);
112 	ofl->ofl_flags |= FLG_OF_FATAL;
113 	return (NULL);
114 }
115 
116 uintptr_t
117 ld_group_process(Is_desc *gisc, Ofl_desc *ofl)
118 {
119 	Ifl_desc	*gifl = gisc->is_file;
120 	Shdr		*sshdr, *gshdr = gisc->is_shdr;
121 	Is_desc		*isc;
122 	Sym		*sym;
123 	const char	*str;
124 	Group_desc	gd;
125 	size_t		ndx;
126 
127 	/*
128 	 * Confirm that the sh_link points to a valid section.
129 	 */
130 	if ((gshdr->sh_link == SHN_UNDEF) ||
131 	    (gshdr->sh_link >= gifl->ifl_shnum) ||
132 	    ((isc = gifl->ifl_isdesc[gshdr->sh_link]) == NULL)) {
133 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHLINK),
134 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx),
135 		    gisc->is_name, EC_XWORD(gshdr->sh_link));
136 		ofl->ofl_flags |= FLG_OF_FATAL;
137 		return (0);
138 	}
139 	if (gshdr->sh_entsize == 0) {
140 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHENTSIZE),
141 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name,
142 		    EC_XWORD(gshdr->sh_entsize));
143 		ofl->ofl_flags |= FLG_OF_FATAL;
144 		return (0);
145 	}
146 
147 	/*
148 	 * Get the associated symbol table.  Sanity check the sh_info field
149 	 * (which points to the signature symbol table entry) against the size
150 	 * of the symbol table.
151 	 */
152 	sshdr = isc->is_shdr;
153 	sym = (Sym *)isc->is_indata->d_buf;
154 
155 	if ((sshdr->sh_info == SHN_UNDEF) ||
156 	    (gshdr->sh_info >= (Word)(sshdr->sh_size / sshdr->sh_entsize)) ||
157 	    ((isc = gifl->ifl_isdesc[sshdr->sh_link]) == NULL)) {
158 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
159 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name,
160 		    EC_XWORD(gshdr->sh_info));
161 		ofl->ofl_flags |= FLG_OF_FATAL;
162 		return (0);
163 	}
164 
165 	sym += gshdr->sh_info;
166 
167 	/*
168 	 * Get the symbol name from the associated string table.
169 	 */
170 	str = (char *)isc->is_indata->d_buf;
171 	str += sym->st_name;
172 
173 	/*
174 	 * Generate a group descriptor.
175 	 */
176 	gd.gd_isc = gisc;
177 	gd.gd_oisc = NULL;
178 	gd.gd_name = str;
179 	gd.gd_data = gisc->is_indata->d_buf;
180 	gd.gd_cnt = gisc->is_indata->d_size / sizeof (Word);
181 
182 	/*
183 	 * If this group is a COMDAT group, validate the signature symbol.
184 	 */
185 	if ((gd.gd_data[0] & GRP_COMDAT) &&
186 	    ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) ||
187 	    (sym->st_shndx == SHN_UNDEF))) {
188 		/* If section symbol, construct a printable name for it */
189 		if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) {
190 			const char *stt_sym_name =
191 			    ld_stt_section_sym_name(gisc);
192 
193 			if (stt_sym_name != NULL)
194 				str = stt_sym_name;
195 		}
196 
197 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_GRP_INVALSYM),
198 		    gifl->ifl_name, EC_WORD(gisc->is_scnndx),
199 		    gisc->is_name, str);
200 		ofl->ofl_flags |= FLG_OF_FATAL;
201 		return (0);
202 	}
203 
204 	/*
205 	 * Validate the section indices within the group.  If this is a COMDAT
206 	 * group, mark each section as COMDAT.
207 	 */
208 	for (ndx = 1; ndx < gd.gd_cnt; ndx++) {
209 		Word	gndx;
210 
211 		if ((gndx = gd.gd_data[ndx]) >= gifl->ifl_shnum) {
212 			eprintf(ofl->ofl_lml, ERR_FATAL,
213 			    MSG_INTL(MSG_GRP_INVALNDX), gifl->ifl_name,
214 			    EC_WORD(gisc->is_scnndx), gisc->is_name, ndx, gndx);
215 			ofl->ofl_flags |= FLG_OF_FATAL;
216 			return (0);
217 		}
218 
219 		if (gd.gd_data[0] & GRP_COMDAT)
220 			gifl->ifl_isdesc[gndx]->is_flags |= FLG_IS_COMDAT;
221 	}
222 
223 	/*
224 	 * If this is a COMDAT group, determine whether this group has already
225 	 * been encountered, or whether this is the first instance of the group.
226 	 */
227 	if ((gd.gd_data[0] & GRP_COMDAT) &&
228 	    (gpavl_loaded(ofl, &gd) == S_ERROR))
229 		return (S_ERROR);
230 
231 	/*
232 	 * Associate the group descriptor with this input file.
233 	 */
234 	if (alist_append(&(gifl->ifl_groups), &gd, sizeof (Group_desc),
235 	    AL_CNT_IFL_GROUPS) == NULL)
236 		return (S_ERROR);
237 
238 	return (1);
239 }
240