dwarf.c revision 1.12 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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * DWARF to tdata conversion
28 *
29 * For the most part, conversion is straightforward, proceeding in two passes.
30 * On the first pass, we iterate through every die, creating new type nodes as
31 * necessary. Referenced tdesc_t's are created in an uninitialized state, thus
32 * allowing type reference pointers to be filled in. If the tdesc_t
33 * corresponding to a given die can be completely filled out (sizes and offsets
34 * calculated, and so forth) without using any referenced types, the tdesc_t is
35 * marked as resolved. Consider an array type. If the type corresponding to
36 * the array contents has not yet been processed, we will create a blank tdesc
37 * for the contents type (only the type ID will be filled in, relying upon the
38 * later portion of the first pass to encounter and complete the referenced
39 * type). We will then attempt to determine the size of the array. If the
40 * array has a byte size attribute, we will have completely characterized the
41 * array type, and will be able to mark it as resolved. The lack of a byte
42 * size attribute, on the other hand, will prevent us from fully resolving the
43 * type, as the size will only be calculable with reference to the contents
44 * type, which has not, as yet, been encountered. The array type will thus be
45 * left without the resolved flag, and the first pass will continue.
46 *
47 * When we begin the second pass, we will have created tdesc_t nodes for every
48 * type in the section. We will traverse the tree, from the iidescs down,
49 * processing each unresolved node. As the referenced nodes will have been
50 * populated, the array type used in our example above will be able to use the
51 * size of the referenced types (if available) to determine its own type. The
52 * traversal will be repeated until all types have been resolved or we have
53 * failed to make progress. When all tdescs have been resolved, the conversion
54 * is complete.
55 *
56 * There are, as always, a few special cases that are handled during the first
57 * and second passes:
58 *
59 * 1. Empty enums - GCC will occasionally emit an enum without any members.
60 * Later on in the file, it will emit the same enum type, though this time
61 * with the full complement of members. All references to the memberless
62 * enum need to be redirected to the full definition. During the first
63 * pass, each enum is entered in dm_enumhash, along with a pointer to its
64 * corresponding tdesc_t. If, during the second pass, we encounter a
65 * memberless enum, we use the hash to locate the full definition. All
66 * tdescs referencing the empty enum are then redirected.
67 *
68 * 2. Forward declarations - If the compiler sees a forward declaration for
69 * a structure, followed by the definition of that structure, it will emit
70 * DWARF data for both the forward declaration and the definition. We need
71 * to resolve the forward declarations when possible, by redirecting
72 * forward-referencing tdescs to the actual struct/union definitions. This
73 * redirection is done completely within the first pass. We begin by
74 * recording all forward declarations in dw_fwdhash. When we define a
75 * structure, we check to see if there have been any corresponding forward
76 * declarations. If so, we redirect the tdescs which referenced the forward
77 * declarations to the structure or union definition.
78 *
79 * XXX see if a post traverser will allow the elimination of repeated pass 2
80 * traversals.
81 */
82
83 #if HAVE_NBTOOL_CONFIG_H
84 # include "nbtool_config.h"
85 #endif
86
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <strings.h>
91 #include <errno.h>
92 #include <libelf.h>
93 #include <libdwarf.h>
94 #include <libgen.h>
95 #include <dwarf.h>
96
97 #include "ctf_headers.h"
98 #include "ctftools.h"
99 #include "memory.h"
100 #include "list.h"
101 #include "traverse.h"
102
103 /*
104 * We need to define a couple of our own intrinsics, to smooth out some of the
105 * differences between the GCC and DevPro DWARF emitters. See the referenced
106 * routines and the special cases in the file comment for more details.
107 *
108 * Type IDs are 32 bits wide. We're going to use the top of that field to
109 * indicate types that we've created ourselves.
110 */
111 #define TID_FILEMAX 0x3fffffff /* highest tid from file */
112 #define TID_VOID 0x40000001 /* see die_void() */
113 #define TID_LONG 0x40000002 /* see die_array() */
114
115 #define TID_MFGTID_BASE 0x40000003 /* first mfg'd tid */
116
117 /*
118 * To reduce the staggering amount of error-handling code that would otherwise
119 * be required, the attribute-retrieval routines handle most of their own
120 * errors. If the following flag is supplied as the value of the `req'
121 * argument, they will also handle the absence of a requested attribute by
122 * terminating the program.
123 */
124 #define DW_ATTR_REQ 1
125
126 #define TDESC_HASH_BUCKETS 511
127
128 typedef struct dwarf {
129 Dwarf_Debug dw_dw; /* for libdwarf */
130 Dwarf_Error dw_err; /* for libdwarf */
131 Dwarf_Off dw_maxoff; /* highest legal offset in this cu */
132 tdata_t *dw_td; /* root of the tdesc/iidesc tree */
133 hash_t *dw_tidhash; /* hash of tdescs by t_id */
134 hash_t *dw_fwdhash; /* hash of fwd decls by name */
135 hash_t *dw_enumhash; /* hash of memberless enums by name */
136 tdesc_t *dw_void; /* manufactured void type */
137 tdesc_t *dw_long; /* manufactured long type for arrays */
138 size_t dw_ptrsz; /* size of a pointer in this file */
139 tid_t dw_mfgtid_last; /* last mfg'd type ID used */
140 uint_t dw_nunres; /* count of unresolved types */
141 char *dw_cuname; /* name of compilation unit */
142 } dwarf_t;
143
144 static void die_create_one(dwarf_t *, Dwarf_Die);
145 static void die_create(dwarf_t *, Dwarf_Die);
146
147 static tid_t
148 mfgtid_next(dwarf_t *dw)
149 {
150 return (++dw->dw_mfgtid_last);
151 }
152
153 static void
154 tdesc_add(dwarf_t *dw, tdesc_t *tdp)
155 {
156 hash_add(dw->dw_tidhash, tdp);
157 }
158
159 static tdesc_t *
160 tdesc_lookup(dwarf_t *dw, int tid)
161 {
162 tdesc_t tmpl;
163 void *tdp;
164
165 tmpl.t_id = tid;
166
167 if (hash_find(dw->dw_tidhash, &tmpl, &tdp))
168 return (tdp);
169 else
170 return (NULL);
171 }
172
173 /*
174 * Resolve a tdesc down to a node which should have a size. Returns the size,
175 * zero if the size hasn't yet been determined.
176 */
177 static size_t
178 tdesc_size(tdesc_t *tdp)
179 {
180 for (;;) {
181 switch (tdp->t_type) {
182 case INTRINSIC:
183 case POINTER:
184 case ARRAY:
185 case FUNCTION:
186 case STRUCT:
187 case UNION:
188 case ENUM:
189 return (tdp->t_size);
190
191 case FORWARD:
192 return (0);
193
194 case TYPEDEF:
195 case VOLATILE:
196 case CONST:
197 case RESTRICT:
198 tdp = tdp->t_tdesc;
199 continue;
200
201 case 0: /* not yet defined */
202 return (0);
203
204 default:
205 terminate("tdp %u: tdesc_size on unknown type %d\n",
206 tdp->t_id, tdp->t_type);
207 }
208 }
209 }
210
211 static size_t
212 tdesc_bitsize(tdesc_t *tdp)
213 {
214 for (;;) {
215 switch (tdp->t_type) {
216 case INTRINSIC:
217 return (tdp->t_intr->intr_nbits);
218
219 case ARRAY:
220 case FUNCTION:
221 case STRUCT:
222 case UNION:
223 case ENUM:
224 case POINTER:
225 return (tdp->t_size * NBBY);
226
227 case FORWARD:
228 return (0);
229
230 case TYPEDEF:
231 case VOLATILE:
232 case RESTRICT:
233 case CONST:
234 tdp = tdp->t_tdesc;
235 continue;
236
237 case 0: /* not yet defined */
238 return (0);
239
240 default:
241 terminate("tdp %u: tdesc_bitsize on unknown type %d\n",
242 tdp->t_id, tdp->t_type);
243 }
244 }
245 }
246
247 static tdesc_t *
248 tdesc_basetype(tdesc_t *tdp)
249 {
250 for (;;) {
251 switch (tdp->t_type) {
252 case TYPEDEF:
253 case VOLATILE:
254 case RESTRICT:
255 case CONST:
256 tdp = tdp->t_tdesc;
257 break;
258 case 0: /* not yet defined */
259 return (NULL);
260 default:
261 return (tdp);
262 }
263 }
264 }
265
266 static Dwarf_Off
267 die_off(dwarf_t *dw, Dwarf_Die die)
268 {
269 Dwarf_Off off;
270
271 if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK)
272 return (off);
273
274 terminate("failed to get offset for die: %s\n",
275 dwarf_errmsg(dw->dw_err));
276 /*NOTREACHED*/
277 return (0);
278 }
279
280 static Dwarf_Die
281 die_sibling(dwarf_t *dw, Dwarf_Die die)
282 {
283 Dwarf_Die sib;
284 int rc;
285
286 if ((rc = dwarf_siblingof(dw->dw_dw, die, &sib, &dw->dw_err)) ==
287 DW_DLV_OK)
288 return (sib);
289 else if (rc == DW_DLV_NO_ENTRY)
290 return (NULL);
291
292 terminate("die %ju: failed to find type sibling: %s\n",
293 (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
294 /*NOTREACHED*/
295 return (NULL);
296 }
297
298 static Dwarf_Die
299 die_child(dwarf_t *dw, Dwarf_Die die)
300 {
301 Dwarf_Die child;
302 int rc;
303
304 if ((rc = dwarf_child(die, &child, &dw->dw_err)) == DW_DLV_OK)
305 return (child);
306 else if (rc == DW_DLV_NO_ENTRY)
307 return (NULL);
308
309 terminate("die %ju: failed to find type child: %s\n",
310 (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
311 /*NOTREACHED*/
312 return (NULL);
313 }
314
315 static Dwarf_Half
316 die_tag(dwarf_t *dw, Dwarf_Die die)
317 {
318 Dwarf_Half tag;
319
320 if (dwarf_tag(die, &tag, &dw->dw_err) == DW_DLV_OK)
321 return (tag);
322
323 terminate("die %ju: failed to get tag for type: %s\n",
324 (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
325 /*NOTREACHED*/
326 return (0);
327 }
328
329 static Dwarf_Attribute
330 die_attr(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, int req)
331 {
332 Dwarf_Attribute attr;
333 int rc;
334
335 if ((rc = dwarf_attr(die, name, &attr, &dw->dw_err)) == DW_DLV_OK) {
336 return (attr);
337 } else if (rc == DW_DLV_NO_ENTRY) {
338 if (req) {
339 terminate("die %ju: no attr 0x%x\n",
340 (uintmax_t)die_off(dw, die),
341 name);
342 } else {
343 return (NULL);
344 }
345 }
346
347 terminate("die %ju: failed to get attribute for type: %s\n",
348 (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
349 /*NOTREACHED*/
350 return (NULL);
351 }
352
353 static int
354 die_signed(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Signed *valp,
355 int req)
356 {
357 *valp = 0;
358 if (dwarf_attrval_signed(die, name, valp, &dw->dw_err) != DW_DLV_OK) {
359 if (req)
360 terminate("die %ju: failed to get signed: %s\n",
361 (uintmax_t)die_off(dw, die),
362 dwarf_errmsg(dw->dw_err));
363 return (0);
364 }
365
366 return (1);
367 }
368
369 static int
370 die_unsigned(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Unsigned *valp,
371 int req)
372 {
373 *valp = 0;
374 if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) != DW_DLV_OK) {
375 if (req)
376 terminate("die %ju: failed to get unsigned: %s\n",
377 (uintmax_t)die_off(dw, die),
378 dwarf_errmsg(dw->dw_err));
379 return (0);
380 }
381
382 return (1);
383 }
384
385 static int
386 die_bool(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Bool *valp, int req)
387 {
388 *valp = 0;
389
390 if (dwarf_attrval_flag(die, name, valp, &dw->dw_err) != DW_DLV_OK) {
391 if (req)
392 terminate("die %ju: failed to get flag: %s\n",
393 (uintmax_t)die_off(dw, die),
394 dwarf_errmsg(dw->dw_err));
395 return (0);
396 }
397
398 return (1);
399 }
400
401 static int
402 die_string(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, char **strp, int req)
403 {
404 const char *str = NULL;
405
406 if (dwarf_attrval_string(die, name, &str, &dw->dw_err) != DW_DLV_OK ||
407 str == NULL) {
408 if (req)
409 terminate("die %ju: failed to get string: %s\n",
410 (uintmax_t)die_off(dw, die),
411 dwarf_errmsg(dw->dw_err));
412 else
413 *strp = NULL;
414 return (0);
415 } else
416 *strp = xstrdup(str);
417
418 return (1);
419 }
420
421 static Dwarf_Off
422 die_attr_ref(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
423 {
424 Dwarf_Unsigned off;
425
426 if (dwarf_attrval_unsigned(die, name, &off, &dw->dw_err) != DW_DLV_OK) {
427 terminate("die %ju: failed to get ref: %s\n",
428 (uintmax_t)die_off(dw, die), dwarf_errmsg(dw->dw_err));
429 }
430
431 return (off);
432 }
433
434 static char *
435 die_name(dwarf_t *dw, Dwarf_Die die)
436 {
437 char *str = NULL;
438
439 (void) die_string(dw, die, DW_AT_name, &str, 0);
440 if (str == NULL)
441 str = xstrdup("");
442
443 return (str);
444 }
445
446 static int
447 die_isdecl(dwarf_t *dw, Dwarf_Die die)
448 {
449 Dwarf_Bool val;
450
451 return (die_bool(dw, die, DW_AT_declaration, &val, 0) && val);
452 }
453
454 static int
455 die_isglobal(dwarf_t *dw, Dwarf_Die die)
456 {
457 Dwarf_Signed vis;
458 Dwarf_Bool ext;
459
460 /*
461 * Some compilers (gcc) use DW_AT_external to indicate function
462 * visibility. Others (Sun) use DW_AT_visibility.
463 */
464 if (die_signed(dw, die, DW_AT_visibility, &vis, 0))
465 return (vis == DW_VIS_exported);
466 else
467 return (die_bool(dw, die, DW_AT_external, &ext, 0) && ext);
468 }
469
470 static tdesc_t *
471 die_add(dwarf_t *dw, Dwarf_Off off)
472 {
473 tdesc_t *tdp = xcalloc(sizeof (tdesc_t));
474
475 tdp->t_id = off;
476
477 tdesc_add(dw, tdp);
478
479 return (tdp);
480 }
481
482 static tdesc_t *
483 die_lookup_pass1(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
484 {
485 Dwarf_Off ref = die_attr_ref(dw, die, name);
486 tdesc_t *tdp;
487
488 if ((tdp = tdesc_lookup(dw, ref)) != NULL)
489 return (tdp);
490
491 return (die_add(dw, ref));
492 }
493
494 static int
495 die_mem_offset(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name,
496 Dwarf_Unsigned *valp, int req __unused)
497 {
498 Dwarf_Locdesc *loc = NULL;
499 Dwarf_Signed locnum = 0;
500 Dwarf_Attribute at;
501 Dwarf_Half form;
502
503 if (name != DW_AT_data_member_location)
504 terminate("die %ju: can only process attribute "
505 "DW_AT_data_member_location\n",
506 (uintmax_t)die_off(dw, die));
507
508 if ((at = die_attr(dw, die, name, 0)) == NULL)
509 return (0);
510
511 if (dwarf_whatform(at, &form, &dw->dw_err) != DW_DLV_OK)
512 return (0);
513
514 switch (form) {
515 case DW_FORM_sec_offset:
516 case DW_FORM_block:
517 case DW_FORM_block1:
518 case DW_FORM_block2:
519 case DW_FORM_block4:
520 /*
521 * GCC in base and Clang (3.3 or below) generates
522 * DW_AT_data_member_location attribute with DW_FORM_block*
523 * form. The attribute contains one DW_OP_plus_uconst
524 * operator. The member offset stores in the operand.
525 */
526 if (dwarf_loclist(at, &loc, &locnum, &dw->dw_err) != DW_DLV_OK)
527 return (0);
528 if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {
529 terminate("die %ju: cannot parse member offset with "
530 "operator other than DW_OP_plus_uconst\n",
531 (uintmax_t)die_off(dw, die));
532 }
533 *valp = loc->ld_s->lr_number;
534 if (loc != NULL) {
535 dwarf_dealloc(dw->dw_dw, loc->ld_s, DW_DLA_LOC_BLOCK);
536 dwarf_dealloc(dw->dw_dw, loc, DW_DLA_LOCDESC);
537 }
538 break;
539
540 case DW_FORM_data1:
541 case DW_FORM_data2:
542 case DW_FORM_data4:
543 case DW_FORM_data8:
544 case DW_FORM_udata:
545 /*
546 * Clang 3.4 generates DW_AT_data_member_location attribute
547 * with DW_FORM_data* form (constant class). The attribute
548 * stores a contant value which is the member offset.
549 *
550 * However, note that DW_FORM_data[48] in DWARF version 2 or 3
551 * could be used as a section offset (offset into .debug_loc in
552 * this case). Here we assume the attribute always stores a
553 * constant because we know Clang 3.4 does this and GCC in
554 * base won't emit DW_FORM_data[48] for this attribute. This
555 * code will remain correct if future vesrions of Clang and
556 * GCC conform to DWARF4 standard and only use the form
557 * DW_FORM_sec_offset for section offset.
558 */
559 if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) !=
560 DW_DLV_OK)
561 return (0);
562 break;
563
564 default:
565 terminate("die %ju: cannot parse member offset with form "
566 "%u\n", (uintmax_t)die_off(dw, die), form);
567 }
568
569 return (1);
570 }
571
572 static tdesc_t *
573 tdesc_intr_common(dwarf_t *dw, int tid, const char *name, size_t sz)
574 {
575 tdesc_t *tdp;
576 intr_t *intr;
577
578 intr = xcalloc(sizeof (intr_t));
579 intr->intr_type = INTR_INT;
580 intr->intr_signed = 1;
581 intr->intr_nbits = sz * NBBY;
582
583 tdp = xcalloc(sizeof (tdesc_t));
584 tdp->t_name = xstrdup(name);
585 tdp->t_size = sz;
586 tdp->t_id = tid;
587 tdp->t_type = INTRINSIC;
588 tdp->t_intr = intr;
589 tdp->t_flags = TDESC_F_RESOLVED;
590
591 tdesc_add(dw, tdp);
592
593 return (tdp);
594 }
595
596 /*
597 * Manufacture a void type. Used for gcc-emitted stabs, where the lack of a
598 * type reference implies a reference to a void type. A void *, for example
599 * will be represented by a pointer die without a DW_AT_type. CTF requires
600 * that pointer nodes point to something, so we'll create a void for use as
601 * the target. Note that the DWARF data may already create a void type. Ours
602 * would then be a duplicate, but it'll be removed in the self-uniquification
603 * merge performed at the completion of DWARF->tdesc conversion.
604 */
605 static tdesc_t *
606 tdesc_intr_void(dwarf_t *dw)
607 {
608 if (dw->dw_void == NULL)
609 dw->dw_void = tdesc_intr_common(dw, TID_VOID, "void", 0);
610
611 return (dw->dw_void);
612 }
613
614 static tdesc_t *
615 tdesc_intr_long(dwarf_t *dw)
616 {
617 if (dw->dw_long == NULL) {
618 dw->dw_long = tdesc_intr_common(dw, TID_LONG, "long",
619 dw->dw_ptrsz);
620 }
621
622 return (dw->dw_long);
623 }
624
625 /*
626 * Used for creating bitfield types. We create a copy of an existing intrinsic,
627 * adjusting the size of the copy to match what the caller requested. The
628 * caller can then use the copy as the type for a bitfield structure member.
629 */
630 static tdesc_t *
631 tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz)
632 {
633 tdesc_t *new = xcalloc(sizeof (tdesc_t));
634
635 if (!(old->t_flags & TDESC_F_RESOLVED)) {
636 terminate("tdp %u: attempt to make a bit field from an "
637 "unresolved type\n", old->t_id);
638 }
639
640 new->t_name = xstrdup(old->t_name);
641 new->t_size = old->t_size;
642 new->t_id = mfgtid_next(dw);
643 new->t_type = INTRINSIC;
644 new->t_flags = TDESC_F_RESOLVED;
645
646 new->t_intr = xcalloc(sizeof (intr_t));
647 bcopy(old->t_intr, new->t_intr, sizeof (intr_t));
648 new->t_intr->intr_nbits = bitsz;
649
650 tdesc_add(dw, new);
651
652 return (new);
653 }
654
655 static void
656 tdesc_array_create(dwarf_t *dw, Dwarf_Die dim, tdesc_t *arrtdp,
657 tdesc_t *dimtdp)
658 {
659 Dwarf_Unsigned uval;
660 Dwarf_Signed sval;
661 tdesc_t *ctdp = NULL;
662 Dwarf_Die dim2;
663 ardef_t *ar;
664
665 if ((dim2 = die_sibling(dw, dim)) == NULL) {
666 ctdp = arrtdp;
667 } else if (die_tag(dw, dim2) == DW_TAG_subrange_type) {
668 ctdp = xcalloc(sizeof (tdesc_t));
669 ctdp->t_id = mfgtid_next(dw);
670 debug(3, "die %ju: creating new type %u for sub-dimension\n",
671 (uintmax_t)die_off(dw, dim2), ctdp->t_id);
672 tdesc_array_create(dw, dim2, arrtdp, ctdp);
673 } else {
674 terminate("die %ju: unexpected non-subrange node in array\n",
675 (uintmax_t)die_off(dw, dim2));
676 }
677
678 dimtdp->t_type = ARRAY;
679 dimtdp->t_ardef = ar = xcalloc(sizeof (ardef_t));
680
681 /*
682 * Array bounds can be signed or unsigned, but there are several kinds
683 * of signless forms (data1, data2, etc) that take their sign from the
684 * routine that is trying to interpret them. That is, data1 can be
685 * either signed or unsigned, depending on whether you use the signed or
686 * unsigned accessor function. GCC will use the signless forms to store
687 * unsigned values which have their high bit set, so we need to try to
688 * read them first as unsigned to get positive values. We could also
689 * try signed first, falling back to unsigned if we got a negative
690 * value.
691 */
692 if (die_unsigned(dw, dim, DW_AT_upper_bound, &uval, 0))
693 ar->ad_nelems = uval + 1;
694 else if (die_signed(dw, dim, DW_AT_upper_bound, &sval, 0))
695 ar->ad_nelems = sval + 1;
696 else
697 ar->ad_nelems = 0;
698
699 /*
700 * Different compilers use different index types. Force the type to be
701 * a common, known value (long).
702 */
703 ar->ad_idxtype = tdesc_intr_long(dw);
704 ar->ad_contents = ctdp;
705
706 if (ar->ad_contents->t_size != 0) {
707 dimtdp->t_size = ar->ad_contents->t_size * ar->ad_nelems;
708 dimtdp->t_flags |= TDESC_F_RESOLVED;
709 }
710 }
711
712 /*
713 * Create a tdesc from an array node. Some arrays will come with byte size
714 * attributes, and thus can be resolved immediately. Others don't, and will
715 * need to wait until the second pass for resolution.
716 */
717 static void
718 die_array_create(dwarf_t *dw, Dwarf_Die arr, Dwarf_Off off, tdesc_t *tdp)
719 {
720 tdesc_t *arrtdp = die_lookup_pass1(dw, arr, DW_AT_type);
721 Dwarf_Unsigned uval;
722 Dwarf_Die dim;
723
724 debug(3, "die %ju <%jx>: creating array\n",
725 (uintmax_t)off, (uintmax_t)off);
726
727 if ((dim = die_child(dw, arr)) == NULL ||
728 die_tag(dw, dim) != DW_TAG_subrange_type)
729 terminate("die %ju: failed to retrieve array bounds\n",
730 (uintmax_t)off);
731
732 tdesc_array_create(dw, dim, arrtdp, tdp);
733
734 if (die_unsigned(dw, arr, DW_AT_byte_size, &uval, 0)) {
735 tdesc_t *dimtdp;
736 int flags;
737
738 /* Check for bogus gcc DW_AT_byte_size attribute */
739 if (uval == (unsigned)-1) {
740 printf("dwarf.c:%s() working around bogus -1 DW_AT_byte_size\n",
741 __func__);
742 uval = 0;
743 }
744
745 tdp->t_size = uval;
746
747 /*
748 * Ensure that sub-dimensions have sizes too before marking
749 * as resolved.
750 */
751 flags = TDESC_F_RESOLVED;
752 for (dimtdp = tdp->t_ardef->ad_contents;
753 dimtdp->t_type == ARRAY;
754 dimtdp = dimtdp->t_ardef->ad_contents) {
755 if (!(dimtdp->t_flags & TDESC_F_RESOLVED)) {
756 flags = 0;
757 break;
758 }
759 }
760
761 tdp->t_flags |= flags;
762 }
763
764 debug(3, "die %ju <%jx>: array nelems %u size %u\n", (uintmax_t)off,
765 (uintmax_t)off, tdp->t_ardef->ad_nelems, tdp->t_size);
766 }
767
768 /*ARGSUSED1*/
769 static int
770 die_array_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
771 {
772 dwarf_t *dw = private;
773 size_t sz;
774
775 if (tdp->t_flags & TDESC_F_RESOLVED)
776 return (1);
777
778 debug(3, "trying to resolve array %d (cont %d)\n", tdp->t_id,
779 tdp->t_ardef->ad_contents->t_id);
780
781 if ((sz = tdesc_size(tdp->t_ardef->ad_contents)) == 0) {
782 debug(3, "unable to resolve array %s (%d) contents %d\n",
783 tdesc_name(tdp), tdp->t_id,
784 tdp->t_ardef->ad_contents->t_id);
785
786 dw->dw_nunres++;
787 return (1);
788 }
789
790 tdp->t_size = sz * tdp->t_ardef->ad_nelems;
791 tdp->t_flags |= TDESC_F_RESOLVED;
792
793 debug(3, "resolved array %d: %u bytes\n", tdp->t_id, tdp->t_size);
794
795 return (1);
796 }
797
798 /*ARGSUSED1*/
799 static int
800 die_array_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)
801 {
802 tdesc_t *cont = tdp->t_ardef->ad_contents;
803
804 if (tdp->t_flags & TDESC_F_RESOLVED)
805 return (1);
806
807 fprintf(stderr, "Array %d: failed to size contents type %s (%d)\n",
808 tdp->t_id, tdesc_name(cont), cont->t_id);
809
810 return (1);
811 }
812
813 /*
814 * Most enums (those with members) will be resolved during this first pass.
815 * Others - those without members (see the file comment) - won't be, and will
816 * need to wait until the second pass when they can be matched with their full
817 * definitions.
818 */
819 static void
820 die_enum_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
821 {
822 Dwarf_Die mem;
823 Dwarf_Unsigned uval;
824 Dwarf_Signed sval;
825
826 debug(3, "die %ju: creating enum\n", (uintmax_t)off);
827
828 tdp->t_type = (die_isdecl(dw, die) ? FORWARD : ENUM);
829 if (tdp->t_type != ENUM)
830 return;
831
832 (void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ);
833 /* Check for bogus gcc DW_AT_byte_size attribute */
834 if (uval == (unsigned)-1) {
835 printf("dwarf.c:%s() working around bogus -1 DW_AT_byte_size\n",
836 __func__);
837 uval = 0;
838 }
839 tdp->t_size = uval;
840
841 if ((mem = die_child(dw, die)) != NULL) {
842 elist_t **elastp = &tdp->t_emem;
843
844 do {
845 elist_t *el;
846
847 if (die_tag(dw, mem) != DW_TAG_enumerator) {
848 /* Nested type declaration */
849 die_create_one(dw, mem);
850 continue;
851 }
852
853 el = xcalloc(sizeof (elist_t));
854 el->el_name = die_name(dw, mem);
855
856 if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) {
857 el->el_number = sval;
858 } else if (die_unsigned(dw, mem, DW_AT_const_value,
859 &uval, 0)) {
860 el->el_number = uval;
861 } else {
862 terminate("die %ju: enum %ju: member without "
863 "value\n", (uintmax_t)off,
864 (uintmax_t)die_off(dw, mem));
865 }
866
867 debug(3, "die %ju: enum %ju: created %s = %d\n",
868 (uintmax_t)off, (uintmax_t)die_off(dw, mem),
869 el->el_name, el->el_number);
870
871 *elastp = el;
872 elastp = &el->el_next;
873
874 } while ((mem = die_sibling(dw, mem)) != NULL);
875
876 hash_add(dw->dw_enumhash, tdp);
877
878 tdp->t_flags |= TDESC_F_RESOLVED;
879
880 if (tdp->t_name != NULL) {
881 iidesc_t *ii = xcalloc(sizeof (iidesc_t));
882 ii->ii_type = II_SOU;
883 ii->ii_name = xstrdup(tdp->t_name);
884 ii->ii_dtype = tdp;
885
886 iidesc_add(dw->dw_td->td_iihash, ii);
887 }
888 }
889 }
890
891 static int
892 die_enum_match(void *arg1, void *arg2)
893 {
894 tdesc_t *tdp = arg1, **fullp = arg2;
895
896 if (tdp->t_emem != NULL) {
897 *fullp = tdp;
898 return (-1); /* stop the iteration */
899 }
900
901 return (0);
902 }
903
904 /*ARGSUSED1*/
905 static int
906 die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
907 {
908 dwarf_t *dw = private;
909 tdesc_t *full = NULL;
910
911 if (tdp->t_flags & TDESC_F_RESOLVED)
912 return (1);
913
914 (void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full);
915
916 /*
917 * The answer to this one won't change from iteration to iteration,
918 * so don't even try.
919 */
920 if (full == NULL) {
921 terminate("tdp %u: enum %s has no members\n", tdp->t_id,
922 tdesc_name(tdp));
923 }
924
925 debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id,
926 tdesc_name(tdp), full->t_id);
927
928 tdp->t_flags |= TDESC_F_RESOLVED;
929
930 return (1);
931 }
932
933 static int
934 die_fwd_map(void *arg1, void *arg2)
935 {
936 tdesc_t *fwd = arg1, *sou = arg2;
937
938 debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id,
939 tdesc_name(fwd), sou->t_id);
940 fwd->t_tdesc = sou;
941
942 return (0);
943 }
944
945 /*
946 * Structures and unions will never be resolved during the first pass, as we
947 * won't be able to fully determine the member sizes. The second pass, which
948 * have access to sizing information, will be able to complete the resolution.
949 */
950 static void
951 die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp,
952 int type, const char *typename)
953 {
954 Dwarf_Unsigned sz, bitsz, bitoff, maxsz=0;
955 #if BYTE_ORDER == _LITTLE_ENDIAN
956 Dwarf_Unsigned bysz;
957 #endif
958 Dwarf_Die mem;
959 mlist_t *ml, **mlastp;
960 iidesc_t *ii;
961
962 tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type);
963
964 debug(3, "die %ju: creating %s %s\n", (uintmax_t)off,
965 (tdp->t_type == FORWARD ? "forward decl" : typename),
966 tdesc_name(tdp));
967
968 if (tdp->t_type == FORWARD) {
969 hash_add(dw->dw_fwdhash, tdp);
970 return;
971 }
972
973 (void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp);
974
975 (void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ);
976 tdp->t_size = sz;
977
978 /*
979 * GCC allows empty SOUs as an extension.
980 */
981 if ((mem = die_child(dw, str)) == NULL) {
982 goto out;
983 }
984
985 mlastp = &tdp->t_members;
986
987 do {
988 Dwarf_Off memoff = die_off(dw, mem);
989 Dwarf_Half tag = die_tag(dw, mem);
990 Dwarf_Unsigned mloff;
991
992 if (tag != DW_TAG_member) {
993 /* Nested type declaration */
994 die_create_one(dw, mem);
995 continue;
996 }
997
998 debug(3, "die %ju: mem %ju: creating member\n",
999 (uintmax_t)off, (uintmax_t)memoff);
1000
1001 ml = xcalloc(sizeof (mlist_t));
1002
1003 /*
1004 * This could be a GCC anon struct/union member, so we'll allow
1005 * an empty name, even though nothing can really handle them
1006 * properly. Note that some versions of GCC miss out debug
1007 * info for anon structs, though recent versions are fixed (gcc
1008 * bug 11816).
1009 */
1010 if ((ml->ml_name = die_name(dw, mem)) == NULL)
1011 ml->ml_name = NULL;
1012
1013 ml->ml_type = die_lookup_pass1(dw, mem, DW_AT_type);
1014 debug(3, "die_sou_create(): ml_type = %p t_id = %d\n",
1015 ml->ml_type, ml->ml_type->t_id);
1016
1017 if (die_mem_offset(dw, mem, DW_AT_data_member_location,
1018 &mloff, 0)) {
1019 debug(3, "die %ju: got mloff 0x%jx\n", (uintmax_t)off,
1020 (uintmax_t)mloff);
1021 ml->ml_offset = mloff * 8;
1022 }
1023
1024 if (die_unsigned(dw, mem, DW_AT_bit_size, &bitsz, 0))
1025 ml->ml_size = bitsz;
1026 else
1027 ml->ml_size = tdesc_bitsize(ml->ml_type);
1028
1029 if (die_unsigned(dw, mem, DW_AT_bit_offset, &bitoff, 0)) {
1030 #if BYTE_ORDER == _BIG_ENDIAN
1031 ml->ml_offset += bitoff;
1032 #else
1033 /*
1034 * Note that Clang 3.4 will sometimes generate
1035 * member DIE before generating the DIE for the
1036 * member's type. The code can not handle this
1037 * properly so that tdesc_bitsize(ml->ml_type) will
1038 * return 0 because ml->ml_type is unknown. As a
1039 * result, a wrong member offset will be calculated.
1040 * To workaround this, we can instead try to
1041 * retrieve the value of DW_AT_byte_size attribute
1042 * which stores the byte size of the space occupied
1043 * by the type. If this attribute exists, its value
1044 * should equal to tdesc_bitsize(ml->ml_type)/NBBY.
1045 */
1046 if (die_unsigned(dw, mem, DW_AT_byte_size, &bysz, 0) &&
1047 bysz > 0)
1048 ml->ml_offset += bysz * NBBY - bitoff -
1049 ml->ml_size;
1050 else
1051 ml->ml_offset += tdesc_bitsize(ml->ml_type) -
1052 bitoff - ml->ml_size;
1053 #endif
1054 }
1055
1056 debug(3, "die %ju: mem %ju: created \"%s\" (off %u sz %u)\n",
1057 (uintmax_t)off, (uintmax_t)memoff, ml->ml_name,
1058 ml->ml_offset, ml->ml_size);
1059
1060 *mlastp = ml;
1061 mlastp = &ml->ml_next;
1062
1063 /* Find the size of the largest member to work around a gcc
1064 * bug. See GCC Bugzilla 35998.
1065 */
1066 if (maxsz < ml->ml_size)
1067 maxsz = ml->ml_size;
1068
1069 } while ((mem = die_sibling(dw, mem)) != NULL);
1070
1071 /* See if we got a bogus DW_AT_byte_size. GCC will sometimes
1072 * emit this.
1073 */
1074 if (sz == (unsigned)-1) {
1075 printf("dwarf.c:%s() working around bogus -1 DW_AT_byte_size\n",
1076 __func__);
1077 tdp->t_size = maxsz / 8; /* maxsz is in bits, t_size is bytes */
1078 }
1079
1080 /*
1081 * GCC will attempt to eliminate unused types, thus decreasing the
1082 * size of the emitted dwarf. That is, if you declare a foo_t in your
1083 * header, include said header in your source file, and neglect to
1084 * actually use (directly or indirectly) the foo_t in the source file,
1085 * the foo_t won't make it into the emitted DWARF. So, at least, goes
1086 * the theory.
1087 *
1088 * Occasionally, it'll emit the DW_TAG_structure_type for the foo_t,
1089 * and then neglect to emit the members. Strangely, the loner struct
1090 * tag will always be followed by a proper nested declaration of
1091 * something else. This is clearly a bug, but we're not going to have
1092 * time to get it fixed before this goo goes back, so we'll have to work
1093 * around it. If we see a no-membered struct with a nested declaration
1094 * (i.e. die_child of the struct tag won't be null), we'll ignore it.
1095 * Being paranoid, we won't simply remove it from the hash. Instead,
1096 * we'll decline to create an iidesc for it, thus ensuring that this
1097 * type won't make it into the output file. To be safe, we'll also
1098 * change the name.
1099 */
1100 if (tdp->t_members == NULL) {
1101 const char *old = tdesc_name(tdp);
1102 size_t newsz = 7 + strlen(old) + 1;
1103 char *new = xmalloc(newsz);
1104 (void) snprintf(new, newsz, "orphan %s", old);
1105
1106 debug(3, "die %ju: worked around %s %s\n", (uintmax_t)off,
1107 typename, old);
1108
1109 if (tdp->t_name != NULL)
1110 free(tdp->t_name);
1111 tdp->t_name = new;
1112 return;
1113 }
1114
1115 out:
1116 if (tdp->t_name != NULL) {
1117 ii = xcalloc(sizeof (iidesc_t));
1118 ii->ii_type = II_SOU;
1119 ii->ii_name = xstrdup(tdp->t_name);
1120 ii->ii_dtype = tdp;
1121
1122 iidesc_add(dw->dw_td->td_iihash, ii);
1123 }
1124 }
1125
1126 static void
1127 die_struct_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1128 {
1129 die_sou_create(dw, die, off, tdp, STRUCT, "struct");
1130 }
1131
1132 static void
1133 die_union_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1134 {
1135 die_sou_create(dw, die, off, tdp, UNION, "union");
1136 }
1137
1138 /*ARGSUSED1*/
1139 static int
1140 die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
1141 {
1142 dwarf_t *dw = private;
1143 mlist_t *ml;
1144 tdesc_t *mt;
1145
1146 if (tdp->t_flags & TDESC_F_RESOLVED)
1147 return (1);
1148
1149 debug(3, "resolving sou %s\n", tdesc_name(tdp));
1150
1151 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
1152 if (ml->ml_size == 0) {
1153 mt = tdesc_basetype(ml->ml_type);
1154
1155 if (mt == NULL)
1156 continue;
1157
1158 if ((ml->ml_size = tdesc_bitsize(mt)) != 0)
1159 continue;
1160
1161 /*
1162 * For empty members, or GCC/C99 flexible array
1163 * members, a size of 0 is correct.
1164 */
1165 if (mt->t_members == NULL)
1166 continue;
1167 if (mt->t_type == ARRAY && mt->t_ardef->ad_nelems == 0)
1168 continue;
1169
1170 if (mt->t_type == STRUCT &&
1171 mt->t_members != NULL &&
1172 mt->t_members->ml_type->t_type == ARRAY &&
1173 mt->t_members->ml_type->t_ardef->ad_nelems == 0) {
1174 /* struct with zero sized array */
1175 continue;
1176 }
1177
1178 /*
1179 * anonymous union members are OK.
1180 * XXX: we should consistently use NULL, instead of ""
1181 */
1182 if (mt->t_type == UNION &&
1183 (mt->t_name == NULL || mt->t_name[0] == '\0'))
1184 continue;
1185
1186 printf("%s unresolved type = %d (%s)\n", tdesc_name(tdp),
1187 mt->t_type, tdesc_name(mt));
1188 dw->dw_nunres++;
1189 return (1);
1190 }
1191
1192 if ((mt = tdesc_basetype(ml->ml_type)) == NULL) {
1193 dw->dw_nunres++;
1194 return (1);
1195 }
1196
1197 if (ml->ml_size != 0 && mt->t_type == INTRINSIC &&
1198 mt->t_intr->intr_nbits != (int)ml->ml_size) {
1199 /*
1200 * This member is a bitfield, and needs to reference
1201 * an intrinsic type with the same width. If the
1202 * currently-referenced type isn't of the same width,
1203 * we'll copy it, adjusting the width of the copy to
1204 * the size we'd like.
1205 */
1206 debug(3, "tdp %u: creating bitfield for %d bits\n",
1207 tdp->t_id, ml->ml_size);
1208
1209 ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size);
1210 }
1211 }
1212
1213 tdp->t_flags |= TDESC_F_RESOLVED;
1214
1215 return (1);
1216 }
1217
1218 /*ARGSUSED1*/
1219 static int
1220 die_sou_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)
1221 {
1222 const char *typename = (tdp->t_type == STRUCT ? "struct" : "union");
1223 mlist_t *ml;
1224
1225 if (tdp->t_flags & TDESC_F_RESOLVED)
1226 return (1);
1227
1228 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
1229 if (ml->ml_size == 0) {
1230 fprintf(stderr, "%s %d <%x>: failed to size member \"%s\" "
1231 "of type %s (%d <%x>)\n", typename, tdp->t_id,
1232 tdp->t_id,
1233 ml->ml_name, tdesc_name(ml->ml_type),
1234 ml->ml_type->t_id, ml->ml_type->t_id);
1235 }
1236 }
1237
1238 return (1);
1239 }
1240
1241 static void
1242 die_funcptr_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1243 {
1244 Dwarf_Attribute attr;
1245 Dwarf_Half tag;
1246 Dwarf_Die arg;
1247 fndef_t *fn;
1248 int i;
1249
1250 debug(3, "die %ju <0x%jx>: creating function pointer\n",
1251 (uintmax_t)off, (uintmax_t)off);
1252
1253 /*
1254 * We'll begin by processing any type definition nodes that may be
1255 * lurking underneath this one.
1256 */
1257 for (arg = die_child(dw, die); arg != NULL;
1258 arg = die_sibling(dw, arg)) {
1259 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1260 tag != DW_TAG_unspecified_parameters) {
1261 /* Nested type declaration */
1262 die_create_one(dw, arg);
1263 }
1264 }
1265
1266 if (die_isdecl(dw, die)) {
1267 /*
1268 * This is a prototype. We don't add prototypes to the
1269 * tree, so we're going to drop the tdesc. Unfortunately,
1270 * it has already been added to the tree. Nobody will reference
1271 * it, though, and it will be leaked.
1272 */
1273 return;
1274 }
1275
1276 fn = xcalloc(sizeof (fndef_t));
1277
1278 tdp->t_type = FUNCTION;
1279
1280 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1281 fn->fn_ret = die_lookup_pass1(dw, die, DW_AT_type);
1282 } else {
1283 fn->fn_ret = tdesc_intr_void(dw);
1284 }
1285
1286 /*
1287 * Count the arguments to the function, then read them in.
1288 */
1289 for (fn->fn_nargs = 0, arg = die_child(dw, die); arg != NULL;
1290 arg = die_sibling(dw, arg)) {
1291 if ((tag = die_tag(dw, arg)) == DW_TAG_formal_parameter)
1292 fn->fn_nargs++;
1293 else if (tag == DW_TAG_unspecified_parameters &&
1294 fn->fn_nargs > 0)
1295 fn->fn_vargs = 1;
1296 }
1297
1298 if (fn->fn_nargs != 0) {
1299 debug(3, "die %ju: adding %d argument%s\n", (uintmax_t)off,
1300 fn->fn_nargs, (fn->fn_nargs > 1 ? "s" : ""));
1301
1302 fn->fn_args = xcalloc(sizeof (tdesc_t *) * fn->fn_nargs);
1303 for (i = 0, arg = die_child(dw, die);
1304 arg != NULL && i < (int) fn->fn_nargs;
1305 arg = die_sibling(dw, arg)) {
1306 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1307 continue;
1308
1309 fn->fn_args[i++] = die_lookup_pass1(dw, arg,
1310 DW_AT_type);
1311 }
1312 }
1313
1314 tdp->t_fndef = fn;
1315 tdp->t_flags |= TDESC_F_RESOLVED;
1316 }
1317
1318 /*
1319 * GCC and DevPro use different names for the base types. While the terms are
1320 * the same, they are arranged in a different order. Some terms, such as int,
1321 * are implied in one, and explicitly named in the other. Given a base type
1322 * as input, this routine will return a common name, along with an intr_t
1323 * that reflects said name.
1324 */
1325 static intr_t *
1326 die_base_name_parse(const char *name, char **newp)
1327 {
1328 char buf[100];
1329 char const *base;
1330 char *c;
1331 int nlong = 0, nshort = 0, nchar = 0, nint = 0;
1332 int sign = 1;
1333 char fmt = '\0';
1334 intr_t *intr;
1335
1336 if (strlen(name) > sizeof (buf) - 1)
1337 terminate("base type name \"%s\" is too long\n", name);
1338
1339 strncpy(buf, name, sizeof (buf));
1340
1341 for (c = strtok(buf, " "); c != NULL; c = strtok(NULL, " ")) {
1342 if (strcmp(c, "signed") == 0)
1343 sign = 1;
1344 else if (strcmp(c, "unsigned") == 0)
1345 sign = 0;
1346 else if (strcmp(c, "long") == 0)
1347 nlong++;
1348 else if (strcmp(c, "char") == 0) {
1349 nchar++;
1350 fmt = 'c';
1351 } else if (strcmp(c, "short") == 0)
1352 nshort++;
1353 else if (strcmp(c, "int") == 0)
1354 nint++;
1355 else {
1356 /*
1357 * If we don't recognize any of the tokens, we'll tell
1358 * the caller to fall back to the dwarf-provided
1359 * encoding information.
1360 */
1361 return (NULL);
1362 }
1363 }
1364
1365 if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2)
1366 return (NULL);
1367
1368 if (nchar > 0) {
1369 if (nlong > 0 || nshort > 0 || nint > 0)
1370 return (NULL);
1371
1372 base = "char";
1373
1374 } else if (nshort > 0) {
1375 if (nlong > 0)
1376 return (NULL);
1377
1378 base = "short";
1379
1380 } else if (nlong > 0) {
1381 base = "long";
1382
1383 } else {
1384 base = "int";
1385 }
1386
1387 intr = xcalloc(sizeof (intr_t));
1388 intr->intr_type = INTR_INT;
1389 intr->intr_signed = sign;
1390 intr->intr_iformat = fmt;
1391
1392 snprintf(buf, sizeof (buf), "%s%s%s",
1393 (sign ? "" : "unsigned "),
1394 (nlong > 1 ? "long " : ""),
1395 base);
1396
1397 *newp = xstrdup(buf);
1398 return (intr);
1399 }
1400
1401 typedef struct fp_size_map {
1402 size_t fsm_typesz[2]; /* size of {32,64} type */
1403 uint_t fsm_enc[3]; /* CTF_FP_* for {bare,cplx,imagry} type */
1404 } fp_size_map_t;
1405
1406 static const fp_size_map_t fp_encodings[] = {
1407 { { 4, 4 }, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
1408 { { 8, 8 }, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
1409 #ifdef __sparc
1410 { { 16, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1411 #else
1412 { { 12, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1413 #endif
1414 { { 0, 0 }, { 0, 0, 0 } }
1415 };
1416
1417 static uint_t
1418 die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Signed enc, size_t sz)
1419 {
1420 const fp_size_map_t *map = fp_encodings;
1421 uint_t szidx = dw->dw_ptrsz == sizeof (uint64_t);
1422 uint_t mult = 1, col = 0;
1423
1424 if (enc == DW_ATE_complex_float) {
1425 mult = 2;
1426 col = 1;
1427 } else if (enc == DW_ATE_imaginary_float
1428 #if defined(sun)
1429 || enc == DW_ATE_SUN_imaginary_float
1430 #endif
1431 )
1432 col = 2;
1433
1434 while (map->fsm_typesz[szidx] != 0) {
1435 if (map->fsm_typesz[szidx] * mult == sz)
1436 return (map->fsm_enc[col]);
1437 map++;
1438 }
1439
1440 terminate("die %ju: unrecognized real type size %ju\n",
1441 (uintmax_t)off, (uintmax_t)sz);
1442 /*NOTREACHED*/
1443 return (0);
1444 }
1445
1446 static intr_t *
1447 die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz)
1448 {
1449 intr_t *intr = xcalloc(sizeof (intr_t));
1450 Dwarf_Signed enc;
1451
1452 (void) die_signed(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ);
1453
1454 switch (enc) {
1455 case DW_ATE_unsigned:
1456 case DW_ATE_address:
1457 intr->intr_type = INTR_INT;
1458 break;
1459 case DW_ATE_unsigned_char:
1460 intr->intr_type = INTR_INT;
1461 intr->intr_iformat = 'c';
1462 break;
1463 case DW_ATE_signed:
1464 intr->intr_type = INTR_INT;
1465 intr->intr_signed = 1;
1466 break;
1467 case DW_ATE_signed_char:
1468 intr->intr_type = INTR_INT;
1469 intr->intr_signed = 1;
1470 intr->intr_iformat = 'c';
1471 break;
1472 case DW_ATE_boolean:
1473 intr->intr_type = INTR_INT;
1474 intr->intr_signed = 1;
1475 intr->intr_iformat = 'b';
1476 break;
1477 case DW_ATE_float:
1478 case DW_ATE_complex_float:
1479 case DW_ATE_imaginary_float:
1480 #if defined(sun)
1481 case DW_ATE_SUN_imaginary_float:
1482 case DW_ATE_SUN_interval_float:
1483 #endif
1484 intr->intr_type = INTR_REAL;
1485 intr->intr_signed = 1;
1486 intr->intr_fformat = die_base_type2enc(dw, off, enc, sz);
1487 break;
1488 default:
1489 terminate("die %ju: unknown base type encoding 0x%jx\n",
1490 (uintmax_t)off, (uintmax_t)enc);
1491 }
1492
1493 return (intr);
1494 }
1495
1496 static void
1497 die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp)
1498 {
1499 Dwarf_Unsigned sz;
1500 intr_t *intr;
1501 char *new;
1502
1503 debug(3, "die %ju: creating base type\n", (uintmax_t)off);
1504
1505 /*
1506 * The compilers have their own clever (internally inconsistent) ideas
1507 * as to what base types should look like. Some times gcc will, for
1508 * example, use DW_ATE_signed_char for char. Other times, however, it
1509 * will use DW_ATE_signed. Needless to say, this causes some problems
1510 * down the road, particularly with merging. We do, however, use the
1511 * DWARF idea of type sizes, as this allows us to avoid caring about
1512 * the data model.
1513 */
1514 (void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ);
1515
1516 /* Check for bogus gcc DW_AT_byte_size attribute */
1517 if (sz == (unsigned)-1) {
1518 printf("dwarf.c:%s() working around bogus -1 DW_AT_byte_size\n",
1519 __func__);
1520 sz = 0;
1521 }
1522
1523 if (tdp->t_name == NULL)
1524 terminate("die %ju: base type without name\n", (uintmax_t)off);
1525
1526 /* XXX make a name parser for float too */
1527 if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) {
1528 /* Found it. We'll use the parsed version */
1529 debug(3, "die %ju: name \"%s\" remapped to \"%s\"\n",
1530 (uintmax_t)off, tdesc_name(tdp), new);
1531
1532 free(tdp->t_name);
1533 tdp->t_name = new;
1534 } else {
1535 /*
1536 * We didn't recognize the type, so we'll create an intr_t
1537 * based on the DWARF data.
1538 */
1539 debug(3, "die %ju: using dwarf data for base \"%s\"\n",
1540 (uintmax_t)off, tdesc_name(tdp));
1541
1542 intr = die_base_from_dwarf(dw, base, off, sz);
1543 }
1544
1545 intr->intr_nbits = sz * 8;
1546
1547 tdp->t_type = INTRINSIC;
1548 tdp->t_intr = intr;
1549 tdp->t_size = sz;
1550
1551 tdp->t_flags |= TDESC_F_RESOLVED;
1552 }
1553
1554 static void
1555 die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp,
1556 int type, const char *typename)
1557 {
1558 Dwarf_Attribute attr;
1559
1560 debug(3, "die %ju <0x%jx>: creating %s type %d\n", (uintmax_t)off,
1561 (uintmax_t)off, typename, type);
1562
1563 tdp->t_type = type;
1564
1565 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1566 tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type);
1567 } else {
1568 tdp->t_tdesc = tdesc_intr_void(dw);
1569 }
1570
1571 if (type == POINTER)
1572 tdp->t_size = dw->dw_ptrsz;
1573
1574 tdp->t_flags |= TDESC_F_RESOLVED;
1575
1576 if (type == TYPEDEF) {
1577 iidesc_t *ii = xcalloc(sizeof (iidesc_t));
1578 ii->ii_type = II_TYPE;
1579 ii->ii_name = xstrdup(tdp->t_name);
1580 ii->ii_dtype = tdp;
1581
1582 iidesc_add(dw->dw_td->td_iihash, ii);
1583 }
1584 }
1585
1586 static void
1587 die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1588 {
1589 die_through_create(dw, die, off, tdp, TYPEDEF, "typedef");
1590 }
1591
1592 static void
1593 die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1594 {
1595 die_through_create(dw, die, off, tdp, CONST, "const");
1596 }
1597
1598 static void
1599 die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1600 {
1601 die_through_create(dw, die, off, tdp, POINTER, "pointer");
1602 }
1603
1604 static void
1605 die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1606 {
1607 die_through_create(dw, die, off, tdp, RESTRICT, "restrict");
1608 }
1609
1610 static void
1611 die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1612 {
1613 die_through_create(dw, die, off, tdp, VOLATILE, "volatile");
1614 }
1615
1616 /*ARGSUSED3*/
1617 static void
1618 die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)
1619 {
1620 Dwarf_Die arg;
1621 Dwarf_Half tag;
1622 iidesc_t *ii;
1623 char *name;
1624
1625 debug(3, "die %ju <0x%jx>: creating function definition\n",
1626 (uintmax_t)off, (uintmax_t)off);
1627
1628 /*
1629 * We'll begin by processing any type definition nodes that may be
1630 * lurking underneath this one.
1631 */
1632 for (arg = die_child(dw, die); arg != NULL;
1633 arg = die_sibling(dw, arg)) {
1634 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1635 tag != DW_TAG_variable) {
1636 /* Nested type declaration */
1637 die_create_one(dw, arg);
1638 }
1639 }
1640
1641 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) {
1642 /*
1643 * We process neither prototypes nor subprograms without
1644 * names.
1645 */
1646 return;
1647 }
1648
1649 ii = xcalloc(sizeof (iidesc_t));
1650 ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN;
1651 ii->ii_name = name;
1652 if (ii->ii_type == II_SFUN)
1653 ii->ii_owner = xstrdup(dw->dw_cuname);
1654
1655 debug(3, "die %ju: function %s is %s\n", (uintmax_t)off, ii->ii_name,
1656 (ii->ii_type == II_GFUN ? "global" : "static"));
1657
1658 if (die_attr(dw, die, DW_AT_type, 0) != NULL)
1659 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1660 else
1661 ii->ii_dtype = tdesc_intr_void(dw);
1662
1663 for (arg = die_child(dw, die); arg != NULL;
1664 arg = die_sibling(dw, arg)) {
1665 char *name1;
1666
1667 debug(3, "die %ju: looking at sub member at %ju\n",
1668 (uintmax_t)off, (uintmax_t)die_off(dw, die));
1669
1670 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1671 continue;
1672
1673 if ((name1 = die_name(dw, arg)) == NULL) {
1674 terminate("die %ju: func arg %d has no name\n",
1675 (uintptr_t)off, ii->ii_nargs + 1);
1676 }
1677
1678 if (strcmp(name1, "...") == 0) {
1679 free(name1);
1680 ii->ii_vargs = 1;
1681 continue;
1682 }
1683
1684 ii->ii_nargs++;
1685 }
1686
1687 if (ii->ii_nargs > 0) {
1688 int i;
1689
1690 debug(3, "die %ju: function has %d argument%s\n",
1691 (uintptr_t)off, ii->ii_nargs, ii->ii_nargs == 1 ? "" : "s");
1692
1693 ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs);
1694
1695 for (arg = die_child(dw, die), i = 0;
1696 arg != NULL && i < ii->ii_nargs;
1697 arg = die_sibling(dw, arg)) {
1698 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1699 continue;
1700
1701 ii->ii_args[i++] = die_lookup_pass1(dw, arg,
1702 DW_AT_type);
1703 }
1704 }
1705
1706 iidesc_add(dw->dw_td->td_iihash, ii);
1707 }
1708
1709 /*ARGSUSED3*/
1710 static void
1711 die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)
1712 {
1713 iidesc_t *ii;
1714 char *name;
1715
1716 debug(3, "die %ju: creating object definition\n", (uintptr_t)off);
1717
1718 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL)
1719 return; /* skip prototypes and nameless objects */
1720
1721 ii = xcalloc(sizeof (iidesc_t));
1722 ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR;
1723 ii->ii_name = name;
1724 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1725 if (ii->ii_type == II_SVAR)
1726 ii->ii_owner = xstrdup(dw->dw_cuname);
1727
1728 iidesc_add(dw->dw_td->td_iihash, ii);
1729 }
1730
1731 /*ARGSUSED2*/
1732 static int
1733 die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private __unused)
1734 {
1735 if (fwd->t_flags & TDESC_F_RESOLVED)
1736 return (1);
1737
1738 if (fwd->t_tdesc != NULL) {
1739 debug(3, "tdp %u: unforwarded %s\n", fwd->t_id,
1740 tdesc_name(fwd));
1741 *fwdp = fwd->t_tdesc;
1742 }
1743
1744 fwd->t_flags |= TDESC_F_RESOLVED;
1745
1746 return (1);
1747 }
1748
1749 /*ARGSUSED*/
1750 static void
1751 die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off __unused, tdesc_t *tdp __unused)
1752 {
1753 Dwarf_Die child = die_child(dw, die);
1754
1755 if (child != NULL)
1756 die_create(dw, child);
1757 }
1758
1759 /*
1760 * Used to map the die to a routine which can parse it, using the tag to do the
1761 * mapping. While the processing of most tags entails the creation of a tdesc,
1762 * there are a few which don't - primarily those which result in the creation of
1763 * iidescs which refer to existing tdescs.
1764 */
1765
1766 #define DW_F_NOTDP 0x1 /* Don't create a tdesc for the creator */
1767
1768 typedef struct die_creator {
1769 Dwarf_Half dc_tag;
1770 uint16_t dc_flags;
1771 void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *);
1772 } die_creator_t;
1773
1774 static const die_creator_t die_creators[] = {
1775 { DW_TAG_array_type, 0, die_array_create },
1776 { DW_TAG_enumeration_type, 0, die_enum_create },
1777 { DW_TAG_lexical_block, DW_F_NOTDP, die_lexblk_descend },
1778 { DW_TAG_pointer_type, 0, die_pointer_create },
1779 { DW_TAG_structure_type, 0, die_struct_create },
1780 { DW_TAG_subroutine_type, 0, die_funcptr_create },
1781 { DW_TAG_typedef, 0, die_typedef_create },
1782 { DW_TAG_union_type, 0, die_union_create },
1783 { DW_TAG_base_type, 0, die_base_create },
1784 { DW_TAG_const_type, 0, die_const_create },
1785 { DW_TAG_subprogram, DW_F_NOTDP, die_function_create },
1786 { DW_TAG_variable, DW_F_NOTDP, die_variable_create },
1787 { DW_TAG_volatile_type, 0, die_volatile_create },
1788 { DW_TAG_restrict_type, 0, die_restrict_create },
1789 { 0, 0, NULL }
1790 };
1791
1792 static const die_creator_t *
1793 die_tag2ctor(Dwarf_Half tag)
1794 {
1795 const die_creator_t *dc;
1796
1797 for (dc = die_creators; dc->dc_create != NULL; dc++) {
1798 if (dc->dc_tag == tag)
1799 return (dc);
1800 }
1801
1802 return (NULL);
1803 }
1804
1805 static void
1806 die_create_one(dwarf_t *dw, Dwarf_Die die)
1807 {
1808 Dwarf_Off off = die_off(dw, die);
1809 const die_creator_t *dc;
1810 Dwarf_Half tag;
1811 tdesc_t *tdp;
1812
1813 debug(3, "die %ju <0x%jx>: create_one\n", (uintptr_t)off,
1814 (uintptr_t)off);
1815
1816 if (off > dw->dw_maxoff) {
1817 terminate("illegal die offset %ju (max %ju)\n", (uintptr_t)off,
1818 dw->dw_maxoff);
1819 }
1820
1821 tag = die_tag(dw, die);
1822
1823 if ((dc = die_tag2ctor(tag)) == NULL) {
1824 debug(2, "die %ju: ignoring tag type %x\n", (uintptr_t)off,
1825 tag);
1826 return;
1827 }
1828
1829 if ((tdp = tdesc_lookup(dw, off)) == NULL &&
1830 !(dc->dc_flags & DW_F_NOTDP)) {
1831 tdp = xcalloc(sizeof (tdesc_t));
1832 tdp->t_id = off;
1833 tdesc_add(dw, tdp);
1834 }
1835
1836 if (tdp != NULL)
1837 tdp->t_name = die_name(dw, die);
1838
1839 dc->dc_create(dw, die, off, tdp);
1840 }
1841
1842 static void
1843 die_create(dwarf_t *dw, Dwarf_Die die)
1844 {
1845 do {
1846 die_create_one(dw, die);
1847 } while ((die = die_sibling(dw, die)) != NULL);
1848 }
1849
1850 static tdtrav_cb_f die_resolvers[] = {
1851 NULL,
1852 NULL, /* intrinsic */
1853 NULL, /* pointer */
1854 die_array_resolve, /* array */
1855 NULL, /* function */
1856 die_sou_resolve, /* struct */
1857 die_sou_resolve, /* union */
1858 die_enum_resolve, /* enum */
1859 die_fwd_resolve, /* forward */
1860 NULL, /* typedef */
1861 NULL, /* typedef unres */
1862 NULL, /* volatile */
1863 NULL, /* const */
1864 NULL, /* restrict */
1865 };
1866
1867 static tdtrav_cb_f die_fail_reporters[] = {
1868 NULL,
1869 NULL, /* intrinsic */
1870 NULL, /* pointer */
1871 die_array_failed, /* array */
1872 NULL, /* function */
1873 die_sou_failed, /* struct */
1874 die_sou_failed, /* union */
1875 NULL, /* enum */
1876 NULL, /* forward */
1877 NULL, /* typedef */
1878 NULL, /* typedef unres */
1879 NULL, /* volatile */
1880 NULL, /* const */
1881 NULL, /* restrict */
1882 };
1883
1884 static void
1885 die_resolve(dwarf_t *dw)
1886 {
1887 int last = -1;
1888 int pass = 0;
1889
1890 do {
1891 pass++;
1892 dw->dw_nunres = 0;
1893
1894 (void) iitraverse_hash(dw->dw_td->td_iihash,
1895 &dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw);
1896
1897 debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres);
1898
1899 if ((int) dw->dw_nunres == last) {
1900 fprintf(stderr, "%s: failed to resolve the following "
1901 "types:\n", progname);
1902
1903 (void) iitraverse_hash(dw->dw_td->td_iihash,
1904 &dw->dw_td->td_curvgen, NULL, NULL,
1905 die_fail_reporters, dw);
1906
1907 terminate("failed to resolve types\n");
1908 }
1909
1910 last = dw->dw_nunres;
1911
1912 } while (dw->dw_nunres != 0);
1913 }
1914
1915 /*
1916 * Any object containing a function or object symbol at any scope should also
1917 * contain DWARF data.
1918 */
1919 static boolean_t
1920 should_have_dwarf(Elf *elf)
1921 {
1922 Elf_Scn *scn = NULL;
1923 Elf_Data *data = NULL;
1924 GElf_Shdr shdr;
1925 GElf_Sym sym;
1926 uint32_t symdx = 0;
1927 size_t nsyms = 0;
1928 boolean_t found = B_FALSE;
1929
1930 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1931 gelf_getshdr(scn, &shdr);
1932
1933 if (shdr.sh_type == SHT_SYMTAB) {
1934 found = B_TRUE;
1935 break;
1936 }
1937 }
1938
1939 if (!found)
1940 terminate("cannot convert stripped objects\n");
1941
1942 data = elf_getdata(scn, NULL);
1943 nsyms = shdr.sh_size / shdr.sh_entsize;
1944
1945 for (symdx = 0; symdx < nsyms; symdx++) {
1946 gelf_getsym(data, symdx, &sym);
1947
1948 if ((GELF_ST_TYPE(sym.st_info) == STT_FUNC) ||
1949 (GELF_ST_TYPE(sym.st_info) == STT_TLS) ||
1950 (GELF_ST_TYPE(sym.st_info) == STT_OBJECT)) {
1951 char *name;
1952
1953 name = elf_strptr(elf, shdr.sh_link, sym.st_name);
1954
1955 /* Studio emits these local symbols regardless */
1956 if ((strcmp(name, "Bbss.bss") != 0) &&
1957 (strcmp(name, "Ttbss.bss") != 0) &&
1958 (strcmp(name, "Ddata.data") != 0) &&
1959 (strcmp(name, "Ttdata.data") != 0) &&
1960 (strcmp(name, "Drodata.rodata") != 0))
1961 return (B_TRUE);
1962 }
1963 }
1964
1965 return (B_FALSE);
1966 }
1967
1968 /*ARGSUSED*/
1969 int
1970 dw_read(tdata_t *td, Elf *elf, char *filename __unused)
1971 {
1972 Dwarf_Unsigned hdrlen, nxthdr;
1973 Dwarf_Off abboff;
1974 Dwarf_Half vers, addrsz, offsz;
1975 Dwarf_Die cu = 0;
1976 Dwarf_Die child = 0;
1977 dwarf_t dw;
1978 char *prod = NULL;
1979 int rc;
1980
1981 bzero(&dw, sizeof (dwarf_t));
1982 dw.dw_td = td;
1983 dw.dw_ptrsz = elf_ptrsz(elf);
1984 dw.dw_mfgtid_last = TID_MFGTID_BASE;
1985 dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp);
1986 dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
1987 tdesc_namecmp);
1988 dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
1989 tdesc_namecmp);
1990
1991 if ((rc = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw.dw_dw,
1992 &dw.dw_err)) == DW_DLV_NO_ENTRY) {
1993 /* The new library does that */
1994 if (dwarf_errno(dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {
1995 /*
1996 * There's no type data in the DWARF section, but
1997 * libdwarf is too clever to handle that properly.
1998 */
1999 return (0);
2000 }
2001 if (should_have_dwarf(elf)) {
2002 errno = ENOENT;
2003 return (-1);
2004 } else {
2005
2006 return (0);
2007 }
2008 } else if (rc != DW_DLV_OK) {
2009 if (dwarf_errno(dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {
2010 /*
2011 * There's no type data in the DWARF section, but
2012 * libdwarf is too clever to handle that properly.
2013 */
2014 return (0);
2015 }
2016
2017 terminate("failed to initialize DWARF: %s\n",
2018 dwarf_errmsg(dw.dw_err));
2019 }
2020
2021 if ((rc = dwarf_next_cu_header_b(dw.dw_dw, &hdrlen, &vers, &abboff,
2022 /*###2022 [cc] error: passing argument 4 of 'dwarf_next_cu_header_b' from incompatible pointer type [-Werror]%%%*/
2023 &addrsz, &offsz, NULL, &nxthdr, &dw.dw_err)) != DW_DLV_OK)
2024 terminate("rc = %d %s\n", rc, dwarf_errmsg(dw.dw_err));
2025
2026 if ((cu = die_sibling(&dw, NULL)) == NULL)
2027 goto out;
2028
2029 if ((child = die_child(&dw, cu)) == NULL) {
2030 Dwarf_Unsigned lang;
2031 if (die_unsigned(&dw, cu, DW_AT_language, &lang, 0)) {
2032 debug(1, "DWARF language: %ju\n", (uintptr_t)lang);
2033 /*
2034 * Assembly languages are typically that.
2035 * They have some dwarf info, but not what
2036 * we expect. They have local symbols for
2037 * example, but they are missing the child info.
2038 */
2039 if (lang >= DW_LANG_lo_user)
2040 return 0;
2041 }
2042 if (should_have_dwarf(elf))
2043 goto out;
2044 }
2045
2046 if (child == NULL)
2047 return (0);
2048
2049 dw.dw_maxoff = nxthdr - 1;
2050
2051 if (dw.dw_maxoff > TID_FILEMAX)
2052 terminate("file contains too many types\n");
2053
2054 debug(1, "DWARF version: %d\n", vers);
2055 if (vers < 2 || vers > 4) {
2056 terminate("file contains incompatible version %d DWARF code "
2057 "(version 2, 3 or 4 required)\n", vers);
2058 }
2059
2060 if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) {
2061 debug(1, "DWARF emitter: %s\n", prod);
2062 free(prod);
2063 }
2064
2065 if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) {
2066 char *base = xstrdup(basename(dw.dw_cuname));
2067 free(dw.dw_cuname);
2068 dw.dw_cuname = base;
2069
2070 debug(1, "CU name: %s\n", dw.dw_cuname);
2071 }
2072
2073 if ((child = die_child(&dw, cu)) != NULL)
2074 die_create(&dw, child);
2075
2076 if ((rc = dwarf_next_cu_header_b(dw.dw_dw, &hdrlen, &vers, &abboff,
2077 /*###2076 [cc] error: passing argument 4 of 'dwarf_next_cu_header_b' from incompatible pointer type [-Werror]%%%*/
2078 &addrsz, &offsz, NULL, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY)
2079 terminate("multiple compilation units not supported\n");
2080
2081 (void) dwarf_finish(dw.dw_dw, &dw.dw_err);
2082
2083 die_resolve(&dw);
2084
2085 cvt_fixups(td, dw.dw_ptrsz);
2086
2087 /* leak the dwarf_t */
2088
2089 return (0);
2090 out:
2091 terminate("file does not contain dwarf type data "
2092 "(try compiling with -g)\n");
2093 }
2094