forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathctf-dedup.c
3169 lines (2709 loc) · 100 KB
/
ctf-dedup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CTF type deduplication.
Copyright (C) 2019-2021 Free Software Foundation, Inc.
This file is part of libctf.
libctf is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not see
<http://www.gnu.org/licenses/>. */
#include <ctf-impl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "hashtab.h"
/* (In the below, relevant functions are named in square brackets.) */
/* Type deduplication is a three-phase process:
[ctf_dedup, ctf_dedup_hash_type, ctf_dedup_rhash_type]
1) come up with unambiguous hash values for all types: no two types may have
the same hash value, and any given type should have only one hash value
(for optimal deduplication).
[ctf_dedup, ctf_dedup_detect_name_ambiguity,
ctf_dedup_conflictify_unshared, ctf_dedup_mark_conflicting_hash]
2) mark those distinct types with names that collide (and thus cannot be
declared simultaneously in the same translation unit) as conflicting, and
recursively mark all types that cite one of those types as conflicting as
well. Possibly mark all types cited in only one TU as conflicting, if
the CTF_LINK_SHARE_DUPLICATED link mode is active.
[ctf_dedup_emit, ctf_dedup_emit_struct_members, ctf_dedup_id_to_target]
3) emit all the types, one hash value at a time. Types not marked
conflicting are emitted once, into the shared dictionary: types marked
conflicting are emitted once per TU into a dictionary corresponding to
each TU in which they appear. Structs marked conflicting get at the very
least a forward emitted into the shared dict so that other dicts can cite
it if needed.
[id_to_packed_id]
This all works over an array of inputs (usually in the same order as the
inputs on the link line). We don't use the ctf_link_inputs hash directly
because it is convenient to be able to address specific input types as a
*global type ID* or 'GID', a pair of an array offset and a ctf_id_t. Since
both are already 32 bits or less or can easily be constrained to that range,
we can pack them both into a single 64-bit hash word for easy lookups, which
would be much more annoying to do with a ctf_dict_t * and a ctf_id_t. (On
32-bit platforms, we must do that anyway, since pointers, and thus hash keys
and values, are only 32 bits wide). We track which inputs are parents of
which other inputs so that we can correctly recognize that types we have
traversed in children may cite types in parents, and so that we can process
the parents first.)
Note that thanks to ld -r, the deduplicator can be fed its own output, so the
inputs may themselves have child dicts. Since we need to support this usage
anyway, we can use it in one other place. If the caller finds translation
units to be too small a unit ambiguous types, links can be 'cu-mapped', where
the caller provides a mapping of input TU names to output child dict names.
This mapping can fuse many child TUs into one potential child dict, so that
ambiguous types in any of those input TUs go into the same child dict.
When a many:1 cu-mapping is detected, the ctf_dedup machinery is called
repeatedly, once for every output name that has more than one input, to fuse
all the input TUs associated with a given output dict into one, and once again
as normal to deduplicate all those intermediate outputs (and any 1:1 inputs)
together. This has much higher memory usage than otherwise, because in the
intermediate state, all the output TUs are in memory at once and cannot be
lazily opened. It also has implications for the emission code: if types
appear ambiguously in multiple input TUs that are all mapped to the same
child dict, we cannot put them in children in the cu-mapping link phase
because this output is meant to *become* a child in the next link stage and
parent/child relationships are only one level deep: so instead, we just hide
all but one of the ambiguous types.
There are a few other subtleties here that make this more complex than it
seems. Let's go over the steps above in more detail.
1) HASHING.
[ctf_dedup_hash_type, ctf_dedup_rhash_type]
Hashing proceeds recursively, mixing in the properties of each input type
(including its name, if any), and then adding the hash values of every type
cited by that type. The result is stashed in the cd_type_hashes so other
phases can find the hash values of input types given their IDs, and so that
if we encounter this type again while hashing we can just return its hash
value: it is also stashed in the *output mapping*, a mapping from hash value
to the set of GIDs corresponding to that type in all inputs. We also keep
track of the GID of the first appearance of the type in any input (in
cd_output_first_gid), and the GID of structs, unions, and forwards that only
appear in one TU (in cd_struct_origin). See below for where these things are
used.
Everything in this phase is time-critical, because it is operating over
non-deduplicated types and so may have hundreds or thousands of times the
data volume to deal with than later phases. Trace output is hidden behind
ENABLE_LIBCTF_HASH_DEBUGGING to prevent the sheer number of calls to
ctf_dprintf from slowing things down (tenfold slowdowns are observed purely
from the calls to ctf_dprintf(), even with debugging switched off), and keep
down the volume of output (hundreds of gigabytes of debug output are not
uncommon on larger links).
We have to do *something* about potential cycles in the type graph. We'd
like to avoid emitting forwards in the final output if possible, because
forwards aren't much use: they have no members. We are mostly saved from
needing to worry about this at emission time by ctf_add_struct*()
automatically replacing newly-created forwards when the real struct/union
comes along. So we only have to avoid getting stuck in cycles during the
hashing phase, while also not confusing types that cite members that are
structs with each other. It is easiest to solve this problem by noting two
things:
- all cycles in C depend on the presence of tagged structs/unions
- all tagged structs/unions have a unique name they can be disambiguated by
[ctf_dedup_is_stub]
This means that we can break all cycles by ceasing to hash in cited types at
every tagged struct/union and instead hashing in a stub consisting of the
struct/union's *decorated name*, which is the name preceded by "s " or "u "
depending on the namespace (cached in cd_decorated_names). Forwards are
decorated identically (so a forward to "struct foo" would be represented as
"s foo"): this means that a citation of a forward to a type and a citation of
a concrete definition of a type with the same name ends up getting the same
hash value.
Of course, it is quite possible to have two TUs with structs with the same
name and different definitions, but that's OK because when we scan for types
with ambiguous names we will identify these and mark them conflicting.
We populate one thing to help conflictedness marking. No unconflicted type
may cite a conflicted one, but this means that conflictedness marking must
walk from types to the types that cite them, which is the opposite of the
usual order. We can make this easier to do by constructing a *citers* graph
in cd_citers, which points from types to the types that cite them: because we
emit forwards corresponding to every conflicted struct/union, we don't need
to do this for citations of structs/unions by other types. This is very
convenient for us, because that's the only type we don't traverse
recursively: so we can construct the citers graph at the same time as we
hash, rather than needing to add an extra pass. (This graph is a dynhash of
*type hash values*, so it's small: in effect it is automatically
deduplicated.)
2) COLLISIONAL MARKING.
[ctf_dedup_detect_name_ambiguity, ctf_dedup_mark_conflicting_hash]
We identify types whose names collide during the hashing process, and count
the rough number of uses of each name (caching may throw it off a bit: this
doesn't need to be accurate). We then mark the less-frequently-cited types
with each names conflicting: the most-frequently-cited one goes into the
shared type dictionary, while all others are duplicated into per-TU
dictionaries, named after the input TU, that have the shared dictionary as a
parent. For structures and unions this is not quite good enough: we'd like
to have citations of forwards to ambiguously named structures and unions
*stay* as citations of forwards, so that the user can tell that the caller
didn't actually know which structure definition was meant: but if we put one
of those structures into the shared dictionary, it would supplant and replace
the forward, leaving no sign. So structures and unions do not take part in
this popularity contest: if their names are ambiguous, they are just
duplicated, and only a forward appears in the shared dict.
[ctf_dedup_propagate_conflictedness]
The process of marking types conflicted is itself recursive: we recursively
traverse the cd_citers graph populated in the hashing pass above and mark
everything that we encounter conflicted (without wasting time re-marking
anything that is already marked). This naturally terminates just where we
want it to (at types that are cited by no other types, and at structures and
unions) and suffices to ensure that types that cite conflicted types are
always marked conflicted.
[ctf_dedup_conflictify_unshared, ctf_dedup_multiple_input_dicts]
When linking in CTF_LINK_SHARE_DUPLICATED mode, we would like all types that
are used in only one TU to end up in a per-CU dict. The easiest way to do
that is to mark them conflicted. ctf_dedup_conflictify_unshared does this,
traversing the output mapping and using ctf_dedup_multiple_input_dicts to
check the number of input dicts each distinct type hash value came from:
types that only came from one get marked conflicted. One caveat here is that
we need to consider both structs and forwards to them: a struct that appears
in one TU and has a dozen citations to an opaque forward in other TUs should
*not* be considered to be used in only one TU, because users would find it
useful to be able to traverse into opaque structures of that sort: so we use
cd_struct_origin to check both structs/unions and the forwards corresponding
to them.
3) EMISSION.
[ctf_dedup_walk_output_mapping, ctf_dedup_rwalk_output_mapping,
ctf_dedup_rwalk_one_output_mapping]
Emission involves another walk of the entire output mapping, this time
traversing everything other than struct members, recursively. Types are
emitted from leaves to trunk, emitting all types a type cites before emitting
the type itself. We sort the output mapping before traversing it, for
reproducibility and also correctness: the input dicts may have parent/child
relationships, so we simply sort all types that first appear in parents
before all children, then sort types that first appear in dicts appearing
earlier on the linker command line before those that appear later, then sort
by input ctf_id_t. (This is where we use cd_output_first_gid, collected
above.)
The walking is done using a recursive traverser which arranges to not revisit
any type already visited and to call its callback once per input GID for
input GIDs corresponding to conflicted output types. The traverser only
finds input types and calls a callback for them as many times as the output
needs to appear: it doesn't try to figure out anything about where the output
might go. That's done by the callback based on whether the type is
marked conflicted or not.
[ctf_dedup_emit_type, ctf_dedup_id_to_target, ctf_dedup_synthesize_forward]
ctf_dedup_emit_type is the (sole) callback for ctf_dedup_walk_output_mapping.
Conflicted types have all necessary dictionaries created, and then we emit
the type into each dictionary in turn, working over each input CTF type
corresponding to each hash value and using ctf_dedup_id_to_target to map each
input ctf_id_t into the corresponding type in the output (dealing with input
ctf_id_t's with parents in the process by simply chasing to the parent dict
if the type we're looking up is in there). Emitting structures involves
simply noting that the members of this structure need emission later on:
because you cannot cite a single structure member from another type, we avoid
emitting the members at this stage to keep recursion depths down a bit.
At this point, if we have by some mischance decided that two different types
with child types that hash to different values have in fact got the same hash
value themselves and *not* marked it conflicting, the type walk will walk
only *one* of them and in all likelihood we'll find that we are trying to
emit a type into some child dictionary that references a type that was never
emitted into that dictionary and assertion-fail. This always indicates a bug
in the conflictedness marking machinery or the hashing code, or both.
ctf_dedup_id_to_target calls ctf_dedup_synthesize_forward to do one extra
thing, alluded to above: if this is a conflicted tagged structure or union,
and the target is the shared dict (i.e., the type we're being asked to emit
is not itself conflicted so can't just point straight at the conflicted
type), we instead synthesise a forward with the same name, emit it into the
shared dict, record it in cd_output_emission_conflicted_forwards so that we
don't re-emit it, and return it. This means that cycles that contain
conflicts do not cause the entire cycle to be replicated in every child: only
that piece of the cycle which takes you back as far as the closest tagged
struct/union needs to be replicated. This trick means that no part of the
deduplicator needs a cycle detector: every recursive walk can stop at tagged
structures.
[ctf_dedup_emit_struct_members]
The final stage of emission is to walk over all structures with members
that need emission and emit all of them. Every type has been emitted at
this stage, so emission cannot fail.
[ctf_dedup_populate_type_mappings, ctf_dedup_populate_type_mapping]
Finally, we update the input -> output type ID mappings used by the ctf-link
machinery to update all the other sections. This is surprisingly expensive
and may be replaced with a scheme which lets the ctf-link machinery extract
the needed info directly from the deduplicator. */
/* Possible future optimizations are flagged with 'optimization opportunity'
below. */
/* Global optimization opportunity: a GC pass, eliminating types with no direct
or indirect citations from the other sections in the dictionary. */
/* Internal flag values for ctf_dedup_hash_type. */
/* Child call: consider forwardable types equivalent to forwards or stubs below
this point. */
#define CTF_DEDUP_HASH_INTERNAL_CHILD 0x01
/* Transform references to single ctf_id_ts in passed-in inputs into a number
that will fit in a uint64_t. Needs rethinking if CTF_MAX_TYPE is boosted.
On 32-bit platforms, we pack things together differently: see the note
above. */
#if UINTPTR_MAX < UINT64_MAX
# define IDS_NEED_ALLOCATION 1
# define CTF_DEDUP_GID(fp, input, type) id_to_packed_id (fp, input, type)
# define CTF_DEDUP_GID_TO_INPUT(id) packed_id_to_input (id)
# define CTF_DEDUP_GID_TO_TYPE(id) packed_id_to_type (id)
#else
# define CTF_DEDUP_GID(fp, input, type) \
(void *) (((uint64_t) input) << 32 | (type))
# define CTF_DEDUP_GID_TO_INPUT(id) ((int) (((uint64_t) id) >> 32))
# define CTF_DEDUP_GID_TO_TYPE(id) (ctf_id_t) (((uint64_t) id) & ~(0xffffffff00000000ULL))
#endif
#ifdef IDS_NEED_ALLOCATION
/* This is the 32-bit path, which stores GIDs in a pool and returns a pointer
into the pool. It is notably less efficient than the 64-bit direct storage
approach, but with a smaller key, this is all we can do. */
static void *
id_to_packed_id (ctf_dict_t *fp, int input_num, ctf_id_t type)
{
const void *lookup;
ctf_type_id_key_t *dynkey = NULL;
ctf_type_id_key_t key = { input_num, type };
if (!ctf_dynhash_lookup_kv (fp->ctf_dedup.cd_id_to_dict_t,
&key, &lookup, NULL))
{
if ((dynkey = malloc (sizeof (ctf_type_id_key_t))) == NULL)
goto oom;
memcpy (dynkey, &key, sizeof (ctf_type_id_key_t));
if (ctf_dynhash_insert (fp->ctf_dedup.cd_id_to_dict_t, dynkey, NULL) < 0)
goto oom;
ctf_dynhash_lookup_kv (fp->ctf_dedup.cd_id_to_dict_t,
dynkey, &lookup, NULL);
}
/* We use a raw assert() here because there isn't really a way to get any sort
of error back from this routine without vastly complicating things for the
much more common case of !IDS_NEED_ALLOCATION. */
assert (lookup);
return (void *) lookup;
oom:
free (dynkey);
ctf_set_errno (fp, ENOMEM);
return NULL;
}
static int
packed_id_to_input (const void *id)
{
const ctf_type_id_key_t *key = (ctf_type_id_key_t *) id;
return key->ctii_input_num;
}
static ctf_id_t
packed_id_to_type (const void *id)
{
const ctf_type_id_key_t *key = (ctf_type_id_key_t *) id;
return key->ctii_type;
}
#endif
/* Make an element in a dynhash-of-dynsets, or return it if already present. */
static ctf_dynset_t *
make_set_element (ctf_dynhash_t *set, const void *key)
{
ctf_dynset_t *element;
if ((element = ctf_dynhash_lookup (set, key)) == NULL)
{
if ((element = ctf_dynset_create (htab_hash_string,
ctf_dynset_eq_string,
NULL)) == NULL)
return NULL;
if (ctf_dynhash_insert (set, (void *) key, element) < 0)
{
ctf_dynset_destroy (element);
return NULL;
}
}
return element;
}
/* Initialize the dedup atoms table. */
int
ctf_dedup_atoms_init (ctf_dict_t *fp)
{
if (fp->ctf_dedup_atoms)
return 0;
if (!fp->ctf_dedup_atoms_alloc)
{
if ((fp->ctf_dedup_atoms_alloc
= ctf_dynset_create (htab_hash_string, ctf_dynset_eq_string,
free)) == NULL)
return ctf_set_errno (fp, ENOMEM);
}
fp->ctf_dedup_atoms = fp->ctf_dedup_atoms_alloc;
return 0;
}
/* Intern things in the dedup atoms table. */
static const char *
intern (ctf_dict_t *fp, char *atom)
{
const void *foo;
if (atom == NULL)
return NULL;
if (!ctf_dynset_exists (fp->ctf_dedup_atoms, atom, &foo))
{
if (ctf_dynset_insert (fp->ctf_dedup_atoms, atom) < 0)
{
ctf_set_errno (fp, ENOMEM);
return NULL;
}
foo = atom;
}
else
free (atom);
return (const char *) foo;
}
/* Add an indication of the namespace to a type name in a way that is not valid
for C identifiers. Used to maintain hashes of type names to other things
while allowing for the four C namespaces (normal, struct, union, enum).
Return a new dynamically-allocated string. */
static const char *
ctf_decorate_type_name (ctf_dict_t *fp, const char *name, int kind)
{
ctf_dedup_t *d = &fp->ctf_dedup;
const char *ret;
const char *k;
char *p;
size_t i;
switch (kind)
{
case CTF_K_STRUCT:
k = "s ";
i = 0;
break;
case CTF_K_UNION:
k = "u ";
i = 1;
break;
case CTF_K_ENUM:
k = "e ";
i = 2;
break;
default:
k = "";
i = 3;
}
if ((ret = ctf_dynhash_lookup (d->cd_decorated_names[i], name)) == NULL)
{
char *str;
if ((str = malloc (strlen (name) + strlen (k) + 1)) == NULL)
goto oom;
p = stpcpy (str, k);
strcpy (p, name);
ret = intern (fp, str);
if (!ret)
goto oom;
if (ctf_dynhash_cinsert (d->cd_decorated_names[i], name, ret) < 0)
goto oom;
}
return ret;
oom:
ctf_set_errno (fp, ENOMEM);
return NULL;
}
/* Hash a type, possibly debugging-dumping something about it as well. */
static inline void
ctf_dedup_sha1_add (ctf_sha1_t *sha1, const void *buf, size_t len,
const char *description _libctf_unused_,
unsigned long depth _libctf_unused_)
{
ctf_sha1_add (sha1, buf, len);
#ifdef ENABLE_LIBCTF_HASH_DEBUGGING
ctf_sha1_t tmp;
char tmp_hval[CTF_SHA1_SIZE];
tmp = *sha1;
ctf_sha1_fini (&tmp, tmp_hval);
ctf_dprintf ("%lu: after hash addition of %s: %s\n", depth, description,
tmp_hval);
#endif
}
static const char *
ctf_dedup_hash_type (ctf_dict_t *fp, ctf_dict_t *input,
ctf_dict_t **inputs, uint32_t *parents,
int input_num, ctf_id_t type, int flags,
unsigned long depth,
int (*populate_fun) (ctf_dict_t *fp,
ctf_dict_t *input,
ctf_dict_t **inputs,
int input_num,
ctf_id_t type,
void *id,
const char *decorated_name,
const char *hash));
/* Determine whether this type is being hashed as a stub (in which case it is
unsafe to cache it). */
static int
ctf_dedup_is_stub (const char *name, int kind, int fwdkind, int flags)
{
/* We can cache all types unless we are recursing to children and are hashing
in a tagged struct, union or forward, all of which are replaced with their
decorated name as a stub and will have different hash values when hashed at
the top level. */
return ((flags & CTF_DEDUP_HASH_INTERNAL_CHILD) && name
&& (kind == CTF_K_STRUCT || kind == CTF_K_UNION
|| (kind == CTF_K_FORWARD && (fwdkind == CTF_K_STRUCT
|| fwdkind == CTF_K_UNION))));
}
/* Populate struct_origin if need be (not already populated, or populated with
a different origin), in which case it must go to -1, "shared".)
Only called for forwards or forwardable types with names, when the link mode
is CTF_LINK_SHARE_DUPLICATED. */
static int
ctf_dedup_record_origin (ctf_dict_t *fp, int input_num, const char *decorated,
void *id)
{
ctf_dedup_t *d = &fp->ctf_dedup;
void *origin;
int populate_origin = 0;
if (ctf_dynhash_lookup_kv (d->cd_struct_origin, decorated, NULL, &origin))
{
if (CTF_DEDUP_GID_TO_INPUT (origin) != input_num
&& CTF_DEDUP_GID_TO_INPUT (origin) != -1)
{
populate_origin = 1;
origin = CTF_DEDUP_GID (fp, -1, -1);
}
}
else
{
populate_origin = 1;
origin = id;
}
if (populate_origin)
if (ctf_dynhash_cinsert (d->cd_struct_origin, decorated, origin) < 0)
return ctf_set_errno (fp, errno);
return 0;
}
/* Do the underlying hashing and recursion for ctf_dedup_hash_type (which it
calls, recursively). */
static const char *
ctf_dedup_rhash_type (ctf_dict_t *fp, ctf_dict_t *input, ctf_dict_t **inputs,
uint32_t *parents, int input_num, ctf_id_t type,
void *type_id, const ctf_type_t *tp, const char *name,
const char *decorated, int kind, int flags,
unsigned long depth,
int (*populate_fun) (ctf_dict_t *fp,
ctf_dict_t *input,
ctf_dict_t **inputs,
int input_num,
ctf_id_t type,
void *id,
const char *decorated_name,
const char *hash))
{
ctf_dedup_t *d = &fp->ctf_dedup;
ctf_next_t *i = NULL;
ctf_sha1_t hash;
ctf_id_t child_type;
char hashbuf[CTF_SHA1_SIZE];
const char *hval = NULL;
const char *whaterr;
int err;
const char *citer = NULL;
ctf_dynset_t *citers = NULL;
/* Add a citer to the citers set. */
#define ADD_CITER(citers, hval) \
do \
{ \
whaterr = N_("error updating citers"); \
if (!citers) \
if ((citers = ctf_dynset_create (htab_hash_string, \
ctf_dynset_eq_string, \
NULL)) == NULL) \
goto oom; \
if (ctf_dynset_cinsert (citers, hval) < 0) \
goto oom; \
} while (0)
/* If this is a named struct or union or a forward to one, and this is a child
traversal, treat this type as if it were a forward -- do not recurse to
children, ignore all content not already hashed in, and hash in the
decorated name of the type instead. */
if (ctf_dedup_is_stub (name, kind, tp->ctt_type, flags))
{
#ifdef ENABLE_LIBCTF_HASH_DEBUGGING
ctf_dprintf ("Struct/union/forward citation: substituting forwarding "
"stub with decorated name %s\n", decorated);
#endif
ctf_sha1_init (&hash);
ctf_dedup_sha1_add (&hash, decorated, strlen (decorated) + 1,
"decorated struct/union/forward name", depth);
ctf_sha1_fini (&hash, hashbuf);
if ((hval = intern (fp, strdup (hashbuf))) == NULL)
{
ctf_err_warn (fp, 0, 0, _("%s (%i): out of memory during forwarding-"
"stub hashing for type with GID %p"),
ctf_link_input_name (input), input_num, type_id);
return NULL; /* errno is set for us. */
}
/* In share-duplicated link mode, make sure the origin of this type is
recorded, even if this is a type in a parent dict which will not be
directly traversed. */
if (d->cd_link_flags & CTF_LINK_SHARE_DUPLICATED
&& ctf_dedup_record_origin (fp, input_num, decorated, type_id) < 0)
return NULL; /* errno is set for us. */
return hval;
}
/* Now ensure that subsequent recursive calls (but *not* the top-level call)
get this treatment. */
flags |= CTF_DEDUP_HASH_INTERNAL_CHILD;
/* If this is a struct, union, or forward with a name, record the unique
originating input TU, if there is one. */
if (decorated && (ctf_forwardable_kind (kind) || kind != CTF_K_FORWARD))
if (d->cd_link_flags & CTF_LINK_SHARE_DUPLICATED
&& ctf_dedup_record_origin (fp, input_num, decorated, type_id) < 0)
return NULL; /* errno is set for us. */
#ifdef ENABLE_LIBCTF_HASH_DEBUGGING
ctf_dprintf ("%lu: hashing thing with ID %i/%lx (kind %i): %s.\n",
depth, input_num, type, kind, name ? name : "");
#endif
/* Some type kinds don't have names: the API provides no way to set the name,
so the type the deduplicator outputs will be nameless even if the input
somehow has a name, and the name should not be mixed into the hash. */
switch (kind)
{
case CTF_K_POINTER:
case CTF_K_ARRAY:
case CTF_K_FUNCTION:
case CTF_K_VOLATILE:
case CTF_K_CONST:
case CTF_K_RESTRICT:
case CTF_K_SLICE:
name = NULL;
}
/* Mix in invariant stuff, transforming the type kind if needed. Note that
the vlen is *not* hashed in: the actual variable-length info is hashed in
instead, piecewise. The vlen is not part of the type, only the
variable-length data is: identical types with distinct vlens are quite
possible. Equally, we do not want to hash in the isroot flag: both the
compiler and the deduplicator set the nonroot flag to indicate clashes with
*other types in the same TU* with the same name: so two types can easily
have distinct nonroot flags, yet be exactly the same type.*/
ctf_sha1_init (&hash);
if (name)
ctf_dedup_sha1_add (&hash, name, strlen (name) + 1, "name", depth);
ctf_dedup_sha1_add (&hash, &kind, sizeof (uint32_t), "kind", depth);
/* Hash content of this type. */
switch (kind)
{
case CTF_K_UNKNOWN:
/* No extra state. */
break;
case CTF_K_FORWARD:
/* Add the forwarded kind, stored in the ctt_type. */
ctf_dedup_sha1_add (&hash, &tp->ctt_type, sizeof (tp->ctt_type),
"forwarded kind", depth);
break;
case CTF_K_INTEGER:
case CTF_K_FLOAT:
{
ctf_encoding_t ep;
memset (&ep, 0, sizeof (ctf_encoding_t));
ctf_dedup_sha1_add (&hash, &tp->ctt_size, sizeof (uint32_t), "size",
depth);
if (ctf_type_encoding (input, type, &ep) < 0)
{
whaterr = N_("error getting encoding");
goto err;
}
ctf_dedup_sha1_add (&hash, &ep, sizeof (ctf_encoding_t), "encoding",
depth);
break;
}
/* Types that reference other types. */
case CTF_K_TYPEDEF:
case CTF_K_VOLATILE:
case CTF_K_CONST:
case CTF_K_RESTRICT:
case CTF_K_POINTER:
/* Hash the referenced type, if not already hashed, and mix it in. */
child_type = ctf_type_reference (input, type);
if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
child_type, flags, depth,
populate_fun)) == NULL)
{
whaterr = N_("error doing referenced type hashing");
goto err;
}
ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "referenced type",
depth);
citer = hval;
break;
/* The slices of two types hash identically only if the type they overlay
also has the same encoding. This is not ideal, but in practice will work
well enough. We work directly rather than using the CTF API because
we do not want the slice's normal automatically-shine-through
semantics to kick in here. */
case CTF_K_SLICE:
{
const ctf_slice_t *slice;
const ctf_dtdef_t *dtd;
ssize_t size;
ssize_t increment;
child_type = ctf_type_reference (input, type);
ctf_get_ctt_size (input, tp, &size, &increment);
ctf_dedup_sha1_add (&hash, &size, sizeof (ssize_t), "size", depth);
if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
child_type, flags, depth,
populate_fun)) == NULL)
{
whaterr = N_("error doing slice-referenced type hashing");
goto err;
}
ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "sliced type",
depth);
citer = hval;
if ((dtd = ctf_dynamic_type (input, type)) != NULL)
slice = &dtd->dtd_u.dtu_slice;
else
slice = (ctf_slice_t *) ((uintptr_t) tp + increment);
ctf_dedup_sha1_add (&hash, &slice->cts_offset,
sizeof (slice->cts_offset), "slice offset", depth);
ctf_dedup_sha1_add (&hash, &slice->cts_bits,
sizeof (slice->cts_bits), "slice bits", depth);
break;
}
case CTF_K_ARRAY:
{
ctf_arinfo_t ar;
if (ctf_array_info (input, type, &ar) < 0)
{
whaterr = N_("error getting array info");
goto err;
}
if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
ar.ctr_contents, flags, depth,
populate_fun)) == NULL)
{
whaterr = N_("error doing array contents type hashing");
goto err;
}
ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "array contents",
depth);
ADD_CITER (citers, hval);
if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
ar.ctr_index, flags, depth,
populate_fun)) == NULL)
{
whaterr = N_("error doing array index type hashing");
goto err;
}
ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "array index",
depth);
ctf_dedup_sha1_add (&hash, &ar.ctr_nelems, sizeof (ar.ctr_nelems),
"element count", depth);
ADD_CITER (citers, hval);
break;
}
case CTF_K_FUNCTION:
{
ctf_funcinfo_t fi;
ctf_id_t *args;
uint32_t j;
if (ctf_func_type_info (input, type, &fi) < 0)
{
whaterr = N_("error getting func type info");
goto err;
}
if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
fi.ctc_return, flags, depth,
populate_fun)) == NULL)
{
whaterr = N_("error getting func return type");
goto err;
}
ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "func return",
depth);
ctf_dedup_sha1_add (&hash, &fi.ctc_argc, sizeof (fi.ctc_argc),
"func argc", depth);
ctf_dedup_sha1_add (&hash, &fi.ctc_flags, sizeof (fi.ctc_flags),
"func flags", depth);
ADD_CITER (citers, hval);
if ((args = calloc (fi.ctc_argc, sizeof (ctf_id_t))) == NULL)
{
whaterr = N_("error doing memory allocation");
goto err;
}
if (ctf_func_type_args (input, type, fi.ctc_argc, args) < 0)
{
free (args);
whaterr = N_("error getting func arg type");
goto err;
}
for (j = 0; j < fi.ctc_argc; j++)
{
if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents,
input_num, args[j], flags, depth,
populate_fun)) == NULL)
{
free (args);
whaterr = N_("error doing func arg type hashing");
goto err;
}
ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "func arg type",
depth);
ADD_CITER (citers, hval);
}
free (args);
break;
}
case CTF_K_ENUM:
{
int val;
const char *ename;
ctf_dedup_sha1_add (&hash, &tp->ctt_size, sizeof (uint32_t),
"enum size", depth);
while ((ename = ctf_enum_next (input, type, &i, &val)) != NULL)
{
ctf_dedup_sha1_add (&hash, ename, strlen (ename) + 1, "enumerator",
depth);
ctf_dedup_sha1_add (&hash, &val, sizeof (val), "enumerand", depth);
}
if (ctf_errno (input) != ECTF_NEXT_END)
{
whaterr = N_("error doing enum member iteration");
goto err;
}
break;
}
/* Top-level only. */
case CTF_K_STRUCT:
case CTF_K_UNION:
{
ssize_t offset;
const char *mname;
ctf_id_t membtype;
ssize_t size;
ctf_get_ctt_size (input, tp, &size, NULL);
ctf_dedup_sha1_add (&hash, &size, sizeof (ssize_t), "struct size",
depth);
while ((offset = ctf_member_next (input, type, &i, &mname,
&membtype)) >= 0)
{
if (mname == NULL)
mname = "";
ctf_dedup_sha1_add (&hash, mname, strlen (mname) + 1,
"member name", depth);
#ifdef ENABLE_LIBCTF_HASH_DEBUGGING
ctf_dprintf ("%lu: Traversing to member %s\n", depth, mname);
#endif
if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents,
input_num, membtype, flags, depth,
populate_fun)) == NULL)
{
whaterr = N_("error doing struct/union member type hashing");
goto iterr;
}
ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "member hash",
depth);
ctf_dedup_sha1_add (&hash, &offset, sizeof (offset), "member offset",
depth);
ADD_CITER (citers, hval);
}
if (ctf_errno (input) != ECTF_NEXT_END)
{
whaterr = N_("error doing struct/union member iteration");
goto err;
}
break;
}
default:
whaterr = N_("error: unknown type kind");
goto err;
}
ctf_sha1_fini (&hash, hashbuf);
if ((hval = intern (fp, strdup (hashbuf))) == NULL)
{
whaterr = N_("cannot intern hash");
goto oom;
}
/* Populate the citers for this type's subtypes, now the hash for the type
itself is known. */
whaterr = N_("error tracking citers");
if (citer)
{
ctf_dynset_t *citer_hashes;
if ((citer_hashes = make_set_element (d->cd_citers, citer)) == NULL)
goto oom;
if (ctf_dynset_cinsert (citer_hashes, hval) < 0)
goto oom;
}
else if (citers)
{
const void *k;
while ((err = ctf_dynset_cnext (citers, &i, &k)) == 0)
{
ctf_dynset_t *citer_hashes;
citer = (const char *) k;
if ((citer_hashes = make_set_element (d->cd_citers, citer)) == NULL)
goto oom;
if (ctf_dynset_exists (citer_hashes, hval, NULL))
continue;
if (ctf_dynset_cinsert (citer_hashes, hval) < 0)
goto oom;
}
if (err != ECTF_NEXT_END)
goto err;
ctf_dynset_destroy (citers);
}
return hval;
iterr:
ctf_next_destroy (i);
err:
ctf_sha1_fini (&hash, NULL);
ctf_err_warn (fp, 0, 0, _("%s (%i): %s: during type hashing for type %lx, "
"kind %i"), ctf_link_input_name (input),
input_num, gettext (whaterr), type, kind);
return NULL;
oom:
ctf_set_errno (fp, errno);
ctf_err_warn (fp, 0, 0, _("%s (%i): %s: during type hashing for type %lx, "
"kind %i"), ctf_link_input_name (input),
input_num, gettext (whaterr), type, kind);
return NULL;
}
/* Hash a TYPE in the INPUT: FP is the eventual output, where the ctf_dedup
state is stored. INPUT_NUM is the number of this input in the set of inputs.
Record its hash in FP's cd_type_hashes once it is known. PARENTS is
described in the comment above ctf_dedup.
(The flags argument currently accepts only the flag
CTF_DEDUP_HASH_INTERNAL_CHILD, an implementation detail used to prevent
struct/union hashing in recursive traversals below the TYPE.)
We use the CTF API rather than direct access wherever possible, because types
that appear identical through the API should be considered identical, with
one exception: slices should only be considered identical to other slices,
not to the corresponding unsliced type.