xref: /onnv-gate/usr/src/common/elfcap/elfcap.c (revision 7833:60e027e61b69)
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 /*
237718SJason.Beloro@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
263446Smrj 
270Sstevel@tonic-gate /* LINTLIBRARY */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * String conversion routine for hardware capabilities types.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate #include	<strings.h>
330Sstevel@tonic-gate #include	<stdio.h>
340Sstevel@tonic-gate #include	<ctype.h>
350Sstevel@tonic-gate #include	<sys/machelf.h>
360Sstevel@tonic-gate #include	<sys/elf.h>
370Sstevel@tonic-gate #include	<sys/auxv_SPARC.h>
380Sstevel@tonic-gate #include	<sys/auxv_386.h>
390Sstevel@tonic-gate #include	<elfcap.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
425565Sab196087  * Given a literal string, generate an initialization for an
435565Sab196087  * elfcap_str_t value.
440Sstevel@tonic-gate  */
455565Sab196087 #define	STRDESC(_str) { _str, sizeof (_str) - 1 }
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
485565Sab196087  * The items in the elfcap_desc_t arrays are required to be
495565Sab196087  * ordered so that the array index is related to the
505565Sab196087  * c_val field as:
515565Sab196087  *
525565Sab196087  *	array[ndx].c_val = 2^ndx
535565Sab196087  *
545565Sab196087  * meaning that
550Sstevel@tonic-gate  *
565565Sab196087  *	array[0].c_val = 2^0 = 1
575565Sab196087  *	array[1].c_val = 2^1 = 2
585565Sab196087  *	array[2].c_val = 2^2 = 4
595565Sab196087  *	.
605565Sab196087  *	.
615565Sab196087  *	.
625565Sab196087  *
635565Sab196087  * Since 0 is not a valid value for the c_val field, we use it to
645565Sab196087  * mark an array entry that is a placeholder. This can happen if there
655565Sab196087  * is a hole in the assigned bits.
665565Sab196087  *
675565Sab196087  * The RESERVED_ELFCAP_DESC macro is used to reserve such holes.
680Sstevel@tonic-gate  */
695565Sab196087 #define	RESERVED_ELFCAP_DESC { 0, { NULL, 0 }, { NULL, 0 }, { NULL, 0 } }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
725565Sab196087  * Define separators for output string processing. This must be kept in
735565Sab196087  * sync with the elfcap_fmt_t values in elfcap.h.
740Sstevel@tonic-gate  */
755565Sab196087 static const elfcap_str_t format[] = {
765565Sab196087 	STRDESC(" "),			/* ELFCAP_FMT_SNGSPACE */
775565Sab196087 	STRDESC("  "),			/* ELFCAP_FMT_DBLSPACE */
785565Sab196087 	STRDESC(" | ")			/* ELFCAP_FMT_PIPSPACE */
795565Sab196087 };
805565Sab196087 #define	FORMAT_NELTS	(sizeof (format) / sizeof (format[0]))
815565Sab196087 
825565Sab196087 
832801Shyw 
845565Sab196087 /*
855565Sab196087  * Define all known software capabilities in all the supported styles.
865565Sab196087  * Order the capabilities by their numeric value. See SF1_SUNW_
875565Sab196087  * values in sys/elf.h.
885565Sab196087  */
895565Sab196087 static const elfcap_desc_t sf1[ELFCAP_NUM_SF1] = {
905565Sab196087 	{						/* 0x00000001 */
915565Sab196087 		SF1_SUNW_FPKNWN, STRDESC("SF1_SUNW_FPKNWN"),
925565Sab196087 		STRDESC("FPKNWN"), STRDESC("fpknwn")
935565Sab196087 	},
945565Sab196087 	{						/* 0x00000002 */
955565Sab196087 		SF1_SUNW_FPUSED, STRDESC("SF1_SUNW_FPUSED"),
965565Sab196087 		STRDESC("FPUSED"), STRDESC("fpused"),
97*7833SRod.Evans@Sun.COM 	},
98*7833SRod.Evans@Sun.COM 	{						/* 0x00000004 */
99*7833SRod.Evans@Sun.COM 		SF1_SUNW_ADDR32, STRDESC("SF1_SUNW_ADDR32"),
100*7833SRod.Evans@Sun.COM 		STRDESC("ADDR32"), STRDESC("addr32"),
1015565Sab196087 	}
1025565Sab196087 };
1035565Sab196087 
1045565Sab196087 
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Order the SPARC hardware capabilities to match their numeric value.  See
1080Sstevel@tonic-gate  * AV_SPARC_ values in sys/auxv_SPARC.h.
1090Sstevel@tonic-gate  */
1105565Sab196087 static const elfcap_desc_t hw1_sparc[ELFCAP_NUM_HW1_SPARC] = {
1115565Sab196087 	{						/* 0x00000001 */
1125565Sab196087 		AV_SPARC_MUL32, STRDESC("AV_SPARC_MUL32"),
1135565Sab196087 		STRDESC("MUL32"), STRDESC("mul32"),
1145565Sab196087 	},
1155565Sab196087 	{						/* 0x00000002 */
1165565Sab196087 		AV_SPARC_DIV32, STRDESC("AV_SPARC_DIV32"),
1175565Sab196087 		STRDESC("DIV32"), STRDESC("div32"),
1185565Sab196087 	},
1195565Sab196087 	{						/* 0x00000004 */
1205565Sab196087 		AV_SPARC_FSMULD, STRDESC("AV_SPARC_FSMULD"),
1215565Sab196087 		STRDESC("FSMULD"), STRDESC("fsmuld"),
1225565Sab196087 	},
1235565Sab196087 	{						/* 0x00000008 */
1245565Sab196087 		AV_SPARC_V8PLUS, STRDESC("AV_SPARC_V8PLUS"),
1255565Sab196087 		STRDESC("V8PLUS"), STRDESC("v8plus"),
1265565Sab196087 	},
1275565Sab196087 	{						/* 0x00000010 */
1285565Sab196087 		AV_SPARC_POPC, STRDESC("AV_SPARC_POPC"),
1295565Sab196087 		STRDESC("POPC"), STRDESC("popc"),
1305565Sab196087 	},
1315565Sab196087 	{						/* 0x00000020 */
1325565Sab196087 		AV_SPARC_VIS, STRDESC("AV_SPARC_VIS"),
1335565Sab196087 		STRDESC("VIS"), STRDESC("vis"),
1345565Sab196087 	},
1355565Sab196087 	{						/* 0x00000040 */
1365565Sab196087 		AV_SPARC_VIS2, STRDESC("AV_SPARC_VIS2"),
1375565Sab196087 		STRDESC("VIS2"), STRDESC("vis2"),
1385565Sab196087 	},
1395565Sab196087 	{						/* 0x00000080 */
1405565Sab196087 		AV_SPARC_ASI_BLK_INIT, STRDESC("AV_SPARC_ASI_BLK_INIT"),
1415565Sab196087 		STRDESC("ASI_BLK_INIT"), STRDESC("asi_blk_init"),
1425565Sab196087 	},
1435565Sab196087 	{						/* 0x00000100 */
1445565Sab196087 		AV_SPARC_FMAF, STRDESC("AV_SPARC_FMAF"),
1455565Sab196087 		STRDESC("FMAF"), STRDESC("fmaf"),
1465565Sab196087 	},
1477718SJason.Beloro@Sun.COM 	{						/* 0x00000200 */
1487718SJason.Beloro@Sun.COM 		AV_SPARC_FMAU, STRDESC("AV_SPARC_FMAU"),
1497718SJason.Beloro@Sun.COM 		STRDESC("FMAU"), STRDESC("fmau"),
1507718SJason.Beloro@Sun.COM 	},
1517718SJason.Beloro@Sun.COM 	{						/* 0x00000400 */
1527718SJason.Beloro@Sun.COM 		AV_SPARC_VIS3, STRDESC("AV_SPARC_VIS3"),
1537718SJason.Beloro@Sun.COM 		STRDESC("VIS3"), STRDESC("vis3"),
1547718SJason.Beloro@Sun.COM 	},
1557718SJason.Beloro@Sun.COM 	{						/* 0x00000800 */
1567718SJason.Beloro@Sun.COM 		AV_SPARC_HPC, STRDESC("AV_SPARC_HPC"),
1577718SJason.Beloro@Sun.COM 		STRDESC("HPC"), STRDESC("hpc"),
1587718SJason.Beloro@Sun.COM 	},
1597718SJason.Beloro@Sun.COM 	{						/* 0x00001000 */
1607718SJason.Beloro@Sun.COM 		AV_SPARC_RANDOM, STRDESC("AV_SPARC_RANDOM"),
1617718SJason.Beloro@Sun.COM 		STRDESC("RANDOM"), STRDESC("random"),
1627718SJason.Beloro@Sun.COM 	},
1637718SJason.Beloro@Sun.COM 	{						/* 0x00002000 */
1647718SJason.Beloro@Sun.COM 		AV_SPARC_TRANS, STRDESC("AV_SPARC_TRANS"),
1657718SJason.Beloro@Sun.COM 		STRDESC("TRANS"), STRDESC("trans"),
1667718SJason.Beloro@Sun.COM 	},
1675565Sab196087 	{						/* 0x00004000 */
1685565Sab196087 		AV_SPARC_FJFMAU, STRDESC("AV_SPARC_FJFMAU"),
1695565Sab196087 		STRDESC("FJFMAU"), STRDESC("fjfmau"),
1705565Sab196087 	},
1715565Sab196087 	{						/* 0x00008000 */
1725565Sab196087 		AV_SPARC_IMA, STRDESC("AV_SPARC_IMA"),
1735565Sab196087 		STRDESC("IMA"), STRDESC("ima"),
1745565Sab196087 	}
1750Sstevel@tonic-gate };
1760Sstevel@tonic-gate 
1775565Sab196087 
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * Order the Intel hardware capabilities to match their numeric value.  See
1810Sstevel@tonic-gate  * AV_386_ values in sys/auxv_386.h.
1820Sstevel@tonic-gate  */
1835565Sab196087 static const elfcap_desc_t hw1_386[ELFCAP_NUM_HW1_386] = {
1845565Sab196087 	{						/* 0x00000001 */
1855565Sab196087 		AV_386_FPU, STRDESC("AV_386_FPU"),
1865565Sab196087 		STRDESC("FPU"), STRDESC("fpu"),
1875565Sab196087 	},
1885565Sab196087 	{						/* 0x00000002 */
1895565Sab196087 		AV_386_TSC, STRDESC("AV_386_TSC"),
1905565Sab196087 		STRDESC("TSC"), STRDESC("tsc"),
1915565Sab196087 	},
1925565Sab196087 	{						/* 0x00000004 */
1935565Sab196087 		AV_386_CX8, STRDESC("AV_386_CX8"),
1945565Sab196087 		STRDESC("CX8"), STRDESC("cx8"),
1955565Sab196087 	},
1965565Sab196087 	{						/* 0x00000008 */
1975565Sab196087 		AV_386_SEP, STRDESC("AV_386_SEP"),
1985565Sab196087 		STRDESC("SEP"), STRDESC("sep"),
1995565Sab196087 	},
2005565Sab196087 	{						/* 0x00000010 */
2015565Sab196087 		AV_386_AMD_SYSC, STRDESC("AV_386_AMD_SYSC"),
2025565Sab196087 		STRDESC("AMD_SYSC"), STRDESC("amd_sysc"),
2035565Sab196087 	},
2045565Sab196087 	{						/* 0x00000020 */
2055565Sab196087 		AV_386_CMOV, STRDESC("AV_386_CMOV"),
2065565Sab196087 		STRDESC("CMOV"), STRDESC("cmov"),
2075565Sab196087 	},
2085565Sab196087 	{						/* 0x00000040 */
2095565Sab196087 		AV_386_MMX, STRDESC("AV_386_MMX"),
2105565Sab196087 		STRDESC("MMX"), STRDESC("mmx"),
2115565Sab196087 	},
2125565Sab196087 	{						/* 0x00000080 */
2135565Sab196087 		AV_386_AMD_MMX, STRDESC("AV_386_AMD_MMX"),
2145565Sab196087 		STRDESC("AMD_MMX"), STRDESC("amd_mmx"),
2155565Sab196087 	},
2165565Sab196087 	{						/* 0x00000100 */
2175565Sab196087 		AV_386_AMD_3DNow, STRDESC("AV_386_AMD_3DNow"),
2185565Sab196087 		STRDESC("AMD_3DNow"), STRDESC("amd_3dnow"),
2195565Sab196087 	},
2205565Sab196087 	{						/* 0x00000200 */
2215565Sab196087 		AV_386_AMD_3DNowx, STRDESC("AV_386_AMD_3DNowx"),
2225565Sab196087 		STRDESC("AMD_3DNowx"), STRDESC("amd_3dnowx"),
2235565Sab196087 	},
2245565Sab196087 	{						/* 0x00000400 */
2255565Sab196087 		AV_386_FXSR, STRDESC("AV_386_FXSR"),
2265565Sab196087 		STRDESC("FXSR"), STRDESC("fxsr"),
2275565Sab196087 	},
2285565Sab196087 	{						/* 0x00000800 */
2295565Sab196087 		AV_386_SSE, STRDESC("AV_386_SSE"),
2305565Sab196087 		STRDESC("SSE"), STRDESC("sse"),
2315565Sab196087 	},
2325565Sab196087 	{						/* 0x00001000 */
2335565Sab196087 		AV_386_SSE2, STRDESC("AV_386_SSE2"),
2345565Sab196087 		STRDESC("SSE2"), STRDESC("sse2"),
2355565Sab196087 	},
2365565Sab196087 	{						/* 0x00002000 */
2375565Sab196087 		AV_386_PAUSE, STRDESC("AV_386_PAUSE"),
2385565Sab196087 		STRDESC("PAUSE"), STRDESC("pause"),
2395565Sab196087 	},
2405565Sab196087 	{						/* 0x00004000 */
2415565Sab196087 		AV_386_SSE3, STRDESC("AV_386_SSE3"),
2425565Sab196087 		STRDESC("SSE3"), STRDESC("sse3"),
2435565Sab196087 	},
2445565Sab196087 	{						/* 0x00008000 */
2455565Sab196087 		AV_386_MON, STRDESC("AV_386_MON"),
2465565Sab196087 		STRDESC("MON"), STRDESC("mon"),
2475565Sab196087 	},
2485565Sab196087 	{						/* 0x00010000 */
2495565Sab196087 		AV_386_CX16, STRDESC("AV_386_CX16"),
2505565Sab196087 		STRDESC("CX16"), STRDESC("cx16"),
2515565Sab196087 	},
2525565Sab196087 	{						/* 0x00020000 */
2535565Sab196087 		AV_386_AHF, STRDESC("AV_386_AHF"),
2545565Sab196087 		STRDESC("AHF"), STRDESC("ahf"),
2555565Sab196087 	},
2565565Sab196087 	{						/* 0x00040000 */
2575565Sab196087 		AV_386_TSCP, STRDESC("AV_386_TSCP"),
2585565Sab196087 		STRDESC("TSCP"), STRDESC("tscp"),
2595565Sab196087 	},
2605565Sab196087 	{						/* 0x00080000 */
2615565Sab196087 		AV_386_AMD_SSE4A, STRDESC("AV_386_AMD_SSE4A"),
2625565Sab196087 		STRDESC("AMD_SSE4A"), STRDESC("amd_sse4a"),
2635565Sab196087 	},
2645565Sab196087 	{						/* 0x00100000 */
2655565Sab196087 		AV_386_POPCNT, STRDESC("AV_386_POPCNT"),
2665565Sab196087 		STRDESC("POPCNT"), STRDESC("popcnt"),
2675565Sab196087 	},
2685565Sab196087 	{						/* 0x00200000 */
2695565Sab196087 		AV_386_AMD_LZCNT, STRDESC("AV_386_AMD_LZCNT"),
2705565Sab196087 		STRDESC("AMD_LZCNT"), STRDESC("amd_lzcnt"),
2715565Sab196087 	},
2725565Sab196087 	{						/* 0x00400000 */
2735565Sab196087 		AV_386_SSSE3, STRDESC("AV_386_SSSE3"),
2745565Sab196087 		STRDESC("SSSE3"), STRDESC("ssse3"),
2755565Sab196087 	},
2765565Sab196087 	{						/* 0x00800000 */
2775565Sab196087 		AV_386_SSE4_1, STRDESC("AV_386_SSE4_1"),
2785565Sab196087 		STRDESC("SSE4.1"), STRDESC("sse4.1"),
2795565Sab196087 	},
2805565Sab196087 	{						/* 0x01000000 */
2815565Sab196087 		AV_386_SSE4_2, STRDESC("AV_386_SSE4_2"),
2825565Sab196087 		STRDESC("SSE4.2"), STRDESC("sse4.2"),
2835565Sab196087 	}
2840Sstevel@tonic-gate };
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2873446Smrj  * Concatenate a token to the string buffer.  This can be a capabilities token
2880Sstevel@tonic-gate  * or a separator token.
2890Sstevel@tonic-gate  */
2905565Sab196087 static elfcap_err_t
2915565Sab196087 token(char **ostr, size_t *olen, const elfcap_str_t *nstr)
2920Sstevel@tonic-gate {
2935565Sab196087 	if (*olen < nstr->s_len)
2945565Sab196087 		return (ELFCAP_ERR_BUFOVFL);
2955565Sab196087 
2965565Sab196087 	(void) strcat(*ostr, nstr->s_str);
2975565Sab196087 	*ostr += nstr->s_len;
2985565Sab196087 	*olen -= nstr->s_len;
2995565Sab196087 
3005565Sab196087 	return (ELFCAP_ERR_NONE);
3015565Sab196087 }
3020Sstevel@tonic-gate 
3035565Sab196087 static elfcap_err_t
3045565Sab196087 get_str_desc(elfcap_style_t style, const elfcap_desc_t *cdp,
3055565Sab196087     const elfcap_str_t **ret_str)
3065565Sab196087 {
3075565Sab196087 	switch (style) {
3085565Sab196087 	case ELFCAP_STYLE_FULL:
3095565Sab196087 		*ret_str = &cdp->c_full;
3105565Sab196087 		break;
3115565Sab196087 	case ELFCAP_STYLE_UC:
3125565Sab196087 		*ret_str = &cdp->c_uc;
3135565Sab196087 		break;
3145565Sab196087 	case ELFCAP_STYLE_LC:
3155565Sab196087 		*ret_str = &cdp->c_lc;
3165565Sab196087 		break;
3175565Sab196087 	default:
3185565Sab196087 		return (ELFCAP_ERR_INVSTYLE);
3195565Sab196087 	}
3200Sstevel@tonic-gate 
3215565Sab196087 	return (ELFCAP_ERR_NONE);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3245565Sab196087 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * Expand a capabilities value into the strings defined in the associated
3270Sstevel@tonic-gate  * capabilities descriptor.
3280Sstevel@tonic-gate  */
3295565Sab196087 static elfcap_err_t
3305565Sab196087 expand(elfcap_style_t style, uint64_t val, const elfcap_desc_t *cdp,
3315565Sab196087     uint_t cnum, char *str, size_t slen, elfcap_fmt_t fmt)
3320Sstevel@tonic-gate {
3335565Sab196087 	uint_t			cnt;
3345565Sab196087 	int			follow = 0, err;
3355565Sab196087 	const elfcap_str_t	*nstr;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (val == 0)
3385565Sab196087 		return (ELFCAP_ERR_NONE);
3390Sstevel@tonic-gate 
3405565Sab196087 	for (cnt = cnum; cnt > 0; cnt--) {
3415565Sab196087 		uint_t mask = cdp[cnt - 1].c_val;
3425565Sab196087 
3435565Sab196087 		if ((val & mask) != 0) {
3440Sstevel@tonic-gate 			if (follow++ && ((err = token(&str, &slen,
3455565Sab196087 			    &format[fmt])) != ELFCAP_ERR_NONE))
3460Sstevel@tonic-gate 				return (err);
3470Sstevel@tonic-gate 
3485565Sab196087 			err = get_str_desc(style, &cdp[cnt - 1], &nstr);
3495565Sab196087 			if (err != ELFCAP_ERR_NONE)
3505565Sab196087 				return (err);
3515565Sab196087 			if ((err = token(&str, &slen, nstr)) != ELFCAP_ERR_NONE)
3520Sstevel@tonic-gate 				return (err);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			val = val & ~mask;
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 * If there are any unknown bits remaining display the numeric value.
3600Sstevel@tonic-gate 	 */
3610Sstevel@tonic-gate 	if (val) {
3625565Sab196087 		if (follow && ((err = token(&str, &slen, &format[fmt])) !=
3635565Sab196087 		    ELFCAP_ERR_NONE))
3640Sstevel@tonic-gate 			return (err);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 		(void) snprintf(str, slen, "0x%llx", val);
3670Sstevel@tonic-gate 	}
3685565Sab196087 	return (ELFCAP_ERR_NONE);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate  * Expand a CA_SUNW_HW_1 value.
3730Sstevel@tonic-gate  */
3745565Sab196087 elfcap_err_t
3755565Sab196087 elfcap_hw1_to_str(elfcap_style_t style, uint64_t val, char *str,
3765565Sab196087     size_t len, elfcap_fmt_t fmt, ushort_t mach)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate 	/*
3790Sstevel@tonic-gate 	 * Initialize the string buffer, and validate the format request.
3800Sstevel@tonic-gate 	 */
3810Sstevel@tonic-gate 	*str = '\0';
3825565Sab196087 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
3835565Sab196087 		return (ELFCAP_ERR_INVFMT);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
3865565Sab196087 		return (expand(style, val, &hw1_386[0], ELFCAP_NUM_HW1_386,
3875565Sab196087 		    str, len, fmt));
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
3900Sstevel@tonic-gate 	    (mach == EM_SPARCV9))
3915565Sab196087 		return (expand(style, val, hw1_sparc, ELFCAP_NUM_HW1_SPARC,
3925565Sab196087 		    str, len, fmt));
3930Sstevel@tonic-gate 
3945565Sab196087 	return (ELFCAP_ERR_UNKMACH);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
3990Sstevel@tonic-gate  * common across all platforms.  The use of "mach" is therefore redundant, but
4005565Sab196087  * is retained for compatibility with the interface of elfcap_hw1_to_str(), and
4010Sstevel@tonic-gate  * possible future expansion.
4020Sstevel@tonic-gate  */
4035565Sab196087 elfcap_err_t
4040Sstevel@tonic-gate /* ARGSUSED4 */
4055565Sab196087 elfcap_sf1_to_str(elfcap_style_t style, uint64_t val, char *str,
4065565Sab196087     size_t len, elfcap_fmt_t fmt, ushort_t mach)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	/*
4090Sstevel@tonic-gate 	 * Initialize the string buffer, and validate the format request.
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate 	*str = '\0';
4125565Sab196087 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
4135565Sab196087 		return (ELFCAP_ERR_INVFMT);
4140Sstevel@tonic-gate 
4155565Sab196087 	return (expand(style, val, &sf1[0], ELFCAP_NUM_SF1, str, len, fmt));
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate /*
4195565Sab196087  * Given a capability tag type and value, map it to a string representation.
4200Sstevel@tonic-gate  */
4215565Sab196087 elfcap_err_t
4225565Sab196087 elfcap_tag_to_str(elfcap_style_t style, uint64_t tag, uint64_t val,
4235565Sab196087     char *str, size_t len, elfcap_fmt_t fmt, ushort_t mach)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	if (tag == CA_SUNW_HW_1)
4265565Sab196087 		return (elfcap_hw1_to_str(style, val, str, len, fmt, mach));
4270Sstevel@tonic-gate 	if (tag == CA_SUNW_SF_1)
4285565Sab196087 		return (elfcap_sf1_to_str(style, val, str, len, fmt, mach));
4290Sstevel@tonic-gate 
4305565Sab196087 	return (ELFCAP_ERR_UNKTAG);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  * Determine a capabilities value from a capabilities string.
4350Sstevel@tonic-gate  */
4360Sstevel@tonic-gate static uint64_t
4375565Sab196087 value(elfcap_style_t style, const char *str, const elfcap_desc_t *cdp,
4385565Sab196087     uint_t cnum)
4390Sstevel@tonic-gate {
4405565Sab196087 	const elfcap_str_t	*nstr;
4410Sstevel@tonic-gate 	uint_t	num;
4425565Sab196087 	int	err;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	for (num = 0; num < cnum; num++) {
4455565Sab196087 		/*
4465565Sab196087 		 * Skip "reserved" bits. These are unassigned bits in the
4475565Sab196087 		 * middle of the assigned range.
4485565Sab196087 		 */
4495565Sab196087 		if (cdp[num].c_val == 0)
4505565Sab196087 			continue;
4515565Sab196087 
4525565Sab196087 		if ((err = get_str_desc(style, &cdp[num], &nstr)) != 0)
4535565Sab196087 			return (err);
4545565Sab196087 		if (strcmp(str, nstr->s_str) == 0)
4550Sstevel@tonic-gate 			return (cdp[num].c_val);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	return (0);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate uint64_t
4615565Sab196087 elfcap_sf1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
4620Sstevel@tonic-gate {
4635565Sab196087 	return (value(style, str, &sf1[0], ELFCAP_NUM_SF1));
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate uint64_t
4675565Sab196087 elfcap_hw1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
4705565Sab196087 		return (value(style, str, &hw1_386[0], ELFCAP_NUM_HW1_386));
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
4730Sstevel@tonic-gate 	    (mach == EM_SPARCV9))
4745565Sab196087 		return (value(style, str, hw1_sparc, ELFCAP_NUM_HW1_SPARC));
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	return (0);
4770Sstevel@tonic-gate }
4785565Sab196087 
4795565Sab196087 /*
4805565Sab196087  * These functions allow the caller to get direct access to the
4815565Sab196087  * cap descriptors.
4825565Sab196087  */
4835565Sab196087 const elfcap_desc_t *
4845565Sab196087 elfcap_getdesc_hw1_sparc(void)
4855565Sab196087 {
4865565Sab196087 	return (hw1_sparc);
4875565Sab196087 }
4885565Sab196087 
4895565Sab196087 const elfcap_desc_t *
4905565Sab196087 elfcap_getdesc_hw1_386(void)
4915565Sab196087 {
4925565Sab196087 	return (hw1_386);
4935565Sab196087 }
4945565Sab196087 
4955565Sab196087 const elfcap_desc_t *
4965565Sab196087 elfcap_getdesc_sf1(void)
4975565Sab196087 {
4985565Sab196087 	return (sf1);
4995565Sab196087 }
500