summaryrefslogtreecommitdiff
path: root/usr.bin/ctfconv/parse.c
blob: ef483f914ad1a16748fc4224ccbd1cf4fd43f6ed (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
/*	$OpenBSD: parse.c,v 1.20 2024/02/22 13:17:18 claudio Exp $ */

/*
 * Copyright (c) 2016-2017 Martin Pieuchot
 * Copyright (c) 2016 Jasper Lievisse Adriaanse <jasper@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * DWARF to IT (internal type) representation parser.
 */

#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/types.h>
#include <sys/ctf.h>

#include <assert.h>
#include <limits.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>

#include "itype.h"
#include "xmalloc.h"
#include "dwarf.h"
#include "dw.h"
#include "pool.h"

#ifdef DEBUG
#include <stdio.h>
#endif

#ifndef NOPOOL
struct pool it_pool, im_pool, ir_pool;
#endif /* NOPOOL */

#ifndef nitems
#define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
#endif

#ifdef DEBUG
#define DPRINTF(x...)	do { printf(x); } while (0)
#else
#define DPRINTF(x...)	do { ; } while (0)
#endif

#define VOID_OFFSET	1	/* Fake offset for generating "void" type. */

/*
 * Tree used to resolve per-CU types based on their offset in
 * the abbrev section.
 */
RB_HEAD(ioff_tree, itype);

/*
 * Per-type trees used to merge existing types with the ones of
 * a newly parsed CU.
 */
RB_HEAD(itype_tree, itype)	 itypet[CTF_K_MAX];

/*
 * Tree of symbols used to build a list matching the order of
 * the ELF symbol table.
 */
struct isymb_tree	 isymbt;

struct itype		*void_it;		/* no type is emited for void */
uint16_t		 tidx, fidx, oidx;	/* type, func & object IDs */
uint16_t		 long_tidx;		/* index of "long", for array */


void		 cu_stat(void);
void		 cu_parse(struct dwcu *, struct itype_queue *,
		     struct ioff_tree *);
void		 cu_resolve(struct dwcu *, struct itype_queue *,
		     struct ioff_tree *);
void		 cu_reference(struct dwcu *, struct itype_queue *);
void		 cu_merge(struct dwcu *, struct itype_queue *);

struct itype	*parse_base(struct dwdie *, size_t);
struct itype	*parse_refers(struct dwdie *, size_t, int);
struct itype	*parse_array(struct dwdie *, size_t);
struct itype	*parse_enum(struct dwdie *, size_t);
struct itype	*parse_struct(struct dwdie *, size_t, int, size_t);
struct itype	*parse_function(struct dwdie *, size_t);
struct itype	*parse_funcptr(struct dwdie *, size_t);
struct itype	*parse_variable(struct dwdie *, size_t);

void		 subparse_subrange(struct dwdie *, size_t, struct itype *);
void		 subparse_enumerator(struct dwdie *, size_t, struct itype *);
void		 subparse_member(struct dwdie *, size_t, struct itype *, size_t);
void		 subparse_arguments(struct dwdie *, size_t, struct itype *);

size_t		 dav2val(struct dwaval *, size_t);
const char	*dav2str(struct dwaval *);
const char	*enc2name(unsigned short);

struct itype	*it_new(uint64_t, size_t, const char *, uint32_t, uint16_t,
		     uint64_t, uint16_t, unsigned int);
void		 it_merge(struct itype *, struct itype *);
void		 it_reference(struct itype *);
void		 it_free(struct itype *);
int		 it_cmp(struct itype *, struct itype *);
int		 it_name_cmp(struct itype *, struct itype *);
int		 it_off_cmp(struct itype *, struct itype *);
void		 ir_add(struct itype *, struct itype *);
void		 ir_purge(struct itype *);
struct imember	*im_new(const char *, size_t, size_t);

RB_GENERATE(itype_tree, itype, it_node, it_cmp);
RB_GENERATE(isymb_tree, itype, it_node, it_name_cmp);
RB_GENERATE(ioff_tree, itype, it_node, it_off_cmp);

/*
 * Construct a list of internal type and functions based on DWARF
 * INFO and ABBREV sections.
 *
 * Multiple CUs are supported.
 */
void
dwarf_parse(const char *infobuf, size_t infolen, const char *abbuf,
    size_t ablen)
{
	struct dwbuf		 info = { .buf = infobuf, .len = infolen };
	struct dwbuf		 abbrev = { .buf = abbuf, .len = ablen };
	struct dwcu		*dcu = NULL;
	struct ioff_tree	 cu_iofft;
	struct itype_queue	 cu_itypeq;
	struct itype		*it;
	int			 i;

	for (i = 0; i < CTF_K_MAX; i++)
		RB_INIT(&itypet[i]);
	RB_INIT(&isymbt);

	void_it = it_new(++tidx, VOID_OFFSET, "void", 0,
	    CTF_INT_SIGNED, 0, CTF_K_INTEGER, 0);
	TAILQ_INSERT_TAIL(&itypeq, void_it, it_next);

	while (dw_cu_parse(&info, &abbrev, infolen, &dcu) == 0) {
		TAILQ_INIT(&cu_itypeq);

		/* We use a tree to speed-up type resolution. */
		RB_INIT(&cu_iofft);

		/* Parse this CU */
		cu_parse(dcu, &cu_itypeq, &cu_iofft);

		/* Resolve its types. */
		cu_resolve(dcu, &cu_itypeq, &cu_iofft);
		assert(RB_EMPTY(&cu_iofft));

		/* Mark used type as such. */
		cu_reference(dcu, &cu_itypeq);

#ifdef DEBUG
		/* Dump statistics for current CU. */
		cu_stat();
#endif

		/* Merge them with the common type list. */
		cu_merge(dcu, &cu_itypeq);

		dw_dcu_free(dcu);
	}

	/* We force array's index type to be 'long', for that we need its ID. */
	RB_FOREACH(it, itype_tree, &itypet[CTF_K_INTEGER]) {
		if (it_name(it) == NULL || it->it_size != (8 * sizeof(long)))
			continue;

		if (strcmp(it_name(it), "unsigned") == 0) {
			long_tidx = it->it_idx;
			break;
		}
	}
}

struct itype *
it_new(uint64_t index, size_t off, const char *name, uint32_t size,
    uint16_t enc, uint64_t ref, uint16_t type, unsigned int flags)
{
	struct itype *it;
#ifndef NOPOOL
	static int it_pool_inited = 0;

	if (!it_pool_inited) {
		pool_init(&it_pool, "it", 512, sizeof(struct itype));
		pool_init(&im_pool, "im", 1024, sizeof(struct imember));
		pool_init(&ir_pool, "ir", 1024, sizeof(struct itref));
		it_pool_inited = 1;
	}
#endif

	assert((name != NULL) || !(flags & (ITF_FUNC|ITF_OBJ)));

	it = pmalloc(&it_pool, sizeof(*it));
	SIMPLEQ_INIT(&it->it_refs);
	TAILQ_INIT(&it->it_members);
	it->it_off = off;
	it->it_ref = ref;
	it->it_refp = NULL;
	it->it_size = size;
	it->it_nelems = 0;
	it->it_enc = enc;
	it->it_idx = index;
	it->it_type = type;
	it->it_flags = flags;

	if (name == NULL) {
		it->it_flags |= ITF_ANON;
	} else {
		size_t n;

		if ((n = strlcpy(it->it_name, name, ITNAME_MAX)) > ITNAME_MAX)
			warnx("name %s too long %zd > %d", name, n, ITNAME_MAX);
	}

	return it;
}

struct itype *
it_dup(struct itype *it)
{
	struct imember *copim, *im;
	struct itype *copit;

	copit = it_new(it->it_idx, it->it_off, it_name(it), it->it_size,
	    it->it_enc, it->it_ref, it->it_type, it->it_flags);

	copit->it_refp = it->it_refp;
	copit->it_nelems = it->it_nelems;

	TAILQ_FOREACH(im, &it->it_members, im_next) {
		copim = im_new(im_name(im), im->im_ref, im->im_off);
		copim->im_refp = im->im_refp;
		TAILQ_INSERT_TAIL(&copit->it_members, copim, im_next);
	}

	return copit;
}

/*
 * Merge the content of ``it'', the full type declaration into the
 * forwarding representation ``fwd''.
 */
void
it_merge(struct itype *fwd, struct itype *it)
{
	assert(fwd->it_flags & ITF_FORWARD);
	assert(fwd->it_type == it->it_type);
	assert(TAILQ_EMPTY(&fwd->it_members));
	assert(SIMPLEQ_EMPTY(&it->it_refs));

	fwd->it_off = it->it_off;
	fwd->it_ref = it->it_ref;
	fwd->it_refp = it->it_refp;
	fwd->it_size = it->it_size;
	fwd->it_nelems = it->it_nelems;
	fwd->it_enc = it->it_enc;
	fwd->it_flags = it->it_flags;

	TAILQ_CONCAT(&fwd->it_members, &it->it_members, im_next);
	assert(TAILQ_EMPTY(&it->it_members));
}

const char *
it_name(struct itype *it)
{
	if (!(it->it_flags & ITF_ANON))
		return it->it_name;

	return NULL;
}

void
it_reference(struct itype *it)
{
	struct imember *im;

	if (it == NULL || it->it_flags & ITF_USED)
		return;

	it->it_flags |= ITF_USED;

	it_reference(it->it_refp);
	TAILQ_FOREACH(im, &it->it_members, im_next)
		it_reference(im->im_refp);
}

void
it_free(struct itype *it)
{
	struct imember *im;

	if (it == NULL)
		return;

	while ((im = TAILQ_FIRST(&it->it_members)) != NULL) {
		TAILQ_REMOVE(&it->it_members, im, im_next);
		pfree(&im_pool, im);
	}

	ir_purge(it);
	pfree(&it_pool, it);
}

/*
 * Return 0 if ``a'' matches ``b''.
 */
int
it_cmp(struct itype *a, struct itype *b)
{
	if (a->it_type > b->it_type)
		return 1;
	if (a->it_type < b->it_type)
		return -1;

	/* Basic types need to have the same encoding and size. */
	if ((a->it_type == CTF_K_INTEGER || a->it_type == CTF_K_FLOAT)) {
		if (a->it_enc > b->it_enc)
			return 1;
		if (a->it_enc < b->it_enc)
			return -1;
		if (a->it_size > b->it_size)
			return 1;
		if (a->it_size < b->it_size)
			return -1;
	}

	/* Arrays need to have same number of elements */
	if (a->it_type == CTF_K_ARRAY) {
		if (a->it_nelems > b->it_nelems)
			return 1;
		if (a->it_nelems < b->it_nelems)
			return -1;
	}

	/* Match by name */
	if (!(a->it_flags & ITF_ANON) && !(b->it_flags & ITF_ANON))
		return strcmp(it_name(a), it_name(b));

	/* Only one of them is anonym */
	if ((a->it_flags & ITF_ANON) != (b->it_flags & ITF_ANON))
		return (a->it_flags & ITF_ANON) ? -1 : 1;

	/* Match by reference */
	if ((a->it_refp != NULL) && (b->it_refp != NULL))
		return it_cmp(a->it_refp, b->it_refp);
	if (a->it_refp == NULL)
		return -1;
	if (b->it_refp == NULL)
		return 1;

	return 0;
}

int
it_name_cmp(struct itype *a, struct itype *b)
{
	int diff;

	if ((diff = strcmp(it_name(a), it_name(b))) != 0)
		return diff;

	return ((a->it_flags|ITF_MASK) - (b->it_flags|ITF_MASK));
}

int
it_off_cmp(struct itype *a, struct itype *b)
{
	return a->it_off - b->it_off;
}

void
ir_add(struct itype *it, struct itype *tmp)
{
	struct itref *ir;

	SIMPLEQ_FOREACH(ir, &tmp->it_refs, ir_next) {
		if (ir->ir_itp == it)
			return;
	}

	ir = pmalloc(&ir_pool, sizeof(*ir));
	ir->ir_itp = it;
	SIMPLEQ_INSERT_TAIL(&tmp->it_refs, ir, ir_next);
}

void
ir_purge(struct itype *it)
{
	struct itref *ir;

	while ((ir = SIMPLEQ_FIRST(&it->it_refs)) != NULL) {
		SIMPLEQ_REMOVE_HEAD(&it->it_refs, ir_next);
		pfree(&ir_pool, ir);
	}
}

struct imember *
im_new(const char *name, size_t ref, size_t off)
{
	struct imember *im;

	im = pmalloc(&im_pool, sizeof(*im));
	im->im_ref = ref;
	im->im_off = off;
	im->im_refp = NULL;
	if (name == NULL) {
		im->im_flags = IMF_ANON;
	} else {
		size_t n;

		n = strlcpy(im->im_name, name, ITNAME_MAX);
		if (n > ITNAME_MAX)
			warnx("name %s too long %zd > %d", name, n,
			    ITNAME_MAX);
		im->im_flags = 0;
	}

	return im;
}

const char *
im_name(struct imember *im)
{
	if (!(im->im_flags & IMF_ANON))
		return im->im_name;

	return NULL;
}

void
cu_stat(void)
{
#ifndef NOPOOL
	pool_dump();
#endif
}

/*
 * Iterate over all types found in a given CU.  For all non-resolved types
 * use their DWARF relative offset to find the relative type they are pointing
 * to.  The CU offset tree, `cuot', is used to speedup relative type lookup.
 */
void
cu_resolve(struct dwcu *dcu, struct itype_queue *cutq, struct ioff_tree *cuot)
{
	struct itype	*it, *ref, tmp;
	struct imember	*im;
	unsigned int	 toresolve;
	size_t		 off = dcu->dcu_offset;

	TAILQ_FOREACH(it, cutq, it_next) {
		if (!(it->it_flags & (ITF_UNRES|ITF_UNRES_MEMB)))
			continue;

		/* If this type references another one, try to find it. */
		if (it->it_flags & ITF_UNRES) {
			tmp.it_off = it->it_ref + off;
			ref = RB_FIND(ioff_tree, cuot, &tmp);
			if (ref != NULL) {
				it->it_refp = ref;
				ir_add(it, ref);
				it->it_flags &= ~ITF_UNRES;
			}
		}

		/* If this type has members, resolve all of them. */
		toresolve = it->it_nelems;
		if ((it->it_flags & ITF_UNRES_MEMB) && toresolve > 0) {
			TAILQ_FOREACH(im, &it->it_members, im_next) {
				tmp.it_off = im->im_ref + off;
				ref = RB_FIND(ioff_tree, cuot, &tmp);
				if (ref != NULL) {
					im->im_refp = ref;
					ir_add(it, ref);
					toresolve--;
				}
			}
			if (toresolve == 0)
				it->it_flags &= ~ITF_UNRES_MEMB;
		}
#if defined(DEBUG)
		if (it->it_flags & (ITF_UNRES|ITF_UNRES_MEMB)) {
			printf("0x%zx: %s type=%d unresolved 0x%llx",
			    it->it_off, it_name(it), it->it_type, it->it_ref);
			if (toresolve)
				printf(": %d members", toresolve);
			TAILQ_FOREACH(im, &it->it_members, im_next) {
				if (im->im_refp != NULL)
					continue;
				printf("\n%zu: %s", im->im_ref, im_name(im));
			}
			printf("\n");
		}
#endif /* defined(DEBUG) */
	}

	/* We'll reuse the tree for the next CU, so empty it. */
	RB_FOREACH_SAFE(it, ioff_tree, cuot, ref)
		RB_REMOVE(ioff_tree, cuot, it);
}

void
cu_reference(struct dwcu *dcu, struct itype_queue *cutq)
{
	struct itype *it;

	TAILQ_FOREACH(it, cutq, it_next) {
		if (it->it_flags & (ITF_OBJ|ITF_FUNC))
			it_reference(it);
	}
}

/*
 * Merge type representation from a CU with already known types.
 */
void
cu_merge(struct dwcu *dcu, struct itype_queue *cutq)
{
	struct itype *it, *nit, *prev, *first;
	int diff;

	/* First ``it'' that needs a duplicate check. */
	first = TAILQ_FIRST(cutq);
	if (first == NULL)
		return;

	TAILQ_CONCAT(&itypeq, cutq, it_next);

	/*
	 * First pass: merge types
	 */
	for (it = first; it != NULL; it = nit) {
		nit = TAILQ_NEXT(it, it_next);

		/* Move functions & variable to their own list. */
		if (it->it_flags & (ITF_FUNC|ITF_OBJ)) {
			/*
			 * FIXME: allow static variables with the same name
			 * to be of different type.
			 */
			if (RB_FIND(isymb_tree, &isymbt, it) == NULL)
				RB_INSERT(isymb_tree, &isymbt, it);
			continue;
		}

		/* Look if we already have this type. */
		if (it->it_flags & ITF_USED)
			prev = RB_FIND(itype_tree, &itypet[it->it_type], it);
		else
			prev = NULL;

		if (prev != NULL) {
			struct itype *old = it;
			struct itref *ir;
			struct imember *im;

			/* Substitute references */
			while ((ir = SIMPLEQ_FIRST(&old->it_refs)) != NULL) {
				it = ir->ir_itp;

				SIMPLEQ_REMOVE_HEAD(&old->it_refs, ir_next);
				pfree(&ir_pool, ir);

				if (it->it_refp == old)
					it->it_refp = prev;

				TAILQ_FOREACH(im, &it->it_members, im_next) {
					if (im->im_refp == old)
						im->im_refp = prev;
				}
			}

			/* If we first got a forward reference, complete it. */
			if ((prev->it_flags & ITF_FORWARD) &&
			    (old->it_flags & ITF_FORWARD) == 0)
			    	it_merge(prev, old);

			old->it_flags &= ~ITF_USED;
		} else if (it->it_flags & ITF_USED) {
			RB_INSERT(itype_tree, &itypet[it->it_type], it);
		}
	}

	/*
	 * Second pass: update indexes
	 */
	diff = 0;
	for (it = first; it != NULL; it = nit) {
		nit = TAILQ_NEXT(it, it_next);

		if (it->it_flags & (ITF_FUNC|ITF_OBJ))
			continue;

		/* Adjust indexes */
		if (it->it_flags & ITF_USED) {
			it->it_idx -= diff;
			continue;
		}

		/* Remove unused */
		TAILQ_REMOVE(&itypeq, it, it_next);
		it_free(it);
		diff++;
	}

	/* Update global index to match removed entries. */
	it = TAILQ_LAST(&itypeq, itype_queue);
	while (it->it_flags & (ITF_FUNC|ITF_OBJ))
		it = TAILQ_PREV(it, itype_queue, it_next);

	tidx = it->it_idx;
}

/*
 * Parse a CU.
 */
void
cu_parse(struct dwcu *dcu, struct itype_queue *cutq, struct ioff_tree *cuot)
{
	struct itype *it = NULL;
	struct dwdie *die;
	size_t psz = dcu->dcu_psize;
	size_t off = dcu->dcu_offset;

	assert(RB_EMPTY(cuot));

	SIMPLEQ_FOREACH(die, &dcu->dcu_dies, die_next) {
		uint64_t tag = die->die_dab->dab_tag;

		switch (tag) {
		case DW_TAG_array_type:
			it = parse_array(die, dcu->dcu_psize);
			break;
		case DW_TAG_enumeration_type:
			it = parse_enum(die, dcu->dcu_psize);
			break;
		case DW_TAG_pointer_type:
			it = parse_refers(die, psz, CTF_K_POINTER);
			break;
		case DW_TAG_structure_type:
			it = parse_struct(die, psz, CTF_K_STRUCT, off);
			if (it == NULL)
				continue;
			break;
		case DW_TAG_typedef:
			it = parse_refers(die, psz, CTF_K_TYPEDEF);
			break;
		case DW_TAG_union_type:
			it = parse_struct(die, psz, CTF_K_UNION, off);
			if (it == NULL)
				continue;
			break;
		case DW_TAG_base_type:
			it = parse_base(die, psz);
			if (it == NULL)
				continue;
			break;
		case DW_TAG_const_type:
			it = parse_refers(die, psz, CTF_K_CONST);
			break;
		case DW_TAG_volatile_type:
			it = parse_refers(die, psz, CTF_K_VOLATILE);
			break;
		case DW_TAG_restrict_type:
			it = parse_refers(die, psz, CTF_K_RESTRICT);
			break;
		case DW_TAG_subprogram:
			it = parse_function(die, psz);
			if (it == NULL)
				continue;
			break;
		case DW_TAG_subroutine_type:
			it = parse_funcptr(die, psz);
			break;
		/*
		 * Children are assumed to be right after their parent in
		 * the list.  The parent parsing function takes care of
		 * parsing them.
		 */
		 case DW_TAG_member:
			 assert(it->it_type == CTF_K_STRUCT ||
			    it->it_type == CTF_K_UNION ||
			    it->it_type == CTF_K_ENUM);
			continue;
		 case DW_TAG_subrange_type:
			assert(it->it_type == CTF_K_ARRAY);
			continue;
		case DW_TAG_formal_parameter:
			/*
			 * If we skipped the second inline definition,
			 * skip its arguments.
			 */
			if (it == NULL)
				continue;

			/* See comment in subparse_arguments(). */
			if (it->it_type == CTF_K_STRUCT ||
			    it->it_type == CTF_K_UNION ||
			    it->it_type == CTF_K_ENUM ||
			    it->it_type == CTF_K_TYPEDEF)
				continue;

			if (it->it_flags & ITF_OBJ)
				continue;

			assert(it->it_type == CTF_K_FUNCTION);
			continue;
		case DW_TAG_variable:
			it = parse_variable(die, psz);
			/* Unnamed variables are discarded. */
			if (it == NULL)
				continue;
			break;
#if 1
		case DW_TAG_lexical_block:
		case DW_TAG_inlined_subroutine:
			continue;
#endif
		case DW_TAG_compile_unit:
		default:
			DPRINTF("%s\n", dw_tag2name(tag));
			continue;
		}

		TAILQ_INSERT_TAIL(cutq, it, it_next);
		RB_INSERT(ioff_tree, cuot, it);
	}
}

struct itype *
parse_base(struct dwdie *die, size_t psz)
{
	struct itype *it;
	struct dwaval *dav;
	uint16_t encoding, enc = 0, bits = 0;
	int type;

	SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
		switch (dav->dav_dat->dat_attr) {
		case DW_AT_encoding:
			enc = dav2val(dav, psz);
			break;
		case DW_AT_byte_size:
			bits = 8 * dav2val(dav, psz);
			break;
		default:
			DPRINTF("%s\n", dw_at2name(dav->dav_dat->dat_attr));
			break;
		}
	}

	switch (enc) {
	case DW_ATE_unsigned:
	case DW_ATE_address:
		encoding = 0;
		type = CTF_K_INTEGER;
		break;
	case DW_ATE_unsigned_char:
		encoding = CTF_INT_CHAR;
		type = CTF_K_INTEGER;
		break;
	case DW_ATE_signed:
		encoding = CTF_INT_SIGNED;
		type = CTF_K_INTEGER;
		break;
	case DW_ATE_signed_char:
		encoding = CTF_INT_SIGNED | CTF_INT_CHAR;
		type = CTF_K_INTEGER;
		break;
	case DW_ATE_boolean:
		encoding = CTF_INT_SIGNED | CTF_INT_BOOL;
		type = CTF_K_INTEGER;
		break;
	case DW_ATE_float:
		if (bits < psz)
			encoding = CTF_FP_SINGLE;
		else if (bits == psz)
			encoding = CTF_FP_DOUBLE;
		else
			encoding = CTF_FP_LDOUBLE;
		type = CTF_K_FLOAT;
		break;
	case DW_ATE_complex_float:
		if (bits < psz)
			encoding = CTF_FP_CPLX;
		else if (bits == psz)
			encoding = CTF_FP_DCPLX;
		else
			encoding = CTF_FP_LDCPLX;
		type = CTF_K_FLOAT;
		break;
	case DW_ATE_imaginary_float:
		if (bits < psz)
			encoding = CTF_FP_IMAGRY;
		else if (bits == psz)
			encoding = CTF_FP_DIMAGRY;
		else
			encoding = CTF_FP_LDIMAGRY;
		type = CTF_K_FLOAT;
		break;
	default:
		DPRINTF("unknown encoding: %d\n", enc);
		return (NULL);
	}

	it = it_new(++tidx, die->die_offset, enc2name(enc), bits,
	    encoding, 0, type, 0);

	return it;
}

struct itype *
parse_refers(struct dwdie *die, size_t psz, int type)
{
	struct itype *it;
	struct dwaval *dav;
	const char *name = NULL;
	size_t ref = 0, size = 0;

	SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
		switch (dav->dav_dat->dat_attr) {
		case DW_AT_name:
			name = dav2str(dav);
			break;
		case DW_AT_type:
			ref = dav2val(dav, psz);
			break;
		case DW_AT_byte_size:
			size = dav2val(dav, psz);
			assert(size < UINT_MAX);
			break;
		default:
			DPRINTF("%s\n", dw_at2name(dav->dav_dat->dat_attr));
			break;
		}
	}

	it = it_new(++tidx, die->die_offset, name, size, 0, ref, type,
	    ITF_UNRES);

	if (it->it_ref == 0 && (it->it_size == sizeof(void *) ||
	    type == CTF_K_CONST || type == CTF_K_VOLATILE ||
	    type == CTF_K_POINTER || type == CTF_K_TYPEDEF)) {
		/* Work around GCC/clang not emiting a type for void */
		it->it_flags &= ~ITF_UNRES;
		it->it_ref = VOID_OFFSET;
		it->it_refp = void_it;
	}

	return it;
}

struct itype *
parse_array(struct dwdie *die, size_t psz)
{
	struct itype *it;
	struct dwaval *dav;
	const char *name = NULL;
	size_t ref = 0;

	SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
		switch (dav->dav_dat->dat_attr) {
		case DW_AT_name:
			name = dav2str(dav);
			break;
		case DW_AT_type:
			ref = dav2val(dav, psz);
			break;
		default:
			DPRINTF("%s\n", dw_at2name(dav->dav_dat->dat_attr));
			break;
		}
	}

	it = it_new(++tidx, die->die_offset, name, 0, 0, ref, CTF_K_ARRAY,
	    ITF_UNRES);

	subparse_subrange(die, psz, it);

	return it;
}

struct itype *
parse_enum(struct dwdie *die, size_t psz)
{
	struct itype *it;
	struct dwaval *dav;
	const char *name = NULL;
	size_t size = 0;

	SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
		switch (dav->dav_dat->dat_attr) {
		case DW_AT_byte_size:
			size = dav2val(dav, psz);
			assert(size < UINT_MAX);
			break;
		case DW_AT_name:
			name = dav2str(dav);
			break;
		default:
			DPRINTF("%s\n", dw_at2name(dav->dav_dat->dat_attr));
			break;
		}
	}

	it = it_new(++tidx, die->die_offset, name, size, 0, 0, CTF_K_ENUM, 0);

	subparse_enumerator(die, psz, it);

	return it;
}

void
subparse_subrange(struct dwdie *die, size_t psz, struct itype *it)
{
	struct dwaval *dav;

	assert(it->it_type == CTF_K_ARRAY);

	if (die->die_dab->dab_children == DW_CHILDREN_no)
		return;

	/*
	 * This loop assumes that the children of a DIE are just
	 * after it on the list.
	 */
	while ((die = SIMPLEQ_NEXT(die, die_next)) != NULL) {
		uint64_t tag = die->die_dab->dab_tag;
		size_t nelems = 0;

		if (tag != DW_TAG_subrange_type)
			break;

		SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
			switch (dav->dav_dat->dat_attr) {
			case DW_AT_count:
				nelems = dav2val(dav, psz);
				break;
			case DW_AT_upper_bound:
				nelems = dav2val(dav, psz) + 1;
				break;
			default:
				DPRINTF("%s\n",
				    dw_at2name(dav->dav_dat->dat_attr));
				break;
			}
		}

		assert(nelems < UINT_MAX);
		it->it_nelems = nelems;
	}
}

void
subparse_enumerator(struct dwdie *die, size_t psz, struct itype *it)
{
	struct imember *im;
	struct dwaval *dav;

	assert(it->it_type == CTF_K_ENUM);

	if (die->die_dab->dab_children == DW_CHILDREN_no)
		return;

	/*
	 * This loop assumes that the children of a DIE are just
	 * after it on the list.
	 */
	while ((die = SIMPLEQ_NEXT(die, die_next)) != NULL) {
		uint64_t tag = die->die_dab->dab_tag;
		size_t val = 0;
		const char *name = NULL;

		if (tag != DW_TAG_enumerator)
			break;

		SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
			switch (dav->dav_dat->dat_attr) {
			case DW_AT_name:
				name = dav2str(dav);
				break;
			case DW_AT_const_value:
				val = dav2val(dav, psz);
				break;
			default:
				DPRINTF("%s\n",
				    dw_at2name(dav->dav_dat->dat_attr));
				break;
			}
		}

		if (name == NULL) {
			warnx("%s with anon member", it_name(it));
			continue;
		}

		im = im_new(name, val, 0);
		assert(it->it_nelems < UINT_MAX);
		it->it_nelems++;
		TAILQ_INSERT_TAIL(&it->it_members, im, im_next);
	}
}

struct itype *
parse_struct(struct dwdie *die, size_t psz, int type, size_t off)
{
	struct itype *it = NULL;
	struct dwaval *dav;
	const char *name = NULL;
	unsigned int flags = 0;
	size_t size = 0;
	int forward = 0;

	SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
		switch (dav->dav_dat->dat_attr) {
		case DW_AT_declaration:
			forward = dav2val(dav, psz);
			break;
		case DW_AT_byte_size:
			size = dav2val(dav, psz);
			assert(size < UINT_MAX);
			break;
		case DW_AT_name:
			name = dav2str(dav);
			break;
		default:
			DPRINTF("%s\n", dw_at2name(dav->dav_dat->dat_attr));
			break;
		}
	}


	if (forward)
		flags = ITF_FORWARD;
	it = it_new(++tidx, die->die_offset, name, size, 0, 0, type, flags);
	subparse_member(die, psz, it, off);

	return it;
}

void
subparse_member(struct dwdie *die, size_t psz, struct itype *it, size_t offset)
{
	struct imember *im;
	struct dwaval *dav;
	const char *name;
	size_t off = 0, ref = 0, bits = 0;
	uint8_t lvl = die->die_lvl;

	assert(it->it_type == CTF_K_STRUCT || it->it_type == CTF_K_UNION);

	if (die->die_dab->dab_children == DW_CHILDREN_no)
		return;

	/*
	 * This loop assumes that the children of a DIE are just
	 * after it on the list.
	 */
	while ((die = SIMPLEQ_NEXT(die, die_next)) != NULL) {
		int64_t tag = die->die_dab->dab_tag;

		name = NULL;
		if (die->die_lvl <= lvl)
			break;

		/* Skip members of members */
		if (die->die_lvl > lvl + 1)
			continue;
		/*
		 * Nested declaration.
		 *
		 * This matches the case where a ``struct'', ``union'',
		 * ``enum'' or ``typedef'' is first declared "inside" a
		 * union or struct declaration.
		 */
		if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type ||
		    tag == DW_TAG_enumeration_type || tag == DW_TAG_typedef)
			continue;

		it->it_flags |= ITF_UNRES_MEMB;

		SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
			switch (dav->dav_dat->dat_attr) {
			case DW_AT_name:
				name = dav2str(dav);
				break;
			case DW_AT_type:
				ref = dav2val(dav, psz);
				break;
			case DW_AT_data_member_location:
				off = 8 * dav2val(dav, psz);
				break;
			case DW_AT_bit_size:
				bits = dav2val(dav, psz);
				assert(bits < USHRT_MAX);
				break;
			default:
				DPRINTF("%s\n",
				    dw_at2name(dav->dav_dat->dat_attr));
				break;
			}
		}

		/*
		 * When a structure is declared inside an union, we
		 * have to generate a reference to make the resolver
		 * happy.
		 */
		if ((ref == 0) && (tag == DW_TAG_structure_type))
			ref = die->die_offset - offset;

		im = im_new(name, ref, off);
		assert(it->it_nelems < UINT_MAX);
		it->it_nelems++;
		TAILQ_INSERT_TAIL(&it->it_members, im, im_next);
	}
}


void
subparse_arguments(struct dwdie *die, size_t psz, struct itype *it)
{
	struct imember *im;
	struct dwaval *dav;
	size_t ref = 0;

	assert(it->it_type == CTF_K_FUNCTION);

	if (die->die_dab->dab_children == DW_CHILDREN_no)
		return;

	/*
	 * This loop assumes that the children of a DIE are after it
	 * on the list.
	 */
	while ((die = SIMPLEQ_NEXT(die, die_next)) != NULL) {
		uint64_t tag = die->die_dab->dab_tag;

		if (tag == DW_TAG_unspecified_parameters) {
			/* TODO */
			continue;
		}

		/*
		 * Nested declaration.
		 *
		 * This matches the case where a ``struct'', ``union'',
		 * ``enum'', ``typedef'' or ``static'' variable is first
		 * declared inside a function declaration.
		 */
		switch (tag) {
		case DW_TAG_structure_type:
		case DW_TAG_union_type:
		case DW_TAG_enumeration_type:
		case DW_TAG_typedef:
		case DW_TAG_variable:
			continue;
		default:
			break;
		}

		if (tag != DW_TAG_formal_parameter)
			break;

		it->it_flags |= ITF_UNRES_MEMB;

		SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
			switch (dav->dav_dat->dat_attr) {
			case DW_AT_type:
				ref = dav2val(dav, psz);
				break;
			default:
				DPRINTF("%s\n",
				    dw_at2name(dav->dav_dat->dat_attr));
				break;
			}
		}

		im = im_new(NULL, ref, 0);
		assert(it->it_nelems < UINT_MAX);
		it->it_nelems++;
		TAILQ_INSERT_TAIL(&it->it_members, im, im_next);
	}
}

struct itype *
parse_function(struct dwdie *die, size_t psz)
{
	struct itype *it;
	struct dwaval *dav;
	const char *name = NULL;
	size_t ref = 0;

	SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
		switch (dav->dav_dat->dat_attr) {
		case DW_AT_name:
			name = dav2str(dav);
			break;
		case DW_AT_type:
			ref = dav2val(dav, psz);
			break;
		case DW_AT_abstract_origin:
			/*
			 * Skip second empty definition for inline
			 * functions.
			 */
			return NULL;
		default:
			DPRINTF("%s\n", dw_at2name(dav->dav_dat->dat_attr));
			break;
		}
	}

	/*
	 * Work around for clang 4.0 generating DW_TAG_subprogram without
	 * any attribute.
	 */
	if (name == NULL)
		return NULL;

	it = it_new(++fidx, die->die_offset, name, 0, 0, ref, CTF_K_FUNCTION,
	    ITF_UNRES|ITF_FUNC);

	subparse_arguments(die, psz, it);

	if (it->it_ref == 0) {
		/* Work around GCC not emiting a type for void */
		it->it_flags &= ~ITF_UNRES;
		it->it_ref = VOID_OFFSET;
		it->it_refp = void_it;
	}

	return it;
}

struct itype *
parse_funcptr(struct dwdie *die, size_t psz)
{
	struct itype *it;
	struct dwaval *dav;
	const char *name = NULL;
	size_t ref = 0;

	SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
		switch (dav->dav_dat->dat_attr) {
		case DW_AT_name:
			name = dav2str(dav);
			break;
		case DW_AT_type:
			ref = dav2val(dav, psz);
			break;
		default:
			DPRINTF("%s\n", dw_at2name(dav->dav_dat->dat_attr));
			break;
		}
	}

	it = it_new(++tidx, die->die_offset, name, 0, 0, ref, CTF_K_FUNCTION,
	    ITF_UNRES);

	subparse_arguments(die, psz, it);

	if (it->it_ref == 0) {
		/* Work around GCC not emiting a type for void */
		it->it_flags &= ~ITF_UNRES;
		it->it_ref = VOID_OFFSET;
		it->it_refp = void_it;
	}

	return it;
}

struct itype *
parse_variable(struct dwdie *die, size_t psz)
{
	struct itype *it = NULL;
	struct dwaval *dav;
	const char *name = NULL;
	size_t ref = 0;
	int forward = 0, global = 0;

	SIMPLEQ_FOREACH(dav, &die->die_avals, dav_next) {
		switch (dav->dav_dat->dat_attr) {
		case DW_AT_declaration:
			forward = dav2val(dav, psz);
			break;
		case DW_AT_name:
			name = dav2str(dav);
			break;
		case DW_AT_type:
			ref = dav2val(dav, psz);
			break;
		case DW_AT_location:
			switch (dav->dav_dat->dat_form) {
			case DW_FORM_block:
			case DW_FORM_block1:
			case DW_FORM_block2:
			case DW_FORM_block4:
				global = 1;
				break;
			default:
				break;
			}
			break;
		default:
			DPRINTF("%s\n", dw_at2name(dav->dav_dat->dat_attr));
			break;
		}
	}


	if (global && !forward && name != NULL) {
		it = it_new(++oidx, die->die_offset, name, 0, 0, ref, 0,
		    ITF_UNRES|ITF_OBJ);
	}

	return it;
}

size_t
dav2val(struct dwaval *dav, size_t psz)
{
	uint64_t val = (uint64_t)-1;

	switch (dav->dav_dat->dat_form) {
	case DW_FORM_addr:
	case DW_FORM_ref_addr:
		if (psz == sizeof(uint32_t))
			val = dav->dav_u32;
		else
			val = dav->dav_u64;
		break;
	case DW_FORM_block1:
	case DW_FORM_block2:
	case DW_FORM_block4:
	case DW_FORM_block:
		dw_loc_parse(&dav->dav_buf, NULL, &val, NULL);
		break;
	case DW_FORM_flag:
	case DW_FORM_data1:
	case DW_FORM_ref1:
		val = dav->dav_u8;
		break;
	case DW_FORM_data2:
	case DW_FORM_ref2:
		val = dav->dav_u16;
		break;
	case DW_FORM_data4:
	case DW_FORM_ref4:
		val = dav->dav_u32;
		break;
	case DW_FORM_sdata:
	case DW_FORM_data8:
	case DW_FORM_ref8:
	case DW_FORM_udata:
	case DW_FORM_ref_udata:
		val = dav->dav_u64;
		break;
	case DW_FORM_strp:
		val = dav->dav_u32;
		break;
	case DW_FORM_flag_present:
		val = 1;
		break;
	default:
		break;
	}

	return val;
}

const char *
dav2str(struct dwaval *dav)
{
	const char *str = NULL;
	extern const char *dstrbuf;
	extern size_t dstrlen;

	switch (dav->dav_dat->dat_form) {
	case DW_FORM_string:
		str = dav->dav_str;
		break;
	case DW_FORM_strp:
		if (dav->dav_u32 >= dstrlen)
			str = NULL;
		else
			str = dstrbuf + dav->dav_u32;
		break;
	default:
		break;
	}

	return str;
}

const char *
enc2name(unsigned short enc)
{
	static const char *enc_name[] = { "address", "boolean", "complex float",
	    "float", "signed", "char", "unsigned", "unsigned char",
	    "imaginary float", "packed decimal", "numeric string", "edited",
	    "signed fixed", "unsigned fixed", "decimal float" };

	if (enc > 0 && enc <= nitems(enc_name))
		return enc_name[enc - 1];

	return "invalid";
}