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