xref: /illumos-gate/usr/src/common/elfcap/elfcap.c (revision 1ba081ee)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2019, Joyent, Inc.
25  */
26 
27 /* LINTLIBRARY */
28 
29 /*
30  * String conversion routine for hardware capabilities types.
31  */
32 #include	<strings.h>
33 #include	<stdio.h>
34 #include	<ctype.h>
35 #include	<sys/machelf.h>
36 #include	<sys/elf.h>
37 #include	<sys/auxv_SPARC.h>
38 #include	<sys/auxv_386.h>
39 #include	<elfcap.h>
40 
41 /*
42  * Given a literal string, generate an initialization for an
43  * elfcap_str_t value.
44  */
45 #define	STRDESC(_str) { _str, sizeof (_str) - 1 }
46 
47 /*
48  * The items in the elfcap_desc_t arrays are required to be
49  * ordered so that the array index is related to the
50  * c_val field as:
51  *
52  *	array[ndx].c_val = 2^ndx
53  *
54  * meaning that
55  *
56  *	array[0].c_val = 2^0 = 1
57  *	array[1].c_val = 2^1 = 2
58  *	array[2].c_val = 2^2 = 4
59  *	.
60  *	.
61  *	.
62  *
63  * Since 0 is not a valid value for the c_val field, we use it to
64  * mark an array entry that is a placeholder. This can happen if there
65  * is a hole in the assigned bits.
66  *
67  * The RESERVED_ELFCAP_DESC macro is used to reserve such holes.
68  */
69 #define	RESERVED_ELFCAP_DESC { 0, { NULL, 0 }, { NULL, 0 }, { NULL, 0 } }
70 
71 /*
72  * Define separators for output string processing. This must be kept in
73  * sync with the elfcap_fmt_t values in elfcap.h.
74  */
75 static const elfcap_str_t format[] = {
76 	STRDESC(" "),			/* ELFCAP_FMT_SNGSPACE */
77 	STRDESC("  "),			/* ELFCAP_FMT_DBLSPACE */
78 	STRDESC(" | ")			/* ELFCAP_FMT_PIPSPACE */
79 };
80 #define	FORMAT_NELTS	(sizeof (format) / sizeof (format[0]))
81 
82 
83 
84 /*
85  * Define all known software capabilities in all the supported styles.
86  * Order the capabilities by their numeric value. See SF1_SUNW_
87  * values in sys/elf.h.
88  */
89 static const elfcap_desc_t sf1[ELFCAP_NUM_SF1] = {
90 	{						/* 0x00000001 */
91 		SF1_SUNW_FPKNWN, STRDESC("SF1_SUNW_FPKNWN"),
92 		STRDESC("FPKNWN"), STRDESC("fpknwn")
93 	},
94 	{						/* 0x00000002 */
95 		SF1_SUNW_FPUSED, STRDESC("SF1_SUNW_FPUSED"),
96 		STRDESC("FPUSED"), STRDESC("fpused"),
97 	},
98 	{						/* 0x00000004 */
99 		SF1_SUNW_ADDR32, STRDESC("SF1_SUNW_ADDR32"),
100 		STRDESC("ADDR32"), STRDESC("addr32"),
101 	}
102 };
103 
104 
105 
106 /*
107  * Order the SPARC hardware capabilities to match their numeric value.  See
108  * AV_SPARC_ values in sys/auxv_SPARC.h.
109  */
110 static const elfcap_desc_t hw1_sparc[ELFCAP_NUM_HW1_SPARC] = {
111 	{						/* 0x00000001 */
112 		AV_SPARC_MUL32, STRDESC("AV_SPARC_MUL32"),
113 		STRDESC("MUL32"), STRDESC("mul32"),
114 	},
115 	{						/* 0x00000002 */
116 		AV_SPARC_DIV32, STRDESC("AV_SPARC_DIV32"),
117 		STRDESC("DIV32"), STRDESC("div32"),
118 	},
119 	{						/* 0x00000004 */
120 		AV_SPARC_FSMULD, STRDESC("AV_SPARC_FSMULD"),
121 		STRDESC("FSMULD"), STRDESC("fsmuld"),
122 	},
123 	{						/* 0x00000008 */
124 		AV_SPARC_V8PLUS, STRDESC("AV_SPARC_V8PLUS"),
125 		STRDESC("V8PLUS"), STRDESC("v8plus"),
126 	},
127 	{						/* 0x00000010 */
128 		AV_SPARC_POPC, STRDESC("AV_SPARC_POPC"),
129 		STRDESC("POPC"), STRDESC("popc"),
130 	},
131 	{						/* 0x00000020 */
132 		AV_SPARC_VIS, STRDESC("AV_SPARC_VIS"),
133 		STRDESC("VIS"), STRDESC("vis"),
134 	},
135 	{						/* 0x00000040 */
136 		AV_SPARC_VIS2, STRDESC("AV_SPARC_VIS2"),
137 		STRDESC("VIS2"), STRDESC("vis2"),
138 	},
139 	{						/* 0x00000080 */
140 		AV_SPARC_ASI_BLK_INIT, STRDESC("AV_SPARC_ASI_BLK_INIT"),
141 		STRDESC("ASI_BLK_INIT"), STRDESC("asi_blk_init"),
142 	},
143 	{						/* 0x00000100 */
144 		AV_SPARC_FMAF, STRDESC("AV_SPARC_FMAF"),
145 		STRDESC("FMAF"), STRDESC("fmaf"),
146 	},
147 	RESERVED_ELFCAP_DESC,				/* 0x00000200 */
148 	{						/* 0x00000400 */
149 		AV_SPARC_VIS3, STRDESC("AV_SPARC_VIS3"),
150 		STRDESC("VIS3"), STRDESC("vis3"),
151 	},
152 	{						/* 0x00000800 */
153 		AV_SPARC_HPC, STRDESC("AV_SPARC_HPC"),
154 		STRDESC("HPC"), STRDESC("hpc"),
155 	},
156 	{						/* 0x00001000 */
157 		AV_SPARC_RANDOM, STRDESC("AV_SPARC_RANDOM"),
158 		STRDESC("RANDOM"), STRDESC("random"),
159 	},
160 	{						/* 0x00002000 */
161 		AV_SPARC_TRANS, STRDESC("AV_SPARC_TRANS"),
162 		STRDESC("TRANS"), STRDESC("trans"),
163 	},
164 	{						/* 0x00004000 */
165 		AV_SPARC_FJFMAU, STRDESC("AV_SPARC_FJFMAU"),
166 		STRDESC("FJFMAU"), STRDESC("fjfmau"),
167 	},
168 	{						/* 0x00008000 */
169 		AV_SPARC_IMA, STRDESC("AV_SPARC_IMA"),
170 		STRDESC("IMA"), STRDESC("ima"),
171 	},
172 	{						/* 0x00010000 */
173 		AV_SPARC_ASI_CACHE_SPARING,
174 		STRDESC("AV_SPARC_ASI_CACHE_SPARING"),
175 		STRDESC("CSPARE"), STRDESC("cspare"),
176 	},
177 	{						/* 0x00020000 */
178 		AV_SPARC_PAUSE,
179 		STRDESC("AV_SPARC_PAUSE"),
180 		STRDESC("PAUSE"), STRDESC("pause"),
181 	},
182 	{						/* 0x00040000 */
183 		AV_SPARC_CBCOND,
184 		STRDESC("AV_SPARC_CBCOND"),
185 		STRDESC("CBCOND"), STRDESC("cbcond"),
186 	},
187 	{						/* 0x00080000 */
188 		AV_SPARC_AES,
189 		STRDESC("AV_SPARC_AES"),
190 		STRDESC("AES"), STRDESC("aes"),
191 	},
192 	{						/* 0x00100000 */
193 		AV_SPARC_DES,
194 		STRDESC("AV_SPARC_DES"),
195 		STRDESC("DES"), STRDESC("des"),
196 	},
197 	{						/* 0x00200000 */
198 		AV_SPARC_KASUMI,
199 		STRDESC("AV_SPARC_KASUMI"),
200 		STRDESC("KASUMI"), STRDESC("kasumi"),
201 	},
202 	{						/* 0x00400000 */
203 		AV_SPARC_CAMELLIA,
204 		STRDESC("AV_SPARC_CAMELLIA"),
205 		STRDESC("CAMELLIA"), STRDESC("camellia"),
206 	},
207 	{						/* 0x00800000 */
208 		AV_SPARC_MD5,
209 		STRDESC("AV_SPARC_MD5"),
210 		STRDESC("MD5"), STRDESC("md5"),
211 	},
212 	{						/* 0x01000000 */
213 		AV_SPARC_SHA1,
214 		STRDESC("AV_SPARC_SHA1"),
215 		STRDESC("SHA1"), STRDESC("sha1"),
216 	},
217 	{						/* 0x02000000 */
218 		AV_SPARC_SHA256,
219 		STRDESC("AV_SPARC_SHA256"),
220 		STRDESC("SHA256"), STRDESC("sha256"),
221 	},
222 	{						/* 0x04000000 */
223 		AV_SPARC_SHA512,
224 		STRDESC("AV_SPARC_SHA512"),
225 		STRDESC("SHA512"), STRDESC("sha512"),
226 	},
227 	{						/* 0x08000000 */
228 		AV_SPARC_MPMUL,
229 		STRDESC("AV_SPARC_MPMUL"),
230 		STRDESC("MPMUL"), STRDESC("mpmul"),
231 	},
232 	{						/* 0x10000000 */
233 		AV_SPARC_MONT,
234 		STRDESC("AV_SPARC_MONT"),
235 		STRDESC("MONT"), STRDESC("mont"),
236 	},
237 	{						/* 0x20000000 */
238 		AV_SPARC_CRC32C,
239 		STRDESC("AV_SPARC_CRC32C"),
240 		STRDESC("CRC32C"), STRDESC("crc32c"),
241 	}
242 };
243 
244 
245 
246 /*
247  * Order the Intel hardware capabilities to match their numeric value.  See
248  * AV_386_ values in sys/auxv_386.h.
249  */
250 static const elfcap_desc_t hw1_386[ELFCAP_NUM_HW1_386] = {
251 	{						/* 0x00000001 */
252 		AV_386_FPU, STRDESC("AV_386_FPU"),
253 		STRDESC("FPU"), STRDESC("fpu"),
254 	},
255 	{						/* 0x00000002 */
256 		AV_386_TSC, STRDESC("AV_386_TSC"),
257 		STRDESC("TSC"), STRDESC("tsc"),
258 	},
259 	{						/* 0x00000004 */
260 		AV_386_CX8, STRDESC("AV_386_CX8"),
261 		STRDESC("CX8"), STRDESC("cx8"),
262 	},
263 	{						/* 0x00000008 */
264 		AV_386_SEP, STRDESC("AV_386_SEP"),
265 		STRDESC("SEP"), STRDESC("sep"),
266 	},
267 	{						/* 0x00000010 */
268 		AV_386_AMD_SYSC, STRDESC("AV_386_AMD_SYSC"),
269 		STRDESC("AMD_SYSC"), STRDESC("amd_sysc"),
270 	},
271 	{						/* 0x00000020 */
272 		AV_386_CMOV, STRDESC("AV_386_CMOV"),
273 		STRDESC("CMOV"), STRDESC("cmov"),
274 	},
275 	{						/* 0x00000040 */
276 		AV_386_MMX, STRDESC("AV_386_MMX"),
277 		STRDESC("MMX"), STRDESC("mmx"),
278 	},
279 	{						/* 0x00000080 */
280 		AV_386_AMD_MMX, STRDESC("AV_386_AMD_MMX"),
281 		STRDESC("AMD_MMX"), STRDESC("amd_mmx"),
282 	},
283 	{						/* 0x00000100 */
284 		AV_386_AMD_3DNow, STRDESC("AV_386_AMD_3DNow"),
285 		STRDESC("AMD_3DNow"), STRDESC("amd_3dnow"),
286 	},
287 	{						/* 0x00000200 */
288 		AV_386_AMD_3DNowx, STRDESC("AV_386_AMD_3DNowx"),
289 		STRDESC("AMD_3DNowx"), STRDESC("amd_3dnowx"),
290 	},
291 	{						/* 0x00000400 */
292 		AV_386_FXSR, STRDESC("AV_386_FXSR"),
293 		STRDESC("FXSR"), STRDESC("fxsr"),
294 	},
295 	{						/* 0x00000800 */
296 		AV_386_SSE, STRDESC("AV_386_SSE"),
297 		STRDESC("SSE"), STRDESC("sse"),
298 	},
299 	{						/* 0x00001000 */
300 		AV_386_SSE2, STRDESC("AV_386_SSE2"),
301 		STRDESC("SSE2"), STRDESC("sse2"),
302 	},
303 	/* 0x02000 withdrawn - do not assign */
304 	{						/* 0x00004000 */
305 		AV_386_SSE3, STRDESC("AV_386_SSE3"),
306 		STRDESC("SSE3"), STRDESC("sse3"),
307 	},
308 	/* 0x08000 withdrawn - do not assign */
309 	{						/* 0x00010000 */
310 		AV_386_CX16, STRDESC("AV_386_CX16"),
311 		STRDESC("CX16"), STRDESC("cx16"),
312 	},
313 	{						/* 0x00020000 */
314 		AV_386_AHF, STRDESC("AV_386_AHF"),
315 		STRDESC("AHF"), STRDESC("ahf"),
316 	},
317 	{						/* 0x00040000 */
318 		AV_386_TSCP, STRDESC("AV_386_TSCP"),
319 		STRDESC("TSCP"), STRDESC("tscp"),
320 	},
321 	{						/* 0x00080000 */
322 		AV_386_AMD_SSE4A, STRDESC("AV_386_AMD_SSE4A"),
323 		STRDESC("AMD_SSE4A"), STRDESC("amd_sse4a"),
324 	},
325 	{						/* 0x00100000 */
326 		AV_386_POPCNT, STRDESC("AV_386_POPCNT"),
327 		STRDESC("POPCNT"), STRDESC("popcnt"),
328 	},
329 	{						/* 0x00200000 */
330 		AV_386_AMD_LZCNT, STRDESC("AV_386_AMD_LZCNT"),
331 		STRDESC("AMD_LZCNT"), STRDESC("amd_lzcnt"),
332 	},
333 	{						/* 0x00400000 */
334 		AV_386_SSSE3, STRDESC("AV_386_SSSE3"),
335 		STRDESC("SSSE3"), STRDESC("ssse3"),
336 	},
337 	{						/* 0x00800000 */
338 		AV_386_SSE4_1, STRDESC("AV_386_SSE4_1"),
339 		STRDESC("SSE4.1"), STRDESC("sse4.1"),
340 	},
341 	{						/* 0x01000000 */
342 		AV_386_SSE4_2, STRDESC("AV_386_SSE4_2"),
343 		STRDESC("SSE4.2"), STRDESC("sse4.2"),
344 	},
345 	{						/* 0x02000000 */
346 		AV_386_MOVBE, STRDESC("AV_386_MOVBE"),
347 		STRDESC("MOVBE"), STRDESC("movbe"),
348 	},
349 	{						/* 0x04000000 */
350 		AV_386_AES, STRDESC("AV_386_AES"),
351 		STRDESC("AES"), STRDESC("aes"),
352 	},
353 	{						/* 0x08000000 */
354 		AV_386_PCLMULQDQ, STRDESC("AV_386_PCLMULQDQ"),
355 		STRDESC("PCLMULQDQ"), STRDESC("pclmulqdq"),
356 	},
357 	{						/* 0x10000000 */
358 		AV_386_XSAVE, STRDESC("AV_386_XSAVE"),
359 		STRDESC("XSAVE"), STRDESC("xsave"),
360 	},
361 	{						/* 0x20000000 */
362 		AV_386_AVX, STRDESC("AV_386_AVX"),
363 		STRDESC("AVX"), STRDESC("avx"),
364 	},
365 	{						/* 0x40000000 */
366 		AV_386_VMX, STRDESC("AV_386_VMX"),
367 		STRDESC("VMX"), STRDESC("vmx"),
368 	},
369 	{						/* 0x80000000 */
370 		AV_386_AMD_SVM, STRDESC("AV_386_AMD_SVM"),
371 		STRDESC("AMD_SVM"), STRDESC("amd_svm"),
372 	}
373 };
374 
375 static const elfcap_desc_t hw2_386[ELFCAP_NUM_HW2_386] = {
376 	{						/* 0x00000001 */
377 		AV_386_2_F16C, STRDESC("AV_386_2_F16C"),
378 		STRDESC("F16C"), STRDESC("f16c"),
379 	},
380 	{						/* 0x00000002 */
381 		AV_386_2_RDRAND, STRDESC("AV_386_2_RDRAND"),
382 		STRDESC("RDRAND"), STRDESC("rdrand"),
383 	},
384 	{						/* 0x00000004 */
385 		AV_386_2_BMI1, STRDESC("AV_386_2_BMI1"),
386 		STRDESC("BMI1"), STRDESC("bmi1"),
387 	},
388 	{						/* 0x00000008 */
389 		AV_386_2_BMI2, STRDESC("AV_386_2_BMI2"),
390 		STRDESC("BMI2"), STRDESC("bmi2"),
391 	},
392 	{						/* 0x00000010 */
393 		AV_386_2_FMA, STRDESC("AV_386_2_FMA"),
394 		STRDESC("FMA"), STRDESC("fma"),
395 	},
396 	{						/* 0x00000020 */
397 		AV_386_2_AVX2, STRDESC("AV_386_2_AVX2"),
398 		STRDESC("AVX2"), STRDESC("avx2"),
399 	},
400 	{						/* 0x00000040 */
401 		AV_386_2_ADX, STRDESC("AV_386_2_ADX"),
402 		STRDESC("ADX"), STRDESC("adx"),
403 	},
404 	{						/* 0x00000080 */
405 		AV_386_2_RDSEED, STRDESC("AV_386_2_RDSEED"),
406 		STRDESC("RDSEED"), STRDESC("rdseed"),
407 	},
408 	{						/* 0x00000100 */
409 		AV_386_2_AVX512F, STRDESC("AV_386_2_AVX512F"),
410 		STRDESC("AVX512F"), STRDESC("avx512f"),
411 	},
412 	{						/* 0x00000200 */
413 		AV_386_2_AVX512DQ, STRDESC("AV_386_2_AVX512DQ"),
414 		STRDESC("AVX512DQ"), STRDESC("avx512dq"),
415 	},
416 	{						/* 0x00000400 */
417 		AV_386_2_AVX512IFMA, STRDESC("AV_386_2_AVX512IFMA"),
418 		STRDESC("AVX512IFMA"), STRDESC("avx512ifma"),
419 	},
420 	{						/* 0x00000800 */
421 		AV_386_2_AVX512PF, STRDESC("AV_386_2_AVX512PF"),
422 		STRDESC("AVX512PF"), STRDESC("avx512pf"),
423 	},
424 	{						/* 0x00001000 */
425 		AV_386_2_AVX512ER, STRDESC("AV_386_2_AVX512ER"),
426 		STRDESC("AVX512ER"), STRDESC("avx512er"),
427 	},
428 	{						/* 0x00002000 */
429 		AV_386_2_AVX512CD, STRDESC("AV_386_2_AVX512CD"),
430 		STRDESC("AVX512CD"), STRDESC("avx512cd"),
431 	},
432 	{						/* 0x00004000 */
433 		AV_386_2_AVX512BW, STRDESC("AV_386_2_AVX512BW"),
434 		STRDESC("AVX512BW"), STRDESC("avx512bw"),
435 	},
436 	{						/* 0x00008000 */
437 		AV_386_2_AVX512VL, STRDESC("AV_386_2_AVX512VL"),
438 		STRDESC("AVX512VL"), STRDESC("avx512vl"),
439 	},
440 	{						/* 0x00010000 */
441 		AV_386_2_AVX512VBMI, STRDESC("AV_386_2_AVX512VBMI"),
442 		STRDESC("AVX512VBMI"), STRDESC("avx512vbmi"),
443 	},
444 	{						/* 0x00020000 */
445 		AV_386_2_AVX512VPOPCDQ, STRDESC("AV_386_2_AVX512_VPOPCDQ"),
446 		STRDESC("AVX512_VPOPCDQ"), STRDESC("avx512_vpopcntdq"),
447 	},
448 	{						/* 0x00040000 */
449 		AV_386_2_AVX512_4NNIW, STRDESC("AV_386_2_AVX512_4NNIW"),
450 		STRDESC("AVX512_4NNIW"), STRDESC("avx512_4nniw"),
451 	},
452 	{						/* 0x00080000 */
453 		AV_386_2_AVX512_4FMAPS, STRDESC("AV_386_2_AVX512_4FMAPS"),
454 		STRDESC("AVX512_4FMAPS"), STRDESC("avx512_4fmaps"),
455 	},
456 	{						/* 0x00100000 */
457 		AV_386_2_SHA, STRDESC("AV_386_2_SHA"),
458 		STRDESC("SHA"), STRDESC("sha"),
459 	},
460 	{						/* 0x00200000 */
461 		AV_386_2_FSGSBASE, STRDESC("AV_386_2_FSGSBASE"),
462 		STRDESC("FSGSBASE"), STRDESC("fsgsbase")
463 	},
464 	{						/* 0x00400000 */
465 		AV_386_2_CLFLUSHOPT, STRDESC("AV_386_2_CLFLUSHOPT"),
466 		STRDESC("CLFLUSHOPT"), STRDESC("clflushopt")
467 	},
468 	{						/* 0x00800000 */
469 		AV_386_2_CLWB, STRDESC("AV_386_2_CLWB"),
470 		STRDESC("CLWB"), STRDESC("clwb")
471 	},
472 	{						/* 0x01000000 */
473 		AV_386_2_MONITORX, STRDESC("AV_386_2_MONITORX"),
474 		STRDESC("MONITORX"), STRDESC("monitorx")
475 	},
476 	{						/* 0x02000000 */
477 		AV_386_2_CLZERO, STRDESC("AV_386_2_CLZERO"),
478 		STRDESC("CLZERO"), STRDESC("clzero")
479 	},
480 	{						/* 0x04000000 */
481 		AV_386_2_AVX512_VNNI, STRDESC("AV_386_2_AVX512_VNNI"),
482 		STRDESC("AVX512_VNNI"), STRDESC("avx512_vnni")
483 	},
484 	{						/* 0x08000000 */
485 		AV_386_2_VPCLMULQDQ, STRDESC("AV_386_2_VPCLMULQDQ"),
486 		STRDESC("VPCLMULQDQ"), STRDESC("vpclmulqdq")
487 	},
488 	{						/* 0x10000000 */
489 		AV_386_2_VAES, STRDESC("AV_386_2_VAES"),
490 		STRDESC("VAES"), STRDESC("vaes")
491 	}
492 };
493 
494 /*
495  * Concatenate a token to the string buffer.  This can be a capabilities token
496  * or a separator token.
497  */
498 static elfcap_err_t
token(char ** ostr,size_t * olen,const elfcap_str_t * nstr)499 token(char **ostr, size_t *olen, const elfcap_str_t *nstr)
500 {
501 	if (*olen < nstr->s_len)
502 		return (ELFCAP_ERR_BUFOVFL);
503 
504 	(void) strcat(*ostr, nstr->s_str);
505 	*ostr += nstr->s_len;
506 	*olen -= nstr->s_len;
507 
508 	return (ELFCAP_ERR_NONE);
509 }
510 
511 static elfcap_err_t
get_str_desc(elfcap_style_t style,const elfcap_desc_t * cdp,const elfcap_str_t ** ret_str)512 get_str_desc(elfcap_style_t style, const elfcap_desc_t *cdp,
513     const elfcap_str_t **ret_str)
514 {
515 	switch (ELFCAP_STYLE_MASK(style)) {
516 	case ELFCAP_STYLE_FULL:
517 		*ret_str = &cdp->c_full;
518 		break;
519 	case ELFCAP_STYLE_UC:
520 		*ret_str = &cdp->c_uc;
521 		break;
522 	case ELFCAP_STYLE_LC:
523 		*ret_str = &cdp->c_lc;
524 		break;
525 	default:
526 		return (ELFCAP_ERR_INVSTYLE);
527 	}
528 
529 	return (ELFCAP_ERR_NONE);
530 }
531 
532 
533 /*
534  * Expand a capabilities value into the strings defined in the associated
535  * capabilities descriptor.
536  */
537 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)538 expand(elfcap_style_t style, elfcap_mask_t val, const elfcap_desc_t *cdp,
539     uint_t cnum, char *str, size_t slen, elfcap_fmt_t fmt)
540 {
541 	uint_t			cnt;
542 	int			follow = 0, err;
543 	const elfcap_str_t	*nstr;
544 
545 	if (val == 0)
546 		return (ELFCAP_ERR_NONE);
547 
548 	for (cnt = cnum; cnt > 0; cnt--) {
549 		uint_t mask = cdp[cnt - 1].c_val;
550 
551 		if ((val & mask) != 0) {
552 			if (follow++ && ((err = token(&str, &slen,
553 			    &format[fmt])) != ELFCAP_ERR_NONE))
554 				return (err);
555 
556 			err = get_str_desc(style, &cdp[cnt - 1], &nstr);
557 			if (err != ELFCAP_ERR_NONE)
558 				return (err);
559 			if ((err = token(&str, &slen, nstr)) != ELFCAP_ERR_NONE)
560 				return (err);
561 
562 			val = val & ~mask;
563 		}
564 	}
565 
566 	/*
567 	 * If there are any unknown bits remaining display the numeric value.
568 	 */
569 	if (val) {
570 		if (follow && ((err = token(&str, &slen, &format[fmt])) !=
571 		    ELFCAP_ERR_NONE))
572 			return (err);
573 
574 		(void) snprintf(str, slen, "0x%x", val);
575 	}
576 	return (ELFCAP_ERR_NONE);
577 }
578 
579 /*
580  * Expand a CA_SUNW_HW_1 value.
581  */
582 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)583 elfcap_hw1_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
584     size_t len, elfcap_fmt_t fmt, ushort_t mach)
585 {
586 	/*
587 	 * Initialize the string buffer, and validate the format request.
588 	 */
589 	*str = '\0';
590 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
591 		return (ELFCAP_ERR_INVFMT);
592 
593 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
594 		return (expand(style, val, &hw1_386[0], ELFCAP_NUM_HW1_386,
595 		    str, len, fmt));
596 
597 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
598 	    (mach == EM_SPARCV9))
599 		return (expand(style, val, hw1_sparc, ELFCAP_NUM_HW1_SPARC,
600 		    str, len, fmt));
601 
602 	return (ELFCAP_ERR_UNKMACH);
603 }
604 
605 /*
606  * Expand a CA_SUNW_HW_2 value.
607  */
608 elfcap_err_t
elfcap_hw2_to_str(elfcap_style_t style,elfcap_mask_t val,char * str,size_t len,elfcap_fmt_t fmt,ushort_t mach)609 elfcap_hw2_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
610     size_t len, elfcap_fmt_t fmt, ushort_t mach)
611 {
612 	/*
613 	 * Initialize the string buffer, and validate the format request.
614 	 */
615 	*str = '\0';
616 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
617 		return (ELFCAP_ERR_INVFMT);
618 
619 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
620 		return (expand(style, val, &hw2_386[0], ELFCAP_NUM_HW2_386,
621 		    str, len, fmt));
622 
623 	return (expand(style, val, NULL, 0, str, len, fmt));
624 }
625 
626 /*
627  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
628  * common across all platforms.  The use of "mach" is therefore redundant, but
629  * is retained for compatibility with the interface of elfcap_hw1_to_str(), and
630  * possible future expansion.
631  */
632 elfcap_err_t
633 /* 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)634 elfcap_sf1_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
635     size_t len, elfcap_fmt_t fmt, ushort_t mach)
636 {
637 	/*
638 	 * Initialize the string buffer, and validate the format request.
639 	 */
640 	*str = '\0';
641 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
642 		return (ELFCAP_ERR_INVFMT);
643 
644 	return (expand(style, val, &sf1[0], ELFCAP_NUM_SF1, str, len, fmt));
645 }
646 
647 /*
648  * Given a capability tag type and value, map it to a string representation.
649  */
650 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)651 elfcap_tag_to_str(elfcap_style_t style, uint64_t tag, elfcap_mask_t val,
652     char *str, size_t len, elfcap_fmt_t fmt, ushort_t mach)
653 {
654 	switch (tag) {
655 	case CA_SUNW_HW_1:
656 		return (elfcap_hw1_to_str(style, val, str, len, fmt, mach));
657 
658 	case CA_SUNW_SF_1:
659 		return (elfcap_sf1_to_str(style, val, str, len, fmt, mach));
660 
661 	case CA_SUNW_HW_2:
662 		return (elfcap_hw2_to_str(style, val, str, len, fmt, mach));
663 
664 	}
665 
666 	return (ELFCAP_ERR_UNKTAG);
667 }
668 
669 /*
670  * Determine a capabilities value from a capabilities string.
671  */
672 static elfcap_mask_t
value(elfcap_style_t style,const char * str,const elfcap_desc_t * cdp,uint_t cnum)673 value(elfcap_style_t style, const char *str, const elfcap_desc_t *cdp,
674     uint_t cnum)
675 {
676 	const elfcap_str_t	*nstr;
677 	uint_t	num;
678 	int	err;
679 
680 	for (num = 0; num < cnum; num++) {
681 		/*
682 		 * Skip "reserved" bits. These are unassigned bits in the
683 		 * middle of the assigned range.
684 		 */
685 		if (cdp[num].c_val == 0)
686 			continue;
687 
688 		if ((err = get_str_desc(style, &cdp[num], &nstr)) != 0)
689 			return (err);
690 		if (style & ELFCAP_STYLE_F_ICMP) {
691 			if (strcasecmp(str, nstr->s_str) == 0)
692 				return (cdp[num].c_val);
693 		} else {
694 			if (strcmp(str, nstr->s_str) == 0)
695 				return (cdp[num].c_val);
696 		}
697 	}
698 
699 	return (0);
700 }
701 
702 elfcap_mask_t
elfcap_sf1_from_str(elfcap_style_t style,const char * str,ushort_t mach)703 elfcap_sf1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
704 {
705 	return (value(style, str, &sf1[0], ELFCAP_NUM_SF1));
706 }
707 
708 elfcap_mask_t
elfcap_hw1_from_str(elfcap_style_t style,const char * str,ushort_t mach)709 elfcap_hw1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
710 {
711 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
712 		return (value(style, str, &hw1_386[0], ELFCAP_NUM_HW1_386));
713 
714 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
715 	    (mach == EM_SPARCV9))
716 		return (value(style, str, hw1_sparc, ELFCAP_NUM_HW1_SPARC));
717 
718 	return (0);
719 }
720 elfcap_mask_t
elfcap_hw2_from_str(elfcap_style_t style,const char * str,ushort_t mach)721 elfcap_hw2_from_str(elfcap_style_t style, const char *str, ushort_t mach)
722 {
723 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
724 		return (value(style, str, &hw2_386[0], ELFCAP_NUM_HW2_386));
725 
726 	return (0);
727 }
728 
729 /*
730  * Given a capability tag type and value, return the capabilities values
731  * contained in the string.
732  */
733 elfcap_mask_t
elfcap_tag_from_str(elfcap_style_t style,uint64_t tag,const char * str,ushort_t mach)734 elfcap_tag_from_str(elfcap_style_t style, uint64_t tag, const char *str,
735     ushort_t mach)
736 {
737 	switch (tag) {
738 	case CA_SUNW_HW_1:
739 		return (elfcap_hw1_from_str(style, str, mach));
740 
741 	case CA_SUNW_SF_1:
742 		return (elfcap_sf1_from_str(style, str, mach));
743 
744 	case CA_SUNW_HW_2:
745 		return (elfcap_hw2_from_str(style, str, mach));
746 	}
747 
748 	return (0);
749 }
750 
751 /*
752  * These functions allow the caller to get direct access to the
753  * cap descriptors.
754  */
755 const elfcap_desc_t *
elfcap_getdesc_hw1_sparc(void)756 elfcap_getdesc_hw1_sparc(void)
757 {
758 	return (hw1_sparc);
759 }
760 
761 const elfcap_desc_t *
elfcap_getdesc_hw1_386(void)762 elfcap_getdesc_hw1_386(void)
763 {
764 	return (hw1_386);
765 }
766 
767 const elfcap_desc_t *
elfcap_getdesc_sf1(void)768 elfcap_getdesc_sf1(void)
769 {
770 	return (sf1);
771 }
772