xref: /illumos-gate/usr/src/cmd/sgs/liblddbg/common/sections.c (revision 0bb073995ac5a95bd35f2dd790df1ea3d8c2d507)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #include	"msg.h"
27 #include	"_debug.h"
28 #include	"libld.h"
29 #include	"_string_table.h"
30 
31 void
32 Dbg_sec_strtab(Lm_list *lml, Os_desc *osp, Str_tbl *stp)
33 {
34 	uint_t	cnt;
35 
36 	if (DBG_NOTCLASS(DBG_C_STRTAB))
37 		return;
38 
39 	if (!osp)
40 		return;
41 
42 	Dbg_util_nl(lml, DBG_NL_STD);
43 	if (stp->st_flags & FLG_STTAB_COMPRESS)
44 		dbg_print(lml, MSG_INTL(MSG_SEC_STRTAB_COMP), osp->os_name,
45 		    EC_XWORD(stp->st_fullstrsize), EC_XWORD(stp->st_strsize));
46 	else
47 		dbg_print(lml, MSG_INTL(MSG_SEC_STRTAB_STND), osp->os_name,
48 		    EC_XWORD(stp->st_fullstrsize));
49 
50 	if ((DBG_NOTDETAIL()) ||
51 	    ((stp->st_flags & FLG_STTAB_COMPRESS) == 0))
52 		return;
53 
54 	dbg_print(lml, MSG_ORIG(MSG_STR_EMPTY));
55 	dbg_print(lml, MSG_INTL(MSG_SEC_STRTAB_HD), osp->os_name,
56 	    stp->st_hbckcnt);
57 
58 	for (cnt = 0; cnt < stp->st_hbckcnt; cnt++) {
59 		Str_hash	*strhash = stp->st_hashbcks[cnt];
60 
61 		if (strhash == 0)
62 			continue;
63 
64 		dbg_print(lml, MSG_INTL(MSG_SEC_STRTAB_BCKT), cnt);
65 
66 		while (strhash) {
67 			size_t	stroff = strhash->hi_mstr->sm_strlen -
68 			    strhash->hi_strlen;
69 
70 			if (stroff == 0) {
71 				dbg_print(lml, MSG_INTL(MSG_SEC_STRTAB_MSTR),
72 				    EC_XWORD(strhash->hi_refcnt),
73 				    strhash->hi_mstr->sm_str);
74 			} else {
75 				dbg_print(lml, MSG_INTL(MSG_SEC_STRTAB_SUFSTR),
76 				    EC_XWORD(strhash->hi_refcnt),
77 				    &strhash->hi_mstr->sm_str[stroff],
78 				    strhash->hi_mstr->sm_str);
79 			}
80 
81 			strhash = strhash->hi_next;
82 		}
83 	}
84 }
85 
86 void
87 Dbg_sec_genstr_compress(Lm_list *lml, const char *os_name,
88     Xword raw_size, Xword merge_size)
89 {
90 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
91 		return;
92 
93 	dbg_print(lml, MSG_INTL(MSG_SEC_GENSTR_COMP), os_name,
94 	    EC_XWORD(raw_size), EC_XWORD(merge_size));
95 }
96 
97 void
98 Dbg_sec_unsup_strmerge(Lm_list *lml, Is_desc *isp)
99 {
100 	const char *str;
101 
102 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
103 		return;
104 
105 	/*
106 	 * We can only merge string table sections with single byte
107 	 * (char) characters. For any other (wide) character types,
108 	 * issue a message so the user will understand why these
109 	 * sections are not being picked up.
110 	 */
111 	if ((isp->is_shdr->sh_entsize > 1) ||
112 	    (isp->is_shdr->sh_addralign > 1)) {
113 		str = (isp->is_file != NULL) ? isp->is_file->ifl_name :
114 		    MSG_INTL(MSG_STR_NULL);
115 		dbg_print(lml, MSG_INTL(MSG_SEC_STRMERGE_UNSUP),
116 		    isp->is_name, str, EC_XWORD(isp->is_shdr->sh_addralign),
117 		    EC_XWORD(isp->is_shdr->sh_entsize));
118 	}
119 }
120 
121 void
122 Dbg_sec_in(Lm_list *lml, Is_desc *isp)
123 {
124 	const char	*str;
125 
126 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
127 		return;
128 
129 	if (isp->is_flags & FLG_IS_GNSTRMRG) {
130 		/*
131 		 * This section was generated because we have 1 or
132 		 * more SHF_MERGE|SHF_STRINGS input sections that we
133 		 * wish to merge. This new section will ultimately
134 		 * end up replacing those sections once it has been filled
135 		 * with their strings (merged and compressed) and relocations
136 		 * have been redirected.
137 		 */
138 		dbg_print(lml, MSG_INTL(MSG_SEC_INPUT_GENSTR), isp->is_name);
139 	} else {
140 		/* Standard input section */
141 		str = (isp->is_file != NULL) ? isp->is_file->ifl_name :
142 		    MSG_INTL(MSG_STR_NULL);
143 		dbg_print(lml, MSG_INTL(MSG_SEC_INPUT), isp->is_name, str);
144 	}
145 }
146 
147 void
148 Dbg_sec_added(Lm_list *lml, Os_desc *osp, Sg_desc *sgp)
149 {
150 	const char	*str;
151 
152 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
153 		return;
154 
155 	if (sgp->sg_name && *sgp->sg_name)
156 		str = sgp->sg_name;
157 	else
158 		str = MSG_INTL(MSG_STR_NULL);
159 
160 	dbg_print(lml, MSG_INTL(MSG_SEC_ADDED), osp->os_name, str);
161 }
162 
163 void
164 Dbg_sec_created(Lm_list *lml, Os_desc *osp, Sg_desc *sgp)
165 {
166 	const char	*str;
167 
168 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
169 		return;
170 
171 	if (sgp->sg_name && *sgp->sg_name)
172 		str = sgp->sg_name;
173 	else
174 		str = MSG_INTL(MSG_STR_NULL);
175 
176 	dbg_print(lml, MSG_INTL(MSG_SEC_CREATED), osp->os_name, str);
177 }
178 
179 void
180 Dbg_sec_discarded(Lm_list *lml, Is_desc *isp, Is_desc *disp)
181 {
182 	if (DBG_NOTCLASS(DBG_C_SECTIONS | DBG_C_UNUSED))
183 		return;
184 
185 	if ((isp->is_flags & FLG_IS_INSTRMRG) &&
186 	    (disp->is_flags & FLG_IS_GNSTRMRG)) {
187 		/*
188 		 * This SHF_MERGE|SHF_STRINGS input section is being
189 		 * discarded in favor of the generated merged string section.
190 		 */
191 		dbg_print(lml, MSG_INTL(MSG_SEC_STRMERGE_DISCARDED),
192 		    isp->is_name, isp->is_file->ifl_name);
193 	} else {
194 		/* Generic section discard */
195 		dbg_print(lml, MSG_INTL(MSG_SEC_DISCARDED), isp->is_name,
196 		    isp->is_file->ifl_name, disp->is_name,
197 		    disp->is_file->ifl_name);
198 	}
199 }
200 
201 void
202 Dbg_sec_group(Lm_list *lml, Is_desc *isp, Group_desc *gdp)
203 {
204 	const char	*comdat;
205 
206 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
207 		return;
208 
209 	if (gdp->gd_data[0] & GRP_COMDAT)
210 		comdat = MSG_ORIG(MSG_STR_COMDAT);
211 	else
212 		comdat = MSG_ORIG(MSG_STR_EMPTY);
213 
214 	if (isp->is_shdr->sh_type == SHT_GROUP) {
215 		dbg_print(lml, MSG_INTL(MSG_SEC_GRP_DEFINE), isp->is_name,
216 		    isp->is_file->ifl_name, comdat, gdp->gd_name);
217 	} else {
218 		dbg_print(lml, MSG_INTL(MSG_SEC_GRP_MEMBER), isp->is_name,
219 		    isp->is_file->ifl_name, comdat, gdp->gd_name);
220 	}
221 
222 	if (gdp->gd_oisc) {
223 		dbg_print(lml, MSG_INTL(MSG_SEC_GRP_DISCARDED), isp->is_name,
224 		    isp->is_file->ifl_name, gdp->gd_name,
225 		    gdp->gd_oisc->is_file->ifl_name);
226 	}
227 }
228 
229 void
230 Dbg_sec_order_list(Ofl_desc *ofl, int flag)
231 {
232 	Os_desc		*osp;
233 	Is_desc		*isp1;
234 	Listnode	*lnp1, *lnp2;
235 	Lm_list		*lml = ofl->ofl_lml;
236 	const char	*str;
237 
238 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
239 		return;
240 	if (DBG_NOTDETAIL())
241 		return;
242 
243 	Dbg_util_nl(lml, DBG_NL_STD);
244 
245 	/*
246 	 * If the flag == 0, then the routine is called before sorting.
247 	 */
248 	if (flag == 0)
249 		str = MSG_INTL(MSG_ORD_SORT_BEFORE);
250 	else
251 		str = MSG_INTL(MSG_ORD_SORT_AFTER);
252 
253 	for (LIST_TRAVERSE(&ofl->ofl_ordered, lnp1, osp)) {
254 		Sort_desc	*sort = osp->os_sort;
255 
256 		dbg_print(lml, str, osp->os_name);
257 		dbg_print(lml, MSG_INTL(MSG_ORD_HDR_1),
258 		    EC_WORD(sort->st_beforecnt), EC_WORD(sort->st_aftercnt),
259 		    EC_WORD(sort->st_ordercnt));
260 
261 		for (LIST_TRAVERSE(&osp->os_isdescs, lnp2, isp1)) {
262 			Word		link;
263 			Ifl_desc	*ifl = isp1->is_file;
264 			Is_desc		*isp2;
265 			const char	*msg;
266 
267 			if ((isp1->is_flags & FLG_IS_ORDERED) == 0) {
268 				dbg_print(lml, MSG_INTL(MSG_ORD_TITLE_0),
269 				    isp1->is_name, isp1->is_file->ifl_name);
270 				continue;
271 			}
272 
273 			if (isp1->is_shdr->sh_flags & SHF_ORDERED) {
274 				link = isp1->is_shdr->sh_info;
275 				msg = MSG_ORIG(MSG_SH_INFO);
276 			} else {
277 				/* SHF_LINK_ORDER */
278 				link = isp1->is_shdr->sh_link;
279 				msg = MSG_ORIG(MSG_SH_LINK);
280 			}
281 
282 			if (link == SHN_BEFORE) {
283 				dbg_print(lml, MSG_INTL(MSG_ORD_TITLE_1),
284 				    isp1->is_name, isp1->is_file->ifl_name,
285 				    msg);
286 				continue;
287 			}
288 
289 			if (link == SHN_AFTER) {
290 				dbg_print(lml, MSG_INTL(MSG_ORD_TITLE_2),
291 				    isp1->is_name, isp1->is_file->ifl_name,
292 				    msg);
293 				continue;
294 			}
295 
296 			isp2 = ifl->ifl_isdesc[link];
297 			dbg_print(lml, MSG_INTL(MSG_ORD_TITLE_3),
298 			    isp1->is_name, ifl->ifl_name, msg, isp2->is_name,
299 			    EC_WORD(isp2->is_keyident));
300 		}
301 	}
302 	Dbg_util_nl(lml, DBG_NL_STD);
303 }
304 
305 /*
306  * Error message string table.
307  */
308 static const Msg order_errors[] = {
309 	MSG_ORD_ERR_INFORANGE,		/* MSG_INTL(MSG_ORD_ERR_INFORANGE) */
310 	MSG_ORD_ERR_ORDER,		/* MSG_INTL(MSG_ORD_ERR_ORDER) */
311 	MSG_ORD_ERR_LINKRANGE,		/* MSG_INTL(MSG_ORD_ERR_LINKRANGE) */
312 	MSG_ORD_ERR_FLAGS,		/* MSG_INTL(MSG_ORD_ERR_FLAGS) */
313 	MSG_ORD_ERR_CYCLIC,		/* MSG_INTL(MSG_ORD_ERR_CYCLIC) */
314 	MSG_ORD_ERR_LINKINV		/* MSG_INTL(MSG_ORD_ERR_LINKINV) */
315 };
316 
317 void
318 Dbg_sec_order_error(Lm_list *lml, Ifl_desc *ifl, Word ndx, int error)
319 {
320 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
321 		return;
322 	if (DBG_NOTDETAIL())
323 		return;
324 
325 	if (error == 0)
326 		return;
327 
328 	dbg_print(lml, MSG_INTL(MSG_ORD_ERR_TITLE),
329 	    ifl->ifl_isdesc[ndx]->is_name, ifl->ifl_name);
330 
331 	if (error)
332 		dbg_print(lml, MSG_INTL(order_errors[error - 1]));
333 }
334 
335 void
336 Dbg_sec_redirected(Lm_list *lml, const char *oname, const char *nname)
337 {
338 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
339 		return;
340 
341 	dbg_print(lml, MSG_INTL(MSG_SEC_REDIRECTED), oname, nname);
342 }
343 
344 void
345 Dbg_sec_gnu_comdat(Lm_list *lml, const char *name, uint_t comdat, uint_t relax)
346 {
347 	const char	*fmt;
348 
349 	if (DBG_NOTCLASS(DBG_C_SECTIONS))
350 		return;
351 
352 	if (comdat && relax)
353 		fmt = MSG_INTL(MSG_SEC_GNU_COMDAT_1);
354 	else if (comdat)
355 		fmt = MSG_INTL(MSG_SEC_GNU_COMDAT_2);
356 	else
357 		fmt = MSG_INTL(MSG_SEC_GNU_COMDAT_3);
358 
359 	dbg_print(lml, fmt, name);
360 }
361