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