summaryrefslogtreecommitdiff
path: root/usr/src/lib/libctf/common/ctf_diff.c
blob: c8facbc72881e82edaa47358317a194f192be8c6 (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
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright (c) 2015 Joyent, Inc.  All rights reserved.
 */

/*
 * The following is a basic overview of how we diff types in containers (the
 * generally interesting part of diff, and what's used by merge). We maintain
 * two mapping tables, a table of forward mappings (src->dest), and a reverse
 * mapping (dest->src). Both are initialized to contain no mapping, and can also
 * be updated to contain a negative mapping.
 *
 * What we do first is iterate over each type in the src container, and compare
 * it with a type in the destination container. This may involve doing recursive
 * comparisons -- which can involve cycles. To deal with this, whenever we
 * encounter something which may be cyclic, we insert a guess. In other words,
 * we assume that it may be true. This is necessary for the classic case of the
 * following structure:
 *
 * struct foo {
 *	struct foo *foo_next;
 * };
 *
 * If it turns out that we were wrong, we discard our guesses.
 *
 * If we find that a given type in src has no corresponding entry in dst, we
 * then mark its map as CTF_ERR (-1) to indicate that it has *no* match, as
 * opposed to the default value of 0, which indicates an unknown match.
 * Once we've done the first iteration through src, we know at that point in
 * time whether everything in dst is similar or not and can simply walk over it
 * and don't have to do any additional checks.
 */

#include <libctf.h>
#include <ctf_impl.h>
#include <sys/debug.h>

typedef struct ctf_diff_func {
	const char *cdf_name;
	ulong_t cdf_symidx;
	ulong_t cdf_matchidx;
} ctf_diff_func_t;

typedef struct ctf_diff_obj {
	const char *cdo_name;
	ulong_t cdo_symidx;
	ctf_id_t cdo_id;
	ulong_t cdo_matchidx;
} ctf_diff_obj_t;

typedef struct ctf_diff_guess {
	struct ctf_diff_guess *cdg_next;
	ctf_id_t cdg_iid;
	ctf_id_t cdg_oid;
} ctf_diff_guess_t;

/* typedef in libctf.h */
struct ctf_diff {
	uint_t cds_flags;
	boolean_t cds_tvalid;	/* types valid */
	ctf_file_t *cds_ifp;
	ctf_file_t *cds_ofp;
	ctf_id_t *cds_forward;
	ctf_id_t *cds_reverse;
	size_t cds_fsize;
	size_t cds_rsize;
	ctf_diff_type_f cds_func;
	ctf_diff_guess_t *cds_guess;
	void *cds_arg;
	uint_t cds_nifuncs;
	uint_t cds_nofuncs;
	uint_t cds_nextifunc;
	uint_t cds_nextofunc;
	ctf_diff_func_t *cds_ifuncs;
	ctf_diff_func_t *cds_ofuncs;
	boolean_t cds_ffillip;
	boolean_t cds_fvalid;
	uint_t cds_niobj;
	uint_t cds_noobj;
	uint_t cds_nextiobj;
	uint_t cds_nextoobj;
	ctf_diff_obj_t *cds_iobj;
	ctf_diff_obj_t *cds_oobj;
	boolean_t cds_ofillip;
	boolean_t cds_ovalid;
};

#define	TINDEX(tid) (tid - 1)

/*
 * Team Diff
 */
static int ctf_diff_type(ctf_diff_t *, ctf_file_t *, ctf_id_t, ctf_file_t *,
    ctf_id_t);

static int
ctf_diff_name(ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp, ctf_id_t oid)
{
	const char *iname, *oname;
	const ctf_type_t *itp, *otp;

	if ((itp = ctf_lookup_by_id(&ifp, iid)) == NULL)
		return (CTF_ERR);

	if ((otp = ctf_lookup_by_id(&ofp, oid)) == NULL)
		return (ctf_set_errno(ifp, iid));

	iname = ctf_strptr(ifp, itp->ctt_name);
	oname = ctf_strptr(ofp, otp->ctt_name);

	if ((iname == NULL || oname == NULL) && (iname != oname))
		return (B_TRUE);

	/* Two anonymous names are the same */
	if (iname == NULL && oname == NULL)
		return (B_FALSE);

	return (strcmp(iname, oname) == 0 ? B_FALSE: B_TRUE);
}

/*
 * For floats and ints
 */
static int
ctf_diff_number(ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp, ctf_id_t oid)
{
	ctf_encoding_t ien, den;

	if (ctf_type_encoding(ifp, iid, &ien) != 0)
		return (CTF_ERR);

	if (ctf_type_encoding(ofp, oid, &den) != 0)
		return (ctf_set_errno(ifp, iid));

	if (bcmp(&ien, &den, sizeof (ctf_encoding_t)) != 0)
		return (B_TRUE);

	return (B_FALSE);
}

/*
 * Two typedefs are equivalent, if after we resolve a chain of typedefs, they
 * point to equivalent types. This means that if a size_t is defined as follows:
 *
 * size_t -> ulong_t -> unsigned long
 * size_t -> unsigned long
 *
 * That we'll ultimately end up treating them the same.
 */
static int
ctf_diff_typedef(ctf_diff_t *cds, ctf_file_t *ifp, ctf_id_t iid,
    ctf_file_t *ofp, ctf_id_t oid)
{
	ctf_id_t iref = CTF_ERR, oref = CTF_ERR;

	while (ctf_type_kind(ifp, iid) == CTF_K_TYPEDEF) {
		iref = ctf_type_reference(ifp, iid);
		if (iref == CTF_ERR)
			return (CTF_ERR);
		iid = iref;
	}

	while (ctf_type_kind(ofp, oid) == CTF_K_TYPEDEF) {
		oref = ctf_type_reference(ofp, oid);
		if (oref == CTF_ERR)
			return (CTF_ERR);
		oid = oref;
	}

	VERIFY(iref != CTF_ERR && oref != CTF_ERR);
	return (ctf_diff_type(cds, ifp, iref, ofp, oref));
}

/*
 * Two qualifiers are equivalent iff they point to two equivalent types.
 */
static int
ctf_diff_qualifier(ctf_diff_t *cds, ctf_file_t *ifp, ctf_id_t iid,
    ctf_file_t *ofp, ctf_id_t oid)
{
	ctf_id_t iref, oref;

	iref = ctf_type_reference(ifp, iid);
	if (iref == CTF_ERR)
		return (CTF_ERR);

	oref = ctf_type_reference(ofp, oid);
	if (oref == CTF_ERR)
		return (ctf_set_errno(ifp, ctf_errno(ofp)));

	return (ctf_diff_type(cds, ifp, iref, ofp, oref));
}

/*
 * Two arrays are the same iff they have the same type for contents, the same
 * type for the index, and the same number of elements.
 */
static int
ctf_diff_array(ctf_diff_t *cds, ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp,
    ctf_id_t oid)
{
	int ret;
	ctf_arinfo_t iar, oar;

	if (ctf_array_info(ifp, iid, &iar) == CTF_ERR)
		return (CTF_ERR);

	if (ctf_array_info(ofp, oid, &oar) == CTF_ERR)
		return (ctf_set_errno(ifp, ctf_errno(ofp)));

	ret = ctf_diff_type(cds, ifp, iar.ctr_contents, ofp, oar.ctr_contents);
	if (ret != B_FALSE)
		return (ret);

	if (iar.ctr_nelems != oar.ctr_nelems)
		return (B_TRUE);

	/*
	 * If we're ignoring integer types names, then we're trying to do a bit
	 * of a logical diff and we don't really care about the fact that the
	 * index element might not be the same here, what we care about are the
	 * number of elements and that they're the same type.
	 */
	if ((cds->cds_flags & CTF_DIFF_F_IGNORE_INTNAMES) == 0) {
		ret = ctf_diff_type(cds, ifp, iar.ctr_index, ofp,
		    oar.ctr_index);
		if (ret != B_FALSE)
			return (ret);
	}

	return (B_FALSE);
}

/*
 * Two function pointers are the same if the following is all true:
 *
 *   o They have the same return type
 *   o They have the same number of arguments
 *   o The arguments are of the same type
 *   o They have the same flags
 */
static int
ctf_diff_fptr(ctf_diff_t *cds, ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp,
    ctf_id_t oid)
{
	int ret, i;
	ctf_funcinfo_t ifunc, ofunc;
	ctf_id_t *iids, *oids;

	if (ctf_func_info_by_id(ifp, iid, &ifunc) == CTF_ERR)
		return (CTF_ERR);

	if (ctf_func_info_by_id(ofp, oid, &ofunc) == CTF_ERR)
		return (ctf_set_errno(ifp, ctf_errno(ofp)));

	if (ifunc.ctc_argc != ofunc.ctc_argc)
		return (B_TRUE);

	if (ifunc.ctc_flags != ofunc.ctc_flags)
		return (B_TRUE);

	ret = ctf_diff_type(cds, ifp, ifunc.ctc_return, ofp, ofunc.ctc_return);
	if (ret != B_FALSE)
		return (ret);

	iids = ctf_alloc(sizeof (ctf_id_t) * ifunc.ctc_argc);
	if (iids == NULL)
		return (ctf_set_errno(ifp, ENOMEM));

	oids = ctf_alloc(sizeof (ctf_id_t) * ifunc.ctc_argc);
	if (oids == NULL) {
		ctf_free(iids, sizeof (ctf_id_t) * ifunc.ctc_argc);
		return (ctf_set_errno(ifp, ENOMEM));
	}

	if (ctf_func_args_by_id(ifp, iid, ifunc.ctc_argc, iids) == CTF_ERR) {
		ret = CTF_ERR;
		goto out;
	}

	if (ctf_func_args_by_id(ofp, oid, ofunc.ctc_argc, oids) == CTF_ERR) {
		ret = ctf_set_errno(ifp, ctf_errno(ofp));
		goto out;
	}

	ret = B_TRUE;
	for (i = 0; i < ifunc.ctc_argc; i++) {
		ret = ctf_diff_type(cds, ifp, iids[i], ofp, oids[i]);
		if (ret != B_FALSE)
			goto out;
	}
	ret = B_FALSE;

out:
	ctf_free(iids, sizeof (ctf_id_t) * ifunc.ctc_argc);
	ctf_free(oids, sizeof (ctf_id_t) * ofunc.ctc_argc);
	return (ret);
}

/*
 * Two structures are the same if every member is identical to its corresponding
 * type, at the same offset, and has the same name, as well as them having the
 * same overall size.
 */
static int
ctf_diff_struct(ctf_diff_t *cds, ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp,
    ctf_id_t oid)
{
	ctf_file_t *oifp;
	const ctf_type_t *itp, *otp;
	ssize_t isize, iincr, osize, oincr;
	const ctf_member_t *imp, *omp;
	const ctf_lmember_t *ilmp, *olmp;
	int n;
	ctf_diff_guess_t *cdg;

	oifp = ifp;

	if ((itp = ctf_lookup_by_id(&ifp, iid)) == NULL)
		return (CTF_ERR);

	if ((otp = ctf_lookup_by_id(&ofp, oid)) == NULL)
		return (ctf_set_errno(oifp, ctf_errno(ofp)));

	if (ctf_type_size(ifp, iid) != ctf_type_size(ofp, oid))
		return (B_TRUE);

	if (LCTF_INFO_VLEN(ifp, itp->ctt_info) !=
	    LCTF_INFO_VLEN(ofp, otp->ctt_info))
		return (B_TRUE);

	(void) ctf_get_ctt_size(ifp, itp, &isize, &iincr);
	(void) ctf_get_ctt_size(ofp, otp, &osize, &oincr);

	if (ifp->ctf_version == CTF_VERSION_1 || isize < CTF_LSTRUCT_THRESH) {
		imp = (const ctf_member_t *)((uintptr_t)itp + iincr);
		ilmp = NULL;
	} else {
		imp = NULL;
		ilmp = (const ctf_lmember_t *)((uintptr_t)itp + iincr);
	}

	if (ofp->ctf_version == CTF_VERSION_1 || osize < CTF_LSTRUCT_THRESH) {
		omp = (const ctf_member_t *)((uintptr_t)otp + oincr);
		olmp = NULL;
	} else {
		omp = NULL;
		olmp = (const ctf_lmember_t *)((uintptr_t)otp + oincr);
	}

	/*
	 * Insert our assumption that they're equal for the moment.
	 */
	cdg = ctf_alloc(sizeof (ctf_diff_guess_t));
	if (cdg == NULL)
		return (ctf_set_errno(ifp, ENOMEM));
	cdg->cdg_iid = iid;
	cdg->cdg_oid = oid;
	cdg->cdg_next = cds->cds_guess;
	cds->cds_guess = cdg;
	cds->cds_forward[TINDEX(iid)] = oid;
	cds->cds_reverse[TINDEX(oid)] = iid;

	for (n = LCTF_INFO_VLEN(ifp, itp->ctt_info); n != 0; n--) {
		const char *iname, *oname;
		ulong_t ioff, ooff;
		ctf_id_t itype, otype;
		int ret;

		if (imp != NULL) {
			iname = ctf_strptr(ifp, imp->ctm_name);
			ioff = imp->ctm_offset;
			itype = imp->ctm_type;
		} else {
			iname = ctf_strptr(ifp, ilmp->ctlm_name);
			ioff = CTF_LMEM_OFFSET(ilmp);
			itype = ilmp->ctlm_type;
		}

		if (omp != NULL) {
			oname = ctf_strptr(ofp, omp->ctm_name);
			ooff = omp->ctm_offset;
			otype = omp->ctm_type;
		} else {
			oname = ctf_strptr(ofp, olmp->ctlm_name);
			ooff = CTF_LMEM_OFFSET(olmp);
			otype = olmp->ctlm_type;
		}

		if (ioff != ooff) {
			return (B_TRUE);
		}
		if (strcmp(iname, oname) != 0) {
			return (B_TRUE);
		}
		ret = ctf_diff_type(cds, ifp, itype, ofp, otype);
		if (ret != B_FALSE) {
			return (ret);
		}

		/* Advance our pointers */
		if (imp != NULL)
			imp++;
		if (ilmp != NULL)
			ilmp++;
		if (omp != NULL)
			omp++;
		if (olmp != NULL)
			olmp++;
	}

	return (B_FALSE);
}

/*
 * Two unions are the same if they have the same set of members. This is similar
 * to, but slightly different from a struct. The offsets of members don't
 * matter. However, there is no guarantee of ordering so we have to fall back to
 * doing an O(N^2) scan.
 */
typedef struct ctf_diff_union_member {
	ctf_diff_t *cdum_cds;
	ctf_file_t *cdum_fp;
	ctf_file_t *cdum_iterfp;
	const char *cdum_name;
	ctf_id_t cdum_type;
	int cdum_ret;
} ctf_diff_union_member_t;

typedef struct ctf_diff_union_fp {
	ctf_diff_t *cduf_cds;
	ctf_file_t *cduf_curfp;
	ctf_file_t *cduf_altfp;
	ctf_id_t cduf_type;
	int cduf_ret;
} ctf_diff_union_fp_t;

/* ARGSUSED */
static int
ctf_diff_union_check_member(const char *name, ctf_id_t id, ulong_t off,
    void *arg)
{
	int ret;
	ctf_diff_union_member_t *cdump = arg;

	if (strcmp(name, cdump->cdum_name) != 0)
		return (0);

	ret = ctf_diff_type(cdump->cdum_cds, cdump->cdum_fp, cdump->cdum_type,
	    cdump->cdum_iterfp, id);
	if (ret == CTF_ERR) {
		cdump->cdum_ret = CTF_ERR;
		return (1);
	}

	if (ret == B_FALSE) {
		cdump->cdum_ret = B_FALSE;
		/* Return non-zero to stop iteration as we have a match */
		return (1);
	}

	return (0);
}

/* ARGSUSED */
static int
ctf_diff_union_check_fp(const char *name, ctf_id_t id, ulong_t off, void *arg)
{
	int ret;
	ctf_diff_union_member_t cdum;
	ctf_diff_union_fp_t *cdufp = arg;

	cdum.cdum_cds = cdufp->cduf_cds;
	cdum.cdum_fp = cdufp->cduf_curfp;
	cdum.cdum_iterfp = cdufp->cduf_altfp;
	cdum.cdum_name = name;
	cdum.cdum_type = id;
	cdum.cdum_ret = B_TRUE;

	ret = ctf_member_iter(cdum.cdum_iterfp, cdufp->cduf_type,
	    ctf_diff_union_check_member, &cdum);
	if (ret == 0 || cdum.cdum_ret == CTF_ERR) {
		/* No match found or error, terminate now */
		cdufp->cduf_ret = cdum.cdum_ret;
		return (1);
	} else if (ret == CTF_ERR) {
		(void) ctf_set_errno(cdum.cdum_fp, ctf_errno(cdum.cdum_iterfp));
		cdufp->cduf_ret = CTF_ERR;
		return (1);
	} else {
		ASSERT(cdum.cdum_ret == B_FALSE);
		cdufp->cduf_ret = cdum.cdum_ret;
		return (0);
	}
}

static int
ctf_diff_union(ctf_diff_t *cds, ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp,
    ctf_id_t oid)
{
	ctf_file_t *oifp;
	const ctf_type_t *itp, *otp;
	ctf_diff_union_fp_t cduf;
	ctf_diff_guess_t *cdg;
	int ret;

	oifp = ifp;
	if ((itp = ctf_lookup_by_id(&ifp, iid)) == NULL)
		return (CTF_ERR);
	if ((otp = ctf_lookup_by_id(&ofp, oid)) == NULL)
		return (ctf_set_errno(oifp, ctf_errno(ofp)));

	if (LCTF_INFO_VLEN(ifp, itp->ctt_info) !=
	    LCTF_INFO_VLEN(ofp, otp->ctt_info))
		return (B_TRUE);

	cdg = ctf_alloc(sizeof (ctf_diff_guess_t));
	if (cdg == NULL)
		return (ctf_set_errno(ifp, ENOMEM));
	cdg->cdg_iid = iid;
	cdg->cdg_oid = oid;
	cdg->cdg_next = cds->cds_guess;
	cds->cds_guess = cdg;
	cds->cds_forward[TINDEX(iid)] = oid;
	cds->cds_reverse[TINDEX(oid)] = iid;

	cduf.cduf_cds = cds;
	cduf.cduf_curfp = ifp;
	cduf.cduf_altfp = ofp;
	cduf.cduf_type = oid;
	cduf.cduf_ret = B_TRUE;
	ret = ctf_member_iter(ifp, iid, ctf_diff_union_check_fp, &cduf);
	if (ret != CTF_ERR)
		ret = cduf.cduf_ret;

	return (ret);
}

/*
 * Two enums are equivalent if they share the same underlying type and they have
 * the same set of members.
 */
static int
ctf_diff_enum(ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp, ctf_id_t oid)
{
	ctf_file_t *oifp;
	const ctf_type_t *itp, *otp;
	ssize_t iincr, oincr;
	const ctf_enum_t *iep, *oep;
	int n;

	oifp = ifp;
	if ((itp = ctf_lookup_by_id(&ifp, iid)) == NULL)
		return (CTF_ERR);
	if ((otp = ctf_lookup_by_id(&ofp, oid)) == NULL)
		return (ctf_set_errno(oifp, ctf_errno(ofp)));

	if (LCTF_INFO_VLEN(ifp, itp->ctt_info) !=
	    LCTF_INFO_VLEN(ofp, otp->ctt_info))
		return (B_TRUE);

	(void) ctf_get_ctt_size(ifp, itp, NULL, &iincr);
	(void) ctf_get_ctt_size(ofp, otp, NULL, &oincr);
	iep = (const ctf_enum_t *)((uintptr_t)itp + iincr);
	oep = (const ctf_enum_t *)((uintptr_t)otp + oincr);

	for (n = LCTF_INFO_VLEN(ifp, itp->ctt_info); n != 0;
	    n--, iep++, oep++) {
		if (strcmp(ctf_strptr(ifp, iep->cte_name),
		    ctf_strptr(ofp, oep->cte_name)) != 0)
			return (B_TRUE);

		if (iep->cte_value != oep->cte_value)
			return (B_TRUE);
	}

	return (B_FALSE);
}

/*
 * Two forwards are equivalent in one of two cases. If both are forwards, than
 * they are the same. Otherwise, they're equivalent if one is a struct or union
 * and the other is a forward.
 */
static int
ctf_diff_forward(ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp, ctf_id_t oid)
{
	int ikind, okind;

	ikind = ctf_type_kind(ifp, iid);
	okind = ctf_type_kind(ofp, oid);

	if (ikind == okind) {
		ASSERT(ikind == CTF_K_FORWARD);
		return (B_FALSE);
	} else if (ikind == CTF_K_FORWARD) {
		return (okind != CTF_K_UNION && okind != CTF_K_STRUCT);
	} else {
		return (ikind != CTF_K_UNION && ikind != CTF_K_STRUCT);
	}
}

/*
 * Are two types equivalent?
 */
int
ctf_diff_type(ctf_diff_t *cds, ctf_file_t *ifp, ctf_id_t iid, ctf_file_t *ofp,
    ctf_id_t oid)
{
	int ret, ikind, okind;

	/* Do a quick short circuit */
	if (ifp == ofp && iid == oid)
		return (B_FALSE);

	/*
	 * Check if it's something we've already encountered in a forward
	 * reference or forward negative table. Also double check the reverse
	 * table.
	 */
	if (cds->cds_forward[TINDEX(iid)] == oid)
		return (B_FALSE);
	if (cds->cds_forward[TINDEX(iid)] != 0)
		return (B_TRUE);
	if (cds->cds_reverse[TINDEX(oid)] == iid)
		return (B_FALSE);
	if ((cds->cds_flags & CTF_DIFF_F_IGNORE_INTNAMES) == 0 &&
	    cds->cds_reverse[TINDEX(oid)] != 0)
		return (B_TRUE);

	ikind = ctf_type_kind(ifp, iid);
	okind = ctf_type_kind(ofp, oid);

	if (ikind != okind &&
	    ikind != CTF_K_FORWARD && okind != CTF_K_FORWARD)
			return (B_TRUE);

	/* Check names */
	if ((ret = ctf_diff_name(ifp, iid, ofp, oid)) != B_FALSE) {
		if (ikind != okind || ikind != CTF_K_INTEGER ||
		    (cds->cds_flags & CTF_DIFF_F_IGNORE_INTNAMES) == 0)
			return (ret);
	}

	if (ikind == CTF_K_FORWARD || okind == CTF_K_FORWARD)
		return (ctf_diff_forward(ifp, iid, ofp, oid));

	switch (ikind) {
	case CTF_K_INTEGER:
	case CTF_K_FLOAT:
		ret = ctf_diff_number(ifp, iid, ofp, oid);
		break;
	case CTF_K_ARRAY:
		ret = ctf_diff_array(cds, ifp, iid, ofp, oid);
		break;
	case CTF_K_FUNCTION:
		ret = ctf_diff_fptr(cds, ifp, iid, ofp, oid);
		break;
	case CTF_K_STRUCT:
		ret = ctf_diff_struct(cds, ifp, iid, ofp, oid);
		break;
	case CTF_K_UNION:
		ret = ctf_diff_union(cds, ifp, iid, ofp, oid);
		break;
	case CTF_K_ENUM:
		ret = ctf_diff_enum(ifp, iid, ofp, oid);
		break;
	case CTF_K_FORWARD:
		ret = ctf_diff_forward(ifp, iid, ofp, oid);
		break;
	case CTF_K_TYPEDEF:
		ret = ctf_diff_typedef(cds, ifp, iid, ofp, oid);
		break;
	case CTF_K_POINTER:
	case CTF_K_VOLATILE:
	case CTF_K_CONST:
	case CTF_K_RESTRICT:
		ret = ctf_diff_qualifier(cds, ifp, iid, ofp, oid);
		break;
	case CTF_K_UNKNOWN:
		/*
		 * The current CTF tools use CTF_K_UNKNOWN as a padding type. We
		 * always declare two instances of CTF_K_UNKNOWN as different,
		 * even though this leads to additional diff noise.
		 */
		ret = B_TRUE;
		break;
	default:
		abort();
	}

	return (ret);
}

/*
 * Walk every type in the first container and try to find a match in the second.
 * If there is a match, then update both the forward and reverse mapping tables.
 *
 * The self variable tells us whether or not we should be comparing the input
 * ctf container with itself or not.
 */
static int
ctf_diff_pass1(ctf_diff_t *cds, boolean_t self)
{
	int i, j, diff;
	int istart, iend, jstart, jend;

	istart = 1;
	iend = cds->cds_ifp->ctf_typemax;
	if (cds->cds_ifp->ctf_flags & LCTF_CHILD) {
		istart += CTF_CHILD_START;
		iend += CTF_CHILD_START;
	}

	jstart = 1;
	jend = cds->cds_ofp->ctf_typemax;
	if (cds->cds_ofp->ctf_flags & LCTF_CHILD) {
		jstart += CTF_CHILD_START;
		jend += CTF_CHILD_START;
	}

	for (i = istart; i <= iend; i++) {
		diff = B_TRUE;

		/*
		 * If we're doing a self diff for dedup purposes, then we want
		 * to ensure that we compare a type i with every type in the
		 * range, [ 1, i ). Yes, this does mean that when i equals 1,
		 * we won't compare anything.
		 */
		if (self == B_TRUE) {
			jstart = istart;
			jend = i - 1;
		}
		for (j = jstart; j <= jend; j++) {
			ctf_diff_guess_t *cdg, *tofree;

			ASSERT(cds->cds_guess == NULL);
			diff = ctf_diff_type(cds, cds->cds_ifp, i,
			    cds->cds_ofp, j);
			if (diff == CTF_ERR)
				return (CTF_ERR);

			/* Clean up our guesses */
			cdg = cds->cds_guess;
			cds->cds_guess = NULL;
			while (cdg != NULL) {
				if (diff == B_TRUE) {
					cds->cds_forward[TINDEX(cdg->cdg_iid)] =
					    0;
					cds->cds_reverse[TINDEX(cdg->cdg_oid)] =
					    0;
				}
				tofree = cdg;
				cdg = cdg->cdg_next;
				ctf_free(tofree, sizeof (ctf_diff_guess_t));
			}

			/* Found a hit, update the tables */
			if (diff == B_FALSE) {
				cds->cds_forward[TINDEX(i)] = j;
				if (cds->cds_reverse[TINDEX(j)] == 0)
					cds->cds_reverse[TINDEX(j)] = i;
				break;
			}
		}

		/* Call the callback at this point */
		if (diff == B_TRUE) {
			cds->cds_forward[TINDEX(i)] = CTF_ERR;
			cds->cds_func(cds->cds_ifp, i, B_FALSE, NULL, CTF_ERR,
			    cds->cds_arg);
		} else {
			cds->cds_func(cds->cds_ifp, i, B_TRUE, cds->cds_ofp, j,
			    cds->cds_arg);
		}
	}

	return (0);
}

/*
 * Now we need to walk the second container and emit anything that we didn't
 * find as common in the first pass.
 */
static int
ctf_diff_pass2(ctf_diff_t *cds)
{
	int i, start, end;

	start = 0x1;
	end = cds->cds_ofp->ctf_typemax;
	if (cds->cds_ofp->ctf_flags & LCTF_CHILD) {
		start += CTF_CHILD_START;
		end += CTF_CHILD_START;
	}

	for (i = start; i <= end; i++) {
		if (cds->cds_reverse[TINDEX(i)] != 0)
			continue;
		cds->cds_func(cds->cds_ofp, i, B_FALSE, NULL, CTF_ERR,
		    cds->cds_arg);
	}

	return (0);
}

int
ctf_diff_init(ctf_file_t *ifp, ctf_file_t *ofp, ctf_diff_t **cdsp)
{
	ctf_diff_t *cds;
	size_t fsize, rsize;

	cds = ctf_alloc(sizeof (ctf_diff_t));
	if (cds == NULL)
		return (ctf_set_errno(ifp, ENOMEM));

	bzero(cds, sizeof (ctf_diff_t));
	cds->cds_ifp = ifp;
	cds->cds_ofp = ofp;

	fsize = sizeof (ctf_id_t) * ifp->ctf_typemax;
	rsize = sizeof (ctf_id_t) * ofp->ctf_typemax;
	if (ifp->ctf_flags & LCTF_CHILD)
		fsize += CTF_CHILD_START * sizeof (ctf_id_t);
	if (ofp->ctf_flags & LCTF_CHILD)
		rsize += CTF_CHILD_START * sizeof (ctf_id_t);

	cds->cds_forward = ctf_alloc(fsize);
	if (cds->cds_forward == NULL) {
		ctf_free(cds, sizeof (ctf_diff_t));
		return (ctf_set_errno(ifp, ENOMEM));
	}
	cds->cds_fsize = fsize;
	cds->cds_reverse = ctf_alloc(rsize);
	if (cds->cds_reverse == NULL) {
		ctf_free(cds->cds_forward, fsize);
		ctf_free(cds, sizeof (ctf_diff_t));
		return (ctf_set_errno(ifp, ENOMEM));
	}
	cds->cds_rsize = rsize;
	bzero(cds->cds_forward, fsize);
	bzero(cds->cds_reverse, rsize);

	cds->cds_ifp->ctf_refcnt++;
	cds->cds_ofp->ctf_refcnt++;
	*cdsp = cds;
	return (0);
}

int
ctf_diff_types(ctf_diff_t *cds, ctf_diff_type_f cb, void *arg)
{
	int ret;

	cds->cds_func = cb;
	cds->cds_arg = arg;

	ret = ctf_diff_pass1(cds, B_FALSE);
	if (ret == 0)
		ret = ctf_diff_pass2(cds);

	cds->cds_func = NULL;
	cds->cds_arg = NULL;
	cds->cds_tvalid = B_TRUE;
	return (ret);
}

/*
 * Do a diff where we're comparing a container with itself. In other words we'd
 * like to know what types are actually duplicates of existing types in the
 * container.
 *
 * Note this should remain private to libctf and not be exported in the public
 * mapfile for the time being.
 */
int
ctf_diff_self(ctf_diff_t *cds, ctf_diff_type_f cb, void *arg)
{
	if (cds->cds_ifp != cds->cds_ofp)
		return (EINVAL);

	cds->cds_func = cb;
	cds->cds_arg = arg;

	return (ctf_diff_pass1(cds, B_TRUE));
}


void
ctf_diff_fini(ctf_diff_t *cds)
{
	ctf_diff_guess_t *cdg;
	size_t fsize, rsize;

	if (cds == NULL)
		return;

	cds->cds_ifp->ctf_refcnt--;
	cds->cds_ofp->ctf_refcnt--;

	fsize = sizeof (ctf_id_t) * cds->cds_ifp->ctf_typemax;
	rsize = sizeof (ctf_id_t) * cds->cds_ofp->ctf_typemax;
	if (cds->cds_ifp->ctf_flags & LCTF_CHILD)
		fsize += CTF_CHILD_START * sizeof (ctf_id_t);
	if (cds->cds_ofp->ctf_flags & LCTF_CHILD)
		rsize += CTF_CHILD_START * sizeof (ctf_id_t);

	if (cds->cds_ifuncs != NULL)
		ctf_free(cds->cds_ifuncs,
		    sizeof (ctf_diff_func_t) * cds->cds_nifuncs);
	if (cds->cds_ofuncs != NULL)
		ctf_free(cds->cds_ofuncs,
		    sizeof (ctf_diff_func_t) * cds->cds_nofuncs);
	if (cds->cds_iobj != NULL)
		ctf_free(cds->cds_iobj,
		    sizeof (ctf_diff_obj_t) * cds->cds_niobj);
	if (cds->cds_oobj != NULL)
		ctf_free(cds->cds_oobj,
		    sizeof (ctf_diff_obj_t) * cds->cds_noobj);
	cdg = cds->cds_guess;
	while (cdg != NULL) {
		ctf_diff_guess_t *tofree = cdg;
		cdg = cdg->cdg_next;
		ctf_free(tofree, sizeof (ctf_diff_guess_t));
	}
	if (cds->cds_forward != NULL)
		ctf_free(cds->cds_forward, cds->cds_fsize);
	if (cds->cds_reverse != NULL)
		ctf_free(cds->cds_reverse, cds->cds_rsize);
	ctf_free(cds, sizeof (ctf_diff_t));
}

uint_t
ctf_diff_getflags(ctf_diff_t *cds)
{
	return (cds->cds_flags);
}

int
ctf_diff_setflags(ctf_diff_t *cds, uint_t flags)
{
	if ((flags & ~CTF_DIFF_F_IGNORE_INTNAMES) != 0)
		return (ctf_set_errno(cds->cds_ifp, EINVAL));

	cds->cds_flags = flags;
	return (0);
}

static boolean_t
ctf_diff_symid(ctf_diff_t *cds, ctf_id_t iid, ctf_id_t oid)
{
	ctf_file_t *ifp, *ofp;

	ifp = cds->cds_ifp;
	ofp = cds->cds_ofp;

	/*
	 * If we have parent containers on the scene here, we need to go through
	 * and do a full diff check because a diff for types will not actually
	 * go through and check types in the parent container.
	 */
	if (iid == 0 || oid == 0)
		return (iid == oid ? B_FALSE: B_TRUE);

	if (!(ifp->ctf_flags & LCTF_CHILD) && !(ofp->ctf_flags & LCTF_CHILD)) {
		if (cds->cds_forward[TINDEX(iid)] != oid)
			return (B_TRUE);
		return (B_FALSE);
	}

	return (ctf_diff_type(cds, ifp, iid, ofp, oid));
}

/* ARGSUSED */
static void
ctf_diff_void_cb(ctf_file_t *ifp, ctf_id_t iid, boolean_t same, ctf_file_t *ofp,
    ctf_id_t oid, void *arg)
{
}

/* ARGSUSED */
static int
ctf_diff_func_count(const char *name, ulong_t symidx, ctf_funcinfo_t *fip,
    void *arg)
{
	uint32_t *ip = arg;

	*ip = *ip + 1;
	return (0);
}

/* ARGSUSED */
static int
ctf_diff_func_fill_cb(const char *name, ulong_t symidx, ctf_funcinfo_t *fip,
    void *arg)
{
	uint_t *next, max;
	ctf_diff_func_t *funcptr;
	ctf_diff_t *cds = arg;

	if (cds->cds_ffillip == B_TRUE) {
		max = cds->cds_nifuncs;
		next = &cds->cds_nextifunc;
		funcptr = cds->cds_ifuncs + *next;
	} else {
		max = cds->cds_nofuncs;
		next = &cds->cds_nextofunc;
		funcptr = cds->cds_ofuncs + *next;

	}

	VERIFY(*next < max);
	funcptr->cdf_name = name;
	funcptr->cdf_symidx = symidx;
	funcptr->cdf_matchidx = ULONG_MAX;
	*next = *next + 1;

	return (0);
}

int
ctf_diff_func_fill(ctf_diff_t *cds)
{
	int ret;
	uint32_t ifcount, ofcount, idcnt, cti;
	ulong_t i, j;
	ctf_id_t *iids, *oids;

	ifcount = 0;
	ofcount = 0;
	idcnt = 0;
	iids = NULL;
	oids = NULL;

	ret = ctf_function_iter(cds->cds_ifp, ctf_diff_func_count, &ifcount);
	if (ret != 0)
		return (ret);
	ret = ctf_function_iter(cds->cds_ofp, ctf_diff_func_count, &ofcount);
	if (ret != 0)
		return (ret);

	cds->cds_ifuncs = ctf_alloc(sizeof (ctf_diff_func_t) * ifcount);
	if (cds->cds_ifuncs == NULL)
		return (ctf_set_errno(cds->cds_ifp, ENOMEM));

	cds->cds_nifuncs = ifcount;
	cds->cds_nextifunc = 0;

	cds->cds_ofuncs = ctf_alloc(sizeof (ctf_diff_func_t) * ofcount);
	if (cds->cds_ofuncs == NULL)
		return (ctf_set_errno(cds->cds_ifp, ENOMEM));

	cds->cds_nofuncs = ofcount;
	cds->cds_nextofunc = 0;

	cds->cds_ffillip = B_TRUE;
	if ((ret = ctf_function_iter(cds->cds_ifp, ctf_diff_func_fill_cb,
	    cds)) != 0)
		return (ret);

	cds->cds_ffillip = B_FALSE;
	if ((ret = ctf_function_iter(cds->cds_ofp, ctf_diff_func_fill_cb,
	    cds)) != 0)
		return (ret);

	/*
	 * Everything is initialized to not match. This could probably be faster
	 * with something that used a hash. But this part of the diff isn't used
	 * by merge.
	 */
	for (i = 0; i < cds->cds_nifuncs; i++) {
		for (j = 0; j < cds->cds_nofuncs; j++) {
			ctf_diff_func_t *ifd, *ofd;
			ctf_funcinfo_t ifip, ofip;
			boolean_t match;

			ifd = &cds->cds_ifuncs[i];
			ofd = &cds->cds_ofuncs[j];
			if (strcmp(ifd->cdf_name, ofd->cdf_name) != 0)
				continue;

			ret = ctf_func_info(cds->cds_ifp, ifd->cdf_symidx,
			    &ifip);
			if (ret != 0)
				goto out;
			ret = ctf_func_info(cds->cds_ofp, ofd->cdf_symidx,
			    &ofip);
			if (ret != 0) {
				ret = ctf_set_errno(cds->cds_ifp,
				    ctf_errno(cds->cds_ofp));
				goto out;
			}

			if (ifip.ctc_argc != ofip.ctc_argc &&
			    ifip.ctc_flags != ofip.ctc_flags)
				continue;

			/* Validate return type and arguments are the same */
			if (ctf_diff_symid(cds, ifip.ctc_return,
			    ofip.ctc_return))
				continue;

			if (ifip.ctc_argc > idcnt) {
				if (iids != NULL)
					ctf_free(iids,
					    sizeof (ctf_id_t) * idcnt);
				if (oids != NULL)
					ctf_free(oids,
					    sizeof (ctf_id_t) * idcnt);
				iids = oids = NULL;
				idcnt = ifip.ctc_argc;
				iids = ctf_alloc(sizeof (ctf_id_t) * idcnt);
				if (iids == NULL) {
					ret = ctf_set_errno(cds->cds_ifp,
					    ENOMEM);
					goto out;
				}
				oids = ctf_alloc(sizeof (ctf_id_t) * idcnt);
				if (iids == NULL) {
					ret = ctf_set_errno(cds->cds_ifp,
					    ENOMEM);
					goto out;
				}
			}

			if ((ret = ctf_func_args(cds->cds_ifp, ifd->cdf_symidx,
			    ifip.ctc_argc, iids)) != 0)
				goto out;
			if ((ret = ctf_func_args(cds->cds_ofp, ofd->cdf_symidx,
			    ofip.ctc_argc, oids)) != 0)
				goto out;

			match = B_TRUE;
			for (cti = 0; cti < ifip.ctc_argc; cti++) {
				if (ctf_diff_symid(cds, iids[cti], oids[cti])) {
					match = B_FALSE;
					break;
				}
			}

			if (match == B_FALSE)
				continue;

			ifd->cdf_matchidx = j;
			ofd->cdf_matchidx = i;
			break;
		}
	}

	ret = 0;

out:
	if (iids != NULL)
		ctf_free(iids, sizeof (ctf_id_t) * idcnt);
	if (oids != NULL)
		ctf_free(oids, sizeof (ctf_id_t) * idcnt);

	return (ret);
}

/*
 * In general, two functions are the same, if they have the same name and their
 * arguments have the same types, including the return type. Like types, we
 * basically have to do this in two passes. In the first phase we walk every
 * type in the first container and try to find a match in the second.
 */
int
ctf_diff_functions(ctf_diff_t *cds, ctf_diff_func_f cb, void *arg)
{
	int ret;
	ulong_t i;

	if (cds->cds_tvalid == B_FALSE) {
		if ((ret = ctf_diff_types(cds, ctf_diff_void_cb, NULL)) != 0)
			return (ret);
	}

	if (cds->cds_fvalid == B_FALSE) {
		if ((ret = ctf_diff_func_fill(cds)) != 0)
			return (ret);
		cds->cds_fvalid = B_TRUE;
	}

	for (i = 0; i < cds->cds_nifuncs; i++) {
		if (cds->cds_ifuncs[i].cdf_matchidx == ULONG_MAX) {
			cb(cds->cds_ifp, cds->cds_ifuncs[i].cdf_symidx,
			    B_FALSE, NULL, ULONG_MAX, arg);
		} else {
			ulong_t idx = cds->cds_ifuncs[i].cdf_matchidx;
			cb(cds->cds_ifp, cds->cds_ifuncs[i].cdf_symidx, B_TRUE,
			    cds->cds_ofp, cds->cds_ofuncs[idx].cdf_symidx, arg);
		}
	}

	for (i = 0; i < cds->cds_nofuncs; i++) {
		if (cds->cds_ofuncs[i].cdf_matchidx != ULONG_MAX)
			continue;
		cb(cds->cds_ofp, cds->cds_ofuncs[i].cdf_symidx, B_FALSE,
		    NULL, ULONG_MAX, arg);
	}

	return (0);
}

static int
ctf_diff_obj_fill_cb(const char *name, ctf_id_t id, ulong_t symidx, void *arg)
{
	uint_t *next, max;
	ctf_diff_obj_t *objptr;
	ctf_diff_t *cds = arg;

	if (cds->cds_ofillip == B_TRUE) {
		max = cds->cds_niobj;
		next = &cds->cds_nextiobj;
		objptr = cds->cds_iobj + *next;
	} else {
		max = cds->cds_noobj;
		next = &cds->cds_nextoobj;
		objptr = cds->cds_oobj+ *next;

	}

	VERIFY(*next < max);
	objptr->cdo_name = name;
	objptr->cdo_symidx = symidx;
	objptr->cdo_id = id;
	objptr->cdo_matchidx = ULONG_MAX;
	*next = *next + 1;

	return (0);
}

/* ARGSUSED */
static int
ctf_diff_obj_count(const char *name, ctf_id_t id, ulong_t symidx, void *arg)
{
	uint32_t *count = arg;

	*count = *count + 1;

	return (0);
}


static int
ctf_diff_obj_fill(ctf_diff_t *cds)
{
	int ret;
	uint32_t iocount, oocount;
	ulong_t i, j;

	iocount = 0;
	oocount = 0;

	ret = ctf_object_iter(cds->cds_ifp, ctf_diff_obj_count, &iocount);
	if (ret != 0)
		return (ret);

	ret = ctf_object_iter(cds->cds_ofp, ctf_diff_obj_count, &oocount);
	if (ret != 0)
		return (ret);

	cds->cds_iobj = ctf_alloc(sizeof (ctf_diff_obj_t) * iocount);
	if (cds->cds_iobj == NULL)
		return (ctf_set_errno(cds->cds_ifp, ENOMEM));
	cds->cds_niobj = iocount;
	cds->cds_nextiobj = 0;

	cds->cds_oobj = ctf_alloc(sizeof (ctf_diff_obj_t) * oocount);
	if (cds->cds_oobj == NULL)
		return (ctf_set_errno(cds->cds_ifp, ENOMEM));
	cds->cds_noobj = oocount;
	cds->cds_nextoobj = 0;

	cds->cds_ofillip = B_TRUE;
	if ((ret = ctf_object_iter(cds->cds_ifp, ctf_diff_obj_fill_cb,
	    cds)) != 0)
		return (ret);

	cds->cds_ofillip = B_FALSE;
	if ((ret = ctf_object_iter(cds->cds_ofp, ctf_diff_obj_fill_cb,
	    cds)) != 0)
		return (ret);

	for (i = 0; i < cds->cds_niobj; i++) {
		for (j = 0; j < cds->cds_noobj; j++) {
			ctf_diff_obj_t *id, *od;

			id = &cds->cds_iobj[i];
			od = &cds->cds_oobj[j];

			if (id->cdo_name == NULL || od->cdo_name == NULL)
				continue;
			if (strcmp(id->cdo_name, od->cdo_name) != 0)
				continue;

			if (ctf_diff_symid(cds, id->cdo_id, od->cdo_id)) {
				continue;
			}

			id->cdo_matchidx = j;
			od->cdo_matchidx = i;
			break;
		}
	}

	return (0);
}

int
ctf_diff_objects(ctf_diff_t *cds, ctf_diff_obj_f cb, void *arg)
{
	int ret;
	ulong_t i;

	if (cds->cds_tvalid == B_FALSE) {
		if ((ret = ctf_diff_types(cds, ctf_diff_void_cb, NULL)) != 0)
			return (ret);
	}

	if (cds->cds_ovalid == B_FALSE) {
		if ((ret = ctf_diff_obj_fill(cds)) != 0)
			return (ret);
		cds->cds_ovalid = B_TRUE;
	}

	for (i = 0; i < cds->cds_niobj; i++) {
		ctf_diff_obj_t *o = &cds->cds_iobj[i];

		if (cds->cds_iobj[i].cdo_matchidx == ULONG_MAX) {
			cb(cds->cds_ifp, o->cdo_symidx, o->cdo_id, B_FALSE,
			    NULL, ULONG_MAX, CTF_ERR, arg);
		} else {
			ctf_diff_obj_t *alt = &cds->cds_oobj[o->cdo_matchidx];
			cb(cds->cds_ifp, o->cdo_symidx, o->cdo_id, B_TRUE,
			    cds->cds_ofp, alt->cdo_symidx, alt->cdo_id, arg);
		}
	}

	for (i = 0; i < cds->cds_noobj; i++) {
		ctf_diff_obj_t *o = &cds->cds_oobj[i];
		if (o->cdo_matchidx != ULONG_MAX)
			continue;
		cb(cds->cds_ofp, o->cdo_symidx, o->cdo_id, B_FALSE, NULL,
		    ULONG_MAX, CTF_ERR, arg);
	}

	return (0);
}