Home | History | Annotate | Line # | Download | only in libctf
      1      1.1  christos /* CTF type deduplication.
      2  1.1.1.3  christos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    This file is part of libctf.
      5      1.1  christos 
      6      1.1  christos    libctf is free software; you can redistribute it and/or modify it under
      7      1.1  christos    the terms of the GNU General Public License as published by the Free
      8      1.1  christos    Software Foundation; either version 3, or (at your option) any later
      9      1.1  christos    version.
     10      1.1  christos 
     11      1.1  christos    This program is distributed in the hope that it will be useful, but
     12      1.1  christos    WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     14      1.1  christos    See the GNU General Public License for more details.
     15      1.1  christos 
     16      1.1  christos    You should have received a copy of the GNU General Public License
     17      1.1  christos    along with this program; see the file COPYING.  If not see
     18      1.1  christos    <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos #include <ctf-impl.h>
     21      1.1  christos #include <string.h>
     22      1.1  christos #include <errno.h>
     23      1.1  christos #include <assert.h>
     24      1.1  christos #include "hashtab.h"
     25      1.1  christos 
     26      1.1  christos /* (In the below, relevant functions are named in square brackets.)  */
     27      1.1  christos 
     28      1.1  christos /* Type deduplication is a three-phase process:
     29      1.1  christos 
     30      1.1  christos     [ctf_dedup, ctf_dedup_hash_type, ctf_dedup_rhash_type]
     31      1.1  christos     1) come up with unambiguous hash values for all types: no two types may have
     32      1.1  christos        the same hash value, and any given type should have only one hash value
     33      1.1  christos        (for optimal deduplication).
     34      1.1  christos 
     35      1.1  christos     [ctf_dedup, ctf_dedup_detect_name_ambiguity,
     36      1.1  christos      ctf_dedup_conflictify_unshared, ctf_dedup_mark_conflicting_hash]
     37      1.1  christos     2) mark those distinct types with names that collide (and thus cannot be
     38      1.1  christos        declared simultaneously in the same translation unit) as conflicting, and
     39      1.1  christos        recursively mark all types that cite one of those types as conflicting as
     40      1.1  christos        well.  Possibly mark all types cited in only one TU as conflicting, if
     41      1.1  christos        the CTF_LINK_SHARE_DUPLICATED link mode is active.
     42      1.1  christos 
     43      1.1  christos     [ctf_dedup_emit, ctf_dedup_emit_struct_members, ctf_dedup_id_to_target]
     44      1.1  christos     3) emit all the types, one hash value at a time.  Types not marked
     45      1.1  christos        conflicting are emitted once, into the shared dictionary: types marked
     46      1.1  christos        conflicting are emitted once per TU into a dictionary corresponding to
     47      1.1  christos        each TU in which they appear.  Structs marked conflicting get at the very
     48      1.1  christos        least a forward emitted into the shared dict so that other dicts can cite
     49      1.1  christos        it if needed.
     50      1.1  christos 
     51      1.1  christos    [id_to_packed_id]
     52      1.1  christos    This all works over an array of inputs (usually in the same order as the
     53      1.1  christos    inputs on the link line).  We don't use the ctf_link_inputs hash directly
     54      1.1  christos    because it is convenient to be able to address specific input types as a
     55      1.1  christos    *global type ID* or 'GID', a pair of an array offset and a ctf_id_t.  Since
     56      1.1  christos    both are already 32 bits or less or can easily be constrained to that range,
     57      1.1  christos    we can pack them both into a single 64-bit hash word for easy lookups, which
     58  1.1.1.2  christos    would be much more annoying to do with a ctf_dict_t * and a ctf_id_t.  (On
     59      1.1  christos    32-bit platforms, we must do that anyway, since pointers, and thus hash keys
     60      1.1  christos    and values, are only 32 bits wide).  We track which inputs are parents of
     61      1.1  christos    which other inputs so that we can correctly recognize that types we have
     62      1.1  christos    traversed in children may cite types in parents, and so that we can process
     63      1.1  christos    the parents first.)
     64      1.1  christos 
     65      1.1  christos    Note that thanks to ld -r, the deduplicator can be fed its own output, so the
     66      1.1  christos    inputs may themselves have child dicts.  Since we need to support this usage
     67      1.1  christos    anyway, we can use it in one other place.  If the caller finds translation
     68      1.1  christos    units to be too small a unit ambiguous types, links can be 'cu-mapped', where
     69      1.1  christos    the caller provides a mapping of input TU names to output child dict names.
     70      1.1  christos    This mapping can fuse many child TUs into one potential child dict, so that
     71      1.1  christos    ambiguous types in any of those input TUs go into the same child dict.
     72      1.1  christos    When a many:1 cu-mapping is detected, the ctf_dedup machinery is called
     73      1.1  christos    repeatedly, once for every output name that has more than one input, to fuse
     74      1.1  christos    all the input TUs associated with a given output dict into one, and once again
     75      1.1  christos    as normal to deduplicate all those intermediate outputs (and any 1:1 inputs)
     76      1.1  christos    together.  This has much higher memory usage than otherwise, because in the
     77      1.1  christos    intermediate state, all the output TUs are in memory at once and cannot be
     78      1.1  christos    lazily opened.  It also has implications for the emission code: if types
     79      1.1  christos    appear ambiguously in multiple input TUs that are all mapped to the same
     80      1.1  christos    child dict, we cannot put them in children in the cu-mapping link phase
     81      1.1  christos    because this output is meant to *become* a child in the next link stage and
     82      1.1  christos    parent/child relationships are only one level deep: so instead, we just hide
     83      1.1  christos    all but one of the ambiguous types.
     84      1.1  christos 
     85      1.1  christos    There are a few other subtleties here that make this more complex than it
     86      1.1  christos    seems.  Let's go over the steps above in more detail.
     87      1.1  christos 
     88      1.1  christos    1) HASHING.
     89      1.1  christos 
     90      1.1  christos    [ctf_dedup_hash_type, ctf_dedup_rhash_type]
     91      1.1  christos    Hashing proceeds recursively, mixing in the properties of each input type
     92      1.1  christos    (including its name, if any), and then adding the hash values of every type
     93      1.1  christos    cited by that type.  The result is stashed in the cd_type_hashes so other
     94      1.1  christos    phases can find the hash values of input types given their IDs, and so that
     95      1.1  christos    if we encounter this type again while hashing we can just return its hash
     96      1.1  christos    value: it is also stashed in the *output mapping*, a mapping from hash value
     97      1.1  christos    to the set of GIDs corresponding to that type in all inputs.  We also keep
     98      1.1  christos    track of the GID of the first appearance of the type in any input (in
     99      1.1  christos    cd_output_first_gid), and the GID of structs, unions, and forwards that only
    100      1.1  christos    appear in one TU (in cd_struct_origin).  See below for where these things are
    101      1.1  christos    used.
    102      1.1  christos 
    103      1.1  christos    Everything in this phase is time-critical, because it is operating over
    104      1.1  christos    non-deduplicated types and so may have hundreds or thousands of times the
    105      1.1  christos    data volume to deal with than later phases.  Trace output is hidden behind
    106      1.1  christos    ENABLE_LIBCTF_HASH_DEBUGGING to prevent the sheer number of calls to
    107      1.1  christos    ctf_dprintf from slowing things down (tenfold slowdowns are observed purely
    108      1.1  christos    from the calls to ctf_dprintf(), even with debugging switched off), and keep
    109      1.1  christos    down the volume of output (hundreds of gigabytes of debug output are not
    110      1.1  christos    uncommon on larger links).
    111      1.1  christos 
    112      1.1  christos    We have to do *something* about potential cycles in the type graph.  We'd
    113      1.1  christos    like to avoid emitting forwards in the final output if possible, because
    114      1.1  christos    forwards aren't much use: they have no members.  We are mostly saved from
    115      1.1  christos    needing to worry about this at emission time by ctf_add_struct*()
    116      1.1  christos    automatically replacing newly-created forwards when the real struct/union
    117      1.1  christos    comes along.  So we only have to avoid getting stuck in cycles during the
    118      1.1  christos    hashing phase, while also not confusing types that cite members that are
    119      1.1  christos    structs with each other.  It is easiest to solve this problem by noting two
    120      1.1  christos    things:
    121      1.1  christos 
    122      1.1  christos     - all cycles in C depend on the presence of tagged structs/unions
    123      1.1  christos     - all tagged structs/unions have a unique name they can be disambiguated by
    124      1.1  christos 
    125      1.1  christos    [ctf_dedup_is_stub]
    126      1.1  christos    This means that we can break all cycles by ceasing to hash in cited types at
    127      1.1  christos    every tagged struct/union and instead hashing in a stub consisting of the
    128      1.1  christos    struct/union's *decorated name*, which is the name preceded by "s " or "u "
    129      1.1  christos    depending on the namespace (cached in cd_decorated_names).  Forwards are
    130      1.1  christos    decorated identically (so a forward to "struct foo" would be represented as
    131      1.1  christos    "s foo"): this means that a citation of a forward to a type and a citation of
    132      1.1  christos    a concrete definition of a type with the same name ends up getting the same
    133      1.1  christos    hash value.
    134      1.1  christos 
    135      1.1  christos    Of course, it is quite possible to have two TUs with structs with the same
    136      1.1  christos    name and different definitions, but that's OK because when we scan for types
    137      1.1  christos    with ambiguous names we will identify these and mark them conflicting.
    138      1.1  christos 
    139      1.1  christos    We populate one thing to help conflictedness marking.  No unconflicted type
    140      1.1  christos    may cite a conflicted one, but this means that conflictedness marking must
    141      1.1  christos    walk from types to the types that cite them, which is the opposite of the
    142      1.1  christos    usual order.  We can make this easier to do by constructing a *citers* graph
    143      1.1  christos    in cd_citers, which points from types to the types that cite them: because we
    144      1.1  christos    emit forwards corresponding to every conflicted struct/union, we don't need
    145      1.1  christos    to do this for citations of structs/unions by other types.  This is very
    146      1.1  christos    convenient for us, because that's the only type we don't traverse
    147      1.1  christos    recursively: so we can construct the citers graph at the same time as we
    148      1.1  christos    hash, rather than needing to add an extra pass.  (This graph is a dynhash of
    149      1.1  christos    *type hash values*, so it's small: in effect it is automatically
    150      1.1  christos    deduplicated.)
    151      1.1  christos 
    152      1.1  christos    2) COLLISIONAL MARKING.
    153      1.1  christos 
    154      1.1  christos    [ctf_dedup_detect_name_ambiguity, ctf_dedup_mark_conflicting_hash]
    155      1.1  christos    We identify types whose names collide during the hashing process, and count
    156      1.1  christos    the rough number of uses of each name (caching may throw it off a bit: this
    157      1.1  christos    doesn't need to be accurate).  We then mark the less-frequently-cited types
    158      1.1  christos    with each names conflicting: the most-frequently-cited one goes into the
    159      1.1  christos    shared type dictionary, while all others are duplicated into per-TU
    160      1.1  christos    dictionaries, named after the input TU, that have the shared dictionary as a
    161      1.1  christos    parent.  For structures and unions this is not quite good enough: we'd like
    162      1.1  christos    to have citations of forwards to ambiguously named structures and unions
    163      1.1  christos    *stay* as citations of forwards, so that the user can tell that the caller
    164      1.1  christos    didn't actually know which structure definition was meant: but if we put one
    165      1.1  christos    of those structures into the shared dictionary, it would supplant and replace
    166      1.1  christos    the forward, leaving no sign.  So structures and unions do not take part in
    167      1.1  christos    this popularity contest: if their names are ambiguous, they are just
    168      1.1  christos    duplicated, and only a forward appears in the shared dict.
    169      1.1  christos 
    170      1.1  christos    [ctf_dedup_propagate_conflictedness]
    171      1.1  christos    The process of marking types conflicted is itself recursive: we recursively
    172      1.1  christos    traverse the cd_citers graph populated in the hashing pass above and mark
    173      1.1  christos    everything that we encounter conflicted (without wasting time re-marking
    174      1.1  christos    anything that is already marked).  This naturally terminates just where we
    175      1.1  christos    want it to (at types that are cited by no other types, and at structures and
    176      1.1  christos    unions) and suffices to ensure that types that cite conflicted types are
    177      1.1  christos    always marked conflicted.
    178      1.1  christos 
    179      1.1  christos    [ctf_dedup_conflictify_unshared, ctf_dedup_multiple_input_dicts]
    180      1.1  christos    When linking in CTF_LINK_SHARE_DUPLICATED mode, we would like all types that
    181      1.1  christos    are used in only one TU to end up in a per-CU dict. The easiest way to do
    182      1.1  christos    that is to mark them conflicted.  ctf_dedup_conflictify_unshared does this,
    183      1.1  christos    traversing the output mapping and using ctf_dedup_multiple_input_dicts to
    184      1.1  christos    check the number of input dicts each distinct type hash value came from:
    185      1.1  christos    types that only came from one get marked conflicted.  One caveat here is that
    186      1.1  christos    we need to consider both structs and forwards to them: a struct that appears
    187      1.1  christos    in one TU and has a dozen citations to an opaque forward in other TUs should
    188      1.1  christos    *not* be considered to be used in only one TU, because users would find it
    189      1.1  christos    useful to be able to traverse into opaque structures of that sort: so we use
    190      1.1  christos    cd_struct_origin to check both structs/unions and the forwards corresponding
    191      1.1  christos    to them.
    192      1.1  christos 
    193      1.1  christos    3) EMISSION.
    194      1.1  christos 
    195      1.1  christos    [ctf_dedup_walk_output_mapping, ctf_dedup_rwalk_output_mapping,
    196      1.1  christos     ctf_dedup_rwalk_one_output_mapping]
    197      1.1  christos    Emission involves another walk of the entire output mapping, this time
    198      1.1  christos    traversing everything other than struct members, recursively.  Types are
    199      1.1  christos    emitted from leaves to trunk, emitting all types a type cites before emitting
    200      1.1  christos    the type itself.  We sort the output mapping before traversing it, for
    201      1.1  christos    reproducibility and also correctness: the input dicts may have parent/child
    202      1.1  christos    relationships, so we simply sort all types that first appear in parents
    203      1.1  christos    before all children, then sort types that first appear in dicts appearing
    204      1.1  christos    earlier on the linker command line before those that appear later, then sort
    205      1.1  christos    by input ctf_id_t.  (This is where we use cd_output_first_gid, collected
    206      1.1  christos    above.)
    207      1.1  christos 
    208      1.1  christos    The walking is done using a recursive traverser which arranges to not revisit
    209      1.1  christos    any type already visited and to call its callback once per input GID for
    210      1.1  christos    input GIDs corresponding to conflicted output types.  The traverser only
    211      1.1  christos    finds input types and calls a callback for them as many times as the output
    212      1.1  christos    needs to appear: it doesn't try to figure out anything about where the output
    213      1.1  christos    might go.  That's done by the callback based on whether the type is
    214      1.1  christos    marked conflicted or not.
    215      1.1  christos 
    216      1.1  christos    [ctf_dedup_emit_type, ctf_dedup_id_to_target, ctf_dedup_synthesize_forward]
    217      1.1  christos    ctf_dedup_emit_type is the (sole) callback for ctf_dedup_walk_output_mapping.
    218      1.1  christos    Conflicted types have all necessary dictionaries created, and then we emit
    219      1.1  christos    the type into each dictionary in turn, working over each input CTF type
    220      1.1  christos    corresponding to each hash value and using ctf_dedup_id_to_target to map each
    221      1.1  christos    input ctf_id_t into the corresponding type in the output (dealing with input
    222      1.1  christos    ctf_id_t's with parents in the process by simply chasing to the parent dict
    223      1.1  christos    if the type we're looking up is in there).  Emitting structures involves
    224      1.1  christos    simply noting that the members of this structure need emission later on:
    225      1.1  christos    because you cannot cite a single structure member from another type, we avoid
    226      1.1  christos    emitting the members at this stage to keep recursion depths down a bit.
    227      1.1  christos 
    228      1.1  christos    At this point, if we have by some mischance decided that two different types
    229      1.1  christos    with child types that hash to different values have in fact got the same hash
    230      1.1  christos    value themselves and *not* marked it conflicting, the type walk will walk
    231      1.1  christos    only *one* of them and in all likelihood we'll find that we are trying to
    232      1.1  christos    emit a type into some child dictionary that references a type that was never
    233      1.1  christos    emitted into that dictionary and assertion-fail.  This always indicates a bug
    234      1.1  christos    in the conflictedness marking machinery or the hashing code, or both.
    235      1.1  christos 
    236      1.1  christos    ctf_dedup_id_to_target calls ctf_dedup_synthesize_forward to do one extra
    237      1.1  christos    thing, alluded to above: if this is a conflicted tagged structure or union,
    238      1.1  christos    and the target is the shared dict (i.e., the type we're being asked to emit
    239      1.1  christos    is not itself conflicted so can't just point straight at the conflicted
    240      1.1  christos    type), we instead synthesise a forward with the same name, emit it into the
    241      1.1  christos    shared dict, record it in cd_output_emission_conflicted_forwards so that we
    242      1.1  christos    don't re-emit it, and return it.  This means that cycles that contain
    243      1.1  christos    conflicts do not cause the entire cycle to be replicated in every child: only
    244      1.1  christos    that piece of the cycle which takes you back as far as the closest tagged
    245      1.1  christos    struct/union needs to be replicated.  This trick means that no part of the
    246      1.1  christos    deduplicator needs a cycle detector: every recursive walk can stop at tagged
    247      1.1  christos    structures.
    248      1.1  christos 
    249      1.1  christos    [ctf_dedup_emit_struct_members]
    250      1.1  christos    The final stage of emission is to walk over all structures with members
    251      1.1  christos    that need emission and emit all of them. Every type has been emitted at
    252      1.1  christos    this stage, so emission cannot fail.
    253      1.1  christos 
    254      1.1  christos    [ctf_dedup_populate_type_mappings, ctf_dedup_populate_type_mapping]
    255      1.1  christos    Finally, we update the input -> output type ID mappings used by the ctf-link
    256      1.1  christos    machinery to update all the other sections.  This is surprisingly expensive
    257      1.1  christos    and may be replaced with a scheme which lets the ctf-link machinery extract
    258      1.1  christos    the needed info directly from the deduplicator.  */
    259      1.1  christos 
    260      1.1  christos /* Possible future optimizations are flagged with 'optimization opportunity'
    261      1.1  christos    below.  */
    262      1.1  christos 
    263      1.1  christos /* Global optimization opportunity: a GC pass, eliminating types with no direct
    264      1.1  christos    or indirect citations from the other sections in the dictionary.  */
    265      1.1  christos 
    266      1.1  christos /* Internal flag values for ctf_dedup_hash_type.  */
    267      1.1  christos 
    268      1.1  christos /* Child call: consider forwardable types equivalent to forwards or stubs below
    269      1.1  christos    this point.  */
    270      1.1  christos #define CTF_DEDUP_HASH_INTERNAL_CHILD         0x01
    271      1.1  christos 
    272      1.1  christos /* Transform references to single ctf_id_ts in passed-in inputs into a number
    273      1.1  christos    that will fit in a uint64_t.  Needs rethinking if CTF_MAX_TYPE is boosted.
    274      1.1  christos 
    275      1.1  christos    On 32-bit platforms, we pack things together differently: see the note
    276      1.1  christos    above.  */
    277      1.1  christos 
    278      1.1  christos #if UINTPTR_MAX < UINT64_MAX
    279      1.1  christos # define IDS_NEED_ALLOCATION 1
    280      1.1  christos # define CTF_DEDUP_GID(fp, input, type) id_to_packed_id (fp, input, type)
    281      1.1  christos # define CTF_DEDUP_GID_TO_INPUT(id) packed_id_to_input (id)
    282      1.1  christos # define CTF_DEDUP_GID_TO_TYPE(id) packed_id_to_type (id)
    283      1.1  christos #else
    284      1.1  christos # define CTF_DEDUP_GID(fp, input, type)	\
    285      1.1  christos   (void *) (((uint64_t) input) << 32 | (type))
    286      1.1  christos # define CTF_DEDUP_GID_TO_INPUT(id) ((int) (((uint64_t) id) >> 32))
    287      1.1  christos # define CTF_DEDUP_GID_TO_TYPE(id) (ctf_id_t) (((uint64_t) id) & ~(0xffffffff00000000ULL))
    288      1.1  christos #endif
    289      1.1  christos 
    290      1.1  christos #ifdef IDS_NEED_ALLOCATION
    291      1.1  christos 
    292      1.1  christos  /* This is the 32-bit path, which stores GIDs in a pool and returns a pointer
    293      1.1  christos     into the pool.  It is notably less efficient than the 64-bit direct storage
    294      1.1  christos     approach, but with a smaller key, this is all we can do.  */
    295      1.1  christos 
    296      1.1  christos static void *
    297  1.1.1.2  christos id_to_packed_id (ctf_dict_t *fp, int input_num, ctf_id_t type)
    298      1.1  christos {
    299      1.1  christos   const void *lookup;
    300      1.1  christos   ctf_type_id_key_t *dynkey = NULL;
    301      1.1  christos   ctf_type_id_key_t key = { input_num, type };
    302      1.1  christos 
    303  1.1.1.2  christos   if (!ctf_dynhash_lookup_kv (fp->ctf_dedup.cd_id_to_dict_t,
    304      1.1  christos 			      &key, &lookup, NULL))
    305      1.1  christos     {
    306      1.1  christos       if ((dynkey = malloc (sizeof (ctf_type_id_key_t))) == NULL)
    307      1.1  christos 	goto oom;
    308      1.1  christos       memcpy (dynkey, &key, sizeof (ctf_type_id_key_t));
    309      1.1  christos 
    310  1.1.1.2  christos       if (ctf_dynhash_insert (fp->ctf_dedup.cd_id_to_dict_t, dynkey, NULL) < 0)
    311      1.1  christos 	goto oom;
    312      1.1  christos 
    313  1.1.1.2  christos       ctf_dynhash_lookup_kv (fp->ctf_dedup.cd_id_to_dict_t,
    314      1.1  christos 			     dynkey, &lookup, NULL);
    315      1.1  christos     }
    316      1.1  christos   /* We use a raw assert() here because there isn't really a way to get any sort
    317      1.1  christos      of error back from this routine without vastly complicating things for the
    318      1.1  christos      much more common case of !IDS_NEED_ALLOCATION.  */
    319      1.1  christos   assert (lookup);
    320      1.1  christos   return (void *) lookup;
    321      1.1  christos 
    322      1.1  christos  oom:
    323      1.1  christos   free (dynkey);
    324      1.1  christos   ctf_set_errno (fp, ENOMEM);
    325      1.1  christos   return NULL;
    326      1.1  christos }
    327      1.1  christos 
    328      1.1  christos static int
    329      1.1  christos packed_id_to_input (const void *id)
    330      1.1  christos {
    331      1.1  christos   const ctf_type_id_key_t *key = (ctf_type_id_key_t *) id;
    332      1.1  christos 
    333      1.1  christos   return key->ctii_input_num;
    334      1.1  christos }
    335      1.1  christos 
    336      1.1  christos static ctf_id_t
    337      1.1  christos packed_id_to_type (const void *id)
    338      1.1  christos {
    339      1.1  christos   const ctf_type_id_key_t *key = (ctf_type_id_key_t *) id;
    340      1.1  christos 
    341      1.1  christos   return key->ctii_type;
    342      1.1  christos }
    343      1.1  christos #endif
    344      1.1  christos 
    345      1.1  christos /* Make an element in a dynhash-of-dynsets, or return it if already present.  */
    346      1.1  christos 
    347      1.1  christos static ctf_dynset_t *
    348      1.1  christos make_set_element (ctf_dynhash_t *set, const void *key)
    349      1.1  christos {
    350      1.1  christos   ctf_dynset_t *element;
    351      1.1  christos 
    352      1.1  christos   if ((element = ctf_dynhash_lookup (set, key)) == NULL)
    353      1.1  christos     {
    354      1.1  christos       if ((element = ctf_dynset_create (htab_hash_string,
    355  1.1.1.2  christos 					htab_eq_string,
    356      1.1  christos 					NULL)) == NULL)
    357      1.1  christos 	return NULL;
    358      1.1  christos 
    359      1.1  christos       if (ctf_dynhash_insert (set, (void *) key, element) < 0)
    360      1.1  christos 	{
    361      1.1  christos 	  ctf_dynset_destroy (element);
    362      1.1  christos 	  return NULL;
    363      1.1  christos 	}
    364      1.1  christos     }
    365      1.1  christos 
    366      1.1  christos   return element;
    367      1.1  christos }
    368      1.1  christos 
    369      1.1  christos /* Initialize the dedup atoms table.  */
    370      1.1  christos int
    371  1.1.1.2  christos ctf_dedup_atoms_init (ctf_dict_t *fp)
    372      1.1  christos {
    373      1.1  christos   if (fp->ctf_dedup_atoms)
    374      1.1  christos     return 0;
    375      1.1  christos 
    376      1.1  christos   if (!fp->ctf_dedup_atoms_alloc)
    377      1.1  christos     {
    378      1.1  christos       if ((fp->ctf_dedup_atoms_alloc
    379  1.1.1.2  christos 	   = ctf_dynset_create (htab_hash_string, htab_eq_string,
    380      1.1  christos 				free)) == NULL)
    381      1.1  christos 	return ctf_set_errno (fp, ENOMEM);
    382      1.1  christos     }
    383      1.1  christos   fp->ctf_dedup_atoms = fp->ctf_dedup_atoms_alloc;
    384      1.1  christos   return 0;
    385      1.1  christos }
    386      1.1  christos 
    387      1.1  christos /* Intern things in the dedup atoms table.  */
    388      1.1  christos 
    389      1.1  christos static const char *
    390  1.1.1.2  christos intern (ctf_dict_t *fp, char *atom)
    391      1.1  christos {
    392      1.1  christos   const void *foo;
    393      1.1  christos 
    394      1.1  christos   if (atom == NULL)
    395      1.1  christos     return NULL;
    396      1.1  christos 
    397      1.1  christos   if (!ctf_dynset_exists (fp->ctf_dedup_atoms, atom, &foo))
    398      1.1  christos     {
    399      1.1  christos       if (ctf_dynset_insert (fp->ctf_dedup_atoms, atom) < 0)
    400      1.1  christos 	{
    401      1.1  christos 	  ctf_set_errno (fp, ENOMEM);
    402      1.1  christos 	  return NULL;
    403      1.1  christos 	}
    404      1.1  christos       foo = atom;
    405      1.1  christos     }
    406      1.1  christos   else
    407      1.1  christos     free (atom);
    408      1.1  christos 
    409      1.1  christos   return (const char *) foo;
    410      1.1  christos }
    411      1.1  christos 
    412      1.1  christos /* Add an indication of the namespace to a type name in a way that is not valid
    413      1.1  christos    for C identifiers.  Used to maintain hashes of type names to other things
    414      1.1  christos    while allowing for the four C namespaces (normal, struct, union, enum).
    415  1.1.1.3  christos    Return a pointer into the cd_decorated_names atoms table.  */
    416      1.1  christos static const char *
    417  1.1.1.2  christos ctf_decorate_type_name (ctf_dict_t *fp, const char *name, int kind)
    418      1.1  christos {
    419      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
    420      1.1  christos   const char *ret;
    421      1.1  christos   const char *k;
    422      1.1  christos   char *p;
    423      1.1  christos   size_t i;
    424      1.1  christos 
    425      1.1  christos   switch (kind)
    426      1.1  christos     {
    427      1.1  christos     case CTF_K_STRUCT:
    428      1.1  christos       k = "s ";
    429      1.1  christos       i = 0;
    430      1.1  christos       break;
    431      1.1  christos     case CTF_K_UNION:
    432      1.1  christos       k = "u ";
    433      1.1  christos       i = 1;
    434      1.1  christos       break;
    435      1.1  christos     case CTF_K_ENUM:
    436      1.1  christos       k = "e ";
    437      1.1  christos       i = 2;
    438      1.1  christos       break;
    439      1.1  christos     default:
    440      1.1  christos       k = "";
    441      1.1  christos       i = 3;
    442      1.1  christos     }
    443      1.1  christos 
    444      1.1  christos   if ((ret = ctf_dynhash_lookup (d->cd_decorated_names[i], name)) == NULL)
    445      1.1  christos     {
    446      1.1  christos       char *str;
    447      1.1  christos 
    448      1.1  christos       if ((str = malloc (strlen (name) + strlen (k) + 1)) == NULL)
    449      1.1  christos 	goto oom;
    450      1.1  christos 
    451      1.1  christos       p = stpcpy (str, k);
    452      1.1  christos       strcpy (p, name);
    453      1.1  christos       ret = intern (fp, str);
    454      1.1  christos       if (!ret)
    455      1.1  christos 	goto oom;
    456      1.1  christos 
    457      1.1  christos       if (ctf_dynhash_cinsert (d->cd_decorated_names[i], name, ret) < 0)
    458      1.1  christos 	goto oom;
    459      1.1  christos     }
    460      1.1  christos 
    461      1.1  christos   return ret;
    462      1.1  christos 
    463      1.1  christos  oom:
    464      1.1  christos   ctf_set_errno (fp, ENOMEM);
    465      1.1  christos   return NULL;
    466      1.1  christos }
    467      1.1  christos 
    468      1.1  christos /* Hash a type, possibly debugging-dumping something about it as well.  */
    469      1.1  christos static inline void
    470      1.1  christos ctf_dedup_sha1_add (ctf_sha1_t *sha1, const void *buf, size_t len,
    471      1.1  christos 		    const char *description _libctf_unused_,
    472      1.1  christos 		    unsigned long depth _libctf_unused_)
    473      1.1  christos {
    474      1.1  christos   ctf_sha1_add (sha1, buf, len);
    475      1.1  christos 
    476      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
    477      1.1  christos   ctf_sha1_t tmp;
    478      1.1  christos   char tmp_hval[CTF_SHA1_SIZE];
    479      1.1  christos   tmp = *sha1;
    480      1.1  christos   ctf_sha1_fini (&tmp, tmp_hval);
    481      1.1  christos   ctf_dprintf ("%lu: after hash addition of %s: %s\n", depth, description,
    482      1.1  christos 	       tmp_hval);
    483      1.1  christos #endif
    484      1.1  christos }
    485      1.1  christos 
    486      1.1  christos static const char *
    487  1.1.1.2  christos ctf_dedup_hash_type (ctf_dict_t *fp, ctf_dict_t *input,
    488  1.1.1.2  christos 		     ctf_dict_t **inputs, uint32_t *parents,
    489      1.1  christos 		     int input_num, ctf_id_t type, int flags,
    490      1.1  christos 		     unsigned long depth,
    491  1.1.1.2  christos 		     int (*populate_fun) (ctf_dict_t *fp,
    492  1.1.1.2  christos 					  ctf_dict_t *input,
    493  1.1.1.2  christos 					  ctf_dict_t **inputs,
    494      1.1  christos 					  int input_num,
    495      1.1  christos 					  ctf_id_t type,
    496      1.1  christos 					  void *id,
    497      1.1  christos 					  const char *decorated_name,
    498      1.1  christos 					  const char *hash));
    499      1.1  christos 
    500      1.1  christos /* Determine whether this type is being hashed as a stub (in which case it is
    501      1.1  christos    unsafe to cache it).  */
    502      1.1  christos static int
    503      1.1  christos ctf_dedup_is_stub (const char *name, int kind, int fwdkind, int flags)
    504      1.1  christos {
    505      1.1  christos   /* We can cache all types unless we are recursing to children and are hashing
    506      1.1  christos      in a tagged struct, union or forward, all of which are replaced with their
    507      1.1  christos      decorated name as a stub and will have different hash values when hashed at
    508      1.1  christos      the top level.  */
    509      1.1  christos 
    510      1.1  christos   return ((flags & CTF_DEDUP_HASH_INTERNAL_CHILD) && name
    511      1.1  christos 	  && (kind == CTF_K_STRUCT || kind == CTF_K_UNION
    512      1.1  christos 	      || (kind == CTF_K_FORWARD && (fwdkind == CTF_K_STRUCT
    513      1.1  christos 					    || fwdkind == CTF_K_UNION))));
    514      1.1  christos }
    515      1.1  christos 
    516      1.1  christos /* Populate struct_origin if need be (not already populated, or populated with
    517      1.1  christos    a different origin), in which case it must go to -1, "shared".)
    518      1.1  christos 
    519      1.1  christos    Only called for forwards or forwardable types with names, when the link mode
    520      1.1  christos    is CTF_LINK_SHARE_DUPLICATED.  */
    521      1.1  christos static int
    522  1.1.1.2  christos ctf_dedup_record_origin (ctf_dict_t *fp, int input_num, const char *decorated,
    523      1.1  christos 			 void *id)
    524      1.1  christos {
    525      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
    526      1.1  christos   void *origin;
    527      1.1  christos   int populate_origin = 0;
    528      1.1  christos 
    529      1.1  christos   if (ctf_dynhash_lookup_kv (d->cd_struct_origin, decorated, NULL, &origin))
    530      1.1  christos     {
    531      1.1  christos       if (CTF_DEDUP_GID_TO_INPUT (origin) != input_num
    532      1.1  christos 	  && CTF_DEDUP_GID_TO_INPUT (origin) != -1)
    533      1.1  christos 	{
    534      1.1  christos 	  populate_origin = 1;
    535      1.1  christos 	  origin = CTF_DEDUP_GID (fp, -1, -1);
    536      1.1  christos 	}
    537      1.1  christos     }
    538      1.1  christos   else
    539      1.1  christos     {
    540      1.1  christos       populate_origin = 1;
    541      1.1  christos       origin = id;
    542      1.1  christos     }
    543      1.1  christos 
    544      1.1  christos   if (populate_origin)
    545      1.1  christos     if (ctf_dynhash_cinsert (d->cd_struct_origin, decorated, origin) < 0)
    546      1.1  christos       return ctf_set_errno (fp, errno);
    547      1.1  christos   return 0;
    548      1.1  christos }
    549      1.1  christos 
    550      1.1  christos /* Do the underlying hashing and recursion for ctf_dedup_hash_type (which it
    551      1.1  christos    calls, recursively).  */
    552      1.1  christos 
    553      1.1  christos static const char *
    554  1.1.1.2  christos ctf_dedup_rhash_type (ctf_dict_t *fp, ctf_dict_t *input, ctf_dict_t **inputs,
    555      1.1  christos 		      uint32_t *parents, int input_num, ctf_id_t type,
    556      1.1  christos 		      void *type_id, const ctf_type_t *tp, const char *name,
    557      1.1  christos 		      const char *decorated, int kind, int flags,
    558      1.1  christos 		      unsigned long depth,
    559  1.1.1.2  christos 		      int (*populate_fun) (ctf_dict_t *fp,
    560  1.1.1.2  christos 					   ctf_dict_t *input,
    561  1.1.1.2  christos 					   ctf_dict_t **inputs,
    562      1.1  christos 					   int input_num,
    563      1.1  christos 					   ctf_id_t type,
    564      1.1  christos 					   void *id,
    565      1.1  christos 					   const char *decorated_name,
    566      1.1  christos 					   const char *hash))
    567      1.1  christos {
    568      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
    569      1.1  christos   ctf_next_t *i = NULL;
    570      1.1  christos   ctf_sha1_t hash;
    571      1.1  christos   ctf_id_t child_type;
    572      1.1  christos   char hashbuf[CTF_SHA1_SIZE];
    573      1.1  christos   const char *hval = NULL;
    574      1.1  christos   const char *whaterr;
    575  1.1.1.2  christos   int err = 0;
    576      1.1  christos 
    577      1.1  christos   const char *citer = NULL;
    578      1.1  christos   ctf_dynset_t *citers = NULL;
    579      1.1  christos 
    580      1.1  christos   /* Add a citer to the citers set.  */
    581      1.1  christos #define ADD_CITER(citers, hval)						\
    582      1.1  christos   do									\
    583      1.1  christos     {									\
    584      1.1  christos       whaterr = N_("error updating citers");				\
    585      1.1  christos       if (!citers)							\
    586      1.1  christos 	if ((citers = ctf_dynset_create (htab_hash_string,		\
    587  1.1.1.2  christos 					 htab_eq_string,		\
    588  1.1.1.2  christos 					 NULL)) == NULL)		\
    589      1.1  christos 	  goto oom;							\
    590      1.1  christos       if (ctf_dynset_cinsert (citers, hval) < 0)			\
    591      1.1  christos 	goto oom;							\
    592  1.1.1.2  christos     }									\
    593  1.1.1.2  christos   while (0)
    594      1.1  christos 
    595      1.1  christos   /* If this is a named struct or union or a forward to one, and this is a child
    596      1.1  christos      traversal, treat this type as if it were a forward -- do not recurse to
    597      1.1  christos      children, ignore all content not already hashed in, and hash in the
    598      1.1  christos      decorated name of the type instead.  */
    599      1.1  christos 
    600      1.1  christos   if (ctf_dedup_is_stub (name, kind, tp->ctt_type, flags))
    601      1.1  christos     {
    602      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
    603      1.1  christos       ctf_dprintf ("Struct/union/forward citation: substituting forwarding "
    604      1.1  christos 		   "stub with decorated name %s\n", decorated);
    605      1.1  christos 
    606      1.1  christos #endif
    607      1.1  christos       ctf_sha1_init (&hash);
    608      1.1  christos       ctf_dedup_sha1_add (&hash, decorated, strlen (decorated) + 1,
    609      1.1  christos 			  "decorated struct/union/forward name", depth);
    610      1.1  christos       ctf_sha1_fini (&hash, hashbuf);
    611      1.1  christos 
    612      1.1  christos       if ((hval = intern (fp, strdup (hashbuf))) == NULL)
    613      1.1  christos 	{
    614      1.1  christos 	  ctf_err_warn (fp, 0, 0, _("%s (%i): out of memory during forwarding-"
    615      1.1  christos 				    "stub hashing for type with GID %p"),
    616      1.1  christos 			ctf_link_input_name (input), input_num, type_id);
    617      1.1  christos 	  return NULL;				/* errno is set for us.  */
    618      1.1  christos 	}
    619      1.1  christos 
    620      1.1  christos       /* In share-duplicated link mode, make sure the origin of this type is
    621      1.1  christos 	 recorded, even if this is a type in a parent dict which will not be
    622      1.1  christos 	 directly traversed.  */
    623      1.1  christos       if (d->cd_link_flags & CTF_LINK_SHARE_DUPLICATED
    624      1.1  christos 	  && ctf_dedup_record_origin (fp, input_num, decorated, type_id) < 0)
    625      1.1  christos 	return NULL;				/* errno is set for us.  */
    626      1.1  christos 
    627      1.1  christos       return hval;
    628      1.1  christos     }
    629      1.1  christos 
    630      1.1  christos   /* Now ensure that subsequent recursive calls (but *not* the top-level call)
    631      1.1  christos      get this treatment.  */
    632      1.1  christos   flags |= CTF_DEDUP_HASH_INTERNAL_CHILD;
    633      1.1  christos 
    634      1.1  christos   /* If this is a struct, union, or forward with a name, record the unique
    635      1.1  christos      originating input TU, if there is one.  */
    636      1.1  christos 
    637      1.1  christos   if (decorated && (ctf_forwardable_kind (kind) || kind != CTF_K_FORWARD))
    638      1.1  christos     if (d->cd_link_flags & CTF_LINK_SHARE_DUPLICATED
    639      1.1  christos 	&& ctf_dedup_record_origin (fp, input_num, decorated, type_id) < 0)
    640      1.1  christos       return NULL;				/* errno is set for us.  */
    641      1.1  christos 
    642  1.1.1.2  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
    643  1.1.1.2  christos   ctf_dprintf ("%lu: hashing thing with ID %i/%lx (kind %i): %s.\n",
    644  1.1.1.2  christos 	       depth, input_num, type, kind, name ? name : "");
    645  1.1.1.2  christos #endif
    646  1.1.1.2  christos 
    647  1.1.1.2  christos   /* Some type kinds don't have names: the API provides no way to set the name,
    648  1.1.1.2  christos      so the type the deduplicator outputs will be nameless even if the input
    649  1.1.1.2  christos      somehow has a name, and the name should not be mixed into the hash.  */
    650  1.1.1.2  christos 
    651  1.1.1.2  christos   switch (kind)
    652  1.1.1.2  christos     {
    653  1.1.1.2  christos     case CTF_K_POINTER:
    654  1.1.1.2  christos     case CTF_K_ARRAY:
    655  1.1.1.2  christos     case CTF_K_FUNCTION:
    656  1.1.1.2  christos     case CTF_K_VOLATILE:
    657  1.1.1.2  christos     case CTF_K_CONST:
    658  1.1.1.2  christos     case CTF_K_RESTRICT:
    659  1.1.1.2  christos     case CTF_K_SLICE:
    660  1.1.1.2  christos       name = NULL;
    661  1.1.1.2  christos     }
    662  1.1.1.2  christos 
    663      1.1  christos   /* Mix in invariant stuff, transforming the type kind if needed.  Note that
    664      1.1  christos      the vlen is *not* hashed in: the actual variable-length info is hashed in
    665      1.1  christos      instead, piecewise.  The vlen is not part of the type, only the
    666      1.1  christos      variable-length data is: identical types with distinct vlens are quite
    667      1.1  christos      possible.  Equally, we do not want to hash in the isroot flag: both the
    668      1.1  christos      compiler and the deduplicator set the nonroot flag to indicate clashes with
    669      1.1  christos      *other types in the same TU* with the same name: so two types can easily
    670      1.1  christos      have distinct nonroot flags, yet be exactly the same type.*/
    671      1.1  christos 
    672      1.1  christos   ctf_sha1_init (&hash);
    673      1.1  christos   if (name)
    674      1.1  christos     ctf_dedup_sha1_add (&hash, name, strlen (name) + 1, "name", depth);
    675      1.1  christos   ctf_dedup_sha1_add (&hash, &kind, sizeof (uint32_t), "kind", depth);
    676      1.1  christos 
    677      1.1  christos   /* Hash content of this type.  */
    678      1.1  christos   switch (kind)
    679      1.1  christos     {
    680      1.1  christos     case CTF_K_UNKNOWN:
    681      1.1  christos       /* No extra state.  */
    682      1.1  christos       break;
    683      1.1  christos     case CTF_K_FORWARD:
    684      1.1  christos 
    685      1.1  christos       /* Add the forwarded kind, stored in the ctt_type.  */
    686      1.1  christos       ctf_dedup_sha1_add (&hash, &tp->ctt_type, sizeof (tp->ctt_type),
    687      1.1  christos 			  "forwarded kind", depth);
    688      1.1  christos       break;
    689      1.1  christos     case CTF_K_INTEGER:
    690      1.1  christos     case CTF_K_FLOAT:
    691      1.1  christos       {
    692      1.1  christos 	ctf_encoding_t ep;
    693      1.1  christos 	memset (&ep, 0, sizeof (ctf_encoding_t));
    694      1.1  christos 
    695      1.1  christos 	ctf_dedup_sha1_add (&hash, &tp->ctt_size, sizeof (uint32_t), "size",
    696      1.1  christos 			    depth);
    697      1.1  christos 	if (ctf_type_encoding (input, type, &ep) < 0)
    698      1.1  christos 	  {
    699      1.1  christos 	    whaterr = N_("error getting encoding");
    700  1.1.1.2  christos 	    goto input_err;
    701      1.1  christos 	  }
    702      1.1  christos 	ctf_dedup_sha1_add (&hash, &ep, sizeof (ctf_encoding_t), "encoding",
    703      1.1  christos 			    depth);
    704      1.1  christos 	break;
    705      1.1  christos       }
    706      1.1  christos       /* Types that reference other types.  */
    707      1.1  christos     case CTF_K_TYPEDEF:
    708      1.1  christos     case CTF_K_VOLATILE:
    709      1.1  christos     case CTF_K_CONST:
    710      1.1  christos     case CTF_K_RESTRICT:
    711      1.1  christos     case CTF_K_POINTER:
    712      1.1  christos       /* Hash the referenced type, if not already hashed, and mix it in.  */
    713      1.1  christos       child_type = ctf_type_reference (input, type);
    714      1.1  christos       if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
    715      1.1  christos 				       child_type, flags, depth,
    716      1.1  christos 				       populate_fun)) == NULL)
    717      1.1  christos 	{
    718      1.1  christos 	  whaterr = N_("error doing referenced type hashing");
    719      1.1  christos 	  goto err;
    720      1.1  christos 	}
    721      1.1  christos       ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "referenced type",
    722      1.1  christos 			  depth);
    723      1.1  christos       citer = hval;
    724      1.1  christos 
    725      1.1  christos       break;
    726      1.1  christos 
    727      1.1  christos       /* The slices of two types hash identically only if the type they overlay
    728      1.1  christos 	 also has the same encoding.  This is not ideal, but in practice will work
    729      1.1  christos 	 well enough.  We work directly rather than using the CTF API because
    730      1.1  christos 	 we do not want the slice's normal automatically-shine-through
    731      1.1  christos 	 semantics to kick in here.  */
    732      1.1  christos     case CTF_K_SLICE:
    733      1.1  christos       {
    734      1.1  christos 	const ctf_slice_t *slice;
    735      1.1  christos 	const ctf_dtdef_t *dtd;
    736      1.1  christos 	ssize_t size;
    737      1.1  christos 	ssize_t increment;
    738      1.1  christos 
    739      1.1  christos 	child_type = ctf_type_reference (input, type);
    740      1.1  christos 	ctf_get_ctt_size (input, tp, &size, &increment);
    741      1.1  christos 	ctf_dedup_sha1_add (&hash, &size, sizeof (ssize_t), "size", depth);
    742      1.1  christos 
    743      1.1  christos 	if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
    744      1.1  christos 					 child_type, flags, depth,
    745      1.1  christos 					 populate_fun)) == NULL)
    746      1.1  christos 	  {
    747      1.1  christos 	    whaterr = N_("error doing slice-referenced type hashing");
    748      1.1  christos 	    goto err;
    749      1.1  christos 	  }
    750      1.1  christos 	ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "sliced type",
    751      1.1  christos 			    depth);
    752      1.1  christos 	citer = hval;
    753      1.1  christos 
    754      1.1  christos 	if ((dtd = ctf_dynamic_type (input, type)) != NULL)
    755  1.1.1.2  christos 	  slice = (ctf_slice_t *) dtd->dtd_vlen;
    756      1.1  christos 	else
    757      1.1  christos 	  slice = (ctf_slice_t *) ((uintptr_t) tp + increment);
    758      1.1  christos 
    759      1.1  christos 	ctf_dedup_sha1_add (&hash, &slice->cts_offset,
    760      1.1  christos 			    sizeof (slice->cts_offset), "slice offset", depth);
    761      1.1  christos 	ctf_dedup_sha1_add (&hash, &slice->cts_bits,
    762      1.1  christos 			    sizeof (slice->cts_bits), "slice bits", depth);
    763      1.1  christos 	break;
    764      1.1  christos       }
    765      1.1  christos 
    766      1.1  christos     case CTF_K_ARRAY:
    767      1.1  christos       {
    768      1.1  christos 	ctf_arinfo_t ar;
    769      1.1  christos 
    770      1.1  christos 	if (ctf_array_info (input, type, &ar) < 0)
    771      1.1  christos 	  {
    772      1.1  christos 	    whaterr = N_("error getting array info");
    773  1.1.1.2  christos 	    goto input_err;
    774      1.1  christos 	  }
    775      1.1  christos 
    776      1.1  christos 	if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
    777      1.1  christos 					 ar.ctr_contents, flags, depth,
    778      1.1  christos 					 populate_fun)) == NULL)
    779      1.1  christos 	  {
    780      1.1  christos 	    whaterr = N_("error doing array contents type hashing");
    781      1.1  christos 	    goto err;
    782      1.1  christos 	  }
    783      1.1  christos 	ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "array contents",
    784      1.1  christos 			    depth);
    785      1.1  christos 	ADD_CITER (citers, hval);
    786      1.1  christos 
    787      1.1  christos 	if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
    788      1.1  christos 					 ar.ctr_index, flags, depth,
    789      1.1  christos 					 populate_fun)) == NULL)
    790      1.1  christos 	  {
    791      1.1  christos 	    whaterr = N_("error doing array index type hashing");
    792      1.1  christos 	    goto err;
    793      1.1  christos 	  }
    794      1.1  christos 	ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "array index",
    795      1.1  christos 			    depth);
    796      1.1  christos 	ctf_dedup_sha1_add (&hash, &ar.ctr_nelems, sizeof (ar.ctr_nelems),
    797      1.1  christos 			    "element count", depth);
    798      1.1  christos 	ADD_CITER (citers, hval);
    799      1.1  christos 
    800      1.1  christos 	break;
    801      1.1  christos       }
    802      1.1  christos     case CTF_K_FUNCTION:
    803      1.1  christos       {
    804      1.1  christos 	ctf_funcinfo_t fi;
    805      1.1  christos 	ctf_id_t *args;
    806      1.1  christos 	uint32_t j;
    807      1.1  christos 
    808      1.1  christos 	if (ctf_func_type_info (input, type, &fi) < 0)
    809      1.1  christos 	  {
    810      1.1  christos 	    whaterr = N_("error getting func type info");
    811  1.1.1.2  christos 	    goto input_err;
    812      1.1  christos 	  }
    813      1.1  christos 
    814      1.1  christos 	if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents, input_num,
    815      1.1  christos 					 fi.ctc_return, flags, depth,
    816      1.1  christos 					 populate_fun)) == NULL)
    817      1.1  christos 	  {
    818      1.1  christos 	    whaterr = N_("error getting func return type");
    819      1.1  christos 	    goto err;
    820      1.1  christos 	  }
    821      1.1  christos 	ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "func return",
    822      1.1  christos 			    depth);
    823      1.1  christos 	ctf_dedup_sha1_add (&hash, &fi.ctc_argc, sizeof (fi.ctc_argc),
    824      1.1  christos 			    "func argc", depth);
    825      1.1  christos 	ctf_dedup_sha1_add (&hash, &fi.ctc_flags, sizeof (fi.ctc_flags),
    826      1.1  christos 			    "func flags", depth);
    827      1.1  christos 	ADD_CITER (citers, hval);
    828      1.1  christos 
    829      1.1  christos 	if ((args = calloc (fi.ctc_argc, sizeof (ctf_id_t))) == NULL)
    830      1.1  christos 	  {
    831  1.1.1.2  christos 	    err = ENOMEM;
    832      1.1  christos 	    whaterr = N_("error doing memory allocation");
    833      1.1  christos 	    goto err;
    834      1.1  christos 	  }
    835      1.1  christos 
    836      1.1  christos 	if (ctf_func_type_args (input, type, fi.ctc_argc, args) < 0)
    837      1.1  christos 	  {
    838      1.1  christos 	    free (args);
    839      1.1  christos 	    whaterr = N_("error getting func arg type");
    840  1.1.1.2  christos 	    goto input_err;
    841      1.1  christos 	  }
    842      1.1  christos 	for (j = 0; j < fi.ctc_argc; j++)
    843      1.1  christos 	  {
    844      1.1  christos 	    if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents,
    845      1.1  christos 					     input_num, args[j], flags, depth,
    846      1.1  christos 					     populate_fun)) == NULL)
    847      1.1  christos 	      {
    848      1.1  christos 		free (args);
    849      1.1  christos 		whaterr = N_("error doing func arg type hashing");
    850      1.1  christos 		goto err;
    851      1.1  christos 	      }
    852      1.1  christos 	    ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "func arg type",
    853      1.1  christos 				depth);
    854      1.1  christos 	    ADD_CITER (citers, hval);
    855      1.1  christos 	  }
    856      1.1  christos 	free (args);
    857      1.1  christos 	break;
    858      1.1  christos       }
    859      1.1  christos     case CTF_K_ENUM:
    860      1.1  christos       {
    861      1.1  christos 	int val;
    862      1.1  christos 	const char *ename;
    863      1.1  christos 
    864      1.1  christos 	ctf_dedup_sha1_add (&hash, &tp->ctt_size, sizeof (uint32_t),
    865      1.1  christos 			    "enum size", depth);
    866      1.1  christos 	while ((ename = ctf_enum_next (input, type, &i, &val)) != NULL)
    867      1.1  christos 	  {
    868      1.1  christos 	    ctf_dedup_sha1_add (&hash, ename, strlen (ename) + 1, "enumerator",
    869      1.1  christos 				depth);
    870      1.1  christos 	    ctf_dedup_sha1_add (&hash, &val, sizeof (val), "enumerand", depth);
    871      1.1  christos 	  }
    872      1.1  christos 	if (ctf_errno (input) != ECTF_NEXT_END)
    873      1.1  christos 	  {
    874      1.1  christos 	    whaterr = N_("error doing enum member iteration");
    875  1.1.1.2  christos 	    goto input_err;
    876      1.1  christos 	  }
    877      1.1  christos 	break;
    878      1.1  christos       }
    879      1.1  christos     /* Top-level only.  */
    880      1.1  christos     case CTF_K_STRUCT:
    881      1.1  christos     case CTF_K_UNION:
    882      1.1  christos       {
    883      1.1  christos 	ssize_t offset;
    884      1.1  christos 	const char *mname;
    885      1.1  christos 	ctf_id_t membtype;
    886      1.1  christos 	ssize_t size;
    887      1.1  christos 
    888      1.1  christos 	ctf_get_ctt_size (input, tp, &size, NULL);
    889      1.1  christos 	ctf_dedup_sha1_add (&hash, &size, sizeof (ssize_t), "struct size",
    890      1.1  christos 			    depth);
    891      1.1  christos 
    892  1.1.1.2  christos 	while ((offset = ctf_member_next (input, type, &i, &mname, &membtype,
    893  1.1.1.2  christos 					  0)) >= 0)
    894      1.1  christos 	  {
    895      1.1  christos 	    if (mname == NULL)
    896      1.1  christos 	      mname = "";
    897      1.1  christos 	    ctf_dedup_sha1_add (&hash, mname, strlen (mname) + 1,
    898      1.1  christos 				"member name", depth);
    899      1.1  christos 
    900      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
    901      1.1  christos 	    ctf_dprintf ("%lu: Traversing to member %s\n", depth, mname);
    902      1.1  christos #endif
    903      1.1  christos 	    if ((hval = ctf_dedup_hash_type (fp, input, inputs, parents,
    904      1.1  christos 					     input_num, membtype, flags, depth,
    905      1.1  christos 					     populate_fun)) == NULL)
    906      1.1  christos 	      {
    907      1.1  christos 		whaterr = N_("error doing struct/union member type hashing");
    908      1.1  christos 		goto iterr;
    909      1.1  christos 	      }
    910      1.1  christos 
    911      1.1  christos 	    ctf_dedup_sha1_add (&hash, hval, strlen (hval) + 1, "member hash",
    912      1.1  christos 				depth);
    913      1.1  christos 	    ctf_dedup_sha1_add (&hash, &offset, sizeof (offset), "member offset",
    914      1.1  christos 				depth);
    915      1.1  christos 	    ADD_CITER (citers, hval);
    916      1.1  christos 	  }
    917      1.1  christos 	if (ctf_errno (input) != ECTF_NEXT_END)
    918      1.1  christos 	  {
    919      1.1  christos 	    whaterr = N_("error doing struct/union member iteration");
    920  1.1.1.2  christos 	    goto input_err;
    921      1.1  christos 	  }
    922      1.1  christos 	break;
    923      1.1  christos       }
    924      1.1  christos     default:
    925      1.1  christos       whaterr = N_("error: unknown type kind");
    926      1.1  christos       goto err;
    927      1.1  christos     }
    928      1.1  christos   ctf_sha1_fini (&hash, hashbuf);
    929      1.1  christos 
    930      1.1  christos   if ((hval = intern (fp, strdup (hashbuf))) == NULL)
    931      1.1  christos     {
    932      1.1  christos       whaterr = N_("cannot intern hash");
    933      1.1  christos       goto oom;
    934      1.1  christos     }
    935      1.1  christos 
    936      1.1  christos   /* Populate the citers for this type's subtypes, now the hash for the type
    937      1.1  christos      itself is known.  */
    938      1.1  christos   whaterr = N_("error tracking citers");
    939      1.1  christos 
    940      1.1  christos   if (citer)
    941      1.1  christos     {
    942      1.1  christos       ctf_dynset_t *citer_hashes;
    943      1.1  christos 
    944      1.1  christos       if ((citer_hashes = make_set_element (d->cd_citers, citer)) == NULL)
    945      1.1  christos 	goto oom;
    946      1.1  christos       if (ctf_dynset_cinsert (citer_hashes, hval) < 0)
    947      1.1  christos 	goto oom;
    948      1.1  christos     }
    949      1.1  christos   else if (citers)
    950      1.1  christos     {
    951      1.1  christos       const void *k;
    952      1.1  christos 
    953      1.1  christos       while ((err = ctf_dynset_cnext (citers, &i, &k)) == 0)
    954      1.1  christos 	{
    955      1.1  christos 	  ctf_dynset_t *citer_hashes;
    956      1.1  christos 	  citer = (const char *) k;
    957      1.1  christos 
    958      1.1  christos 	  if ((citer_hashes = make_set_element (d->cd_citers, citer)) == NULL)
    959      1.1  christos 	    goto oom;
    960      1.1  christos 
    961      1.1  christos 	  if (ctf_dynset_exists (citer_hashes, hval, NULL))
    962      1.1  christos 	    continue;
    963      1.1  christos 	  if (ctf_dynset_cinsert (citer_hashes, hval) < 0)
    964      1.1  christos 	    goto oom;
    965      1.1  christos 	}
    966      1.1  christos       if (err != ECTF_NEXT_END)
    967      1.1  christos 	goto err;
    968      1.1  christos       ctf_dynset_destroy (citers);
    969      1.1  christos     }
    970      1.1  christos 
    971      1.1  christos   return hval;
    972      1.1  christos 
    973      1.1  christos  iterr:
    974      1.1  christos   ctf_next_destroy (i);
    975  1.1.1.2  christos  input_err:
    976  1.1.1.2  christos   err = ctf_errno (input);
    977      1.1  christos  err:
    978      1.1  christos   ctf_sha1_fini (&hash, NULL);
    979  1.1.1.2  christos   ctf_err_warn (fp, 0, err, _("%s (%i): %s: during type hashing for type %lx, "
    980  1.1.1.2  christos 			      "kind %i"), ctf_link_input_name (input),
    981      1.1  christos 		input_num, gettext (whaterr), type, kind);
    982      1.1  christos   return NULL;
    983      1.1  christos  oom:
    984      1.1  christos   ctf_set_errno (fp, errno);
    985      1.1  christos   ctf_err_warn (fp, 0, 0, _("%s (%i): %s: during type hashing for type %lx, "
    986      1.1  christos 			    "kind %i"), ctf_link_input_name (input),
    987      1.1  christos 		input_num, gettext (whaterr), type, kind);
    988      1.1  christos   return NULL;
    989      1.1  christos }
    990      1.1  christos 
    991      1.1  christos /* Hash a TYPE in the INPUT: FP is the eventual output, where the ctf_dedup
    992      1.1  christos    state is stored.  INPUT_NUM is the number of this input in the set of inputs.
    993      1.1  christos    Record its hash in FP's cd_type_hashes once it is known.  PARENTS is
    994      1.1  christos    described in the comment above ctf_dedup.
    995      1.1  christos 
    996      1.1  christos    (The flags argument currently accepts only the flag
    997      1.1  christos    CTF_DEDUP_HASH_INTERNAL_CHILD, an implementation detail used to prevent
    998      1.1  christos    struct/union hashing in recursive traversals below the TYPE.)
    999      1.1  christos 
   1000      1.1  christos    We use the CTF API rather than direct access wherever possible, because types
   1001      1.1  christos    that appear identical through the API should be considered identical, with
   1002      1.1  christos    one exception: slices should only be considered identical to other slices,
   1003      1.1  christos    not to the corresponding unsliced type.
   1004      1.1  christos 
   1005      1.1  christos    The POPULATE_FUN is a mandatory hook that populates other mappings with each
   1006      1.1  christos    type we see (excepting types that are recursively hashed as stubs).  The
   1007      1.1  christos    caller should not rely on the order of calls to this hook, though it will be
   1008      1.1  christos    called at least once for every non-stub reference to every type.
   1009      1.1  christos 
   1010      1.1  christos    Returns a hash value (an atom), or NULL on error.  */
   1011      1.1  christos 
   1012      1.1  christos static const char *
   1013  1.1.1.2  christos ctf_dedup_hash_type (ctf_dict_t *fp, ctf_dict_t *input,
   1014  1.1.1.2  christos 		     ctf_dict_t **inputs, uint32_t *parents,
   1015      1.1  christos 		     int input_num, ctf_id_t type, int flags,
   1016      1.1  christos 		     unsigned long depth,
   1017  1.1.1.2  christos 		     int (*populate_fun) (ctf_dict_t *fp,
   1018  1.1.1.2  christos 					  ctf_dict_t *input,
   1019  1.1.1.2  christos 					  ctf_dict_t **inputs,
   1020      1.1  christos 					  int input_num,
   1021      1.1  christos 					  ctf_id_t type,
   1022      1.1  christos 					  void *id,
   1023      1.1  christos 					  const char *decorated_name,
   1024      1.1  christos 					  const char *hash))
   1025      1.1  christos {
   1026      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
   1027      1.1  christos   const ctf_type_t *tp;
   1028      1.1  christos   void *type_id;
   1029      1.1  christos   const char *hval = NULL;
   1030      1.1  christos   const char *name;
   1031      1.1  christos   const char *whaterr;
   1032      1.1  christos   const char *decorated = NULL;
   1033      1.1  christos   uint32_t kind, fwdkind;
   1034      1.1  christos 
   1035      1.1  christos   depth++;
   1036      1.1  christos 
   1037      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   1038      1.1  christos   ctf_dprintf ("%lu: ctf_dedup_hash_type (%i, %lx, flags %x)\n", depth, input_num, type, flags);
   1039      1.1  christos #endif
   1040      1.1  christos 
   1041      1.1  christos   /* The unimplemented type doesn't really exist, but must be noted in parent
   1042      1.1  christos      hashes: so it gets a fixed, arbitrary hash.  */
   1043      1.1  christos   if (type == 0)
   1044      1.1  christos     return "00000000000000000000";
   1045      1.1  christos 
   1046      1.1  christos   /* Possible optimization: if the input type is in the parent type space, just
   1047      1.1  christos      copy recursively-cited hashes from the parent's types into the output
   1048      1.1  christos      mapping rather than rehashing them.  */
   1049      1.1  christos 
   1050      1.1  christos   type_id = CTF_DEDUP_GID (fp, input_num, type);
   1051      1.1  christos 
   1052      1.1  christos   if ((tp = ctf_lookup_by_id (&input, type)) == NULL)
   1053      1.1  christos     {
   1054      1.1  christos       ctf_set_errno (fp, ctf_errno (input));
   1055      1.1  christos       ctf_err_warn (fp, 0, 0, _("%s (%i): lookup failure for type %lx: "
   1056      1.1  christos 				"flags %x"), ctf_link_input_name (input),
   1057      1.1  christos 		    input_num, type, flags);
   1058      1.1  christos       return NULL;		/* errno is set for us.  */
   1059      1.1  christos     }
   1060      1.1  christos 
   1061      1.1  christos   kind = LCTF_INFO_KIND (input, tp->ctt_info);
   1062      1.1  christos   name = ctf_strraw (input, tp->ctt_name);
   1063      1.1  christos 
   1064      1.1  christos   if (tp->ctt_name == 0 || !name || name[0] == '\0')
   1065      1.1  christos     name = NULL;
   1066      1.1  christos 
   1067      1.1  christos   /* Decorate the name appropriately for the namespace it appears in: forwards
   1068      1.1  christos      appear in the namespace of their referent.  */
   1069      1.1  christos 
   1070      1.1  christos   fwdkind = kind;
   1071      1.1  christos   if (name)
   1072      1.1  christos     {
   1073      1.1  christos       if (kind == CTF_K_FORWARD)
   1074      1.1  christos 	fwdkind = tp->ctt_type;
   1075      1.1  christos 
   1076      1.1  christos       if ((decorated = ctf_decorate_type_name (fp, name, fwdkind)) == NULL)
   1077      1.1  christos 	return NULL;				/* errno is set for us.  */
   1078      1.1  christos     }
   1079      1.1  christos 
   1080      1.1  christos   /* If not hashing a stub, we can rely on various sorts of caches.
   1081      1.1  christos 
   1082      1.1  christos      Optimization opportunity: we may be able to avoid calling the populate_fun
   1083      1.1  christos      sometimes here.  */
   1084      1.1  christos 
   1085      1.1  christos   if (!ctf_dedup_is_stub (name, kind, fwdkind, flags))
   1086      1.1  christos     {
   1087      1.1  christos       if ((hval = ctf_dynhash_lookup (d->cd_type_hashes, type_id)) != NULL)
   1088      1.1  christos 	{
   1089      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   1090      1.1  christos 	  ctf_dprintf ("%lu: Known hash for ID %i/%lx: %s\n", depth, input_num,
   1091      1.1  christos 		       type,  hval);
   1092      1.1  christos #endif
   1093      1.1  christos 	  populate_fun (fp, input, inputs, input_num, type, type_id,
   1094      1.1  christos 			decorated, hval);
   1095      1.1  christos 
   1096      1.1  christos 	  return hval;
   1097      1.1  christos 	}
   1098      1.1  christos     }
   1099      1.1  christos 
   1100      1.1  christos   /* We have never seen this type before, and must figure out its hash and the
   1101      1.1  christos      hashes of the types it cites.
   1102      1.1  christos 
   1103      1.1  christos      Hash this type, and call ourselves recursively.  (The hashing part is
   1104      1.1  christos      optional, and is disabled if overidden_hval is set.)  */
   1105      1.1  christos 
   1106      1.1  christos   if ((hval = ctf_dedup_rhash_type (fp, input, inputs, parents, input_num,
   1107      1.1  christos 				    type, type_id, tp, name, decorated,
   1108      1.1  christos 				    kind, flags, depth, populate_fun)) == NULL)
   1109      1.1  christos     return NULL;				/* errno is set for us.  */
   1110      1.1  christos 
   1111      1.1  christos   /* The hash of this type is now known: record it unless caching is unsafe
   1112      1.1  christos      because the hash value will change later.  This will be the final storage
   1113      1.1  christos      of this type's hash, so we call the population function on it.  */
   1114      1.1  christos 
   1115      1.1  christos   if (!ctf_dedup_is_stub (name, kind, fwdkind, flags))
   1116      1.1  christos     {
   1117      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   1118      1.1  christos       ctf_dprintf ("Caching %lx, ID %p (%s), %s in final location\n", type,
   1119      1.1  christos 		   type_id, name ? name : "", hval);
   1120      1.1  christos #endif
   1121      1.1  christos 
   1122      1.1  christos       if (ctf_dynhash_cinsert (d->cd_type_hashes, type_id, hval) < 0)
   1123      1.1  christos 	{
   1124      1.1  christos 	  whaterr = N_("error hash caching");
   1125      1.1  christos 	  goto oom;
   1126      1.1  christos 	}
   1127      1.1  christos 
   1128      1.1  christos       if (populate_fun (fp, input, inputs, input_num, type, type_id,
   1129      1.1  christos 			decorated, hval) < 0)
   1130      1.1  christos 	{
   1131      1.1  christos 	  whaterr = N_("error calling population function");
   1132      1.1  christos 	  goto err;				/* errno is set for us. */
   1133      1.1  christos 	}
   1134      1.1  christos     }
   1135      1.1  christos 
   1136      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   1137      1.1  christos   ctf_dprintf ("%lu: Returning final hash for ID %i/%lx: %s\n", depth,
   1138      1.1  christos 	       input_num, type, hval);
   1139      1.1  christos #endif
   1140      1.1  christos   return hval;
   1141      1.1  christos 
   1142      1.1  christos  oom:
   1143      1.1  christos   ctf_set_errno (fp, errno);
   1144      1.1  christos  err:
   1145      1.1  christos   ctf_err_warn (fp, 0, 0, _("%s (%i): %s: during type hashing, "
   1146      1.1  christos 			    "type %lx, kind %i"),
   1147      1.1  christos 		ctf_link_input_name (input), input_num,
   1148      1.1  christos 		gettext (whaterr), type, kind);
   1149      1.1  christos   return NULL;
   1150      1.1  christos }
   1151      1.1  christos 
   1152      1.1  christos /* Populate a number of useful mappings not directly used by the hashing
   1153      1.1  christos    machinery: the output mapping, the cd_name_counts mapping from name -> hash
   1154      1.1  christos    -> count of hashval deduplication state for a given hashed type, and the
   1155      1.1  christos    cd_output_first_tu mapping.  */
   1156      1.1  christos 
   1157      1.1  christos static int
   1158  1.1.1.2  christos ctf_dedup_populate_mappings (ctf_dict_t *fp, ctf_dict_t *input _libctf_unused_,
   1159  1.1.1.2  christos 			     ctf_dict_t **inputs _libctf_unused_,
   1160      1.1  christos 			     int input_num _libctf_unused_,
   1161      1.1  christos 			     ctf_id_t type _libctf_unused_, void *id,
   1162      1.1  christos 			     const char *decorated_name,
   1163      1.1  christos 			     const char *hval)
   1164      1.1  christos {
   1165      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
   1166      1.1  christos   ctf_dynset_t *type_ids;
   1167      1.1  christos   ctf_dynhash_t *name_counts;
   1168      1.1  christos   long int count;
   1169      1.1  christos 
   1170      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   1171      1.1  christos   ctf_dprintf ("Hash %s, %s, into output mapping for %i/%lx @ %s\n",
   1172      1.1  christos 	       hval, decorated_name ? decorated_name : "(unnamed)",
   1173      1.1  christos 	       input_num, type, ctf_link_input_name (input));
   1174      1.1  christos 
   1175      1.1  christos   const char *orig_hval;
   1176      1.1  christos 
   1177      1.1  christos   /* Make sure we never map a single GID to multiple hash values.  */
   1178      1.1  christos 
   1179      1.1  christos   if ((orig_hval = ctf_dynhash_lookup (d->cd_output_mapping_guard, id)) != NULL)
   1180      1.1  christos     {
   1181      1.1  christos       /* We can rely on pointer identity here, since all hashes are
   1182      1.1  christos 	 interned.  */
   1183      1.1  christos       if (!ctf_assert (fp, orig_hval == hval))
   1184      1.1  christos 	return -1;
   1185      1.1  christos     }
   1186      1.1  christos   else
   1187      1.1  christos     if (ctf_dynhash_cinsert (d->cd_output_mapping_guard, id, hval) < 0)
   1188      1.1  christos       return ctf_set_errno (fp, errno);
   1189      1.1  christos #endif
   1190      1.1  christos 
   1191      1.1  christos   /* Record the type in the output mapping: if this is the first time this type
   1192      1.1  christos      has been seen, also record it in the cd_output_first_gid.  Because we
   1193      1.1  christos      traverse types in TU order and we do not merge types after the hashing
   1194      1.1  christos      phase, this will be the lowest TU this type ever appears in.  */
   1195      1.1  christos 
   1196      1.1  christos   if ((type_ids = ctf_dynhash_lookup (d->cd_output_mapping,
   1197      1.1  christos 				      hval)) == NULL)
   1198      1.1  christos     {
   1199      1.1  christos       if (ctf_dynhash_cinsert (d->cd_output_first_gid, hval, id) < 0)
   1200      1.1  christos 	return ctf_set_errno (fp, errno);
   1201      1.1  christos 
   1202      1.1  christos       if ((type_ids = ctf_dynset_create (htab_hash_pointer,
   1203      1.1  christos 					 htab_eq_pointer,
   1204      1.1  christos 					 NULL)) == NULL)
   1205      1.1  christos 	return ctf_set_errno (fp, errno);
   1206      1.1  christos       if (ctf_dynhash_insert (d->cd_output_mapping, (void *) hval,
   1207      1.1  christos 			      type_ids) < 0)
   1208      1.1  christos 	{
   1209      1.1  christos 	  ctf_dynset_destroy (type_ids);
   1210      1.1  christos 	  return ctf_set_errno (fp, errno);
   1211      1.1  christos 	}
   1212      1.1  christos     }
   1213      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   1214      1.1  christos     {
   1215      1.1  christos       /* Verify that all types with this hash are of the same kind, and that the
   1216      1.1  christos 	 first TU a type was seen in never falls.  */
   1217      1.1  christos 
   1218      1.1  christos       int err;
   1219      1.1  christos       const void *one_id;
   1220      1.1  christos       ctf_next_t *i = NULL;
   1221      1.1  christos       int orig_kind = ctf_type_kind_unsliced (input, type);
   1222      1.1  christos       int orig_first_tu;
   1223      1.1  christos 
   1224      1.1  christos       orig_first_tu = CTF_DEDUP_GID_TO_INPUT
   1225      1.1  christos 	(ctf_dynhash_lookup (d->cd_output_first_gid, hval));
   1226      1.1  christos       if (!ctf_assert (fp, orig_first_tu <= CTF_DEDUP_GID_TO_INPUT (id)))
   1227      1.1  christos 	return -1;
   1228      1.1  christos 
   1229      1.1  christos       while ((err = ctf_dynset_cnext (type_ids, &i, &one_id)) == 0)
   1230      1.1  christos 	{
   1231  1.1.1.2  christos 	  ctf_dict_t *foo = inputs[CTF_DEDUP_GID_TO_INPUT (one_id)];
   1232      1.1  christos 	  ctf_id_t bar = CTF_DEDUP_GID_TO_TYPE (one_id);
   1233      1.1  christos 	  if (ctf_type_kind_unsliced (foo, bar) != orig_kind)
   1234      1.1  christos 	    {
   1235      1.1  christos 	      ctf_err_warn (fp, 1, 0, "added wrong kind to output mapping "
   1236      1.1  christos 			    "for hash %s named %s: %p/%lx from %s is "
   1237      1.1  christos 			    "kind %i, but newly-added %p/%lx from %s is "
   1238      1.1  christos 			    "kind %i", hval,
   1239      1.1  christos 			    decorated_name ? decorated_name : "(unnamed)",
   1240      1.1  christos 			    (void *) foo, bar,
   1241      1.1  christos 			    ctf_link_input_name (foo),
   1242      1.1  christos 			    ctf_type_kind_unsliced (foo, bar),
   1243      1.1  christos 			    (void *) input, type,
   1244      1.1  christos 			    ctf_link_input_name (input), orig_kind);
   1245      1.1  christos 	      if (!ctf_assert (fp, ctf_type_kind_unsliced (foo, bar)
   1246      1.1  christos 			       == orig_kind))
   1247      1.1  christos 		return -1;
   1248      1.1  christos 	    }
   1249      1.1  christos 	}
   1250      1.1  christos       if (err != ECTF_NEXT_END)
   1251      1.1  christos 	return ctf_set_errno (fp, err);
   1252      1.1  christos     }
   1253      1.1  christos #endif
   1254      1.1  christos 
   1255      1.1  christos   /* This function will be repeatedly called for the same types many times:
   1256      1.1  christos      don't waste time reinserting the same keys in that case.  */
   1257      1.1  christos   if (!ctf_dynset_exists (type_ids, id, NULL)
   1258      1.1  christos       && ctf_dynset_insert (type_ids, id) < 0)
   1259      1.1  christos     return ctf_set_errno (fp, errno);
   1260      1.1  christos 
   1261      1.1  christos   /* The rest only needs to happen for types with names.  */
   1262      1.1  christos   if (!decorated_name)
   1263      1.1  christos     return 0;
   1264      1.1  christos 
   1265      1.1  christos   /* Count the number of occurrences of the hash value for this GID.  */
   1266      1.1  christos 
   1267      1.1  christos   hval = ctf_dynhash_lookup (d->cd_type_hashes, id);
   1268      1.1  christos 
   1269      1.1  christos   /* Mapping from name -> hash(hashval, count) not already present?  */
   1270      1.1  christos   if ((name_counts = ctf_dynhash_lookup (d->cd_name_counts,
   1271      1.1  christos 					 decorated_name)) == NULL)
   1272      1.1  christos     {
   1273      1.1  christos       if ((name_counts = ctf_dynhash_create (ctf_hash_string,
   1274      1.1  christos 					     ctf_hash_eq_string,
   1275      1.1  christos 					     NULL, NULL)) == NULL)
   1276      1.1  christos 	  return ctf_set_errno (fp, errno);
   1277      1.1  christos       if (ctf_dynhash_cinsert (d->cd_name_counts, decorated_name,
   1278      1.1  christos 			       name_counts) < 0)
   1279      1.1  christos 	{
   1280      1.1  christos 	  ctf_dynhash_destroy (name_counts);
   1281      1.1  christos 	  return ctf_set_errno (fp, errno);
   1282      1.1  christos 	}
   1283      1.1  christos     }
   1284      1.1  christos 
   1285      1.1  christos   /* This will, conveniently, return NULL (i.e. 0) for a new entry.  */
   1286      1.1  christos   count = (long int) (uintptr_t) ctf_dynhash_lookup (name_counts, hval);
   1287      1.1  christos 
   1288      1.1  christos   if (ctf_dynhash_cinsert (name_counts, hval,
   1289      1.1  christos 			   (const void *) (uintptr_t) (count + 1)) < 0)
   1290      1.1  christos     return ctf_set_errno (fp, errno);
   1291      1.1  christos 
   1292      1.1  christos   return 0;
   1293      1.1  christos }
   1294      1.1  christos 
   1295      1.1  christos /* Mark a single hash as corresponding to a conflicting type.  Mark all types
   1296      1.1  christos    that cite it as conflicting as well, terminating the recursive walk only when
   1297      1.1  christos    types that are already conflicted or types do not cite other types are seen.
   1298      1.1  christos    (Tagged structures and unions do not appear in the cd_citers graph, so the
   1299      1.1  christos    walk also terminates there, since any reference to a conflicting structure is
   1300      1.1  christos    just going to reference an unconflicting forward instead: see
   1301      1.1  christos    ctf_dedup_maybe_synthesize_forward.)  */
   1302      1.1  christos 
   1303      1.1  christos static int
   1304  1.1.1.2  christos ctf_dedup_mark_conflicting_hash (ctf_dict_t *fp, const char *hval)
   1305      1.1  christos {
   1306      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
   1307      1.1  christos   ctf_next_t *i = NULL;
   1308      1.1  christos   int err;
   1309      1.1  christos   const void *k;
   1310      1.1  christos   ctf_dynset_t *citers;
   1311      1.1  christos 
   1312      1.1  christos   /* Mark conflicted if not already so marked.  */
   1313      1.1  christos   if (ctf_dynset_exists (d->cd_conflicting_types, hval, NULL))
   1314      1.1  christos     return 0;
   1315      1.1  christos 
   1316      1.1  christos   ctf_dprintf ("Marking %s as conflicted\n", hval);
   1317      1.1  christos 
   1318      1.1  christos   if (ctf_dynset_cinsert (d->cd_conflicting_types, hval) < 0)
   1319      1.1  christos     {
   1320      1.1  christos       ctf_dprintf ("Out of memory marking %s as conflicted\n", hval);
   1321  1.1.1.3  christos       return ctf_set_errno (fp, errno);
   1322      1.1  christos     }
   1323      1.1  christos 
   1324      1.1  christos   /* If any types cite this type, mark them conflicted too.  */
   1325      1.1  christos   if ((citers = ctf_dynhash_lookup (d->cd_citers, hval)) == NULL)
   1326      1.1  christos     return 0;
   1327      1.1  christos 
   1328      1.1  christos   while ((err = ctf_dynset_cnext (citers, &i, &k)) == 0)
   1329      1.1  christos     {
   1330      1.1  christos       const char *hv = (const char *) k;
   1331      1.1  christos 
   1332      1.1  christos       if (ctf_dynset_exists (d->cd_conflicting_types, hv, NULL))
   1333      1.1  christos 	continue;
   1334      1.1  christos 
   1335      1.1  christos       if (ctf_dedup_mark_conflicting_hash (fp, hv) < 0)
   1336      1.1  christos 	{
   1337      1.1  christos 	  ctf_next_destroy (i);
   1338      1.1  christos 	  return -1;				/* errno is set for us.  */
   1339      1.1  christos 	}
   1340      1.1  christos     }
   1341      1.1  christos   if (err != ECTF_NEXT_END)
   1342      1.1  christos     return ctf_set_errno (fp, err);
   1343      1.1  christos 
   1344      1.1  christos   return 0;
   1345      1.1  christos }
   1346      1.1  christos 
   1347      1.1  christos /* Look up a type kind from the output mapping, given a type hash value.  */
   1348      1.1  christos static int
   1349  1.1.1.2  christos ctf_dedup_hash_kind (ctf_dict_t *fp, ctf_dict_t **inputs, const char *hash)
   1350      1.1  christos {
   1351      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
   1352      1.1  christos   void *id;
   1353      1.1  christos   ctf_dynset_t *type_ids;
   1354      1.1  christos 
   1355      1.1  christos   /* Precondition: the output mapping is populated.  */
   1356      1.1  christos   if (!ctf_assert (fp, ctf_dynhash_elements (d->cd_output_mapping) > 0))
   1357      1.1  christos     return -1;
   1358      1.1  christos 
   1359      1.1  christos   /* Look up some GID from the output hash for this type.  (They are all
   1360      1.1  christos      identical, so we can pick any).  Don't assert if someone calls this
   1361      1.1  christos      function wrongly, but do assert if the output mapping knows about the hash,
   1362      1.1  christos      but has nothing associated with it.  */
   1363      1.1  christos 
   1364      1.1  christos   type_ids = ctf_dynhash_lookup (d->cd_output_mapping, hash);
   1365      1.1  christos   if (!type_ids)
   1366      1.1  christos     {
   1367      1.1  christos       ctf_dprintf ("Looked up type kind by nonexistent hash %s.\n", hash);
   1368      1.1  christos       return ctf_set_errno (fp, ECTF_INTERNAL);
   1369      1.1  christos     }
   1370      1.1  christos   id = ctf_dynset_lookup_any (type_ids);
   1371      1.1  christos   if (!ctf_assert (fp, id))
   1372      1.1  christos     return -1;
   1373      1.1  christos 
   1374      1.1  christos   return ctf_type_kind_unsliced (inputs[CTF_DEDUP_GID_TO_INPUT (id)],
   1375      1.1  christos 				 CTF_DEDUP_GID_TO_TYPE (id));
   1376      1.1  christos }
   1377      1.1  christos 
   1378      1.1  christos /* Used to keep a count of types: i.e. distinct type hash values.  */
   1379      1.1  christos typedef struct ctf_dedup_type_counter
   1380      1.1  christos {
   1381  1.1.1.2  christos   ctf_dict_t *fp;
   1382  1.1.1.2  christos   ctf_dict_t **inputs;
   1383      1.1  christos   int num_non_forwards;
   1384      1.1  christos } ctf_dedup_type_counter_t;
   1385      1.1  christos 
   1386      1.1  christos /* Add to the type counter for one name entry from the cd_name_counts.  */
   1387      1.1  christos static int
   1388      1.1  christos ctf_dedup_count_types (void *key_, void *value _libctf_unused_, void *arg_)
   1389      1.1  christos {
   1390      1.1  christos   const char *hval = (const char *) key_;
   1391      1.1  christos   int kind;
   1392      1.1  christos   ctf_dedup_type_counter_t *arg = (ctf_dedup_type_counter_t *) arg_;
   1393      1.1  christos 
   1394      1.1  christos   kind = ctf_dedup_hash_kind (arg->fp, arg->inputs, hval);
   1395      1.1  christos 
   1396      1.1  christos   /* We rely on ctf_dedup_hash_kind setting the fp to -ECTF_INTERNAL on error to
   1397      1.1  christos      smuggle errors out of here.  */
   1398      1.1  christos 
   1399      1.1  christos   if (kind != CTF_K_FORWARD)
   1400      1.1  christos     {
   1401      1.1  christos       arg->num_non_forwards++;
   1402      1.1  christos       ctf_dprintf ("Counting hash %s: kind %i: num_non_forwards is %i\n",
   1403      1.1  christos 		   hval, kind, arg->num_non_forwards);
   1404      1.1  christos     }
   1405      1.1  christos 
   1406      1.1  christos   /* We only need to know if there is more than one non-forward (an ambiguous
   1407      1.1  christos      type): don't waste time iterating any more than needed to figure that
   1408      1.1  christos      out.  */
   1409      1.1  christos 
   1410      1.1  christos   if (arg->num_non_forwards > 1)
   1411      1.1  christos     return 1;
   1412      1.1  christos 
   1413      1.1  christos   return 0;
   1414      1.1  christos }
   1415      1.1  christos 
   1416      1.1  christos /* Detect name ambiguity and mark ambiguous names as conflicting, other than the
   1417      1.1  christos    most common.  */
   1418      1.1  christos static int
   1419  1.1.1.2  christos ctf_dedup_detect_name_ambiguity (ctf_dict_t *fp, ctf_dict_t **inputs)
   1420      1.1  christos {
   1421      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
   1422      1.1  christos   ctf_next_t *i = NULL;
   1423      1.1  christos   void *k;
   1424      1.1  christos   void *v;
   1425      1.1  christos   int err;
   1426      1.1  christos   const char *whaterr;
   1427      1.1  christos 
   1428      1.1  christos   /* Go through cd_name_counts for all CTF namespaces in turn.  */
   1429      1.1  christos 
   1430      1.1  christos   while ((err = ctf_dynhash_next (d->cd_name_counts, &i, &k, &v)) == 0)
   1431      1.1  christos     {
   1432      1.1  christos       const char *decorated = (const char *) k;
   1433      1.1  christos       ctf_dynhash_t *name_counts = (ctf_dynhash_t *) v;
   1434      1.1  christos       ctf_next_t *j = NULL;
   1435      1.1  christos 
   1436      1.1  christos       /* If this is a forwardable kind or a forward (which we can tell without
   1437      1.1  christos 	 consulting the type because its decorated name has a space as its
   1438      1.1  christos 	 second character: see ctf_decorate_type_name), we are only interested
   1439      1.1  christos 	 in whether this name has many hashes associated with it: any such name
   1440      1.1  christos 	 is necessarily ambiguous, and types with that name are conflicting.
   1441      1.1  christos 	 Once we know whether this is true, we can skip to the next name: so use
   1442      1.1  christos 	 ctf_dynhash_iter_find for efficiency.  */
   1443      1.1  christos 
   1444      1.1  christos       if (decorated[0] != '\0' && decorated[1] == ' ')
   1445      1.1  christos 	{
   1446      1.1  christos 	  ctf_dedup_type_counter_t counters = { fp, inputs, 0 };
   1447      1.1  christos 	  ctf_dynhash_t *counts = (ctf_dynhash_t *) v;
   1448      1.1  christos 
   1449      1.1  christos 	  ctf_dynhash_iter_find (counts, ctf_dedup_count_types, &counters);
   1450      1.1  christos 
   1451      1.1  christos 	  /* Check for assertion failure and pass it up.  */
   1452      1.1  christos 	  if (ctf_errno (fp) == ECTF_INTERNAL)
   1453      1.1  christos 	    goto assert_err;
   1454      1.1  christos 
   1455      1.1  christos 	  if (counters.num_non_forwards > 1)
   1456      1.1  christos 	    {
   1457      1.1  christos 	      const void *hval_;
   1458      1.1  christos 
   1459      1.1  christos 	      while ((err = ctf_dynhash_cnext (counts, &j, &hval_, NULL)) == 0)
   1460      1.1  christos 		{
   1461      1.1  christos 		  const char *hval = (const char *) hval_;
   1462      1.1  christos 		  ctf_dynset_t *type_ids;
   1463      1.1  christos 		  void *id;
   1464      1.1  christos 		  int kind;
   1465      1.1  christos 
   1466      1.1  christos 		  /* Dig through the types in this hash to find the non-forwards
   1467      1.1  christos 		     and mark them ambiguous.  */
   1468      1.1  christos 
   1469      1.1  christos 		  type_ids = ctf_dynhash_lookup (d->cd_output_mapping, hval);
   1470      1.1  christos 
   1471      1.1  christos 		  /* Nonexistent? Must be a forward with no referent.  */
   1472      1.1  christos 		  if (!type_ids)
   1473      1.1  christos 		    continue;
   1474      1.1  christos 
   1475      1.1  christos 		  id = ctf_dynset_lookup_any (type_ids);
   1476      1.1  christos 
   1477      1.1  christos 		  kind = ctf_type_kind (inputs[CTF_DEDUP_GID_TO_INPUT (id)],
   1478      1.1  christos 					CTF_DEDUP_GID_TO_TYPE (id));
   1479      1.1  christos 
   1480      1.1  christos 		  if (kind != CTF_K_FORWARD)
   1481      1.1  christos 		    {
   1482      1.1  christos 		      ctf_dprintf ("Marking %p, with hash %s, conflicting: one "
   1483      1.1  christos 				   "of many non-forward GIDs for %s\n", id,
   1484      1.1  christos 				   hval, (char *) k);
   1485      1.1  christos 		      ctf_dedup_mark_conflicting_hash (fp, hval);
   1486      1.1  christos 		    }
   1487      1.1  christos 		}
   1488      1.1  christos 	      if (err != ECTF_NEXT_END)
   1489      1.1  christos 		{
   1490      1.1  christos 		  whaterr = N_("error marking conflicting structs/unions");
   1491      1.1  christos 		  goto iterr;
   1492      1.1  christos 		}
   1493      1.1  christos 	    }
   1494      1.1  christos 	}
   1495      1.1  christos       else
   1496      1.1  christos 	{
   1497      1.1  christos 	  /* This is an ordinary type.  Find the most common type with this
   1498      1.1  christos 	     name, and mark it unconflicting: all others are conflicting.  (We
   1499      1.1  christos 	     cannot do this sort of popularity contest with forwardable types
   1500      1.1  christos 	     because any forwards to that type would be immediately unified with
   1501      1.1  christos 	     the most-popular type on insertion, and we want conflicting structs
   1502      1.1  christos 	     et al to have all forwards left intact, so the user is notified
   1503      1.1  christos 	     that this type is conflicting.  TODO: improve this in future by
   1504  1.1.1.2  christos 	     setting such forwards non-root-visible.)
   1505  1.1.1.2  christos 
   1506  1.1.1.2  christos 	     If multiple distinct types are "most common", pick the one that
   1507  1.1.1.2  christos 	     appears first on the link line, and within that, the one with the
   1508  1.1.1.2  christos 	     lowest type ID.  (See sort_output_mapping.)  */
   1509      1.1  christos 
   1510      1.1  christos 	  const void *key;
   1511      1.1  christos 	  const void *count;
   1512      1.1  christos 	  const char *hval;
   1513      1.1  christos 	  long max_hcount = -1;
   1514  1.1.1.2  christos 	  void *max_gid = NULL;
   1515      1.1  christos 	  const char *max_hval = NULL;
   1516      1.1  christos 
   1517      1.1  christos 	  if (ctf_dynhash_elements (name_counts) <= 1)
   1518      1.1  christos 	    continue;
   1519      1.1  christos 
   1520      1.1  christos 	  /* First find the most common.  */
   1521      1.1  christos 	  while ((err = ctf_dynhash_cnext (name_counts, &j, &key, &count)) == 0)
   1522      1.1  christos 	    {
   1523      1.1  christos 	      hval = (const char *) key;
   1524  1.1.1.2  christos 
   1525      1.1  christos 	      if ((long int) (uintptr_t) count > max_hcount)
   1526      1.1  christos 		{
   1527      1.1  christos 		  max_hcount = (long int) (uintptr_t) count;
   1528      1.1  christos 		  max_hval = hval;
   1529  1.1.1.2  christos 		  max_gid = ctf_dynhash_lookup (d->cd_output_first_gid, hval);
   1530  1.1.1.2  christos 		}
   1531  1.1.1.2  christos 	      else if ((long int) (uintptr_t) count == max_hcount)
   1532  1.1.1.2  christos 		{
   1533  1.1.1.2  christos 		  void *gid = ctf_dynhash_lookup (d->cd_output_first_gid, hval);
   1534  1.1.1.2  christos 
   1535  1.1.1.2  christos 		  if (CTF_DEDUP_GID_TO_INPUT(gid) < CTF_DEDUP_GID_TO_INPUT(max_gid)
   1536  1.1.1.2  christos 		      || (CTF_DEDUP_GID_TO_INPUT(gid) == CTF_DEDUP_GID_TO_INPUT(max_gid)
   1537  1.1.1.2  christos 			  && CTF_DEDUP_GID_TO_TYPE(gid) < CTF_DEDUP_GID_TO_TYPE(max_gid)))
   1538  1.1.1.2  christos 		    {
   1539  1.1.1.2  christos 		      max_hval = hval;
   1540  1.1.1.2  christos 		      max_gid = ctf_dynhash_lookup (d->cd_output_first_gid, hval);
   1541  1.1.1.2  christos 		    }
   1542      1.1  christos 		}
   1543      1.1  christos 	    }
   1544      1.1  christos 	  if (err != ECTF_NEXT_END)
   1545      1.1  christos 	    {
   1546      1.1  christos 	      whaterr = N_("error finding commonest conflicting type");
   1547      1.1  christos 	      goto iterr;
   1548      1.1  christos 	    }
   1549      1.1  christos 
   1550      1.1  christos 	  /* Mark all the others as conflicting.   */
   1551      1.1  christos 	  while ((err = ctf_dynhash_cnext (name_counts, &j, &key, NULL)) == 0)
   1552      1.1  christos 	    {
   1553      1.1  christos 	      hval = (const char *) key;
   1554      1.1  christos 	      if (strcmp (max_hval, hval) == 0)
   1555      1.1  christos 		continue;
   1556      1.1  christos 
   1557      1.1  christos 	      ctf_dprintf ("Marking %s, an uncommon hash for %s, conflicting\n",
   1558      1.1  christos 			   hval, (const char *) k);
   1559      1.1  christos 	      if (ctf_dedup_mark_conflicting_hash (fp, hval) < 0)
   1560      1.1  christos 		{
   1561      1.1  christos 		  whaterr = N_("error marking hashes as conflicting");
   1562      1.1  christos 		  goto err;
   1563      1.1  christos 		}
   1564      1.1  christos 	    }
   1565      1.1  christos 	  if (err != ECTF_NEXT_END)
   1566      1.1  christos 	    {
   1567      1.1  christos 	      whaterr = N_("marking uncommon conflicting types");
   1568      1.1  christos 	      goto iterr;
   1569      1.1  christos 	    }
   1570      1.1  christos 	}
   1571      1.1  christos     }
   1572      1.1  christos   if (err != ECTF_NEXT_END)
   1573      1.1  christos     {
   1574      1.1  christos       whaterr = N_("scanning for ambiguous names");
   1575      1.1  christos       goto iterr;
   1576      1.1  christos     }
   1577      1.1  christos 
   1578      1.1  christos   return 0;
   1579      1.1  christos 
   1580      1.1  christos  err:
   1581      1.1  christos   ctf_next_destroy (i);
   1582      1.1  christos   ctf_err_warn (fp, 0, 0, "%s", gettext (whaterr));
   1583      1.1  christos   return -1;					/* errno is set for us.  */
   1584      1.1  christos 
   1585      1.1  christos  iterr:
   1586      1.1  christos   ctf_err_warn (fp, 0, err, _("iteration failed: %s"), gettext (whaterr));
   1587      1.1  christos   return ctf_set_errno (fp, err);
   1588      1.1  christos 
   1589      1.1  christos  assert_err:
   1590      1.1  christos   ctf_next_destroy (i);
   1591      1.1  christos   return -1; 					/* errno is set for us.  */
   1592      1.1  christos }
   1593      1.1  christos 
   1594      1.1  christos /* Initialize the deduplication machinery.  */
   1595      1.1  christos 
   1596      1.1  christos static int
   1597  1.1.1.2  christos ctf_dedup_init (ctf_dict_t *fp)
   1598      1.1  christos {
   1599      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
   1600      1.1  christos   size_t i;
   1601      1.1  christos 
   1602      1.1  christos   if (ctf_dedup_atoms_init (fp) < 0)
   1603      1.1  christos       goto oom;
   1604      1.1  christos 
   1605      1.1  christos #if IDS_NEED_ALLOCATION
   1606  1.1.1.2  christos   if ((d->cd_id_to_dict_t = ctf_dynhash_create (ctf_hash_type_id_key,
   1607      1.1  christos 						ctf_hash_eq_type_id_key,
   1608      1.1  christos 						free, NULL)) == NULL)
   1609      1.1  christos     goto oom;
   1610      1.1  christos #endif
   1611      1.1  christos 
   1612      1.1  christos   for (i = 0; i < 4; i++)
   1613      1.1  christos     {
   1614      1.1  christos       if ((d->cd_decorated_names[i] = ctf_dynhash_create (ctf_hash_string,
   1615      1.1  christos 							  ctf_hash_eq_string,
   1616      1.1  christos 							  NULL, NULL)) == NULL)
   1617      1.1  christos 	goto oom;
   1618      1.1  christos     }
   1619      1.1  christos 
   1620      1.1  christos   if ((d->cd_name_counts
   1621      1.1  christos        = ctf_dynhash_create (ctf_hash_string,
   1622      1.1  christos 			     ctf_hash_eq_string, NULL,
   1623      1.1  christos 			     (ctf_hash_free_fun) ctf_dynhash_destroy)) == NULL)
   1624      1.1  christos     goto oom;
   1625      1.1  christos 
   1626      1.1  christos   if ((d->cd_type_hashes
   1627      1.1  christos        = ctf_dynhash_create (ctf_hash_integer,
   1628      1.1  christos 			     ctf_hash_eq_integer,
   1629      1.1  christos 			     NULL, NULL)) == NULL)
   1630      1.1  christos     goto oom;
   1631      1.1  christos 
   1632      1.1  christos   if ((d->cd_struct_origin
   1633      1.1  christos        = ctf_dynhash_create (ctf_hash_string,
   1634      1.1  christos 			     ctf_hash_eq_string,
   1635      1.1  christos 			     NULL, NULL)) == NULL)
   1636      1.1  christos     goto oom;
   1637      1.1  christos 
   1638      1.1  christos   if ((d->cd_citers
   1639      1.1  christos        = ctf_dynhash_create (ctf_hash_string,
   1640      1.1  christos 			     ctf_hash_eq_string, NULL,
   1641      1.1  christos 			     (ctf_hash_free_fun) ctf_dynset_destroy)) == NULL)
   1642      1.1  christos     goto oom;
   1643      1.1  christos 
   1644      1.1  christos   if ((d->cd_output_mapping
   1645      1.1  christos        = ctf_dynhash_create (ctf_hash_string,
   1646      1.1  christos 			     ctf_hash_eq_string, NULL,
   1647      1.1  christos 			     (ctf_hash_free_fun) ctf_dynset_destroy)) == NULL)
   1648      1.1  christos     goto oom;
   1649      1.1  christos 
   1650      1.1  christos   if ((d->cd_output_first_gid
   1651      1.1  christos        = ctf_dynhash_create (ctf_hash_string,
   1652      1.1  christos 			     ctf_hash_eq_string,
   1653      1.1  christos 			     NULL, NULL)) == NULL)
   1654      1.1  christos     goto oom;
   1655      1.1  christos 
   1656      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   1657      1.1  christos   if ((d->cd_output_mapping_guard
   1658      1.1  christos        = ctf_dynhash_create (ctf_hash_integer,
   1659      1.1  christos 			     ctf_hash_eq_integer, NULL, NULL)) == NULL)
   1660      1.1  christos     goto oom;
   1661      1.1  christos #endif
   1662      1.1  christos 
   1663  1.1.1.2  christos   if ((d->cd_input_nums
   1664  1.1.1.2  christos        = ctf_dynhash_create (ctf_hash_integer,
   1665  1.1.1.2  christos 			     ctf_hash_eq_integer,
   1666  1.1.1.2  christos 			     NULL, NULL)) == NULL)
   1667  1.1.1.2  christos     goto oom;
   1668  1.1.1.2  christos 
   1669      1.1  christos   if ((d->cd_emission_struct_members
   1670      1.1  christos        = ctf_dynhash_create (ctf_hash_integer,
   1671      1.1  christos 			     ctf_hash_eq_integer,
   1672      1.1  christos 			     NULL, NULL)) == NULL)
   1673      1.1  christos     goto oom;
   1674      1.1  christos 
   1675      1.1  christos   if ((d->cd_conflicting_types
   1676      1.1  christos        = ctf_dynset_create (htab_hash_string,
   1677  1.1.1.2  christos 			    htab_eq_string, NULL)) == NULL)
   1678      1.1  christos     goto oom;
   1679      1.1  christos 
   1680      1.1  christos   return 0;
   1681      1.1  christos 
   1682      1.1  christos  oom:
   1683      1.1  christos   ctf_err_warn (fp, 0, ENOMEM, _("ctf_dedup_init: cannot initialize: "
   1684      1.1  christos 				 "out of memory"));
   1685      1.1  christos   return ctf_set_errno (fp, ENOMEM);
   1686      1.1  christos }
   1687      1.1  christos 
   1688  1.1.1.2  christos /* No ctf_dedup calls are allowed after this call other than starting a new
   1689  1.1.1.2  christos    deduplication via ctf_dedup (not even ctf_dedup_type_mapping lookups).  */
   1690      1.1  christos void
   1691  1.1.1.2  christos ctf_dedup_fini (ctf_dict_t *fp, ctf_dict_t **outputs, uint32_t noutputs)
   1692      1.1  christos {
   1693      1.1  christos   ctf_dedup_t *d = &fp->ctf_dedup;
   1694      1.1  christos   size_t i;
   1695      1.1  christos 
   1696      1.1  christos   /* ctf_dedup_atoms is kept across links.  */
   1697      1.1  christos #if IDS_NEED_ALLOCATION
   1698  1.1.1.2  christos   ctf_dynhash_destroy (d->cd_id_to_dict_t);
   1699      1.1  christos #endif
   1700      1.1  christos   for (i = 0; i < 4; i++)
   1701      1.1  christos     ctf_dynhash_destroy (d->cd_decorated_names[i]);
   1702      1.1  christos   ctf_dynhash_destroy (d->cd_name_counts);
   1703      1.1  christos   ctf_dynhash_destroy (d->cd_type_hashes);
   1704      1.1  christos   ctf_dynhash_destroy (d->cd_struct_origin);
   1705      1.1  christos   ctf_dynhash_destroy (d->cd_citers);
   1706      1.1  christos   ctf_dynhash_destroy (d->cd_output_mapping);
   1707      1.1  christos   ctf_dynhash_destroy (d->cd_output_first_gid);
   1708      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   1709      1.1  christos   ctf_dynhash_destroy (d->cd_output_mapping_guard);
   1710      1.1  christos #endif
   1711  1.1.1.2  christos   ctf_dynhash_destroy (d->cd_input_nums);
   1712      1.1  christos   ctf_dynhash_destroy (d->cd_emission_struct_members);
   1713      1.1  christos   ctf_dynset_destroy (d->cd_conflicting_types);
   1714      1.1  christos 
   1715      1.1  christos   /* Free the per-output state.  */
   1716      1.1  christos   if (outputs)
   1717      1.1  christos     {
   1718      1.1  christos       for (i = 0; i < noutputs; i++)
   1719      1.1  christos 	{
   1720      1.1  christos 	  ctf_dedup_t *od = &outputs[i]->ctf_dedup;
   1721      1.1  christos 	  ctf_dynhash_destroy (od->cd_output_emission_hashes);
   1722      1.1  christos 	  ctf_dynhash_destroy (od->cd_output_emission_conflicted_forwards);
   1723  1.1.1.2  christos 	  ctf_dict_close (od->cd_output);
   1724      1.1  christos 	}
   1725      1.1  christos     }
   1726      1.1  christos   memset (d, 0, sizeof (ctf_dedup_t));
   1727      1.1  christos }
   1728      1.1  christos 
   1729      1.1  christos /* Return 1 if this type is cited by multiple input dictionaries.  */
   1730      1.1  christos 
   1731      1.1  christos static int
   1732  1.1.1.2  christos ctf_dedup_multiple_input_dicts (ctf_dict_t *output, ctf_dict_t **inputs,
   1733      1.1  christos 				const char *hval)
   1734      1.1  christos {
   1735      1.1  christos   ctf_dedup_t *d = &output->ctf_dedup;
   1736      1.1  christos   ctf_dynset_t *type_ids;
   1737      1.1  christos   ctf_next_t *i = NULL;
   1738      1.1  christos   void *id;
   1739  1.1.1.2  christos   ctf_dict_t *found = NULL, *relative_found = NULL;
   1740      1.1  christos   const char *type_id;
   1741  1.1.1.2  christos   ctf_dict_t *input_fp;
   1742      1.1  christos   ctf_id_t input_id;
   1743      1.1  christos   const char *name;
   1744      1.1  christos   const char *decorated;
   1745      1.1  christos   int fwdkind;
   1746      1.1  christos   int multiple = 0;
   1747      1.1  christos   int err;
   1748      1.1  christos 
   1749      1.1  christos   type_ids = ctf_dynhash_lookup (d->cd_output_mapping, hval);
   1750      1.1  christos   if (!ctf_assert (output, type_ids))
   1751      1.1  christos     return -1;
   1752      1.1  christos 
   1753      1.1  christos   /* Scan across the IDs until we find proof that two disjoint dictionaries
   1754      1.1  christos      are referenced.  Exit as soon as possible.  Optimization opportunity, but
   1755      1.1  christos      possibly not worth it, given that this is only executed in
   1756      1.1  christos      CTF_LINK_SHARE_DUPLICATED mode.  */
   1757      1.1  christos 
   1758      1.1  christos   while ((err = ctf_dynset_next (type_ids, &i, &id)) == 0)
   1759      1.1  christos     {
   1760  1.1.1.2  christos       ctf_dict_t *fp = inputs[CTF_DEDUP_GID_TO_INPUT (id)];
   1761      1.1  christos 
   1762      1.1  christos       if (fp == found || fp == relative_found)
   1763      1.1  christos 	continue;
   1764      1.1  christos 
   1765      1.1  christos       if (!found)
   1766      1.1  christos 	{
   1767      1.1  christos 	  found = fp;
   1768      1.1  christos 	  continue;
   1769      1.1  christos 	}
   1770      1.1  christos 
   1771      1.1  christos       if (!relative_found
   1772      1.1  christos 	  && (fp->ctf_parent == found || found->ctf_parent == fp))
   1773      1.1  christos 	{
   1774      1.1  christos 	  relative_found = fp;
   1775      1.1  christos 	  continue;
   1776      1.1  christos 	}
   1777      1.1  christos 
   1778      1.1  christos       multiple = 1;
   1779      1.1  christos       ctf_next_destroy (i);
   1780      1.1  christos       break;
   1781      1.1  christos     }
   1782      1.1  christos   if ((err != ECTF_NEXT_END) && (err != 0))
   1783      1.1  christos     {
   1784      1.1  christos       ctf_err_warn (output, 0, err, _("iteration error "
   1785      1.1  christos 				      "propagating conflictedness"));
   1786      1.1  christos       return ctf_set_errno (output, err);
   1787      1.1  christos     }
   1788      1.1  christos 
   1789      1.1  christos   if (multiple)
   1790      1.1  christos     return multiple;
   1791      1.1  christos 
   1792      1.1  christos   /* This type itself does not appear in multiple input dicts: how about another
   1793      1.1  christos      related type with the same name (e.g. a forward if this is a struct,
   1794      1.1  christos      etc).  */
   1795      1.1  christos 
   1796      1.1  christos   type_id = ctf_dynset_lookup_any (type_ids);
   1797      1.1  christos   if (!ctf_assert (output, type_id))
   1798      1.1  christos     return -1;
   1799      1.1  christos 
   1800      1.1  christos   input_fp = inputs[CTF_DEDUP_GID_TO_INPUT (type_id)];
   1801      1.1  christos   input_id = CTF_DEDUP_GID_TO_TYPE (type_id);
   1802      1.1  christos   fwdkind = ctf_type_kind_forwarded (input_fp, input_id);
   1803      1.1  christos   name = ctf_type_name_raw (input_fp, input_id);
   1804      1.1  christos 
   1805      1.1  christos   if ((fwdkind == CTF_K_STRUCT || fwdkind == CTF_K_UNION)
   1806  1.1.1.2  christos       && name[0] != '\0')
   1807      1.1  christos     {
   1808      1.1  christos       const void *origin;
   1809      1.1  christos 
   1810      1.1  christos       if ((decorated = ctf_decorate_type_name (output, name,
   1811      1.1  christos 					       fwdkind)) == NULL)
   1812      1.1  christos 	return -1;				/* errno is set for us.  */
   1813      1.1  christos 
   1814      1.1  christos       origin = ctf_dynhash_lookup (d->cd_struct_origin, decorated);
   1815      1.1  christos       if ((origin != NULL) && (CTF_DEDUP_GID_TO_INPUT (origin) < 0))
   1816      1.1  christos 	multiple = 1;
   1817      1.1  christos     }
   1818      1.1  christos 
   1819      1.1  christos   return multiple;
   1820      1.1  christos }
   1821      1.1  christos 
   1822      1.1  christos /* Demote unconflicting types which reference only one input, or which reference
   1823      1.1  christos    two inputs where one input is the parent of the other, into conflicting
   1824      1.1  christos    types.  Only used if the link mode is CTF_LINK_SHARE_DUPLICATED.  */
   1825      1.1  christos 
   1826      1.1  christos static int
   1827  1.1.1.2  christos ctf_dedup_conflictify_unshared (ctf_dict_t *output, ctf_dict_t **inputs)
   1828      1.1  christos {
   1829      1.1  christos   ctf_dedup_t *d = &output->ctf_dedup;
   1830      1.1  christos   ctf_next_t *i = NULL;
   1831      1.1  christos   int err;
   1832      1.1  christos   const void *k;
   1833      1.1  christos   ctf_dynset_t *to_mark = NULL;
   1834      1.1  christos 
   1835  1.1.1.2  christos   if ((to_mark = ctf_dynset_create (htab_hash_string, htab_eq_string,
   1836      1.1  christos 				    NULL)) == NULL)
   1837      1.1  christos     goto err_no;
   1838      1.1  christos 
   1839      1.1  christos   while ((err = ctf_dynhash_cnext (d->cd_output_mapping, &i, &k, NULL)) == 0)
   1840      1.1  christos     {
   1841      1.1  christos       const char *hval = (const char *) k;
   1842      1.1  christos       int conflicting;
   1843      1.1  christos 
   1844      1.1  christos       /* Types referenced by only one dict, with no type appearing under that
   1845      1.1  christos 	 name elsewhere, are marked conflicting.  */
   1846      1.1  christos 
   1847      1.1  christos       conflicting = !ctf_dedup_multiple_input_dicts (output, inputs, hval);
   1848      1.1  christos 
   1849      1.1  christos       if (conflicting < 0)
   1850      1.1  christos 	goto err;				/* errno is set for us.  */
   1851      1.1  christos 
   1852      1.1  christos       if (conflicting)
   1853      1.1  christos 	if (ctf_dynset_cinsert (to_mark, hval) < 0)
   1854      1.1  christos 	  goto err;
   1855      1.1  christos     }
   1856      1.1  christos   if (err != ECTF_NEXT_END)
   1857      1.1  christos     goto iterr;
   1858      1.1  christos 
   1859      1.1  christos   while ((err = ctf_dynset_cnext (to_mark, &i, &k)) == 0)
   1860      1.1  christos     {
   1861      1.1  christos       const char *hval = (const char *) k;
   1862      1.1  christos 
   1863      1.1  christos       if (ctf_dedup_mark_conflicting_hash (output, hval) < 0)
   1864      1.1  christos 	goto err;
   1865      1.1  christos     }
   1866      1.1  christos   if (err != ECTF_NEXT_END)
   1867      1.1  christos     goto iterr;
   1868      1.1  christos 
   1869      1.1  christos   ctf_dynset_destroy (to_mark);
   1870      1.1  christos 
   1871      1.1  christos   return 0;
   1872      1.1  christos 
   1873      1.1  christos  err_no:
   1874      1.1  christos   ctf_set_errno (output, errno);
   1875      1.1  christos  err:
   1876      1.1  christos   err = ctf_errno (output);
   1877      1.1  christos   ctf_next_destroy (i);
   1878      1.1  christos  iterr:
   1879      1.1  christos   ctf_dynset_destroy (to_mark);
   1880      1.1  christos   ctf_err_warn (output, 0, err, _("conflictifying unshared types"));
   1881      1.1  christos   return ctf_set_errno (output, err);
   1882      1.1  christos }
   1883      1.1  christos 
   1884      1.1  christos /* The core deduplicator.  Populate cd_output_mapping in the output ctf_dedup
   1885      1.1  christos    with a mapping of all types that belong in this dictionary and where they
   1886      1.1  christos    come from, and cd_conflicting_types with an indication of whether each type
   1887      1.1  christos    is conflicted or not.  OUTPUT is the top-level output: INPUTS is the array of
   1888      1.1  christos    input dicts; NINPUTS is the size of that array; PARENTS is an NINPUTS-element
   1889      1.1  christos    array with each element corresponding to a input which is a child dict set to
   1890      1.1  christos    the number in the INPUTS array of that input's parent.
   1891      1.1  christos 
   1892      1.1  christos    If CU_MAPPED is set, this is a first pass for a link with a non-empty CU
   1893      1.1  christos    mapping: only one output will result.
   1894      1.1  christos 
   1895      1.1  christos    Only deduplicates: does not emit the types into the output.  Call
   1896      1.1  christos    ctf_dedup_emit afterwards to do that.  */
   1897      1.1  christos 
   1898      1.1  christos int
   1899  1.1.1.2  christos ctf_dedup (ctf_dict_t *output, ctf_dict_t **inputs, uint32_t ninputs,
   1900      1.1  christos 	   uint32_t *parents, int cu_mapped)
   1901      1.1  christos {
   1902      1.1  christos   ctf_dedup_t *d = &output->ctf_dedup;
   1903      1.1  christos   size_t i;
   1904      1.1  christos   ctf_next_t *it = NULL;
   1905      1.1  christos 
   1906      1.1  christos   if (ctf_dedup_init (output) < 0)
   1907      1.1  christos     return -1; 					/* errno is set for us.  */
   1908      1.1  christos 
   1909  1.1.1.2  christos   for (i = 0; i < ninputs; i++)
   1910  1.1.1.2  christos     {
   1911  1.1.1.2  christos       ctf_dprintf ("Input %i: %s\n", (int) i, ctf_link_input_name (inputs[i]));
   1912  1.1.1.2  christos       if (ctf_dynhash_insert (d->cd_input_nums, inputs[i],
   1913  1.1.1.2  christos 			      (void *) (uintptr_t) i) < 0)
   1914  1.1.1.2  christos 	{
   1915  1.1.1.2  christos 	  ctf_set_errno (output, errno);
   1916  1.1.1.2  christos 	  ctf_err_warn (output, 0, errno, _("ctf_dedup: cannot initialize: %s\n"),
   1917  1.1.1.2  christos 			ctf_errmsg (errno));
   1918  1.1.1.2  christos 	  goto err;
   1919  1.1.1.2  christos 	}
   1920  1.1.1.2  christos     }
   1921  1.1.1.2  christos 
   1922      1.1  christos   /* Some flags do not apply when CU-mapping: this is not a duplicated link,
   1923      1.1  christos      because there is only one output and we really don't want to end up marking
   1924      1.1  christos      all nonconflicting but appears-only-once types as conflicting (which in the
   1925      1.1  christos      CU-mapped link means we'd mark them all as non-root-visible!).  */
   1926      1.1  christos   d->cd_link_flags = output->ctf_link_flags;
   1927      1.1  christos   if (cu_mapped)
   1928      1.1  christos     d->cd_link_flags &= ~(CTF_LINK_SHARE_DUPLICATED);
   1929      1.1  christos 
   1930      1.1  christos   /* Compute hash values for all types, recursively, treating child structures
   1931      1.1  christos      and unions equivalent to forwards, and hashing in the name of the referent
   1932      1.1  christos      of each such type into structures, unions, and non-opaque forwards.
   1933      1.1  christos      Populate a mapping from decorated name (including an indication of
   1934      1.1  christos      struct/union/enum namespace) to count of type hash values in
   1935      1.1  christos      cd_name_counts, a mapping from and a mapping from hash values to input type
   1936      1.1  christos      IDs in cd_output_mapping.  */
   1937      1.1  christos 
   1938      1.1  christos   ctf_dprintf ("Computing type hashes\n");
   1939      1.1  christos   for (i = 0; i < ninputs; i++)
   1940      1.1  christos     {
   1941      1.1  christos       ctf_id_t id;
   1942      1.1  christos 
   1943      1.1  christos       while ((id = ctf_type_next (inputs[i], &it, NULL, 1)) != CTF_ERR)
   1944      1.1  christos 	{
   1945  1.1.1.2  christos 	  if (ctf_dedup_hash_type (output, inputs[i], inputs,
   1946  1.1.1.2  christos 				   parents, i, id, 0, 0,
   1947  1.1.1.2  christos 				   ctf_dedup_populate_mappings) == NULL)
   1948  1.1.1.2  christos 	    goto err;				/* errno is set for us.  */
   1949      1.1  christos 	}
   1950      1.1  christos       if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
   1951      1.1  christos 	{
   1952      1.1  christos 	  ctf_set_errno (output, ctf_errno (inputs[i]));
   1953      1.1  christos 	  ctf_err_warn (output, 0, 0, _("iteration failure "
   1954      1.1  christos 					"computing type hashes"));
   1955  1.1.1.2  christos 	  goto err;
   1956      1.1  christos 	}
   1957      1.1  christos     }
   1958      1.1  christos 
   1959      1.1  christos   /* Go through the cd_name_counts name->hash->count mapping for all CTF
   1960      1.1  christos      namespaces: any name with many hashes associated with it at this stage is
   1961      1.1  christos      necessarily ambiguous.  Mark all the hashes except the most common as
   1962      1.1  christos      conflicting in the output.  */
   1963      1.1  christos 
   1964      1.1  christos   ctf_dprintf ("Detecting type name ambiguity\n");
   1965      1.1  christos   if (ctf_dedup_detect_name_ambiguity (output, inputs) < 0)
   1966  1.1.1.2  christos       goto err;					/* errno is set for us.  */
   1967      1.1  christos 
   1968      1.1  christos   /* If the link mode is CTF_LINK_SHARE_DUPLICATED, we change any unconflicting
   1969      1.1  christos      types whose output mapping references only one input dict into a
   1970      1.1  christos      conflicting type, so that they end up in the per-CU dictionaries.  */
   1971      1.1  christos 
   1972      1.1  christos   if (d->cd_link_flags & CTF_LINK_SHARE_DUPLICATED)
   1973      1.1  christos     {
   1974      1.1  christos       ctf_dprintf ("Conflictifying unshared types\n");
   1975      1.1  christos       if (ctf_dedup_conflictify_unshared (output, inputs) < 0)
   1976  1.1.1.2  christos 	goto err;				/* errno is set for us.  */
   1977      1.1  christos     }
   1978      1.1  christos   return 0;
   1979  1.1.1.2  christos 
   1980  1.1.1.2  christos  err:
   1981  1.1.1.2  christos   ctf_dedup_fini (output, NULL, 0);
   1982  1.1.1.2  christos   return -1;
   1983      1.1  christos }
   1984      1.1  christos 
   1985      1.1  christos static int
   1986  1.1.1.2  christos ctf_dedup_rwalk_output_mapping (ctf_dict_t *output, ctf_dict_t **inputs,
   1987      1.1  christos 				uint32_t ninputs, uint32_t *parents,
   1988      1.1  christos 				ctf_dynset_t *already_visited,
   1989      1.1  christos 				const char *hval,
   1990      1.1  christos 				int (*visit_fun) (const char *hval,
   1991  1.1.1.2  christos 						  ctf_dict_t *output,
   1992  1.1.1.2  christos 						  ctf_dict_t **inputs,
   1993      1.1  christos 						  uint32_t ninputs,
   1994      1.1  christos 						  uint32_t *parents,
   1995      1.1  christos 						  int already_visited,
   1996  1.1.1.2  christos 						  ctf_dict_t *input,
   1997      1.1  christos 						  ctf_id_t type,
   1998      1.1  christos 						  void *id,
   1999      1.1  christos 						  int depth,
   2000      1.1  christos 						  void *arg),
   2001      1.1  christos 				void *arg, unsigned long depth);
   2002      1.1  christos 
   2003      1.1  christos /* Like ctf_dedup_rwalk_output_mapping (which see), only takes a single target
   2004      1.1  christos    type and visits it.  */
   2005      1.1  christos static int
   2006  1.1.1.2  christos ctf_dedup_rwalk_one_output_mapping (ctf_dict_t *output,
   2007  1.1.1.2  christos 				    ctf_dict_t **inputs, uint32_t ninputs,
   2008      1.1  christos 				    uint32_t *parents,
   2009      1.1  christos 				    ctf_dynset_t *already_visited,
   2010      1.1  christos 				    int visited, void *type_id,
   2011      1.1  christos 				    const char *hval,
   2012      1.1  christos 				    int (*visit_fun) (const char *hval,
   2013  1.1.1.2  christos 						      ctf_dict_t *output,
   2014  1.1.1.2  christos 						      ctf_dict_t **inputs,
   2015      1.1  christos 						      uint32_t ninputs,
   2016      1.1  christos 						      uint32_t *parents,
   2017      1.1  christos 						      int already_visited,
   2018  1.1.1.2  christos 						      ctf_dict_t *input,
   2019      1.1  christos 						      ctf_id_t type,
   2020      1.1  christos 						      void *id,
   2021      1.1  christos 						      int depth,
   2022      1.1  christos 						      void *arg),
   2023      1.1  christos 				    void *arg, unsigned long depth)
   2024      1.1  christos {
   2025      1.1  christos   ctf_dedup_t *d = &output->ctf_dedup;
   2026  1.1.1.2  christos   ctf_dict_t *fp;
   2027      1.1  christos   int input_num;
   2028      1.1  christos   ctf_id_t type;
   2029      1.1  christos   int ret;
   2030      1.1  christos   const char *whaterr;
   2031      1.1  christos 
   2032      1.1  christos   input_num = CTF_DEDUP_GID_TO_INPUT (type_id);
   2033      1.1  christos   fp = inputs[input_num];
   2034      1.1  christos   type = CTF_DEDUP_GID_TO_TYPE (type_id);
   2035      1.1  christos 
   2036      1.1  christos   ctf_dprintf ("%lu: Starting walk over type %s, %i/%lx (%p), from %s, "
   2037      1.1  christos 	       "kind %i\n", depth, hval, input_num, type, (void *) fp,
   2038      1.1  christos 	       ctf_link_input_name (fp), ctf_type_kind_unsliced (fp, type));
   2039      1.1  christos 
   2040      1.1  christos   /* Get the single call we do if this type has already been visited out of the
   2041      1.1  christos      way.  */
   2042      1.1  christos   if (visited)
   2043      1.1  christos     return visit_fun (hval, output, inputs, ninputs, parents, visited, fp,
   2044      1.1  christos 		      type, type_id, depth, arg);
   2045      1.1  christos 
   2046      1.1  christos   /* This macro is really ugly, but the alternative is repeating this code many
   2047      1.1  christos      times, which is worse.  */
   2048      1.1  christos 
   2049      1.1  christos #define CTF_TYPE_WALK(type, errlabel, errmsg)				\
   2050  1.1.1.2  christos   do									\
   2051  1.1.1.2  christos     {									\
   2052  1.1.1.2  christos       void *type_id;							\
   2053  1.1.1.2  christos       const char *hashval;						\
   2054  1.1.1.2  christos       int cited_type_input_num = input_num;				\
   2055      1.1  christos 									\
   2056  1.1.1.2  christos       if ((fp->ctf_flags & LCTF_CHILD) && (LCTF_TYPE_ISPARENT (fp, type))) \
   2057  1.1.1.2  christos 	cited_type_input_num = parents[input_num];			\
   2058      1.1  christos 									\
   2059  1.1.1.2  christos       type_id = CTF_DEDUP_GID (output, cited_type_input_num, type);	\
   2060      1.1  christos 									\
   2061  1.1.1.2  christos       if (type == 0)							\
   2062  1.1.1.2  christos 	{								\
   2063  1.1.1.2  christos 	  ctf_dprintf ("Walking: unimplemented type\n");		\
   2064  1.1.1.2  christos 	  break;							\
   2065  1.1.1.2  christos 	}								\
   2066      1.1  christos 									\
   2067  1.1.1.2  christos       ctf_dprintf ("Looking up ID %i/%lx in type hashes\n",		\
   2068  1.1.1.2  christos 		   cited_type_input_num, type);				\
   2069  1.1.1.2  christos       hashval = ctf_dynhash_lookup (d->cd_type_hashes, type_id);	\
   2070  1.1.1.2  christos       if (!ctf_assert (output, hashval))				\
   2071  1.1.1.2  christos 	{								\
   2072  1.1.1.2  christos 	  whaterr = N_("error looking up ID in type hashes");		\
   2073  1.1.1.2  christos 	  goto errlabel;						\
   2074  1.1.1.2  christos 	}								\
   2075  1.1.1.2  christos       ctf_dprintf ("ID %i/%lx has hash %s\n", cited_type_input_num, type, \
   2076  1.1.1.2  christos 		   hashval);						\
   2077      1.1  christos 									\
   2078  1.1.1.2  christos       ret = ctf_dedup_rwalk_output_mapping (output, inputs, ninputs, parents, \
   2079  1.1.1.2  christos 					    already_visited, hashval,	\
   2080  1.1.1.2  christos 					    visit_fun, arg, depth);	\
   2081  1.1.1.2  christos       if (ret < 0)							\
   2082  1.1.1.2  christos 	{								\
   2083  1.1.1.2  christos 	  whaterr = errmsg;						\
   2084  1.1.1.2  christos 	  goto errlabel;						\
   2085  1.1.1.2  christos 	}								\
   2086  1.1.1.2  christos     }									\
   2087  1.1.1.2  christos   while (0)
   2088      1.1  christos 
   2089      1.1  christos   switch (ctf_type_kind_unsliced (fp, type))
   2090      1.1  christos     {
   2091      1.1  christos     case CTF_K_UNKNOWN:
   2092      1.1  christos     case CTF_K_FORWARD:
   2093      1.1  christos     case CTF_K_INTEGER:
   2094      1.1  christos     case CTF_K_FLOAT:
   2095      1.1  christos     case CTF_K_ENUM:
   2096      1.1  christos       /* No types referenced.  */
   2097      1.1  christos       break;
   2098      1.1  christos 
   2099      1.1  christos     case CTF_K_TYPEDEF:
   2100      1.1  christos     case CTF_K_VOLATILE:
   2101      1.1  christos     case CTF_K_CONST:
   2102      1.1  christos     case CTF_K_RESTRICT:
   2103      1.1  christos     case CTF_K_POINTER:
   2104      1.1  christos     case CTF_K_SLICE:
   2105      1.1  christos       CTF_TYPE_WALK (ctf_type_reference (fp, type), err,
   2106      1.1  christos 		     N_("error during referenced type walk"));
   2107      1.1  christos       break;
   2108      1.1  christos 
   2109      1.1  christos     case CTF_K_ARRAY:
   2110      1.1  christos       {
   2111      1.1  christos 	ctf_arinfo_t ar;
   2112      1.1  christos 
   2113      1.1  christos 	if (ctf_array_info (fp, type, &ar) < 0)
   2114      1.1  christos 	  {
   2115      1.1  christos 	    whaterr = N_("error during array info lookup");
   2116      1.1  christos 	    goto err_msg;
   2117      1.1  christos 	  }
   2118      1.1  christos 
   2119      1.1  christos 	CTF_TYPE_WALK (ar.ctr_contents, err,
   2120      1.1  christos 		       N_("error during array contents type walk"));
   2121      1.1  christos 	CTF_TYPE_WALK (ar.ctr_index, err,
   2122      1.1  christos 		       N_("error during array index type walk"));
   2123      1.1  christos 	break;
   2124      1.1  christos       }
   2125      1.1  christos 
   2126      1.1  christos     case CTF_K_FUNCTION:
   2127      1.1  christos       {
   2128      1.1  christos 	ctf_funcinfo_t fi;
   2129      1.1  christos 	ctf_id_t *args;
   2130      1.1  christos 	uint32_t j;
   2131      1.1  christos 
   2132      1.1  christos 	if (ctf_func_type_info (fp, type, &fi) < 0)
   2133      1.1  christos 	  {
   2134      1.1  christos 	    whaterr = N_("error during func type info lookup");
   2135      1.1  christos 	    goto err_msg;
   2136      1.1  christos 	  }
   2137      1.1  christos 
   2138      1.1  christos 	CTF_TYPE_WALK (fi.ctc_return, err,
   2139      1.1  christos 		       N_("error during func return type walk"));
   2140      1.1  christos 
   2141      1.1  christos 	if ((args = calloc (fi.ctc_argc, sizeof (ctf_id_t))) == NULL)
   2142      1.1  christos 	  {
   2143      1.1  christos 	    whaterr = N_("error doing memory allocation");
   2144      1.1  christos 	    goto err_msg;
   2145      1.1  christos 	  }
   2146      1.1  christos 
   2147      1.1  christos 	if (ctf_func_type_args (fp, type, fi.ctc_argc, args) < 0)
   2148      1.1  christos 	  {
   2149      1.1  christos 	    whaterr = N_("error doing func arg type lookup");
   2150      1.1  christos 	    free (args);
   2151      1.1  christos 	    goto err_msg;
   2152      1.1  christos 	  }
   2153      1.1  christos 
   2154      1.1  christos 	for (j = 0; j < fi.ctc_argc; j++)
   2155      1.1  christos 	  CTF_TYPE_WALK (args[j], err_free_args,
   2156      1.1  christos 			 N_("error during Func arg type walk"));
   2157      1.1  christos 	free (args);
   2158      1.1  christos 	break;
   2159      1.1  christos 
   2160      1.1  christos       err_free_args:
   2161      1.1  christos 	free (args);
   2162      1.1  christos 	goto err;
   2163      1.1  christos       }
   2164      1.1  christos     case CTF_K_STRUCT:
   2165      1.1  christos     case CTF_K_UNION:
   2166      1.1  christos       /* We do not recursively traverse the members of structures: they are
   2167      1.1  christos 	 emitted later, in a separate pass.  */
   2168      1.1  christos 	break;
   2169      1.1  christos     default:
   2170      1.1  christos       whaterr = N_("CTF dict corruption: unknown type kind");
   2171      1.1  christos       goto err_msg;
   2172      1.1  christos     }
   2173      1.1  christos 
   2174      1.1  christos   return visit_fun (hval, output, inputs, ninputs, parents, visited, fp, type,
   2175      1.1  christos 		    type_id, depth, arg);
   2176      1.1  christos 
   2177      1.1  christos  err_msg:
   2178      1.1  christos   ctf_set_errno (output, ctf_errno (fp));
   2179      1.1  christos   ctf_err_warn (output, 0, 0, _("%s in input file %s at type ID %lx"),
   2180      1.1  christos 		gettext (whaterr), ctf_link_input_name (fp), type);
   2181      1.1  christos  err:
   2182      1.1  christos   return -1;
   2183      1.1  christos }
   2184      1.1  christos /* Recursively traverse the output mapping, and do something with each type
   2185      1.1  christos    visited, from leaves to root.  VISIT_FUN, called as recursion unwinds,
   2186      1.1  christos    returns a negative error code or zero.  Type hashes may be visited more than
   2187      1.1  christos    once, but are not recursed through repeatedly: ALREADY_VISITED tracks whether
   2188      1.1  christos    types have already been visited.  */
   2189      1.1  christos static int
   2190  1.1.1.2  christos ctf_dedup_rwalk_output_mapping (ctf_dict_t *output, ctf_dict_t **inputs,
   2191      1.1  christos 				uint32_t ninputs, uint32_t *parents,
   2192      1.1  christos 				ctf_dynset_t *already_visited,
   2193      1.1  christos 				const char *hval,
   2194      1.1  christos 				int (*visit_fun) (const char *hval,
   2195  1.1.1.2  christos 						  ctf_dict_t *output,
   2196  1.1.1.2  christos 						  ctf_dict_t **inputs,
   2197      1.1  christos 						  uint32_t ninputs,
   2198      1.1  christos 						  uint32_t *parents,
   2199      1.1  christos 						  int already_visited,
   2200  1.1.1.2  christos 						  ctf_dict_t *input,
   2201      1.1  christos 						  ctf_id_t type,
   2202      1.1  christos 						  void *id,
   2203      1.1  christos 						  int depth,
   2204      1.1  christos 						  void *arg),
   2205      1.1  christos 				void *arg, unsigned long depth)
   2206      1.1  christos {
   2207      1.1  christos   ctf_dedup_t *d = &output->ctf_dedup;
   2208      1.1  christos   ctf_next_t *i = NULL;
   2209      1.1  christos   int err;
   2210      1.1  christos   int visited = 1;
   2211      1.1  christos   ctf_dynset_t *type_ids;
   2212      1.1  christos   void *id;
   2213      1.1  christos 
   2214      1.1  christos   depth++;
   2215      1.1  christos 
   2216      1.1  christos   type_ids = ctf_dynhash_lookup (d->cd_output_mapping, hval);
   2217      1.1  christos   if (!type_ids)
   2218      1.1  christos     {
   2219      1.1  christos       ctf_err_warn (output, 0, ECTF_INTERNAL,
   2220      1.1  christos 		    _("looked up type kind by nonexistent hash %s"), hval);
   2221      1.1  christos       return ctf_set_errno (output, ECTF_INTERNAL);
   2222      1.1  christos     }
   2223      1.1  christos 
   2224      1.1  christos   /* Have we seen this type before?  */
   2225      1.1  christos 
   2226      1.1  christos   if (!ctf_dynset_exists (already_visited, hval, NULL))
   2227      1.1  christos     {
   2228      1.1  christos       /* Mark as already-visited immediately, to eliminate the possibility of
   2229      1.1  christos 	 cycles: but remember we have not actually visited it yet for the
   2230      1.1  christos 	 upcoming call to the visit_fun.  (All our callers handle cycles
   2231      1.1  christos 	 properly themselves, so we can just abort them aggressively as soon as
   2232      1.1  christos 	 we find ourselves in one.)  */
   2233      1.1  christos 
   2234      1.1  christos       visited = 0;
   2235      1.1  christos       if (ctf_dynset_cinsert (already_visited, hval) < 0)
   2236      1.1  christos 	{
   2237      1.1  christos 	  ctf_err_warn (output, 0, ENOMEM,
   2238      1.1  christos 			_("out of memory tracking already-visited types"));
   2239      1.1  christos 	  return ctf_set_errno (output, ENOMEM);
   2240      1.1  christos 	}
   2241      1.1  christos     }
   2242      1.1  christos 
   2243      1.1  christos   /* If this type is marked conflicted, traverse members and call
   2244      1.1  christos      ctf_dedup_rwalk_output_mapping_once on all the unique ones: otherwise, just
   2245      1.1  christos      pick a random one and use it.  */
   2246      1.1  christos 
   2247      1.1  christos   if (!ctf_dynset_exists (d->cd_conflicting_types, hval, NULL))
   2248      1.1  christos     {
   2249      1.1  christos       id = ctf_dynset_lookup_any (type_ids);
   2250      1.1  christos       if (!ctf_assert (output, id))
   2251      1.1  christos 	return -1;
   2252      1.1  christos 
   2253      1.1  christos       return ctf_dedup_rwalk_one_output_mapping (output, inputs, ninputs,
   2254      1.1  christos 						 parents, already_visited,
   2255      1.1  christos 						 visited, id, hval, visit_fun,
   2256      1.1  christos 						 arg, depth);
   2257      1.1  christos     }
   2258      1.1  christos 
   2259      1.1  christos   while ((err = ctf_dynset_next (type_ids, &i, &id)) == 0)
   2260      1.1  christos     {
   2261      1.1  christos       int ret;
   2262      1.1  christos 
   2263      1.1  christos       ret = ctf_dedup_rwalk_one_output_mapping (output, inputs, ninputs,
   2264      1.1  christos 						parents, already_visited,
   2265      1.1  christos 						visited, id, hval,
   2266      1.1  christos 						visit_fun, arg, depth);
   2267      1.1  christos       if (ret < 0)
   2268      1.1  christos 	{
   2269      1.1  christos 	  ctf_next_destroy (i);
   2270      1.1  christos 	  return ret;				/* errno is set for us.  */
   2271      1.1  christos 	}
   2272      1.1  christos     }
   2273      1.1  christos   if (err != ECTF_NEXT_END)
   2274      1.1  christos     {
   2275      1.1  christos       ctf_err_warn (output, 0, err, _("cannot walk conflicted type"));
   2276      1.1  christos       return ctf_set_errno (output, err);
   2277      1.1  christos     }
   2278      1.1  christos 
   2279      1.1  christos   return 0;
   2280      1.1  christos }
   2281      1.1  christos 
   2282      1.1  christos typedef struct ctf_sort_om_cb_arg
   2283      1.1  christos {
   2284  1.1.1.2  christos   ctf_dict_t **inputs;
   2285      1.1  christos   uint32_t ninputs;
   2286      1.1  christos   ctf_dedup_t *d;
   2287      1.1  christos } ctf_sort_om_cb_arg_t;
   2288      1.1  christos 
   2289      1.1  christos /* Sort the output mapping into order: types first appearing in earlier inputs
   2290      1.1  christos    first, parents preceding children: if types first appear in the same input,
   2291      1.1  christos    sort those with earlier ctf_id_t's first.  */
   2292      1.1  christos static int
   2293      1.1  christos sort_output_mapping (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
   2294      1.1  christos 		     void *arg_)
   2295      1.1  christos {
   2296      1.1  christos   ctf_sort_om_cb_arg_t *arg = (ctf_sort_om_cb_arg_t *) arg_;
   2297      1.1  christos   ctf_dedup_t *d = arg->d;
   2298      1.1  christos   const char *one_hval = (const char *) one->hkv_key;
   2299      1.1  christos   const char *two_hval = (const char *) two->hkv_key;
   2300      1.1  christos   void *one_gid, *two_gid;
   2301      1.1  christos   uint32_t one_ninput;
   2302      1.1  christos   uint32_t two_ninput;
   2303  1.1.1.2  christos   ctf_dict_t *one_fp;
   2304  1.1.1.2  christos   ctf_dict_t *two_fp;
   2305      1.1  christos   ctf_id_t one_type;
   2306      1.1  christos   ctf_id_t two_type;
   2307      1.1  christos 
   2308  1.1.1.3  christos   /* Inputs are always equal to themselves.  */
   2309  1.1.1.3  christos   if (one == two)
   2310  1.1.1.3  christos     return 0;
   2311  1.1.1.3  christos 
   2312      1.1  christos   one_gid = ctf_dynhash_lookup (d->cd_output_first_gid, one_hval);
   2313      1.1  christos   two_gid = ctf_dynhash_lookup (d->cd_output_first_gid, two_hval);
   2314      1.1  christos 
   2315      1.1  christos   one_ninput = CTF_DEDUP_GID_TO_INPUT (one_gid);
   2316      1.1  christos   two_ninput = CTF_DEDUP_GID_TO_INPUT (two_gid);
   2317      1.1  christos 
   2318      1.1  christos   one_type = CTF_DEDUP_GID_TO_TYPE (one_gid);
   2319      1.1  christos   two_type = CTF_DEDUP_GID_TO_TYPE (two_gid);
   2320      1.1  christos 
   2321      1.1  christos   /* It's kind of hard to smuggle an assertion failure out of here.  */
   2322      1.1  christos   assert (one_ninput < arg->ninputs && two_ninput < arg->ninputs);
   2323      1.1  christos 
   2324      1.1  christos   one_fp = arg->inputs[one_ninput];
   2325      1.1  christos   two_fp = arg->inputs[two_ninput];
   2326      1.1  christos 
   2327      1.1  christos   /* Parents before children.  */
   2328      1.1  christos 
   2329      1.1  christos   if (!(one_fp->ctf_flags & LCTF_CHILD)
   2330      1.1  christos       && (two_fp->ctf_flags & LCTF_CHILD))
   2331      1.1  christos     return -1;
   2332      1.1  christos   else if ((one_fp->ctf_flags & LCTF_CHILD)
   2333      1.1  christos       && !(two_fp->ctf_flags & LCTF_CHILD))
   2334      1.1  christos     return 1;
   2335      1.1  christos 
   2336      1.1  christos   /* ninput order, types appearing in earlier TUs first.  */
   2337      1.1  christos 
   2338      1.1  christos   if (one_ninput < two_ninput)
   2339      1.1  christos     return -1;
   2340      1.1  christos   else if (two_ninput < one_ninput)
   2341      1.1  christos     return 1;
   2342      1.1  christos 
   2343      1.1  christos   /* Same TU.  Earliest ctf_id_t first.  They cannot be the same.  */
   2344      1.1  christos 
   2345      1.1  christos   assert (one_type != two_type);
   2346      1.1  christos   if (one_type < two_type)
   2347      1.1  christos     return -1;
   2348      1.1  christos   else
   2349      1.1  christos     return 1;
   2350      1.1  christos }
   2351      1.1  christos 
   2352      1.1  christos /* The public entry point to ctf_dedup_rwalk_output_mapping, above.  */
   2353      1.1  christos static int
   2354  1.1.1.2  christos ctf_dedup_walk_output_mapping (ctf_dict_t *output, ctf_dict_t **inputs,
   2355      1.1  christos 			       uint32_t ninputs, uint32_t *parents,
   2356      1.1  christos 			       int (*visit_fun) (const char *hval,
   2357  1.1.1.2  christos 						 ctf_dict_t *output,
   2358  1.1.1.2  christos 						 ctf_dict_t **inputs,
   2359      1.1  christos 						 uint32_t ninputs,
   2360      1.1  christos 						 uint32_t *parents,
   2361      1.1  christos 						 int already_visited,
   2362  1.1.1.2  christos 						 ctf_dict_t *input,
   2363      1.1  christos 						 ctf_id_t type,
   2364      1.1  christos 						 void *id,
   2365      1.1  christos 						 int depth,
   2366      1.1  christos 						 void *arg),
   2367      1.1  christos 			       void *arg)
   2368      1.1  christos {
   2369      1.1  christos   ctf_dynset_t *already_visited;
   2370      1.1  christos   ctf_next_t *i = NULL;
   2371      1.1  christos   ctf_sort_om_cb_arg_t sort_arg;
   2372      1.1  christos   int err;
   2373      1.1  christos   void *k;
   2374      1.1  christos 
   2375      1.1  christos   if ((already_visited = ctf_dynset_create (htab_hash_string,
   2376  1.1.1.2  christos 					    htab_eq_string,
   2377      1.1  christos 					    NULL)) == NULL)
   2378      1.1  christos     return ctf_set_errno (output, ENOMEM);
   2379      1.1  christos 
   2380      1.1  christos   sort_arg.inputs = inputs;
   2381      1.1  christos   sort_arg.ninputs = ninputs;
   2382      1.1  christos   sort_arg.d = &output->ctf_dedup;
   2383      1.1  christos 
   2384      1.1  christos   while ((err = ctf_dynhash_next_sorted (output->ctf_dedup.cd_output_mapping,
   2385      1.1  christos 					 &i, &k, NULL, sort_output_mapping,
   2386      1.1  christos 					 &sort_arg)) == 0)
   2387      1.1  christos     {
   2388      1.1  christos       const char *hval = (const char *) k;
   2389      1.1  christos 
   2390      1.1  christos       err = ctf_dedup_rwalk_output_mapping (output, inputs, ninputs, parents,
   2391      1.1  christos 					    already_visited, hval, visit_fun,
   2392      1.1  christos 					    arg, 0);
   2393      1.1  christos       if (err < 0)
   2394      1.1  christos 	{
   2395      1.1  christos 	  ctf_next_destroy (i);
   2396      1.1  christos 	  goto err;				/* errno is set for us.  */
   2397      1.1  christos 	}
   2398      1.1  christos     }
   2399      1.1  christos   if (err != ECTF_NEXT_END)
   2400      1.1  christos     {
   2401      1.1  christos       ctf_set_errno (output, err);
   2402  1.1.1.3  christos       ctf_err_warn (output, 0, 0, _("cannot recurse over output mapping"));
   2403      1.1  christos       goto err;
   2404      1.1  christos     }
   2405      1.1  christos   ctf_dynset_destroy (already_visited);
   2406      1.1  christos 
   2407      1.1  christos   return 0;
   2408      1.1  christos  err:
   2409      1.1  christos   ctf_dynset_destroy (already_visited);
   2410      1.1  christos   return -1;
   2411      1.1  christos }
   2412      1.1  christos 
   2413      1.1  christos /* Possibly synthesise a synthetic forward in TARGET to subsitute for a
   2414      1.1  christos    conflicted per-TU type ID in INPUT with hash HVAL.  Return its CTF ID, or 0
   2415      1.1  christos    if none was needed.  */
   2416      1.1  christos static ctf_id_t
   2417  1.1.1.2  christos ctf_dedup_maybe_synthesize_forward (ctf_dict_t *output, ctf_dict_t *target,
   2418  1.1.1.2  christos 				    ctf_dict_t *input, ctf_id_t id,
   2419      1.1  christos 				    const char *hval)
   2420      1.1  christos {
   2421      1.1  christos   ctf_dedup_t *od = &output->ctf_dedup;
   2422      1.1  christos   ctf_dedup_t *td = &target->ctf_dedup;
   2423      1.1  christos   int kind;
   2424      1.1  christos   int fwdkind;
   2425  1.1.1.2  christos   const char *name = ctf_type_name_raw (input, id);
   2426      1.1  christos   const char *decorated;
   2427      1.1  christos   void *v;
   2428      1.1  christos   ctf_id_t emitted_forward;
   2429      1.1  christos 
   2430      1.1  christos   if (!ctf_dynset_exists (od->cd_conflicting_types, hval, NULL)
   2431      1.1  christos       || target->ctf_flags & LCTF_CHILD
   2432  1.1.1.2  christos       || name[0] == '\0'
   2433      1.1  christos       || (((kind = ctf_type_kind_unsliced (input, id)) != CTF_K_STRUCT
   2434      1.1  christos 	   && kind != CTF_K_UNION && kind != CTF_K_FORWARD)))
   2435      1.1  christos     return 0;
   2436      1.1  christos 
   2437      1.1  christos   fwdkind = ctf_type_kind_forwarded (input, id);
   2438      1.1  christos 
   2439      1.1  christos   ctf_dprintf ("Using synthetic forward for conflicted struct/union with "
   2440      1.1  christos 	       "hval %s\n", hval);
   2441      1.1  christos 
   2442      1.1  christos   if (!ctf_assert (output, name))
   2443      1.1  christos     return CTF_ERR;
   2444      1.1  christos 
   2445      1.1  christos   if ((decorated = ctf_decorate_type_name (output, name, fwdkind)) == NULL)
   2446      1.1  christos     return CTF_ERR;
   2447      1.1  christos 
   2448      1.1  christos   if (!ctf_dynhash_lookup_kv (td->cd_output_emission_conflicted_forwards,
   2449      1.1  christos 			      decorated, NULL, &v))
   2450      1.1  christos     {
   2451      1.1  christos       if ((emitted_forward = ctf_add_forward (target, CTF_ADD_ROOT, name,
   2452      1.1  christos 					      fwdkind)) == CTF_ERR)
   2453  1.1.1.3  christos 	return ctf_set_typed_errno (output, ctf_errno (target));
   2454      1.1  christos 
   2455      1.1  christos       if (ctf_dynhash_cinsert (td->cd_output_emission_conflicted_forwards,
   2456      1.1  christos 			       decorated, (void *) (uintptr_t)
   2457      1.1  christos 			       emitted_forward) < 0)
   2458  1.1.1.3  christos 	return ctf_set_typed_errno (output, ENOMEM);
   2459      1.1  christos     }
   2460      1.1  christos   else
   2461      1.1  christos     emitted_forward = (ctf_id_t) (uintptr_t) v;
   2462      1.1  christos 
   2463      1.1  christos   ctf_dprintf ("Cross-TU conflicted struct: passing back forward, %lx\n",
   2464      1.1  christos 	       emitted_forward);
   2465      1.1  christos 
   2466      1.1  christos   return emitted_forward;
   2467      1.1  christos }
   2468      1.1  christos 
   2469      1.1  christos /* Map a GID in some INPUT dict, in the form of an input number and a ctf_id_t,
   2470      1.1  christos    into a GID in a target output dict.  If it returns 0, this is the
   2471      1.1  christos    unimplemented type, and the input type must have been 0.  The OUTPUT dict is
   2472      1.1  christos    assumed to be the parent of the TARGET, if it is not the TARGET itself.
   2473      1.1  christos 
   2474      1.1  christos    Returns CTF_ERR on failure.  Responds to an incoming CTF_ERR as an 'id' by
   2475      1.1  christos    returning CTF_ERR, to simplify callers.  Errors are always propagated to the
   2476      1.1  christos    input, even if they relate to the target, for the same reason.  (Target
   2477      1.1  christos    errors are expected to be very rare.)
   2478      1.1  christos 
   2479      1.1  christos    If the type in question is a citation of a conflicted type in a different TU,
   2480      1.1  christos    emit a forward of the right type in its place (if not already emitted), and
   2481      1.1  christos    record that forward in cd_output_emission_conflicted_forwards.  This avoids
   2482      1.1  christos    the need to replicate the entire type graph below this point in the current
   2483      1.1  christos    TU (an appalling waste of space).
   2484      1.1  christos 
   2485      1.1  christos    TODO: maybe replace forwards in the same TU with their referents?  Might
   2486      1.1  christos    make usability a bit better.  */
   2487      1.1  christos 
   2488      1.1  christos static ctf_id_t
   2489  1.1.1.2  christos ctf_dedup_id_to_target (ctf_dict_t *output, ctf_dict_t *target,
   2490  1.1.1.2  christos 			ctf_dict_t **inputs, uint32_t ninputs,
   2491  1.1.1.2  christos 			uint32_t *parents, ctf_dict_t *input, int input_num,
   2492      1.1  christos 			ctf_id_t id)
   2493      1.1  christos {
   2494      1.1  christos   ctf_dedup_t *od = &output->ctf_dedup;
   2495      1.1  christos   ctf_dedup_t *td = &target->ctf_dedup;
   2496  1.1.1.2  christos   ctf_dict_t *err_fp = input;
   2497      1.1  christos   const char *hval;
   2498      1.1  christos   void *target_id;
   2499      1.1  christos   ctf_id_t emitted_forward;
   2500      1.1  christos 
   2501      1.1  christos   /* The target type of an error is an error.  */
   2502      1.1  christos   if (id == CTF_ERR)
   2503      1.1  christos     return CTF_ERR;
   2504      1.1  christos 
   2505      1.1  christos   /* The unimplemented type's ID never changes.  */
   2506      1.1  christos   if (!id)
   2507      1.1  christos     {
   2508      1.1  christos       ctf_dprintf ("%i/%lx: unimplemented type\n", input_num, id);
   2509      1.1  christos       return 0;
   2510      1.1  christos     }
   2511      1.1  christos 
   2512      1.1  christos   ctf_dprintf ("Mapping %i/%lx to target %p (%s)\n", input_num,
   2513      1.1  christos 	       id, (void *) target, ctf_link_input_name (target));
   2514      1.1  christos 
   2515      1.1  christos   /* If the input type is in the parent type space, and this is a child, reset
   2516      1.1  christos      the input to the parent (which must already have been emitted, since
   2517      1.1  christos      emission of parent dicts happens before children).  */
   2518      1.1  christos   if ((input->ctf_flags & LCTF_CHILD) && (LCTF_TYPE_ISPARENT (input, id)))
   2519      1.1  christos     {
   2520      1.1  christos       if (!ctf_assert (output, parents[input_num] <= ninputs))
   2521  1.1.1.3  christos 	return CTF_ERR;
   2522      1.1  christos       input = inputs[parents[input_num]];
   2523      1.1  christos       input_num = parents[input_num];
   2524      1.1  christos     }
   2525      1.1  christos 
   2526      1.1  christos   hval = ctf_dynhash_lookup (od->cd_type_hashes,
   2527      1.1  christos 			     CTF_DEDUP_GID (output, input_num, id));
   2528      1.1  christos 
   2529      1.1  christos   if (!ctf_assert (output, hval && td->cd_output_emission_hashes))
   2530  1.1.1.3  christos     return CTF_ERR;
   2531      1.1  christos 
   2532      1.1  christos   /* If this type is a conflicted tagged structure, union, or forward,
   2533      1.1  christos      substitute a synthetic forward instead, emitting it if need be.  Only do
   2534      1.1  christos      this if the target is in the parent dict: if it's in the child dict, we can
   2535      1.1  christos      just point straight at the thing itself.  Of course, we might be looking in
   2536      1.1  christos      the child dict right now and not find it and have to look in the parent, so
   2537      1.1  christos      we have to do this check twice.  */
   2538      1.1  christos 
   2539      1.1  christos   emitted_forward = ctf_dedup_maybe_synthesize_forward (output, target,
   2540      1.1  christos 							input, id, hval);
   2541      1.1  christos   switch (emitted_forward)
   2542      1.1  christos     {
   2543      1.1  christos     case 0: /* No forward needed.  */
   2544      1.1  christos       break;
   2545      1.1  christos     case -1:
   2546      1.1  christos       ctf_set_errno (err_fp, ctf_errno (output));
   2547      1.1  christos       ctf_err_warn (err_fp, 0, 0, _("cannot add synthetic forward for type "
   2548      1.1  christos 				    "%i/%lx"), input_num, id);
   2549  1.1.1.3  christos       return CTF_ERR;
   2550      1.1  christos     default:
   2551      1.1  christos       return emitted_forward;
   2552      1.1  christos     }
   2553      1.1  christos 
   2554      1.1  christos   ctf_dprintf ("Looking up %i/%lx, hash %s, in target\n", input_num, id, hval);
   2555      1.1  christos 
   2556      1.1  christos   target_id = ctf_dynhash_lookup (td->cd_output_emission_hashes, hval);
   2557      1.1  christos   if (!target_id)
   2558      1.1  christos     {
   2559      1.1  christos       /* Must be in the parent, so this must be a child, and they must not be
   2560      1.1  christos 	 the same dict.  */
   2561      1.1  christos       ctf_dprintf ("Checking shared parent for target\n");
   2562      1.1  christos       if (!ctf_assert (output, (target != output)
   2563      1.1  christos 		       && (target->ctf_flags & LCTF_CHILD)))
   2564  1.1.1.3  christos 	return CTF_ERR;
   2565      1.1  christos 
   2566      1.1  christos       target_id = ctf_dynhash_lookup (od->cd_output_emission_hashes, hval);
   2567      1.1  christos 
   2568      1.1  christos       emitted_forward = ctf_dedup_maybe_synthesize_forward (output, output,
   2569      1.1  christos 							    input, id, hval);
   2570      1.1  christos       switch (emitted_forward)
   2571      1.1  christos 	{
   2572      1.1  christos 	case 0: /* No forward needed.  */
   2573      1.1  christos 	  break;
   2574      1.1  christos 	case -1:
   2575      1.1  christos 	  ctf_err_warn (err_fp, 0, ctf_errno (output),
   2576      1.1  christos 			_("cannot add synthetic forward for type %i/%lx"),
   2577      1.1  christos 			input_num, id);
   2578  1.1.1.3  christos 	  return ctf_set_typed_errno (err_fp, ctf_errno (output));
   2579      1.1  christos 	default:
   2580      1.1  christos 	  return emitted_forward;
   2581      1.1  christos 	}
   2582      1.1  christos     }
   2583      1.1  christos   if (!ctf_assert (output, target_id))
   2584  1.1.1.3  christos     return CTF_ERR;
   2585      1.1  christos   return (ctf_id_t) (uintptr_t) target_id;
   2586      1.1  christos }
   2587      1.1  christos 
   2588      1.1  christos /* Emit a single deduplicated TYPE with the given HVAL, located in a given
   2589      1.1  christos    INPUT, with the given (G)ID, into the shared OUTPUT or a
   2590      1.1  christos    possibly-newly-created per-CU dict.  All the types this type depends upon
   2591      1.1  christos    have already been emitted.  (This type itself may also have been emitted.)
   2592      1.1  christos 
   2593      1.1  christos    If the ARG is 1, this is a CU-mapped deduplication round mapping many
   2594  1.1.1.2  christos    ctf_dict_t's into precisely one: conflicting types should be marked
   2595      1.1  christos    non-root-visible.  If the ARG is 0, conflicting types go into per-CU
   2596      1.1  christos    dictionaries stored in the input's ctf_dedup.cd_output: otherwise, everything
   2597      1.1  christos    is emitted directly into the output.  No struct/union members are emitted.
   2598      1.1  christos 
   2599      1.1  christos    Optimization opportunity: trace the ancestry of non-root-visible types and
   2600      1.1  christos    elide all that neither have a root-visible type somewhere towards their root,
   2601      1.1  christos    nor have the type visible via any other route (the function info section,
   2602      1.1  christos    data object section, backtrace section etc).  */
   2603      1.1  christos 
   2604      1.1  christos static int
   2605  1.1.1.2  christos ctf_dedup_emit_type (const char *hval, ctf_dict_t *output, ctf_dict_t **inputs,
   2606      1.1  christos 		     uint32_t ninputs, uint32_t *parents, int already_visited,
   2607  1.1.1.2  christos 		     ctf_dict_t *input, ctf_id_t type, void *id, int depth,
   2608      1.1  christos 		     void *arg)
   2609      1.1  christos {
   2610      1.1  christos   ctf_dedup_t *d = &output->ctf_dedup;
   2611      1.1  christos   int kind = ctf_type_kind_unsliced (input, type);
   2612      1.1  christos   const char *name;
   2613  1.1.1.2  christos   ctf_dict_t *target = output;
   2614  1.1.1.2  christos   ctf_dict_t *real_input;
   2615      1.1  christos   const ctf_type_t *tp;
   2616      1.1  christos   int input_num = CTF_DEDUP_GID_TO_INPUT (id);
   2617      1.1  christos   int output_num = (uint32_t) -1;		/* 'shared' */
   2618      1.1  christos   int cu_mapped = *(int *)arg;
   2619      1.1  christos   int isroot = 1;
   2620      1.1  christos   int is_conflicting;
   2621      1.1  christos 
   2622      1.1  christos   ctf_next_t *i = NULL;
   2623      1.1  christos   ctf_id_t new_type;
   2624      1.1  christos   ctf_id_t ref;
   2625      1.1  christos   ctf_id_t maybe_dup = 0;
   2626      1.1  christos   ctf_encoding_t ep;
   2627      1.1  christos   const char *errtype;
   2628      1.1  christos   int emission_hashed = 0;
   2629      1.1  christos 
   2630      1.1  christos   /* We don't want to re-emit something we've already emitted.  */
   2631      1.1  christos 
   2632      1.1  christos   if (already_visited)
   2633      1.1  christos     return 0;
   2634      1.1  christos 
   2635      1.1  christos   ctf_dprintf ("%i: Emitting type with hash %s from %s: determining target\n",
   2636      1.1  christos 	       depth, hval, ctf_link_input_name (input));
   2637      1.1  christos 
   2638      1.1  christos   /* Conflicting types go into a per-CU output dictionary, unless this is a
   2639      1.1  christos      CU-mapped run.  The import is not refcounted, since it goes into the
   2640      1.1  christos      ctf_link_outputs dict of the output that is its parent.  */
   2641      1.1  christos   is_conflicting = ctf_dynset_exists (d->cd_conflicting_types, hval, NULL);
   2642      1.1  christos 
   2643      1.1  christos   if (is_conflicting && !cu_mapped)
   2644      1.1  christos     {
   2645      1.1  christos       ctf_dprintf ("%i: Type %s in %i/%lx is conflicted: "
   2646      1.1  christos 		   "inserting into per-CU target.\n",
   2647      1.1  christos 		   depth, hval, input_num, type);
   2648      1.1  christos 
   2649      1.1  christos       if (input->ctf_dedup.cd_output)
   2650      1.1  christos 	target = input->ctf_dedup.cd_output;
   2651      1.1  christos       else
   2652      1.1  christos 	{
   2653      1.1  christos 	  int err;
   2654      1.1  christos 
   2655      1.1  christos 	  if ((target = ctf_create (&err)) == NULL)
   2656      1.1  christos 	    {
   2657      1.1  christos 	      ctf_err_warn (output, 0, err,
   2658      1.1  christos 			    _("cannot create per-CU CTF archive for CU %s"),
   2659      1.1  christos 			    ctf_link_input_name (input));
   2660      1.1  christos 	      return ctf_set_errno (output, err);
   2661      1.1  christos 	    }
   2662      1.1  christos 
   2663      1.1  christos 	  ctf_import_unref (target, output);
   2664      1.1  christos 	  if (ctf_cuname (input) != NULL)
   2665      1.1  christos 	    ctf_cuname_set (target, ctf_cuname (input));
   2666      1.1  christos 	  else
   2667      1.1  christos 	    ctf_cuname_set (target, "unnamed-CU");
   2668      1.1  christos 	  ctf_parent_name_set (target, _CTF_SECTION);
   2669      1.1  christos 
   2670      1.1  christos 	  input->ctf_dedup.cd_output = target;
   2671  1.1.1.2  christos 	  input->ctf_link_in_out = target;
   2672  1.1.1.2  christos 	  target->ctf_link_in_out = input;
   2673      1.1  christos 	}
   2674      1.1  christos       output_num = input_num;
   2675      1.1  christos     }
   2676      1.1  christos 
   2677      1.1  christos   real_input = input;
   2678      1.1  christos   if ((tp = ctf_lookup_by_id (&real_input, type)) == NULL)
   2679      1.1  christos     {
   2680      1.1  christos       ctf_err_warn (output, 0, ctf_errno (input),
   2681      1.1  christos 		    _("%s: lookup failure for type %lx"),
   2682      1.1  christos 		    ctf_link_input_name (real_input), type);
   2683      1.1  christos       return ctf_set_errno (output, ctf_errno (input));
   2684      1.1  christos     }
   2685      1.1  christos 
   2686      1.1  christos   name = ctf_strraw (real_input, tp->ctt_name);
   2687      1.1  christos 
   2688      1.1  christos   /* Hide conflicting types, if we were asked to: also hide if a type with this
   2689      1.1  christos      name already exists and is not a forward.  */
   2690      1.1  christos   if (cu_mapped && is_conflicting)
   2691      1.1  christos     isroot = 0;
   2692      1.1  christos   else if (name
   2693      1.1  christos 	   && (maybe_dup = ctf_lookup_by_rawname (target, kind, name)) != 0)
   2694      1.1  christos     {
   2695      1.1  christos       if (ctf_type_kind (target, maybe_dup) != CTF_K_FORWARD)
   2696      1.1  christos 	isroot = 0;
   2697      1.1  christos     }
   2698      1.1  christos 
   2699      1.1  christos   ctf_dprintf ("%i: Emitting type with hash %s (%s), into target %i/%p\n",
   2700      1.1  christos 	       depth, hval, name ? name : "", input_num, (void *) target);
   2701      1.1  christos 
   2702      1.1  christos   if (!target->ctf_dedup.cd_output_emission_hashes)
   2703      1.1  christos     if ((target->ctf_dedup.cd_output_emission_hashes
   2704      1.1  christos 	 = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
   2705      1.1  christos 			      NULL, NULL)) == NULL)
   2706      1.1  christos       goto oom_hash;
   2707      1.1  christos 
   2708      1.1  christos   if (!target->ctf_dedup.cd_output_emission_conflicted_forwards)
   2709      1.1  christos     if ((target->ctf_dedup.cd_output_emission_conflicted_forwards
   2710      1.1  christos 	 = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
   2711      1.1  christos 			      NULL, NULL)) == NULL)
   2712      1.1  christos       goto oom_hash;
   2713      1.1  christos 
   2714      1.1  christos   switch (kind)
   2715      1.1  christos     {
   2716      1.1  christos     case CTF_K_UNKNOWN:
   2717  1.1.1.2  christos       /* These are types that CTF cannot encode, marked as such by the
   2718  1.1.1.2  christos 	 compiler.  */
   2719  1.1.1.2  christos       errtype = _("unknown type");
   2720  1.1.1.2  christos       if ((new_type = ctf_add_unknown (target, isroot, name)) == CTF_ERR)
   2721  1.1.1.2  christos 	goto err_target;
   2722      1.1  christos       break;
   2723      1.1  christos     case CTF_K_FORWARD:
   2724      1.1  christos       /* This will do nothing if the type to which this forwards already exists,
   2725      1.1  christos 	 and will be replaced with such a type if it appears later.  */
   2726      1.1  christos 
   2727      1.1  christos       errtype = _("forward");
   2728      1.1  christos       if ((new_type = ctf_add_forward (target, isroot, name,
   2729      1.1  christos 				       ctf_type_kind_forwarded (input, type)))
   2730      1.1  christos 	  == CTF_ERR)
   2731      1.1  christos 	goto err_target;
   2732      1.1  christos       break;
   2733      1.1  christos 
   2734      1.1  christos     case CTF_K_FLOAT:
   2735      1.1  christos     case CTF_K_INTEGER:
   2736      1.1  christos       errtype = _("float/int");
   2737      1.1  christos       if (ctf_type_encoding (input, type, &ep) < 0)
   2738      1.1  christos 	goto err_input;				/* errno is set for us.  */
   2739      1.1  christos       if ((new_type = ctf_add_encoded (target, isroot, name, &ep, kind))
   2740      1.1  christos 	  == CTF_ERR)
   2741      1.1  christos 	goto err_target;
   2742      1.1  christos       break;
   2743      1.1  christos 
   2744      1.1  christos     case CTF_K_ENUM:
   2745      1.1  christos       {
   2746      1.1  christos 	int val;
   2747      1.1  christos 	errtype = _("enum");
   2748      1.1  christos 	if ((new_type = ctf_add_enum (target, isroot, name)) == CTF_ERR)
   2749      1.1  christos 	  goto err_input;				/* errno is set for us.  */
   2750      1.1  christos 
   2751      1.1  christos 	while ((name = ctf_enum_next (input, type, &i, &val)) != NULL)
   2752      1.1  christos 	  {
   2753      1.1  christos 	    if (ctf_add_enumerator (target, new_type, name, val) < 0)
   2754      1.1  christos 	      {
   2755      1.1  christos 		ctf_err_warn (target, 0, ctf_errno (target),
   2756      1.1  christos 			      _("%s (%i): cannot add enumeration value %s "
   2757      1.1  christos 				"from input type %lx"),
   2758      1.1  christos 			      ctf_link_input_name (input), input_num, name,
   2759      1.1  christos 			      type);
   2760      1.1  christos 		ctf_next_destroy (i);
   2761      1.1  christos 		return ctf_set_errno (output, ctf_errno (target));
   2762      1.1  christos 	      }
   2763      1.1  christos 	  }
   2764      1.1  christos 	if (ctf_errno (input) != ECTF_NEXT_END)
   2765      1.1  christos 	  goto err_input;
   2766      1.1  christos 	break;
   2767      1.1  christos       }
   2768      1.1  christos 
   2769      1.1  christos     case CTF_K_TYPEDEF:
   2770      1.1  christos       errtype = _("typedef");
   2771      1.1  christos 
   2772      1.1  christos       ref = ctf_type_reference (input, type);
   2773      1.1  christos       if ((ref = ctf_dedup_id_to_target (output, target, inputs, ninputs,
   2774      1.1  christos 					 parents, input, input_num,
   2775      1.1  christos 					 ref)) == CTF_ERR)
   2776      1.1  christos 	goto err_input;				/* errno is set for us.  */
   2777      1.1  christos 
   2778      1.1  christos       if ((new_type = ctf_add_typedef (target, isroot, name, ref)) == CTF_ERR)
   2779      1.1  christos 	goto err_target;			/* errno is set for us.  */
   2780      1.1  christos       break;
   2781      1.1  christos 
   2782      1.1  christos     case CTF_K_VOLATILE:
   2783      1.1  christos     case CTF_K_CONST:
   2784      1.1  christos     case CTF_K_RESTRICT:
   2785      1.1  christos     case CTF_K_POINTER:
   2786      1.1  christos       errtype = _("pointer or cvr-qual");
   2787      1.1  christos 
   2788      1.1  christos       ref = ctf_type_reference (input, type);
   2789      1.1  christos       if ((ref = ctf_dedup_id_to_target (output, target, inputs, ninputs,
   2790      1.1  christos 					 parents, input, input_num,
   2791      1.1  christos 					 ref)) == CTF_ERR)
   2792      1.1  christos 	goto err_input;				/* errno is set for us.  */
   2793      1.1  christos 
   2794      1.1  christos       if ((new_type = ctf_add_reftype (target, isroot, ref, kind)) == CTF_ERR)
   2795      1.1  christos 	goto err_target;			/* errno is set for us.  */
   2796      1.1  christos       break;
   2797      1.1  christos 
   2798      1.1  christos     case CTF_K_SLICE:
   2799      1.1  christos       errtype = _("slice");
   2800      1.1  christos 
   2801      1.1  christos       if (ctf_type_encoding (input, type, &ep) < 0)
   2802      1.1  christos 	goto err_input;				/* errno is set for us.  */
   2803      1.1  christos 
   2804      1.1  christos       ref = ctf_type_reference (input, type);
   2805      1.1  christos       if ((ref = ctf_dedup_id_to_target (output, target, inputs, ninputs,
   2806      1.1  christos 					 parents, input, input_num,
   2807      1.1  christos 					 ref)) == CTF_ERR)
   2808      1.1  christos 	goto err_input;
   2809      1.1  christos 
   2810      1.1  christos       if ((new_type = ctf_add_slice (target, isroot, ref, &ep)) == CTF_ERR)
   2811      1.1  christos 	goto err_target;
   2812      1.1  christos       break;
   2813      1.1  christos 
   2814      1.1  christos     case CTF_K_ARRAY:
   2815      1.1  christos       {
   2816      1.1  christos 	ctf_arinfo_t ar;
   2817      1.1  christos 
   2818      1.1  christos 	errtype = _("array info");
   2819      1.1  christos 	if (ctf_array_info (input, type, &ar) < 0)
   2820      1.1  christos 	  goto err_input;
   2821      1.1  christos 
   2822      1.1  christos 	ar.ctr_contents = ctf_dedup_id_to_target (output, target, inputs,
   2823      1.1  christos 						  ninputs, parents, input,
   2824      1.1  christos 						  input_num, ar.ctr_contents);
   2825      1.1  christos 	ar.ctr_index = ctf_dedup_id_to_target (output, target, inputs, ninputs,
   2826      1.1  christos 					       parents, input, input_num,
   2827      1.1  christos 					       ar.ctr_index);
   2828      1.1  christos 
   2829      1.1  christos 	if (ar.ctr_contents == CTF_ERR || ar.ctr_index == CTF_ERR)
   2830      1.1  christos 	  goto err_input;
   2831      1.1  christos 
   2832      1.1  christos 	if ((new_type = ctf_add_array (target, isroot, &ar)) == CTF_ERR)
   2833      1.1  christos 	  goto err_target;
   2834      1.1  christos 
   2835      1.1  christos 	break;
   2836      1.1  christos       }
   2837      1.1  christos 
   2838      1.1  christos     case CTF_K_FUNCTION:
   2839      1.1  christos       {
   2840      1.1  christos 	ctf_funcinfo_t fi;
   2841      1.1  christos 	ctf_id_t *args;
   2842      1.1  christos 	uint32_t j;
   2843      1.1  christos 
   2844      1.1  christos 	errtype = _("function");
   2845      1.1  christos 	if (ctf_func_type_info (input, type, &fi) < 0)
   2846      1.1  christos 	  goto err_input;
   2847      1.1  christos 
   2848      1.1  christos 	fi.ctc_return = ctf_dedup_id_to_target (output, target, inputs, ninputs,
   2849      1.1  christos 						parents, input, input_num,
   2850      1.1  christos 						fi.ctc_return);
   2851      1.1  christos 	if (fi.ctc_return == CTF_ERR)
   2852      1.1  christos 	  goto err_input;
   2853      1.1  christos 
   2854      1.1  christos 	if ((args = calloc (fi.ctc_argc, sizeof (ctf_id_t))) == NULL)
   2855      1.1  christos 	  {
   2856      1.1  christos 	    ctf_set_errno (input, ENOMEM);
   2857      1.1  christos 	    goto err_input;
   2858      1.1  christos 	  }
   2859      1.1  christos 
   2860      1.1  christos 	errtype = _("function args");
   2861      1.1  christos 	if (ctf_func_type_args (input, type, fi.ctc_argc, args) < 0)
   2862      1.1  christos 	  {
   2863      1.1  christos 	    free (args);
   2864      1.1  christos 	    goto err_input;
   2865      1.1  christos 	  }
   2866      1.1  christos 
   2867      1.1  christos 	for (j = 0; j < fi.ctc_argc; j++)
   2868      1.1  christos 	  {
   2869      1.1  christos 	    args[j] = ctf_dedup_id_to_target (output, target, inputs, ninputs,
   2870      1.1  christos 					      parents, input, input_num,
   2871      1.1  christos 					      args[j]);
   2872      1.1  christos 	    if (args[j] == CTF_ERR)
   2873      1.1  christos 	      goto err_input;
   2874      1.1  christos 	  }
   2875      1.1  christos 
   2876      1.1  christos 	if ((new_type = ctf_add_function (target, isroot,
   2877      1.1  christos 					  &fi, args)) == CTF_ERR)
   2878      1.1  christos 	  {
   2879      1.1  christos 	    free (args);
   2880      1.1  christos 	    goto err_target;
   2881      1.1  christos 	  }
   2882      1.1  christos 	free (args);
   2883      1.1  christos 	break;
   2884      1.1  christos       }
   2885      1.1  christos 
   2886      1.1  christos     case CTF_K_STRUCT:
   2887      1.1  christos     case CTF_K_UNION:
   2888      1.1  christos       {
   2889      1.1  christos 	size_t size = ctf_type_size (input, type);
   2890      1.1  christos 	void *out_id;
   2891      1.1  christos 	/* Insert the structure itself, so other types can refer to it.  */
   2892      1.1  christos 
   2893      1.1  christos 	errtype = _("structure/union");
   2894      1.1  christos 	if (kind == CTF_K_STRUCT)
   2895      1.1  christos 	  new_type = ctf_add_struct_sized (target, isroot, name, size);
   2896      1.1  christos 	else
   2897      1.1  christos 	  new_type = ctf_add_union_sized (target, isroot, name, size);
   2898      1.1  christos 
   2899      1.1  christos 	if (new_type == CTF_ERR)
   2900      1.1  christos 	  goto err_target;
   2901      1.1  christos 
   2902      1.1  christos 	out_id = CTF_DEDUP_GID (output, output_num, new_type);
   2903      1.1  christos 	ctf_dprintf ("%i: Noting need to emit members of %p -> %p\n", depth,
   2904      1.1  christos 		     id, out_id);
   2905      1.1  christos 	/* Record the need to emit the members of this structure later.  */
   2906      1.1  christos 	if (ctf_dynhash_insert (d->cd_emission_struct_members, id, out_id) < 0)
   2907  1.1.1.2  christos 	  {
   2908  1.1.1.2  christos 	    ctf_set_errno (target, errno);
   2909  1.1.1.2  christos 	    goto err_target;
   2910  1.1.1.2  christos 	  }
   2911      1.1  christos 	break;
   2912      1.1  christos       }
   2913      1.1  christos     default:
   2914      1.1  christos       ctf_err_warn (output, 0, ECTF_CORRUPT, _("%s: unknown type kind for "
   2915      1.1  christos 					       "input type %lx"),
   2916      1.1  christos 		    ctf_link_input_name (input), type);
   2917      1.1  christos       return ctf_set_errno (output, ECTF_CORRUPT);
   2918      1.1  christos     }
   2919      1.1  christos 
   2920      1.1  christos   if (!emission_hashed
   2921      1.1  christos       && new_type != 0
   2922      1.1  christos       && ctf_dynhash_cinsert (target->ctf_dedup.cd_output_emission_hashes,
   2923      1.1  christos 			      hval, (void *) (uintptr_t) new_type) < 0)
   2924      1.1  christos     {
   2925      1.1  christos       ctf_err_warn (output, 0, ENOMEM, _("out of memory tracking deduplicated "
   2926      1.1  christos 					 "global type IDs"));
   2927      1.1  christos 	return ctf_set_errno (output, ENOMEM);
   2928      1.1  christos     }
   2929      1.1  christos 
   2930      1.1  christos   if (!emission_hashed && new_type != 0)
   2931      1.1  christos     ctf_dprintf ("%i: Inserted %s, %i/%lx -> %lx into emission hash for "
   2932      1.1  christos 		 "target %p (%s)\n", depth, hval, input_num, type, new_type,
   2933      1.1  christos 		 (void *) target, ctf_link_input_name (target));
   2934      1.1  christos 
   2935      1.1  christos   return 0;
   2936      1.1  christos 
   2937      1.1  christos  oom_hash:
   2938      1.1  christos   ctf_err_warn (output, 0, ENOMEM, _("out of memory creating emission-tracking "
   2939      1.1  christos 				     "hashes"));
   2940      1.1  christos   return ctf_set_errno (output, ENOMEM);
   2941      1.1  christos 
   2942      1.1  christos  err_input:
   2943      1.1  christos   ctf_err_warn (output, 0, ctf_errno (input),
   2944      1.1  christos 		_("%s (%i): while emitting deduplicated %s, error getting "
   2945      1.1  christos 		  "input type %lx"), ctf_link_input_name (input),
   2946      1.1  christos 		input_num, errtype, type);
   2947      1.1  christos   return ctf_set_errno (output, ctf_errno (input));
   2948      1.1  christos  err_target:
   2949      1.1  christos   ctf_err_warn (output, 0, ctf_errno (target),
   2950      1.1  christos 		_("%s (%i): while emitting deduplicated %s, error emitting "
   2951      1.1  christos 		  "target type from input type %lx"),
   2952      1.1  christos 		ctf_link_input_name (input), input_num,
   2953      1.1  christos 		errtype, type);
   2954      1.1  christos   return ctf_set_errno (output, ctf_errno (target));
   2955      1.1  christos }
   2956      1.1  christos 
   2957      1.1  christos /* Traverse the cd_emission_struct_members and emit the members of all
   2958      1.1  christos    structures and unions.  All other types are emitted and complete by this
   2959      1.1  christos    point.  */
   2960      1.1  christos 
   2961      1.1  christos static int
   2962  1.1.1.2  christos ctf_dedup_emit_struct_members (ctf_dict_t *output, ctf_dict_t **inputs,
   2963      1.1  christos 			       uint32_t ninputs, uint32_t *parents)
   2964      1.1  christos {
   2965      1.1  christos   ctf_dedup_t *d = &output->ctf_dedup;
   2966      1.1  christos   ctf_next_t *i = NULL;
   2967      1.1  christos   void *input_id, *target_id;
   2968      1.1  christos   int err;
   2969  1.1.1.2  christos   ctf_dict_t *err_fp, *input_fp;
   2970      1.1  christos   int input_num;
   2971      1.1  christos   ctf_id_t err_type;
   2972      1.1  christos 
   2973      1.1  christos   while ((err = ctf_dynhash_next (d->cd_emission_struct_members, &i,
   2974      1.1  christos 				  &input_id, &target_id)) == 0)
   2975      1.1  christos     {
   2976      1.1  christos       ctf_next_t *j = NULL;
   2977  1.1.1.2  christos       ctf_dict_t *target;
   2978      1.1  christos       uint32_t target_num;
   2979      1.1  christos       ctf_id_t input_type, target_type;
   2980      1.1  christos       ssize_t offset;
   2981      1.1  christos       ctf_id_t membtype;
   2982      1.1  christos       const char *name;
   2983      1.1  christos 
   2984      1.1  christos       input_num = CTF_DEDUP_GID_TO_INPUT (input_id);
   2985      1.1  christos       input_fp = inputs[input_num];
   2986      1.1  christos       input_type = CTF_DEDUP_GID_TO_TYPE (input_id);
   2987      1.1  christos 
   2988      1.1  christos       /* The output is either -1 (for the shared, parent output dict) or the
   2989      1.1  christos 	 number of the corresponding input.  */
   2990      1.1  christos       target_num = CTF_DEDUP_GID_TO_INPUT (target_id);
   2991      1.1  christos       if (target_num == (uint32_t) -1)
   2992      1.1  christos 	target = output;
   2993      1.1  christos       else
   2994      1.1  christos 	{
   2995      1.1  christos 	  target = inputs[target_num]->ctf_dedup.cd_output;
   2996      1.1  christos 	  if (!ctf_assert (output, target))
   2997      1.1  christos 	    {
   2998      1.1  christos 	      err_fp = output;
   2999      1.1  christos 	      err_type = input_type;
   3000      1.1  christos 	      goto err_target;
   3001      1.1  christos 	    }
   3002      1.1  christos 	}
   3003      1.1  christos       target_type = CTF_DEDUP_GID_TO_TYPE (target_id);
   3004      1.1  christos 
   3005      1.1  christos       while ((offset = ctf_member_next (input_fp, input_type, &j, &name,
   3006  1.1.1.2  christos 					&membtype, 0)) >= 0)
   3007      1.1  christos 	{
   3008      1.1  christos 	  err_fp = target;
   3009      1.1  christos 	  err_type = target_type;
   3010      1.1  christos 	  if ((membtype = ctf_dedup_id_to_target (output, target, inputs,
   3011      1.1  christos 						  ninputs, parents, input_fp,
   3012      1.1  christos 						  input_num,
   3013      1.1  christos 						  membtype)) == CTF_ERR)
   3014      1.1  christos 	    {
   3015      1.1  christos 	      ctf_next_destroy (j);
   3016      1.1  christos 	      goto err_target;
   3017      1.1  christos 	    }
   3018      1.1  christos 
   3019      1.1  christos 	  if (name == NULL)
   3020      1.1  christos 	    name = "";
   3021      1.1  christos #ifdef ENABLE_LIBCTF_HASH_DEBUGGING
   3022      1.1  christos 	  ctf_dprintf ("Emitting %s, offset %zi\n", name, offset);
   3023      1.1  christos #endif
   3024      1.1  christos 	  if (ctf_add_member_offset (target, target_type, name,
   3025      1.1  christos 				     membtype, offset) < 0)
   3026      1.1  christos 	    {
   3027      1.1  christos 	      ctf_next_destroy (j);
   3028      1.1  christos 	      goto err_target;
   3029      1.1  christos 	    }
   3030      1.1  christos 	}
   3031      1.1  christos       if (ctf_errno (input_fp) != ECTF_NEXT_END)
   3032      1.1  christos 	{
   3033      1.1  christos 	  err = ctf_errno (input_fp);
   3034      1.1  christos 	  ctf_next_destroy (i);
   3035      1.1  christos 	  goto iterr;
   3036      1.1  christos 	}
   3037      1.1  christos     }
   3038      1.1  christos   if (err != ECTF_NEXT_END)
   3039      1.1  christos     goto iterr;
   3040      1.1  christos 
   3041      1.1  christos   return 0;
   3042      1.1  christos  err_target:
   3043      1.1  christos   ctf_next_destroy (i);
   3044      1.1  christos   ctf_err_warn (output, 0, ctf_errno (err_fp),
   3045      1.1  christos 		_("%s (%i): error emitting members for structure type %lx"),
   3046      1.1  christos 		ctf_link_input_name (input_fp), input_num, err_type);
   3047      1.1  christos   return ctf_set_errno (output, ctf_errno (err_fp));
   3048      1.1  christos  iterr:
   3049      1.1  christos   ctf_err_warn (output, 0, err, _("iteration failure emitting "
   3050      1.1  christos 				  "structure members"));
   3051      1.1  christos   return ctf_set_errno (output, err);
   3052      1.1  christos }
   3053      1.1  christos 
   3054      1.1  christos /* Emit deduplicated types into the outputs.  The shared type repository is
   3055      1.1  christos    OUTPUT, on which the ctf_dedup function must have already been called.  The
   3056      1.1  christos    PARENTS array contains the INPUTS index of the parent dict for every child
   3057      1.1  christos    dict at the corresponding index in the INPUTS (for non-child dicts, the value
   3058      1.1  christos    is undefined).
   3059      1.1  christos 
   3060      1.1  christos    Return an array of fps with content emitted into them (starting with OUTPUT,
   3061      1.1  christos    which is the parent of all others, then all the newly-generated outputs).
   3062      1.1  christos 
   3063      1.1  christos    If CU_MAPPED is set, this is a first pass for a link with a non-empty CU
   3064      1.1  christos    mapping: only one output will result.  */
   3065      1.1  christos 
   3066  1.1.1.2  christos ctf_dict_t **
   3067  1.1.1.2  christos ctf_dedup_emit (ctf_dict_t *output, ctf_dict_t **inputs, uint32_t ninputs,
   3068      1.1  christos 		uint32_t *parents, uint32_t *noutputs, int cu_mapped)
   3069      1.1  christos {
   3070      1.1  christos   size_t num_outputs = 1;		/* Always at least one output: us.  */
   3071  1.1.1.2  christos   ctf_dict_t **outputs;
   3072  1.1.1.2  christos   ctf_dict_t **walk;
   3073      1.1  christos   size_t i;
   3074      1.1  christos 
   3075      1.1  christos   ctf_dprintf ("Triggering emission.\n");
   3076      1.1  christos   if (ctf_dedup_walk_output_mapping (output, inputs, ninputs, parents,
   3077      1.1  christos 				     ctf_dedup_emit_type, &cu_mapped) < 0)
   3078      1.1  christos     return NULL;				/* errno is set for us.  */
   3079      1.1  christos 
   3080      1.1  christos   ctf_dprintf ("Populating struct members.\n");
   3081      1.1  christos   if (ctf_dedup_emit_struct_members (output, inputs, ninputs, parents) < 0)
   3082      1.1  christos     return NULL;				/* errno is set for us.  */
   3083      1.1  christos 
   3084      1.1  christos   for (i = 0; i < ninputs; i++)
   3085      1.1  christos     {
   3086      1.1  christos       if (inputs[i]->ctf_dedup.cd_output)
   3087      1.1  christos 	num_outputs++;
   3088      1.1  christos     }
   3089      1.1  christos 
   3090      1.1  christos   if (!ctf_assert (output, !cu_mapped || (cu_mapped && num_outputs == 1)))
   3091      1.1  christos     return NULL;
   3092      1.1  christos 
   3093  1.1.1.2  christos   if ((outputs = calloc (num_outputs, sizeof (ctf_dict_t *))) == NULL)
   3094      1.1  christos     {
   3095      1.1  christos       ctf_set_errno (output, ENOMEM);
   3096  1.1.1.3  christos       ctf_err_warn (output, 0, 0,
   3097  1.1.1.3  christos 		    _("out of memory allocating link outputs array"));
   3098      1.1  christos       return NULL;
   3099      1.1  christos     }
   3100      1.1  christos   *noutputs = num_outputs;
   3101      1.1  christos 
   3102      1.1  christos   walk = outputs;
   3103      1.1  christos   *walk = output;
   3104      1.1  christos   output->ctf_refcnt++;
   3105      1.1  christos   walk++;
   3106      1.1  christos 
   3107      1.1  christos   for (i = 0; i < ninputs; i++)
   3108      1.1  christos     {
   3109      1.1  christos       if (inputs[i]->ctf_dedup.cd_output)
   3110      1.1  christos 	{
   3111      1.1  christos 	  *walk = inputs[i]->ctf_dedup.cd_output;
   3112      1.1  christos 	  inputs[i]->ctf_dedup.cd_output = NULL;
   3113      1.1  christos 	  walk++;
   3114      1.1  christos 	}
   3115      1.1  christos     }
   3116      1.1  christos 
   3117      1.1  christos   return outputs;
   3118      1.1  christos }
   3119  1.1.1.2  christos 
   3120  1.1.1.2  christos /* Determine what type SRC_FP / SRC_TYPE was emitted as in the FP, which
   3121  1.1.1.2  christos    must be the shared dict or have it as a parent: return 0 if none.  The SRC_FP
   3122  1.1.1.2  christos    must be a past input to ctf_dedup.  */
   3123  1.1.1.2  christos 
   3124  1.1.1.2  christos ctf_id_t
   3125  1.1.1.2  christos ctf_dedup_type_mapping (ctf_dict_t *fp, ctf_dict_t *src_fp, ctf_id_t src_type)
   3126  1.1.1.2  christos {
   3127  1.1.1.2  christos   ctf_dict_t *output = NULL;
   3128  1.1.1.2  christos   ctf_dedup_t *d;
   3129  1.1.1.2  christos   int input_num;
   3130  1.1.1.2  christos   void *num_ptr;
   3131  1.1.1.2  christos   void *type_ptr;
   3132  1.1.1.2  christos   int found;
   3133  1.1.1.2  christos   const char *hval;
   3134  1.1.1.2  christos 
   3135  1.1.1.2  christos   /* It is an error (an internal error in the caller, in ctf-link.c) to call
   3136  1.1.1.2  christos      this with an FP that is not a per-CU output or shared output dict, or with
   3137  1.1.1.2  christos      a SRC_FP that was not passed to ctf_dedup as an input; it is an internal
   3138  1.1.1.2  christos      error in ctf-dedup for the type passed not to have been hashed, though if
   3139  1.1.1.2  christos      the src_fp is a child dict and the type is not a child type, it will have
   3140  1.1.1.2  christos      been hashed under the GID corresponding to the parent.  */
   3141  1.1.1.2  christos 
   3142  1.1.1.2  christos   if (fp->ctf_dedup.cd_type_hashes != NULL)
   3143  1.1.1.2  christos     output = fp;
   3144  1.1.1.2  christos   else if (fp->ctf_parent && fp->ctf_parent->ctf_dedup.cd_type_hashes != NULL)
   3145  1.1.1.2  christos     output = fp->ctf_parent;
   3146  1.1.1.2  christos   else
   3147  1.1.1.2  christos     {
   3148  1.1.1.2  christos       ctf_set_errno (fp, ECTF_INTERNAL);
   3149  1.1.1.3  christos       ctf_err_warn (fp, 0, 0,
   3150  1.1.1.2  christos 		    _("dict %p passed to ctf_dedup_type_mapping is not a "
   3151  1.1.1.2  christos 		      "deduplicated output"), (void *) fp);
   3152  1.1.1.2  christos       return CTF_ERR;
   3153  1.1.1.2  christos     }
   3154  1.1.1.2  christos 
   3155  1.1.1.2  christos   if (src_fp->ctf_parent && ctf_type_isparent (src_fp, src_type))
   3156  1.1.1.2  christos     src_fp = src_fp->ctf_parent;
   3157  1.1.1.2  christos 
   3158  1.1.1.2  christos   d = &output->ctf_dedup;
   3159  1.1.1.2  christos 
   3160  1.1.1.2  christos   found = ctf_dynhash_lookup_kv (d->cd_input_nums, src_fp, NULL, &num_ptr);
   3161  1.1.1.2  christos   if (!ctf_assert (output, found != 0))
   3162  1.1.1.2  christos     return CTF_ERR;				/* errno is set for us.  */
   3163  1.1.1.2  christos   input_num = (uintptr_t) num_ptr;
   3164  1.1.1.2  christos 
   3165  1.1.1.2  christos   hval = ctf_dynhash_lookup (d->cd_type_hashes,
   3166  1.1.1.2  christos 			     CTF_DEDUP_GID (output, input_num, src_type));
   3167  1.1.1.2  christos 
   3168  1.1.1.2  christos   if (!ctf_assert (output, hval != NULL))
   3169  1.1.1.2  christos     return CTF_ERR;				/* errno is set for us.  */
   3170  1.1.1.2  christos 
   3171  1.1.1.2  christos   /* The emission hashes may be unset if this dict was created after
   3172  1.1.1.2  christos      deduplication to house variables or other things that would conflict if
   3173  1.1.1.2  christos      stored in the shared dict.  */
   3174  1.1.1.2  christos   if (fp->ctf_dedup.cd_output_emission_hashes)
   3175  1.1.1.2  christos     if (ctf_dynhash_lookup_kv (fp->ctf_dedup.cd_output_emission_hashes, hval,
   3176  1.1.1.2  christos 			       NULL, &type_ptr))
   3177  1.1.1.2  christos       return (ctf_id_t) (uintptr_t) type_ptr;
   3178  1.1.1.2  christos 
   3179  1.1.1.2  christos   if (fp->ctf_parent)
   3180  1.1.1.2  christos     {
   3181  1.1.1.2  christos       ctf_dict_t *pfp = fp->ctf_parent;
   3182  1.1.1.2  christos       if (pfp->ctf_dedup.cd_output_emission_hashes)
   3183  1.1.1.2  christos 	if (ctf_dynhash_lookup_kv (pfp->ctf_dedup.cd_output_emission_hashes,
   3184  1.1.1.2  christos 				   hval, NULL, &type_ptr))
   3185  1.1.1.2  christos 	  return (ctf_id_t) (uintptr_t) type_ptr;
   3186  1.1.1.2  christos     }
   3187  1.1.1.2  christos 
   3188  1.1.1.2  christos   return 0;
   3189  1.1.1.2  christos }
   3190