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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
30 * Functions and data structures used in the manipulation of stabs and CTF data
40 #include <sys/ccompile.h>
41 #include <sys/endian.h>
55 #define DEBUG_STREAM stderr
59 #define MAX(a, b) ((a) < (b) ? (b) : (a))
63 #define MIN(a, b) ((a) > (b) ? (b) : (a))
66 /* Sanity check for cross-build bootstrap tools */
67 #if !defined(BYTE_ORDER)
68 #error "Missing BYTE_ORDER defines"
69 #elif !defined(_LITTLE_ENDIAN)
70 #error "Missing _LITTLE_ENDIAN defines"
71 #elif !defined(_BIG_ENDIAN)
72 #error "Missing _BIG_ENDIAN defines"
78 #define CTF_ELF_SCN_NAME ".SUNW_ctf"
80 #define CTF_LABEL_LASTIDX -1
82 #define CTF_DEFAULT_LABEL "*** No Label Provided ***"
87 #define TDATA_LAYOUT_HASH_SIZE 8191 /* A tdesc hash based on layout */
88 #define TDATA_ID_HASH_SIZE 997 /* A tdesc hash based on type id */
89 #define IIDESC_HASH_SIZE 8191 /* Hash of iidesc's */
92 * The default function argument array size. We'll realloc the array larger
93 * if we need to, but we want a default value that will allow us to avoid
94 * reallocation in the common case.
98 extern const char *progname;
99 extern int debug_level;
103 * This is a partial copy of the stab.h that DevPro includes with their
106 typedef struct stab {
114 #define N_GSYM 0x20 /* global symbol: name,,0,type,0 */
115 #define N_FUN 0x24 /* procedure: name,,0,linenumber,0 */
116 #define N_STSYM 0x26 /* static symbol: name,,0,type,0 or section relative */
117 #define N_LCSYM 0x28 /* .lcomm symbol: name,,0,type,0 or section relative */
118 #define N_ROSYM 0x2c /* ro_data: name,,0,type,0 or section relative */
119 #define N_OPT 0x3c /* compiler options */
120 #define N_RSYM 0x40 /* register sym: name,,0,type,register */
121 #define N_SO 0x64 /* source file name: name,,0,0,0 */
122 #define N_LSYM 0x80 /* local sym: name,,0,type,offset */
123 #define N_SOL 0x84 /* #included file name: name,,0,0,0 */
124 #define N_PSYM 0xa0 /* parameter: name,,0,type,offset */
125 #define N_LBRAC 0xc0 /* left bracket: 0,,0,nesting level,function relative */
126 #define N_RBRAC 0xe0 /* right bracket: 0,,0,nesting level,func relative */
127 #define N_BINCL 0x82 /* header file: name,,0,0,0 */
128 #define N_EINCL 0xa2 /* end of include file */
131 * Nodes in the type tree
133 * Each node consists of a single tdesc_t, with one of several auxiliary
134 * structures linked in via the `data' union.
137 /* The type of tdesc_t node */
138 typedef enum stabtype {
139 STABTYPE_FIRST, /* do not use */
153 STABTYPE_LAST /* do not use */
156 typedef struct tdesc tdesc_t;
158 /* Auxiliary structure for array tdesc_t */
159 typedef struct ardef {
160 tdesc_t *ad_contents;
165 /* Auxiliary structure for structure/union tdesc_t */
166 typedef struct mlist {
167 int ml_offset; /* Offset from start of structure (in bits) */
168 int ml_size; /* Member size (in bits) */
169 char *ml_name; /* Member name */
170 struct tdesc *ml_type; /* Member type */
171 struct mlist *ml_next; /* Next member */
174 /* Auxiliary structure for enum tdesc_t */
175 typedef struct elist {
178 struct elist *el_next;
181 /* Auxiliary structure for intrinsics (integers and reals) */
187 typedef struct intr {
188 intrtype_t intr_type;
198 #define intr_iformat _u._iformat
199 #define intr_fformat _u._fformat
201 typedef struct fnarg {
203 struct tdesc *fna_type;
206 #define FN_F_GLOBAL 0x1
207 #define FN_F_VARARGS 0x2
209 typedef struct fndef {
210 struct tdesc *fn_ret;
216 typedef int32_t tid_t;
219 * The tdesc_t (Type DESCription) is the basic node type used in the stabs data
220 * structure. Each data node gets a tdesc structure. Each node is linked into
221 * a directed graph (think of it as a tree with multiple roots and multiple
222 * leaves), with the root nodes at the top, and intrinsics at the bottom. The
223 * root nodes, which are pointed to by iidesc nodes, correspond to the types,
224 * globals, and statics defined by the stabs.
228 tdesc_t *t_next; /* Name hash next pointer */
231 tdesc_t *t_hash; /* ID hash next pointer */
234 int t_size; /* Size in bytes of object represented by this node */
237 intr_t *intr; /* int, real */
238 tdesc_t *tdesc; /* ptr, typedef, vol, const, restr */
239 ardef_t *ardef; /* array */
240 mlist_t *members; /* struct, union */
241 elist_t *emem; /* enum */
242 fndef_t *fndef; /* function - first is return type */
246 int t_vgen; /* Visitation generation (see traverse.c) */
247 int t_emark; /* Equality mark (see equiv_cb() in merge.c) */
250 #define t_intr t_data.intr
251 #define t_tdesc t_data.tdesc
252 #define t_ardef t_data.ardef
253 #define t_members t_data.members
254 #define t_emem t_data.emem
255 #define t_fndef t_data.fndef
257 #define TDESC_F_ISROOT 0x1 /* Has an iidesc_t (see below) */
258 #define TDESC_F_GLOBAL 0x2
259 #define TDESC_F_RESOLVED 0x4
262 * iidesc_t (Interesting Item DESCription) nodes point to tdesc_t nodes that
263 * correspond to "interesting" stabs. A stab is interesting if it defines a
264 * global or static variable, a global or static function, or a data type.
266 typedef enum iitype {
268 II_GFUN, /* Global function */
269 II_SFUN, /* Static function */
270 II_GVAR, /* Global variable */
271 II_SVAR, /* Static variable */
272 II_PSYM, /* Function argument */
273 II_SOU, /* Struct or union */
274 II_TYPE /* Type (typedef) */
277 typedef struct iidesc {
281 char *ii_owner; /* File that defined this node */
284 /* Function arguments (if any) */
287 int ii_vargs; /* Function uses varargs */
290 #define IIDESC_F_USED 0x1 /* Write this iidesc out */
293 * labelent_t nodes identify labels and corresponding type ranges associated
294 * with them. The label in a given labelent_t is associated with types with
297 typedef struct labelent {
303 * The tdata_t (Type DATA) structure contains or references all type data for
304 * a given file or, during merging, several files.
306 typedef struct tdata {
307 int td_curemark; /* Equality mark (see merge.c) */
308 int td_curvgen; /* Visitation generation (see traverse.c) */
309 int td_nextid; /* The ID for the next tdesc_t created */
310 hash_t *td_iihash; /* The iidesc_t nodes for this file */
312 hash_t *td_layouthash; /* The tdesc nodes, hashed by structure */
313 hash_t *td_idhash; /* The tdesc nodes, hashed by type id */
314 list_t *td_fwdlist; /* All forward declaration tdesc nodes */
316 char *td_parlabel; /* Top label uniq'd against in parent */
317 char *td_parname; /* Basename of parent */
318 list_t *td_labels; /* Labels and their type ranges */
320 pthread_mutex_t td_mergelock;
326 * By design, the iidesc hash is heterogeneous. The CTF emitter, on the
327 * other hand, needs to be able to access the elements of the list by type,
328 * and in a specific sorted order. An iiburst holds these elements in that
329 * order. (A burster is a machine that separates carbon-copy forms)
331 typedef struct iiburst {
334 iidesc_t **iib_funcs;
338 iidesc_t **iib_objts;
344 struct tdtrav_data *iib_tdtd; /* tdtrav_data_t */
347 typedef struct ctf_buf ctf_buf_t;
349 typedef struct symit_data symit_data_t;
352 void cvt_fixstabs(tdata_t *);
353 void cvt_fixups(tdata_t *, size_t);
356 caddr_t ctf_gen(iiburst_t *, size_t *, int);
357 tdata_t *ctf_load(char *, caddr_t, size_t, symit_data_t *, char *);
360 iidesc_t *iidesc_new(char *);
361 int iidesc_hash(int, void *);
362 void iter_iidescs_by_name(tdata_t *, const char *,
363 int (*)(void *, void *), void *);
364 iidesc_t *iidesc_dup(iidesc_t *);
365 iidesc_t *iidesc_dup_rename(iidesc_t *, char const *, char const *);
366 void iidesc_add(hash_t *, iidesc_t *);
367 void iidesc_free(void *, void *);
368 int iidesc_count_type(void *, void *);
369 void iidesc_stats(hash_t *);
370 int iidesc_dump(iidesc_t *);
373 typedef enum source_types {
380 source_types_t built_source_types(Elf *, const char *);
381 int count_files(char **, int);
382 int read_ctf(char **, int, char *, int (*)(tdata_t *, char *, void *),
384 int read_ctf_save_cb(tdata_t *, char *, void *);
385 symit_data_t *symit_new(Elf *, const char *);
386 void symit_reset(symit_data_t *);
387 char *symit_curfile(symit_data_t *);
388 GElf_Sym *symit_next(symit_data_t *, int);
389 char *symit_name(symit_data_t *);
390 void symit_free(symit_data_t *);
393 void merge_into_master(tdata_t *, tdata_t *, tdata_t *, int);
396 #define CTF_FUZZY_MATCH 0x1 /* match local symbols to global CTF */
397 #define CTF_USE_DYNSYM 0x2 /* use .dynsym not .symtab */
398 #define CTF_COMPRESS 0x4 /* compress CTF output */
399 #define CTF_KEEP_STABS 0x8 /* keep .stabs sections */
400 #define CTF_SWAP_BYTES 0x10 /* target byte order is different from host */
402 void write_ctf(tdata_t *, const char *, const char *, int);
405 int dw_read(tdata_t *, Elf *, char *);
406 const char *dw_tag2str(uint_t);
409 tdata_t *tdata_new(void);
410 void tdata_free(tdata_t *);
411 void tdata_build_hashes(tdata_t *td);
412 const char *tdesc_name(tdesc_t *);
413 int tdesc_idhash(int, void *);
414 int tdesc_idcmp(void *, void *);
415 int tdesc_namehash(int, void *);
416 int tdesc_namecmp(void *, void *);
417 int tdesc_layouthash(int, void *);
418 int tdesc_layoutcmp(void *, void *);
419 void tdesc_free(tdesc_t *);
420 void tdata_label_add(tdata_t *, const char *, int);
421 labelent_t *tdata_label_top(tdata_t *);
422 int tdata_label_find(tdata_t *, char *);
423 void tdata_label_free(tdata_t *);
424 void tdata_merge(tdata_t *, tdata_t *);
425 void tdata_label_newmax(tdata_t *, int);
428 int streq(const char *, const char *);
429 int findelfsecidx(Elf *, const char *, const char *);
430 size_t elf_ptrsz(Elf *);
431 char *mktmpname(const char *, const char *);
432 void terminate(const char *, ...) __NORETURN;
433 void aborterr(const char *, ...) __NORETURN;
434 void set_terminate_cleanup(void (*)(void));
435 void elfterminate(const char *, const char *, ...);
436 void warning(const char *, ...);
437 void vadebug(int, const char *, va_list);
438 void debug(int, const char *, ...);
441 void watch_dump(int);
442 void watch_set(void *, int);
448 #endif /* _CTFTOOLS_H */