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