1178481Sjb/* 2178481Sjb * CDDL HEADER START 3178481Sjb * 4178481Sjb * The contents of this file are subject to the terms of the 5178481Sjb * Common Development and Distribution License (the "License"). 6178481Sjb * You may not use this file except in compliance with the License. 7178481Sjb * 8178481Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9178481Sjb * or http://www.opensolaris.org/os/licensing. 10178481Sjb * See the License for the specific language governing permissions 11178481Sjb * and limitations under the License. 12178481Sjb * 13178481Sjb * When distributing Covered Code, include this CDDL HEADER in each 14178481Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15178481Sjb * If applicable, add the following below this CDDL HEADER, with the 16178481Sjb * fields enclosed by brackets "[]" replaced with your own identifying 17178481Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 18178481Sjb * 19178481Sjb * CDDL HEADER END 20178481Sjb */ 21178481Sjb/* 22178481Sjb * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23178481Sjb * Use is subject to license terms. 24178481Sjb */ 25178481Sjb 26178481Sjb#ifndef _CTFTOOLS_H 27178481Sjb#define _CTFTOOLS_H 28178481Sjb 29178481Sjb#pragma ident "%Z%%M% %I% %E% SMI" 30178481Sjb 31178481Sjb/* 32178481Sjb * Functions and data structures used in the manipulation of stabs and CTF data 33178481Sjb */ 34178481Sjb 35178481Sjb#include <stdio.h> 36178481Sjb#include <stdlib.h> 37178481Sjb#include <stdarg.h> 38178481Sjb#include <libelf.h> 39178481Sjb#include <gelf.h> 40178481Sjb#include <pthread.h> 41178481Sjb 42178481Sjb#ifdef __cplusplus 43178481Sjbextern "C" { 44178481Sjb#endif 45178481Sjb 46178481Sjb#include "list.h" 47178481Sjb#include "hash.h" 48178481Sjb 49178481Sjb#ifndef DEBUG_LEVEL 50178481Sjb#define DEBUG_LEVEL 0 51178481Sjb#endif 52178481Sjb#ifndef DEBUG_PARSE 53178481Sjb#define DEBUG_PARSE 0 54178481Sjb#endif 55178481Sjb 56178481Sjb#ifndef DEBUG_STREAM 57178481Sjb#define DEBUG_STREAM stderr 58178481Sjb#endif 59178481Sjb 60178481Sjb#ifndef MAX 61178481Sjb#define MAX(a, b) ((a) < (b) ? (b) : (a)) 62178481Sjb#endif 63178481Sjb 64178481Sjb#ifndef MIN 65178481Sjb#define MIN(a, b) ((a) > (b) ? (b) : (a)) 66178481Sjb#endif 67178481Sjb 68178481Sjb#define TRUE 1 69178481Sjb#define FALSE 0 70178481Sjb 71178481Sjb#define CTF_ELF_SCN_NAME ".SUNW_ctf" 72178481Sjb 73178481Sjb#define CTF_LABEL_LASTIDX -1 74178481Sjb 75178481Sjb#define CTF_DEFAULT_LABEL "*** No Label Provided ***" 76178481Sjb 77178481Sjb/* 78178481Sjb * Default hash sizes 79178481Sjb */ 80178481Sjb#define TDATA_LAYOUT_HASH_SIZE 8191 /* A tdesc hash based on layout */ 81178481Sjb#define TDATA_ID_HASH_SIZE 997 /* A tdesc hash based on type id */ 82178481Sjb#define IIDESC_HASH_SIZE 8191 /* Hash of iidesc's */ 83178481Sjb 84178481Sjb/* 85178481Sjb * The default function argument array size. We'll realloc the array larger 86178481Sjb * if we need to, but we want a default value that will allow us to avoid 87178481Sjb * reallocation in the common case. 88178481Sjb */ 89178481Sjb#define FUNCARG_DEF 5 90178481Sjb 91178481Sjbextern const char *progname; 92178481Sjbextern int debug_level; 93178481Sjbextern int debug_parse; 94178546Sjbextern char *curhdr; 95178481Sjb 96178481Sjb/* 97178481Sjb * This is a partial copy of the stab.h that DevPro includes with their 98178481Sjb * compiler. 99178481Sjb */ 100178481Sjbtypedef struct stab { 101178481Sjb uint32_t n_strx; 102178481Sjb uint8_t n_type; 103178481Sjb int8_t n_other; 104178481Sjb int16_t n_desc; 105178481Sjb uint32_t n_value; 106178481Sjb} stab_t; 107178481Sjb 108178481Sjb#define N_GSYM 0x20 /* global symbol: name,,0,type,0 */ 109178481Sjb#define N_FUN 0x24 /* procedure: name,,0,linenumber,0 */ 110178481Sjb#define N_STSYM 0x26 /* static symbol: name,,0,type,0 or section relative */ 111178481Sjb#define N_LCSYM 0x28 /* .lcomm symbol: name,,0,type,0 or section relative */ 112178481Sjb#define N_ROSYM 0x2c /* ro_data: name,,0,type,0 or section relative */ 113178481Sjb#define N_OPT 0x3c /* compiler options */ 114178481Sjb#define N_RSYM 0x40 /* register sym: name,,0,type,register */ 115178481Sjb#define N_SO 0x64 /* source file name: name,,0,0,0 */ 116178481Sjb#define N_LSYM 0x80 /* local sym: name,,0,type,offset */ 117178481Sjb#define N_SOL 0x84 /* #included file name: name,,0,0,0 */ 118178481Sjb#define N_PSYM 0xa0 /* parameter: name,,0,type,offset */ 119178481Sjb#define N_LBRAC 0xc0 /* left bracket: 0,,0,nesting level,function relative */ 120178481Sjb#define N_RBRAC 0xe0 /* right bracket: 0,,0,nesting level,func relative */ 121178481Sjb#define N_BINCL 0x82 /* header file: name,,0,0,0 */ 122178481Sjb#define N_EINCL 0xa2 /* end of include file */ 123178481Sjb 124178481Sjb/* 125178481Sjb * Nodes in the type tree 126178481Sjb * 127178481Sjb * Each node consists of a single tdesc_t, with one of several auxiliary 128178481Sjb * structures linked in via the `data' union. 129178481Sjb */ 130178481Sjb 131178481Sjb/* The type of tdesc_t node */ 132178481Sjbtypedef enum stabtype { 133178481Sjb STABTYPE_FIRST, /* do not use */ 134178481Sjb INTRINSIC, 135178481Sjb POINTER, 136178481Sjb ARRAY, 137178481Sjb FUNCTION, 138178481Sjb STRUCT, 139178481Sjb UNION, 140178481Sjb ENUM, 141178481Sjb FORWARD, 142178481Sjb TYPEDEF, 143178481Sjb TYPEDEF_UNRES, 144178481Sjb VOLATILE, 145178481Sjb CONST, 146178481Sjb RESTRICT, 147178481Sjb STABTYPE_LAST /* do not use */ 148178481Sjb} stabtype_t; 149178481Sjb 150178481Sjbtypedef struct tdesc tdesc_t; 151178481Sjb 152178481Sjb/* Auxiliary structure for array tdesc_t */ 153178481Sjbtypedef struct ardef { 154178481Sjb tdesc_t *ad_contents; 155178481Sjb tdesc_t *ad_idxtype; 156178481Sjb uint_t ad_nelems; 157178481Sjb} ardef_t; 158178481Sjb 159178481Sjb/* Auxiliary structure for structure/union tdesc_t */ 160178481Sjbtypedef struct mlist { 161178481Sjb int ml_offset; /* Offset from start of structure (in bits) */ 162178481Sjb int ml_size; /* Member size (in bits) */ 163178481Sjb char *ml_name; /* Member name */ 164178481Sjb struct tdesc *ml_type; /* Member type */ 165178481Sjb struct mlist *ml_next; /* Next member */ 166178481Sjb} mlist_t; 167178481Sjb 168178481Sjb/* Auxiliary structure for enum tdesc_t */ 169178481Sjbtypedef struct elist { 170178481Sjb char *el_name; 171178481Sjb int el_number; 172178481Sjb struct elist *el_next; 173178481Sjb} elist_t; 174178481Sjb 175178481Sjb/* Auxiliary structure for intrinsics (integers and reals) */ 176178481Sjbtypedef enum { 177178481Sjb INTR_INT, 178178481Sjb INTR_REAL 179178481Sjb} intrtype_t; 180178481Sjb 181178481Sjbtypedef struct intr { 182178481Sjb intrtype_t intr_type; 183178481Sjb int intr_signed; 184178481Sjb union { 185178481Sjb char _iformat; 186178481Sjb int _fformat; 187178481Sjb } _u; 188178481Sjb int intr_offset; 189178481Sjb int intr_nbits; 190178481Sjb} intr_t; 191178481Sjb 192178481Sjb#define intr_iformat _u._iformat 193178481Sjb#define intr_fformat _u._fformat 194178481Sjb 195178481Sjbtypedef struct fnarg { 196178481Sjb char *fna_name; 197178481Sjb struct tdesc *fna_type; 198178481Sjb} fnarg_t; 199178481Sjb 200178481Sjb#define FN_F_GLOBAL 0x1 201178481Sjb#define FN_F_VARARGS 0x2 202178481Sjb 203178481Sjbtypedef struct fndef { 204178481Sjb struct tdesc *fn_ret; 205178481Sjb uint_t fn_nargs; 206178481Sjb tdesc_t **fn_args; 207178481Sjb uint_t fn_vargs; 208178481Sjb} fndef_t; 209178481Sjb 210178481Sjbtypedef int32_t tid_t; 211178481Sjb 212178481Sjb/* 213178481Sjb * The tdesc_t (Type DESCription) is the basic node type used in the stabs data 214178481Sjb * structure. Each data node gets a tdesc structure. Each node is linked into 215178481Sjb * a directed graph (think of it as a tree with multiple roots and multiple 216178481Sjb * leaves), with the root nodes at the top, and intrinsics at the bottom. The 217178481Sjb * root nodes, which are pointed to by iidesc nodes, correspond to the types, 218178481Sjb * globals, and statics defined by the stabs. 219178481Sjb */ 220178481Sjbstruct tdesc { 221178481Sjb char *t_name; 222178481Sjb tdesc_t *t_next; /* Name hash next pointer */ 223178481Sjb 224178481Sjb tid_t t_id; 225178481Sjb tdesc_t *t_hash; /* ID hash next pointer */ 226178481Sjb 227178481Sjb stabtype_t t_type; 228178481Sjb int t_size; /* Size in bytes of object represented by this node */ 229178481Sjb 230178481Sjb union { 231178481Sjb intr_t *intr; /* int, real */ 232178481Sjb tdesc_t *tdesc; /* ptr, typedef, vol, const, restr */ 233178481Sjb ardef_t *ardef; /* array */ 234178481Sjb mlist_t *members; /* struct, union */ 235178481Sjb elist_t *emem; /* enum */ 236178481Sjb fndef_t *fndef; /* function - first is return type */ 237178481Sjb } t_data; 238178481Sjb 239178481Sjb int t_flags; 240178481Sjb int t_vgen; /* Visitation generation (see traverse.c) */ 241178481Sjb int t_emark; /* Equality mark (see equiv_cb() in merge.c) */ 242178481Sjb}; 243178481Sjb 244178481Sjb#define t_intr t_data.intr 245178481Sjb#define t_tdesc t_data.tdesc 246178481Sjb#define t_ardef t_data.ardef 247178481Sjb#define t_members t_data.members 248178481Sjb#define t_emem t_data.emem 249178481Sjb#define t_fndef t_data.fndef 250178481Sjb 251178481Sjb#define TDESC_F_ISROOT 0x1 /* Has an iidesc_t (see below) */ 252178481Sjb#define TDESC_F_GLOBAL 0x2 253178481Sjb#define TDESC_F_RESOLVED 0x4 254178481Sjb 255178481Sjb/* 256178481Sjb * iidesc_t (Interesting Item DESCription) nodes point to tdesc_t nodes that 257178481Sjb * correspond to "interesting" stabs. A stab is interesting if it defines a 258178481Sjb * global or static variable, a global or static function, or a data type. 259178481Sjb */ 260178481Sjbtypedef enum iitype { 261178481Sjb II_NOT = 0, 262178481Sjb II_GFUN, /* Global function */ 263178481Sjb II_SFUN, /* Static function */ 264178481Sjb II_GVAR, /* Global variable */ 265178481Sjb II_SVAR, /* Static variable */ 266178481Sjb II_PSYM, /* Function argument */ 267178481Sjb II_SOU, /* Struct or union */ 268178481Sjb II_TYPE /* Type (typedef) */ 269178481Sjb} iitype_t; 270178481Sjb 271178481Sjbtypedef struct iidesc { 272178481Sjb iitype_t ii_type; 273178481Sjb char *ii_name; 274178481Sjb tdesc_t *ii_dtype; 275178481Sjb char *ii_owner; /* File that defined this node */ 276178481Sjb int ii_flags; 277178481Sjb 278178481Sjb /* Function arguments (if any) */ 279178481Sjb int ii_nargs; 280178481Sjb tdesc_t **ii_args; 281178481Sjb int ii_vargs; /* Function uses varargs */ 282178481Sjb} iidesc_t; 283178481Sjb 284178481Sjb#define IIDESC_F_USED 0x1 /* Write this iidesc out */ 285178481Sjb 286178481Sjb/* 287178481Sjb * labelent_t nodes identify labels and corresponding type ranges associated 288178481Sjb * with them. The label in a given labelent_t is associated with types with 289178481Sjb * ids <= le_idx. 290178481Sjb */ 291178481Sjbtypedef struct labelent { 292178481Sjb char *le_name; 293178481Sjb int le_idx; 294178481Sjb} labelent_t; 295178481Sjb 296178481Sjb/* 297178481Sjb * The tdata_t (Type DATA) structure contains or references all type data for 298178481Sjb * a given file or, during merging, several files. 299178481Sjb */ 300178481Sjbtypedef struct tdata { 301178481Sjb int td_curemark; /* Equality mark (see merge.c) */ 302178481Sjb int td_curvgen; /* Visitation generation (see traverse.c) */ 303178481Sjb int td_nextid; /* The ID for the next tdesc_t created */ 304178481Sjb hash_t *td_iihash; /* The iidesc_t nodes for this file */ 305178481Sjb 306178481Sjb hash_t *td_layouthash; /* The tdesc nodes, hashed by structure */ 307178481Sjb hash_t *td_idhash; /* The tdesc nodes, hashed by type id */ 308178481Sjb list_t *td_fwdlist; /* All forward declaration tdesc nodes */ 309178481Sjb 310178481Sjb char *td_parlabel; /* Top label uniq'd against in parent */ 311178481Sjb char *td_parname; /* Basename of parent */ 312178481Sjb list_t *td_labels; /* Labels and their type ranges */ 313178481Sjb 314178481Sjb pthread_mutex_t td_mergelock; 315178481Sjb 316178481Sjb int td_ref; 317178481Sjb} tdata_t; 318178481Sjb 319178481Sjb/* 320178481Sjb * By design, the iidesc hash is heterogeneous. The CTF emitter, on the 321178481Sjb * other hand, needs to be able to access the elements of the list by type, 322178481Sjb * and in a specific sorted order. An iiburst holds these elements in that 323178481Sjb * order. (A burster is a machine that separates carbon-copy forms) 324178481Sjb */ 325178481Sjbtypedef struct iiburst { 326178481Sjb int iib_nfuncs; 327178481Sjb int iib_curfunc; 328178481Sjb iidesc_t **iib_funcs; 329178481Sjb 330178481Sjb int iib_nobjts; 331178481Sjb int iib_curobjt; 332178481Sjb iidesc_t **iib_objts; 333178481Sjb 334178481Sjb list_t *iib_types; 335178481Sjb int iib_maxtypeid; 336178481Sjb 337178481Sjb tdata_t *iib_td; 338178481Sjb struct tdtrav_data *iib_tdtd; /* tdtrav_data_t */ 339178481Sjb} iiburst_t; 340178481Sjb 341178481Sjbtypedef struct ctf_buf ctf_buf_t; 342178481Sjb 343178481Sjbtypedef struct symit_data symit_data_t; 344178481Sjb 345178481Sjb/* fixup_tdescs.c */ 346178481Sjbvoid cvt_fixstabs(tdata_t *); 347178481Sjbvoid cvt_fixups(tdata_t *, size_t); 348178481Sjb 349178481Sjb/* ctf.c */ 350178481Sjbcaddr_t ctf_gen(iiburst_t *, size_t *, int); 351178481Sjbtdata_t *ctf_load(char *, caddr_t, size_t, symit_data_t *, char *); 352178481Sjb 353178481Sjb/* iidesc.c */ 354178481Sjbiidesc_t *iidesc_new(char *); 355178481Sjbint iidesc_hash(int, void *); 356178481Sjbvoid iter_iidescs_by_name(tdata_t *, const char *, 357178546Sjb int (*)(void *, void *), void *); 358178481Sjbiidesc_t *iidesc_dup(iidesc_t *); 359178481Sjbiidesc_t *iidesc_dup_rename(iidesc_t *, char const *, char const *); 360178481Sjbvoid iidesc_add(hash_t *, iidesc_t *); 361178546Sjbvoid iidesc_free(void *, void *); 362178481Sjbint iidesc_count_type(void *, void *); 363178481Sjbvoid iidesc_stats(hash_t *); 364178481Sjbint iidesc_dump(iidesc_t *); 365178481Sjb 366178481Sjb/* input.c */ 367178481Sjbtypedef enum source_types { 368178481Sjb SOURCE_NONE = 0, 369178481Sjb SOURCE_UNKNOWN = 1, 370178481Sjb SOURCE_C = 2, 371178481Sjb SOURCE_S = 4 372178481Sjb} source_types_t; 373178481Sjb 374178481Sjbsource_types_t built_source_types(Elf *, const char *); 375178481Sjbint count_files(char **, int); 376178481Sjbint read_ctf(char **, int, char *, int (*)(tdata_t *, char *, void *), 377178481Sjb void *, int); 378178481Sjbint read_ctf_save_cb(tdata_t *, char *, void *); 379178481Sjbsymit_data_t *symit_new(Elf *, const char *); 380178481Sjbvoid symit_reset(symit_data_t *); 381178481Sjbchar *symit_curfile(symit_data_t *); 382178481SjbGElf_Sym *symit_next(symit_data_t *, int); 383178481Sjbchar *symit_name(symit_data_t *); 384178481Sjbvoid symit_free(symit_data_t *); 385178481Sjb 386178481Sjb/* merge.c */ 387178481Sjbvoid merge_into_master(tdata_t *, tdata_t *, tdata_t *, int); 388178481Sjb 389178481Sjb/* output.c */ 390178481Sjb#define CTF_FUZZY_MATCH 0x1 /* match local symbols to global CTF */ 391178481Sjb#define CTF_USE_DYNSYM 0x2 /* use .dynsym not .symtab */ 392178481Sjb#define CTF_COMPRESS 0x4 /* compress CTF output */ 393178481Sjb#define CTF_KEEP_STABS 0x8 /* keep .stabs sections */ 394178481Sjb 395178481Sjbvoid write_ctf(tdata_t *, const char *, const char *, int); 396178481Sjb 397178481Sjb/* parse.c */ 398178481Sjbvoid parse_init(tdata_t *); 399178481Sjbvoid parse_finish(tdata_t *); 400178481Sjbint parse_stab(stab_t *, char *, iidesc_t **); 401178481Sjbtdesc_t *lookup(int); 402178481Sjbtdesc_t *lookupname(const char *); 403178481Sjbvoid check_hash(void); 404178481Sjbvoid resolve_typed_bitfields(void); 405178481Sjb 406178481Sjb/* stabs.c */ 407178546Sjbint stabs_read(tdata_t *, Elf *, char *); 408178481Sjb 409178481Sjb/* dwarf.c */ 410178546Sjbint dw_read(tdata_t *, Elf *, char *); 411178481Sjbconst char *dw_tag2str(uint_t); 412178481Sjb 413178481Sjb/* tdata.c */ 414178481Sjbtdata_t *tdata_new(void); 415178481Sjbvoid tdata_free(tdata_t *); 416178481Sjbvoid tdata_build_hashes(tdata_t *td); 417178481Sjbconst char *tdesc_name(tdesc_t *); 418178481Sjbint tdesc_idhash(int, void *); 419178481Sjbint tdesc_idcmp(void *, void *); 420178481Sjbint tdesc_namehash(int, void *); 421178481Sjbint tdesc_namecmp(void *, void *); 422178481Sjbint tdesc_layouthash(int, void *); 423178481Sjbint tdesc_layoutcmp(void *, void *); 424178481Sjbvoid tdesc_free(tdesc_t *); 425178546Sjbvoid tdata_label_add(tdata_t *, const char *, int); 426178481Sjblabelent_t *tdata_label_top(tdata_t *); 427178481Sjbint tdata_label_find(tdata_t *, char *); 428178481Sjbvoid tdata_label_free(tdata_t *); 429178481Sjbvoid tdata_merge(tdata_t *, tdata_t *); 430178481Sjbvoid tdata_label_newmax(tdata_t *, int); 431178481Sjb 432178481Sjb/* util.c */ 433178481Sjbint streq(const char *, const char *); 434178481Sjbint findelfsecidx(Elf *, const char *, const char *); 435178481Sjbsize_t elf_ptrsz(Elf *); 436178481Sjbchar *mktmpname(const char *, const char *); 437178546Sjbvoid terminate(const char *, ...); 438178546Sjbvoid aborterr(const char *, ...); 439178546Sjbvoid set_terminate_cleanup(void (*)(void)); 440178481Sjbvoid elfterminate(const char *, const char *, ...); 441178546Sjbvoid warning(const char *, ...); 442178546Sjbvoid vadebug(int, const char *, va_list); 443178546Sjbvoid debug(int, const char *, ...); 444178481Sjb 445178546Sjb 446178546Sjbvoid watch_dump(int); 447178546Sjbvoid watch_set(void *, int); 448178546Sjb 449178481Sjb#ifdef __cplusplus 450178481Sjb} 451178481Sjb#endif 452178481Sjb 453178481Sjb#endif /* _CTFTOOLS_H */ 454