/
dwarf.c
1993 lines (1639 loc) · 49.9 KB
/
dwarf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2012 Jason King. All rights reserved.
* Use is subject to license terms.
*/
/*
* DWARF to tdata conversion
*
* For the most part, conversion is straightforward, proceeding in two passes.
* On the first pass, we iterate through every die, creating new type nodes as
* necessary. Referenced tdesc_t's are created in an uninitialized state, thus
* allowing type reference pointers to be filled in. If the tdesc_t
* corresponding to a given die can be completely filled out (sizes and offsets
* calculated, and so forth) without using any referenced types, the tdesc_t is
* marked as resolved. Consider an array type. If the type corresponding to
* the array contents has not yet been processed, we will create a blank tdesc
* for the contents type (only the type ID will be filled in, relying upon the
* later portion of the first pass to encounter and complete the referenced
* type). We will then attempt to determine the size of the array. If the
* array has a byte size attribute, we will have completely characterized the
* array type, and will be able to mark it as resolved. The lack of a byte
* size attribute, on the other hand, will prevent us from fully resolving the
* type, as the size will only be calculable with reference to the contents
* type, which has not, as yet, been encountered. The array type will thus be
* left without the resolved flag, and the first pass will continue.
*
* When we begin the second pass, we will have created tdesc_t nodes for every
* type in the section. We will traverse the tree, from the iidescs down,
* processing each unresolved node. As the referenced nodes will have been
* populated, the array type used in our example above will be able to use the
* size of the referenced types (if available) to determine its own type. The
* traversal will be repeated until all types have been resolved or we have
* failed to make progress. When all tdescs have been resolved, the conversion
* is complete.
*
* There are, as always, a few special cases that are handled during the first
* and second passes:
*
* 1. Empty enums - GCC will occasionally emit an enum without any members.
* Later on in the file, it will emit the same enum type, though this time
* with the full complement of members. All references to the memberless
* enum need to be redirected to the full definition. During the first
* pass, each enum is entered in dm_enumhash, along with a pointer to its
* corresponding tdesc_t. If, during the second pass, we encounter a
* memberless enum, we use the hash to locate the full definition. All
* tdescs referencing the empty enum are then redirected.
*
* 2. Forward declarations - If the compiler sees a forward declaration for
* a structure, followed by the definition of that structure, it will emit
* DWARF data for both the forward declaration and the definition. We need
* to resolve the forward declarations when possible, by redirecting
* forward-referencing tdescs to the actual struct/union definitions. This
* redirection is done completely within the first pass. We begin by
* recording all forward declarations in dw_fwdhash. When we define a
* structure, we check to see if there have been any corresponding forward
* declarations. If so, we redirect the tdescs which referenced the forward
* declarations to the structure or union definition.
*
* XXX see if a post traverser will allow the elimination of repeated pass 2
* traversals.
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <errno.h>
#include <libelf.h>
#include <libdwarf.h>
#include <libgen.h>
#include <dwarf.h>
#include "ctf_headers.h"
#include "ctftools.h"
#include "memory.h"
#include "list.h"
#include "traverse.h"
/* The version of DWARF which we support. */
#define DWARF_VERSION 2
/*
* We need to define a couple of our own intrinsics, to smooth out some of the
* differences between the GCC and DevPro DWARF emitters. See the referenced
* routines and the special cases in the file comment for more details.
*
* Type IDs are 32 bits wide. We're going to use the top of that field to
* indicate types that we've created ourselves.
*/
#define TID_FILEMAX 0x3fffffff /* highest tid from file */
#define TID_VOID 0x40000001 /* see die_void() */
#define TID_LONG 0x40000002 /* see die_array() */
#define TID_MFGTID_BASE 0x40000003 /* first mfg'd tid */
/*
* To reduce the staggering amount of error-handling code that would otherwise
* be required, the attribute-retrieval routines handle most of their own
* errors. If the following flag is supplied as the value of the `req'
* argument, they will also handle the absence of a requested attribute by
* terminating the program.
*/
#define DW_ATTR_REQ 1
#define TDESC_HASH_BUCKETS 511
typedef struct dwarf {
Dwarf_Debug dw_dw; /* for libdwarf */
Dwarf_Error dw_err; /* for libdwarf */
Dwarf_Unsigned dw_maxoff; /* highest legal offset in this cu */
tdata_t *dw_td; /* root of the tdesc/iidesc tree */
hash_t *dw_tidhash; /* hash of tdescs by t_id */
hash_t *dw_fwdhash; /* hash of fwd decls by name */
hash_t *dw_enumhash; /* hash of memberless enums by name */
tdesc_t *dw_void; /* manufactured void type */
tdesc_t *dw_long; /* manufactured long type for arrays */
size_t dw_ptrsz; /* size of a pointer in this file */
tid_t dw_mfgtid_last; /* last mfg'd type ID used */
uint_t dw_nunres; /* count of unresolved types */
char *dw_cuname; /* name of compilation unit */
} dwarf_t;
static void die_create_one(dwarf_t *, Dwarf_Die);
static void die_create(dwarf_t *, Dwarf_Die);
static tid_t
mfgtid_next(dwarf_t *dw)
{
return (++dw->dw_mfgtid_last);
}
static void
tdesc_add(dwarf_t *dw, tdesc_t *tdp)
{
hash_add(dw->dw_tidhash, tdp);
}
static tdesc_t *
tdesc_lookup(dwarf_t *dw, int tid)
{
tdesc_t tmpl, *tdp;
tmpl.t_id = tid;
if (hash_find(dw->dw_tidhash, &tmpl, (void **)&tdp))
return (tdp);
else
return (NULL);
}
/*
* Resolve a tdesc down to a node which should have a size. Returns the size,
* zero if the size hasn't yet been determined.
*/
static size_t
tdesc_size(tdesc_t *tdp)
{
for (;;) {
switch (tdp->t_type) {
case INTRINSIC:
case POINTER:
case ARRAY:
case FUNCTION:
case STRUCT:
case UNION:
case ENUM:
return (tdp->t_size);
case FORWARD:
return (0);
case TYPEDEF:
case VOLATILE:
case CONST:
case RESTRICT:
tdp = tdp->t_tdesc;
continue;
case 0: /* not yet defined */
return (0);
default:
terminate("tdp %u: tdesc_size on unknown type %d\n",
tdp->t_id, tdp->t_type);
}
}
}
static size_t
tdesc_bitsize(tdesc_t *tdp)
{
for (;;) {
switch (tdp->t_type) {
case INTRINSIC:
return (tdp->t_intr->intr_nbits);
case ARRAY:
case FUNCTION:
case STRUCT:
case UNION:
case ENUM:
case POINTER:
return (tdp->t_size * NBBY);
case FORWARD:
return (0);
case TYPEDEF:
case VOLATILE:
case RESTRICT:
case CONST:
tdp = tdp->t_tdesc;
continue;
case 0: /* not yet defined */
return (0);
default:
terminate("tdp %u: tdesc_bitsize on unknown type %d\n",
tdp->t_id, tdp->t_type);
}
}
}
static tdesc_t *
tdesc_basetype(tdesc_t *tdp)
{
for (;;) {
switch (tdp->t_type) {
case TYPEDEF:
case VOLATILE:
case RESTRICT:
case CONST:
tdp = tdp->t_tdesc;
break;
case 0: /* not yet defined */
return (NULL);
default:
return (tdp);
}
}
}
static Dwarf_Off
die_off(dwarf_t *dw, Dwarf_Die die)
{
Dwarf_Off off;
if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK)
return (off);
terminate("failed to get offset for die: %s\n",
dwarf_errmsg(dw->dw_err));
/*NOTREACHED*/
return (0);
}
static Dwarf_Die
die_sibling(dwarf_t *dw, Dwarf_Die die)
{
Dwarf_Die sib;
int rc;
if ((rc = dwarf_siblingof(dw->dw_dw, die, &sib, &dw->dw_err)) ==
DW_DLV_OK)
return (sib);
else if (rc == DW_DLV_NO_ENTRY)
return (NULL);
terminate("die %llu: failed to find type sibling: %s\n",
die_off(dw, die), dwarf_errmsg(dw->dw_err));
/*NOTREACHED*/
return (NULL);
}
static Dwarf_Die
die_child(dwarf_t *dw, Dwarf_Die die)
{
Dwarf_Die child;
int rc;
if ((rc = dwarf_child(die, &child, &dw->dw_err)) == DW_DLV_OK)
return (child);
else if (rc == DW_DLV_NO_ENTRY)
return (NULL);
terminate("die %llu: failed to find type child: %s\n",
die_off(dw, die), dwarf_errmsg(dw->dw_err));
/*NOTREACHED*/
return (NULL);
}
static Dwarf_Half
die_tag(dwarf_t *dw, Dwarf_Die die)
{
Dwarf_Half tag;
if (dwarf_tag(die, &tag, &dw->dw_err) == DW_DLV_OK)
return (tag);
terminate("die %llu: failed to get tag for type: %s\n",
die_off(dw, die), dwarf_errmsg(dw->dw_err));
/*NOTREACHED*/
return (0);
}
static Dwarf_Attribute
die_attr(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, int req)
{
Dwarf_Attribute attr;
int rc;
if ((rc = dwarf_attr(die, name, &attr, &dw->dw_err)) == DW_DLV_OK) {
return (attr);
} else if (rc == DW_DLV_NO_ENTRY) {
if (req) {
terminate("die %llu: no attr 0x%x\n", die_off(dw, die),
name);
} else {
return (NULL);
}
}
terminate("die %llu: failed to get attribute for type: %s\n",
die_off(dw, die), dwarf_errmsg(dw->dw_err));
/*NOTREACHED*/
return (NULL);
}
static Dwarf_Half
die_attr_form(dwarf_t *dw, Dwarf_Attribute attr)
{
Dwarf_Half form;
if (dwarf_whatform(attr, &form, &dw->dw_err) == DW_DLV_OK)
return (form);
terminate("failed to get attribute form for type: %s\n",
dwarf_errmsg(dw->dw_err));
/*NOTREACHED*/
return (0);
}
/*
* the following functions lookup the value of an attribute in a DIE:
*
* die_signed
* die_unsigned
* die_bool
* die_string
*
* They all take the same parameters (with the exception of valp which is
* a pointer to the type of the attribute we are looking up):
*
* dw - the dwarf object to look in
* die - the DIE we're interested in
* name - the name of the attribute to lookup
* valp - pointer to where the value of the attribute is placed
* req - if the value is required (0 / non-zero)
*
* If the attribute is not found, one of the following happens:
* - program terminates (req is non-zero)
* - function returns 0
*
* If the value is found, and in a form (class) we can handle, the function
* returns 1.
*
* Currently, we can only handle attribute values that are stored as
* constants (immediate value). If an attribute has a form we cannot
* handle (for example VLAs may store the dimensions of the array
* as a DWARF expression that can compute it at runtime by reading
* values off the stack or other locations in memory), it is treated
* the same as if the attribute does not exist.
*/
static int
die_signed(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Signed *valp,
int req)
{
Dwarf_Attribute attr;
Dwarf_Signed val;
if ((attr = die_attr(dw, die, name, req)) == NULL)
return (0); /* die_attr will terminate for us if necessary */
if (dwarf_formsdata(attr, &val, &dw->dw_err) != DW_DLV_OK) {
if (req == 0)
return (0);
terminate("die %llu: failed to get signed (form 0x%x)\n",
die_off(dw, die), die_attr_form(dw, attr));
}
dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
*valp = val;
return (1);
}
static int
die_unsigned(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Unsigned *valp,
int req)
{
Dwarf_Attribute attr;
Dwarf_Unsigned val;
if ((attr = die_attr(dw, die, name, req)) == NULL)
return (0); /* die_attr will terminate for us if necessary */
if (dwarf_formudata(attr, &val, &dw->dw_err) != DW_DLV_OK) {
if (req == 0)
return (0);
terminate("die %llu: failed to get unsigned (form 0x%x)\n",
die_off(dw, die), die_attr_form(dw, attr));
}
dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
*valp = val;
return (1);
}
static int
die_bool(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Bool *valp, int req)
{
Dwarf_Attribute attr;
Dwarf_Bool val;
if ((attr = die_attr(dw, die, name, req)) == NULL)
return (0); /* die_attr will terminate for us if necessary */
if (dwarf_formflag(attr, &val, &dw->dw_err) != DW_DLV_OK) {
if (req == 0)
return (0);
terminate("die %llu: failed to get bool (form 0x%x)\n",
die_off(dw, die), die_attr_form(dw, attr));
}
dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
*valp = val;
return (1);
}
static int
die_string(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, char **strp, int req)
{
Dwarf_Attribute attr;
char *str;
if ((attr = die_attr(dw, die, name, req)) == NULL)
return (0); /* die_attr will terminate for us if necessary */
if (dwarf_formstring(attr, &str, &dw->dw_err) != DW_DLV_OK) {
if (req == 0)
return (0);
terminate("die %llu: failed to get string (form 0x%x)\n",
die_off(dw, die), die_attr_form(dw, attr));
}
*strp = xstrdup(str);
dwarf_dealloc(dw->dw_dw, str, DW_DLA_STRING);
return (1);
}
static Dwarf_Off
die_attr_ref(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
{
Dwarf_Attribute attr;
Dwarf_Off off;
attr = die_attr(dw, die, name, DW_ATTR_REQ);
if (dwarf_formref(attr, &off, &dw->dw_err) != DW_DLV_OK) {
terminate("die %llu: failed to get ref (form 0x%x)\n",
die_off(dw, die), die_attr_form(dw, attr));
}
dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
return (off);
}
static char *
die_name(dwarf_t *dw, Dwarf_Die die)
{
char *str = NULL;
(void) die_string(dw, die, DW_AT_name, &str, 0);
return (str);
}
static int
die_isdecl(dwarf_t *dw, Dwarf_Die die)
{
Dwarf_Bool val;
return (die_bool(dw, die, DW_AT_declaration, &val, 0) && val);
}
static int
die_isglobal(dwarf_t *dw, Dwarf_Die die)
{
Dwarf_Signed vis;
Dwarf_Bool ext;
/*
* Some compilers (gcc) use DW_AT_external to indicate function
* visibility. Others (Sun) use DW_AT_visibility.
*/
if (die_signed(dw, die, DW_AT_visibility, &vis, 0))
return (vis == DW_VIS_exported);
else
return (die_bool(dw, die, DW_AT_external, &ext, 0) && ext);
}
static tdesc_t *
die_add(dwarf_t *dw, Dwarf_Off off)
{
tdesc_t *tdp = xcalloc(sizeof (tdesc_t));
tdp->t_id = off;
tdesc_add(dw, tdp);
return (tdp);
}
static tdesc_t *
die_lookup_pass1(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
{
Dwarf_Off ref = die_attr_ref(dw, die, name);
tdesc_t *tdp;
if ((tdp = tdesc_lookup(dw, ref)) != NULL)
return (tdp);
return (die_add(dw, ref));
}
static int
die_mem_offset(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name,
Dwarf_Unsigned *valp, int req)
{
Dwarf_Attribute attr;
Dwarf_Locdesc *loc;
Dwarf_Signed locnum;
if ((attr = die_attr(dw, die, name, req)) == NULL)
return (0); /* die_attr will terminate for us if necessary */
if (dwarf_loclist(attr, &loc, &locnum, &dw->dw_err) != DW_DLV_OK) {
terminate("die %llu: failed to get mem offset location list\n",
die_off(dw, die));
}
dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR);
if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {
terminate("die %llu: cannot parse member offset\n",
die_off(dw, die));
}
*valp = loc->ld_s->lr_number;
dwarf_dealloc(dw->dw_dw, loc->ld_s, DW_DLA_LOC_BLOCK);
dwarf_dealloc(dw->dw_dw, loc, DW_DLA_LOCDESC);
return (1);
}
static tdesc_t *
tdesc_intr_common(dwarf_t *dw, int tid, const char *name, size_t sz)
{
tdesc_t *tdp;
intr_t *intr;
intr = xcalloc(sizeof (intr_t));
intr->intr_type = INTR_INT;
intr->intr_signed = 1;
intr->intr_nbits = sz * NBBY;
tdp = xcalloc(sizeof (tdesc_t));
tdp->t_name = xstrdup(name);
tdp->t_size = sz;
tdp->t_id = tid;
tdp->t_type = INTRINSIC;
tdp->t_intr = intr;
tdp->t_flags = TDESC_F_RESOLVED;
tdesc_add(dw, tdp);
return (tdp);
}
/*
* Manufacture a void type. Used for gcc-emitted stabs, where the lack of a
* type reference implies a reference to a void type. A void *, for example
* will be represented by a pointer die without a DW_AT_type. CTF requires
* that pointer nodes point to something, so we'll create a void for use as
* the target. Note that the DWARF data may already create a void type. Ours
* would then be a duplicate, but it'll be removed in the self-uniquification
* merge performed at the completion of DWARF->tdesc conversion.
*/
static tdesc_t *
tdesc_intr_void(dwarf_t *dw)
{
if (dw->dw_void == NULL)
dw->dw_void = tdesc_intr_common(dw, TID_VOID, "void", 0);
return (dw->dw_void);
}
static tdesc_t *
tdesc_intr_long(dwarf_t *dw)
{
if (dw->dw_long == NULL) {
dw->dw_long = tdesc_intr_common(dw, TID_LONG, "long",
dw->dw_ptrsz);
}
return (dw->dw_long);
}
/*
* Used for creating bitfield types. We create a copy of an existing intrinsic,
* adjusting the size of the copy to match what the caller requested. The
* caller can then use the copy as the type for a bitfield structure member.
*/
static tdesc_t *
tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz)
{
tdesc_t *new = xcalloc(sizeof (tdesc_t));
if (!(old->t_flags & TDESC_F_RESOLVED)) {
terminate("tdp %u: attempt to make a bit field from an "
"unresolved type\n", old->t_id);
}
new->t_name = xstrdup(old->t_name);
new->t_size = old->t_size;
new->t_id = mfgtid_next(dw);
new->t_type = INTRINSIC;
new->t_flags = TDESC_F_RESOLVED;
new->t_intr = xcalloc(sizeof (intr_t));
bcopy(old->t_intr, new->t_intr, sizeof (intr_t));
new->t_intr->intr_nbits = bitsz;
tdesc_add(dw, new);
return (new);
}
static void
tdesc_array_create(dwarf_t *dw, Dwarf_Die dim, tdesc_t *arrtdp,
tdesc_t *dimtdp)
{
Dwarf_Unsigned uval;
Dwarf_Signed sval;
tdesc_t *ctdp;
Dwarf_Die dim2;
ardef_t *ar;
if ((dim2 = die_sibling(dw, dim)) == NULL) {
ctdp = arrtdp;
} else if (die_tag(dw, dim2) == DW_TAG_subrange_type) {
ctdp = xcalloc(sizeof (tdesc_t));
ctdp->t_id = mfgtid_next(dw);
debug(3, "die %llu: creating new type %u for sub-dimension\n",
die_off(dw, dim2), ctdp->t_id);
tdesc_array_create(dw, dim2, arrtdp, ctdp);
} else {
terminate("die %llu: unexpected non-subrange node in array\n",
die_off(dw, dim2));
}
dimtdp->t_type = ARRAY;
dimtdp->t_ardef = ar = xcalloc(sizeof (ardef_t));
/*
* Array bounds can be signed or unsigned, but there are several kinds
* of signless forms (data1, data2, etc) that take their sign from the
* routine that is trying to interpret them. That is, data1 can be
* either signed or unsigned, depending on whether you use the signed or
* unsigned accessor function. GCC will use the signless forms to store
* unsigned values which have their high bit set, so we need to try to
* read them first as unsigned to get positive values. We could also
* try signed first, falling back to unsigned if we got a negative
* value.
*/
if (die_unsigned(dw, dim, DW_AT_upper_bound, &uval, 0))
ar->ad_nelems = uval + 1;
else if (die_signed(dw, dim, DW_AT_upper_bound, &sval, 0))
ar->ad_nelems = sval + 1;
else
ar->ad_nelems = 0;
/*
* Different compilers use different index types. Force the type to be
* a common, known value (long).
*/
ar->ad_idxtype = tdesc_intr_long(dw);
ar->ad_contents = ctdp;
if (ar->ad_contents->t_size != 0) {
dimtdp->t_size = ar->ad_contents->t_size * ar->ad_nelems;
dimtdp->t_flags |= TDESC_F_RESOLVED;
}
}
/*
* Create a tdesc from an array node. Some arrays will come with byte size
* attributes, and thus can be resolved immediately. Others don't, and will
* need to wait until the second pass for resolution.
*/
static void
die_array_create(dwarf_t *dw, Dwarf_Die arr, Dwarf_Off off, tdesc_t *tdp)
{
tdesc_t *arrtdp = die_lookup_pass1(dw, arr, DW_AT_type);
Dwarf_Unsigned uval;
Dwarf_Die dim;
debug(3, "die %llu: creating array\n", off);
if ((dim = die_child(dw, arr)) == NULL ||
die_tag(dw, dim) != DW_TAG_subrange_type)
terminate("die %llu: failed to retrieve array bounds\n", off);
tdesc_array_create(dw, dim, arrtdp, tdp);
if (die_unsigned(dw, arr, DW_AT_byte_size, &uval, 0)) {
tdesc_t *dimtdp;
int flags;
tdp->t_size = uval;
/*
* Ensure that sub-dimensions have sizes too before marking
* as resolved.
*/
flags = TDESC_F_RESOLVED;
for (dimtdp = tdp->t_ardef->ad_contents;
dimtdp->t_type == ARRAY;
dimtdp = dimtdp->t_ardef->ad_contents) {
if (!(dimtdp->t_flags & TDESC_F_RESOLVED)) {
flags = 0;
break;
}
}
tdp->t_flags |= flags;
}
debug(3, "die %llu: array nelems %u size %u\n", off,
tdp->t_ardef->ad_nelems, tdp->t_size);
}
/*ARGSUSED1*/
static int
die_array_resolve(tdesc_t *tdp, tdesc_t **tdpp, void *private)
{
dwarf_t *dw = private;
size_t sz;
if (tdp->t_flags & TDESC_F_RESOLVED)
return (1);
debug(3, "trying to resolve array %d (cont %d)\n", tdp->t_id,
tdp->t_ardef->ad_contents->t_id);
if ((sz = tdesc_size(tdp->t_ardef->ad_contents)) == 0) {
debug(3, "unable to resolve array %s (%d) contents %d\n",
tdesc_name(tdp), tdp->t_id,
tdp->t_ardef->ad_contents->t_id);
dw->dw_nunres++;
return (1);
}
tdp->t_size = sz * tdp->t_ardef->ad_nelems;
tdp->t_flags |= TDESC_F_RESOLVED;
debug(3, "resolved array %d: %u bytes\n", tdp->t_id, tdp->t_size);
return (1);
}
/*ARGSUSED1*/
static int
die_array_failed(tdesc_t *tdp, tdesc_t **tdpp, void *private)
{
tdesc_t *cont = tdp->t_ardef->ad_contents;
if (tdp->t_flags & TDESC_F_RESOLVED)
return (1);
fprintf(stderr, "Array %d: failed to size contents type %s (%d)\n",
tdp->t_id, tdesc_name(cont), cont->t_id);
return (1);
}
/*
* Most enums (those with members) will be resolved during this first pass.
* Others - those without members (see the file comment) - won't be, and will
* need to wait until the second pass when they can be matched with their full
* definitions.
*/
static void
die_enum_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
{
Dwarf_Die mem;
Dwarf_Unsigned uval;
Dwarf_Signed sval;
debug(3, "die %llu: creating enum\n", off);
tdp->t_type = ENUM;
(void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ);
tdp->t_size = uval;
if ((mem = die_child(dw, die)) != NULL) {
elist_t **elastp = &tdp->t_emem;
do {
elist_t *el;
if (die_tag(dw, mem) != DW_TAG_enumerator) {
/* Nested type declaration */
die_create_one(dw, mem);
continue;
}
el = xcalloc(sizeof (elist_t));
el->el_name = die_name(dw, mem);
if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) {
el->el_number = sval;
} else if (die_unsigned(dw, mem, DW_AT_const_value,
&uval, 0)) {
el->el_number = uval;
} else {
terminate("die %llu: enum %llu: member without "
"value\n", off, die_off(dw, mem));
}
debug(3, "die %llu: enum %llu: created %s = %d\n", off,
die_off(dw, mem), el->el_name, el->el_number);
*elastp = el;
elastp = &el->el_next;
} while ((mem = die_sibling(dw, mem)) != NULL);
hash_add(dw->dw_enumhash, tdp);
tdp->t_flags |= TDESC_F_RESOLVED;
if (tdp->t_name != NULL) {
iidesc_t *ii = xcalloc(sizeof (iidesc_t));
ii->ii_type = II_SOU;
ii->ii_name = xstrdup(tdp->t_name);
ii->ii_dtype = tdp;
iidesc_add(dw->dw_td->td_iihash, ii);
}
}
}
static int
die_enum_match(void *arg1, void *arg2)
{
tdesc_t *tdp = arg1, **fullp = arg2;
if (tdp->t_emem != NULL) {
*fullp = tdp;
return (-1); /* stop the iteration */
}
return (0);
}
/*ARGSUSED1*/
static int
die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp, void *private)
{
dwarf_t *dw = private;
tdesc_t *full = NULL;
if (tdp->t_flags & TDESC_F_RESOLVED)
return (1);
(void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full);
/*
* The answer to this one won't change from iteration to iteration,
* so don't even try.
*/
if (full == NULL) {
terminate("tdp %u: enum %s has no members\n", tdp->t_id,
tdesc_name(tdp));
}
debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id,
tdesc_name(tdp), full->t_id);
tdp->t_flags |= TDESC_F_RESOLVED;
return (1);
}
static int
die_fwd_map(void *arg1, void *arg2)
{
tdesc_t *fwd = arg1, *sou = arg2;
debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id,
tdesc_name(fwd), sou->t_id);
fwd->t_tdesc = sou;
return (0);
}
/*
* Structures and unions will never be resolved during the first pass, as we
* won't be able to fully determine the member sizes. The second pass, which
* have access to sizing information, will be able to complete the resolution.
*/
static void
die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp,
int type, const char *typename)
{
Dwarf_Unsigned sz, bitsz, bitoff;
Dwarf_Die mem;
mlist_t *ml, **mlastp;
iidesc_t *ii;
tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type);
debug(3, "die %llu: creating %s %s\n", off,
(tdp->t_type == FORWARD ? "forward decl" : typename),
tdesc_name(tdp));
if (tdp->t_type == FORWARD) {
hash_add(dw->dw_fwdhash, tdp);
return;
}
(void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp);
(void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ);
tdp->t_size = sz;
/*
* GCC allows empty SOUs as an extension.
*/
if ((mem = die_child(dw, str)) == NULL)
goto out;
mlastp = &tdp->t_members;
do {
Dwarf_Off memoff = die_off(dw, mem);
Dwarf_Half tag = die_tag(dw, mem);
Dwarf_Unsigned mloff;
if (tag != DW_TAG_member) {
/* Nested type declaration */
die_create_one(dw, mem);
continue;
}