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 287178481Sjb while (ml1 && ml2) { 288178481Sjb if (ml1->ml_offset != ml2->ml_offset || 289274565Smarkj strcmp(ml1->ml_name, ml2->ml_name) != 0 || 290274565Smarkj ml1->ml_size != ml2->ml_size || 291274565Smarkj !equiv_node(ml1->ml_type, ml2->ml_type, ed)) 292178481Sjb return (0); 293178481Sjb 294178481Sjb ml1 = ml1->ml_next; 295178481Sjb ml2 = ml2->ml_next; 296178481Sjb } 297178481Sjb 298178481Sjb if (ml1 || ml2) 299178481Sjb return (0); 300178481Sjb 301178481Sjb return (1); 302178481Sjb} 303178481Sjb 304178481Sjb/*ARGSUSED2*/ 305178481Sjbstatic int 306178546Sjbequiv_enum(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed __unused) 307178481Sjb{ 308178481Sjb elist_t *el1 = stdp->t_emem; 309178481Sjb elist_t *el2 = ttdp->t_emem; 310178481Sjb 311178481Sjb while (el1 && el2) { 312178481Sjb if (el1->el_number != el2->el_number || 313178481Sjb strcmp(el1->el_name, el2->el_name) != 0) 314178481Sjb return (0); 315178481Sjb 316178481Sjb el1 = el1->el_next; 317178481Sjb el2 = el2->el_next; 318178481Sjb } 319178481Sjb 320178481Sjb if (el1 || el2) 321178481Sjb return (0); 322178481Sjb 323178481Sjb return (1); 324178481Sjb} 325178481Sjb 326178481Sjb/*ARGSUSED*/ 327178481Sjbstatic int 328178546Sjbequiv_assert(tdesc_t *stdp __unused, tdesc_t *ttdp __unused, equiv_data_t *ed __unused) 329178481Sjb{ 330178481Sjb /* foul, evil, and very bad - this is a "shouldn't happen" */ 331178481Sjb assert(1 == 0); 332178481Sjb 333178481Sjb return (0); 334178481Sjb} 335178481Sjb 336178481Sjbstatic int 337178481Sjbfwd_equiv(tdesc_t *ctdp, tdesc_t *mtdp) 338178481Sjb{ 339178481Sjb tdesc_t *defn = (ctdp->t_type == FORWARD ? mtdp : ctdp); 340178481Sjb 341305105Smarkj return (defn->t_type == STRUCT || defn->t_type == UNION || 342305105Smarkj defn->t_type == ENUM); 343178481Sjb} 344178481Sjb 345178481Sjbstatic int 346178481Sjbequiv_node(tdesc_t *ctdp, tdesc_t *mtdp, equiv_data_t *ed) 347178481Sjb{ 348178546Sjb int (*equiv)(tdesc_t *, tdesc_t *, equiv_data_t *); 349178481Sjb int mapping; 350178481Sjb 351287234Smarkj if (ctdp->t_emark > ed->ed_clear_mark && 352178481Sjb mtdp->t_emark > ed->ed_clear_mark) 353178481Sjb return (ctdp->t_emark == mtdp->t_emark); 354178481Sjb 355178481Sjb /* 356178481Sjb * In normal (non-self-uniquify) mode, we don't want to do equivalency 357178481Sjb * checking on a subgraph that has already been checked. If a mapping 358178481Sjb * has already been established for a given child node, we can simply 359178481Sjb * compare the mapping for the child node with the ID of the parent 360178481Sjb * node. If we are in self-uniquify mode, then we're comparing two 361178481Sjb * subgraphs within the child graph, and thus need to ignore any 362178481Sjb * type mappings that have been created, as they are only valid into the 363178481Sjb * parent. 364178481Sjb */ 365178481Sjb if ((mapping = get_mapping(ed->ed_ta, ctdp->t_id)) > 0 && 366178481Sjb mapping == mtdp->t_id && !ed->ed_selfuniquify) 367178481Sjb return (1); 368178481Sjb 369178481Sjb if (!streq(ctdp->t_name, mtdp->t_name)) 370178481Sjb return (0); 371178481Sjb 372178481Sjb if (ctdp->t_type != mtdp->t_type) { 373178481Sjb if (ctdp->t_type == FORWARD || mtdp->t_type == FORWARD) 374178481Sjb return (fwd_equiv(ctdp, mtdp)); 375178481Sjb else 376178481Sjb return (0); 377178481Sjb } 378178481Sjb 379178481Sjb ctdp->t_emark = ed->ed_cur_mark; 380178481Sjb mtdp->t_emark = ed->ed_cur_mark; 381178481Sjb ed->ed_cur_mark++; 382178481Sjb 383178481Sjb if ((equiv = tdesc_ops[ctdp->t_type].equiv) != NULL) 384178481Sjb return (equiv(ctdp, mtdp, ed)); 385178481Sjb 386178481Sjb return (1); 387178481Sjb} 388178481Sjb 389178481Sjb/* 390178481Sjb * We perform an equivalency check on two subgraphs by traversing through them 391178481Sjb * in lockstep. If a given node is equivalent in both the parent and the child, 392178481Sjb * we mark it in both subgraphs, using the t_emark field, with a monotonically 393178481Sjb * increasing number. If, in the course of the traversal, we reach a node that 394178481Sjb * we have visited and numbered during this equivalency check, we have a cycle. 395178481Sjb * If the previously-visited nodes don't have the same emark, then the edges 396178481Sjb * that brought us to these nodes are not equivalent, and so the check ends. 397178481Sjb * If the emarks are the same, the edges are equivalent. We then backtrack and 398178481Sjb * continue the traversal. If we have exhausted all edges in the subgraph, and 399178481Sjb * have not found any inequivalent nodes, then the subgraphs are equivalent. 400178481Sjb */ 401178481Sjbstatic int 402178481Sjbequiv_cb(void *bucket, void *arg) 403178481Sjb{ 404178481Sjb equiv_data_t *ed = arg; 405178481Sjb tdesc_t *mtdp = bucket; 406178481Sjb tdesc_t *ctdp = ed->ed_node; 407178481Sjb 408178481Sjb ed->ed_clear_mark = ed->ed_cur_mark + 1; 409178481Sjb ed->ed_cur_mark = ed->ed_clear_mark + 1; 410178481Sjb 411178481Sjb if (equiv_node(ctdp, mtdp, ed)) { 412178546Sjb debug(3, "equiv_node matched %d <%x> %d <%x>\n", 413178546Sjb ctdp->t_id, ctdp->t_id, mtdp->t_id, mtdp->t_id); 414178481Sjb ed->ed_tgt = mtdp; 415178481Sjb /* matched. stop looking */ 416178481Sjb return (-1); 417178481Sjb } 418178481Sjb 419178481Sjb return (0); 420178481Sjb} 421178481Sjb 422178481Sjb/*ARGSUSED1*/ 423178481Sjbstatic int 424178546Sjbmap_td_tree_pre(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private) 425178481Sjb{ 426178481Sjb merge_cb_data_t *mcd = private; 427178481Sjb 428178481Sjb if (get_mapping(mcd->md_ta, ctdp->t_id) > 0) 429178481Sjb return (0); 430178481Sjb 431178481Sjb return (1); 432178481Sjb} 433178481Sjb 434178481Sjb/*ARGSUSED1*/ 435178481Sjbstatic int 436178546Sjbmap_td_tree_post(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private) 437178481Sjb{ 438178481Sjb merge_cb_data_t *mcd = private; 439178481Sjb equiv_data_t ed; 440178481Sjb 441178481Sjb ed.ed_ta = mcd->md_ta; 442178481Sjb ed.ed_clear_mark = mcd->md_parent->td_curemark; 443178481Sjb ed.ed_cur_mark = mcd->md_parent->td_curemark + 1; 444178481Sjb ed.ed_node = ctdp; 445178481Sjb ed.ed_selfuniquify = 0; 446178481Sjb 447178546Sjb debug(3, "map_td_tree_post on %d <%x> %s\n", ctdp->t_id, ctdp->t_id,tdesc_name(ctdp)); 448178481Sjb 449178481Sjb if (hash_find_iter(mcd->md_parent->td_layouthash, ctdp, 450178481Sjb equiv_cb, &ed) < 0) { 451178481Sjb /* We found an equivalent node */ 452178481Sjb if (ed.ed_tgt->t_type == FORWARD && ctdp->t_type != FORWARD) { 453178481Sjb int id = mcd->md_tgt->td_nextid++; 454178481Sjb 455178546Sjb debug(3, "Creating new defn type %d <%x>\n", id, id); 456178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, id); 457178481Sjb alist_add(mcd->md_fdida, (void *)(ulong_t)ed.ed_tgt, 458178481Sjb (void *)(ulong_t)id); 459178481Sjb hash_add(mcd->md_tdtba, ctdp); 460178481Sjb } else 461178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, ed.ed_tgt->t_id); 462178481Sjb 463178481Sjb } else if (debug_level > 1 && hash_iter(mcd->md_parent->td_idhash, 464178481Sjb equiv_cb, &ed) < 0) { 465178481Sjb /* 466178481Sjb * We didn't find an equivalent node by looking through the 467178481Sjb * layout hash, but we somehow found it by performing an 468178481Sjb * exhaustive search through the entire graph. This usually 469178481Sjb * means that the "name" hash function is broken. 470178481Sjb */ 471178481Sjb aborterr("Second pass for %d (%s) == %d\n", ctdp->t_id, 472178481Sjb tdesc_name(ctdp), ed.ed_tgt->t_id); 473178481Sjb } else { 474178481Sjb int id = mcd->md_tgt->td_nextid++; 475178481Sjb 476178546Sjb debug(3, "Creating new type %d <%x>\n", id, id); 477178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, id); 478178481Sjb hash_add(mcd->md_tdtba, ctdp); 479178481Sjb } 480178481Sjb 481178481Sjb mcd->md_parent->td_curemark = ed.ed_cur_mark + 1; 482178481Sjb 483178481Sjb return (1); 484178481Sjb} 485178481Sjb 486178481Sjb/*ARGSUSED1*/ 487178481Sjbstatic int 488178546Sjbmap_td_tree_self_post(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private) 489178481Sjb{ 490178481Sjb merge_cb_data_t *mcd = private; 491178481Sjb equiv_data_t ed; 492178481Sjb 493178481Sjb ed.ed_ta = mcd->md_ta; 494178481Sjb ed.ed_clear_mark = mcd->md_parent->td_curemark; 495178481Sjb ed.ed_cur_mark = mcd->md_parent->td_curemark + 1; 496178481Sjb ed.ed_node = ctdp; 497178481Sjb ed.ed_selfuniquify = 1; 498178481Sjb ed.ed_tgt = NULL; 499178481Sjb 500178481Sjb if (hash_find_iter(mcd->md_tdtba, ctdp, equiv_cb, &ed) < 0) { 501178546Sjb debug(3, "Self check found %d <%x> in %d <%x>\n", ctdp->t_id, 502178546Sjb ctdp->t_id, ed.ed_tgt->t_id, ed.ed_tgt->t_id); 503178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, 504178481Sjb get_mapping(mcd->md_ta, ed.ed_tgt->t_id)); 505178481Sjb } else if (debug_level > 1 && hash_iter(mcd->md_tdtba, 506178481Sjb equiv_cb, &ed) < 0) { 507178481Sjb /* 508178481Sjb * We didn't find an equivalent node using the quick way (going 509178481Sjb * through the hash normally), but we did find it by iterating 510178481Sjb * through the entire hash. This usually means that the hash 511178481Sjb * function is broken. 512178481Sjb */ 513178546Sjb aborterr("Self-unique second pass for %d <%x> (%s) == %d <%x>\n", 514178546Sjb ctdp->t_id, ctdp->t_id, tdesc_name(ctdp), ed.ed_tgt->t_id, 515178546Sjb ed.ed_tgt->t_id); 516178481Sjb } else { 517178481Sjb int id = mcd->md_tgt->td_nextid++; 518178481Sjb 519178546Sjb debug(3, "Creating new type %d <%x>\n", id, id); 520178481Sjb add_mapping(mcd->md_ta, ctdp->t_id, id); 521178481Sjb hash_add(mcd->md_tdtba, ctdp); 522178481Sjb } 523178481Sjb 524178481Sjb mcd->md_parent->td_curemark = ed.ed_cur_mark + 1; 525178481Sjb 526178481Sjb return (1); 527178481Sjb} 528178481Sjb 529178481Sjbstatic tdtrav_cb_f map_pre[] = { 530178481Sjb NULL, 531178481Sjb map_td_tree_pre, /* intrinsic */ 532178481Sjb map_td_tree_pre, /* pointer */ 533178481Sjb map_td_tree_pre, /* array */ 534178481Sjb map_td_tree_pre, /* function */ 535178481Sjb map_td_tree_pre, /* struct */ 536178481Sjb map_td_tree_pre, /* union */ 537178481Sjb map_td_tree_pre, /* enum */ 538178481Sjb map_td_tree_pre, /* forward */ 539178481Sjb map_td_tree_pre, /* typedef */ 540178481Sjb tdtrav_assert, /* typedef_unres */ 541178481Sjb map_td_tree_pre, /* volatile */ 542178481Sjb map_td_tree_pre, /* const */ 543178481Sjb map_td_tree_pre /* restrict */ 544178481Sjb}; 545178481Sjb 546178481Sjbstatic tdtrav_cb_f map_post[] = { 547178481Sjb NULL, 548178481Sjb map_td_tree_post, /* intrinsic */ 549178481Sjb map_td_tree_post, /* pointer */ 550178481Sjb map_td_tree_post, /* array */ 551178481Sjb map_td_tree_post, /* function */ 552178481Sjb map_td_tree_post, /* struct */ 553178481Sjb map_td_tree_post, /* union */ 554178481Sjb map_td_tree_post, /* enum */ 555178481Sjb map_td_tree_post, /* forward */ 556178481Sjb map_td_tree_post, /* typedef */ 557178481Sjb tdtrav_assert, /* typedef_unres */ 558178481Sjb map_td_tree_post, /* volatile */ 559178481Sjb map_td_tree_post, /* const */ 560178481Sjb map_td_tree_post /* restrict */ 561178481Sjb}; 562178481Sjb 563178481Sjbstatic tdtrav_cb_f map_self_post[] = { 564178481Sjb NULL, 565178481Sjb map_td_tree_self_post, /* intrinsic */ 566178481Sjb map_td_tree_self_post, /* pointer */ 567178481Sjb map_td_tree_self_post, /* array */ 568178481Sjb map_td_tree_self_post, /* function */ 569178481Sjb map_td_tree_self_post, /* struct */ 570178481Sjb map_td_tree_self_post, /* union */ 571178481Sjb map_td_tree_self_post, /* enum */ 572178481Sjb map_td_tree_self_post, /* forward */ 573178481Sjb map_td_tree_self_post, /* typedef */ 574178481Sjb tdtrav_assert, /* typedef_unres */ 575178481Sjb map_td_tree_self_post, /* volatile */ 576178481Sjb map_td_tree_self_post, /* const */ 577178481Sjb map_td_tree_self_post /* restrict */ 578178481Sjb}; 579178481Sjb 580178481Sjb/* 581178481Sjb * Determining equivalence of iidesc_t nodes 582178481Sjb */ 583178481Sjb 584178481Sjbtypedef struct iifind_data { 585178481Sjb iidesc_t *iif_template; 586178481Sjb alist_t *iif_ta; 587178481Sjb int iif_newidx; 588178481Sjb int iif_refmerge; 589178481Sjb} iifind_data_t; 590178481Sjb 591178481Sjb/* 592178481Sjb * Check to see if this iidesc_t (node) - the current one on the list we're 593178481Sjb * iterating through - matches the target one (iif->iif_template). Return -1 594178481Sjb * if it matches, to stop the iteration. 595178481Sjb */ 596178481Sjbstatic int 597178481Sjbiidesc_match(void *data, void *arg) 598178481Sjb{ 599178481Sjb iidesc_t *node = data; 600178481Sjb iifind_data_t *iif = arg; 601178481Sjb int i; 602178481Sjb 603178481Sjb if (node->ii_type != iif->iif_template->ii_type || 604178481Sjb !streq(node->ii_name, iif->iif_template->ii_name) || 605178481Sjb node->ii_dtype->t_id != iif->iif_newidx) 606178481Sjb return (0); 607178481Sjb 608178481Sjb if ((node->ii_type == II_SVAR || node->ii_type == II_SFUN) && 609178481Sjb !streq(node->ii_owner, iif->iif_template->ii_owner)) 610178481Sjb return (0); 611178481Sjb 612178481Sjb if (node->ii_nargs != iif->iif_template->ii_nargs) 613178481Sjb return (0); 614178481Sjb 615178481Sjb for (i = 0; i < node->ii_nargs; i++) { 616178481Sjb if (get_mapping(iif->iif_ta, 617178481Sjb iif->iif_template->ii_args[i]->t_id) != 618178481Sjb node->ii_args[i]->t_id) 619178481Sjb return (0); 620178481Sjb } 621178481Sjb 622178481Sjb if (iif->iif_refmerge) { 623178481Sjb switch (iif->iif_template->ii_type) { 624178481Sjb case II_GFUN: 625178481Sjb case II_SFUN: 626178481Sjb case II_GVAR: 627178481Sjb case II_SVAR: 628178481Sjb debug(3, "suppressing duping of %d %s from %s\n", 629178481Sjb iif->iif_template->ii_type, 630178481Sjb iif->iif_template->ii_name, 631178481Sjb (iif->iif_template->ii_owner ? 632178481Sjb iif->iif_template->ii_owner : "NULL")); 633178481Sjb return (0); 634178481Sjb case II_NOT: 635178481Sjb case II_PSYM: 636178481Sjb case II_SOU: 637178481Sjb case II_TYPE: 638178481Sjb break; 639178481Sjb } 640178481Sjb } 641178481Sjb 642178481Sjb return (-1); 643178481Sjb} 644178481Sjb 645178481Sjbstatic int 646178481Sjbmerge_type_cb(void *data, void *arg) 647178481Sjb{ 648178481Sjb iidesc_t *sii = data; 649178481Sjb merge_cb_data_t *mcd = arg; 650178481Sjb iifind_data_t iif; 651178481Sjb tdtrav_cb_f *post; 652178481Sjb 653178481Sjb post = (mcd->md_flags & MCD_F_SELFUNIQUIFY ? map_self_post : map_post); 654178481Sjb 655178481Sjb /* Map the tdesc nodes */ 656178481Sjb (void) iitraverse(sii, &mcd->md_parent->td_curvgen, NULL, map_pre, post, 657178481Sjb mcd); 658178481Sjb 659178481Sjb /* Map the iidesc nodes */ 660178481Sjb iif.iif_template = sii; 661178481Sjb iif.iif_ta = mcd->md_ta; 662178481Sjb iif.iif_newidx = get_mapping(mcd->md_ta, sii->ii_dtype->t_id); 663178481Sjb iif.iif_refmerge = (mcd->md_flags & MCD_F_REFMERGE); 664178481Sjb 665178481Sjb if (hash_match(mcd->md_parent->td_iihash, sii, iidesc_match, 666178481Sjb &iif) == 1) 667178481Sjb /* successfully mapped */ 668178481Sjb return (1); 669178481Sjb 670178481Sjb debug(3, "tba %s (%d)\n", (sii->ii_name ? sii->ii_name : "(anon)"), 671178481Sjb sii->ii_type); 672178481Sjb 673178481Sjb list_add(mcd->md_iitba, sii); 674178481Sjb 675178481Sjb return (0); 676178481Sjb} 677178481Sjb 678178481Sjbstatic int 679178481Sjbremap_node(tdesc_t **tgtp, tdesc_t *oldtgt, int selftid, tdesc_t *newself, 680178481Sjb merge_cb_data_t *mcd) 681178481Sjb{ 682178481Sjb tdesc_t *tgt = NULL; 683178481Sjb tdesc_t template; 684178481Sjb int oldid = oldtgt->t_id; 685178481Sjb 686178481Sjb if (oldid == selftid) { 687178481Sjb *tgtp = newself; 688178481Sjb return (1); 689178481Sjb } 690178481Sjb 691178481Sjb if ((template.t_id = get_mapping(mcd->md_ta, oldid)) == 0) 692178546Sjb aborterr("failed to get mapping for tid %d <%x>\n", oldid, oldid); 693178481Sjb 694178481Sjb if (!hash_find(mcd->md_parent->td_idhash, (void *)&template, 695178481Sjb (void *)&tgt) && (!(mcd->md_flags & MCD_F_REFMERGE) || 696178481Sjb !hash_find(mcd->md_tgt->td_idhash, (void *)&template, 697178481Sjb (void *)&tgt))) { 698178546Sjb debug(3, "Remap couldn't find %d <%x> (from %d <%x>)\n", template.t_id, 699178546Sjb template.t_id, oldid, oldid); 700178481Sjb *tgtp = oldtgt; 701178481Sjb list_add(mcd->md_tdtbr, tgtp); 702178481Sjb return (0); 703178481Sjb } 704178481Sjb 705178481Sjb *tgtp = tgt; 706178481Sjb return (1); 707178481Sjb} 708178481Sjb 709178481Sjbstatic tdesc_t * 710178481Sjbconjure_template(tdesc_t *old, int newselfid) 711178481Sjb{ 712178481Sjb tdesc_t *new = xcalloc(sizeof (tdesc_t)); 713178481Sjb 714178481Sjb new->t_name = old->t_name ? xstrdup(old->t_name) : NULL; 715178481Sjb new->t_type = old->t_type; 716178481Sjb new->t_size = old->t_size; 717178481Sjb new->t_id = newselfid; 718178481Sjb new->t_flags = old->t_flags; 719178481Sjb 720178481Sjb return (new); 721178481Sjb} 722178481Sjb 723178481Sjb/*ARGSUSED2*/ 724178481Sjbstatic tdesc_t * 725178546Sjbconjure_intrinsic(tdesc_t *old, int newselfid, merge_cb_data_t *mcd __unused) 726178481Sjb{ 727178481Sjb tdesc_t *new = conjure_template(old, newselfid); 728178481Sjb 729178481Sjb new->t_intr = xmalloc(sizeof (intr_t)); 730178481Sjb bcopy(old->t_intr, new->t_intr, sizeof (intr_t)); 731178481Sjb 732178481Sjb return (new); 733178481Sjb} 734178481Sjb 735178481Sjbstatic tdesc_t * 736178481Sjbconjure_plain(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 737178481Sjb{ 738178481Sjb tdesc_t *new = conjure_template(old, newselfid); 739178481Sjb 740178481Sjb (void) remap_node(&new->t_tdesc, old->t_tdesc, old->t_id, new, mcd); 741178481Sjb 742178481Sjb return (new); 743178481Sjb} 744178481Sjb 745178481Sjbstatic tdesc_t * 746178481Sjbconjure_function(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 747178481Sjb{ 748178481Sjb tdesc_t *new = conjure_template(old, newselfid); 749178481Sjb fndef_t *nfn = xmalloc(sizeof (fndef_t)); 750178481Sjb fndef_t *ofn = old->t_fndef; 751178481Sjb int i; 752178481Sjb 753178481Sjb (void) remap_node(&nfn->fn_ret, ofn->fn_ret, old->t_id, new, mcd); 754178481Sjb 755178481Sjb nfn->fn_nargs = ofn->fn_nargs; 756178481Sjb nfn->fn_vargs = ofn->fn_vargs; 757178481Sjb 758178481Sjb if (nfn->fn_nargs > 0) 759178481Sjb nfn->fn_args = xcalloc(sizeof (tdesc_t *) * ofn->fn_nargs); 760178481Sjb 761178546Sjb for (i = 0; i < (int) ofn->fn_nargs; i++) { 762178481Sjb (void) remap_node(&nfn->fn_args[i], ofn->fn_args[i], old->t_id, 763178481Sjb new, mcd); 764178481Sjb } 765178481Sjb 766178481Sjb new->t_fndef = nfn; 767178481Sjb 768178481Sjb return (new); 769178481Sjb} 770178481Sjb 771178481Sjbstatic tdesc_t * 772178481Sjbconjure_array(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 773178481Sjb{ 774178481Sjb tdesc_t *new = conjure_template(old, newselfid); 775178481Sjb ardef_t *nar = xmalloc(sizeof (ardef_t)); 776178481Sjb ardef_t *oar = old->t_ardef; 777178481Sjb 778178481Sjb (void) remap_node(&nar->ad_contents, oar->ad_contents, old->t_id, new, 779178481Sjb mcd); 780178481Sjb (void) remap_node(&nar->ad_idxtype, oar->ad_idxtype, old->t_id, new, 781178481Sjb mcd); 782178481Sjb 783178481Sjb nar->ad_nelems = oar->ad_nelems; 784178481Sjb 785178481Sjb new->t_ardef = nar; 786178481Sjb 787178481Sjb return (new); 788178481Sjb} 789178481Sjb 790178481Sjbstatic tdesc_t * 791178481Sjbconjure_su(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 792178481Sjb{ 793178481Sjb tdesc_t *new = conjure_template(old, newselfid); 794178481Sjb mlist_t *omem, **nmemp; 795178481Sjb 796178481Sjb for (omem = old->t_members, nmemp = &new->t_members; 797178481Sjb omem; omem = omem->ml_next, nmemp = &((*nmemp)->ml_next)) { 798178481Sjb *nmemp = xmalloc(sizeof (mlist_t)); 799178481Sjb (*nmemp)->ml_offset = omem->ml_offset; 800178481Sjb (*nmemp)->ml_size = omem->ml_size; 801178546Sjb (*nmemp)->ml_name = xstrdup(omem->ml_name ? omem->ml_name : "empty omem->ml_name"); 802178481Sjb (void) remap_node(&((*nmemp)->ml_type), omem->ml_type, 803178481Sjb old->t_id, new, mcd); 804178481Sjb } 805178481Sjb *nmemp = NULL; 806178481Sjb 807178481Sjb return (new); 808178481Sjb} 809178481Sjb 810178481Sjb/*ARGSUSED2*/ 811178481Sjbstatic tdesc_t * 812178546Sjbconjure_enum(tdesc_t *old, int newselfid, merge_cb_data_t *mcd __unused) 813178481Sjb{ 814178481Sjb tdesc_t *new = conjure_template(old, newselfid); 815178481Sjb elist_t *oel, **nelp; 816178481Sjb 817178481Sjb for (oel = old->t_emem, nelp = &new->t_emem; 818178481Sjb oel; oel = oel->el_next, nelp = &((*nelp)->el_next)) { 819178481Sjb *nelp = xmalloc(sizeof (elist_t)); 820178481Sjb (*nelp)->el_name = xstrdup(oel->el_name); 821178481Sjb (*nelp)->el_number = oel->el_number; 822178481Sjb } 823178481Sjb *nelp = NULL; 824178481Sjb 825178481Sjb return (new); 826178481Sjb} 827178481Sjb 828178481Sjb/*ARGSUSED2*/ 829178481Sjbstatic tdesc_t * 830178481Sjbconjure_forward(tdesc_t *old, int newselfid, merge_cb_data_t *mcd) 831178481Sjb{ 832178481Sjb tdesc_t *new = conjure_template(old, newselfid); 833178481Sjb 834178481Sjb list_add(&mcd->md_tgt->td_fwdlist, new); 835178481Sjb 836178481Sjb return (new); 837178481Sjb} 838178481Sjb 839178481Sjb/*ARGSUSED*/ 840178481Sjbstatic tdesc_t * 841178546Sjbconjure_assert(tdesc_t *old __unused, int newselfid __unused, merge_cb_data_t *mcd __unused) 842178481Sjb{ 843178481Sjb assert(1 == 0); 844178481Sjb return (NULL); 845178481Sjb} 846178481Sjb 847178481Sjbstatic iidesc_t * 848178481Sjbconjure_iidesc(iidesc_t *old, merge_cb_data_t *mcd) 849178481Sjb{ 850178481Sjb iidesc_t *new = iidesc_dup(old); 851178481Sjb int i; 852178481Sjb 853178481Sjb (void) remap_node(&new->ii_dtype, old->ii_dtype, -1, NULL, mcd); 854178481Sjb for (i = 0; i < new->ii_nargs; i++) { 855178481Sjb (void) remap_node(&new->ii_args[i], old->ii_args[i], -1, NULL, 856178481Sjb mcd); 857178481Sjb } 858178481Sjb 859178481Sjb return (new); 860178481Sjb} 861178481Sjb 862178481Sjbstatic int 863178481Sjbfwd_redir(tdesc_t *fwd, tdesc_t **fwdp, void *private) 864178481Sjb{ 865178481Sjb alist_t *map = private; 866178546Sjb void *defn; 867178481Sjb 868178481Sjb if (!alist_find(map, (void *)fwd, (void **)&defn)) 869178481Sjb return (0); 870178481Sjb 871178481Sjb debug(3, "Redirecting an edge to %s\n", tdesc_name(defn)); 872178481Sjb 873178481Sjb *fwdp = defn; 874178481Sjb 875178481Sjb return (1); 876178481Sjb} 877178481Sjb 878178481Sjbstatic tdtrav_cb_f fwd_redir_cbs[] = { 879178481Sjb NULL, 880178481Sjb NULL, /* intrinsic */ 881178481Sjb NULL, /* pointer */ 882178481Sjb NULL, /* array */ 883178481Sjb NULL, /* function */ 884178481Sjb NULL, /* struct */ 885178481Sjb NULL, /* union */ 886178481Sjb NULL, /* enum */ 887178481Sjb fwd_redir, /* forward */ 888178481Sjb NULL, /* typedef */ 889178481Sjb tdtrav_assert, /* typedef_unres */ 890178481Sjb NULL, /* volatile */ 891178481Sjb NULL, /* const */ 892178481Sjb NULL /* restrict */ 893178481Sjb}; 894178481Sjb 895178481Sjbtypedef struct redir_mstr_data { 896178481Sjb tdata_t *rmd_tgt; 897178481Sjb alist_t *rmd_map; 898178481Sjb} redir_mstr_data_t; 899178481Sjb 900178481Sjbstatic int 901178481Sjbredir_mstr_fwd_cb(void *name, void *value, void *arg) 902178481Sjb{ 903178481Sjb tdesc_t *fwd = name; 904178546Sjb int defnid = (uintptr_t)value; 905178481Sjb redir_mstr_data_t *rmd = arg; 906178481Sjb tdesc_t template; 907178481Sjb tdesc_t *defn; 908178481Sjb 909178481Sjb template.t_id = defnid; 910178481Sjb 911178481Sjb if (!hash_find(rmd->rmd_tgt->td_idhash, (void *)&template, 912178481Sjb (void *)&defn)) { 913178481Sjb aborterr("Couldn't unforward %d (%s)\n", defnid, 914178481Sjb tdesc_name(defn)); 915178481Sjb } 916178481Sjb 917178481Sjb debug(3, "Forward map: resolved %d to %s\n", defnid, tdesc_name(defn)); 918178481Sjb 919178481Sjb alist_add(rmd->rmd_map, (void *)fwd, (void *)defn); 920178481Sjb 921178481Sjb return (1); 922178481Sjb} 923178481Sjb 924178481Sjbstatic void 925178481Sjbredir_mstr_fwds(merge_cb_data_t *mcd) 926178481Sjb{ 927178481Sjb redir_mstr_data_t rmd; 928178481Sjb alist_t *map = alist_new(NULL, NULL); 929178481Sjb 930178481Sjb rmd.rmd_tgt = mcd->md_tgt; 931178481Sjb rmd.rmd_map = map; 932178481Sjb 933178481Sjb if (alist_iter(mcd->md_fdida, redir_mstr_fwd_cb, &rmd)) { 934178481Sjb (void) iitraverse_hash(mcd->md_tgt->td_iihash, 935178481Sjb &mcd->md_tgt->td_curvgen, fwd_redir_cbs, NULL, NULL, map); 936178481Sjb } 937178481Sjb 938178481Sjb alist_free(map); 939178481Sjb} 940178481Sjb 941178481Sjbstatic int 942178481Sjbadd_iitba_cb(void *data, void *private) 943178481Sjb{ 944178481Sjb merge_cb_data_t *mcd = private; 945178481Sjb iidesc_t *tba = data; 946178481Sjb iidesc_t *new; 947178481Sjb iifind_data_t iif; 948178481Sjb int newidx; 949178481Sjb 950178481Sjb newidx = get_mapping(mcd->md_ta, tba->ii_dtype->t_id); 951178481Sjb assert(newidx != -1); 952178481Sjb 953178481Sjb (void) list_remove(mcd->md_iitba, data, NULL, NULL); 954178481Sjb 955178481Sjb iif.iif_template = tba; 956178481Sjb iif.iif_ta = mcd->md_ta; 957178481Sjb iif.iif_newidx = newidx; 958178481Sjb iif.iif_refmerge = (mcd->md_flags & MCD_F_REFMERGE); 959178481Sjb 960178481Sjb if (hash_match(mcd->md_parent->td_iihash, tba, iidesc_match, 961178481Sjb &iif) == 1) { 962178481Sjb debug(3, "iidesc_t %s already exists\n", 963178481Sjb (tba->ii_name ? tba->ii_name : "(anon)")); 964178481Sjb return (1); 965178481Sjb } 966178481Sjb 967178481Sjb new = conjure_iidesc(tba, mcd); 968178481Sjb hash_add(mcd->md_tgt->td_iihash, new); 969178481Sjb 970178481Sjb return (1); 971178481Sjb} 972178481Sjb 973178481Sjbstatic int 974178481Sjbadd_tdesc(tdesc_t *oldtdp, int newid, merge_cb_data_t *mcd) 975178481Sjb{ 976178481Sjb tdesc_t *newtdp; 977178481Sjb tdesc_t template; 978178481Sjb 979178481Sjb template.t_id = newid; 980178481Sjb assert(hash_find(mcd->md_parent->td_idhash, 981178481Sjb (void *)&template, NULL) == 0); 982178481Sjb 983178546Sjb debug(3, "trying to conjure %d %s (%d, <%x>) as %d, <%x>\n", 984178546Sjb oldtdp->t_type, tdesc_name(oldtdp), oldtdp->t_id, 985178546Sjb oldtdp->t_id, newid, newid); 986178481Sjb 987178481Sjb if ((newtdp = tdesc_ops[oldtdp->t_type].conjure(oldtdp, newid, 988178481Sjb mcd)) == NULL) 989178481Sjb /* couldn't map everything */ 990178481Sjb return (0); 991178481Sjb 992178481Sjb debug(3, "succeeded\n"); 993178481Sjb 994178481Sjb hash_add(mcd->md_tgt->td_idhash, newtdp); 995178481Sjb hash_add(mcd->md_tgt->td_layouthash, newtdp); 996178481Sjb 997178481Sjb return (1); 998178481Sjb} 999178481Sjb 1000178481Sjbstatic int 1001178481Sjbadd_tdtba_cb(void *data, void *arg) 1002178481Sjb{ 1003178481Sjb tdesc_t *tdp = data; 1004178481Sjb merge_cb_data_t *mcd = arg; 1005178481Sjb int newid; 1006178481Sjb int rc; 1007178481Sjb 1008178481Sjb newid = get_mapping(mcd->md_ta, tdp->t_id); 1009178481Sjb assert(newid != -1); 1010178481Sjb 1011178481Sjb if ((rc = add_tdesc(tdp, newid, mcd))) 1012178481Sjb hash_remove(mcd->md_tdtba, (void *)tdp); 1013178481Sjb 1014178481Sjb return (rc); 1015178481Sjb} 1016178481Sjb 1017178481Sjbstatic int 1018178481Sjbadd_tdtbr_cb(void *data, void *arg) 1019178481Sjb{ 1020178481Sjb tdesc_t **tdpp = data; 1021178481Sjb merge_cb_data_t *mcd = arg; 1022178481Sjb 1023178481Sjb debug(3, "Remapping %s (%d)\n", tdesc_name(*tdpp), (*tdpp)->t_id); 1024178481Sjb 1025178481Sjb if (!remap_node(tdpp, *tdpp, -1, NULL, mcd)) 1026178481Sjb return (0); 1027178481Sjb 1028178481Sjb (void) list_remove(mcd->md_tdtbr, (void *)tdpp, NULL, NULL); 1029178481Sjb return (1); 1030178481Sjb} 1031178481Sjb 1032178481Sjbstatic void 1033178481Sjbmerge_types(hash_t *src, merge_cb_data_t *mcd) 1034178481Sjb{ 1035178481Sjb list_t *iitba = NULL; 1036178481Sjb list_t *tdtbr = NULL; 1037178481Sjb int iirc, tdrc; 1038178481Sjb 1039178481Sjb mcd->md_iitba = &iitba; 1040178481Sjb mcd->md_tdtba = hash_new(TDATA_LAYOUT_HASH_SIZE, tdesc_layouthash, 1041178481Sjb tdesc_layoutcmp); 1042178481Sjb mcd->md_tdtbr = &tdtbr; 1043178481Sjb 1044178481Sjb (void) hash_iter(src, merge_type_cb, mcd); 1045178481Sjb 1046178546Sjb tdrc = hash_iter(mcd->md_tdtba, add_tdtba_cb, mcd); 1047178481Sjb debug(3, "add_tdtba_cb added %d items\n", tdrc); 1048178481Sjb 1049178546Sjb iirc = list_iter(*mcd->md_iitba, add_iitba_cb, mcd); 1050178481Sjb debug(3, "add_iitba_cb added %d items\n", iirc); 1051178481Sjb 1052178481Sjb assert(list_count(*mcd->md_iitba) == 0 && 1053178481Sjb hash_count(mcd->md_tdtba) == 0); 1054178481Sjb 1055178546Sjb tdrc = list_iter(*mcd->md_tdtbr, add_tdtbr_cb, mcd); 1056178481Sjb debug(3, "add_tdtbr_cb added %d items\n", tdrc); 1057178481Sjb 1058178481Sjb if (list_count(*mcd->md_tdtbr) != 0) 1059178481Sjb aborterr("Couldn't remap all nodes\n"); 1060178481Sjb 1061178481Sjb /* 1062178481Sjb * We now have an alist of master forwards and the ids of the new master 1063178481Sjb * definitions for those forwards in mcd->md_fdida. By this point, 1064178481Sjb * we're guaranteed that all of the master definitions referenced in 1065178481Sjb * fdida have been added to the master tree. We now traverse through 1066178481Sjb * the master tree, redirecting all edges inbound to forwards that have 1067178481Sjb * definitions to those definitions. 1068178481Sjb */ 1069178481Sjb if (mcd->md_parent == mcd->md_tgt) { 1070178481Sjb redir_mstr_fwds(mcd); 1071178481Sjb } 1072178481Sjb} 1073178481Sjb 1074178481Sjbvoid 1075178481Sjbmerge_into_master(tdata_t *cur, tdata_t *mstr, tdata_t *tgt, int selfuniquify) 1076178481Sjb{ 1077178481Sjb merge_cb_data_t mcd; 1078178481Sjb 1079178481Sjb cur->td_ref++; 1080178481Sjb mstr->td_ref++; 1081178481Sjb if (tgt) 1082178481Sjb tgt->td_ref++; 1083178481Sjb 1084178481Sjb assert(cur->td_ref == 1 && mstr->td_ref == 1 && 1085178481Sjb (tgt == NULL || tgt->td_ref == 1)); 1086178481Sjb 1087178481Sjb mcd.md_parent = mstr; 1088178481Sjb mcd.md_tgt = (tgt ? tgt : mstr); 1089178481Sjb mcd.md_ta = alist_new(NULL, NULL); 1090178481Sjb mcd.md_fdida = alist_new(NULL, NULL); 1091178481Sjb mcd.md_flags = 0; 1092178481Sjb 1093178481Sjb if (selfuniquify) 1094178481Sjb mcd.md_flags |= MCD_F_SELFUNIQUIFY; 1095178481Sjb if (tgt) 1096178481Sjb mcd.md_flags |= MCD_F_REFMERGE; 1097178481Sjb 1098178481Sjb mstr->td_curvgen = MAX(mstr->td_curvgen, cur->td_curvgen); 1099178481Sjb mstr->td_curemark = MAX(mstr->td_curemark, cur->td_curemark); 1100178481Sjb 1101178481Sjb merge_types(cur->td_iihash, &mcd); 1102178481Sjb 1103178481Sjb if (debug_level >= 3) { 1104178481Sjb debug(3, "Type association stats\n"); 1105178481Sjb alist_stats(mcd.md_ta, 0); 1106178481Sjb debug(3, "Layout hash stats\n"); 1107178481Sjb hash_stats(mcd.md_tgt->td_layouthash, 1); 1108178481Sjb } 1109178481Sjb 1110178481Sjb alist_free(mcd.md_fdida); 1111178481Sjb alist_free(mcd.md_ta); 1112178481Sjb 1113178481Sjb cur->td_ref--; 1114178481Sjb mstr->td_ref--; 1115178481Sjb if (tgt) 1116178481Sjb tgt->td_ref--; 1117178481Sjb} 1118178481Sjb 1119178481Sjbtdesc_ops_t tdesc_ops[] = { 1120178481Sjb { "ERROR! BAD tdesc TYPE", NULL, NULL }, 1121178481Sjb { "intrinsic", equiv_intrinsic, conjure_intrinsic }, 1122178481Sjb { "pointer", equiv_plain, conjure_plain }, 1123178481Sjb { "array", equiv_array, conjure_array }, 1124178481Sjb { "function", equiv_function, conjure_function }, 1125178481Sjb { "struct", equiv_su, conjure_su }, 1126178481Sjb { "union", equiv_su, conjure_su }, 1127178481Sjb { "enum", equiv_enum, conjure_enum }, 1128178481Sjb { "forward", NULL, conjure_forward }, 1129178481Sjb { "typedef", equiv_plain, conjure_plain }, 1130178481Sjb { "typedef_unres", equiv_assert, conjure_assert }, 1131178481Sjb { "volatile", equiv_plain, conjure_plain }, 1132178481Sjb { "const", equiv_plain, conjure_plain }, 1133178481Sjb { "restrict", equiv_plain, conjure_plain } 1134178481Sjb}; 1135