xref: /illumos-gate/usr/src/common/elfcap/elfcap.h (revision 56726c7e321b6e5ecb2f10215f5386016547e68c)
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  * Copyright 2022 Oxide Computer Company
26  */
27 
28 #ifndef _ELFCAP_DOT_H
29 #define	_ELFCAP_DOT_H
30 
31 #include <sys/types.h>
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 /*
38  * Type used to represent capability bitmasks. This 32-bit type cannot be
39  * widened without breaking the ability to use them in ELFCLASS32 objects.
40  */
41 typedef uint32_t elfcap_mask_t;
42 
43 /*
44  * The elfcap code handles mappings to and from several string styles.
45  * The caller uses elfcap_style_t to specify the style to use.
46  *
47  * The bottom 16 bits are used to represent styles, and the upper 16
48  * bits are used for flags to modify default behavior.
49  */
50 #define	ELFCAP_STYLE_MASK(_style) (_style & 0xff)
51 
52 typedef enum {
53 	ELFCAP_STYLE_FULL =	1,	/* Full formal name (e.g. AV_386_SSE) */
54 	ELFCAP_STYLE_UC =	2,	/* Informal upper case (e.g. SSE) */
55 	ELFCAP_STYLE_LC =	3,	/* Informal lower case (e.g. sse) */
56 
57 	ELFCAP_STYLE_F_ICMP =	0x0100	 /* Use case insensitive strcmp */
58 } elfcap_style_t;
59 
60 /*
61  * String descriptor: Contains the string and strlen(string). elfcap can
62  * be used in contexts (ld.so.1) where we do not want to make calls to
63  * string processing functions, so the length is calculated at compile time.
64  */
65 typedef	struct {
66 	const char	*s_str;
67 	size_t		s_len;
68 } elfcap_str_t;
69 
70 /*
71  * Capabilities descriptor: This maps the integer bit value
72  * (c_val) to/from the various strings that represent it.
73  *
74  * c_val is normally expected to be a non-zero power of 2
75  * value (i.e. a single set bit). The value 0 is special, and
76  * used to represent a "reserved" placeholder in an array of
77  * capabilities. These reserved values have NULL string pointers,
78  * and are intended to be ignored by the processing code.
79  */
80 typedef	struct {
81 	elfcap_mask_t	c_val;		/* Bit value */
82 	elfcap_str_t	c_full;		/* ELFCAP_STYLE_FULL */
83 	elfcap_str_t	c_uc;		/* ELFCAP_STYLE_UC */
84 	elfcap_str_t	c_lc;		/* ELFCAP_STYLE_LC */
85 } elfcap_desc_t;
86 
87 /*
88  * Valid format values: The various formats in which a generated
89  * string representing bitmap values can be displayed.
90  *
91  * This must be kept in sync with the format[] array in elfcap.c.
92  */
93 typedef enum {
94 	ELFCAP_FMT_SNGSPACE =		0,
95 	ELFCAP_FMT_DBLSPACE =		1,
96 	ELFCAP_FMT_PIPSPACE =		2
97 } elfcap_fmt_t;
98 
99 /*
100  * Error codes:
101  */
102 typedef enum {
103 	ELFCAP_ERR_NONE =		0,	/* no error */
104 	ELFCAP_ERR_BUFOVFL =		1,	/* buffer overfow */
105 	ELFCAP_ERR_INVFMT =		2,	/* invalid format */
106 	ELFCAP_ERR_UNKTAG =		3,	/* unknown capabilities tag */
107 	ELFCAP_ERR_UNKMACH =		4,	/* unknown machine type */
108 	ELFCAP_ERR_INVSTYLE =		5	/* unknown style */
109 } elfcap_err_t;
110 
111 
112 /*
113  * # of each type of capability known to the system. These values
114  * must be kept in sync with the arrays found in elfcap.c.
115  */
116 #define	ELFCAP_NUM_SF1			3
117 #define	ELFCAP_NUM_HW1_SPARC		30
118 #define	ELFCAP_NUM_HW1_386		32
119 #define	ELFCAP_NUM_HW2_386		32
120 #define	ELFCAP_NUM_HW3_386		2
121 
122 /*
123  * String buffer lengths that should be sufficient for elfcap values today. This
124  * is used because this is compiled into programs. Do not assume it won't change
125  * or make it part of an ABI. This uses the worst case values as calculated by
126  * the elfcap_chk program that is run in sgs and checked as part of the build.
127  */
128 #define	ELFCAP_SF1_BUFSIZE	73
129 #define	ELFCAP_HW1_BUFSIZE	528
130 #define	ELFCAP_HW2_BUFSIZE	632
131 #define	ELFCAP_HW3_BUFSIZE	66
132 
133 /*
134  * Given a capability section tag and value, call the proper underlying
135  * "to str" function to generate the string description.
136  */
137 extern elfcap_err_t elfcap_tag_to_str(elfcap_style_t, uint64_t,
138     elfcap_mask_t, char *, size_t, elfcap_fmt_t, ushort_t);
139 
140 /*
141  * The functions that convert from a specific capability value to
142  * a string representation all use the same common prototype.
143  */
144 typedef elfcap_err_t elfcap_to_str_func_t(elfcap_style_t, elfcap_mask_t, char *,
145     size_t, elfcap_fmt_t, ushort_t);
146 
147 extern elfcap_to_str_func_t elfcap_hw1_to_str;
148 extern elfcap_to_str_func_t elfcap_hw2_to_str;
149 extern elfcap_to_str_func_t elfcap_hw3_to_str;
150 extern elfcap_to_str_func_t elfcap_sf1_to_str;
151 
152 /*
153  * The reverse mapping: Given a string representation, turn it back into
154  * integer form.
155  */
156 typedef elfcap_mask_t elfcap_from_str_func_t(elfcap_style_t,
157     const char *, ushort_t mach);
158 
159 /*
160  * Given a capability section tag and string, call the proper underlying
161  * "from str" function to generate the numeric value.
162  */
163 extern elfcap_mask_t elfcap_tag_from_str(elfcap_style_t, uint64_t,
164     const char *, ushort_t);
165 
166 extern elfcap_from_str_func_t elfcap_hw1_from_str;
167 extern elfcap_from_str_func_t elfcap_hw2_from_str;
168 extern elfcap_from_str_func_t elfcap_sf1_from_str;
169 extern elfcap_from_str_func_t elfcap_hw3_from_str;
170 
171 /*
172  * These functions give access to the individual descriptor arrays.
173  * The caller is allowed to copy and use the string pointers contained
174  * in the descriptors, but must not alter them. Functions are used instead
175  * of making the arrays directly visible to preclude copy relocations in
176  * non-pic code.
177  */
178 extern const elfcap_str_t *elfcap_getdesc_formats(void);
179 extern const elfcap_desc_t *elfcap_getdesc_hw1_sparc(void);
180 extern const elfcap_desc_t *elfcap_getdesc_hw1_386(void);
181 extern const elfcap_desc_t *elfcap_getdesc_hw2_386(void);
182 extern const elfcap_desc_t *elfcap_getdesc_hw3_386(void);
183 extern const elfcap_desc_t *elfcap_getdesc_sf1(void);
184 
185 #ifdef	__cplusplus
186 }
187 #endif
188 
189 #endif /* _ELFCAP_DOT_H */
190