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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #ifndef _CTFTOOLS_H 27 #define _CTFTOOLS_H 28 29 /* 30 * Functions and data structures used in the manipulation of stabs and CTF data 31 */ 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <stdarg.h> 36 #include <libelf.h> 37 #include <gelf.h> 38 #include <pthread.h> 39 40 #include <sys/endian.h> 41 #include <sys/ccompile.h> 42 43 #ifdef __cplusplus 44 extern "C" { 45 #endif 46 47 #include "list.h" 48 #include "hash.h" 49 50 #ifndef DEBUG_LEVEL 51 #define DEBUG_LEVEL 0 52 #endif 53 #ifndef DEBUG_PARSE 54 #define DEBUG_PARSE 0 55 #endif 56 57 #ifndef DEBUG_STREAM 58 #define DEBUG_STREAM stderr 59 #endif 60 61 #ifndef MAX 62 #define MAX(a, b) ((a) < (b) ? (b) : (a)) 63 #endif 64 65 #ifndef MIN 66 #define MIN(a, b) ((a) > (b) ? (b) : (a)) 67 #endif 68 69 /* Sanity check for cross-build bootstrap tools */ 70 #if !defined(BYTE_ORDER) 71 #error "Missing BYTE_ORDER defines" 72 #elif !defined(LITTLE_ENDIAN) 73 #error "Missing LITTLE_ENDIAN defines" 74 #elif !defined(BIG_ENDIAN) 75 #error "Missing BIG_ENDIAN defines" 76 #endif 77 78 #ifndef TRUE 79 #define TRUE 1 80 #endif 81 #ifndef FALSE 82 #define FALSE 0 83 #endif 84 85 #define CTF_ELF_SCN_NAME ".SUNW_ctf" 86 87 #define CTF_LABEL_LASTIDX -1 88 89 #define CTF_DEFAULT_LABEL "*** No Label Provided ***" 90 91 /* 92 * Default hash sizes 93 */ 94 #define TDATA_LAYOUT_HASH_SIZE 8191 /* A tdesc hash based on layout */ 95 #define TDATA_ID_HASH_SIZE 997 /* A tdesc hash based on type id */ 96 #define IIDESC_HASH_SIZE 8191 /* Hash of iidesc's */ 97 98 /* 99 * The default function argument array size. We'll realloc the array larger 100 * if we need to, but we want a default value that will allow us to avoid 101 * reallocation in the common case. 102 */ 103 #define FUNCARG_DEF 5 104 105 extern const char *progname; 106 extern int debug_level; 107 extern int debug_parse; 108 extern char *curhdr; 109 110 /* 111 * This is a partial copy of the stab.h that DevPro includes with their 112 * compiler. 113 */ 114 typedef struct stab { 115 uint32_t n_strx; 116 uint8_t n_type; 117 int8_t n_other; 118 int16_t n_desc; 119 uint32_t n_value; 120 } stab_t; 121 122 #define N_GSYM 0x20 /* global symbol: name,,0,type,0 */ 123 #define N_FUN 0x24 /* procedure: name,,0,linenumber,0 */ 124 #define N_STSYM 0x26 /* static symbol: name,,0,type,0 or section relative */ 125 #define N_LCSYM 0x28 /* .lcomm symbol: name,,0,type,0 or section relative */ 126 #define N_ROSYM 0x2c /* ro_data: name,,0,type,0 or section relative */ 127 #define N_OPT 0x3c /* compiler options */ 128 #define N_RSYM 0x40 /* register sym: name,,0,type,register */ 129 #define N_SO 0x64 /* source file name: name,,0,0,0 */ 130 #define N_LSYM 0x80 /* local sym: name,,0,type,offset */ 131 #define N_SOL 0x84 /* #included file name: name,,0,0,0 */ 132 #define N_PSYM 0xa0 /* parameter: name,,0,type,offset */ 133 #define N_LBRAC 0xc0 /* left bracket: 0,,0,nesting level,function relative */ 134 #define N_RBRAC 0xe0 /* right bracket: 0,,0,nesting level,func relative */ 135 #define N_BINCL 0x82 /* header file: name,,0,0,0 */ 136 #define N_EINCL 0xa2 /* end of include file */ 137 138 /* 139 * Nodes in the type tree 140 * 141 * Each node consists of a single tdesc_t, with one of several auxiliary 142 * structures linked in via the `data' union. 143 */ 144 145 /* The type of tdesc_t node */ 146 typedef enum stabtype { 147 STABTYPE_FIRST, /* do not use */ 148 INTRINSIC, 149 POINTER, 150 REFERENCE, 151 ARRAY, 152 FUNCTION, 153 STRUCT, 154 UNION, 155 CLASS, 156 ENUM, 157 FORWARD, 158 TYPEDEF, 159 TYPEDEF_UNRES, 160 VOLATILE, 161 CONST, 162 RESTRICT, 163 STABTYPE_LAST /* do not use */ 164 } stabtype_t; 165 166 typedef struct tdesc tdesc_t; 167 168 /* Auxiliary structure for array tdesc_t */ 169 typedef struct ardef { 170 tdesc_t *ad_contents; 171 tdesc_t *ad_idxtype; 172 uint_t ad_nelems; 173 } ardef_t; 174 175 /* Auxiliary structure for structure/union tdesc_t */ 176 typedef struct mlist { 177 int ml_offset; /* Offset from start of structure (in bits) */ 178 uint_t ml_size; /* Member size (in bits) */ 179 char *ml_name; /* Member name */ 180 struct tdesc *ml_type; /* Member type */ 181 struct mlist *ml_next; /* Next member */ 182 } mlist_t; 183 184 /* Auxiliary structure for enum tdesc_t */ 185 typedef struct elist { 186 char *el_name; 187 int el_number; 188 struct elist *el_next; 189 } elist_t; 190 191 /* Auxiliary structure for intrinsics (integers and reals) */ 192 typedef enum { 193 INTR_INT, 194 INTR_REAL 195 } intrtype_t; 196 197 typedef struct intr { 198 intrtype_t intr_type; 199 int intr_signed; 200 union { 201 char _iformat; 202 int _fformat; 203 } _u; 204 int intr_offset; 205 int intr_nbits; 206 } intr_t; 207 208 #define intr_iformat _u._iformat 209 #define intr_fformat _u._fformat 210 211 typedef struct fnarg { 212 char *fna_name; 213 struct tdesc *fna_type; 214 } fnarg_t; 215 216 #define FN_F_GLOBAL 0x1 217 #define FN_F_VARARGS 0x2 218 219 typedef struct fndef { 220 struct tdesc *fn_ret; 221 uint_t fn_nargs; 222 tdesc_t **fn_args; 223 uint_t fn_vargs; 224 } fndef_t; 225 226 typedef int32_t tid_t; 227 228 /* 229 * The tdesc_t (Type DESCription) is the basic node type used in the stabs data 230 * structure. Each data node gets a tdesc structure. Each node is linked into 231 * a directed graph (think of it as a tree with multiple roots and multiple 232 * leaves), with the root nodes at the top, and intrinsics at the bottom. The 233 * root nodes, which are pointed to by iidesc nodes, correspond to the types, 234 * globals, and statics defined by the stabs. 235 */ 236 struct tdesc { 237 char *t_name; 238 tdesc_t *t_next; /* Name hash next pointer */ 239 240 tid_t t_id; 241 tdesc_t *t_hash; /* ID hash next pointer */ 242 243 stabtype_t t_type; 244 int t_size; /* Size in bytes of object represented by this node */ 245 246 union { 247 intr_t *intr; /* int, real */ 248 tdesc_t *tdesc; /* ptr, typedef, vol, const, restr */ 249 ardef_t *ardef; /* array */ 250 mlist_t *members; /* struct, union */ 251 elist_t *emem; /* enum */ 252 fndef_t *fndef; /* function - first is return type */ 253 } t_data; 254 255 int t_flags; 256 int t_vgen; /* Visitation generation (see traverse.c) */ 257 int t_emark; /* Equality mark (see equiv_cb() in merge.c) */ 258 }; 259 260 #define t_intr t_data.intr 261 #define t_tdesc t_data.tdesc 262 #define t_ardef t_data.ardef 263 #define t_members t_data.members 264 #define t_emem t_data.emem 265 #define t_fndef t_data.fndef 266 267 #define TDESC_F_ISROOT 0x1 /* Has an iidesc_t (see below) */ 268 #define TDESC_F_GLOBAL 0x2 269 #define TDESC_F_RESOLVED 0x4 270 271 /* 272 * iidesc_t (Interesting Item DESCription) nodes point to tdesc_t nodes that 273 * correspond to "interesting" stabs. A stab is interesting if it defines a 274 * global or static variable, a global or static function, or a data type. 275 */ 276 typedef enum iitype { 277 II_NOT = 0, 278 II_GFUN, /* Global function */ 279 II_SFUN, /* Static function */ 280 II_GVAR, /* Global variable */ 281 II_SVAR, /* Static variable */ 282 II_PSYM, /* Function argument */ 283 II_SOU, /* Struct or union */ 284 II_TYPE /* Type (typedef) */ 285 } iitype_t; 286 287 typedef struct iidesc { 288 iitype_t ii_type; 289 char *ii_name; 290 tdesc_t *ii_dtype; 291 char *ii_owner; /* File that defined this node */ 292 int ii_flags; 293 294 /* Function arguments (if any) */ 295 int ii_nargs; 296 tdesc_t **ii_args; 297 int ii_vargs; /* Function uses varargs */ 298 } iidesc_t; 299 300 #define IIDESC_F_USED 0x1 /* Write this iidesc out */ 301 302 /* 303 * labelent_t nodes identify labels and corresponding type ranges associated 304 * with them. The label in a given labelent_t is associated with types with 305 * ids <= le_idx. 306 */ 307 typedef struct labelent { 308 char *le_name; 309 int le_idx; 310 } labelent_t; 311 312 /* 313 * The tdata_t (Type DATA) structure contains or references all type data for 314 * a given file or, during merging, several files. 315 */ 316 typedef struct tdata { 317 int td_curemark; /* Equality mark (see merge.c) */ 318 int td_curvgen; /* Visitation generation (see traverse.c) */ 319 int td_nextid; /* The ID for the next tdesc_t created */ 320 hash_t *td_iihash; /* The iidesc_t nodes for this file */ 321 322 hash_t *td_layouthash; /* The tdesc nodes, hashed by structure */ 323 hash_t *td_idhash; /* The tdesc nodes, hashed by type id */ 324 list_t *td_fwdlist; /* All forward declaration tdesc nodes */ 325 326 char *td_parlabel; /* Top label uniq'd against in parent */ 327 char *td_parname; /* Basename of parent */ 328 list_t *td_labels; /* Labels and their type ranges */ 329 330 pthread_mutex_t td_mergelock; 331 332 int td_ref; 333 } tdata_t; 334 335 /* 336 * By design, the iidesc hash is heterogeneous. The CTF emitter, on the 337 * other hand, needs to be able to access the elements of the list by type, 338 * and in a specific sorted order. An iiburst holds these elements in that 339 * order. (A burster is a machine that separates carbon-copy forms) 340 */ 341 typedef struct iiburst { 342 int iib_nfuncs; 343 int iib_curfunc; 344 iidesc_t **iib_funcs; 345 346 int iib_nobjts; 347 int iib_curobjt; 348 iidesc_t **iib_objts; 349 350 list_t *iib_types; 351 int iib_maxtypeid; 352 353 tdata_t *iib_td; 354 struct tdtrav_data *iib_tdtd; /* tdtrav_data_t */ 355 } iiburst_t; 356 357 typedef struct ctf_buf ctf_buf_t; 358 359 typedef struct symit_data symit_data_t; 360 361 /* fixup_tdescs.c */ 362 void cvt_fixstabs(tdata_t *); 363 void cvt_fixups(tdata_t *, size_t); 364 365 /* ctf.c */ 366 caddr_t ctf_gen(iiburst_t *, size_t *, int); 367 tdata_t *ctf_load(char *, caddr_t, size_t, symit_data_t *, char *); 368 369 /* iidesc.c */ 370 iidesc_t *iidesc_new(char *); 371 int iidesc_hash(int, void *); 372 void iter_iidescs_by_name(tdata_t *, const char *, 373 int (*)(void *, void *), void *); 374 iidesc_t *iidesc_dup(iidesc_t *); 375 iidesc_t *iidesc_dup_rename(iidesc_t *, char const *, char const *); 376 void iidesc_add(hash_t *, iidesc_t *); 377 void iidesc_free(void *, void *); 378 int iidesc_count_type(void *, void *); 379 void iidesc_stats(hash_t *); 380 int iidesc_dump(iidesc_t *); 381 382 /* input.c */ 383 typedef enum source_types { 384 SOURCE_NONE = 0, 385 SOURCE_UNKNOWN = 1, 386 SOURCE_C = 2, 387 SOURCE_S = 4 388 } source_types_t; 389 390 source_types_t built_source_types(Elf *, const char *); 391 int count_files(char **, int); 392 int read_ctf(char **, int, char *, int (*)(tdata_t *, char *, void *), 393 void *, int); 394 int read_ctf_save_cb(tdata_t *, char *, void *); 395 symit_data_t *symit_new(Elf *, const char *); 396 void symit_reset(symit_data_t *); 397 char *symit_curfile(symit_data_t *); 398 GElf_Sym *symit_next(symit_data_t *, int); 399 char *symit_name(symit_data_t *); 400 void symit_free(symit_data_t *); 401 402 /* merge.c */ 403 void merge_into_master(tdata_t *, tdata_t *, tdata_t *, int); 404 405 /* output.c */ 406 #define CTF_FUZZY_MATCH 0x1 /* match local symbols to global CTF */ 407 #define CTF_USE_DYNSYM 0x2 /* use .dynsym not .symtab */ 408 #define CTF_COMPRESS 0x4 /* compress CTF output */ 409 #define CTF_KEEP_STABS 0x8 /* keep .stabs sections */ 410 #define CTF_SWAP_BYTES 0x10 /* target byte order is different from host */ 411 412 void write_ctf(tdata_t *, const char *, const char *, int); 413 414 /* parse.c */ 415 void parse_init(tdata_t *); 416 void parse_finish(tdata_t *); 417 int parse_stab(stab_t *, char *, iidesc_t **); 418 tdesc_t *lookup(int); 419 tdesc_t *lookupname(const char *); 420 void check_hash(void); 421 void resolve_typed_bitfields(void); 422 423 /* stabs.c */ 424 int stabs_read(tdata_t *, Elf *, char *); 425 426 /* dwarf.c */ 427 int dw_read(tdata_t *, Elf *, char *); 428 const char *dw_tag2str(uint_t); 429 430 /* tdata.c */ 431 tdata_t *tdata_new(void); 432 void tdata_free(tdata_t *); 433 void tdata_build_hashes(tdata_t *td); 434 const char *tdesc_name(tdesc_t *); 435 int tdesc_idhash(int, void *); 436 int tdesc_idcmp(void *, void *); 437 int tdesc_namehash(int, void *); 438 int tdesc_namecmp(void *, void *); 439 int tdesc_layouthash(int, void *); 440 int tdesc_layoutcmp(void *, void *); 441 void tdesc_free(tdesc_t *); 442 void tdata_label_add(tdata_t *, const char *, int); 443 labelent_t *tdata_label_top(tdata_t *); 444 int tdata_label_find(tdata_t *, char *); 445 void tdata_label_free(tdata_t *); 446 void tdata_merge(tdata_t *, tdata_t *); 447 void tdata_label_newmax(tdata_t *, int); 448 449 /* util.c */ 450 int streq(const char *, const char *); 451 int findelfsecidx(Elf *, const char *, const char *); 452 size_t elf_ptrsz(Elf *); 453 char *mktmpname(const char *, const char *); 454 void terminate(const char *, ...) __printflike(1, 2) __dead; 455 void aborterr(const char *, ...) __printflike(1, 2) __dead; 456 void set_terminate_cleanup(void (*)(void)); 457 void elfterminate(const char *, const char *, ...) __printflike(2, 3) __dead; 458 void warning(const char *, ...) __printflike(1, 2); 459 void vadebug(int, const char *, va_list) __printflike(2, 0); 460 void debug(int, const char *, ...) __printflike(2, 3); 461 462 463 void watch_dump(int); 464 void watch_set(void *, int); 465 466 #ifdef __cplusplus 467 } 468 #endif 469 470 #endif /* _CTFTOOLS_H */ 471