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 (c) 2012, Joyent, Inc. All rights reserved. 29 */ 30 31 #ifndef _CTF_IMPL_H 32 #define _CTF_IMPL_H 33 34 #include <sys/types.h> 35 #include <sys/errno.h> 36 #include <sys/sysmacros.h> 37 #include <sys/ctf_api.h> 38 39 #ifdef _KERNEL 40 41 #include <sys/systm.h> 42 #include <sys/cmn_err.h> 43 #include <sys/varargs.h> 44 45 #define isspace(c) \ 46 ((c) == ' ' || (c) == '\t' || (c) == '\n' || \ 47 (c) == '\r' || (c) == '\f' || (c) == '\v') 48 49 #define MAP_FAILED ((void *)-1) 50 51 #else /* _KERNEL */ 52 53 #include <strings.h> 54 #include <stdlib.h> 55 #include <stdarg.h> 56 #include <stdio.h> 57 #include <limits.h> 58 #include <ctype.h> 59 60 #endif /* _KERNEL */ 61 62 #ifdef __cplusplus 63 extern "C" { 64 #endif 65 66 typedef struct ctf_helem { 67 uint_t h_name; /* reference to name in string table */ 68 uint_t h_type; /* corresponding type ID number */ 69 uint_t h_next; /* index of next element in hash chain */ 70 } ctf_helem_t; 71 72 typedef struct ctf_hash { 73 uint_t *h_buckets; /* hash bucket array (chain indices) */ 74 ctf_helem_t *h_chains; /* hash chains buffer */ 75 uint_t h_nbuckets; /* number of elements in bucket array */ 76 uint_t h_nelems; /* number of elements in hash table */ 77 uint_t h_free; /* index of next free hash element */ 78 } ctf_hash_t; 79 80 typedef struct ctf_strs { 81 const char *cts_strs; /* base address of string table */ 82 size_t cts_len; /* size of string table in bytes */ 83 } ctf_strs_t; 84 85 typedef struct ctf_dmodel { 86 const char *ctd_name; /* data model name */ 87 int ctd_code; /* data model code */ 88 size_t ctd_pointer; /* size of void * in bytes */ 89 size_t ctd_char; /* size of char in bytes */ 90 size_t ctd_short; /* size of short in bytes */ 91 size_t ctd_int; /* size of int in bytes */ 92 size_t ctd_long; /* size of long in bytes */ 93 } ctf_dmodel_t; 94 95 typedef struct ctf_lookup { 96 const char *ctl_prefix; /* string prefix for this lookup */ 97 size_t ctl_len; /* length of prefix string in bytes */ 98 ctf_hash_t *ctl_hash; /* pointer to hash table for lookup */ 99 } ctf_lookup_t; 100 101 typedef struct ctf_fileops { 102 uint_t (*ctfo_get_kind)(uint_t); 103 uint_t (*ctfo_get_root)(uint_t); 104 uint_t (*ctfo_get_vlen)(uint_t); 105 uint_t (*ctfo_get_max_vlen)(void); 106 uint_t (*ctfo_get_max_size)(void); 107 uint_t (*ctfo_get_max_type)(void); 108 uint_t (*ctfo_get_lsize_sent)(void); 109 uint_t (*ctfo_get_lstruct_thresh)(void); 110 111 uint_t (*ctfo_type_info)(uint_t, uint_t, uint_t); 112 int (*ctfo_type_isparent)(uint_t); 113 int (*ctfo_type_ischild)(uint_t); 114 uint_t (*ctfo_type_to_index)(uint_t); 115 uint_t (*ctfo_index_to_type)(uint_t, uint_t); 116 } ctf_fileops_t; 117 118 typedef struct ctf_list { 119 struct ctf_list *l_prev; /* previous pointer or tail pointer */ 120 struct ctf_list *l_next; /* next pointer or head pointer */ 121 } ctf_list_t; 122 123 typedef enum { 124 CTF_PREC_BASE, 125 CTF_PREC_POINTER, 126 CTF_PREC_ARRAY, 127 CTF_PREC_FUNCTION, 128 CTF_PREC_MAX 129 } ctf_decl_prec_t; 130 131 typedef struct ctf_decl_node { 132 ctf_list_t cd_list; /* linked list pointers */ 133 ctf_id_t cd_type; /* type identifier */ 134 uint_t cd_kind; /* type kind */ 135 uint_t cd_n; /* type dimension if array */ 136 } ctf_decl_node_t; 137 138 typedef struct ctf_decl { 139 ctf_list_t cd_nodes[CTF_PREC_MAX]; /* declaration node stacks */ 140 int cd_order[CTF_PREC_MAX]; /* storage order of decls */ 141 ctf_decl_prec_t cd_qualp; /* qualifier precision */ 142 ctf_decl_prec_t cd_ordp; /* ordered precision */ 143 char *cd_buf; /* buffer for output */ 144 char *cd_ptr; /* buffer location */ 145 char *cd_end; /* buffer limit */ 146 size_t cd_len; /* buffer space required */ 147 int cd_err; /* saved error value */ 148 } ctf_decl_t; 149 150 typedef struct ctf_dmdef { 151 ctf_list_t dmd_list; /* list forward/back pointers */ 152 char *dmd_name; /* name of this member */ 153 ctf_id_t dmd_type; /* type of this member (for sou) */ 154 ulong_t dmd_offset; /* offset of this member in bits (for sou) */ 155 int dmd_value; /* value of this member (for enum) */ 156 } ctf_dmdef_t; 157 158 typedef struct ctf_dtdef { 159 ctf_list_t dtd_list; /* list forward/back pointers */ 160 struct ctf_dtdef *dtd_hash; /* hash chain pointer for ctf_dthash */ 161 char *dtd_name; /* name associated with definition (if any) */ 162 ctf_id_t dtd_type; /* type identifier for this definition */ 163 struct ctf_type_v3 dtd_data; /* type node (see <sys/ctf.h>) */ 164 int dtd_ref; /* recfount for dyanmic types */ 165 union { 166 ctf_list_t dtu_members; /* struct, union, or enum */ 167 ctf_arinfo_t dtu_arr; /* array */ 168 ctf_encoding_t dtu_enc; /* integer or float */ 169 ctf_id_t *dtu_argv; /* function */ 170 } dtd_u; 171 } ctf_dtdef_t; 172 173 typedef struct ctf_bundle { 174 ctf_file_t *ctb_file; /* CTF container handle */ 175 ctf_id_t ctb_type; /* CTF type identifier */ 176 ctf_dtdef_t *ctb_dtd; /* CTF dynamic type definition (if any) */ 177 } ctf_bundle_t; 178 179 /* 180 * The ctf_file is the structure used to represent a CTF container to library 181 * clients, who see it only as an opaque pointer. Modifications can therefore 182 * be made freely to this structure without regard to client versioning. The 183 * ctf_file_t typedef appears in <sys/ctf_api.h> and declares a forward tag. 184 * 185 * NOTE: ctf_update() requires that everything inside of ctf_file either be an 186 * immediate value, a pointer to dynamically allocated data *outside* of the 187 * ctf_file itself, or a pointer to statically allocated data. If you add a 188 * pointer to ctf_file that points to something within the ctf_file itself, 189 * you must make corresponding changes to ctf_update(). 190 */ 191 struct ctf_file { 192 const ctf_fileops_t *ctf_fileops; /* version-specific file operations */ 193 ctf_sect_t ctf_data; /* CTF data from object file */ 194 ctf_sect_t ctf_symtab; /* symbol table from object file */ 195 ctf_sect_t ctf_strtab; /* string table from object file */ 196 ctf_hash_t ctf_structs; /* hash table of struct types */ 197 ctf_hash_t ctf_unions; /* hash table of union types */ 198 ctf_hash_t ctf_enums; /* hash table of enum types */ 199 ctf_hash_t ctf_names; /* hash table of remaining type names */ 200 ctf_lookup_t ctf_lookups[5]; /* pointers to hashes for name lookup */ 201 ctf_strs_t ctf_str[2]; /* array of string table base and bounds */ 202 const uchar_t *ctf_base; /* base of CTF header + uncompressed buffer */ 203 const uchar_t *ctf_buf; /* uncompressed CTF data buffer */ 204 size_t ctf_size; /* size of CTF header + uncompressed data */ 205 uint_t *ctf_sxlate; /* translation table for symtab entries */ 206 ulong_t ctf_nsyms; /* number of entries in symtab xlate table */ 207 uint_t *ctf_txlate; /* translation table for type IDs */ 208 uint_t *ctf_ptrtab; /* translation table for pointer-to lookups */ 209 ulong_t ctf_typemax; /* maximum valid type ID number */ 210 const ctf_dmodel_t *ctf_dmodel; /* data model pointer (see above) */ 211 struct ctf_file *ctf_parent; /* parent CTF container (if any) */ 212 const char *ctf_parlabel; /* label in parent container (if any) */ 213 const char *ctf_parname; /* basename of parent (if any) */ 214 uint_t ctf_refcnt; /* reference count (for parent links) */ 215 uint_t ctf_flags; /* libctf flags (see below) */ 216 int ctf_errno; /* error code for most recent error */ 217 int ctf_version; /* CTF data version */ 218 size_t ctf_idwidth; /* Size, in bytes, of a type ID */ 219 ctf_dtdef_t **ctf_dthash; /* hash of dynamic type definitions */ 220 ulong_t ctf_dthashlen; /* size of dynamic type hash bucket array */ 221 ctf_list_t ctf_dtdefs; /* list of dynamic type definitions */ 222 size_t ctf_dtstrlen; /* total length of dynamic type strings */ 223 ulong_t ctf_dtnextid; /* next dynamic type id to assign */ 224 ulong_t ctf_dtoldid; /* oldest id that has been committed */ 225 void *ctf_specific; /* data for ctf_get/setspecific */ 226 }; 227 228 #define LCTF_INDEX_TO_TYPEPTR(fp, i) \ 229 ((void *)((uintptr_t)(fp)->ctf_buf + (fp)->ctf_txlate[(i)])) 230 231 #define LCTF_INFO_KIND(fp, info) ((fp)->ctf_fileops->ctfo_get_kind(info)) 232 #define LCTF_INFO_ROOT(fp, info) ((fp)->ctf_fileops->ctfo_get_root(info)) 233 #define LCTF_INFO_VLEN(fp, info) ((fp)->ctf_fileops->ctfo_get_vlen(info)) 234 #define LCTF_MAX_VLEN(fp) ((fp)->ctf_fileops->ctfo_get_max_vlen()) 235 #define LCTF_MAX_SIZE(fp) ((fp)->ctf_fileops->ctfo_get_max_size()) 236 #define LCTF_MAX_TYPE(fp) ((fp)->ctf_fileops->ctfo_get_max_type()) 237 #define LCTF_LSIZE_SENT(fp) \ 238 ((fp)->ctf_fileops->ctfo_get_lsize_sent()) 239 #define LCTF_LSTRUCT_THRESH(fp) \ 240 ((fp)->ctf_fileops->ctfo_get_lstruct_thresh()) 241 242 #define LCTF_TYPE_INFO(fp, k, r, l) ((fp)->ctf_fileops->ctfo_type_info(k, r, l)) 243 #define LCTF_TYPE_ISPARENT(fp, id) ((fp)->ctf_fileops->ctfo_type_isparent(id)) 244 #define LCTF_TYPE_ISCHILD(fp, id) ((fp)->ctf_fileops->ctfo_type_ischild(id)) 245 #define LCTF_TYPE_TO_INDEX(fp, t) ((fp)->ctf_fileops->ctfo_type_to_index(t)) 246 #define LCTF_INDEX_TO_TYPE(fp, id, c) ((fp)->ctf_fileops->ctfo_index_to_type(id, c)) 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 #define ECTF_BASE 1000 /* base value for libctf errnos */ 254 255 enum { 256 ECTF_FMT = ECTF_BASE, /* file is not in CTF or ELF format */ 257 ECTF_ELFVERS, /* ELF version is more recent than libctf */ 258 ECTF_CTFVERS, /* CTF version is more recent than libctf */ 259 ECTF_ENDIAN, /* data is different endian-ness than lib */ 260 ECTF_SYMTAB, /* symbol table uses invalid entry size */ 261 ECTF_SYMBAD, /* symbol table data buffer invalid */ 262 ECTF_STRBAD, /* string table data buffer invalid */ 263 ECTF_CORRUPT, /* file data corruption detected */ 264 ECTF_NOCTFDATA, /* ELF file does not contain CTF data */ 265 ECTF_NOCTFBUF, /* buffer does not contain CTF data */ 266 ECTF_NOSYMTAB, /* symbol table data is not available */ 267 ECTF_NOPARENT, /* parent CTF container is not available */ 268 ECTF_DMODEL, /* data model mismatch */ 269 ECTF_MMAP, /* failed to mmap a data section */ 270 ECTF_ZMISSING, /* decompression library not installed */ 271 ECTF_ZINIT, /* failed to initialize decompression library */ 272 ECTF_ZALLOC, /* failed to allocate decompression buffer */ 273 ECTF_DECOMPRESS, /* failed to decompress CTF data */ 274 ECTF_STRTAB, /* string table for this string is missing */ 275 ECTF_BADNAME, /* string offset is corrupt w.r.t. strtab */ 276 ECTF_BADID, /* invalid type ID number */ 277 ECTF_NOTSOU, /* type is not a struct or union */ 278 ECTF_NOTENUM, /* type is not an enum */ 279 ECTF_NOTSUE, /* type is not a struct, union, or enum */ 280 ECTF_NOTINTFP, /* type is not an integer or float */ 281 ECTF_NOTARRAY, /* type is not an array */ 282 ECTF_NOTREF, /* type does not reference another type */ 283 ECTF_NAMELEN, /* buffer is too small to hold type name */ 284 ECTF_NOTYPE, /* no type found corresponding to name */ 285 ECTF_SYNTAX, /* syntax error in type name */ 286 ECTF_NOTFUNC, /* symtab entry does not refer to a function */ 287 ECTF_NOFUNCDAT, /* no func info available for function */ 288 ECTF_NOTDATA, /* symtab entry does not refer to a data obj */ 289 ECTF_NOTYPEDAT, /* no type info available for object */ 290 ECTF_NOLABEL, /* no label found corresponding to name */ 291 ECTF_NOLABELDATA, /* file does not contain any labels */ 292 ECTF_NOTSUP, /* feature not supported */ 293 ECTF_NOENUMNAM, /* enum element name not found */ 294 ECTF_NOMEMBNAM, /* member name not found */ 295 ECTF_RDONLY, /* CTF container is read-only */ 296 ECTF_DTFULL, /* CTF type is full (no more members allowed) */ 297 ECTF_FULL, /* CTF container is full */ 298 ECTF_DUPMEMBER, /* duplicate member name definition */ 299 ECTF_CONFLICT, /* conflicting type definition present */ 300 ECTF_REFERENCED, /* type has outstanding references */ 301 ECTF_NOTDYN /* type is not a dynamic type */ 302 }; 303 304 extern void ctf_get_ctt_index(const ctf_file_t *fp, const void *v, 305 uint_t *indexp, uint_t *typep, int *ischildp); 306 extern ssize_t ctf_get_ctt_size(const ctf_file_t *, const void *v, ssize_t *, 307 ssize_t *); 308 extern void ctf_get_ctt_info(const ctf_file_t *, const void *v, uint_t *kind, 309 uint_t *vlen, int *isroot); 310 311 extern void ctf_get_ctm_info(const ctf_file_t *fp, const void *v, size_t sz, 312 size_t *incrementp, uint_t *typep, ulong_t *offsetp, const char **namep); 313 314 extern const void *ctf_lookup_by_id(ctf_file_t **, ctf_id_t); 315 extern const char *ctf_type_rname(ctf_file_t *, const void *); 316 317 extern int ctf_hash_create(ctf_hash_t *, ulong_t); 318 extern int ctf_hash_insert(ctf_hash_t *, ctf_file_t *, uint_t, uint_t); 319 extern int ctf_hash_define(ctf_hash_t *, ctf_file_t *, uint_t, uint_t); 320 extern ctf_helem_t *ctf_hash_lookup(ctf_hash_t *, ctf_file_t *, 321 const char *, size_t); 322 extern uint_t ctf_hash_size(const ctf_hash_t *); 323 extern void ctf_hash_destroy(ctf_hash_t *); 324 325 #define ctf_list_prev(elem) ((void *)(((ctf_list_t *)(elem))->l_prev)) 326 #define ctf_list_next(elem) ((void *)(((ctf_list_t *)(elem))->l_next)) 327 328 extern void ctf_list_append(ctf_list_t *, void *); 329 extern void ctf_list_prepend(ctf_list_t *, void *); 330 extern void ctf_list_delete(ctf_list_t *, void *); 331 332 extern void ctf_dtd_insert(ctf_file_t *, ctf_dtdef_t *); 333 extern void ctf_dtd_delete(ctf_file_t *, ctf_dtdef_t *); 334 extern ctf_dtdef_t *ctf_dtd_lookup(ctf_file_t *, ctf_id_t); 335 336 extern void ctf_decl_init(ctf_decl_t *, char *, size_t); 337 extern void ctf_decl_fini(ctf_decl_t *); 338 extern void ctf_decl_push(ctf_decl_t *, ctf_file_t *, ctf_id_t); 339 extern void ctf_decl_sprintf(ctf_decl_t *, const char *, ...); 340 341 extern const char *ctf_strraw(const ctf_file_t *, uint_t); 342 extern const char *ctf_strptr(const ctf_file_t *, uint_t); 343 344 extern ctf_file_t *ctf_set_open_errno(int *, int); 345 extern long ctf_set_errno(ctf_file_t *, int); 346 347 extern const void *ctf_sect_mmap(ctf_sect_t *, int); 348 extern void ctf_sect_munmap(const ctf_sect_t *); 349 350 extern void *ctf_data_alloc(size_t); 351 extern void ctf_data_free(void *, size_t); 352 extern void ctf_data_protect(void *, size_t); 353 354 extern void *ctf_alloc(size_t); 355 extern void ctf_free(void *, size_t); 356 357 extern char *ctf_strdup(const char *); 358 extern const char *ctf_strerror(int); 359 extern void ctf_dprintf(const char *, ...); 360 361 extern void *ctf_zopen(int *); 362 363 extern const char _CTF_SECTION[]; /* name of CTF ELF section */ 364 extern const char _CTF_NULLSTR[]; /* empty string */ 365 366 extern int _libctf_version; /* library client version */ 367 extern int _libctf_debug; /* debugging messages enabled */ 368 369 #ifdef __cplusplus 370 } 371 #endif 372 373 #endif /* _CTF_IMPL_H */ 374