merge.c revision 178546
1178481Sjb/* 2178481Sjb * CDDL HEADER START 3178481Sjb * 4178481Sjb * The contents of this file are subject to the terms of the 5178481Sjb * Common Development and Distribution License (the "License"). 6178481Sjb * You may not use this file except in compliance with the License. 7178481Sjb * 8178481Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9178481Sjb * or http://www.opensolaris.org/os/licensing. 10178481Sjb * See the License for the specific language governing permissions 11178481Sjb * and limitations under the License. 12178481Sjb * 13178481Sjb * When distributing Covered Code, include this CDDL HEADER in each 14178481Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15178481Sjb * If applicable, add the following below this CDDL HEADER, with the 16178481Sjb * fields enclosed by brackets "[]" replaced with your own identifying 17178481Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 18178481Sjb * 19178481Sjb * CDDL HEADER END 20178481Sjb */ 21178481Sjb/* 22178481Sjb * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23178481Sjb * Use is subject to license terms. 24178481Sjb */ 25178481Sjb 26178481Sjb#pragma ident "%Z%%M% %I% %E% SMI" 27178481Sjb 28178481Sjb/* 29178481Sjb * This file contains routines that merge one tdata_t tree, called the child, 30178481Sjb * into another, called the parent. Note that these names are used mainly for 31178481Sjb * convenience and to represent the direction of the merge. They are not meant 32178481Sjb * to imply any relationship between the tdata_t graphs prior to the merge. 33178481Sjb * 34178481Sjb * tdata_t structures contain two main elements - a hash of iidesc_t nodes, and 35178481Sjb * a directed graph of tdesc_t nodes, pointed to by the iidesc_t nodes. Simply 36178481Sjb * put, we merge the tdesc_t graphs, followed by the iidesc_t nodes, and then we 37178481Sjb * clean up loose ends. 38178481Sjb * 39178481Sjb * The algorithm is as follows: 40178481Sjb * 41178481Sjb * 1. Mapping iidesc_t nodes 42178481Sjb * 43178481Sjb * For each child iidesc_t node, we first try to map its tdesc_t subgraph 44178481Sjb * against the tdesc_t graph in the parent. For each node in the child subgraph 45178481Sjb * that exists in the parent, a mapping between the two (between their type IDs) 46178481Sjb * is established. For the child nodes that cannot be mapped onto existing 47178481Sjb * parent nodes, a mapping is established between the child node ID and a 48178481Sjb * newly-allocated ID that the node will use when it is re-created in the 49178481Sjb * parent. These unmappable nodes are added to the md_tdtba (tdesc_t To Be 50178481Sjb * Added) hash, which tracks nodes that need to be created in the parent. 51178481Sjb * 52178481Sjb * If all of the nodes in the subgraph for an iidesc_t in the child can be 53178481Sjb * mapped to existing nodes in the parent, then we can try to map the child 54178481Sjb * iidesc_t onto an iidesc_t in the parent. If we cannot find an equivalent 55178481Sjb * iidesc_t, or if we were not able to completely map the tdesc_t subgraph(s), 56178481Sjb * then we add this iidesc_t to the md_iitba (iidesc_t To Be Added) list. This 57178481Sjb * list tracks iidesc_t nodes that are to be created in the parent. 58178481Sjb * 59178481Sjb * While visiting the tdesc_t nodes, we may discover a forward declaration (a 60178481Sjb * FORWARD tdesc_t) in the parent that is resolved in the child. That is, there 61178481Sjb * may be a structure or union definition in the child with the same name as the 62178481Sjb * forward declaration in the parent. If we find such a node, we record an 63178481Sjb * association in the md_fdida (Forward => Definition ID Association) list 64178481Sjb * between the parent ID of the forward declaration and the ID that the 65178481Sjb * definition will use when re-created in the parent. 66178481Sjb * 67178481Sjb * 2. Creating new tdesc_t nodes (the md_tdtba hash) 68178481Sjb * 69178481Sjb * We have now attempted to map all tdesc_t nodes from the child into the 70178481Sjb * parent, and have, in md_tdtba, a hash of all tdesc_t nodes that need to be 71178481Sjb * created (or, as we so wittily call it, conjured) in the parent. We iterate 72178481Sjb * through this hash, creating the indicated tdesc_t nodes. For a given tdesc_t 73178481Sjb * node, conjuring requires two steps - the copying of the common tdesc_t data 74178481Sjb * (name, type, etc) from the child node, and the creation of links from the 75178481Sjb * newly-created node to the parent equivalents of other tdesc_t nodes pointed 76178481Sjb * to by node being conjured. Note that in some cases, the targets of these 77178481Sjb * links will be on the md_tdtba hash themselves, and may not have been created 78178481Sjb * yet. As such, we can't establish the links from these new nodes into the 79178481Sjb * parent graph. We therefore conjure them with links to nodes in the *child* 80178481Sjb * graph, and add pointers to the links to be created to the md_tdtbr (tdesc_t 81178481Sjb * To Be Remapped) hash. For example, a POINTER tdesc_t that could not be 82178481Sjb * resolved would have its &tdesc_t->t_tdesc added to md_tdtbr. 83178481Sjb * 84178481Sjb * 3. Creating new iidesc_t nodes (the md_iitba list) 85178481Sjb * 86178481Sjb * When we have completed step 2, all tdesc_t nodes have been created (or 87178481Sjb * already existed) in the parent. Some of them may have incorrect links (the 88178481Sjb * members of the md_tdtbr list), but they've all been created. As such, we can 89178481Sjb * create all of the iidesc_t nodes, as we can attach the tdesc_t subgraph 90178481Sjb * pointers correctly. We create each node, and attach the pointers to the 91178481Sjb * appropriate parts of the parent tdesc_t graph. 92178481Sjb * 93178481Sjb * 4. Resolving newly-created tdesc_t node links (the md_tdtbr list) 94178481Sjb * 95178481Sjb * As in step 3, we rely on the fact that all of the tdesc_t nodes have been 96178481Sjb * created. Each entry in the md_tdtbr list is a pointer to where a link into 97178481Sjb * the parent will be established. As saved in the md_tdtbr list, these 98178481Sjb * pointers point into the child tdesc_t subgraph. We can thus get the target 99178481Sjb * type ID from the child, look at the ID mapping to determine the desired link 100178481Sjb * target, and redirect the link accordingly. 101178481Sjb * 102178481Sjb * 5. Parent => child forward declaration resolution 103178481Sjb * 104178481Sjb * If entries were made in the md_fdida list in step 1, we have forward 105178481Sjb * declarations in the parent that need to be resolved to their definitions 106178481Sjb * re-created in step 2 from the child. Using the md_fdida list, we can locate 107178481Sjb * the definition for the forward declaration, and we can redirect all inbound 108178481Sjb * edges to the forward declaration node to the actual definition. 109178481Sjb * 110178481Sjb * A pox on the house of anyone who changes the algorithm without updating 111178481Sjb * this comment. 112178481Sjb */ 113178481Sjb 114178481Sjb#include <stdio.h> 115178481Sjb#include <strings.h> 116178481Sjb#include <assert.h> 117178481Sjb#include <pthread.h> 118178481Sjb 119178481Sjb#include "ctf_headers.h" 120178481Sjb#include "ctftools.h" 121178481Sjb#include "list.h" 122178481Sjb#include "alist.h" 123178481Sjb#include "memory.h" 124178481Sjb#include "traverse.h" 125178481Sjb 126178481Sjbtypedef struct equiv_data equiv_data_t; 127178481Sjbtypedef struct merge_cb_data merge_cb_data_t; 128178481Sjb 129178481Sjb/* 130178481Sjb * There are two traversals in this file, for equivalency and for tdesc_t 131178481Sjb * re-creation, that do not fit into the tdtraverse() framework. We have our 132178481Sjb * own traversal mechanism and ops vector here for those two cases. 133178481Sjb */ 134178481Sjbtypedef struct tdesc_ops { 135178546Sjb const char *name; 136178481Sjb int (*equiv)(tdesc_t *, tdesc_t *, equiv_data_t *); 137178481Sjb tdesc_t *(*conjure)(tdesc_t *, int, merge_cb_data_t *); 138178481Sjb} tdesc_ops_t; 139178481Sjbextern tdesc_ops_t tdesc_ops[]; 140178481Sjb 141178481Sjb/* 142178481Sjb * The workhorse structure of tdata_t merging. Holds all lists of nodes to be 143178481Sjb * processed during various phases of the merge algorithm. 144178481Sjb */ 145178481Sjbstruct merge_cb_data { 146178481Sjb tdata_t *md_parent; 147178481Sjb tdata_t *md_tgt; 148178481Sjb alist_t *md_ta; /* Type Association */ 149178481Sjb alist_t *md_fdida; /* Forward -> Definition ID Association */ 150178481Sjb list_t **md_iitba; /* iidesc_t nodes To Be Added to the parent */ 151178481Sjb hash_t *md_tdtba; /* tdesc_t nodes To Be Added to the parent */ 152178481Sjb list_t **md_tdtbr; /* tdesc_t nodes To Be Remapped */ 153178481Sjb int md_flags; 154178481Sjb}; /* merge_cb_data_t */ 155178481Sjb 156178481Sjb/* 157178481Sjb * When we first create a tdata_t from stabs data, we will have duplicate nodes. 158178481Sjb * Normal merges, however, assume that the child tdata_t is already self-unique, 159178481Sjb * and for speed reasons do not attempt to self-uniquify. If this flag is set, 160178481Sjb * the merge algorithm will self-uniquify by avoiding the insertion of 161178481Sjb * duplicates in the md_tdtdba list. 162178481Sjb */ 163178481Sjb#define MCD_F_SELFUNIQUIFY 0x1 164178481Sjb 165178481Sjb/* 166178481Sjb * When we merge the CTF data for the modules, we don't want it to contain any 167178481Sjb * data that can be found in the reference module (usually genunix). If this 168178481Sjb * flag is set, we're doing a merge between the fully merged tdata_t for this 169178481Sjb * module and the tdata_t for the reference module, with the data unique to this 170178481Sjb * module ending up in a third tdata_t. It is this third tdata_t that will end 171178481Sjb * up in the .SUNW_ctf section for the module. 172178481Sjb */ 173178481Sjb#define MCD_F_REFMERGE 0x2 174178481Sjb 175178481Sjb/* 176178481Sjb * Mapping of child type IDs to parent type IDs 177178481Sjb */ 178178481Sjb 179178481Sjbstatic void 180178481Sjbadd_mapping(alist_t *ta, tid_t srcid, tid_t tgtid) 181178481Sjb{ 182178546Sjb debug(3, "Adding mapping %u <%x> => %u <%x>\n", srcid, srcid, tgtid, tgtid); 183178481Sjb 184178546Sjb assert(!alist_find(ta, (void *)(uintptr_t)srcid, NULL)); 185178481Sjb assert(srcid != 0 && tgtid != 0); 186178481Sjb 187178546Sjb alist_add(ta, (void *)(uintptr_t)srcid, (void *)(uintptr_t)tgtid); 188178481Sjb} 189178481Sjb 190178481Sjbstatic tid_t 191178481Sjbget_mapping(alist_t *ta, int srcid) 192178481Sjb{ 193178546Sjb void *ltgtid; 194178481Sjb 195178546Sjb if (alist_find(ta, (void *)(uintptr_t)srcid, (void **)<gtid)) 196178546Sjb return ((uintptr_t)ltgtid); 197178481Sjb else 198178481Sjb return (0); 199178481Sjb} 200178481Sjb 201178481Sjb/* 202178481Sjb * Determining equivalence of tdesc_t subgraphs 203178481Sjb */ 204178481Sjb 205178481Sjbstruct equiv_data { 206178481Sjb alist_t *ed_ta; 207178481Sjb tdesc_t *ed_node; 208178481Sjb tdesc_t *ed_tgt; 209178481Sjb 210178481Sjb int ed_clear_mark; 211178481Sjb int ed_cur_mark; 212178481Sjb int ed_selfuniquify; 213178481Sjb}; /* equiv_data_t */ 214178481Sjb 215178481Sjbstatic int equiv_node(tdesc_t *, tdesc_t *, equiv_data_t *); 216178481Sjb 217178481Sjb/*ARGSUSED2*/ 218178481Sjbstatic int 219178546Sjbequiv_intrinsic(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed __unused) 220178481Sjb{ 221178481Sjb intr_t *si = stdp->t_intr; 222178481Sjb intr_t *ti = ttdp->t_intr; 223178481Sjb 224178481Sjb if (si->intr_type != ti->intr_type || 225178481Sjb si->intr_signed != ti->intr_signed || 226178481Sjb si->intr_offset != ti->intr_offset || 227178481Sjb si->intr_nbits != ti->intr_nbits) 228178481Sjb return (0); 229178481Sjb 230178481Sjb if (si->intr_type == INTR_INT && 231178481Sjb si->intr_iformat != ti->intr_iformat) 232178481Sjb return (0); 233178481Sjb else if (si->intr_type == INTR_REAL && 234178481Sjb si->intr_fformat != ti->intr_fformat) 235178481Sjb return (0); 236178481Sjb 237178481Sjb return (1); 238178481Sjb} 239178481Sjb 240178481Sjbstatic int 241178481Sjbequiv_plain(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed) 242178481Sjb{ 243178481Sjb return (equiv_node(stdp->t_tdesc, ttdp->t_tdesc, ed)); 244178481Sjb} 245178481Sjb 246178481Sjbstatic int 247178481Sjbequiv_function(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed) 248178481Sjb{ 249178481Sjb fndef_t *fn1 = stdp->t_fndef, *fn2 = ttdp->t_fndef; 250178481Sjb int i; 251178481Sjb 252178481Sjb if (fn1->fn_nargs != fn2->fn_nargs || 253178481Sjb fn1->fn_vargs != fn2->fn_vargs) 254178481Sjb return (0); 255178481Sjb 256178481Sjb if (!equiv_node(fn1->fn_ret, fn2->fn_ret, ed)) 257178481Sjb return (0); 258178481Sjb 259178546Sjb for (i = 0; i < (int) fn1->fn_nargs; i++) { 260178481Sjb if (!equiv_node(fn1->fn_args[i], fn2->fn_args[i], ed)) 261178481Sjb return (0); 262178481Sjb } 263178481Sjb 264178481Sjb return (1); 265178481Sjb} 266178481Sjb 267178481Sjbstatic int 268178481Sjbequiv_array(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed) 269178481Sjb{ 270178481Sjb ardef_t *ar1 = stdp->t_ardef, *ar2 = ttdp->t_ardef; 271178481Sjb 272178481Sjb if (!equiv_node(ar1->ad_contents, ar2->ad_contents, ed) || 273178481Sjb !equiv_node(ar1->ad_idxtype, ar2->ad_idxtype, ed)) 274178481Sjb return (0); 275178481Sjb 276178481Sjb if (ar1->ad_nelems != ar2->ad_nelems) 277178481Sjb return (0); 278178481Sjb 279178481Sjb return (1); 280178481Sjb} 281178481Sjb 282178481Sjbstatic int 283178481Sjbequiv_su(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed) 284178481Sjb{ 285178481Sjb mlist_t *ml1 = stdp->t_members, *ml2 = ttdp->t_members; 286178481Sjb mlist_t *olm1 = NULL; 287178481Sjb 288178481Sjb while (ml1 && ml2) { 289178481Sjb if (ml1->ml_offset != ml2->ml_offset || 290178481Sjb strcmp(ml1->ml_name, ml2->ml_name) != 0) 291178481Sjb return (0); 292178481Sjb 293178481Sjb /* 294178481Sjb * Don't do the recursive equivalency checking more than 295178481Sjb * we have to. 296178481Sjb */ 297178481Sjb if (olm1 == NULL || olm1->ml_type->t_id != ml1->ml_type->t_id) { 298178481Sjb if (ml1->ml_size != ml2->ml_size || 299178481Sjb !equiv_node(ml1->ml_type, ml2->ml_type, ed)) 300178481Sjb return (0); 301178481Sjb } 302178481Sjb 303178481Sjb olm1 = ml1; 304178481Sjb ml1 = ml1->ml_next; 305178481Sjb ml2 = ml2->ml_next; 306178481Sjb } 307178481Sjb 308178481Sjb if (ml1 || ml2) 309178481Sjb return (0); 310178481Sjb 311178481Sjb return (1); 312178481Sjb} 313178481Sjb 314178481Sjb/*ARGSUSED2*/ 315178481Sjbstatic int 316178546Sjbequiv_enum(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed __unused) 317178481Sjb{ 318178481Sjb elist_t *el1 = stdp->t_emem; 319178481Sjb elist_t *el2 = ttdp->t_emem; 320178481Sjb 321178481Sjb while (el1 && el2) { 322178481Sjb if (el1->el_number != el2->el_number || 323178481Sjb strcmp(el1->el_name, el2->el_name) != 0) 324178481Sjb return (0); 325178481Sjb 326178481Sjb el1 = el1->el_next; 327178481Sjb el2 = el2->el_next; 328178481Sjb } 329178481Sjb 330178481Sjb if (el1 || el2) 331178481Sjb return (0); 332178481Sjb 333178481Sjb return (1); 334178481Sjb} 335178481Sjb 336178481Sjb/*ARGSUSED*/ 337178481Sjbstatic int 338178546Sjbequiv_assert(tdesc_t *stdp __unused, tdesc_t *ttdp __unused, equiv_data_t *ed __unused) 339178481Sjb{ 340178481Sjb /* foul, evil, and very bad - this is a "shouldn't happen" */ 341178481Sjb assert(1 == 0); 342178481Sjb 343178481Sjb return (0); 344178481Sjb} 345178481Sjb 346178481Sjbstatic int 347178481Sjbfwd_equiv(tdesc_t *ctdp, tdesc_t *mtdp) 348178481Sjb{ 349178481Sjb tdesc_t *defn = (ctdp->t_type == FORWARD ? mtdp : ctdp); 350178481Sjb 351178481Sjb return (defn->t_type == STRUCT || defn->t_type == UNION); 352178481Sjb} 353178481Sjb 354178481Sjbstatic int 355178481Sjbequiv_node(tdesc_t *ctdp, tdesc_t *mtdp, equiv_data_t *ed) 356178481Sjb{ 357178546Sjb int (*equiv)(tdesc_t *, tdesc_t *, equiv_data_t *); 358178481Sjb int mapping; 359178481Sjb 360178481Sjb if (ctdp->t_emark > ed->ed_clear_mark || 361178481Sjb mtdp->t_emark > ed->ed_clear_mark) 362178481Sjb return (ctdp->t_emark == mtdp->t_emark); 363178481Sjb 364178481Sjb /* 365178481Sjb * In normal (non-self-uniquify) mode, we don't want to do equivalency 366178481Sjb * checking on a subgraph that has already been checked. If a mapping 367178481Sjb * has already been established for a given child node, we can simply 368178481Sjb * compare the mapping for the child node with the ID of the parent 369178481Sjb * node. If we are in self-uniquify mode, then we're comparing two 370178481Sjb * subgraphs within the child graph, and thus need to ignore any 371178481Sjb * type mappings that have been created, as they are only valid into the 372178481Sjb * parent. 373178481Sjb */ 374178481Sjb if ((mapping = get_mapping(ed->ed_ta, ctdp->t_id)) > 0 && 375178481Sjb mapping == mtdp->t_id && !ed->ed_selfuniquify) 376178481Sjb return (1); 377178481Sjb 378178481Sjb if (!streq(ctdp->t_name, mtdp->t_name)) 379178481Sjb return (0); 380178481Sjb 381178481Sjb if (ctdp->t_type != mtdp->t_type) { 382178481Sjb if (ctdp->t_type == FORWARD || mtdp->t_type == FORWARD) 383178481Sjb return (fwd_equiv(ctdp, mtdp)); 384178481Sjb else 385178481Sjb return (0); 386178481Sjb } 387178481Sjb 388178481Sjb ctdp->t_emark = ed->ed_cur_mark; 389178481Sjb mtdp->t_emark = ed->ed_cur_mark; 390178481Sjb ed->ed_cur_mark++; 391178481Sjb 392178481Sjb if ((equiv = tdesc_ops[ctdp->t_type].equiv) != NULL) 393178481Sjb return (equiv(ctdp, mtdp, ed)); 394178481Sjb 395178481Sjb return (1); 396178481Sjb} 397178481Sjb 398178481Sjb/* 399178481Sjb * We perform an equivalency check on two subgraphs by traversing through them 400178481Sjb * in lockstep. If a given node is equivalent in both the parent and the child, 401178481Sjb * we mark it in both subgraphs, using the t_emark field, with a monotonically 402178481Sjb * increasing number. If, in the course of the traversal, we reach a node that 403178481Sjb * we have visited and numbered during this equivalency check, we have a cycle. 404178481Sjb * If the previously-visited nodes don't have the same emark, then the edges 405178481Sjb * that brought us to these nodes are not equivalent, and so the check ends. 406178481Sjb * If the emarks are the same, the edges are equivalent. We then backtrack and 407178481Sjb * continue the traversal. If we have exhausted all edges in the subgraph, and 408178481Sjb * have not found any inequivalent nodes, then the subgraphs are equivalent. 409178481Sjb */ 410178481Sjbstatic int 411178481Sjbequiv_cb(void *bucket, void *arg) 412178481Sjb{ 413178481Sjb equiv_data_t *ed = arg; 414178481Sjb tdesc_t *mtdp = bucket; 415178481Sjb tdesc_t *ctdp = ed->ed_node; 416178481Sjb 417178481Sjb ed->ed_clear_mark = ed->ed_cur_mark + 1; 418178481Sjb ed->ed_cur_mark = ed->ed_clear_mark + 1; 419178481Sjb 420178481Sjb if (equiv_node(ctdp, mtdp, ed)) { 421178546Sjb debug(3, "equiv_node matched %d <%x> %d <%x>\n", 422178546Sjb ctdp->t_id, ctdp->t_id, mtdp->t_id, mtdp->t_id); 423178481Sjb ed->ed_tgt = mtdp; 424178481Sjb /* matched. stop looking */ 425178481Sjb return (-1); 426178481Sjb } 427178481Sjb 428178481Sjb return (0); 429178481Sjb} 430178481Sjb 431178481Sjb/*ARGSUSED1*/ 432178481Sjbstatic int 433178546Sjbmap_td_tree_pre(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private) 434178481Sjb{ 435178481Sjb merge_cb_data_t *mcd = private; 436178481Sjb 437178481Sjb if (get_mapping(mcd->md_ta, ctdp->t_id) > 0) 438178481Sjb return (0); 439178481Sjb 440178481Sjb return (1); 441178481Sjb} 442178481Sjb 443178481Sjb/*ARGSUSED1*/ 444178481Sjbstatic int 445178546Sjbmap_td_tree_post(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private) 446178481Sjb{ 447178481Sjb merge_cb_data_t *mcd = private; 448178481Sjb equiv_data_t ed; 449178481Sjb 450178481Sjb ed.ed_ta = mcd->md_ta; 451178481Sjb ed.ed_clear_mark = mcd->md_parent->td_curemark; 452178481Sjb ed.ed_cur_mark = mcd->md_parent->td_curemark + 1; 453178481Sjb ed.ed_node = ctdp; 454178481Sjb ed.ed_selfuniquify = 0; 455178481Sjb 456178546Sjb debug(3, "map_td_tree_post on %d <%x> %s\n", ctdp->t_id, ctdp->t_id,tdesc_name(ctdp)); 457178481Sjb 458178481Sjb if (hash_find_iter(mcd->md_parent->td_layouthash, ctdp, 459178481Sjb equiv_cb, &ed) < 0) { 460178481Sjb /* We found an equivalent node */ 461178481Sjb if (ed.ed_tgt->t_type == FORWARD && ctdp->t_type != FORWARD) { 462178481Sjb int id = mcd->md_tgt->td_nextid++; 463178481Sjb 464178546Sjb debug(3, "Creating new defn type %d <%x>\n", id, id); 465178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, id); 466178481Sjb alist_add(mcd->md_fdida, (void *)(ulong_t)ed.ed_tgt, 467178481Sjb (void *)(ulong_t)id); 468178481Sjb hash_add(mcd->md_tdtba, ctdp); 469178481Sjb } else 470178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, ed.ed_tgt->t_id); 471178481Sjb 472178481Sjb } else if (debug_level > 1 && hash_iter(mcd->md_parent->td_idhash, 473178481Sjb equiv_cb, &ed) < 0) { 474178481Sjb /* 475178481Sjb * We didn't find an equivalent node by looking through the 476178481Sjb * layout hash, but we somehow found it by performing an 477178481Sjb * exhaustive search through the entire graph. This usually 478178481Sjb * means that the "name" hash function is broken. 479178481Sjb */ 480178481Sjb aborterr("Second pass for %d (%s) == %d\n", ctdp->t_id, 481178481Sjb tdesc_name(ctdp), ed.ed_tgt->t_id); 482178481Sjb } else { 483178481Sjb int id = mcd->md_tgt->td_nextid++; 484178481Sjb 485178546Sjb debug(3, "Creating new type %d <%x>\n", id, id); 486178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, id); 487178481Sjb hash_add(mcd->md_tdtba, ctdp); 488178481Sjb } 489178481Sjb 490178481Sjb mcd->md_parent->td_curemark = ed.ed_cur_mark + 1; 491178481Sjb 492178481Sjb return (1); 493178481Sjb} 494178481Sjb 495178481Sjb/*ARGSUSED1*/ 496178481Sjbstatic int 497178546Sjbmap_td_tree_self_post(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private) 498178481Sjb{ 499178481Sjb merge_cb_data_t *mcd = private; 500178481Sjb equiv_data_t ed; 501178481Sjb 502178481Sjb ed.ed_ta = mcd->md_ta; 503178481Sjb ed.ed_clear_mark = mcd->md_parent->td_curemark; 504178481Sjb ed.ed_cur_mark = mcd->md_parent->td_curemark + 1; 505178481Sjb ed.ed_node = ctdp; 506178481Sjb ed.ed_selfuniquify = 1; 507178481Sjb ed.ed_tgt = NULL; 508178481Sjb 509178481Sjb if (hash_find_iter(mcd->md_tdtba, ctdp, equiv_cb, &ed) < 0) { 510178546Sjb debug(3, "Self check found %d <%x> in %d <%x>\n", ctdp->t_id, 511178546Sjb ctdp->t_id, ed.ed_tgt->t_id, ed.ed_tgt->t_id); 512178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, 513178481Sjb get_mapping(mcd->md_ta, ed.ed_tgt->t_id)); 514178481Sjb } else if (debug_level > 1 && hash_iter(mcd->md_tdtba, 515178481Sjb equiv_cb, &ed) < 0) { 516178481Sjb /* 517178481Sjb * We didn't find an equivalent node using the quick way (going 518178481Sjb * through the hash normally), but we did find it by iterating 519178481Sjb * through the entire hash. This usually means that the hash 520178481Sjb * function is broken. 521178481Sjb */ 522178546Sjb aborterr("Self-unique second pass for %d <%x> (%s) == %d <%x>\n", 523178546Sjb ctdp->t_id, ctdp->t_id, tdesc_name(ctdp), ed.ed_tgt->t_id, 524178546Sjb ed.ed_tgt->t_id); 525178481Sjb } else { 526178481Sjb int id = mcd->md_tgt->td_nextid++; 527178481Sjb 528178546Sjb debug(3, "Creating new type %d <%x>\n", id, id); 529178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, id); 530178481Sjb hash_add(mcd->md_tdtba, ctdp); 531178481Sjb } 532178481Sjb 533178481Sjb mcd->md_parent->td_curemark = ed.ed_cur_mark + 1; 534178481Sjb 535178481Sjb return (1); 536178481Sjb} 537178481Sjb 538178481Sjbstatic tdtrav_cb_f map_pre[] = { 539178481Sjb NULL, 540178481Sjb map_td_tree_pre, /* intrinsic */ 541178481Sjb map_td_tree_pre, /* pointer */ 542178481Sjb map_td_tree_pre, /* array */ 543178481Sjb map_td_tree_pre, /* function */ 544178481Sjb map_td_tree_pre, /* struct */ 545178481Sjb map_td_tree_pre, /* union */ 546178481Sjb map_td_tree_pre, /* enum */ 547178481Sjb map_td_tree_pre, /* forward */ 548178481Sjb map_td_tree_pre, /* typedef */ 549178481Sjb tdtrav_assert, /* typedef_unres */ 550178481Sjb map_td_tree_pre, /* volatile */ 551178481Sjb map_td_tree_pre, /* const */ 552178481Sjb map_td_tree_pre /* restrict */ 553178481Sjb}; 554178481Sjb 555178481Sjbstatic tdtrav_cb_f map_post[] = { 556178481Sjb NULL, 557178481Sjb map_td_tree_post, /* intrinsic */ 558178481Sjb map_td_tree_post, /* pointer */ 559178481Sjb map_td_tree_post, /* array */ 560178481Sjb map_td_tree_post, /* function */ 561178481Sjb map_td_tree_post, /* struct */ 562178481Sjb map_td_tree_post, /* union */ 563178481Sjb map_td_tree_post, /* enum */ 564178481Sjb map_td_tree_post, /* forward */ 565178481Sjb map_td_tree_post, /* typedef */ 566178481Sjb tdtrav_assert, /* typedef_unres */ 567178481Sjb map_td_tree_post, /* volatile */ 568178481Sjb map_td_tree_post, /* const */ 569178481Sjb map_td_tree_post /* restrict */ 570178481Sjb}; 571178481Sjb 572178481Sjbstatic tdtrav_cb_f map_self_post[] = { 573178481Sjb NULL, 574178481Sjb map_td_tree_self_post, /* intrinsic */ 575178481Sjb map_td_tree_self_post, /* pointer */ 576178481Sjb map_td_tree_self_post, /* array */ 577178481Sjb map_td_tree_self_post, /* function */ 578178481Sjb map_td_tree_self_post, /* struct */ 579178481Sjb map_td_tree_self_post, /* union */ 580178481Sjb map_td_tree_self_post, /* enum */ 581178481Sjb map_td_tree_self_post, /* forward */ 582178481Sjb map_td_tree_self_post, /* typedef */ 583178481Sjb tdtrav_assert, /* typedef_unres */ 584178481Sjb map_td_tree_self_post, /* volatile */ 585178481Sjb map_td_tree_self_post, /* const */ 586178481Sjb map_td_tree_self_post /* restrict */ 587178481Sjb}; 588178481Sjb 589178481Sjb/* 590178481Sjb * Determining equivalence of iidesc_t nodes 591178481Sjb */ 592178481Sjb 593178481Sjbtypedef struct iifind_data { 594178481Sjb iidesc_t *iif_template; 595178481Sjb alist_t *iif_ta; 596178481Sjb int iif_newidx; 597178481Sjb int iif_refmerge; 598178481Sjb} iifind_data_t; 599178481Sjb 600178481Sjb/* 601178481Sjb * Check to see if this iidesc_t (node) - the current one on the list we're 602178481Sjb * iterating through - matches the target one (iif->iif_template). Return -1 603178481Sjb * if it matches, to stop the iteration. 604178481Sjb */ 605178481Sjbstatic int 606178481Sjbiidesc_match(void *data, void *arg) 607178481Sjb{ 608178481Sjb iidesc_t *node = data; 609178481Sjb iifind_data_t *iif = arg; 610178481Sjb int i; 611178481Sjb 612178481Sjb if (node->ii_type != iif->iif_template->ii_type || 613178481Sjb !streq(node->ii_name, iif->iif_template->ii_name) || 614178481Sjb node->ii_dtype->t_id != iif->iif_newidx) 615178481Sjb return (0); 616178481Sjb 617178481Sjb if ((node->ii_type == II_SVAR || node->ii_type == II_SFUN) && 618178481Sjb !streq(node->ii_owner, iif->iif_template->ii_owner)) 619178481Sjb return (0); 620178481Sjb 621178481Sjb if (node->ii_nargs != iif->iif_template->ii_nargs) 622178481Sjb return (0); 623178481Sjb 624178481Sjb for (i = 0; i < node->ii_nargs; i++) { 625178481Sjb if (get_mapping(iif->iif_ta, 626178481Sjb iif->iif_template->ii_args[i]->t_id) != 627178481Sjb node->ii_args[i]->t_id) 628178481Sjb return (0); 629178481Sjb } 630178481Sjb 631178481Sjb if (iif->iif_refmerge) { 632178481Sjb switch (iif->iif_template->ii_type) { 633178481Sjb case II_GFUN: 634178481Sjb case II_SFUN: 635178481Sjb case II_GVAR: 636178481Sjb case II_SVAR: 637178481Sjb debug(3, "suppressing duping of %d %s from %s\n", 638178481Sjb iif->iif_template->ii_type, 639178481Sjb iif->iif_template->ii_name, 640178481Sjb (iif->iif_template->ii_owner ? 641178481Sjb iif->iif_template->ii_owner : "NULL")); 642178481Sjb return (0); 643178481Sjb case II_NOT: 644178481Sjb case II_PSYM: 645178481Sjb case II_SOU: 646178481Sjb case II_TYPE: 647178481Sjb break; 648178481Sjb } 649178481Sjb } 650178481Sjb 651178481Sjb return (-1); 652178481Sjb} 653178481Sjb 654178481Sjbstatic int 655178481Sjbmerge_type_cb(void *data, void *arg) 656178481Sjb{ 657178481Sjb iidesc_t *sii = data; 658178481Sjb merge_cb_data_t *mcd = arg; 659178481Sjb iifind_data_t iif; 660178481Sjb tdtrav_cb_f *post; 661178481Sjb 662178481Sjb post = (mcd->md_flags & MCD_F_SELFUNIQUIFY ? map_self_post : map_post); 663178481Sjb 664178481Sjb /* Map the tdesc nodes */ 665178481Sjb (void) iitraverse(sii, &mcd->md_parent->td_curvgen, NULL, map_pre, post, 666178481Sjb mcd); 667178481Sjb 668178481Sjb /* Map the iidesc nodes */ 669178481Sjb iif.iif_template = sii; 670178481Sjb iif.iif_ta = mcd->md_ta; 671178481Sjb iif.iif_newidx = get_mapping(mcd->md_ta, sii->ii_dtype->t_id); 672178481Sjb iif.iif_refmerge = (mcd->md_flags & MCD_F_REFMERGE); 673178481Sjb 674178481Sjb if (hash_match(mcd->md_parent->td_iihash, sii, iidesc_match, 675178481Sjb &iif) == 1) 676178481Sjb /* successfully mapped */ 677178481Sjb return (1); 678178481Sjb 679178481Sjb debug(3, "tba %s (%d)\n", (sii->ii_name ? sii->ii_name : "(anon)"), 680178481Sjb sii->ii_type); 681178481Sjb 682178481Sjb list_add(mcd->md_iitba, sii); 683178481Sjb 684178481Sjb return (0); 685178481Sjb} 686178481Sjb 687178481Sjbstatic int 688178481Sjbremap_node(tdesc_t **tgtp, tdesc_t *oldtgt, int selftid, tdesc_t *newself, 689178481Sjb merge_cb_data_t *mcd) 690178481Sjb{ 691178481Sjb tdesc_t *tgt = NULL; 692178481Sjb tdesc_t template; 693178481Sjb int oldid = oldtgt->t_id; 694178481Sjb 695178481Sjb if (oldid == selftid) { 696178481Sjb *tgtp = newself; 697178481Sjb return (1); 698178481Sjb } 699178481Sjb 700178481Sjb if ((template.t_id = get_mapping(mcd->md_ta, oldid)) == 0) 701178546Sjb aborterr("failed to get mapping for tid %d <%x>\n", oldid, oldid); 702178481Sjb 703178481Sjb if (!hash_find(mcd->md_parent->td_idhash, (void *)&template, 704178481Sjb (void *)&tgt) && (!(mcd->md_flags & MCD_F_REFMERGE) || 705178481Sjb !hash_find(mcd->md_tgt->td_idhash, (void *)&template, 706178481Sjb (void *)&tgt))) { 707178546Sjb debug(3, "Remap couldn't find %d <%x> (from %d <%x>)\n", template.t_id, 708178546Sjb template.t_id, oldid, oldid); 709178481Sjb *tgtp = oldtgt; 710178481Sjb list_add(mcd->md_tdtbr, tgtp); 711178481Sjb return (0); 712178481Sjb } 713178481Sjb 714178481Sjb *tgtp = tgt; 715178481Sjb return (1); 716178481Sjb} 717178481Sjb 718178481Sjbstatic tdesc_t * 719178481Sjbconjure_template(tdesc_t *old, int newselfid) 720178481Sjb{ 721178481Sjb tdesc_t *new = xcalloc(sizeof (tdesc_t)); 722178481Sjb 723178481Sjb new->t_name = old->t_name ? xstrdup(old->t_name) : NULL; 724178481Sjb new->t_type = old->t_type; 725178481Sjb new->t_size = old->t_size; 726178481Sjb new->t_id = newselfid; 727178481Sjb new->t_flags = old->t_flags; 728178481Sjb 729178481Sjb return (new); 730178481Sjb} 731178481Sjb 732178481Sjb/*ARGSUSED2*/ 733178481Sjbstatic tdesc_t * 734178546Sjbconjure_intrinsic(tdesc_t *old, int newselfid, merge_cb_data_t *mcd __unused) 735178481Sjb{ 736178481Sjb tdesc_t *new = conjure_template(old, newselfid); 737178481Sjb 738178481Sjb new->t_intr = xmalloc(sizeof (intr_t)); 739178481Sjb bcopy(old->t_intr, new->t_intr, sizeof (intr_t)); 740178481Sjb 741178481Sjb return (new); 742178481Sjb} 743178481Sjb 744178481Sjbstatic tdesc_t * 745178481Sjbconjure_plain(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 746178481Sjb{ 747178481Sjb tdesc_t *new = conjure_template(old, newselfid); 748178481Sjb 749178481Sjb (void) remap_node(&new->t_tdesc, old->t_tdesc, old->t_id, new, mcd); 750178481Sjb 751178481Sjb return (new); 752178481Sjb} 753178481Sjb 754178481Sjbstatic tdesc_t * 755178481Sjbconjure_function(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 756178481Sjb{ 757178481Sjb tdesc_t *new = conjure_template(old, newselfid); 758178481Sjb fndef_t *nfn = xmalloc(sizeof (fndef_t)); 759178481Sjb fndef_t *ofn = old->t_fndef; 760178481Sjb int i; 761178481Sjb 762178481Sjb (void) remap_node(&nfn->fn_ret, ofn->fn_ret, old->t_id, new, mcd); 763178481Sjb 764178481Sjb nfn->fn_nargs = ofn->fn_nargs; 765178481Sjb nfn->fn_vargs = ofn->fn_vargs; 766178481Sjb 767178481Sjb if (nfn->fn_nargs > 0) 768178481Sjb nfn->fn_args = xcalloc(sizeof (tdesc_t *) * ofn->fn_nargs); 769178481Sjb 770178546Sjb for (i = 0; i < (int) ofn->fn_nargs; i++) { 771178481Sjb (void) remap_node(&nfn->fn_args[i], ofn->fn_args[i], old->t_id, 772178481Sjb new, mcd); 773178481Sjb } 774178481Sjb 775178481Sjb new->t_fndef = nfn; 776178481Sjb 777178481Sjb return (new); 778178481Sjb} 779178481Sjb 780178481Sjbstatic tdesc_t * 781178481Sjbconjure_array(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 782178481Sjb{ 783178481Sjb tdesc_t *new = conjure_template(old, newselfid); 784178481Sjb ardef_t *nar = xmalloc(sizeof (ardef_t)); 785178481Sjb ardef_t *oar = old->t_ardef; 786178481Sjb 787178481Sjb (void) remap_node(&nar->ad_contents, oar->ad_contents, old->t_id, new, 788178481Sjb mcd); 789178481Sjb (void) remap_node(&nar->ad_idxtype, oar->ad_idxtype, old->t_id, new, 790178481Sjb mcd); 791178481Sjb 792178481Sjb nar->ad_nelems = oar->ad_nelems; 793178481Sjb 794178481Sjb new->t_ardef = nar; 795178481Sjb 796178481Sjb return (new); 797178481Sjb} 798178481Sjb 799178481Sjbstatic tdesc_t * 800178481Sjbconjure_su(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 801178481Sjb{ 802178481Sjb tdesc_t *new = conjure_template(old, newselfid); 803178481Sjb mlist_t *omem, **nmemp; 804178481Sjb 805178481Sjb for (omem = old->t_members, nmemp = &new->t_members; 806178481Sjb omem; omem = omem->ml_next, nmemp = &((*nmemp)->ml_next)) { 807178481Sjb *nmemp = xmalloc(sizeof (mlist_t)); 808178481Sjb (*nmemp)->ml_offset = omem->ml_offset; 809178481Sjb (*nmemp)->ml_size = omem->ml_size; 810178546Sjb (*nmemp)->ml_name = xstrdup(omem->ml_name ? omem->ml_name : "empty omem->ml_name"); 811178481Sjb (void) remap_node(&((*nmemp)->ml_type), omem->ml_type, 812178481Sjb old->t_id, new, mcd); 813178481Sjb } 814178481Sjb *nmemp = NULL; 815178481Sjb 816178481Sjb return (new); 817178481Sjb} 818178481Sjb 819178481Sjb/*ARGSUSED2*/ 820178481Sjbstatic tdesc_t * 821178546Sjbconjure_enum(tdesc_t *old, int newselfid, merge_cb_data_t *mcd __unused) 822178481Sjb{ 823178481Sjb tdesc_t *new = conjure_template(old, newselfid); 824178481Sjb elist_t *oel, **nelp; 825178481Sjb 826178481Sjb for (oel = old->t_emem, nelp = &new->t_emem; 827178481Sjb oel; oel = oel->el_next, nelp = &((*nelp)->el_next)) { 828178481Sjb *nelp = xmalloc(sizeof (elist_t)); 829178481Sjb (*nelp)->el_name = xstrdup(oel->el_name); 830178481Sjb (*nelp)->el_number = oel->el_number; 831178481Sjb } 832178481Sjb *nelp = NULL; 833178481Sjb 834178481Sjb return (new); 835178481Sjb} 836178481Sjb 837178481Sjb/*ARGSUSED2*/ 838178481Sjbstatic tdesc_t * 839178481Sjbconjure_forward(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 840178481Sjb{ 841178481Sjb tdesc_t *new = conjure_template(old, newselfid); 842178481Sjb 843178481Sjb list_add(&mcd->md_tgt->td_fwdlist, new); 844178481Sjb 845178481Sjb return (new); 846178481Sjb} 847178481Sjb 848178481Sjb/*ARGSUSED*/ 849178481Sjbstatic tdesc_t * 850178546Sjbconjure_assert(tdesc_t *old __unused, int newselfid __unused, merge_cb_data_t *mcd __unused) 851178481Sjb{ 852178481Sjb assert(1 == 0); 853178481Sjb return (NULL); 854178481Sjb} 855178481Sjb 856178481Sjbstatic iidesc_t * 857178481Sjbconjure_iidesc(iidesc_t *old, merge_cb_data_t *mcd) 858178481Sjb{ 859178481Sjb iidesc_t *new = iidesc_dup(old); 860178481Sjb int i; 861178481Sjb 862178481Sjb (void) remap_node(&new->ii_dtype, old->ii_dtype, -1, NULL, mcd); 863178481Sjb for (i = 0; i < new->ii_nargs; i++) { 864178481Sjb (void) remap_node(&new->ii_args[i], old->ii_args[i], -1, NULL, 865178481Sjb mcd); 866178481Sjb } 867178481Sjb 868178481Sjb return (new); 869178481Sjb} 870178481Sjb 871178481Sjbstatic int 872178481Sjbfwd_redir(tdesc_t *fwd, tdesc_t **fwdp, void *private) 873178481Sjb{ 874178481Sjb alist_t *map = private; 875178546Sjb void *defn; 876178481Sjb 877178481Sjb if (!alist_find(map, (void *)fwd, (void **)&defn)) 878178481Sjb return (0); 879178481Sjb 880178481Sjb debug(3, "Redirecting an edge to %s\n", tdesc_name(defn)); 881178481Sjb 882178481Sjb *fwdp = defn; 883178481Sjb 884178481Sjb return (1); 885178481Sjb} 886178481Sjb 887178481Sjbstatic tdtrav_cb_f fwd_redir_cbs[] = { 888178481Sjb NULL, 889178481Sjb NULL, /* intrinsic */ 890178481Sjb NULL, /* pointer */ 891178481Sjb NULL, /* array */ 892178481Sjb NULL, /* function */ 893178481Sjb NULL, /* struct */ 894178481Sjb NULL, /* union */ 895178481Sjb NULL, /* enum */ 896178481Sjb fwd_redir, /* forward */ 897178481Sjb NULL, /* typedef */ 898178481Sjb tdtrav_assert, /* typedef_unres */ 899178481Sjb NULL, /* volatile */ 900178481Sjb NULL, /* const */ 901178481Sjb NULL /* restrict */ 902178481Sjb}; 903178481Sjb 904178481Sjbtypedef struct redir_mstr_data { 905178481Sjb tdata_t *rmd_tgt; 906178481Sjb alist_t *rmd_map; 907178481Sjb} redir_mstr_data_t; 908178481Sjb 909178481Sjbstatic int 910178481Sjbredir_mstr_fwd_cb(void *name, void *value, void *arg) 911178481Sjb{ 912178481Sjb tdesc_t *fwd = name; 913178546Sjb int defnid = (uintptr_t)value; 914178481Sjb redir_mstr_data_t *rmd = arg; 915178481Sjb tdesc_t template; 916178481Sjb tdesc_t *defn; 917178481Sjb 918178481Sjb template.t_id = defnid; 919178481Sjb 920178481Sjb if (!hash_find(rmd->rmd_tgt->td_idhash, (void *)&template, 921178481Sjb (void *)&defn)) { 922178481Sjb aborterr("Couldn't unforward %d (%s)\n", defnid, 923178481Sjb tdesc_name(defn)); 924178481Sjb } 925178481Sjb 926178481Sjb debug(3, "Forward map: resolved %d to %s\n", defnid, tdesc_name(defn)); 927178481Sjb 928178481Sjb alist_add(rmd->rmd_map, (void *)fwd, (void *)defn); 929178481Sjb 930178481Sjb return (1); 931178481Sjb} 932178481Sjb 933178481Sjbstatic void 934178481Sjbredir_mstr_fwds(merge_cb_data_t *mcd) 935178481Sjb{ 936178481Sjb redir_mstr_data_t rmd; 937178481Sjb alist_t *map = alist_new(NULL, NULL); 938178481Sjb 939178481Sjb rmd.rmd_tgt = mcd->md_tgt; 940178481Sjb rmd.rmd_map = map; 941178481Sjb 942178481Sjb if (alist_iter(mcd->md_fdida, redir_mstr_fwd_cb, &rmd)) { 943178481Sjb (void) iitraverse_hash(mcd->md_tgt->td_iihash, 944178481Sjb &mcd->md_tgt->td_curvgen, fwd_redir_cbs, NULL, NULL, map); 945178481Sjb } 946178481Sjb 947178481Sjb alist_free(map); 948178481Sjb} 949178481Sjb 950178481Sjbstatic int 951178481Sjbadd_iitba_cb(void *data, void *private) 952178481Sjb{ 953178481Sjb merge_cb_data_t *mcd = private; 954178481Sjb iidesc_t *tba = data; 955178481Sjb iidesc_t *new; 956178481Sjb iifind_data_t iif; 957178481Sjb int newidx; 958178481Sjb 959178481Sjb newidx = get_mapping(mcd->md_ta, tba->ii_dtype->t_id); 960178481Sjb assert(newidx != -1); 961178481Sjb 962178481Sjb (void) list_remove(mcd->md_iitba, data, NULL, NULL); 963178481Sjb 964178481Sjb iif.iif_template = tba; 965178481Sjb iif.iif_ta = mcd->md_ta; 966178481Sjb iif.iif_newidx = newidx; 967178481Sjb iif.iif_refmerge = (mcd->md_flags & MCD_F_REFMERGE); 968178481Sjb 969178481Sjb if (hash_match(mcd->md_parent->td_iihash, tba, iidesc_match, 970178481Sjb &iif) == 1) { 971178481Sjb debug(3, "iidesc_t %s already exists\n", 972178481Sjb (tba->ii_name ? tba->ii_name : "(anon)")); 973178481Sjb return (1); 974178481Sjb } 975178481Sjb 976178481Sjb new = conjure_iidesc(tba, mcd); 977178481Sjb hash_add(mcd->md_tgt->td_iihash, new); 978178481Sjb 979178481Sjb return (1); 980178481Sjb} 981178481Sjb 982178481Sjbstatic int 983178481Sjbadd_tdesc(tdesc_t *oldtdp, int newid, merge_cb_data_t *mcd) 984178481Sjb{ 985178481Sjb tdesc_t *newtdp; 986178481Sjb tdesc_t template; 987178481Sjb 988178481Sjb template.t_id = newid; 989178481Sjb assert(hash_find(mcd->md_parent->td_idhash, 990178481Sjb (void *)&template, NULL) == 0); 991178481Sjb 992178546Sjb debug(3, "trying to conjure %d %s (%d, <%x>) as %d, <%x>\n", 993178546Sjb oldtdp->t_type, tdesc_name(oldtdp), oldtdp->t_id, 994178546Sjb oldtdp->t_id, newid, newid); 995178481Sjb 996178481Sjb if ((newtdp = tdesc_ops[oldtdp->t_type].conjure(oldtdp, newid, 997178481Sjb mcd)) == NULL) 998178481Sjb /* couldn't map everything */ 999178481Sjb return (0); 1000178481Sjb 1001178481Sjb debug(3, "succeeded\n"); 1002178481Sjb 1003178481Sjb hash_add(mcd->md_tgt->td_idhash, newtdp); 1004178481Sjb hash_add(mcd->md_tgt->td_layouthash, newtdp); 1005178481Sjb 1006178481Sjb return (1); 1007178481Sjb} 1008178481Sjb 1009178481Sjbstatic int 1010178481Sjbadd_tdtba_cb(void *data, void *arg) 1011178481Sjb{ 1012178481Sjb tdesc_t *tdp = data; 1013178481Sjb merge_cb_data_t *mcd = arg; 1014178481Sjb int newid; 1015178481Sjb int rc; 1016178481Sjb 1017178481Sjb newid = get_mapping(mcd->md_ta, tdp->t_id); 1018178481Sjb assert(newid != -1); 1019178481Sjb 1020178481Sjb if ((rc = add_tdesc(tdp, newid, mcd))) 1021178481Sjb hash_remove(mcd->md_tdtba, (void *)tdp); 1022178481Sjb 1023178481Sjb return (rc); 1024178481Sjb} 1025178481Sjb 1026178481Sjbstatic int 1027178481Sjbadd_tdtbr_cb(void *data, void *arg) 1028178481Sjb{ 1029178481Sjb tdesc_t **tdpp = data; 1030178481Sjb merge_cb_data_t *mcd = arg; 1031178481Sjb 1032178481Sjb debug(3, "Remapping %s (%d)\n", tdesc_name(*tdpp), (*tdpp)->t_id); 1033178481Sjb 1034178481Sjb if (!remap_node(tdpp, *tdpp, -1, NULL, mcd)) 1035178481Sjb return (0); 1036178481Sjb 1037178481Sjb (void) list_remove(mcd->md_tdtbr, (void *)tdpp, NULL, NULL); 1038178481Sjb return (1); 1039178481Sjb} 1040178481Sjb 1041178481Sjbstatic void 1042178481Sjbmerge_types(hash_t *src, merge_cb_data_t *mcd) 1043178481Sjb{ 1044178481Sjb list_t *iitba = NULL; 1045178481Sjb list_t *tdtbr = NULL; 1046178481Sjb int iirc, tdrc; 1047178481Sjb 1048178481Sjb mcd->md_iitba = &iitba; 1049178481Sjb mcd->md_tdtba = hash_new(TDATA_LAYOUT_HASH_SIZE, tdesc_layouthash, 1050178481Sjb tdesc_layoutcmp); 1051178481Sjb mcd->md_tdtbr = &tdtbr; 1052178481Sjb 1053178481Sjb (void) hash_iter(src, merge_type_cb, mcd); 1054178481Sjb 1055178546Sjb tdrc = hash_iter(mcd->md_tdtba, add_tdtba_cb, mcd); 1056178481Sjb debug(3, "add_tdtba_cb added %d items\n", tdrc); 1057178481Sjb 1058178546Sjb iirc = list_iter(*mcd->md_iitba, add_iitba_cb, mcd); 1059178481Sjb debug(3, "add_iitba_cb added %d items\n", iirc); 1060178481Sjb 1061178481Sjb assert(list_count(*mcd->md_iitba) == 0 && 1062178481Sjb hash_count(mcd->md_tdtba) == 0); 1063178481Sjb 1064178546Sjb tdrc = list_iter(*mcd->md_tdtbr, add_tdtbr_cb, mcd); 1065178481Sjb debug(3, "add_tdtbr_cb added %d items\n", tdrc); 1066178481Sjb 1067178481Sjb if (list_count(*mcd->md_tdtbr) != 0) 1068178481Sjb aborterr("Couldn't remap all nodes\n"); 1069178481Sjb 1070178481Sjb /* 1071178481Sjb * We now have an alist of master forwards and the ids of the new master 1072178481Sjb * definitions for those forwards in mcd->md_fdida. By this point, 1073178481Sjb * we're guaranteed that all of the master definitions referenced in 1074178481Sjb * fdida have been added to the master tree. We now traverse through 1075178481Sjb * the master tree, redirecting all edges inbound to forwards that have 1076178481Sjb * definitions to those definitions. 1077178481Sjb */ 1078178481Sjb if (mcd->md_parent == mcd->md_tgt) { 1079178481Sjb redir_mstr_fwds(mcd); 1080178481Sjb } 1081178481Sjb} 1082178481Sjb 1083178481Sjbvoid 1084178481Sjbmerge_into_master(tdata_t *cur, tdata_t *mstr, tdata_t *tgt, int selfuniquify) 1085178481Sjb{ 1086178481Sjb merge_cb_data_t mcd; 1087178481Sjb 1088178481Sjb cur->td_ref++; 1089178481Sjb mstr->td_ref++; 1090178481Sjb if (tgt) 1091178481Sjb tgt->td_ref++; 1092178481Sjb 1093178481Sjb assert(cur->td_ref == 1 && mstr->td_ref == 1 && 1094178481Sjb (tgt == NULL || tgt->td_ref == 1)); 1095178481Sjb 1096178481Sjb mcd.md_parent = mstr; 1097178481Sjb mcd.md_tgt = (tgt ? tgt : mstr); 1098178481Sjb mcd.md_ta = alist_new(NULL, NULL); 1099178481Sjb mcd.md_fdida = alist_new(NULL, NULL); 1100178481Sjb mcd.md_flags = 0; 1101178481Sjb 1102178481Sjb if (selfuniquify) 1103178481Sjb mcd.md_flags |= MCD_F_SELFUNIQUIFY; 1104178481Sjb if (tgt) 1105178481Sjb mcd.md_flags |= MCD_F_REFMERGE; 1106178481Sjb 1107178481Sjb mstr->td_curvgen = MAX(mstr->td_curvgen, cur->td_curvgen); 1108178481Sjb mstr->td_curemark = MAX(mstr->td_curemark, cur->td_curemark); 1109178481Sjb 1110178481Sjb merge_types(cur->td_iihash, &mcd); 1111178481Sjb 1112178481Sjb if (debug_level >= 3) { 1113178481Sjb debug(3, "Type association stats\n"); 1114178481Sjb alist_stats(mcd.md_ta, 0); 1115178481Sjb debug(3, "Layout hash stats\n"); 1116178481Sjb hash_stats(mcd.md_tgt->td_layouthash, 1); 1117178481Sjb } 1118178481Sjb 1119178481Sjb alist_free(mcd.md_fdida); 1120178481Sjb alist_free(mcd.md_ta); 1121178481Sjb 1122178481Sjb cur->td_ref--; 1123178481Sjb mstr->td_ref--; 1124178481Sjb if (tgt) 1125178481Sjb tgt->td_ref--; 1126178481Sjb} 1127178481Sjb 1128178481Sjbtdesc_ops_t tdesc_ops[] = { 1129178481Sjb { "ERROR! BAD tdesc TYPE", NULL, NULL }, 1130178481Sjb { "intrinsic", equiv_intrinsic, conjure_intrinsic }, 1131178481Sjb { "pointer", equiv_plain, conjure_plain }, 1132178481Sjb { "array", equiv_array, conjure_array }, 1133178481Sjb { "function", equiv_function, conjure_function }, 1134178481Sjb { "struct", equiv_su, conjure_su }, 1135178481Sjb { "union", equiv_su, conjure_su }, 1136178481Sjb { "enum", equiv_enum, conjure_enum }, 1137178481Sjb { "forward", NULL, conjure_forward }, 1138178481Sjb { "typedef", equiv_plain, conjure_plain }, 1139178481Sjb { "typedef_unres", equiv_assert, conjure_assert }, 1140178481Sjb { "volatile", equiv_plain, conjure_plain }, 1141178481Sjb { "const", equiv_plain, conjure_plain }, 1142178481Sjb { "restrict", equiv_plain, conjure_plain } 1143178481Sjb}; 1144