xref: /onnv-gate/usr/src/common/elfcap/elfcap.c (revision 13134:8315ff49e22e)
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 /*
23*13134Skuriakose.kuruvilla@oracle.com  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
253446Smrj 
260Sstevel@tonic-gate /* LINTLIBRARY */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * String conversion routine for hardware capabilities types.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #include	<strings.h>
320Sstevel@tonic-gate #include	<stdio.h>
330Sstevel@tonic-gate #include	<ctype.h>
340Sstevel@tonic-gate #include	<sys/machelf.h>
350Sstevel@tonic-gate #include	<sys/elf.h>
360Sstevel@tonic-gate #include	<sys/auxv_SPARC.h>
370Sstevel@tonic-gate #include	<sys/auxv_386.h>
380Sstevel@tonic-gate #include	<elfcap.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
415565Sab196087  * Given a literal string, generate an initialization for an
425565Sab196087  * elfcap_str_t value.
430Sstevel@tonic-gate  */
445565Sab196087 #define	STRDESC(_str) { _str, sizeof (_str) - 1 }
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
475565Sab196087  * The items in the elfcap_desc_t arrays are required to be
485565Sab196087  * ordered so that the array index is related to the
495565Sab196087  * c_val field as:
505565Sab196087  *
515565Sab196087  *	array[ndx].c_val = 2^ndx
525565Sab196087  *
535565Sab196087  * meaning that
540Sstevel@tonic-gate  *
555565Sab196087  *	array[0].c_val = 2^0 = 1
565565Sab196087  *	array[1].c_val = 2^1 = 2
575565Sab196087  *	array[2].c_val = 2^2 = 4
585565Sab196087  *	.
595565Sab196087  *	.
605565Sab196087  *	.
615565Sab196087  *
625565Sab196087  * Since 0 is not a valid value for the c_val field, we use it to
635565Sab196087  * mark an array entry that is a placeholder. This can happen if there
645565Sab196087  * is a hole in the assigned bits.
655565Sab196087  *
665565Sab196087  * The RESERVED_ELFCAP_DESC macro is used to reserve such holes.
670Sstevel@tonic-gate  */
685565Sab196087 #define	RESERVED_ELFCAP_DESC { 0, { NULL, 0 }, { NULL, 0 }, { NULL, 0 } }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
715565Sab196087  * Define separators for output string processing. This must be kept in
725565Sab196087  * sync with the elfcap_fmt_t values in elfcap.h.
730Sstevel@tonic-gate  */
745565Sab196087 static const elfcap_str_t format[] = {
755565Sab196087 	STRDESC(" "),			/* ELFCAP_FMT_SNGSPACE */
765565Sab196087 	STRDESC("  "),			/* ELFCAP_FMT_DBLSPACE */
775565Sab196087 	STRDESC(" | ")			/* ELFCAP_FMT_PIPSPACE */
785565Sab196087 };
795565Sab196087 #define	FORMAT_NELTS	(sizeof (format) / sizeof (format[0]))
805565Sab196087 
815565Sab196087 
822801Shyw 
835565Sab196087 /*
845565Sab196087  * Define all known software capabilities in all the supported styles.
855565Sab196087  * Order the capabilities by their numeric value. See SF1_SUNW_
865565Sab196087  * values in sys/elf.h.
875565Sab196087  */
885565Sab196087 static const elfcap_desc_t sf1[ELFCAP_NUM_SF1] = {
895565Sab196087 	{						/* 0x00000001 */
905565Sab196087 		SF1_SUNW_FPKNWN, STRDESC("SF1_SUNW_FPKNWN"),
915565Sab196087 		STRDESC("FPKNWN"), STRDESC("fpknwn")
925565Sab196087 	},
935565Sab196087 	{						/* 0x00000002 */
945565Sab196087 		SF1_SUNW_FPUSED, STRDESC("SF1_SUNW_FPUSED"),
955565Sab196087 		STRDESC("FPUSED"), STRDESC("fpused"),
967833SRod.Evans@Sun.COM 	},
977833SRod.Evans@Sun.COM 	{						/* 0x00000004 */
987833SRod.Evans@Sun.COM 		SF1_SUNW_ADDR32, STRDESC("SF1_SUNW_ADDR32"),
997833SRod.Evans@Sun.COM 		STRDESC("ADDR32"), STRDESC("addr32"),
1005565Sab196087 	}
1015565Sab196087 };
1025565Sab196087 
1035565Sab196087 
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Order the SPARC hardware capabilities to match their numeric value.  See
1070Sstevel@tonic-gate  * AV_SPARC_ values in sys/auxv_SPARC.h.
1080Sstevel@tonic-gate  */
1095565Sab196087 static const elfcap_desc_t hw1_sparc[ELFCAP_NUM_HW1_SPARC] = {
1105565Sab196087 	{						/* 0x00000001 */
1115565Sab196087 		AV_SPARC_MUL32, STRDESC("AV_SPARC_MUL32"),
1125565Sab196087 		STRDESC("MUL32"), STRDESC("mul32"),
1135565Sab196087 	},
1145565Sab196087 	{						/* 0x00000002 */
1155565Sab196087 		AV_SPARC_DIV32, STRDESC("AV_SPARC_DIV32"),
1165565Sab196087 		STRDESC("DIV32"), STRDESC("div32"),
1175565Sab196087 	},
1185565Sab196087 	{						/* 0x00000004 */
1195565Sab196087 		AV_SPARC_FSMULD, STRDESC("AV_SPARC_FSMULD"),
1205565Sab196087 		STRDESC("FSMULD"), STRDESC("fsmuld"),
1215565Sab196087 	},
1225565Sab196087 	{						/* 0x00000008 */
1235565Sab196087 		AV_SPARC_V8PLUS, STRDESC("AV_SPARC_V8PLUS"),
1245565Sab196087 		STRDESC("V8PLUS"), STRDESC("v8plus"),
1255565Sab196087 	},
1265565Sab196087 	{						/* 0x00000010 */
1275565Sab196087 		AV_SPARC_POPC, STRDESC("AV_SPARC_POPC"),
1285565Sab196087 		STRDESC("POPC"), STRDESC("popc"),
1295565Sab196087 	},
1305565Sab196087 	{						/* 0x00000020 */
1315565Sab196087 		AV_SPARC_VIS, STRDESC("AV_SPARC_VIS"),
1325565Sab196087 		STRDESC("VIS"), STRDESC("vis"),
1335565Sab196087 	},
1345565Sab196087 	{						/* 0x00000040 */
1355565Sab196087 		AV_SPARC_VIS2, STRDESC("AV_SPARC_VIS2"),
1365565Sab196087 		STRDESC("VIS2"), STRDESC("vis2"),
1375565Sab196087 	},
1385565Sab196087 	{						/* 0x00000080 */
1395565Sab196087 		AV_SPARC_ASI_BLK_INIT, STRDESC("AV_SPARC_ASI_BLK_INIT"),
1405565Sab196087 		STRDESC("ASI_BLK_INIT"), STRDESC("asi_blk_init"),
1415565Sab196087 	},
1425565Sab196087 	{						/* 0x00000100 */
1435565Sab196087 		AV_SPARC_FMAF, STRDESC("AV_SPARC_FMAF"),
1445565Sab196087 		STRDESC("FMAF"), STRDESC("fmaf"),
1455565Sab196087 	},
14610271SJason.Beloro@Sun.COM 	RESERVED_ELFCAP_DESC,				/* 0x00000200 */
1477718SJason.Beloro@Sun.COM 	{						/* 0x00000400 */
1487718SJason.Beloro@Sun.COM 		AV_SPARC_VIS3, STRDESC("AV_SPARC_VIS3"),
1497718SJason.Beloro@Sun.COM 		STRDESC("VIS3"), STRDESC("vis3"),
1507718SJason.Beloro@Sun.COM 	},
1517718SJason.Beloro@Sun.COM 	{						/* 0x00000800 */
1527718SJason.Beloro@Sun.COM 		AV_SPARC_HPC, STRDESC("AV_SPARC_HPC"),
1537718SJason.Beloro@Sun.COM 		STRDESC("HPC"), STRDESC("hpc"),
1547718SJason.Beloro@Sun.COM 	},
1557718SJason.Beloro@Sun.COM 	{						/* 0x00001000 */
1567718SJason.Beloro@Sun.COM 		AV_SPARC_RANDOM, STRDESC("AV_SPARC_RANDOM"),
1577718SJason.Beloro@Sun.COM 		STRDESC("RANDOM"), STRDESC("random"),
1587718SJason.Beloro@Sun.COM 	},
1597718SJason.Beloro@Sun.COM 	{						/* 0x00002000 */
1607718SJason.Beloro@Sun.COM 		AV_SPARC_TRANS, STRDESC("AV_SPARC_TRANS"),
1617718SJason.Beloro@Sun.COM 		STRDESC("TRANS"), STRDESC("trans"),
1627718SJason.Beloro@Sun.COM 	},
1635565Sab196087 	{						/* 0x00004000 */
1645565Sab196087 		AV_SPARC_FJFMAU, STRDESC("AV_SPARC_FJFMAU"),
1655565Sab196087 		STRDESC("FJFMAU"), STRDESC("fjfmau"),
1665565Sab196087 	},
1675565Sab196087 	{						/* 0x00008000 */
1685565Sab196087 		AV_SPARC_IMA, STRDESC("AV_SPARC_IMA"),
1695565Sab196087 		STRDESC("IMA"), STRDESC("ima"),
1709202SJason.Beloro@Sun.COM 	},
1719202SJason.Beloro@Sun.COM 	{						/* 0x00010000 */
1729202SJason.Beloro@Sun.COM 		AV_SPARC_ASI_CACHE_SPARING,
1739202SJason.Beloro@Sun.COM 		STRDESC("AV_SPARC_ASI_CACHE_SPARING"),
1749202SJason.Beloro@Sun.COM 		STRDESC("CSPARE"), STRDESC("cspare"),
1755565Sab196087 	}
1760Sstevel@tonic-gate };
1770Sstevel@tonic-gate 
1785565Sab196087 
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * Order the Intel hardware capabilities to match their numeric value.  See
1820Sstevel@tonic-gate  * AV_386_ values in sys/auxv_386.h.
1830Sstevel@tonic-gate  */
1845565Sab196087 static const elfcap_desc_t hw1_386[ELFCAP_NUM_HW1_386] = {
1855565Sab196087 	{						/* 0x00000001 */
1865565Sab196087 		AV_386_FPU, STRDESC("AV_386_FPU"),
1875565Sab196087 		STRDESC("FPU"), STRDESC("fpu"),
1885565Sab196087 	},
1895565Sab196087 	{						/* 0x00000002 */
1905565Sab196087 		AV_386_TSC, STRDESC("AV_386_TSC"),
1915565Sab196087 		STRDESC("TSC"), STRDESC("tsc"),
1925565Sab196087 	},
1935565Sab196087 	{						/* 0x00000004 */
1945565Sab196087 		AV_386_CX8, STRDESC("AV_386_CX8"),
1955565Sab196087 		STRDESC("CX8"), STRDESC("cx8"),
1965565Sab196087 	},
1975565Sab196087 	{						/* 0x00000008 */
1985565Sab196087 		AV_386_SEP, STRDESC("AV_386_SEP"),
1995565Sab196087 		STRDESC("SEP"), STRDESC("sep"),
2005565Sab196087 	},
2015565Sab196087 	{						/* 0x00000010 */
2025565Sab196087 		AV_386_AMD_SYSC, STRDESC("AV_386_AMD_SYSC"),
2035565Sab196087 		STRDESC("AMD_SYSC"), STRDESC("amd_sysc"),
2045565Sab196087 	},
2055565Sab196087 	{						/* 0x00000020 */
2065565Sab196087 		AV_386_CMOV, STRDESC("AV_386_CMOV"),
2075565Sab196087 		STRDESC("CMOV"), STRDESC("cmov"),
2085565Sab196087 	},
2095565Sab196087 	{						/* 0x00000040 */
2105565Sab196087 		AV_386_MMX, STRDESC("AV_386_MMX"),
2115565Sab196087 		STRDESC("MMX"), STRDESC("mmx"),
2125565Sab196087 	},
2135565Sab196087 	{						/* 0x00000080 */
2145565Sab196087 		AV_386_AMD_MMX, STRDESC("AV_386_AMD_MMX"),
2155565Sab196087 		STRDESC("AMD_MMX"), STRDESC("amd_mmx"),
2165565Sab196087 	},
2175565Sab196087 	{						/* 0x00000100 */
2185565Sab196087 		AV_386_AMD_3DNow, STRDESC("AV_386_AMD_3DNow"),
2195565Sab196087 		STRDESC("AMD_3DNow"), STRDESC("amd_3dnow"),
2205565Sab196087 	},
2215565Sab196087 	{						/* 0x00000200 */
2225565Sab196087 		AV_386_AMD_3DNowx, STRDESC("AV_386_AMD_3DNowx"),
2235565Sab196087 		STRDESC("AMD_3DNowx"), STRDESC("amd_3dnowx"),
2245565Sab196087 	},
2255565Sab196087 	{						/* 0x00000400 */
2265565Sab196087 		AV_386_FXSR, STRDESC("AV_386_FXSR"),
2275565Sab196087 		STRDESC("FXSR"), STRDESC("fxsr"),
2285565Sab196087 	},
2295565Sab196087 	{						/* 0x00000800 */
2305565Sab196087 		AV_386_SSE, STRDESC("AV_386_SSE"),
2315565Sab196087 		STRDESC("SSE"), STRDESC("sse"),
2325565Sab196087 	},
2335565Sab196087 	{						/* 0x00001000 */
2345565Sab196087 		AV_386_SSE2, STRDESC("AV_386_SSE2"),
2355565Sab196087 		STRDESC("SSE2"), STRDESC("sse2"),
2365565Sab196087 	},
23711919SRod.Evans@Sun.COM 	/* 0x02000 withdrawn - do not assign */
2385565Sab196087 	{						/* 0x00004000 */
2395565Sab196087 		AV_386_SSE3, STRDESC("AV_386_SSE3"),
2405565Sab196087 		STRDESC("SSE3"), STRDESC("sse3"),
2415565Sab196087 	},
24211919SRod.Evans@Sun.COM 	/* 0x08000 withdrawn - do not assign */
2435565Sab196087 	{						/* 0x00010000 */
2445565Sab196087 		AV_386_CX16, STRDESC("AV_386_CX16"),
2455565Sab196087 		STRDESC("CX16"), STRDESC("cx16"),
2465565Sab196087 	},
2475565Sab196087 	{						/* 0x00020000 */
2485565Sab196087 		AV_386_AHF, STRDESC("AV_386_AHF"),
2495565Sab196087 		STRDESC("AHF"), STRDESC("ahf"),
2505565Sab196087 	},
2515565Sab196087 	{						/* 0x00040000 */
2525565Sab196087 		AV_386_TSCP, STRDESC("AV_386_TSCP"),
2535565Sab196087 		STRDESC("TSCP"), STRDESC("tscp"),
2545565Sab196087 	},
2555565Sab196087 	{						/* 0x00080000 */
2565565Sab196087 		AV_386_AMD_SSE4A, STRDESC("AV_386_AMD_SSE4A"),
2575565Sab196087 		STRDESC("AMD_SSE4A"), STRDESC("amd_sse4a"),
2585565Sab196087 	},
2595565Sab196087 	{						/* 0x00100000 */
2605565Sab196087 		AV_386_POPCNT, STRDESC("AV_386_POPCNT"),
2615565Sab196087 		STRDESC("POPCNT"), STRDESC("popcnt"),
2625565Sab196087 	},
2635565Sab196087 	{						/* 0x00200000 */
2645565Sab196087 		AV_386_AMD_LZCNT, STRDESC("AV_386_AMD_LZCNT"),
2655565Sab196087 		STRDESC("AMD_LZCNT"), STRDESC("amd_lzcnt"),
2665565Sab196087 	},
2675565Sab196087 	{						/* 0x00400000 */
2685565Sab196087 		AV_386_SSSE3, STRDESC("AV_386_SSSE3"),
2695565Sab196087 		STRDESC("SSSE3"), STRDESC("ssse3"),
2705565Sab196087 	},
2715565Sab196087 	{						/* 0x00800000 */
2725565Sab196087 		AV_386_SSE4_1, STRDESC("AV_386_SSE4_1"),
2735565Sab196087 		STRDESC("SSE4.1"), STRDESC("sse4.1"),
2745565Sab196087 	},
2755565Sab196087 	{						/* 0x01000000 */
2765565Sab196087 		AV_386_SSE4_2, STRDESC("AV_386_SSE4_2"),
2775565Sab196087 		STRDESC("SSE4.2"), STRDESC("sse4.2"),
2788418SKrishnendu.Sadhukhan@Sun.COM 	},
2798418SKrishnendu.Sadhukhan@Sun.COM 	{						/* 0x02000000 */
2808418SKrishnendu.Sadhukhan@Sun.COM 		AV_386_MOVBE, STRDESC("AV_386_MOVBE"),
2818418SKrishnendu.Sadhukhan@Sun.COM 		STRDESC("MOVBE"), STRDESC("movbe"),
2829370SKuriakose.Kuruvilla@Sun.COM 	},
2839370SKuriakose.Kuruvilla@Sun.COM 	{						/* 0x04000000 */
2849370SKuriakose.Kuruvilla@Sun.COM 		AV_386_AES, STRDESC("AV_386_AES"),
2859370SKuriakose.Kuruvilla@Sun.COM 		STRDESC("AES"), STRDESC("aes"),
2869370SKuriakose.Kuruvilla@Sun.COM 	},
2879370SKuriakose.Kuruvilla@Sun.COM 	{						/* 0x08000000 */
2889370SKuriakose.Kuruvilla@Sun.COM 		AV_386_PCLMULQDQ, STRDESC("AV_386_PCLMULQDQ"),
2899370SKuriakose.Kuruvilla@Sun.COM 		STRDESC("PCLMULQDQ"), STRDESC("pclmulqdq"),
290*13134Skuriakose.kuruvilla@oracle.com 	},
291*13134Skuriakose.kuruvilla@oracle.com 	{						/* 0x10000000 */
292*13134Skuriakose.kuruvilla@oracle.com 		AV_386_XSAVE, STRDESC("AV_386_XSAVE"),
293*13134Skuriakose.kuruvilla@oracle.com 		STRDESC("XSAVE"), STRDESC("xsave"),
294*13134Skuriakose.kuruvilla@oracle.com 	},
295*13134Skuriakose.kuruvilla@oracle.com 	{						/* 0x20000000 */
296*13134Skuriakose.kuruvilla@oracle.com 		AV_386_AVX, STRDESC("AV_386_AVX"),
297*13134Skuriakose.kuruvilla@oracle.com 		STRDESC("AVX"), STRDESC("avx"),
2985565Sab196087 	}
2990Sstevel@tonic-gate };
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /*
3023446Smrj  * Concatenate a token to the string buffer.  This can be a capabilities token
3030Sstevel@tonic-gate  * or a separator token.
3040Sstevel@tonic-gate  */
3055565Sab196087 static elfcap_err_t
token(char ** ostr,size_t * olen,const elfcap_str_t * nstr)3065565Sab196087 token(char **ostr, size_t *olen, const elfcap_str_t *nstr)
3070Sstevel@tonic-gate {
3085565Sab196087 	if (*olen < nstr->s_len)
3095565Sab196087 		return (ELFCAP_ERR_BUFOVFL);
3105565Sab196087 
3115565Sab196087 	(void) strcat(*ostr, nstr->s_str);
3125565Sab196087 	*ostr += nstr->s_len;
3135565Sab196087 	*olen -= nstr->s_len;
3145565Sab196087 
3155565Sab196087 	return (ELFCAP_ERR_NONE);
3165565Sab196087 }
3170Sstevel@tonic-gate 
3185565Sab196087 static elfcap_err_t
get_str_desc(elfcap_style_t style,const elfcap_desc_t * cdp,const elfcap_str_t ** ret_str)3195565Sab196087 get_str_desc(elfcap_style_t style, const elfcap_desc_t *cdp,
3205565Sab196087     const elfcap_str_t **ret_str)
3215565Sab196087 {
32211734SAli.Bahrami@Sun.COM 	switch (ELFCAP_STYLE_MASK(style)) {
3235565Sab196087 	case ELFCAP_STYLE_FULL:
3245565Sab196087 		*ret_str = &cdp->c_full;
3255565Sab196087 		break;
3265565Sab196087 	case ELFCAP_STYLE_UC:
3275565Sab196087 		*ret_str = &cdp->c_uc;
3285565Sab196087 		break;
3295565Sab196087 	case ELFCAP_STYLE_LC:
3305565Sab196087 		*ret_str = &cdp->c_lc;
3315565Sab196087 		break;
3325565Sab196087 	default:
3335565Sab196087 		return (ELFCAP_ERR_INVSTYLE);
3345565Sab196087 	}
3350Sstevel@tonic-gate 
3365565Sab196087 	return (ELFCAP_ERR_NONE);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3395565Sab196087 
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate  * Expand a capabilities value into the strings defined in the associated
3420Sstevel@tonic-gate  * capabilities descriptor.
3430Sstevel@tonic-gate  */
3445565Sab196087 static elfcap_err_t
expand(elfcap_style_t style,elfcap_mask_t val,const elfcap_desc_t * cdp,uint_t cnum,char * str,size_t slen,elfcap_fmt_t fmt)34511734SAli.Bahrami@Sun.COM expand(elfcap_style_t style, elfcap_mask_t val, const elfcap_desc_t *cdp,
3465565Sab196087     uint_t cnum, char *str, size_t slen, elfcap_fmt_t fmt)
3470Sstevel@tonic-gate {
3485565Sab196087 	uint_t			cnt;
3495565Sab196087 	int			follow = 0, err;
3505565Sab196087 	const elfcap_str_t	*nstr;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if (val == 0)
3535565Sab196087 		return (ELFCAP_ERR_NONE);
3540Sstevel@tonic-gate 
3555565Sab196087 	for (cnt = cnum; cnt > 0; cnt--) {
3565565Sab196087 		uint_t mask = cdp[cnt - 1].c_val;
3575565Sab196087 
3585565Sab196087 		if ((val & mask) != 0) {
3590Sstevel@tonic-gate 			if (follow++ && ((err = token(&str, &slen,
3605565Sab196087 			    &format[fmt])) != ELFCAP_ERR_NONE))
3610Sstevel@tonic-gate 				return (err);
3620Sstevel@tonic-gate 
3635565Sab196087 			err = get_str_desc(style, &cdp[cnt - 1], &nstr);
3645565Sab196087 			if (err != ELFCAP_ERR_NONE)
3655565Sab196087 				return (err);
3665565Sab196087 			if ((err = token(&str, &slen, nstr)) != ELFCAP_ERR_NONE)
3670Sstevel@tonic-gate 				return (err);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 			val = val & ~mask;
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 * If there are any unknown bits remaining display the numeric value.
3750Sstevel@tonic-gate 	 */
3760Sstevel@tonic-gate 	if (val) {
3775565Sab196087 		if (follow && ((err = token(&str, &slen, &format[fmt])) !=
3785565Sab196087 		    ELFCAP_ERR_NONE))
3790Sstevel@tonic-gate 			return (err);
3800Sstevel@tonic-gate 
38111734SAli.Bahrami@Sun.COM 		(void) snprintf(str, slen, "0x%x", val);
3820Sstevel@tonic-gate 	}
3835565Sab196087 	return (ELFCAP_ERR_NONE);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate  * Expand a CA_SUNW_HW_1 value.
3880Sstevel@tonic-gate  */
3895565Sab196087 elfcap_err_t
elfcap_hw1_to_str(elfcap_style_t style,elfcap_mask_t val,char * str,size_t len,elfcap_fmt_t fmt,ushort_t mach)39011734SAli.Bahrami@Sun.COM elfcap_hw1_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
3915565Sab196087     size_t len, elfcap_fmt_t fmt, ushort_t mach)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	/*
3940Sstevel@tonic-gate 	 * Initialize the string buffer, and validate the format request.
3950Sstevel@tonic-gate 	 */
3960Sstevel@tonic-gate 	*str = '\0';
3975565Sab196087 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
3985565Sab196087 		return (ELFCAP_ERR_INVFMT);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
4015565Sab196087 		return (expand(style, val, &hw1_386[0], ELFCAP_NUM_HW1_386,
4025565Sab196087 		    str, len, fmt));
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
4050Sstevel@tonic-gate 	    (mach == EM_SPARCV9))
4065565Sab196087 		return (expand(style, val, hw1_sparc, ELFCAP_NUM_HW1_SPARC,
4075565Sab196087 		    str, len, fmt));
4080Sstevel@tonic-gate 
4095565Sab196087 	return (ELFCAP_ERR_UNKMACH);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*
41311734SAli.Bahrami@Sun.COM  * Expand a CA_SUNW_HW_2 value.  Presently, there are no values, this routine
41411734SAli.Bahrami@Sun.COM  * is simply a place holder for future development.
41511734SAli.Bahrami@Sun.COM  */
41611734SAli.Bahrami@Sun.COM elfcap_err_t
41711734SAli.Bahrami@Sun.COM /* ARGSUSED0 */
elfcap_hw2_to_str(elfcap_style_t style,elfcap_mask_t val,char * str,size_t len,elfcap_fmt_t fmt,ushort_t mach)41811734SAli.Bahrami@Sun.COM elfcap_hw2_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
41911734SAli.Bahrami@Sun.COM     size_t len, elfcap_fmt_t fmt, ushort_t mach)
42011734SAli.Bahrami@Sun.COM {
42111734SAli.Bahrami@Sun.COM 	/*
42211734SAli.Bahrami@Sun.COM 	 * Initialize the string buffer, and validate the format request.
42311734SAli.Bahrami@Sun.COM 	 */
42411734SAli.Bahrami@Sun.COM 	*str = '\0';
42511734SAli.Bahrami@Sun.COM 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
42611734SAli.Bahrami@Sun.COM 		return (ELFCAP_ERR_INVFMT);
42711734SAli.Bahrami@Sun.COM 
42811734SAli.Bahrami@Sun.COM 	return (expand(style, val, NULL, 0, str, len, fmt));
42911734SAli.Bahrami@Sun.COM }
43011734SAli.Bahrami@Sun.COM 
43111734SAli.Bahrami@Sun.COM /*
4320Sstevel@tonic-gate  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
4330Sstevel@tonic-gate  * common across all platforms.  The use of "mach" is therefore redundant, but
4345565Sab196087  * is retained for compatibility with the interface of elfcap_hw1_to_str(), and
4350Sstevel@tonic-gate  * possible future expansion.
4360Sstevel@tonic-gate  */
4375565Sab196087 elfcap_err_t
4380Sstevel@tonic-gate /* ARGSUSED4 */
elfcap_sf1_to_str(elfcap_style_t style,elfcap_mask_t val,char * str,size_t len,elfcap_fmt_t fmt,ushort_t mach)43911734SAli.Bahrami@Sun.COM elfcap_sf1_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
4405565Sab196087     size_t len, elfcap_fmt_t fmt, ushort_t mach)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate 	/*
4430Sstevel@tonic-gate 	 * Initialize the string buffer, and validate the format request.
4440Sstevel@tonic-gate 	 */
4450Sstevel@tonic-gate 	*str = '\0';
4465565Sab196087 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
4475565Sab196087 		return (ELFCAP_ERR_INVFMT);
4480Sstevel@tonic-gate 
4495565Sab196087 	return (expand(style, val, &sf1[0], ELFCAP_NUM_SF1, str, len, fmt));
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /*
4535565Sab196087  * Given a capability tag type and value, map it to a string representation.
4540Sstevel@tonic-gate  */
4555565Sab196087 elfcap_err_t
elfcap_tag_to_str(elfcap_style_t style,uint64_t tag,elfcap_mask_t val,char * str,size_t len,elfcap_fmt_t fmt,ushort_t mach)45611734SAli.Bahrami@Sun.COM elfcap_tag_to_str(elfcap_style_t style, uint64_t tag, elfcap_mask_t val,
4575565Sab196087     char *str, size_t len, elfcap_fmt_t fmt, ushort_t mach)
4580Sstevel@tonic-gate {
45911734SAli.Bahrami@Sun.COM 	switch (tag) {
46011734SAli.Bahrami@Sun.COM 	case CA_SUNW_HW_1:
4615565Sab196087 		return (elfcap_hw1_to_str(style, val, str, len, fmt, mach));
46211734SAli.Bahrami@Sun.COM 
46311734SAli.Bahrami@Sun.COM 	case CA_SUNW_SF_1:
4645565Sab196087 		return (elfcap_sf1_to_str(style, val, str, len, fmt, mach));
4650Sstevel@tonic-gate 
46611734SAli.Bahrami@Sun.COM 	case CA_SUNW_HW_2:
46711734SAli.Bahrami@Sun.COM 		return (elfcap_hw2_to_str(style, val, str, len, fmt, mach));
46811734SAli.Bahrami@Sun.COM 
46911734SAli.Bahrami@Sun.COM 	}
47011734SAli.Bahrami@Sun.COM 
4715565Sab196087 	return (ELFCAP_ERR_UNKTAG);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * Determine a capabilities value from a capabilities string.
4760Sstevel@tonic-gate  */
47711734SAli.Bahrami@Sun.COM static elfcap_mask_t
value(elfcap_style_t style,const char * str,const elfcap_desc_t * cdp,uint_t cnum)4785565Sab196087 value(elfcap_style_t style, const char *str, const elfcap_desc_t *cdp,
4795565Sab196087     uint_t cnum)
4800Sstevel@tonic-gate {
4815565Sab196087 	const elfcap_str_t	*nstr;
4820Sstevel@tonic-gate 	uint_t	num;
4835565Sab196087 	int	err;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	for (num = 0; num < cnum; num++) {
4865565Sab196087 		/*
4875565Sab196087 		 * Skip "reserved" bits. These are unassigned bits in the
4885565Sab196087 		 * middle of the assigned range.
4895565Sab196087 		 */
4905565Sab196087 		if (cdp[num].c_val == 0)
4915565Sab196087 			continue;
4925565Sab196087 
4935565Sab196087 		if ((err = get_str_desc(style, &cdp[num], &nstr)) != 0)
4945565Sab196087 			return (err);
49511734SAli.Bahrami@Sun.COM 		if (style & ELFCAP_STYLE_F_ICMP) {
49611734SAli.Bahrami@Sun.COM 			if (strcasecmp(str, nstr->s_str) == 0)
49711734SAli.Bahrami@Sun.COM 				return (cdp[num].c_val);
49811734SAli.Bahrami@Sun.COM 		} else {
49911734SAli.Bahrami@Sun.COM 			if (strcmp(str, nstr->s_str) == 0)
50011734SAli.Bahrami@Sun.COM 				return (cdp[num].c_val);
50111734SAli.Bahrami@Sun.COM 		}
5020Sstevel@tonic-gate 	}
50311734SAli.Bahrami@Sun.COM 
5040Sstevel@tonic-gate 	return (0);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
50711734SAli.Bahrami@Sun.COM elfcap_mask_t
elfcap_sf1_from_str(elfcap_style_t style,const char * str,ushort_t mach)5085565Sab196087 elfcap_sf1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
5090Sstevel@tonic-gate {
5105565Sab196087 	return (value(style, str, &sf1[0], ELFCAP_NUM_SF1));
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
51311734SAli.Bahrami@Sun.COM elfcap_mask_t
elfcap_hw1_from_str(elfcap_style_t style,const char * str,ushort_t mach)5145565Sab196087 elfcap_hw1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
5175565Sab196087 		return (value(style, str, &hw1_386[0], ELFCAP_NUM_HW1_386));
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
5200Sstevel@tonic-gate 	    (mach == EM_SPARCV9))
5215565Sab196087 		return (value(style, str, hw1_sparc, ELFCAP_NUM_HW1_SPARC));
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	return (0);
5240Sstevel@tonic-gate }
52511734SAli.Bahrami@Sun.COM elfcap_mask_t
52611734SAli.Bahrami@Sun.COM /* ARGSUSED0 */
elfcap_hw2_from_str(elfcap_style_t style,const char * str,ushort_t mach)52711734SAli.Bahrami@Sun.COM elfcap_hw2_from_str(elfcap_style_t style, const char *str, ushort_t mach)
52811734SAli.Bahrami@Sun.COM {
52911734SAli.Bahrami@Sun.COM 	return (0);
53011734SAli.Bahrami@Sun.COM }
53111734SAli.Bahrami@Sun.COM 
53211734SAli.Bahrami@Sun.COM /*
53311734SAli.Bahrami@Sun.COM  * Given a capability tag type and value, return the capabilities values
53411734SAli.Bahrami@Sun.COM  * contained in the string.
53511734SAli.Bahrami@Sun.COM  */
53611734SAli.Bahrami@Sun.COM elfcap_mask_t
elfcap_tag_from_str(elfcap_style_t style,uint64_t tag,const char * str,ushort_t mach)53711734SAli.Bahrami@Sun.COM elfcap_tag_from_str(elfcap_style_t style, uint64_t tag, const char *str,
53811734SAli.Bahrami@Sun.COM     ushort_t mach)
53911734SAli.Bahrami@Sun.COM {
54011734SAli.Bahrami@Sun.COM 	switch (tag) {
54111734SAli.Bahrami@Sun.COM 	case CA_SUNW_HW_1:
54211734SAli.Bahrami@Sun.COM 		return (elfcap_hw1_from_str(style, str, mach));
54311734SAli.Bahrami@Sun.COM 
54411734SAli.Bahrami@Sun.COM 	case CA_SUNW_SF_1:
54511734SAli.Bahrami@Sun.COM 		return (elfcap_sf1_from_str(style, str, mach));
54611734SAli.Bahrami@Sun.COM 
54711734SAli.Bahrami@Sun.COM 	case CA_SUNW_HW_2:
54811734SAli.Bahrami@Sun.COM 		return (elfcap_hw2_from_str(style, str, mach));
54911734SAli.Bahrami@Sun.COM 	}
55011734SAli.Bahrami@Sun.COM 
55111734SAli.Bahrami@Sun.COM 	return (0);
55211734SAli.Bahrami@Sun.COM }
5535565Sab196087 
5545565Sab196087 /*
5555565Sab196087  * These functions allow the caller to get direct access to the
5565565Sab196087  * cap descriptors.
5575565Sab196087  */
5585565Sab196087 const elfcap_desc_t *
elfcap_getdesc_hw1_sparc(void)5595565Sab196087 elfcap_getdesc_hw1_sparc(void)
5605565Sab196087 {
5615565Sab196087 	return (hw1_sparc);
5625565Sab196087 }
5635565Sab196087 
5645565Sab196087 const elfcap_desc_t *
elfcap_getdesc_hw1_386(void)5655565Sab196087 elfcap_getdesc_hw1_386(void)
5665565Sab196087 {
5675565Sab196087 	return (hw1_386);
5685565Sab196087 }
5695565Sab196087 
5705565Sab196087 const elfcap_desc_t *
elfcap_getdesc_sf1(void)5715565Sab196087 elfcap_getdesc_sf1(void)
5725565Sab196087 {
5735565Sab196087 	return (sf1);
5745565Sab196087 }
575