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