xref: /illumos-gate/usr/src/common/ctf/ctf_impl.h (revision 88a08813800ed7ba7c927986421cee437f7f2233)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /*
28  * Copyright 2020 Joyent, Inc.
29  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
30  */
31 
32 #ifndef	_CTF_IMPL_H
33 #define	_CTF_IMPL_H
34 
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/sysmacros.h>
38 #include <sys/ctf_api.h>
39 
40 #ifdef _KERNEL
41 
42 #include <sys/systm.h>
43 #include <sys/cmn_err.h>
44 #include <sys/varargs.h>
45 #include <sys/ddi.h>
46 #include <sys/sunddi.h>
47 
48 #define	isspace(c) \
49 	((c) == ' ' || (c) == '\t' || (c) == '\n' || \
50 	(c) == '\r' || (c) == '\f' || (c) == '\v')
51 
52 #define	MAP_FAILED	((void *)-1)
53 
54 #else	/* _KERNEL */
55 
56 #include <strings.h>
57 #include <stdlib.h>
58 #include <stdarg.h>
59 #include <stdio.h>
60 #include <limits.h>
61 #include <ctype.h>
62 #include <stddef.h>
63 
64 #endif	/* _KERNEL */
65 
66 #ifdef	__cplusplus
67 extern "C" {
68 #endif
69 
70 typedef struct ctf_helem {
71 	uint_t h_name;		/* reference to name in string table */
72 	ushort_t h_type;	/* corresponding type ID number */
73 	ushort_t h_next;	/* index of next element in hash chain */
74 } ctf_helem_t;
75 
76 typedef struct ctf_hash {
77 	ushort_t *h_buckets;	/* hash bucket array (chain indices) */
78 	ctf_helem_t *h_chains;	/* hash chains buffer */
79 	ushort_t h_nbuckets;	/* number of elements in bucket array */
80 	ushort_t h_nelems;	/* number of elements in hash table */
81 	uint_t h_free;		/* index of next free hash element */
82 } ctf_hash_t;
83 
84 struct ctf_idhash_iter {
85 	int cii_id;	/* Current iteration id */
86 };
87 
88 typedef struct ctf_strs {
89 	const char *cts_strs;	/* base address of string table */
90 	size_t cts_len;		/* size of string table in bytes */
91 } ctf_strs_t;
92 
93 typedef struct ctf_dmodel {
94 	const char *ctd_name;	/* data model name */
95 	int ctd_code;		/* data model code */
96 	size_t ctd_pointer;	/* size of void * in bytes */
97 	size_t ctd_char;	/* size of char in bytes */
98 	size_t ctd_short;	/* size of short in bytes */
99 	size_t ctd_int;		/* size of int in bytes */
100 	size_t ctd_long;	/* size of long in bytes */
101 } ctf_dmodel_t;
102 
103 typedef struct ctf_lookup {
104 	const char *ctl_prefix;	/* string prefix for this lookup */
105 	size_t ctl_len;		/* length of prefix string in bytes */
106 	ctf_hash_t *ctl_hash;	/* pointer to hash table for lookup */
107 } ctf_lookup_t;
108 
109 typedef struct ctf_fileops {
110 	ushort_t (*ctfo_get_kind)(ushort_t);
111 	ushort_t (*ctfo_get_root)(ushort_t);
112 	ushort_t (*ctfo_get_vlen)(ushort_t);
113 } ctf_fileops_t;
114 
115 typedef struct ctf_list {
116 	struct ctf_list *l_prev; /* previous pointer or tail pointer */
117 	struct ctf_list *l_next; /* next pointer or head pointer */
118 } ctf_list_t;
119 
120 typedef enum {
121 	CTF_PREC_BASE,
122 	CTF_PREC_POINTER,
123 	CTF_PREC_ARRAY,
124 	CTF_PREC_FUNCTION,
125 	CTF_PREC_MAX
126 } ctf_decl_prec_t;
127 
128 typedef struct ctf_decl_node {
129 	ctf_list_t cd_list;			/* linked list pointers */
130 	ctf_id_t cd_type;			/* type identifier */
131 	uint_t cd_kind;				/* type kind */
132 	uint_t cd_n;				/* type dimension if array */
133 } ctf_decl_node_t;
134 
135 typedef struct ctf_decl {
136 	ctf_list_t cd_nodes[CTF_PREC_MAX];	/* declaration node stacks */
137 	int cd_order[CTF_PREC_MAX];		/* storage order of decls */
138 	ctf_decl_prec_t cd_qualp;		/* qualifier precision */
139 	ctf_decl_prec_t cd_ordp;		/* ordered precision */
140 	char *cd_buf;				/* buffer for output */
141 	char *cd_ptr;				/* buffer location */
142 	char *cd_end;				/* buffer limit */
143 	size_t cd_len;				/* buffer space required */
144 	int cd_err;				/* saved error value */
145 } ctf_decl_t;
146 
147 typedef struct ctf_dmdef {
148 	ctf_list_t dmd_list;	/* list forward/back pointers */
149 	char *dmd_name;		/* name of this member */
150 	ctf_id_t dmd_type;	/* type of this member (for sou) */
151 	ulong_t dmd_offset;	/* offset of this member in bits (for sou) */
152 	int dmd_value;		/* value of this member (for enum) */
153 } ctf_dmdef_t;
154 
155 typedef struct ctf_dtdef {
156 	ctf_list_t dtd_list;	/* list forward/back pointers */
157 	struct ctf_dtdef *dtd_hash; /* hash chain pointer for ctf_dthash */
158 	char *dtd_name;		/* name associated with definition (if any) */
159 	ctf_id_t dtd_type;	/* type identifier for this definition */
160 	ctf_type_t dtd_data;	/* type node (see <sys/ctf.h>) */
161 	int dtd_ref;		/* recfount for dyanmic types */
162 	union {
163 		ctf_list_t dtu_members;	/* struct, union, or enum */
164 		ctf_arinfo_t dtu_arr;	/* array */
165 		ctf_encoding_t dtu_enc;	/* integer or float */
166 		ctf_id_t *dtu_argv;	/* function */
167 	} dtd_u;
168 } ctf_dtdef_t;
169 
170 typedef struct ctf_dsdef {
171 	ctf_list_t dsd_list;	/* list forward/back pointers */
172 	ulong_t dsd_symidx;	/* symbol id */
173 	ctf_id_t dsd_tid;	/* type for obj, 0 if function */
174 	uint_t dsd_nargs;
175 	ctf_id_t *dsd_argc;	/* function argv */
176 } ctf_dsdef_t;
177 
178 typedef struct ctf_dldef {
179 	ctf_list_t dld_list;	/* list forward/back pointers */
180 	char *dld_name;		/* name of the label */
181 	ctf_id_t dld_type;	/* type ID associated with the label */
182 } ctf_dldef_t;
183 
184 typedef struct ctf_bundle {
185 	ctf_file_t *ctb_file;	/* CTF container handle */
186 	ctf_id_t ctb_type;	/* CTF type identifier */
187 	ctf_dtdef_t *ctb_dtd;	/* CTF dynamic type definition (if any) */
188 } ctf_bundle_t;
189 
190 /*
191  * The ctf_file is the structure used to represent a CTF container to library
192  * clients, who see it only as an opaque pointer.  Modifications can therefore
193  * be made freely to this structure without regard to client versioning.  The
194  * ctf_file_t typedef appears in <sys/ctf_api.h> and declares a forward tag.
195  *
196  * NOTE: ctf_update() requires that everything inside of ctf_file either be an
197  * immediate value, a pointer to dynamically allocated data *outside* of the
198  * ctf_file itself, or a pointer to statically allocated data.  If you add a
199  * pointer to ctf_file that points to something within the ctf_file itself,
200  * you must make corresponding changes to ctf_update().
201  */
202 struct ctf_file {
203 	const ctf_fileops_t *ctf_fileops; /* version-specific file operations */
204 	ctf_sect_t ctf_data;	/* CTF data from object file */
205 	ctf_sect_t ctf_symtab;	/* symbol table from object file */
206 	ctf_sect_t ctf_strtab;	/* string table from object file */
207 	ctf_hash_t ctf_structs;	/* hash table of struct types */
208 	ctf_hash_t ctf_unions;	/* hash table of union types */
209 	ctf_hash_t ctf_enums;	/* hash table of enum types */
210 	ctf_hash_t ctf_names;	/* hash table of remaining type names */
211 	ctf_lookup_t ctf_lookups[5];	/* pointers to hashes for name lookup */
212 	ctf_strs_t ctf_str[2];	/* array of string table base and bounds */
213 	const uchar_t *ctf_base; /* base of CTF header + uncompressed buffer */
214 	const uchar_t *ctf_buf;	/* uncompressed CTF data buffer */
215 	size_t ctf_size;	/* size of CTF header + uncompressed data */
216 	uint_t *ctf_sxlate;	/* translation table for symtab entries */
217 	ulong_t ctf_nsyms;	/* number of entries in symtab xlate table */
218 	uint_t *ctf_txlate;	/* translation table for type IDs */
219 	ushort_t *ctf_ptrtab;	/* translation table for pointer-to lookups */
220 	ulong_t ctf_typemax;	/* maximum valid type ID number */
221 	const ctf_dmodel_t *ctf_dmodel;	/* data model pointer (see above) */
222 	struct ctf_file *ctf_parent;	/* parent CTF container (if any) */
223 	const char *ctf_parlabel;	/* label in parent container (if any) */
224 	const char *ctf_parname;	/* basename of parent (if any) */
225 	uint_t ctf_refcnt;	/* reference count (for parent links) */
226 	uint_t ctf_flags;	/* libctf flags (see below) */
227 	int ctf_errno;		/* error code for most recent error */
228 	int ctf_version;	/* CTF data version */
229 	ctf_dtdef_t **ctf_dthash; /* hash of dynamic type definitions */
230 	ulong_t ctf_dthashlen;	/* size of dynamic type hash bucket array */
231 	ctf_list_t ctf_dtdefs;	/* list of dynamic type definitions */
232 	size_t ctf_dtstrlen;	/* total length of dynamic type strings */
233 	ulong_t ctf_dtnextid;	/* next dynamic type id to assign */
234 	ulong_t ctf_dtoldid;	/* oldest id that has been committed */
235 	void *ctf_specific;	/* data for ctf_get/setspecific */
236 	ctf_list_t ctf_dsdefs;	/* list of dynamic obj/func definitions */
237 	ctf_list_t ctf_dldefs;	/* list of dynamic labels */
238 	uint_t ctf_hflags;	/* original flags on the header */
239 };
240 
241 #define	LCTF_INDEX_TO_TYPEPTR(fp, i) \
242 	((ctf_type_t *)((uintptr_t)(fp)->ctf_buf + (fp)->ctf_txlate[(i)]))
243 
244 #define	LCTF_INFO_KIND(fp, info)	((fp)->ctf_fileops->ctfo_get_kind(info))
245 #define	LCTF_INFO_ROOT(fp, info)	((fp)->ctf_fileops->ctfo_get_root(info))
246 #define	LCTF_INFO_VLEN(fp, info)	((fp)->ctf_fileops->ctfo_get_vlen(info))
247 
248 #define	LCTF_MMAP	0x0001	/* libctf should munmap buffers on close */
249 #define	LCTF_CHILD	0x0002	/* CTF container is a child */
250 #define	LCTF_RDWR	0x0004	/* CTF container is writable */
251 #define	LCTF_DIRTY	0x0008	/* CTF container has been modified */
252 /*
253  * The storage for this CTF container was allocated via ctf_data_alloc()
254  * and libctf should free it with ctf_data_free() on close.
255  */
256 #define	LCTF_FREE	0x0010
257 
258 #define	CTF_ELF_SCN_NAME	".SUNW_ctf"
259 
260 extern ssize_t ctf_get_ctt_size(const ctf_file_t *, const ctf_type_t *,
261     ssize_t *, ssize_t *);
262 
263 extern void ctf_set_ctt_size(ctf_type_t *, ssize_t);
264 
265 extern const ctf_type_t *ctf_lookup_by_id(ctf_file_t **, ctf_id_t);
266 
267 extern ctf_file_t *ctf_fdcreate_int(int, int *, ctf_sect_t *);
268 
269 extern int ctf_hash_create(ctf_hash_t *, ulong_t);
270 extern int ctf_hash_insert(ctf_hash_t *, ctf_file_t *, ushort_t, uint_t);
271 extern int ctf_hash_define(ctf_hash_t *, ctf_file_t *, ushort_t, uint_t);
272 extern ctf_helem_t *ctf_hash_lookup(ctf_hash_t *, ctf_file_t *,
273     const char *, size_t);
274 extern uint_t ctf_hash_size(const ctf_hash_t *);
275 extern void ctf_hash_destroy(ctf_hash_t *);
276 extern void ctf_hash_dump(const char *, ctf_hash_t *, ctf_file_t *);
277 
278 #define	ctf_list_prev(elem)	((void *)(((ctf_list_t *)(elem))->l_prev))
279 #define	ctf_list_next(elem)	((void *)(((ctf_list_t *)(elem))->l_next))
280 
281 extern void ctf_list_append(ctf_list_t *, void *);
282 extern void ctf_list_prepend(ctf_list_t *, void *);
283 extern void ctf_list_insert_before(ctf_list_t *, void *, void *);
284 extern void ctf_list_delete(ctf_list_t *, void *);
285 
286 extern void ctf_dtd_insert(ctf_file_t *, ctf_dtdef_t *);
287 extern void ctf_dtd_delete(ctf_file_t *, ctf_dtdef_t *);
288 extern ctf_dtdef_t *ctf_dtd_lookup(ctf_file_t *, ctf_id_t);
289 
290 extern void ctf_dsd_delete(ctf_file_t *, ctf_dsdef_t *);
291 extern void ctf_dld_delete(ctf_file_t *, ctf_dldef_t *);
292 
293 extern void ctf_decl_init(ctf_decl_t *, char *, size_t);
294 extern void ctf_decl_fini(ctf_decl_t *);
295 extern void ctf_decl_push(ctf_decl_t *, ctf_file_t *, ctf_id_t);
296 extern void ctf_decl_sprintf(ctf_decl_t *, const char *, ...);
297 
298 extern const char *ctf_strraw(ctf_file_t *, uint_t);
299 extern const char *ctf_strptr(ctf_file_t *, uint_t);
300 
301 extern ctf_file_t *ctf_set_open_errno(int *, int);
302 extern long ctf_set_errno(ctf_file_t *, int);
303 
304 extern const void *ctf_sect_mmap(ctf_sect_t *, int);
305 extern void ctf_sect_munmap(const ctf_sect_t *);
306 
307 extern void *ctf_data_alloc(size_t);
308 extern void ctf_data_free(void *, size_t);
309 extern void ctf_data_protect(void *, size_t);
310 
311 extern void *ctf_alloc(size_t);
312 extern void ctf_free(void *, size_t);
313 
314 extern char *ctf_strdup(const char *);
315 extern void ctf_strfree(char *);
316 extern const char *ctf_strerror(int);
317 extern void ctf_dprintf(const char *, ...);
318 
319 extern void *ctf_zopen(int *);
320 
321 extern ctf_id_t ctf_add_encoded(ctf_file_t *, uint_t, const char *,
322     const ctf_encoding_t *, uint_t);
323 extern ctf_id_t ctf_add_reftype(ctf_file_t *, uint_t, const char *, ctf_id_t,
324     uint_t);
325 extern boolean_t ctf_sym_valid(uintptr_t, int, uint16_t, uint64_t,
326     uint32_t);
327 
328 extern const ctf_type_t *ctf_dyn_lookup_by_id(ctf_file_t *, ctf_id_t);
329 extern int ctf_dyn_array_info(ctf_file_t *, ctf_id_t, ctf_arinfo_t *);
330 
331 extern const char _CTF_SECTION[];	/* name of CTF ELF section */
332 extern const char _CTF_NULLSTR[];	/* empty string */
333 
334 extern int _libctf_version;		/* library client version */
335 extern int _libctf_debug;		/* debugging messages enabled */
336 
337 #ifdef	__cplusplus
338 }
339 #endif
340 
341 #endif	/* _CTF_IMPL_H */
342