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