Path: blob/main/cddl/contrib/opensolaris/common/ctf/ctf_impl.h
39507 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2006 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/26/*27* Copyright (c) 2012, Joyent, Inc. All rights reserved.28*/2930#ifndef _CTF_IMPL_H31#define _CTF_IMPL_H3233#include <sys/types.h>34#include <sys/errno.h>35#include <sys/sysmacros.h>36#include <sys/ctf_api.h>3738#ifdef _KERNEL3940#include <sys/systm.h>41#include <sys/cmn_err.h>42#include <sys/varargs.h>4344#define isspace(c) \45((c) == ' ' || (c) == '\t' || (c) == '\n' || \46(c) == '\r' || (c) == '\f' || (c) == '\v')4748#define MAP_FAILED ((void *)-1)4950#else /* _KERNEL */5152#include <strings.h>53#include <stdlib.h>54#include <stdarg.h>55#include <stdio.h>56#include <limits.h>57#include <ctype.h>5859#endif /* _KERNEL */6061#ifdef __cplusplus62extern "C" {63#endif6465typedef struct ctf_helem {66uint_t h_name; /* reference to name in string table */67uint_t h_type; /* corresponding type ID number */68uint_t h_next; /* index of next element in hash chain */69} ctf_helem_t;7071typedef struct ctf_hash {72uint_t *h_buckets; /* hash bucket array (chain indices) */73ctf_helem_t *h_chains; /* hash chains buffer */74uint_t h_nbuckets; /* number of elements in bucket array */75uint_t h_nelems; /* number of elements in hash table */76uint_t h_free; /* index of next free hash element */77} ctf_hash_t;7879typedef struct ctf_strs {80const char *cts_strs; /* base address of string table */81size_t cts_len; /* size of string table in bytes */82} ctf_strs_t;8384typedef struct ctf_dmodel {85const char *ctd_name; /* data model name */86int ctd_code; /* data model code */87size_t ctd_pointer; /* size of void * in bytes */88size_t ctd_char; /* size of char in bytes */89size_t ctd_short; /* size of short in bytes */90size_t ctd_int; /* size of int in bytes */91size_t ctd_long; /* size of long in bytes */92} ctf_dmodel_t;9394typedef struct ctf_lookup {95const char *ctl_prefix; /* string prefix for this lookup */96size_t ctl_len; /* length of prefix string in bytes */97ctf_hash_t *ctl_hash; /* pointer to hash table for lookup */98} ctf_lookup_t;99100typedef struct ctf_fileops {101uint_t (*ctfo_get_kind)(uint_t);102uint_t (*ctfo_get_root)(uint_t);103uint_t (*ctfo_get_vlen)(uint_t);104uint_t (*ctfo_get_max_vlen)(void);105uint_t (*ctfo_get_max_size)(void);106uint_t (*ctfo_get_max_type)(void);107uint_t (*ctfo_get_lsize_sent)(void);108uint_t (*ctfo_get_lstruct_thresh)(void);109110uint_t (*ctfo_type_info)(uint_t, uint_t, uint_t);111int (*ctfo_type_isparent)(uint_t);112int (*ctfo_type_ischild)(uint_t);113uint_t (*ctfo_type_to_index)(uint_t);114uint_t (*ctfo_index_to_type)(uint_t, uint_t);115} ctf_fileops_t;116117typedef struct ctf_list {118struct ctf_list *l_prev; /* previous pointer or tail pointer */119struct ctf_list *l_next; /* next pointer or head pointer */120} ctf_list_t;121122typedef enum {123CTF_PREC_BASE,124CTF_PREC_POINTER,125CTF_PREC_ARRAY,126CTF_PREC_FUNCTION,127CTF_PREC_MAX128} ctf_decl_prec_t;129130typedef struct ctf_decl_node {131ctf_list_t cd_list; /* linked list pointers */132ctf_id_t cd_type; /* type identifier */133uint_t cd_kind; /* type kind */134uint_t cd_n; /* type dimension if array */135} ctf_decl_node_t;136137typedef struct ctf_decl {138ctf_list_t cd_nodes[CTF_PREC_MAX]; /* declaration node stacks */139int cd_order[CTF_PREC_MAX]; /* storage order of decls */140ctf_decl_prec_t cd_qualp; /* qualifier precision */141ctf_decl_prec_t cd_ordp; /* ordered precision */142char *cd_buf; /* buffer for output */143char *cd_ptr; /* buffer location */144char *cd_end; /* buffer limit */145size_t cd_len; /* buffer space required */146int cd_err; /* saved error value */147} ctf_decl_t;148149typedef struct ctf_dmdef {150ctf_list_t dmd_list; /* list forward/back pointers */151char *dmd_name; /* name of this member */152ctf_id_t dmd_type; /* type of this member (for sou) */153ulong_t dmd_offset; /* offset of this member in bits (for sou) */154int dmd_value; /* value of this member (for enum) */155} ctf_dmdef_t;156157typedef struct ctf_dtdef {158ctf_list_t dtd_list; /* list forward/back pointers */159struct ctf_dtdef *dtd_hash; /* hash chain pointer for ctf_dthash */160char *dtd_name; /* name associated with definition (if any) */161ctf_id_t dtd_type; /* type identifier for this definition */162struct ctf_type_v3 dtd_data; /* type node (see <sys/ctf.h>) */163int dtd_ref; /* recfount for dyanmic types */164union {165ctf_list_t dtu_members; /* struct, union, or enum */166ctf_arinfo_t dtu_arr; /* array */167ctf_encoding_t dtu_enc; /* integer or float */168ctf_id_t *dtu_argv; /* function */169} dtd_u;170} ctf_dtdef_t;171172typedef struct ctf_bundle {173ctf_file_t *ctb_file; /* CTF container handle */174ctf_id_t ctb_type; /* CTF type identifier */175ctf_dtdef_t *ctb_dtd; /* CTF dynamic type definition (if any) */176} ctf_bundle_t;177178/*179* The ctf_file is the structure used to represent a CTF container to library180* clients, who see it only as an opaque pointer. Modifications can therefore181* be made freely to this structure without regard to client versioning. The182* ctf_file_t typedef appears in <sys/ctf_api.h> and declares a forward tag.183*184* NOTE: ctf_update() requires that everything inside of ctf_file either be an185* immediate value, a pointer to dynamically allocated data *outside* of the186* ctf_file itself, or a pointer to statically allocated data. If you add a187* pointer to ctf_file that points to something within the ctf_file itself,188* you must make corresponding changes to ctf_update().189*/190struct ctf_file {191const ctf_fileops_t *ctf_fileops; /* version-specific file operations */192ctf_sect_t ctf_data; /* CTF data from object file */193ctf_sect_t ctf_symtab; /* symbol table from object file */194ctf_sect_t ctf_strtab; /* string table from object file */195ctf_hash_t ctf_structs; /* hash table of struct types */196ctf_hash_t ctf_unions; /* hash table of union types */197ctf_hash_t ctf_enums; /* hash table of enum types */198ctf_hash_t ctf_names; /* hash table of remaining type names */199ctf_lookup_t ctf_lookups[5]; /* pointers to hashes for name lookup */200ctf_strs_t ctf_str[2]; /* array of string table base and bounds */201const uchar_t *ctf_base; /* base of CTF header + uncompressed buffer */202const uchar_t *ctf_buf; /* uncompressed CTF data buffer */203size_t ctf_size; /* size of CTF header + uncompressed data */204uint_t *ctf_sxlate; /* translation table for symtab entries */205ulong_t ctf_nsyms; /* number of entries in symtab xlate table */206uint_t *ctf_txlate; /* translation table for type IDs */207uint_t *ctf_ptrtab; /* translation table for pointer-to lookups */208ulong_t ctf_typemax; /* maximum valid type ID number */209const ctf_dmodel_t *ctf_dmodel; /* data model pointer (see above) */210struct ctf_file *ctf_parent; /* parent CTF container (if any) */211const char *ctf_parlabel; /* label in parent container (if any) */212const char *ctf_parname; /* basename of parent (if any) */213uint_t ctf_refcnt; /* reference count (for parent links) */214uint_t ctf_flags; /* libctf flags (see below) */215int ctf_errno; /* error code for most recent error */216int ctf_version; /* CTF data version */217size_t ctf_idwidth; /* Size, in bytes, of a type ID */218ctf_dtdef_t **ctf_dthash; /* hash of dynamic type definitions */219ulong_t ctf_dthashlen; /* size of dynamic type hash bucket array */220ctf_list_t ctf_dtdefs; /* list of dynamic type definitions */221size_t ctf_dtstrlen; /* total length of dynamic type strings */222ulong_t ctf_dtnextid; /* next dynamic type id to assign */223ulong_t ctf_dtoldid; /* oldest id that has been committed */224void *ctf_specific; /* data for ctf_get/setspecific */225};226227#define LCTF_INDEX_TO_TYPEPTR(fp, i) \228((void *)((uintptr_t)(fp)->ctf_buf + (fp)->ctf_txlate[(i)]))229230#define LCTF_INFO_KIND(fp, info) ((fp)->ctf_fileops->ctfo_get_kind(info))231#define LCTF_INFO_ROOT(fp, info) ((fp)->ctf_fileops->ctfo_get_root(info))232#define LCTF_INFO_VLEN(fp, info) ((fp)->ctf_fileops->ctfo_get_vlen(info))233#define LCTF_MAX_VLEN(fp) ((fp)->ctf_fileops->ctfo_get_max_vlen())234#define LCTF_MAX_SIZE(fp) ((fp)->ctf_fileops->ctfo_get_max_size())235#define LCTF_MAX_TYPE(fp) ((fp)->ctf_fileops->ctfo_get_max_type())236#define LCTF_LSIZE_SENT(fp) \237((fp)->ctf_fileops->ctfo_get_lsize_sent())238#define LCTF_LSTRUCT_THRESH(fp) \239((fp)->ctf_fileops->ctfo_get_lstruct_thresh())240241#define LCTF_TYPE_INFO(fp, k, r, l) ((fp)->ctf_fileops->ctfo_type_info(k, r, l))242#define LCTF_TYPE_ISPARENT(fp, id) ((fp)->ctf_fileops->ctfo_type_isparent(id))243#define LCTF_TYPE_ISCHILD(fp, id) ((fp)->ctf_fileops->ctfo_type_ischild(id))244#define LCTF_TYPE_TO_INDEX(fp, t) ((fp)->ctf_fileops->ctfo_type_to_index(t))245#define LCTF_INDEX_TO_TYPE(fp, id, c) ((fp)->ctf_fileops->ctfo_index_to_type(id, c))246247#define LCTF_MMAP 0x0001 /* libctf should munmap buffers on close */248#define LCTF_CHILD 0x0002 /* CTF container is a child */249#define LCTF_RDWR 0x0004 /* CTF container is writable */250#define LCTF_DIRTY 0x0008 /* CTF container has been modified */251252#define ECTF_BASE 1000 /* base value for libctf errnos */253254enum {255ECTF_FMT = ECTF_BASE, /* file is not in CTF or ELF format */256ECTF_ELFVERS, /* ELF version is more recent than libctf */257ECTF_CTFVERS, /* CTF version is more recent than libctf */258ECTF_ENDIAN, /* data is different endian-ness than lib */259ECTF_SYMTAB, /* symbol table uses invalid entry size */260ECTF_SYMBAD, /* symbol table data buffer invalid */261ECTF_STRBAD, /* string table data buffer invalid */262ECTF_CORRUPT, /* file data corruption detected */263ECTF_NOCTFDATA, /* ELF file does not contain CTF data */264ECTF_NOCTFBUF, /* buffer does not contain CTF data */265ECTF_NOSYMTAB, /* symbol table data is not available */266ECTF_NOPARENT, /* parent CTF container is not available */267ECTF_DMODEL, /* data model mismatch */268ECTF_MMAP, /* failed to mmap a data section */269ECTF_ZMISSING, /* decompression library not installed */270ECTF_ZINIT, /* failed to initialize decompression library */271ECTF_ZALLOC, /* failed to allocate decompression buffer */272ECTF_DECOMPRESS, /* failed to decompress CTF data */273ECTF_STRTAB, /* string table for this string is missing */274ECTF_BADNAME, /* string offset is corrupt w.r.t. strtab */275ECTF_BADID, /* invalid type ID number */276ECTF_NOTSOU, /* type is not a struct or union */277ECTF_NOTENUM, /* type is not an enum */278ECTF_NOTSUE, /* type is not a struct, union, or enum */279ECTF_NOTINTFP, /* type is not an integer or float */280ECTF_NOTARRAY, /* type is not an array */281ECTF_NOTREF, /* type does not reference another type */282ECTF_NAMELEN, /* buffer is too small to hold type name */283ECTF_NOTYPE, /* no type found corresponding to name */284ECTF_SYNTAX, /* syntax error in type name */285ECTF_NOTFUNC, /* symtab entry does not refer to a function */286ECTF_NOFUNCDAT, /* no func info available for function */287ECTF_NOTDATA, /* symtab entry does not refer to a data obj */288ECTF_NOTYPEDAT, /* no type info available for object */289ECTF_NOLABEL, /* no label found corresponding to name */290ECTF_NOLABELDATA, /* file does not contain any labels */291ECTF_NOTSUP, /* feature not supported */292ECTF_NOENUMNAM, /* enum element name not found */293ECTF_NOMEMBNAM, /* member name not found */294ECTF_RDONLY, /* CTF container is read-only */295ECTF_DTFULL, /* CTF type is full (no more members allowed) */296ECTF_FULL, /* CTF container is full */297ECTF_DUPMEMBER, /* duplicate member name definition */298ECTF_CONFLICT, /* conflicting type definition present */299ECTF_REFERENCED, /* type has outstanding references */300ECTF_NOTDYN /* type is not a dynamic type */301};302303extern void ctf_get_ctt_index(const ctf_file_t *fp, const void *v,304uint_t *indexp, uint_t *typep, int *ischildp);305extern ssize_t ctf_get_ctt_size(const ctf_file_t *, const void *v, ssize_t *,306ssize_t *);307extern void ctf_get_ctt_info(const ctf_file_t *, const void *v, uint_t *kind,308uint_t *vlen, int *isroot);309310extern void ctf_get_ctm_info(const ctf_file_t *fp, const void *v, size_t sz,311size_t *incrementp, uint_t *typep, ulong_t *offsetp, const char **namep);312313extern const void *ctf_lookup_by_id(ctf_file_t **, ctf_id_t);314extern const char *ctf_type_rname(ctf_file_t *, const void *);315316extern int ctf_hash_create(ctf_hash_t *, ulong_t);317extern int ctf_hash_insert(ctf_hash_t *, ctf_file_t *, uint_t, uint_t);318extern int ctf_hash_define(ctf_hash_t *, ctf_file_t *, uint_t, uint_t);319extern ctf_helem_t *ctf_hash_lookup(ctf_hash_t *, ctf_file_t *,320const char *, size_t);321extern uint_t ctf_hash_size(const ctf_hash_t *);322extern void ctf_hash_destroy(ctf_hash_t *);323324#define ctf_list_prev(elem) ((void *)(((ctf_list_t *)(elem))->l_prev))325#define ctf_list_next(elem) ((void *)(((ctf_list_t *)(elem))->l_next))326327extern void ctf_list_append(ctf_list_t *, void *);328extern void ctf_list_prepend(ctf_list_t *, void *);329extern void ctf_list_delete(ctf_list_t *, void *);330331extern void ctf_dtd_insert(ctf_file_t *, ctf_dtdef_t *);332extern void ctf_dtd_delete(ctf_file_t *, ctf_dtdef_t *);333extern ctf_dtdef_t *ctf_dtd_lookup(ctf_file_t *, ctf_id_t);334335extern void ctf_decl_init(ctf_decl_t *, char *, size_t);336extern void ctf_decl_fini(ctf_decl_t *);337extern void ctf_decl_push(ctf_decl_t *, ctf_file_t *, ctf_id_t);338extern void ctf_decl_sprintf(ctf_decl_t *, const char *, ...);339340extern const char *ctf_strraw(const ctf_file_t *, uint_t);341extern const char *ctf_strptr(const ctf_file_t *, uint_t);342343extern ctf_file_t *ctf_set_open_errno(int *, int);344extern long ctf_set_errno(ctf_file_t *, int);345346extern const void *ctf_sect_mmap(ctf_sect_t *, int);347extern void ctf_sect_munmap(const ctf_sect_t *);348349extern void *ctf_data_alloc(size_t);350extern void ctf_data_free(void *, size_t);351extern void ctf_data_protect(void *, size_t);352353extern void *ctf_alloc(size_t);354extern void ctf_free(void *, size_t);355356extern char *ctf_strdup(const char *);357extern const char *ctf_strerror(int);358extern void ctf_dprintf(const char *, ...);359360extern void *ctf_zopen(int *);361362extern const char _CTF_SECTION[]; /* name of CTF ELF section */363extern const char _CTF_NULLSTR[]; /* empty string */364365extern int _libctf_version; /* library client version */366extern int _libctf_debug; /* debugging messages enabled */367368#ifdef __cplusplus369}370#endif371372#endif /* _CTF_IMPL_H */373374375