xref: /onnv-gate/usr/src/common/elfcap/elfcap.c (revision 5565:538e7adac11a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52801Shyw  * Common Development and Distribution License (the "License").
62801Shyw  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213446Smrj 
220Sstevel@tonic-gate /*
233446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
263446Smrj 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /* LINTLIBRARY */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * String conversion routine for hardware capabilities types.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate #include	<strings.h>
350Sstevel@tonic-gate #include	<stdio.h>
360Sstevel@tonic-gate #include	<ctype.h>
370Sstevel@tonic-gate #include	<sys/machelf.h>
380Sstevel@tonic-gate #include	<sys/elf.h>
390Sstevel@tonic-gate #include	<sys/auxv_SPARC.h>
400Sstevel@tonic-gate #include	<sys/auxv_386.h>
410Sstevel@tonic-gate #include	<elfcap.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
44*5565Sab196087  * Given a literal string, generate an initialization for an
45*5565Sab196087  * elfcap_str_t value.
460Sstevel@tonic-gate  */
47*5565Sab196087 #define	STRDESC(_str) { _str, sizeof (_str) - 1 }
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
50*5565Sab196087  * The items in the elfcap_desc_t arrays are required to be
51*5565Sab196087  * ordered so that the array index is related to the
52*5565Sab196087  * c_val field as:
53*5565Sab196087  *
54*5565Sab196087  *	array[ndx].c_val = 2^ndx
55*5565Sab196087  *
56*5565Sab196087  * meaning that
570Sstevel@tonic-gate  *
58*5565Sab196087  *	array[0].c_val = 2^0 = 1
59*5565Sab196087  *	array[1].c_val = 2^1 = 2
60*5565Sab196087  *	array[2].c_val = 2^2 = 4
61*5565Sab196087  *	.
62*5565Sab196087  *	.
63*5565Sab196087  *	.
64*5565Sab196087  *
65*5565Sab196087  * Since 0 is not a valid value for the c_val field, we use it to
66*5565Sab196087  * mark an array entry that is a placeholder. This can happen if there
67*5565Sab196087  * is a hole in the assigned bits.
68*5565Sab196087  *
69*5565Sab196087  * The RESERVED_ELFCAP_DESC macro is used to reserve such holes.
700Sstevel@tonic-gate  */
71*5565Sab196087 #define	RESERVED_ELFCAP_DESC { 0, { NULL, 0 }, { NULL, 0 }, { NULL, 0 } }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
74*5565Sab196087  * Define separators for output string processing. This must be kept in
75*5565Sab196087  * sync with the elfcap_fmt_t values in elfcap.h.
760Sstevel@tonic-gate  */
77*5565Sab196087 static const elfcap_str_t format[] = {
78*5565Sab196087 	STRDESC(" "),			/* ELFCAP_FMT_SNGSPACE */
79*5565Sab196087 	STRDESC("  "),			/* ELFCAP_FMT_DBLSPACE */
80*5565Sab196087 	STRDESC(" | ")			/* ELFCAP_FMT_PIPSPACE */
81*5565Sab196087 };
82*5565Sab196087 #define	FORMAT_NELTS	(sizeof (format) / sizeof (format[0]))
83*5565Sab196087 
84*5565Sab196087 
852801Shyw 
86*5565Sab196087 /*
87*5565Sab196087  * Define all known software capabilities in all the supported styles.
88*5565Sab196087  * Order the capabilities by their numeric value. See SF1_SUNW_
89*5565Sab196087  * values in sys/elf.h.
90*5565Sab196087  */
91*5565Sab196087 static const elfcap_desc_t sf1[ELFCAP_NUM_SF1] = {
92*5565Sab196087 	{						/* 0x00000001 */
93*5565Sab196087 		SF1_SUNW_FPKNWN, STRDESC("SF1_SUNW_FPKNWN"),
94*5565Sab196087 		STRDESC("FPKNWN"), STRDESC("fpknwn")
95*5565Sab196087 	},
96*5565Sab196087 	{						/* 0x00000002 */
97*5565Sab196087 		SF1_SUNW_FPUSED, STRDESC("SF1_SUNW_FPUSED"),
98*5565Sab196087 		STRDESC("FPUSED"), STRDESC("fpused"),
99*5565Sab196087 	}
100*5565Sab196087 };
101*5565Sab196087 
102*5565Sab196087 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Order the SPARC hardware capabilities to match their numeric value.  See
1060Sstevel@tonic-gate  * AV_SPARC_ values in sys/auxv_SPARC.h.
1070Sstevel@tonic-gate  */
108*5565Sab196087 static const elfcap_desc_t hw1_sparc[ELFCAP_NUM_HW1_SPARC] = {
109*5565Sab196087 	{						/* 0x00000001 */
110*5565Sab196087 		AV_SPARC_MUL32, STRDESC("AV_SPARC_MUL32"),
111*5565Sab196087 		STRDESC("MUL32"), STRDESC("mul32"),
112*5565Sab196087 	},
113*5565Sab196087 	{						/* 0x00000002 */
114*5565Sab196087 		AV_SPARC_DIV32, STRDESC("AV_SPARC_DIV32"),
115*5565Sab196087 		STRDESC("DIV32"), STRDESC("div32"),
116*5565Sab196087 	},
117*5565Sab196087 	{						/* 0x00000004 */
118*5565Sab196087 		AV_SPARC_FSMULD, STRDESC("AV_SPARC_FSMULD"),
119*5565Sab196087 		STRDESC("FSMULD"), STRDESC("fsmuld"),
120*5565Sab196087 	},
121*5565Sab196087 	{						/* 0x00000008 */
122*5565Sab196087 		AV_SPARC_V8PLUS, STRDESC("AV_SPARC_V8PLUS"),
123*5565Sab196087 		STRDESC("V8PLUS"), STRDESC("v8plus"),
124*5565Sab196087 	},
125*5565Sab196087 	{						/* 0x00000010 */
126*5565Sab196087 		AV_SPARC_POPC, STRDESC("AV_SPARC_POPC"),
127*5565Sab196087 		STRDESC("POPC"), STRDESC("popc"),
128*5565Sab196087 	},
129*5565Sab196087 	{						/* 0x00000020 */
130*5565Sab196087 		AV_SPARC_VIS, STRDESC("AV_SPARC_VIS"),
131*5565Sab196087 		STRDESC("VIS"), STRDESC("vis"),
132*5565Sab196087 	},
133*5565Sab196087 	{						/* 0x00000040 */
134*5565Sab196087 		AV_SPARC_VIS2, STRDESC("AV_SPARC_VIS2"),
135*5565Sab196087 		STRDESC("VIS2"), STRDESC("vis2"),
136*5565Sab196087 	},
137*5565Sab196087 	{						/* 0x00000080 */
138*5565Sab196087 		AV_SPARC_ASI_BLK_INIT, STRDESC("AV_SPARC_ASI_BLK_INIT"),
139*5565Sab196087 		STRDESC("ASI_BLK_INIT"), STRDESC("asi_blk_init"),
140*5565Sab196087 	},
141*5565Sab196087 	{						/* 0x00000100 */
142*5565Sab196087 		AV_SPARC_FMAF, STRDESC("AV_SPARC_FMAF"),
143*5565Sab196087 		STRDESC("FMAF"), STRDESC("fmaf"),
144*5565Sab196087 	},
145*5565Sab196087 	RESERVED_ELFCAP_DESC,				/* 0x00000200 */
146*5565Sab196087 	RESERVED_ELFCAP_DESC,				/* 0x00000400 */
147*5565Sab196087 	RESERVED_ELFCAP_DESC,				/* 0x00000800 */
148*5565Sab196087 	RESERVED_ELFCAP_DESC,				/* 0x00001000 */
149*5565Sab196087 	RESERVED_ELFCAP_DESC,				/* 0x00002000 */
150*5565Sab196087 	{						/* 0x00004000 */
151*5565Sab196087 		AV_SPARC_FJFMAU, STRDESC("AV_SPARC_FJFMAU"),
152*5565Sab196087 		STRDESC("FJFMAU"), STRDESC("fjfmau"),
153*5565Sab196087 	},
154*5565Sab196087 	{						/* 0x00008000 */
155*5565Sab196087 		AV_SPARC_IMA, STRDESC("AV_SPARC_IMA"),
156*5565Sab196087 		STRDESC("IMA"), STRDESC("ima"),
157*5565Sab196087 	}
1580Sstevel@tonic-gate };
1590Sstevel@tonic-gate 
160*5565Sab196087 
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Order the Intel hardware capabilities to match their numeric value.  See
1640Sstevel@tonic-gate  * AV_386_ values in sys/auxv_386.h.
1650Sstevel@tonic-gate  */
166*5565Sab196087 static const elfcap_desc_t hw1_386[ELFCAP_NUM_HW1_386] = {
167*5565Sab196087 	{						/* 0x00000001 */
168*5565Sab196087 		AV_386_FPU, STRDESC("AV_386_FPU"),
169*5565Sab196087 		STRDESC("FPU"), STRDESC("fpu"),
170*5565Sab196087 	},
171*5565Sab196087 	{						/* 0x00000002 */
172*5565Sab196087 		AV_386_TSC, STRDESC("AV_386_TSC"),
173*5565Sab196087 		STRDESC("TSC"), STRDESC("tsc"),
174*5565Sab196087 	},
175*5565Sab196087 	{						/* 0x00000004 */
176*5565Sab196087 		AV_386_CX8, STRDESC("AV_386_CX8"),
177*5565Sab196087 		STRDESC("CX8"), STRDESC("cx8"),
178*5565Sab196087 	},
179*5565Sab196087 	{						/* 0x00000008 */
180*5565Sab196087 		AV_386_SEP, STRDESC("AV_386_SEP"),
181*5565Sab196087 		STRDESC("SEP"), STRDESC("sep"),
182*5565Sab196087 	},
183*5565Sab196087 	{						/* 0x00000010 */
184*5565Sab196087 		AV_386_AMD_SYSC, STRDESC("AV_386_AMD_SYSC"),
185*5565Sab196087 		STRDESC("AMD_SYSC"), STRDESC("amd_sysc"),
186*5565Sab196087 	},
187*5565Sab196087 	{						/* 0x00000020 */
188*5565Sab196087 		AV_386_CMOV, STRDESC("AV_386_CMOV"),
189*5565Sab196087 		STRDESC("CMOV"), STRDESC("cmov"),
190*5565Sab196087 	},
191*5565Sab196087 	{						/* 0x00000040 */
192*5565Sab196087 		AV_386_MMX, STRDESC("AV_386_MMX"),
193*5565Sab196087 		STRDESC("MMX"), STRDESC("mmx"),
194*5565Sab196087 	},
195*5565Sab196087 	{						/* 0x00000080 */
196*5565Sab196087 		AV_386_AMD_MMX, STRDESC("AV_386_AMD_MMX"),
197*5565Sab196087 		STRDESC("AMD_MMX"), STRDESC("amd_mmx"),
198*5565Sab196087 	},
199*5565Sab196087 	{						/* 0x00000100 */
200*5565Sab196087 		AV_386_AMD_3DNow, STRDESC("AV_386_AMD_3DNow"),
201*5565Sab196087 		STRDESC("AMD_3DNow"), STRDESC("amd_3dnow"),
202*5565Sab196087 	},
203*5565Sab196087 	{						/* 0x00000200 */
204*5565Sab196087 		AV_386_AMD_3DNowx, STRDESC("AV_386_AMD_3DNowx"),
205*5565Sab196087 		STRDESC("AMD_3DNowx"), STRDESC("amd_3dnowx"),
206*5565Sab196087 	},
207*5565Sab196087 	{						/* 0x00000400 */
208*5565Sab196087 		AV_386_FXSR, STRDESC("AV_386_FXSR"),
209*5565Sab196087 		STRDESC("FXSR"), STRDESC("fxsr"),
210*5565Sab196087 	},
211*5565Sab196087 	{						/* 0x00000800 */
212*5565Sab196087 		AV_386_SSE, STRDESC("AV_386_SSE"),
213*5565Sab196087 		STRDESC("SSE"), STRDESC("sse"),
214*5565Sab196087 	},
215*5565Sab196087 	{						/* 0x00001000 */
216*5565Sab196087 		AV_386_SSE2, STRDESC("AV_386_SSE2"),
217*5565Sab196087 		STRDESC("SSE2"), STRDESC("sse2"),
218*5565Sab196087 	},
219*5565Sab196087 	{						/* 0x00002000 */
220*5565Sab196087 		AV_386_PAUSE, STRDESC("AV_386_PAUSE"),
221*5565Sab196087 		STRDESC("PAUSE"), STRDESC("pause"),
222*5565Sab196087 	},
223*5565Sab196087 	{						/* 0x00004000 */
224*5565Sab196087 		AV_386_SSE3, STRDESC("AV_386_SSE3"),
225*5565Sab196087 		STRDESC("SSE3"), STRDESC("sse3"),
226*5565Sab196087 	},
227*5565Sab196087 	{						/* 0x00008000 */
228*5565Sab196087 		AV_386_MON, STRDESC("AV_386_MON"),
229*5565Sab196087 		STRDESC("MON"), STRDESC("mon"),
230*5565Sab196087 	},
231*5565Sab196087 	{						/* 0x00010000 */
232*5565Sab196087 		AV_386_CX16, STRDESC("AV_386_CX16"),
233*5565Sab196087 		STRDESC("CX16"), STRDESC("cx16"),
234*5565Sab196087 	},
235*5565Sab196087 	{						/* 0x00020000 */
236*5565Sab196087 		AV_386_AHF, STRDESC("AV_386_AHF"),
237*5565Sab196087 		STRDESC("AHF"), STRDESC("ahf"),
238*5565Sab196087 	},
239*5565Sab196087 	{						/* 0x00040000 */
240*5565Sab196087 		AV_386_TSCP, STRDESC("AV_386_TSCP"),
241*5565Sab196087 		STRDESC("TSCP"), STRDESC("tscp"),
242*5565Sab196087 	},
243*5565Sab196087 	{						/* 0x00080000 */
244*5565Sab196087 		AV_386_AMD_SSE4A, STRDESC("AV_386_AMD_SSE4A"),
245*5565Sab196087 		STRDESC("AMD_SSE4A"), STRDESC("amd_sse4a"),
246*5565Sab196087 	},
247*5565Sab196087 	{						/* 0x00100000 */
248*5565Sab196087 		AV_386_POPCNT, STRDESC("AV_386_POPCNT"),
249*5565Sab196087 		STRDESC("POPCNT"), STRDESC("popcnt"),
250*5565Sab196087 	},
251*5565Sab196087 	{						/* 0x00200000 */
252*5565Sab196087 		AV_386_AMD_LZCNT, STRDESC("AV_386_AMD_LZCNT"),
253*5565Sab196087 		STRDESC("AMD_LZCNT"), STRDESC("amd_lzcnt"),
254*5565Sab196087 	},
255*5565Sab196087 	{						/* 0x00400000 */
256*5565Sab196087 		AV_386_SSSE3, STRDESC("AV_386_SSSE3"),
257*5565Sab196087 		STRDESC("SSSE3"), STRDESC("ssse3"),
258*5565Sab196087 	},
259*5565Sab196087 	{						/* 0x00800000 */
260*5565Sab196087 		AV_386_SSE4_1, STRDESC("AV_386_SSE4_1"),
261*5565Sab196087 		STRDESC("SSE4.1"), STRDESC("sse4.1"),
262*5565Sab196087 	},
263*5565Sab196087 	{						/* 0x01000000 */
264*5565Sab196087 		AV_386_SSE4_2, STRDESC("AV_386_SSE4_2"),
265*5565Sab196087 		STRDESC("SSE4.2"), STRDESC("sse4.2"),
266*5565Sab196087 	}
2670Sstevel@tonic-gate };
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2703446Smrj  * Concatenate a token to the string buffer.  This can be a capabilities token
2710Sstevel@tonic-gate  * or a separator token.
2720Sstevel@tonic-gate  */
273*5565Sab196087 static elfcap_err_t
274*5565Sab196087 token(char **ostr, size_t *olen, const elfcap_str_t *nstr)
2750Sstevel@tonic-gate {
276*5565Sab196087 	if (*olen < nstr->s_len)
277*5565Sab196087 		return (ELFCAP_ERR_BUFOVFL);
278*5565Sab196087 
279*5565Sab196087 	(void) strcat(*ostr, nstr->s_str);
280*5565Sab196087 	*ostr += nstr->s_len;
281*5565Sab196087 	*olen -= nstr->s_len;
282*5565Sab196087 
283*5565Sab196087 	return (ELFCAP_ERR_NONE);
284*5565Sab196087 }
2850Sstevel@tonic-gate 
286*5565Sab196087 static elfcap_err_t
287*5565Sab196087 get_str_desc(elfcap_style_t style, const elfcap_desc_t *cdp,
288*5565Sab196087     const elfcap_str_t **ret_str)
289*5565Sab196087 {
290*5565Sab196087 	switch (style) {
291*5565Sab196087 	case ELFCAP_STYLE_FULL:
292*5565Sab196087 		*ret_str = &cdp->c_full;
293*5565Sab196087 		break;
294*5565Sab196087 	case ELFCAP_STYLE_UC:
295*5565Sab196087 		*ret_str = &cdp->c_uc;
296*5565Sab196087 		break;
297*5565Sab196087 	case ELFCAP_STYLE_LC:
298*5565Sab196087 		*ret_str = &cdp->c_lc;
299*5565Sab196087 		break;
300*5565Sab196087 	default:
301*5565Sab196087 		return (ELFCAP_ERR_INVSTYLE);
302*5565Sab196087 	}
3030Sstevel@tonic-gate 
304*5565Sab196087 	return (ELFCAP_ERR_NONE);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
307*5565Sab196087 
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate  * Expand a capabilities value into the strings defined in the associated
3100Sstevel@tonic-gate  * capabilities descriptor.
3110Sstevel@tonic-gate  */
312*5565Sab196087 static elfcap_err_t
313*5565Sab196087 expand(elfcap_style_t style, uint64_t val, const elfcap_desc_t *cdp,
314*5565Sab196087     uint_t cnum, char *str, size_t slen, elfcap_fmt_t fmt)
3150Sstevel@tonic-gate {
316*5565Sab196087 	uint_t			cnt;
317*5565Sab196087 	int			follow = 0, err;
318*5565Sab196087 	const elfcap_str_t	*nstr;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if (val == 0)
321*5565Sab196087 		return (ELFCAP_ERR_NONE);
3220Sstevel@tonic-gate 
323*5565Sab196087 	for (cnt = cnum; cnt > 0; cnt--) {
324*5565Sab196087 		uint_t mask = cdp[cnt - 1].c_val;
325*5565Sab196087 
326*5565Sab196087 		if ((val & mask) != 0) {
3270Sstevel@tonic-gate 			if (follow++ && ((err = token(&str, &slen,
328*5565Sab196087 			    &format[fmt])) != ELFCAP_ERR_NONE))
3290Sstevel@tonic-gate 				return (err);
3300Sstevel@tonic-gate 
331*5565Sab196087 			err = get_str_desc(style, &cdp[cnt - 1], &nstr);
332*5565Sab196087 			if (err != ELFCAP_ERR_NONE)
333*5565Sab196087 				return (err);
334*5565Sab196087 			if ((err = token(&str, &slen, nstr)) != ELFCAP_ERR_NONE)
3350Sstevel@tonic-gate 				return (err);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 			val = val & ~mask;
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * If there are any unknown bits remaining display the numeric value.
3430Sstevel@tonic-gate 	 */
3440Sstevel@tonic-gate 	if (val) {
345*5565Sab196087 		if (follow && ((err = token(&str, &slen, &format[fmt])) !=
346*5565Sab196087 		    ELFCAP_ERR_NONE))
3470Sstevel@tonic-gate 			return (err);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		(void) snprintf(str, slen, "0x%llx", val);
3500Sstevel@tonic-gate 	}
351*5565Sab196087 	return (ELFCAP_ERR_NONE);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * Expand a CA_SUNW_HW_1 value.
3560Sstevel@tonic-gate  */
357*5565Sab196087 elfcap_err_t
358*5565Sab196087 elfcap_hw1_to_str(elfcap_style_t style, uint64_t val, char *str,
359*5565Sab196087     size_t len, elfcap_fmt_t fmt, ushort_t mach)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * Initialize the string buffer, and validate the format request.
3630Sstevel@tonic-gate 	 */
3640Sstevel@tonic-gate 	*str = '\0';
365*5565Sab196087 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
366*5565Sab196087 		return (ELFCAP_ERR_INVFMT);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
369*5565Sab196087 		return (expand(style, val, &hw1_386[0], ELFCAP_NUM_HW1_386,
370*5565Sab196087 		    str, len, fmt));
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
3730Sstevel@tonic-gate 	    (mach == EM_SPARCV9))
374*5565Sab196087 		return (expand(style, val, hw1_sparc, ELFCAP_NUM_HW1_SPARC,
375*5565Sab196087 		    str, len, fmt));
3760Sstevel@tonic-gate 
377*5565Sab196087 	return (ELFCAP_ERR_UNKMACH);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
3820Sstevel@tonic-gate  * common across all platforms.  The use of "mach" is therefore redundant, but
383*5565Sab196087  * is retained for compatibility with the interface of elfcap_hw1_to_str(), and
3840Sstevel@tonic-gate  * possible future expansion.
3850Sstevel@tonic-gate  */
386*5565Sab196087 elfcap_err_t
3870Sstevel@tonic-gate /* ARGSUSED4 */
388*5565Sab196087 elfcap_sf1_to_str(elfcap_style_t style, uint64_t val, char *str,
389*5565Sab196087     size_t len, elfcap_fmt_t fmt, ushort_t mach)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate 	/*
3920Sstevel@tonic-gate 	 * Initialize the string buffer, and validate the format request.
3930Sstevel@tonic-gate 	 */
3940Sstevel@tonic-gate 	*str = '\0';
395*5565Sab196087 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
396*5565Sab196087 		return (ELFCAP_ERR_INVFMT);
3970Sstevel@tonic-gate 
398*5565Sab196087 	return (expand(style, val, &sf1[0], ELFCAP_NUM_SF1, str, len, fmt));
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*
402*5565Sab196087  * Given a capability tag type and value, map it to a string representation.
4030Sstevel@tonic-gate  */
404*5565Sab196087 elfcap_err_t
405*5565Sab196087 elfcap_tag_to_str(elfcap_style_t style, uint64_t tag, uint64_t val,
406*5565Sab196087     char *str, size_t len, elfcap_fmt_t fmt, ushort_t mach)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	if (tag == CA_SUNW_HW_1)
409*5565Sab196087 		return (elfcap_hw1_to_str(style, val, str, len, fmt, mach));
4100Sstevel@tonic-gate 	if (tag == CA_SUNW_SF_1)
411*5565Sab196087 		return (elfcap_sf1_to_str(style, val, str, len, fmt, mach));
4120Sstevel@tonic-gate 
413*5565Sab196087 	return (ELFCAP_ERR_UNKTAG);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * Determine a capabilities value from a capabilities string.
4180Sstevel@tonic-gate  */
4190Sstevel@tonic-gate static uint64_t
420*5565Sab196087 value(elfcap_style_t style, const char *str, const elfcap_desc_t *cdp,
421*5565Sab196087     uint_t cnum)
4220Sstevel@tonic-gate {
423*5565Sab196087 	const elfcap_str_t	*nstr;
4240Sstevel@tonic-gate 	uint_t	num;
425*5565Sab196087 	int	err;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	for (num = 0; num < cnum; num++) {
428*5565Sab196087 		/*
429*5565Sab196087 		 * Skip "reserved" bits. These are unassigned bits in the
430*5565Sab196087 		 * middle of the assigned range.
431*5565Sab196087 		 */
432*5565Sab196087 		if (cdp[num].c_val == 0)
433*5565Sab196087 			continue;
434*5565Sab196087 
435*5565Sab196087 		if ((err = get_str_desc(style, &cdp[num], &nstr)) != 0)
436*5565Sab196087 			return (err);
437*5565Sab196087 		if (strcmp(str, nstr->s_str) == 0)
4380Sstevel@tonic-gate 			return (cdp[num].c_val);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 	return (0);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate uint64_t
444*5565Sab196087 elfcap_sf1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
4450Sstevel@tonic-gate {
446*5565Sab196087 	return (value(style, str, &sf1[0], ELFCAP_NUM_SF1));
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate uint64_t
450*5565Sab196087 elfcap_hw1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
453*5565Sab196087 		return (value(style, str, &hw1_386[0], ELFCAP_NUM_HW1_386));
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
4560Sstevel@tonic-gate 	    (mach == EM_SPARCV9))
457*5565Sab196087 		return (value(style, str, hw1_sparc, ELFCAP_NUM_HW1_SPARC));
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	return (0);
4600Sstevel@tonic-gate }
461*5565Sab196087 
462*5565Sab196087 /*
463*5565Sab196087  * These functions allow the caller to get direct access to the
464*5565Sab196087  * cap descriptors.
465*5565Sab196087  */
466*5565Sab196087 const elfcap_desc_t *
467*5565Sab196087 elfcap_getdesc_hw1_sparc(void)
468*5565Sab196087 {
469*5565Sab196087 	return (hw1_sparc);
470*5565Sab196087 }
471*5565Sab196087 
472*5565Sab196087 const elfcap_desc_t *
473*5565Sab196087 elfcap_getdesc_hw1_386(void)
474*5565Sab196087 {
475*5565Sab196087 	return (hw1_386);
476*5565Sab196087 }
477*5565Sab196087 
478*5565Sab196087 const elfcap_desc_t *
479*5565Sab196087 elfcap_getdesc_sf1(void)
480*5565Sab196087 {
481*5565Sab196087 	return (sf1);
482*5565Sab196087 }
483