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