Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Create and parse buffers containing CTF data.
 */

#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <zlib.h>
#include <elf.h>

#include "ctf_headers.h"
#include "ctftools.h"
#include "strtab.h"
#include "memory.h"

/*
 * Name of the file currently being read, used to print error messages.  We
 * assume that only one file will be read at a time, and thus make no attempt
 * to allow curfile to be used simultaneously by multiple threads.
 *
 * The value is only valid during a call to ctf_load.
 */
static char *curfile;

#define	CTF_BUF_CHUNK_SIZE	(64 * 1024)
#define	RES_BUF_CHUNK_SIZE	(64 * 1024)

struct ctf_buf {
	strtab_t ctb_strtab;	/* string table */
	caddr_t ctb_base;	/* pointer to base of buffer */
	caddr_t ctb_end;	/* pointer to end of buffer */
	caddr_t ctb_ptr;	/* pointer to empty buffer space */
	size_t ctb_size;	/* size of buffer */
	int nptent;		/* number of processed types */
	int ntholes;		/* number of type holes */
};

/*
 * Macros to reverse byte order
 */
#define	BSWAP_8(x)	((x) & 0xff)
#define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
#define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))

#define	SWAP_16(x)	(x) = BSWAP_16(x)
#define	SWAP_32(x)	(x) = BSWAP_32(x)

static int target_requires_swap;

/*PRINTFLIKE1*/
static void __printflike(1, 2) __dead
parseterminate(const char *fmt, ...)
{
	static char msgbuf[1024]; /* sigh */
	va_list ap;

	va_start(ap, fmt);
	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
	va_end(ap);

	terminate("%s: %s\n", curfile, msgbuf);
}

static void
ctf_buf_grow(ctf_buf_t *b)
{
	off_t ptroff = b->ctb_ptr - b->ctb_base;

	b->ctb_size += CTF_BUF_CHUNK_SIZE;
	b->ctb_base = xrealloc(b->ctb_base, b->ctb_size);
	b->ctb_end = b->ctb_base + b->ctb_size;
	b->ctb_ptr = b->ctb_base + ptroff;
}

static ctf_buf_t *
ctf_buf_new(void)
{
	ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t));

	strtab_create(&b->ctb_strtab);
	ctf_buf_grow(b);

	return (b);
}

static void
ctf_buf_free(ctf_buf_t *b)
{
	strtab_destroy(&b->ctb_strtab);
	free(b->ctb_base);
	free(b);
}

static uint_t
ctf_buf_cur(ctf_buf_t *b)
{
	return (b->ctb_ptr - b->ctb_base);
}

static void
ctf_buf_write(ctf_buf_t *b, void const *p, size_t n)
{
	size_t len;

	while (n != 0) {
		if (b->ctb_ptr == b->ctb_end)
			ctf_buf_grow(b);

		len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n);
		bcopy(p, b->ctb_ptr, len);
		b->ctb_ptr += len;

		p = (char const *)p + len;
		n -= len;
	}
}

static int
write_label(void *arg1, void *arg2)
{
	labelent_t *le = arg1;
	ctf_buf_t *b = arg2;
	ctf_lblent_t ctl;

	ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name);
	ctl.ctl_typeidx = le->le_idx;

	if (target_requires_swap) {
		SWAP_32(ctl.ctl_label);
		SWAP_32(ctl.ctl_typeidx);
	}

	ctf_buf_write(b, &ctl, sizeof (ctl));

	return (1);
}

static void
write_objects(iidesc_t *idp, ctf_buf_t *b)
{
	ushort_t id = (idp ? idp->ii_dtype->t_id : 0);

	if (target_requires_swap) {
		SWAP_16(id);
	}

	ctf_buf_write(b, &id, sizeof (id));

	debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id);
}

static void
write_functions(iidesc_t *idp, ctf_buf_t *b)
{
	ushort_t fdata[2];
	ushort_t id;
	int nargs;
	int i;

	if (!idp) {
		fdata[0] = 0;
		ctf_buf_write(b, &fdata[0], sizeof (fdata[0]));

		debug(3, "Wrote function (null)\n");
		return;
	}

	nargs = idp->ii_nargs + (idp->ii_vargs != 0);

	if (nargs > CTF_MAX_VLEN) {
		terminate("function %s has too many args: %d > %d\n",
		    idp->ii_name, nargs, CTF_MAX_VLEN);
	}

	fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs);
	fdata[1] = idp->ii_dtype->t_id;

	if (target_requires_swap) {
		SWAP_16(fdata[0]);
		SWAP_16(fdata[1]);
	}

	ctf_buf_write(b, fdata, sizeof (fdata));

	for (i = 0; i < idp->ii_nargs; i++) {
		id = idp->ii_args[i]->t_id;

		if (target_requires_swap) {
			SWAP_16(id);
		}

		ctf_buf_write(b, &id, sizeof (id));
	}

	if (idp->ii_vargs) {
		id = 0;
		ctf_buf_write(b, &id, sizeof (id));
	}

	debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs);
}

/*
 * Depending on the size of the type being described, either a ctf_stype_t (for
 * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be
 * written.  We isolate the determination here so the rest of the writer code
 * doesn't need to care.
 */
static void
write_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size)
{
	if (size > CTF_MAX_SIZE) {
		ctt->ctt_size = CTF_LSIZE_SENT;
		ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
		ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
		if (target_requires_swap) {
			SWAP_32(ctt->ctt_name);
			SWAP_16(ctt->ctt_info);
			SWAP_16(ctt->ctt_size);
			SWAP_32(ctt->ctt_lsizehi);
			SWAP_32(ctt->ctt_lsizelo);
		}
		ctf_buf_write(b, ctt, sizeof (*ctt));
	} else {
		ctf_stype_t *cts = (ctf_stype_t *)ctt;

		cts->ctt_size = (ushort_t)size;

		if (target_requires_swap) {
			SWAP_32(cts->ctt_name);
			SWAP_16(cts->ctt_info);
			SWAP_16(cts->ctt_size);
		}

		ctf_buf_write(b, cts, sizeof (*cts));
	}
}

static void
write_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt)
{
	ctf_stype_t *cts = (ctf_stype_t *)ctt;

	if (target_requires_swap) {
		SWAP_32(cts->ctt_name);
		SWAP_16(cts->ctt_info);
		SWAP_16(cts->ctt_size);
	}

	ctf_buf_write(b, cts, sizeof (*cts));
}

static int
write_type(void *arg1, void *arg2)
{
	tdesc_t *tp = arg1;
	ctf_buf_t *b = arg2;
	elist_t *ep;
	mlist_t *mp;
	intr_t *ip;

	size_t offset;
	uint_t encoding;
	uint_t data;
	int isroot = tp->t_flags & TDESC_F_ISROOT;
	int i;

	ctf_type_t ctt;
	ctf_array_t cta;
	ctf_member_t ctm;
	ctf_lmember_t ctlm;
	ctf_enum_t cte;
	ushort_t id;

	ctlm.ctlm_pad = 0;

	/*
	 * There shouldn't be any holes in the type list (where a hole is
	 * defined as two consecutive tdescs without consecutive ids), but
	 * check for them just in case.  If we do find holes, we need to make
	 * fake entries to fill the holes, or we won't be able to reconstruct
	 * the tree from the written data.
	 */
	if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
		debug(2, "genctf: type hole from %d < x < %d\n",
		    b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id));

		ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0);
		ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0);
		while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
			write_sized_type_rec(b, &ctt, 0);
			b->nptent++;
		}
	}

	offset = strtab_insert(&b->ctb_strtab, tp->t_name);
	ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);

	switch (tp->t_type) {
	case INTRINSIC:
		ip = tp->t_intr;
		if (ip->intr_type == INTR_INT)
			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER,
			    isroot, 1);
		else
			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1);
		write_sized_type_rec(b, &ctt, tp->t_size);

		encoding = 0;

		if (ip->intr_type == INTR_INT) {
			if (ip->intr_signed)
				encoding |= CTF_INT_SIGNED;
			if (ip->intr_iformat == 'c')
				encoding |= CTF_INT_CHAR;
			else if (ip->intr_iformat == 'b')
				encoding |= CTF_INT_BOOL;
			else if (ip->intr_iformat == 'v')
				encoding |= CTF_INT_VARARGS;
		} else
			encoding = ip->intr_fformat;

		data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits);
		if (target_requires_swap) {
			SWAP_32(data);
		}
		ctf_buf_write(b, &data, sizeof (data));
		break;

	case POINTER:
	case REFERENCE:	/* XXX: */
		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0);
		ctt.ctt_type = tp->t_tdesc->t_id;
		write_unsized_type_rec(b, &ctt);
		break;

	case ARRAY:
		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1);
		write_sized_type_rec(b, &ctt, tp->t_size);

		cta.cta_contents = tp->t_ardef->ad_contents->t_id;
		cta.cta_index = tp->t_ardef->ad_idxtype->t_id;
		cta.cta_nelems = tp->t_ardef->ad_nelems;
		if (target_requires_swap) {
			SWAP_16(cta.cta_contents);
			SWAP_16(cta.cta_index);
			SWAP_32(cta.cta_nelems);
		}
		ctf_buf_write(b, &cta, sizeof (cta));
		break;

	case STRUCT:
	case UNION:
	case CLASS:
		for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next)
			i++; /* count up struct or union members */

		if (i > CTF_MAX_VLEN) {
			warning("sou %s has too many members: %d > %d\n",
			    tdesc_name(tp), i, CTF_MAX_VLEN);
			i = CTF_MAX_VLEN;
		}

		if (tp->t_type == STRUCT)
			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i);
		else
			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i);

		write_sized_type_rec(b, &ctt, tp->t_size);

		if (tp->t_size < CTF_LSTRUCT_THRESH) {
			for (mp = tp->t_members; mp != NULL && i > 0;
			    mp = mp->ml_next) {
				offset = strtab_insert(&b->ctb_strtab,
				    mp->ml_name);

				ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
				    offset);
				ctm.ctm_type = mp->ml_type->t_id;
				ctm.ctm_offset = mp->ml_offset;
				if (target_requires_swap) {
					SWAP_32(ctm.ctm_name);
					SWAP_16(ctm.ctm_type);
					SWAP_16(ctm.ctm_offset);
				}
				ctf_buf_write(b, &ctm, sizeof (ctm));
				i--;
			}
		} else {
			for (mp = tp->t_members; mp != NULL && i > 0;
			    mp = mp->ml_next) {
				offset = strtab_insert(&b->ctb_strtab,
				    mp->ml_name);

				ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
				    offset);
				ctlm.ctlm_type = mp->ml_type->t_id;
				ctlm.ctlm_offsethi =
				    CTF_OFFSET_TO_LMEMHI(mp->ml_offset);
				ctlm.ctlm_offsetlo =
				    CTF_OFFSET_TO_LMEMLO(mp->ml_offset);

				if (target_requires_swap) {
					SWAP_32(ctlm.ctlm_name);
					SWAP_16(ctlm.ctlm_type);
					SWAP_32(ctlm.ctlm_offsethi);
					SWAP_32(ctlm.ctlm_offsetlo);
				}

				ctf_buf_write(b, &ctlm, sizeof (ctlm));
				i--;
			}
		}
		break;

	case ENUM:
		for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next)
			i++; /* count up enum members */

		if (i > CTF_MAX_VLEN) {
			warning("enum %s has too many values: %d > %d\n",
			    tdesc_name(tp), i, CTF_MAX_VLEN);
			i = CTF_MAX_VLEN;
		}

		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i);
		write_sized_type_rec(b, &ctt, tp->t_size);

		for (ep = tp->t_emem; ep != NULL && i > 0; ep = ep->el_next) {
			offset = strtab_insert(&b->ctb_strtab, ep->el_name);
			cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
			cte.cte_value = ep->el_number;

			if (target_requires_swap) {
				SWAP_32(cte.cte_name);
				SWAP_32(cte.cte_value);
			}

			ctf_buf_write(b, &cte, sizeof (cte));
			i--;
		}
		break;

	case FORWARD:
		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0);
		ctt.ctt_type = 0;
		write_unsized_type_rec(b, &ctt);
		break;

	case TYPEDEF:
		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0);
		ctt.ctt_type = tp->t_tdesc->t_id;
		write_unsized_type_rec(b, &ctt);
		break;

	case VOLATILE:
		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0);
		ctt.ctt_type = tp->t_tdesc->t_id;
		write_unsized_type_rec(b, &ctt);
		break;

	case CONST:
		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0);
		ctt.ctt_type = tp->t_tdesc->t_id;
		write_unsized_type_rec(b, &ctt);
		break;

	case FUNCTION:
		i = tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs;

		if (i > CTF_MAX_VLEN) {
			terminate("function %s has too many args: %d > %d\n",
			    tdesc_name(tp), i, CTF_MAX_VLEN);
		}

		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, i);
		ctt.ctt_type = tp->t_fndef->fn_ret->t_id;
		write_unsized_type_rec(b, &ctt);

		for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) {
			id = tp->t_fndef->fn_args[i]->t_id;

			if (target_requires_swap) {
				SWAP_16(id);
			}

			ctf_buf_write(b, &id, sizeof (id));
		}

		if (tp->t_fndef->fn_vargs) {
			id = 0;
			ctf_buf_write(b, &id, sizeof (id));
			i++;
		}

		if (i & 1) {
			id = 0;
			ctf_buf_write(b, &id, sizeof (id));
		}
		break;

	case RESTRICT:
		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0);
		ctt.ctt_type = tp->t_tdesc->t_id;
		write_unsized_type_rec(b, &ctt);
		break;

	default:
		warning("Can't write unknown type %d\n", tp->t_type);
	}

	debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp));

	return (1);
}

typedef struct resbuf {
	caddr_t rb_base;
	caddr_t rb_ptr;
	size_t rb_size;
	z_stream rb_zstr;
} resbuf_t;

static void
rbzs_grow(resbuf_t *rb)
{
	off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base;

	rb->rb_size += RES_BUF_CHUNK_SIZE;
	rb->rb_base = xrealloc(rb->rb_base, rb->rb_size);
	rb->rb_ptr = rb->rb_base + ptroff;
	rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr);
	rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE;
}

static void
compress_start(resbuf_t *rb)
{
	int rc;

	rb->rb_zstr.zalloc = (alloc_func)0;
	rb->rb_zstr.zfree = (free_func)0;
	rb->rb_zstr.opaque = (voidpf)0;

	if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK)
		parseterminate("zlib start failed: %s", zError(rc));
}

static ssize_t
compress_buffer(void *buf, size_t n, void *data)
{
	resbuf_t *rb = (resbuf_t *)data;
	int rc;

	rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr;
	rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base);
	rb->rb_zstr.next_in = buf;
	rb->rb_zstr.avail_in = n;

	while (rb->rb_zstr.avail_in) {
		if (rb->rb_zstr.avail_out == 0)
			rbzs_grow(rb);

		if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK)
			parseterminate("zlib deflate failed: %s", zError(rc));
	}
	rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;

	return (n);
}

static void
compress_flush(resbuf_t *rb, int type)
{
	int rc;

	for (;;) {
		if (rb->rb_zstr.avail_out == 0)
			rbzs_grow(rb);

		rc = deflate(&rb->rb_zstr, type);
		if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) ||
		    (type == Z_FINISH && rc == Z_STREAM_END))
			break;
		else if (rc != Z_OK)
			parseterminate("zlib finish failed: %s", zError(rc));
	}
	rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
}

static void
compress_end(resbuf_t *rb)
{
	int rc;

	compress_flush(rb, Z_FINISH);

	if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
		parseterminate("zlib end failed: %s", zError(rc));
}

/*
 * Pad the buffer to a power-of-2 boundary
 */
static void
pad_buffer(ctf_buf_t *buf, int align)
{
	uint_t cur = ctf_buf_cur(buf);
	ssize_t topad = (align - (cur % align)) % align;
	static const char pad[8] = { 0 };

	while (topad > 0) {
		ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad));
		topad -= 8;
	}
}

static ssize_t
bcopy_data(void *buf, size_t n, void *data)
{
	caddr_t *posp = (caddr_t *)data;
	bcopy(buf, *posp, n);
	*posp += n;
	return (n);
}

static caddr_t
write_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
{
	caddr_t outbuf;
	caddr_t bufpos;

	outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base)
	    + buf->ctb_strtab.str_size);

	bufpos = outbuf;
	(void) bcopy_data(h, sizeof (ctf_header_t), &bufpos);
	(void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
	    &bufpos);
	(void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos);
	*resszp = bufpos - outbuf;
	return (outbuf);
}

/*
 * Create the compression buffer, and fill it with the CTF and string
 * table data.  We flush the compression state between the two so the
 * dictionary used for the string tables won't be polluted with values
 * that made sense for the CTF data.
 */
static caddr_t
write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
{
	resbuf_t resbuf;
	resbuf.rb_size = RES_BUF_CHUNK_SIZE;
	resbuf.rb_base = xmalloc(resbuf.rb_size);
	bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
	resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);

	compress_start(&resbuf);
	(void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
	    &resbuf);
	compress_flush(&resbuf, Z_FULL_FLUSH);
	(void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf);
	compress_end(&resbuf);

	*resszp = (resbuf.rb_ptr - resbuf.rb_base);
	return (resbuf.rb_base);
}

caddr_t
ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
{
	ctf_buf_t *buf = ctf_buf_new();
	ctf_header_t h;
	caddr_t outbuf;

	int i;

	target_requires_swap = do_compress & CTF_SWAP_BYTES;
	do_compress &= ~CTF_SWAP_BYTES;

	/*
	 * Prepare the header, and create the CTF output buffers.  The data
	 * object section and function section are both lists of 2-byte
	 * integers; we pad these out to the next 4-byte boundary if needed.
	 */
	h.cth_magic = CTF_MAGIC;
	h.cth_version = CTF_VERSION;
	h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
	h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
	    iiburst->iib_td->td_parlabel);
	h.cth_parname = strtab_insert(&buf->ctb_strtab,
	    iiburst->iib_td->td_parname);

	h.cth_lbloff = 0;
	(void) list_iter(iiburst->iib_td->td_labels, write_label,
	    buf);

	pad_buffer(buf, 2);
	h.cth_objtoff = ctf_buf_cur(buf);
	for (i = 0; i < iiburst->iib_nobjts; i++)
		write_objects(iiburst->iib_objts[i], buf);

	pad_buffer(buf, 2);
	h.cth_funcoff = ctf_buf_cur(buf);
	for (i = 0; i < iiburst->iib_nfuncs; i++)
		write_functions(iiburst->iib_funcs[i], buf);

	pad_buffer(buf, 4);
	h.cth_typeoff = ctf_buf_cur(buf);
	(void) list_iter(iiburst->iib_types, write_type, buf);

	debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));

	h.cth_stroff = ctf_buf_cur(buf);
	h.cth_strlen = strtab_size(&buf->ctb_strtab);

	if (target_requires_swap) {
		SWAP_16(h.cth_preamble.ctp_magic);
		SWAP_32(h.cth_parlabel);
		SWAP_32(h.cth_parname);
		SWAP_32(h.cth_lbloff);
		SWAP_32(h.cth_objtoff);
		SWAP_32(h.cth_funcoff);
		SWAP_32(h.cth_typeoff);
		SWAP_32(h.cth_stroff);
		SWAP_32(h.cth_strlen);
	}

	/*
	 * We only do compression for ctfmerge, as ctfconvert is only
	 * supposed to be used on intermediary build objects. This is
	 * significantly faster.
	 */
	if (do_compress)
		outbuf = write_compressed_buffer(&h, buf, resszp);
	else
		outbuf = write_buffer(&h, buf, resszp);

	ctf_buf_free(buf);
	return (outbuf);
}

static void
get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
{
	if (ctt->ctt_size == CTF_LSIZE_SENT) {
		*sizep = (size_t)CTF_TYPE_LSIZE(ctt);
		*incrementp = sizeof (ctf_type_t);
	} else {
		*sizep = ctt->ctt_size;
		*incrementp = sizeof (ctf_stype_t);
	}
}

static int
count_types(ctf_header_t *h, caddr_t data)
{
	caddr_t dptr = data + h->cth_typeoff;
	int count = 0;

	dptr = data + h->cth_typeoff;
	while (dptr < data + h->cth_stroff) {
		void *v = (void *) dptr;
		ctf_type_t *ctt = v;
		size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
		size_t size, increment;

		get_ctt_size(ctt, &size, &increment);

		switch (CTF_INFO_KIND(ctt->ctt_info)) {
		case CTF_K_INTEGER:
		case CTF_K_FLOAT:
			dptr += 4;
			break;
		case CTF_K_POINTER:
		case CTF_K_FORWARD:
		case CTF_K_TYPEDEF:
		case CTF_K_VOLATILE:
		case CTF_K_CONST:
		case CTF_K_RESTRICT:
		case CTF_K_FUNCTION:
			dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
			break;
		case CTF_K_ARRAY:
			dptr += sizeof (ctf_array_t);
			break;
		case CTF_K_STRUCT:
		case CTF_K_UNION:
			if (size < CTF_LSTRUCT_THRESH)
				dptr += sizeof (ctf_member_t) * vlen;
			else
				dptr += sizeof (ctf_lmember_t) * vlen;
			break;
		case CTF_K_ENUM:
			dptr += sizeof (ctf_enum_t) * vlen;
			break;
		case CTF_K_UNKNOWN:
			break;
		default:
			parseterminate("Unknown CTF type %d (#%d) at %#jx",
			    CTF_INFO_KIND(ctt->ctt_info), count,
			    (intmax_t)(dptr - data));
		}

		dptr += increment;
		count++;
	}

	debug(3, "CTF read %d types\n", count);

	return (count);
}

/*
 * Resurrect the labels stored in the CTF data, returning the index associated
 * with a label provided by the caller.  There are several cases, outlined
 * below.  Note that, given two labels, the one associated with the lesser type
 * index is considered to be older than the other.
 *
 *  1. matchlbl == NULL - return the index of the most recent label.
 *  2. matchlbl == "BASE" - return the index of the oldest label.
 *  3. matchlbl != NULL, but doesn't match any labels in the section - warn
 *	the user, and proceed as if matchlbl == "BASE" (for safety).
 *  4. matchlbl != NULL, and matches one of the labels in the section - return
 *	the type index associated with the label.
 */
static int
resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
{
	caddr_t buf = ctfdata + h->cth_lbloff;
	caddr_t sbuf = ctfdata + h->cth_stroff;
	size_t bufsz = h->cth_objtoff - h->cth_lbloff;
	int lastidx = 0, baseidx = -1;
	char *baselabel = NULL;
	ctf_lblent_t *ctl;
	void *v = (void *) buf;

	for (ctl = v; (caddr_t)ctl < buf + bufsz; ctl++) {
		char *label = sbuf + ctl->ctl_label;

		lastidx = ctl->ctl_typeidx;

		debug(3, "Resurrected label %s type idx %d\n", label, lastidx);

		tdata_label_add(td, label, lastidx);

		if (baseidx == -1) {
			baseidx = lastidx;
			baselabel = label;
			if (matchlbl != NULL && streq(matchlbl, "BASE"))
				return (lastidx);
		}

		if (matchlbl != NULL && streq(label, matchlbl))
			return (lastidx);
	}

	if (matchlbl != NULL) {
		/* User provided a label that didn't match */
		warning("%s: Cannot find label `%s' - using base (%s)\n",
		    curfile, matchlbl, (baselabel ? baselabel : "NONE"));

		tdata_label_free(td);
		tdata_label_add(td, baselabel, baseidx);

		return (baseidx);
	}

	return (lastidx);
}

static void
resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
    caddr_t ctfdata, symit_data_t *si)
{
	caddr_t buf = ctfdata + h->cth_objtoff;
	size_t bufsz = h->cth_funcoff - h->cth_objtoff;
	caddr_t dptr;

	symit_reset(si);
	for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
		void *v = (void *) dptr;
		ushort_t id = *((ushort_t *)v);
		iidesc_t *ii;
		GElf_Sym *sym;

		if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
			parseterminate(
			    "Unexpected end of object symbols at %ju of %zu",
			    (intmax_t)(dptr - buf), bufsz);
		}

		if (id == 0) {
			debug(3, "Skipping null object\n");
			continue;
		} else if (id >= tdsize) {
			parseterminate("Reference to invalid type %d", id);
		}

		ii = iidesc_new(symit_name(si));
		ii->ii_dtype = tdarr[id];
		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
			ii->ii_type = II_SVAR;
			ii->ii_owner = xstrdup(symit_curfile(si));
		} else
			ii->ii_type = II_GVAR;
		hash_add(td->td_iihash, ii);

		debug(3, "Resurrected %s object %s (%d) from %s\n",
		    (ii->ii_type == II_GVAR ? "global" : "static"),
		    ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
	}
}

static void
resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
    caddr_t ctfdata, symit_data_t *si)
{
	caddr_t buf = ctfdata + h->cth_funcoff;
	size_t bufsz = h->cth_typeoff - h->cth_funcoff;
	caddr_t dptr = buf;
	iidesc_t *ii;
	ushort_t info;
	ushort_t retid;
	GElf_Sym *sym;
	int i;

	symit_reset(si);
	while (dptr < buf + bufsz) {
		void *v = (void *) dptr;
		info = *((ushort_t *)v);
		dptr += 2;

		if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
			parseterminate("Unexpected end of function symbols");

		if (info == 0) {
			debug(3, "Skipping null function (%s)\n",
			    symit_name(si));
			continue;
		}

		v = (void *) dptr;
		retid = *((ushort_t *)v);
		dptr += 2;

		if (retid >= tdsize)
			parseterminate("Reference to invalid type %d", retid);

		ii = iidesc_new(symit_name(si));
		ii->ii_dtype = tdarr[retid];
		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
			ii->ii_type = II_SFUN;
			ii->ii_owner = xstrdup(symit_curfile(si));
		} else
			ii->ii_type = II_GFUN;
		ii->ii_nargs = CTF_INFO_VLEN(info);
		if (ii->ii_nargs)
			ii->ii_args =
			    xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);

		for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
			v = (void *) dptr;
			ushort_t id = *((ushort_t *)v);
			if (id >= tdsize)
				parseterminate("Reference to invalid type %d",
				    id);
			ii->ii_args[i] = tdarr[id];
		}

		if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
			ii->ii_nargs--;
			ii->ii_vargs = 1;
		}

		hash_add(td->td_iihash, ii);

		debug(3, "Resurrected %s function %s (%d, %d args)\n",
		    (ii->ii_type == II_GFUN ? "global" : "static"),
		    ii->ii_name, retid, ii->ii_nargs);
	}
}

static void
resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
    caddr_t ctfdata, int maxid)
{
	caddr_t buf = ctfdata + h->cth_typeoff;
	size_t bufsz = h->cth_stroff - h->cth_typeoff;
	caddr_t sbuf = ctfdata + h->cth_stroff;
	caddr_t dptr = buf;
	tdesc_t *tdp;
	uint_t data;
	uint_t encoding;
	size_t size, increment;
	int tcnt;
	int iicnt = 0;
	tid_t tid, argid;
	int kind, vlen;
	int i;

	elist_t **epp;
	mlist_t **mpp;
	intr_t *ip;

	ctf_type_t *ctt;
	ctf_array_t *cta;
	ctf_enum_t *cte;

	/*
	 * A maxid of zero indicates a request to resurrect all types, so reset
	 * maxid to the maximum type id.
	 */
	if (maxid == 0)
		maxid = CTF_MAX_TYPE;

	for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
		if (tid > maxid)
			break;

		if (tid >= tdsize)
			parseterminate("Reference to invalid type %d", tid);

		void *v = (void *) dptr;
		ctt = v;

		get_ctt_size(ctt, &size, &increment);
		dptr += increment;

		tdp = tdarr[tid];

		if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
			parseterminate(
			    "Unable to cope with non-zero strtab id");
		if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
			tdp->t_name =
			    xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
		} else
			tdp->t_name = NULL;

		kind = CTF_INFO_KIND(ctt->ctt_info);
		vlen = CTF_INFO_VLEN(ctt->ctt_info);

		switch (kind) {
		case CTF_K_INTEGER:
			tdp->t_type = INTRINSIC;
			tdp->t_size = size;

			v = (void *) dptr;
			data = *((uint_t *)v);
			dptr += sizeof (uint_t);
			encoding = CTF_INT_ENCODING(data);

			ip = xmalloc(sizeof (intr_t));
			ip->intr_type = INTR_INT;
			ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;

			if (encoding & CTF_INT_CHAR)
				ip->intr_iformat = 'c';
			else if (encoding & CTF_INT_BOOL)
				ip->intr_iformat = 'b';
			else if (encoding & CTF_INT_VARARGS)
				ip->intr_iformat = 'v';
			else
				ip->intr_iformat = '\0';

			ip->intr_offset = CTF_INT_OFFSET(data);
			ip->intr_nbits = CTF_INT_BITS(data);
			tdp->t_intr = ip;
			break;

		case CTF_K_FLOAT:
			tdp->t_type = INTRINSIC;
			tdp->t_size = size;

			v = (void *) dptr;
			data = *((uint_t *)v);
			dptr += sizeof (uint_t);

			ip = xcalloc(sizeof (intr_t));
			ip->intr_type = INTR_REAL;
			ip->intr_fformat = CTF_FP_ENCODING(data);
			ip->intr_offset = CTF_FP_OFFSET(data);
			ip->intr_nbits = CTF_FP_BITS(data);
			tdp->t_intr = ip;
			break;

		case CTF_K_POINTER:
			tdp->t_type = POINTER;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_ARRAY:
			tdp->t_type = ARRAY;
			tdp->t_size = size;

			v = (void *) dptr;
			cta = v;
			dptr += sizeof (ctf_array_t);

			tdp->t_ardef = xmalloc(sizeof (ardef_t));
			tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
			tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
			tdp->t_ardef->ad_nelems = cta->cta_nelems;
			break;

		case CTF_K_STRUCT:
		case CTF_K_UNION:
			tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
			tdp->t_size = size;

			if (size < CTF_LSTRUCT_THRESH) {
				for (i = 0, mpp = &tdp->t_members; i < vlen;
				    i++, mpp = &((*mpp)->ml_next)) {
					v = (void *) dptr;
					ctf_member_t *ctm = v;
					dptr += sizeof (ctf_member_t);

					*mpp = xmalloc(sizeof (mlist_t));
					(*mpp)->ml_name = xstrdup(sbuf +
					    ctm->ctm_name);
					(*mpp)->ml_type = tdarr[ctm->ctm_type];
					(*mpp)->ml_offset = ctm->ctm_offset;
					(*mpp)->ml_size = 0;
				}
			} else {
				for (i = 0, mpp = &tdp->t_members; i < vlen;
				    i++, mpp = &((*mpp)->ml_next)) {
					v = (void *) dptr;
					ctf_lmember_t *ctlm = v;
					dptr += sizeof (ctf_lmember_t);

					*mpp = xmalloc(sizeof (mlist_t));
					(*mpp)->ml_name = xstrdup(sbuf +
					    ctlm->ctlm_name);
					(*mpp)->ml_type =
					    tdarr[ctlm->ctlm_type];
					(*mpp)->ml_offset =
					    (int)CTF_LMEM_OFFSET(ctlm);
					(*mpp)->ml_size = 0;
				}
			}

			*mpp = NULL;
			break;

		case CTF_K_ENUM:
			tdp->t_type = ENUM;
			tdp->t_size = size;

			for (i = 0, epp = &tdp->t_emem; i < vlen;
			    i++, epp = &((*epp)->el_next)) {
				v = (void *) dptr;
				cte = v;
				dptr += sizeof (ctf_enum_t);

				*epp = xmalloc(sizeof (elist_t));
				(*epp)->el_name = xstrdup(sbuf + cte->cte_name);
				(*epp)->el_number = cte->cte_value;
			}
			*epp = NULL;
			break;

		case CTF_K_FORWARD:
			tdp->t_type = FORWARD;
			list_add(&td->td_fwdlist, tdp);
			break;

		case CTF_K_TYPEDEF:
			tdp->t_type = TYPEDEF;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_VOLATILE:
			tdp->t_type = VOLATILE;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_CONST:
			tdp->t_type = CONST;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_FUNCTION:
			tdp->t_type = FUNCTION;
			tdp->t_fndef = xcalloc(sizeof (fndef_t));
			tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];

			v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1)));
			if (vlen > 0 && *(ushort_t *)v == 0)
				tdp->t_fndef->fn_vargs = 1;

			tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
			tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
			    vlen - tdp->t_fndef->fn_vargs);

			for (i = 0; i < vlen; i++) {
				v = (void *) dptr;
				argid = *(ushort_t *)v;
				dptr += sizeof (ushort_t);

				if (argid != 0)
					tdp->t_fndef->fn_args[i] = tdarr[argid];
			}

			if (vlen & 1)
				dptr += sizeof (ushort_t);
			break;

		case CTF_K_RESTRICT:
			tdp->t_type = RESTRICT;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_UNKNOWN:
			break;

		default:
			warning("Can't parse unknown CTF type %d\n", kind);
		}

		if (CTF_INFO_ISROOT(ctt->ctt_info)) {
			iidesc_t *ii = iidesc_new(tdp->t_name);
			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
			    tdp->t_type == ENUM)
				ii->ii_type = II_SOU;
			else
				ii->ii_type = II_TYPE;
			ii->ii_dtype = tdp;
			hash_add(td->td_iihash, ii);

			iicnt++;
		}

		debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
		    (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
		    tdesc_name(tdp), tdp->t_id);
	}

	debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
}

/*
 * For lack of other inspiration, we're going to take the boring route.  We
 * count the number of types.  This lets us malloc that many tdesc structs
 * before we start filling them in.  This has the advantage of allowing us to
 * avoid a merge-esque remap step.
 */
static tdata_t *
ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
{
	tdata_t *td = tdata_new();
	tdesc_t **tdarr;
	int ntypes = count_types(h, buf);
	int idx, i;

	/* shudder */
	tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
	tdarr[0] = NULL;
	for (i = 1; i <= ntypes; i++) {
		tdarr[i] = xcalloc(sizeof (tdesc_t));
		tdarr[i]->t_id = i;
	}

	td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);

	/* we have the technology - we can rebuild them */
	idx = resurrect_labels(h, td, buf, label);

	resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
	resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
	resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);

	free(tdarr);

	td->td_nextid = ntypes + 1;

	return (td);
}

static size_t
decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
{
	z_stream zstr;
	int rc;

	zstr.zalloc = (alloc_func)0;
	zstr.zfree = (free_func)0;
	zstr.opaque = (voidpf)0;

	zstr.next_in = (Bytef *)cbuf;
	zstr.avail_in = cbufsz;
	zstr.next_out = (Bytef *)dbuf;
	zstr.avail_out = dbufsz;

	if ((rc = inflateInit(&zstr)) != Z_OK ||
	    (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
	    (rc = inflateEnd(&zstr)) != Z_OK) {
		warning("CTF decompress zlib error %s\n", zError(rc));
		return (0);
	}

	debug(3, "reflated %lu bytes to %lu, pointer at 0x%jx\n",
	    zstr.total_in, zstr.total_out,
	    (intmax_t)((caddr_t)zstr.next_in - cbuf));

	return (zstr.total_out);
}

/*
 * Reconstruct the type tree from a given buffer of CTF data.  Only the types
 * up to the type associated with the provided label, inclusive, will be
 * reconstructed.  If a NULL label is provided, all types will be reconstructed.
 *
 * This function won't work on files that have been uniquified.
 */
tdata_t *
ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
{
	ctf_header_t *h;
	caddr_t ctfdata;
	size_t ctfdatasz;
	tdata_t *td;

	curfile = file;

	if (bufsz < sizeof (ctf_header_t))
		parseterminate("Corrupt CTF - short header");

	void *v = (void *) buf;
	h = v;
	buf += sizeof (ctf_header_t);
	bufsz -= sizeof (ctf_header_t);

	if (h->cth_magic != CTF_MAGIC)
		parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);

	if (h->cth_version != CTF_VERSION)
		parseterminate("Unknown CTF version %d", h->cth_version);

	ctfdatasz = h->cth_stroff + h->cth_strlen;
	if (h->cth_flags & CTF_F_COMPRESS) {
		size_t actual;

		ctfdata = xmalloc(ctfdatasz);
		if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
		    ctfdatasz) {
			parseterminate("Corrupt CTF - short decompression "
			    "(was %zu, expecting %zu)", actual, ctfdatasz);
		}
	} else {
		ctfdata = buf;
		ctfdatasz = bufsz;
	}

	td = ctf_parse(h, ctfdata, si, label);

	if (h->cth_flags & CTF_F_COMPRESS)
		free(ctfdata);

	curfile = NULL;

	return (td);
}