Path: blob/main/cddl/contrib/opensolaris/tools/ctf/cvt/ctftools.h
39586 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/20/*21* Copyright 2006 Sun Microsystems, Inc. All rights reserved.22* Use is subject to license terms.23*/2425#ifndef _CTFTOOLS_H26#define _CTFTOOLS_H2728/*29* Functions and data structures used in the manipulation of stabs and CTF data30*/3132#include <stdio.h>33#include <stdlib.h>34#include <stdarg.h>35#include <libelf.h>36#include <gelf.h>37#include <pthread.h>3839#include <sys/ccompile.h>40#include <sys/endian.h>4142#ifdef __cplusplus43extern "C" {44#endif4546#include "list.h"47#include "hash.h"4849#ifndef DEBUG_LEVEL50#define DEBUG_LEVEL 051#endif5253#ifndef DEBUG_STREAM54#define DEBUG_STREAM stderr55#endif5657#ifndef MAX58#define MAX(a, b) ((a) < (b) ? (b) : (a))59#endif6061#ifndef MIN62#define MIN(a, b) ((a) > (b) ? (b) : (a))63#endif6465/* Sanity check for cross-build bootstrap tools */66#if !defined(BYTE_ORDER)67#error "Missing BYTE_ORDER defines"68#elif !defined(_LITTLE_ENDIAN)69#error "Missing _LITTLE_ENDIAN defines"70#elif !defined(_BIG_ENDIAN)71#error "Missing _BIG_ENDIAN defines"72#endif7374#define TRUE 175#define FALSE 07677#define CTF_ELF_SCN_NAME ".SUNW_ctf"7879#define CTF_LABEL_LASTIDX -18081#define CTF_DEFAULT_LABEL "*** No Label Provided ***"8283/*84* Default hash sizes85*/86#define TDATA_LAYOUT_HASH_SIZE 8191 /* A tdesc hash based on layout */87#define TDATA_ID_HASH_SIZE 997 /* A tdesc hash based on type id */88#define IIDESC_HASH_SIZE 8191 /* Hash of iidesc's */8990/*91* The default function argument array size. We'll realloc the array larger92* if we need to, but we want a default value that will allow us to avoid93* reallocation in the common case.94*/95#define FUNCARG_DEF 59697extern const char *progname;98extern int debug_level;99extern char *curhdr;100101/*102* This is a partial copy of the stab.h that DevPro includes with their103* compiler.104*/105typedef struct stab {106uint32_t n_strx;107uint8_t n_type;108int8_t n_other;109int16_t n_desc;110uint32_t n_value;111} stab_t;112113#define N_GSYM 0x20 /* global symbol: name,,0,type,0 */114#define N_FUN 0x24 /* procedure: name,,0,linenumber,0 */115#define N_STSYM 0x26 /* static symbol: name,,0,type,0 or section relative */116#define N_LCSYM 0x28 /* .lcomm symbol: name,,0,type,0 or section relative */117#define N_ROSYM 0x2c /* ro_data: name,,0,type,0 or section relative */118#define N_OPT 0x3c /* compiler options */119#define N_RSYM 0x40 /* register sym: name,,0,type,register */120#define N_SO 0x64 /* source file name: name,,0,0,0 */121#define N_LSYM 0x80 /* local sym: name,,0,type,offset */122#define N_SOL 0x84 /* #included file name: name,,0,0,0 */123#define N_PSYM 0xa0 /* parameter: name,,0,type,offset */124#define N_LBRAC 0xc0 /* left bracket: 0,,0,nesting level,function relative */125#define N_RBRAC 0xe0 /* right bracket: 0,,0,nesting level,func relative */126#define N_BINCL 0x82 /* header file: name,,0,0,0 */127#define N_EINCL 0xa2 /* end of include file */128129/*130* Nodes in the type tree131*132* Each node consists of a single tdesc_t, with one of several auxiliary133* structures linked in via the `data' union.134*/135136/* The type of tdesc_t node */137typedef enum stabtype {138STABTYPE_FIRST, /* do not use */139INTRINSIC,140POINTER,141ARRAY,142FUNCTION,143STRUCT,144UNION,145ENUM,146FORWARD,147TYPEDEF,148TYPEDEF_UNRES,149VOLATILE,150CONST,151RESTRICT,152STABTYPE_LAST /* do not use */153} stabtype_t;154155typedef struct tdesc tdesc_t;156157/* Auxiliary structure for array tdesc_t */158typedef struct ardef {159tdesc_t *ad_contents;160tdesc_t *ad_idxtype;161uint_t ad_nelems;162} ardef_t;163164/* Auxiliary structure for structure/union tdesc_t */165typedef struct mlist {166int ml_offset; /* Offset from start of structure (in bits) */167int ml_size; /* Member size (in bits) */168char *ml_name; /* Member name */169struct tdesc *ml_type; /* Member type */170struct mlist *ml_next; /* Next member */171} mlist_t;172173/* Auxiliary structure for enum tdesc_t */174typedef struct elist {175char *el_name;176int el_number;177struct elist *el_next;178} elist_t;179180/* Auxiliary structure for intrinsics (integers and reals) */181typedef enum {182INTR_INT,183INTR_REAL184} intrtype_t;185186typedef struct intr {187intrtype_t intr_type;188int intr_signed;189union {190char _iformat;191int _fformat;192} _u;193int intr_offset;194int intr_nbits;195} intr_t;196197#define intr_iformat _u._iformat198#define intr_fformat _u._fformat199200typedef struct fnarg {201char *fna_name;202struct tdesc *fna_type;203} fnarg_t;204205#define FN_F_GLOBAL 0x1206#define FN_F_VARARGS 0x2207208typedef struct fndef {209struct tdesc *fn_ret;210uint_t fn_nargs;211tdesc_t **fn_args;212uint_t fn_vargs;213} fndef_t;214215typedef int32_t tid_t;216217/*218* The tdesc_t (Type DESCription) is the basic node type used in the stabs data219* structure. Each data node gets a tdesc structure. Each node is linked into220* a directed graph (think of it as a tree with multiple roots and multiple221* leaves), with the root nodes at the top, and intrinsics at the bottom. The222* root nodes, which are pointed to by iidesc nodes, correspond to the types,223* globals, and statics defined by the stabs.224*/225struct tdesc {226char *t_name;227tdesc_t *t_next; /* Name hash next pointer */228229tid_t t_id;230tdesc_t *t_hash; /* ID hash next pointer */231232stabtype_t t_type;233int t_size; /* Size in bytes of object represented by this node */234235union {236intr_t *intr; /* int, real */237tdesc_t *tdesc; /* ptr, typedef, vol, const, restr */238ardef_t *ardef; /* array */239mlist_t *members; /* struct, union */240elist_t *emem; /* enum */241fndef_t *fndef; /* function - first is return type */242} t_data;243244int t_flags;245int t_vgen; /* Visitation generation (see traverse.c) */246int t_emark; /* Equality mark (see equiv_cb() in merge.c) */247};248249#define t_intr t_data.intr250#define t_tdesc t_data.tdesc251#define t_ardef t_data.ardef252#define t_members t_data.members253#define t_emem t_data.emem254#define t_fndef t_data.fndef255256#define TDESC_F_ISROOT 0x1 /* Has an iidesc_t (see below) */257#define TDESC_F_GLOBAL 0x2258#define TDESC_F_RESOLVED 0x4259260/*261* iidesc_t (Interesting Item DESCription) nodes point to tdesc_t nodes that262* correspond to "interesting" stabs. A stab is interesting if it defines a263* global or static variable, a global or static function, or a data type.264*/265typedef enum iitype {266II_NOT = 0,267II_GFUN, /* Global function */268II_SFUN, /* Static function */269II_GVAR, /* Global variable */270II_SVAR, /* Static variable */271II_PSYM, /* Function argument */272II_SOU, /* Struct or union */273II_TYPE /* Type (typedef) */274} iitype_t;275276typedef struct iidesc {277iitype_t ii_type;278char *ii_name;279tdesc_t *ii_dtype;280char *ii_owner; /* File that defined this node */281int ii_flags;282283/* Function arguments (if any) */284int ii_nargs;285tdesc_t **ii_args;286int ii_vargs; /* Function uses varargs */287} iidesc_t;288289#define IIDESC_F_USED 0x1 /* Write this iidesc out */290291/*292* labelent_t nodes identify labels and corresponding type ranges associated293* with them. The label in a given labelent_t is associated with types with294* ids <= le_idx.295*/296typedef struct labelent {297char *le_name;298int le_idx;299} labelent_t;300301/*302* The tdata_t (Type DATA) structure contains or references all type data for303* a given file or, during merging, several files.304*/305typedef struct tdata {306int td_curemark; /* Equality mark (see merge.c) */307int td_curvgen; /* Visitation generation (see traverse.c) */308int td_nextid; /* The ID for the next tdesc_t created */309hash_t *td_iihash; /* The iidesc_t nodes for this file */310311hash_t *td_layouthash; /* The tdesc nodes, hashed by structure */312hash_t *td_idhash; /* The tdesc nodes, hashed by type id */313list_t *td_fwdlist; /* All forward declaration tdesc nodes */314315char *td_parlabel; /* Top label uniq'd against in parent */316char *td_parname; /* Basename of parent */317list_t *td_labels; /* Labels and their type ranges */318319pthread_mutex_t td_mergelock;320321int td_ref;322} tdata_t;323324/*325* By design, the iidesc hash is heterogeneous. The CTF emitter, on the326* other hand, needs to be able to access the elements of the list by type,327* and in a specific sorted order. An iiburst holds these elements in that328* order. (A burster is a machine that separates carbon-copy forms)329*/330typedef struct iiburst {331int iib_nfuncs;332int iib_curfunc;333iidesc_t **iib_funcs;334335int iib_nobjts;336int iib_curobjt;337iidesc_t **iib_objts;338339list_t *iib_types;340int iib_maxtypeid;341342tdata_t *iib_td;343struct tdtrav_data *iib_tdtd; /* tdtrav_data_t */344} iiburst_t;345346typedef struct ctf_buf ctf_buf_t;347348typedef struct symit_data symit_data_t;349350/* fixup_tdescs.c */351void cvt_fixstabs(tdata_t *);352void cvt_fixups(tdata_t *, size_t);353354/* ctf.c */355caddr_t ctf_gen(iiburst_t *, size_t *, int);356tdata_t *ctf_load(char *, caddr_t, size_t, symit_data_t *, char *);357358/* iidesc.c */359iidesc_t *iidesc_new(char *);360int iidesc_hash(int, void *);361void iter_iidescs_by_name(tdata_t *, const char *,362int (*)(void *, void *), void *);363iidesc_t *iidesc_dup(iidesc_t *);364iidesc_t *iidesc_dup_rename(iidesc_t *, char const *, char const *);365void iidesc_add(hash_t *, iidesc_t *);366void iidesc_free(void *, void *);367int iidesc_count_type(void *, void *);368void iidesc_stats(hash_t *);369int iidesc_dump(iidesc_t *);370371/* input.c */372typedef enum source_types {373SOURCE_NONE = 0,374SOURCE_UNKNOWN = 1,375SOURCE_C = 2,376SOURCE_S = 4377} source_types_t;378379source_types_t built_source_types(Elf *, const char *);380int count_files(char **, int);381int read_ctf(char **, int, char *, int (*)(tdata_t *, char *, void *),382void *, int);383int read_ctf_save_cb(tdata_t *, char *, void *);384symit_data_t *symit_new(Elf *, const char *);385void symit_reset(symit_data_t *);386char *symit_curfile(symit_data_t *);387GElf_Sym *symit_next(symit_data_t *, int);388char *symit_name(symit_data_t *);389void symit_free(symit_data_t *);390391/* merge.c */392void merge_into_master(tdata_t *, tdata_t *, tdata_t *, int);393394/* output.c */395#define CTF_FUZZY_MATCH 0x1 /* match local symbols to global CTF */396#define CTF_USE_DYNSYM 0x2 /* use .dynsym not .symtab */397#define CTF_COMPRESS 0x4 /* compress CTF output */398#define CTF_KEEP_STABS 0x8 /* keep .stabs sections */399#define CTF_SWAP_BYTES 0x10 /* target byte order is different from host */400401void write_ctf(tdata_t *, const char *, const char *, int);402403/* dwarf.c */404int dw_read(tdata_t *, Elf *, char *);405const char *dw_tag2str(uint_t);406407/* tdata.c */408tdata_t *tdata_new(void);409void tdata_free(tdata_t *);410void tdata_build_hashes(tdata_t *td);411const char *tdesc_name(tdesc_t *);412int tdesc_idhash(int, void *);413int tdesc_idcmp(void *, void *);414int tdesc_namehash(int, void *);415int tdesc_namecmp(void *, void *);416int tdesc_layouthash(int, void *);417int tdesc_layoutcmp(void *, void *);418void tdesc_free(tdesc_t *);419void tdata_label_add(tdata_t *, const char *, int);420labelent_t *tdata_label_top(tdata_t *);421int tdata_label_find(tdata_t *, char *);422void tdata_label_free(tdata_t *);423void tdata_merge(tdata_t *, tdata_t *);424void tdata_label_newmax(tdata_t *, int);425426/* util.c */427int streq(const char *, const char *);428int findelfsecidx(Elf *, const char *, const char *);429size_t elf_ptrsz(Elf *);430char *mktmpname(const char *, const char *);431void terminate(const char *, ...) __attribute__((noreturn));432void aborterr(const char *, ...) __attribute__((noreturn));433void set_terminate_cleanup(void (*)(void));434void elfterminate(const char *, const char *, ...);435void warning(const char *, ...);436void vadebug(int, const char *, va_list);437void debug(int, const char *, ...);438439440void watch_dump(int);441void watch_set(void *, int);442443#ifdef __cplusplus444}445#endif446447#endif /* _CTFTOOLS_H */448449450