xref: /onnv-gate/usr/src/common/elfcap/elfcap.c (revision 9370:5f964d9a7826)
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 /*
239202SJason.Beloro@Sun.COM  * Copyright 2009 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"),
977833SRod.Evans@Sun.COM 	},
987833SRod.Evans@Sun.COM 	{						/* 0x00000004 */
997833SRod.Evans@Sun.COM 		SF1_SUNW_ADDR32, STRDESC("SF1_SUNW_ADDR32"),
1007833SRod.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"),
1749202SJason.Beloro@Sun.COM 	},
1759202SJason.Beloro@Sun.COM 	{						/* 0x00010000 */
1769202SJason.Beloro@Sun.COM 		AV_SPARC_ASI_CACHE_SPARING,
1779202SJason.Beloro@Sun.COM 		STRDESC("AV_SPARC_ASI_CACHE_SPARING"),
1789202SJason.Beloro@Sun.COM 		STRDESC("CSPARE"), STRDESC("cspare"),
1795565Sab196087 	}
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate 
1825565Sab196087 
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * Order the Intel hardware capabilities to match their numeric value.  See
1860Sstevel@tonic-gate  * AV_386_ values in sys/auxv_386.h.
1870Sstevel@tonic-gate  */
1885565Sab196087 static const elfcap_desc_t hw1_386[ELFCAP_NUM_HW1_386] = {
1895565Sab196087 	{						/* 0x00000001 */
1905565Sab196087 		AV_386_FPU, STRDESC("AV_386_FPU"),
1915565Sab196087 		STRDESC("FPU"), STRDESC("fpu"),
1925565Sab196087 	},
1935565Sab196087 	{						/* 0x00000002 */
1945565Sab196087 		AV_386_TSC, STRDESC("AV_386_TSC"),
1955565Sab196087 		STRDESC("TSC"), STRDESC("tsc"),
1965565Sab196087 	},
1975565Sab196087 	{						/* 0x00000004 */
1985565Sab196087 		AV_386_CX8, STRDESC("AV_386_CX8"),
1995565Sab196087 		STRDESC("CX8"), STRDESC("cx8"),
2005565Sab196087 	},
2015565Sab196087 	{						/* 0x00000008 */
2025565Sab196087 		AV_386_SEP, STRDESC("AV_386_SEP"),
2035565Sab196087 		STRDESC("SEP"), STRDESC("sep"),
2045565Sab196087 	},
2055565Sab196087 	{						/* 0x00000010 */
2065565Sab196087 		AV_386_AMD_SYSC, STRDESC("AV_386_AMD_SYSC"),
2075565Sab196087 		STRDESC("AMD_SYSC"), STRDESC("amd_sysc"),
2085565Sab196087 	},
2095565Sab196087 	{						/* 0x00000020 */
2105565Sab196087 		AV_386_CMOV, STRDESC("AV_386_CMOV"),
2115565Sab196087 		STRDESC("CMOV"), STRDESC("cmov"),
2125565Sab196087 	},
2135565Sab196087 	{						/* 0x00000040 */
2145565Sab196087 		AV_386_MMX, STRDESC("AV_386_MMX"),
2155565Sab196087 		STRDESC("MMX"), STRDESC("mmx"),
2165565Sab196087 	},
2175565Sab196087 	{						/* 0x00000080 */
2185565Sab196087 		AV_386_AMD_MMX, STRDESC("AV_386_AMD_MMX"),
2195565Sab196087 		STRDESC("AMD_MMX"), STRDESC("amd_mmx"),
2205565Sab196087 	},
2215565Sab196087 	{						/* 0x00000100 */
2225565Sab196087 		AV_386_AMD_3DNow, STRDESC("AV_386_AMD_3DNow"),
2235565Sab196087 		STRDESC("AMD_3DNow"), STRDESC("amd_3dnow"),
2245565Sab196087 	},
2255565Sab196087 	{						/* 0x00000200 */
2265565Sab196087 		AV_386_AMD_3DNowx, STRDESC("AV_386_AMD_3DNowx"),
2275565Sab196087 		STRDESC("AMD_3DNowx"), STRDESC("amd_3dnowx"),
2285565Sab196087 	},
2295565Sab196087 	{						/* 0x00000400 */
2305565Sab196087 		AV_386_FXSR, STRDESC("AV_386_FXSR"),
2315565Sab196087 		STRDESC("FXSR"), STRDESC("fxsr"),
2325565Sab196087 	},
2335565Sab196087 	{						/* 0x00000800 */
2345565Sab196087 		AV_386_SSE, STRDESC("AV_386_SSE"),
2355565Sab196087 		STRDESC("SSE"), STRDESC("sse"),
2365565Sab196087 	},
2375565Sab196087 	{						/* 0x00001000 */
2385565Sab196087 		AV_386_SSE2, STRDESC("AV_386_SSE2"),
2395565Sab196087 		STRDESC("SSE2"), STRDESC("sse2"),
2405565Sab196087 	},
2415565Sab196087 	{						/* 0x00002000 */
2425565Sab196087 		AV_386_PAUSE, STRDESC("AV_386_PAUSE"),
2435565Sab196087 		STRDESC("PAUSE"), STRDESC("pause"),
2445565Sab196087 	},
2455565Sab196087 	{						/* 0x00004000 */
2465565Sab196087 		AV_386_SSE3, STRDESC("AV_386_SSE3"),
2475565Sab196087 		STRDESC("SSE3"), STRDESC("sse3"),
2485565Sab196087 	},
2495565Sab196087 	{						/* 0x00008000 */
2505565Sab196087 		AV_386_MON, STRDESC("AV_386_MON"),
2515565Sab196087 		STRDESC("MON"), STRDESC("mon"),
2525565Sab196087 	},
2535565Sab196087 	{						/* 0x00010000 */
2545565Sab196087 		AV_386_CX16, STRDESC("AV_386_CX16"),
2555565Sab196087 		STRDESC("CX16"), STRDESC("cx16"),
2565565Sab196087 	},
2575565Sab196087 	{						/* 0x00020000 */
2585565Sab196087 		AV_386_AHF, STRDESC("AV_386_AHF"),
2595565Sab196087 		STRDESC("AHF"), STRDESC("ahf"),
2605565Sab196087 	},
2615565Sab196087 	{						/* 0x00040000 */
2625565Sab196087 		AV_386_TSCP, STRDESC("AV_386_TSCP"),
2635565Sab196087 		STRDESC("TSCP"), STRDESC("tscp"),
2645565Sab196087 	},
2655565Sab196087 	{						/* 0x00080000 */
2665565Sab196087 		AV_386_AMD_SSE4A, STRDESC("AV_386_AMD_SSE4A"),
2675565Sab196087 		STRDESC("AMD_SSE4A"), STRDESC("amd_sse4a"),
2685565Sab196087 	},
2695565Sab196087 	{						/* 0x00100000 */
2705565Sab196087 		AV_386_POPCNT, STRDESC("AV_386_POPCNT"),
2715565Sab196087 		STRDESC("POPCNT"), STRDESC("popcnt"),
2725565Sab196087 	},
2735565Sab196087 	{						/* 0x00200000 */
2745565Sab196087 		AV_386_AMD_LZCNT, STRDESC("AV_386_AMD_LZCNT"),
2755565Sab196087 		STRDESC("AMD_LZCNT"), STRDESC("amd_lzcnt"),
2765565Sab196087 	},
2775565Sab196087 	{						/* 0x00400000 */
2785565Sab196087 		AV_386_SSSE3, STRDESC("AV_386_SSSE3"),
2795565Sab196087 		STRDESC("SSSE3"), STRDESC("ssse3"),
2805565Sab196087 	},
2815565Sab196087 	{						/* 0x00800000 */
2825565Sab196087 		AV_386_SSE4_1, STRDESC("AV_386_SSE4_1"),
2835565Sab196087 		STRDESC("SSE4.1"), STRDESC("sse4.1"),
2845565Sab196087 	},
2855565Sab196087 	{						/* 0x01000000 */
2865565Sab196087 		AV_386_SSE4_2, STRDESC("AV_386_SSE4_2"),
2875565Sab196087 		STRDESC("SSE4.2"), STRDESC("sse4.2"),
2888418SKrishnendu.Sadhukhan@Sun.COM 	},
2898418SKrishnendu.Sadhukhan@Sun.COM 	{						/* 0x02000000 */
2908418SKrishnendu.Sadhukhan@Sun.COM 		AV_386_MOVBE, STRDESC("AV_386_MOVBE"),
2918418SKrishnendu.Sadhukhan@Sun.COM 		STRDESC("MOVBE"), STRDESC("movbe"),
292*9370SKuriakose.Kuruvilla@Sun.COM 	},
293*9370SKuriakose.Kuruvilla@Sun.COM 	{						/* 0x04000000 */
294*9370SKuriakose.Kuruvilla@Sun.COM 		AV_386_AES, STRDESC("AV_386_AES"),
295*9370SKuriakose.Kuruvilla@Sun.COM 		STRDESC("AES"), STRDESC("aes"),
296*9370SKuriakose.Kuruvilla@Sun.COM 	},
297*9370SKuriakose.Kuruvilla@Sun.COM 	{						/* 0x08000000 */
298*9370SKuriakose.Kuruvilla@Sun.COM 		AV_386_PCLMULQDQ, STRDESC("AV_386_PCLMULQDQ"),
299*9370SKuriakose.Kuruvilla@Sun.COM 		STRDESC("PCLMULQDQ"), STRDESC("pclmulqdq"),
3005565Sab196087 	}
3010Sstevel@tonic-gate };
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3043446Smrj  * Concatenate a token to the string buffer.  This can be a capabilities token
3050Sstevel@tonic-gate  * or a separator token.
3060Sstevel@tonic-gate  */
3075565Sab196087 static elfcap_err_t
3085565Sab196087 token(char **ostr, size_t *olen, const elfcap_str_t *nstr)
3090Sstevel@tonic-gate {
3105565Sab196087 	if (*olen < nstr->s_len)
3115565Sab196087 		return (ELFCAP_ERR_BUFOVFL);
3125565Sab196087 
3135565Sab196087 	(void) strcat(*ostr, nstr->s_str);
3145565Sab196087 	*ostr += nstr->s_len;
3155565Sab196087 	*olen -= nstr->s_len;
3165565Sab196087 
3175565Sab196087 	return (ELFCAP_ERR_NONE);
3185565Sab196087 }
3190Sstevel@tonic-gate 
3205565Sab196087 static elfcap_err_t
3215565Sab196087 get_str_desc(elfcap_style_t style, const elfcap_desc_t *cdp,
3225565Sab196087     const elfcap_str_t **ret_str)
3235565Sab196087 {
3245565Sab196087 	switch (style) {
3255565Sab196087 	case ELFCAP_STYLE_FULL:
3265565Sab196087 		*ret_str = &cdp->c_full;
3275565Sab196087 		break;
3285565Sab196087 	case ELFCAP_STYLE_UC:
3295565Sab196087 		*ret_str = &cdp->c_uc;
3305565Sab196087 		break;
3315565Sab196087 	case ELFCAP_STYLE_LC:
3325565Sab196087 		*ret_str = &cdp->c_lc;
3335565Sab196087 		break;
3345565Sab196087 	default:
3355565Sab196087 		return (ELFCAP_ERR_INVSTYLE);
3365565Sab196087 	}
3370Sstevel@tonic-gate 
3385565Sab196087 	return (ELFCAP_ERR_NONE);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3415565Sab196087 
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate  * Expand a capabilities value into the strings defined in the associated
3440Sstevel@tonic-gate  * capabilities descriptor.
3450Sstevel@tonic-gate  */
3465565Sab196087 static elfcap_err_t
3475565Sab196087 expand(elfcap_style_t style, uint64_t val, const elfcap_desc_t *cdp,
3485565Sab196087     uint_t cnum, char *str, size_t slen, elfcap_fmt_t fmt)
3490Sstevel@tonic-gate {
3505565Sab196087 	uint_t			cnt;
3515565Sab196087 	int			follow = 0, err;
3525565Sab196087 	const elfcap_str_t	*nstr;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (val == 0)
3555565Sab196087 		return (ELFCAP_ERR_NONE);
3560Sstevel@tonic-gate 
3575565Sab196087 	for (cnt = cnum; cnt > 0; cnt--) {
3585565Sab196087 		uint_t mask = cdp[cnt - 1].c_val;
3595565Sab196087 
3605565Sab196087 		if ((val & mask) != 0) {
3610Sstevel@tonic-gate 			if (follow++ && ((err = token(&str, &slen,
3625565Sab196087 			    &format[fmt])) != ELFCAP_ERR_NONE))
3630Sstevel@tonic-gate 				return (err);
3640Sstevel@tonic-gate 
3655565Sab196087 			err = get_str_desc(style, &cdp[cnt - 1], &nstr);
3665565Sab196087 			if (err != ELFCAP_ERR_NONE)
3675565Sab196087 				return (err);
3685565Sab196087 			if ((err = token(&str, &slen, nstr)) != ELFCAP_ERR_NONE)
3690Sstevel@tonic-gate 				return (err);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 			val = val & ~mask;
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * If there are any unknown bits remaining display the numeric value.
3770Sstevel@tonic-gate 	 */
3780Sstevel@tonic-gate 	if (val) {
3795565Sab196087 		if (follow && ((err = token(&str, &slen, &format[fmt])) !=
3805565Sab196087 		    ELFCAP_ERR_NONE))
3810Sstevel@tonic-gate 			return (err);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		(void) snprintf(str, slen, "0x%llx", val);
3840Sstevel@tonic-gate 	}
3855565Sab196087 	return (ELFCAP_ERR_NONE);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate  * Expand a CA_SUNW_HW_1 value.
3900Sstevel@tonic-gate  */
3915565Sab196087 elfcap_err_t
3925565Sab196087 elfcap_hw1_to_str(elfcap_style_t style, uint64_t val, char *str,
3935565Sab196087     size_t len, elfcap_fmt_t fmt, ushort_t mach)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Initialize the string buffer, and validate the format request.
3970Sstevel@tonic-gate 	 */
3980Sstevel@tonic-gate 	*str = '\0';
3995565Sab196087 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
4005565Sab196087 		return (ELFCAP_ERR_INVFMT);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
4035565Sab196087 		return (expand(style, val, &hw1_386[0], ELFCAP_NUM_HW1_386,
4045565Sab196087 		    str, len, fmt));
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
4070Sstevel@tonic-gate 	    (mach == EM_SPARCV9))
4085565Sab196087 		return (expand(style, val, hw1_sparc, ELFCAP_NUM_HW1_SPARC,
4095565Sab196087 		    str, len, fmt));
4100Sstevel@tonic-gate 
4115565Sab196087 	return (ELFCAP_ERR_UNKMACH);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
4160Sstevel@tonic-gate  * common across all platforms.  The use of "mach" is therefore redundant, but
4175565Sab196087  * is retained for compatibility with the interface of elfcap_hw1_to_str(), and
4180Sstevel@tonic-gate  * possible future expansion.
4190Sstevel@tonic-gate  */
4205565Sab196087 elfcap_err_t
4210Sstevel@tonic-gate /* ARGSUSED4 */
4225565Sab196087 elfcap_sf1_to_str(elfcap_style_t style, uint64_t val, char *str,
4235565Sab196087     size_t len, elfcap_fmt_t fmt, ushort_t mach)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	/*
4260Sstevel@tonic-gate 	 * Initialize the string buffer, and validate the format request.
4270Sstevel@tonic-gate 	 */
4280Sstevel@tonic-gate 	*str = '\0';
4295565Sab196087 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
4305565Sab196087 		return (ELFCAP_ERR_INVFMT);
4310Sstevel@tonic-gate 
4325565Sab196087 	return (expand(style, val, &sf1[0], ELFCAP_NUM_SF1, str, len, fmt));
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate /*
4365565Sab196087  * Given a capability tag type and value, map it to a string representation.
4370Sstevel@tonic-gate  */
4385565Sab196087 elfcap_err_t
4395565Sab196087 elfcap_tag_to_str(elfcap_style_t style, uint64_t tag, uint64_t val,
4405565Sab196087     char *str, size_t len, elfcap_fmt_t fmt, ushort_t mach)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate 	if (tag == CA_SUNW_HW_1)
4435565Sab196087 		return (elfcap_hw1_to_str(style, val, str, len, fmt, mach));
4440Sstevel@tonic-gate 	if (tag == CA_SUNW_SF_1)
4455565Sab196087 		return (elfcap_sf1_to_str(style, val, str, len, fmt, mach));
4460Sstevel@tonic-gate 
4475565Sab196087 	return (ELFCAP_ERR_UNKTAG);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate  * Determine a capabilities value from a capabilities string.
4520Sstevel@tonic-gate  */
4530Sstevel@tonic-gate static uint64_t
4545565Sab196087 value(elfcap_style_t style, const char *str, const elfcap_desc_t *cdp,
4555565Sab196087     uint_t cnum)
4560Sstevel@tonic-gate {
4575565Sab196087 	const elfcap_str_t	*nstr;
4580Sstevel@tonic-gate 	uint_t	num;
4595565Sab196087 	int	err;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	for (num = 0; num < cnum; num++) {
4625565Sab196087 		/*
4635565Sab196087 		 * Skip "reserved" bits. These are unassigned bits in the
4645565Sab196087 		 * middle of the assigned range.
4655565Sab196087 		 */
4665565Sab196087 		if (cdp[num].c_val == 0)
4675565Sab196087 			continue;
4685565Sab196087 
4695565Sab196087 		if ((err = get_str_desc(style, &cdp[num], &nstr)) != 0)
4705565Sab196087 			return (err);
4715565Sab196087 		if (strcmp(str, nstr->s_str) == 0)
4720Sstevel@tonic-gate 			return (cdp[num].c_val);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 	return (0);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate uint64_t
4785565Sab196087 elfcap_sf1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
4790Sstevel@tonic-gate {
4805565Sab196087 	return (value(style, str, &sf1[0], ELFCAP_NUM_SF1));
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate uint64_t
4845565Sab196087 elfcap_hw1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
4875565Sab196087 		return (value(style, str, &hw1_386[0], ELFCAP_NUM_HW1_386));
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
4900Sstevel@tonic-gate 	    (mach == EM_SPARCV9))
4915565Sab196087 		return (value(style, str, hw1_sparc, ELFCAP_NUM_HW1_SPARC));
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	return (0);
4940Sstevel@tonic-gate }
4955565Sab196087 
4965565Sab196087 /*
4975565Sab196087  * These functions allow the caller to get direct access to the
4985565Sab196087  * cap descriptors.
4995565Sab196087  */
5005565Sab196087 const elfcap_desc_t *
5015565Sab196087 elfcap_getdesc_hw1_sparc(void)
5025565Sab196087 {
5035565Sab196087 	return (hw1_sparc);
5045565Sab196087 }
5055565Sab196087 
5065565Sab196087 const elfcap_desc_t *
5075565Sab196087 elfcap_getdesc_hw1_386(void)
5085565Sab196087 {
5095565Sab196087 	return (hw1_386);
5105565Sab196087 }
5115565Sab196087 
5125565Sab196087 const elfcap_desc_t *
5135565Sab196087 elfcap_getdesc_sf1(void)
5145565Sab196087 {
5155565Sab196087 	return (sf1);
5165565Sab196087 }
517