1 /* LTO symbol table. 2 Copyright (C) 2009-2020 Free Software Foundation, Inc. 3 Contributed by CodeSourcery, Inc. 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify it under 8 the terms of the GNU General Public License as published by the Free 9 Software Foundation; either version 3, or (at your option) any later 10 version. 11 12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13 WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 #include "config.h" 22 #include "system.h" 23 #include "coretypes.h" 24 #include "target.h" 25 #include "function.h" 26 #include "basic-block.h" 27 #include "tree.h" 28 #include "gimple.h" 29 #include "cgraph.h" 30 #include "lto-streamer.h" 31 #include "ipa-utils.h" 32 #include "builtins.h" 33 #include "alias.h" 34 #include "lto.h" 35 #include "lto-symtab.h" 36 #include "stringpool.h" 37 #include "attribs.h" 38 39 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging 40 all edges and removing the old node. */ 41 42 static void 43 lto_cgraph_replace_node (struct cgraph_node *node, 44 struct cgraph_node *prevailing_node) 45 { 46 struct cgraph_edge *e, *next; 47 bool compatible_p; 48 49 if (dump_file) 50 { 51 fprintf (dump_file, "Replacing cgraph node %s by %s" 52 " for symbol %s\n", 53 node->dump_name (), 54 prevailing_node->dump_name (), 55 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) 56 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl))))); 57 } 58 59 /* Merge node flags. */ 60 if (node->force_output) 61 prevailing_node->mark_force_output (); 62 if (node->forced_by_abi) 63 prevailing_node->forced_by_abi = true; 64 if (node->address_taken) 65 { 66 gcc_assert (!prevailing_node->inlined_to); 67 prevailing_node->mark_address_taken (); 68 } 69 if (node->definition && prevailing_node->definition 70 && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl)) 71 prevailing_node->merged_comdat = true; 72 else if ((node->definition || node->body_removed) 73 && DECL_DECLARED_INLINE_P (node->decl) 74 && DECL_EXTERNAL (node->decl) 75 && prevailing_node->definition) 76 prevailing_node->merged_extern_inline = true; 77 prevailing_node->merged_comdat |= node->merged_comdat; 78 prevailing_node->merged_extern_inline |= node->merged_extern_inline; 79 80 /* Redirect all incoming edges. */ 81 compatible_p 82 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)), 83 TREE_TYPE (TREE_TYPE (node->decl))); 84 for (e = node->callers; e; e = next) 85 { 86 next = e->next_caller; 87 e->redirect_callee (prevailing_node); 88 /* If there is a mismatch between the supposed callee return type and 89 the real one do not attempt to inline this function. 90 ??? We really need a way to match function signatures for ABI 91 compatibility and perform related promotions at inlining time. */ 92 if (!compatible_p) 93 { 94 e->inline_failed = CIF_LTO_MISMATCHED_DECLARATIONS; 95 e->call_stmt_cannot_inline_p = 1; 96 } 97 } 98 /* Redirect incomming references. */ 99 prevailing_node->clone_referring (node); 100 lto_free_function_in_decl_state_for_node (node); 101 102 if (node->decl != prevailing_node->decl) 103 node->release_body (); 104 105 /* Finally remove the replaced node. */ 106 node->remove (); 107 } 108 109 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging 110 all edges and removing the old node. */ 111 112 static void 113 lto_varpool_replace_node (varpool_node *vnode, 114 varpool_node *prevailing_node) 115 { 116 gcc_assert (!vnode->definition || prevailing_node->definition); 117 gcc_assert (!vnode->analyzed || prevailing_node->analyzed); 118 119 prevailing_node->clone_referring (vnode); 120 if (vnode->force_output) 121 prevailing_node->force_output = true; 122 if (vnode->forced_by_abi) 123 prevailing_node->forced_by_abi = true; 124 125 /* Be sure we can garbage collect the initializer. */ 126 if (DECL_INITIAL (vnode->decl) 127 && vnode->decl != prevailing_node->decl) 128 DECL_INITIAL (vnode->decl) = error_mark_node; 129 130 /* Check and report ODR violations on virtual tables. */ 131 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl)) 132 compare_virtual_tables (prevailing_node, vnode); 133 134 if (vnode->tls_model != prevailing_node->tls_model) 135 { 136 bool error = false; 137 138 /* Non-TLS and TLS never mix together. Also emulated model is not 139 compatible with anything else. */ 140 if (prevailing_node->tls_model == TLS_MODEL_NONE 141 || prevailing_node->tls_model == TLS_MODEL_EMULATED 142 || vnode->tls_model == TLS_MODEL_NONE 143 || vnode->tls_model == TLS_MODEL_EMULATED) 144 error = true; 145 /* Linked is silently supporting transitions 146 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE. 147 Do the same transitions and error out on others. */ 148 else if ((prevailing_node->tls_model == TLS_MODEL_REAL 149 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC) 150 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC 151 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC)) 152 prevailing_node->tls_model = vnode->tls_model; 153 else if ((vnode->tls_model == TLS_MODEL_REAL 154 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC) 155 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC 156 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)) 157 ; 158 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC 159 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC) 160 prevailing_node->tls_model = vnode->tls_model; 161 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC 162 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC) 163 ; 164 else 165 error = true; 166 if (error) 167 { 168 error_at (DECL_SOURCE_LOCATION (vnode->decl), 169 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]); 170 inform (DECL_SOURCE_LOCATION (prevailing_node->decl), 171 "previously defined here as %s", 172 tls_model_names [prevailing_node->tls_model]); 173 } 174 } 175 /* Finally remove the replaced node. */ 176 vnode->remove (); 177 } 178 179 /* Return non-zero if we want to output waring about T1 and T2. 180 Return value is a bitmask of reasons of violation: 181 Bit 0 indicates that types are not compatible. 182 Bit 1 indicates that types are not compatible because of C++ ODR rule. 183 If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays. 184 Bit 2 indicates that types are not ODR compatible 185 186 The interoperability rules are language specific. At present we do only 187 full checking for C++ ODR rule and for other languages we do basic check 188 that data structures are of same size and TBAA compatible. Our TBAA 189 implementation should be coarse enough so all valid type transitions 190 across different languages are allowed. 191 192 In partiucular we thus allow almost arbitrary type changes with 193 -fno-strict-aliasing which may be tough of as a feature rather than bug 194 as it allows to implement dodgy tricks in the language runtimes. 195 196 Naturally this code can be strenghtened significantly if we could track 197 down the language of origin. */ 198 199 static int 200 warn_type_compatibility_p (tree prevailing_type, tree type, 201 bool common_or_extern) 202 { 203 int lev = 0; 204 bool odr_p = odr_or_derived_type_p (prevailing_type) 205 && odr_or_derived_type_p (type); 206 207 if (prevailing_type == type) 208 return 0; 209 210 /* C++ provide a robust way to check for type compatibility via the ODR 211 rule. */ 212 if (odr_p && !odr_types_equivalent_p (prevailing_type, type)) 213 lev |= 2; 214 215 /* Function types needs special care, because types_compatible_p never 216 thinks prototype is compatible to non-prototype. */ 217 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE) 218 { 219 if (TREE_CODE (type) != TREE_CODE (prevailing_type)) 220 lev |= 1; 221 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type), 222 TREE_TYPE (type), false); 223 if (TREE_CODE (type) == METHOD_TYPE 224 && TREE_CODE (prevailing_type) == METHOD_TYPE) 225 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type), 226 TYPE_METHOD_BASETYPE (type), false); 227 if (prototype_p (prevailing_type) && prototype_p (type) 228 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type)) 229 { 230 tree parm1, parm2; 231 for (parm1 = TYPE_ARG_TYPES (prevailing_type), 232 parm2 = TYPE_ARG_TYPES (type); 233 parm1 && parm2; 234 parm1 = TREE_CHAIN (parm1), 235 parm2 = TREE_CHAIN (parm2)) 236 lev |= warn_type_compatibility_p (TREE_VALUE (parm1), 237 TREE_VALUE (parm2), false); 238 if (parm1 || parm2) 239 lev |= odr_p ? 3 : 1; 240 } 241 if (comp_type_attributes (prevailing_type, type) == 0) 242 lev |= 1; 243 return lev; 244 } 245 246 /* Get complete type. */ 247 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type); 248 type = TYPE_MAIN_VARIANT (type); 249 250 /* We cannot use types_compatible_p because we permit some changes 251 across types. For example unsigned size_t and "signed size_t" may be 252 compatible when merging C and Fortran types. */ 253 if (COMPLETE_TYPE_P (prevailing_type) 254 && COMPLETE_TYPE_P (type) 255 /* While global declarations are never variadic, we can recurse here 256 for function parameter types. */ 257 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST 258 && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST 259 && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type))) 260 { 261 /* As a special case do not warn about merging 262 int a[]; 263 and 264 int a[]={1,2,3}; 265 here the first declaration is COMMON or EXTERN 266 and sizeof(a) == sizeof (int). */ 267 if (!common_or_extern 268 || TREE_CODE (type) != ARRAY_TYPE 269 || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type))) 270 lev |= 1; 271 } 272 273 /* Verify TBAA compatibility. Take care of alias set 0 and the fact that 274 we make ptr_type_node to TBAA compatible with every other type. */ 275 if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type)) 276 { 277 alias_set_type set1 = get_alias_set (type); 278 alias_set_type set2 = get_alias_set (prevailing_type); 279 280 if (set1 && set2 && set1 != set2) 281 { 282 tree t1 = type, t2 = prevailing_type; 283 284 /* Alias sets of arrays with aliased components are the same as alias 285 sets of the inner types. */ 286 while (TREE_CODE (t1) == ARRAY_TYPE 287 && !TYPE_NONALIASED_COMPONENT (t1) 288 && TREE_CODE (t2) == ARRAY_TYPE 289 && !TYPE_NONALIASED_COMPONENT (t2)) 290 { 291 t1 = TREE_TYPE (t1); 292 t2 = TREE_TYPE (t2); 293 } 294 if ((!POINTER_TYPE_P (t1) || !POINTER_TYPE_P (t2)) 295 || (set1 != TYPE_ALIAS_SET (ptr_type_node) 296 && set2 != TYPE_ALIAS_SET (ptr_type_node))) 297 lev |= 5; 298 } 299 } 300 301 return lev; 302 } 303 304 /* Merge two variable or function symbol table entries PREVAILING and ENTRY. 305 Return false if the symbols are not fully compatible and a diagnostic 306 should be emitted. */ 307 308 static bool 309 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry) 310 { 311 tree prevailing_decl = prevailing->decl; 312 tree decl = entry->decl; 313 314 if (prevailing_decl == decl) 315 return true; 316 317 if (TREE_CODE (decl) != TREE_CODE (prevailing_decl)) 318 return false; 319 320 /* Merge decl state in both directions, we may still end up using 321 the new decl. */ 322 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl); 323 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl); 324 325 /* The linker may ask us to combine two incompatible symbols. 326 Detect this case and notify the caller of required diagnostics. */ 327 328 if (TREE_CODE (decl) == FUNCTION_DECL) 329 { 330 /* Merge decl state in both directions, we may still end up using 331 the new decl. */ 332 DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl); 333 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl); 334 335 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl), 336 TREE_TYPE (decl), 337 DECL_COMMON (decl) 338 || DECL_EXTERNAL (decl))) 339 return false; 340 341 return true; 342 } 343 344 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl), 345 TREE_TYPE (decl), 346 DECL_COMMON (decl) || DECL_EXTERNAL (decl))) 347 return false; 348 349 /* There is no point in comparing too many details of the decls here. 350 The type compatibility checks or the completing of types has properly 351 dealt with most issues. */ 352 353 /* The following should all not invoke fatal errors as in non-LTO 354 mode the linker wouldn't complain either. Just emit warnings. */ 355 356 /* Report a warning if user-specified alignments do not match. */ 357 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl)) 358 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl)) 359 return false; 360 361 if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl) 362 && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl))) 363 { 364 if (!DECL_COMMON (decl) && !DECL_EXTERNAL (decl)) 365 return false; 366 367 tree type = TREE_TYPE (decl); 368 369 /* For record type, check for array at the end of the structure. */ 370 if (TREE_CODE (type) == RECORD_TYPE) 371 { 372 tree field = TYPE_FIELDS (type); 373 while (DECL_CHAIN (field) != NULL_TREE) 374 field = DECL_CHAIN (field); 375 376 return TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE; 377 } 378 /* As a special case do not warn about merging 379 int a[]; 380 and 381 int a[]={1,2,3}; 382 here the first declaration is COMMON 383 and sizeof(a) == sizeof (int). */ 384 else if (TREE_CODE (type) != ARRAY_TYPE 385 || (TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))) 386 return false; 387 } 388 389 return true; 390 } 391 392 /* Return true if the symtab entry E can be replaced by another symtab 393 entry. */ 394 395 static bool 396 lto_symtab_resolve_replaceable_p (symtab_node *e) 397 { 398 if (DECL_EXTERNAL (e->decl) 399 || DECL_COMDAT (e->decl) 400 || DECL_ONE_ONLY (e->decl) 401 || DECL_WEAK (e->decl)) 402 return true; 403 404 if (TREE_CODE (e->decl) == VAR_DECL) 405 return (DECL_COMMON (e->decl) 406 || (!flag_no_common && !DECL_INITIAL (e->decl))); 407 408 return false; 409 } 410 411 /* Return true, if the symbol E should be resolved by lto-symtab. 412 Those are all external symbols and all real symbols that are not static (we 413 handle renaming of static later in partitioning). */ 414 415 static bool 416 lto_symtab_symbol_p (symtab_node *e) 417 { 418 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl)) 419 return false; 420 return e->real_symbol_p (); 421 } 422 423 /* Return true if the symtab entry E can be the prevailing one. */ 424 425 static bool 426 lto_symtab_resolve_can_prevail_p (symtab_node *e) 427 { 428 if (!lto_symtab_symbol_p (e)) 429 return false; 430 431 /* The C++ frontend ends up neither setting TREE_STATIC nor 432 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC. 433 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */ 434 if (DECL_EXTERNAL (e->decl)) 435 return false; 436 437 return e->definition; 438 } 439 440 /* Resolve the symbol with the candidates in the chain *SLOT and store 441 their resolutions. */ 442 443 static symtab_node * 444 lto_symtab_resolve_symbols (symtab_node *first) 445 { 446 symtab_node *e; 447 symtab_node *prevailing = NULL; 448 449 /* Always set e->node so that edges are updated to reflect decl merging. */ 450 for (e = first; e; e = e->next_sharing_asm_name) 451 if (lto_symtab_symbol_p (e) 452 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY 453 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP 454 || e->resolution == LDPR_PREVAILING_DEF)) 455 { 456 prevailing = e; 457 break; 458 } 459 460 /* If the chain is already resolved there is nothing else to do. */ 461 if (prevailing) 462 { 463 /* Assert it's the only one. 464 GCC should silence multiple PREVAILING_DEF_IRONLY defs error 465 on COMMON symbols since it isn't error. 466 See: https://sourceware.org/bugzilla/show_bug.cgi?id=23079. */ 467 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name) 468 if (lto_symtab_symbol_p (e) 469 && !DECL_COMMON (prevailing->decl) 470 && !DECL_COMMON (e->decl) 471 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY 472 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP 473 || e->resolution == LDPR_PREVAILING_DEF)) 474 fatal_error (input_location, "multiple prevailing defs for %qE", 475 DECL_NAME (prevailing->decl)); 476 return prevailing; 477 } 478 479 /* Find the single non-replaceable prevailing symbol and 480 diagnose ODR violations. */ 481 for (e = first; e; e = e->next_sharing_asm_name) 482 { 483 if (!lto_symtab_resolve_can_prevail_p (e)) 484 continue; 485 486 /* If we have a non-replaceable definition it prevails. */ 487 if (!lto_symtab_resolve_replaceable_p (e)) 488 { 489 if (prevailing) 490 { 491 error_at (DECL_SOURCE_LOCATION (e->decl), 492 "%qD has already been defined", e->decl); 493 inform (DECL_SOURCE_LOCATION (prevailing->decl), 494 "previously defined here"); 495 } 496 prevailing = e; 497 } 498 } 499 if (prevailing) 500 return prevailing; 501 502 /* Do a second round choosing one from the replaceable prevailing decls. */ 503 for (e = first; e; e = e->next_sharing_asm_name) 504 { 505 if (!lto_symtab_resolve_can_prevail_p (e)) 506 continue; 507 508 /* Choose the first function that can prevail as prevailing. */ 509 if (TREE_CODE (e->decl) == FUNCTION_DECL) 510 { 511 prevailing = e; 512 break; 513 } 514 515 /* From variables that can prevail choose the largest one. */ 516 if (!prevailing 517 || tree_int_cst_lt (DECL_SIZE (prevailing->decl), 518 DECL_SIZE (e->decl)) 519 /* When variables are equivalent try to chose one that has useful 520 DECL_INITIAL. This makes sense for keyed vtables that are 521 DECL_EXTERNAL but initialized. In units that do not need them 522 we replace the initializer by error_mark_node to conserve 523 memory. 524 525 We know that the vtable is keyed outside the LTO unit - otherwise 526 the keyed instance would prevail. We still can preserve useful 527 info in the initializer. */ 528 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl) 529 && (DECL_INITIAL (e->decl) 530 && DECL_INITIAL (e->decl) != error_mark_node) 531 && (!DECL_INITIAL (prevailing->decl) 532 || DECL_INITIAL (prevailing->decl) == error_mark_node))) 533 prevailing = e; 534 } 535 536 return prevailing; 537 } 538 539 /* Decide if it is OK to merge DECL into PREVAILING. 540 Because we wrap most of uses of declarations in MEM_REF, we can tolerate 541 some differences but other code may inspect directly the DECL. */ 542 543 static bool 544 lto_symtab_merge_p (tree prevailing, tree decl) 545 { 546 if (TREE_CODE (prevailing) != TREE_CODE (decl)) 547 { 548 if (dump_file) 549 fprintf (dump_file, "Not merging decls; " 550 "TREE_CODE mismatch\n"); 551 return false; 552 } 553 gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl)); 554 555 if (TREE_CODE (prevailing) == FUNCTION_DECL) 556 { 557 if (fndecl_built_in_p (prevailing) != fndecl_built_in_p (decl)) 558 { 559 if (dump_file) 560 fprintf (dump_file, "Not merging decls; " 561 "DECL_BUILT_IN mismatch\n"); 562 return false; 563 } 564 if (fndecl_built_in_p (prevailing) 565 && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl) 566 || (DECL_UNCHECKED_FUNCTION_CODE (prevailing) 567 != DECL_UNCHECKED_FUNCTION_CODE (decl)))) 568 { 569 if (dump_file) 570 fprintf (dump_file, "Not merging decls; " 571 "DECL_BUILT_IN_CLASS or CODE mismatch\n"); 572 return false; 573 } 574 } 575 576 if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl)) 577 { 578 tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing)); 579 tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl)); 580 if ((prev_attr == NULL) != (attr == NULL) 581 || (prev_attr && !attribute_value_equal (prev_attr, attr))) 582 { 583 if (dump_file) 584 fprintf (dump_file, "Not merging decls; " 585 "error attribute mismatch\n"); 586 return false; 587 } 588 589 prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing)); 590 attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl)); 591 if ((prev_attr == NULL) != (attr == NULL) 592 || (prev_attr && !attribute_value_equal (prev_attr, attr))) 593 { 594 if (dump_file) 595 fprintf (dump_file, "Not merging decls; " 596 "warning attribute mismatch\n"); 597 return false; 598 } 599 600 prev_attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (prevailing)); 601 attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (decl)); 602 if ((prev_attr == NULL) != (attr == NULL)) 603 { 604 if (dump_file) 605 fprintf (dump_file, "Not merging decls; " 606 "noreturn attribute mismatch\n"); 607 return false; 608 } 609 } 610 return true; 611 } 612 613 /* Merge all decls in the symbol table chain to the prevailing decl and 614 issue diagnostics about type mismatches. If DIAGNOSED_P is true 615 do not issue further diagnostics.*/ 616 617 static void 618 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p) 619 { 620 symtab_node *prevailing; 621 symtab_node *e; 622 vec<tree> mismatches = vNULL; 623 unsigned i; 624 tree decl; 625 bool tbaa_p = false; 626 627 /* Nothing to do for a single entry. */ 628 prevailing = first; 629 if (!prevailing->next_sharing_asm_name) 630 return; 631 632 /* Try to merge each entry with the prevailing one. */ 633 symtab_node *last_prevailing = prevailing, *next; 634 for (e = prevailing->next_sharing_asm_name; e; e = next) 635 { 636 next = e->next_sharing_asm_name; 637 638 /* Skip non-LTO symbols and symbols whose declaration we already 639 visited. */ 640 if (lto_symtab_prevailing_decl (e->decl) != e->decl 641 || !lto_symtab_symbol_p (e) 642 || e->decl == prevailing->decl) 643 continue; 644 645 if (!lto_symtab_merge (prevailing, e) 646 && !diagnosed_p 647 && !DECL_ARTIFICIAL (e->decl)) 648 mismatches.safe_push (e->decl); 649 650 symtab_node *this_prevailing; 651 for (this_prevailing = prevailing; ; 652 this_prevailing = this_prevailing->next_sharing_asm_name) 653 { 654 if (this_prevailing->decl != e->decl 655 && lto_symtab_merge_p (this_prevailing->decl, e->decl)) 656 break; 657 if (this_prevailing == last_prevailing) 658 { 659 this_prevailing = NULL; 660 break; 661 } 662 } 663 664 if (this_prevailing) 665 lto_symtab_prevail_decl (this_prevailing->decl, e->decl); 666 /* Maintain LRU list: relink the new prevaililng symbol 667 just after previaling node in the chain and update last_prevailing. 668 Since the number of possible declarations of a given symbol is 669 small, this should be faster than building a hash. */ 670 else if (e == prevailing->next_sharing_asm_name) 671 last_prevailing = e; 672 else 673 { 674 if (e->next_sharing_asm_name) 675 e->next_sharing_asm_name->previous_sharing_asm_name 676 = e->previous_sharing_asm_name; 677 e->previous_sharing_asm_name->next_sharing_asm_name 678 = e->next_sharing_asm_name; 679 e->previous_sharing_asm_name = prevailing; 680 e->next_sharing_asm_name = prevailing->next_sharing_asm_name; 681 prevailing->next_sharing_asm_name->previous_sharing_asm_name = e; 682 prevailing->next_sharing_asm_name = e; 683 if (last_prevailing == prevailing) 684 last_prevailing = e; 685 } 686 } 687 if (mismatches.is_empty ()) 688 return; 689 690 /* Diagnose all mismatched re-declarations. */ 691 FOR_EACH_VEC_ELT (mismatches, i, decl) 692 { 693 /* Do not diagnose two built-in declarations, there is no useful 694 location in that case. It also happens for AVR if two built-ins 695 use the same asm name because their libgcc assembler code is the 696 same, see PR78562. */ 697 if (DECL_IS_BUILTIN (prevailing->decl) 698 && DECL_IS_BUILTIN (decl)) 699 continue; 700 701 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl), 702 TREE_TYPE (decl), 703 DECL_COMDAT (decl)); 704 if (level) 705 { 706 bool diag = false; 707 if (level & 2) 708 { 709 /* Silence warning for method and variables which belong 710 to types which already have ODR violation reported. Complaining 711 once is enough. */ 712 if (TREE_CODE (decl) != FUNCTION_DECL 713 || TREE_CODE (TREE_TYPE (decl)) != METHOD_TYPE 714 || !TYPE_METHOD_BASETYPE (TREE_TYPE (decl)) 715 || !odr_type_p (TYPE_METHOD_BASETYPE (TREE_TYPE (decl))) 716 || !odr_type_violation_reported_p 717 (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)))) 718 diag = warning_at (DECL_SOURCE_LOCATION (decl), 719 OPT_Wodr, 720 "%qD violates the C++ One Definition Rule", 721 decl); 722 } 723 if (!diag && (level & 1)) 724 diag = warning_at (DECL_SOURCE_LOCATION (decl), 725 OPT_Wlto_type_mismatch, 726 "type of %qD does not match original " 727 "declaration", decl); 728 if (diag) 729 { 730 warn_types_mismatch (TREE_TYPE (prevailing->decl), 731 TREE_TYPE (decl), 732 DECL_SOURCE_LOCATION (prevailing->decl), 733 DECL_SOURCE_LOCATION (decl)); 734 if ((level & 4) 735 && !TREE_READONLY (prevailing->decl)) 736 tbaa_p = true; 737 } 738 diagnosed_p |= diag; 739 } 740 else if ((DECL_USER_ALIGN (prevailing->decl) 741 && DECL_USER_ALIGN (decl)) 742 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl)) 743 { 744 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 745 OPT_Wlto_type_mismatch, 746 "alignment of %qD is bigger than " 747 "original declaration", decl); 748 } 749 else 750 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 751 OPT_Wlto_type_mismatch, 752 "size of %qD differ from the size of " 753 "original declaration", decl); 754 } 755 if (diagnosed_p) 756 inform (DECL_SOURCE_LOCATION (prevailing->decl), 757 "%qD was previously declared here", prevailing->decl); 758 if (tbaa_p) 759 inform (DECL_SOURCE_LOCATION (prevailing->decl), 760 "code may be misoptimized unless " 761 "%<-fno-strict-aliasing%> is used"); 762 763 mismatches.release (); 764 } 765 766 /* Helper to process the decl chain for the symbol table entry *SLOT. */ 767 768 static void 769 lto_symtab_merge_decls_1 (symtab_node *first) 770 { 771 symtab_node *e; 772 symtab_node *prevailing; 773 bool diagnosed_p = false; 774 775 if (dump_file) 776 { 777 fprintf (dump_file, "Merging nodes for %s. Candidates:\n", 778 first->asm_name ()); 779 for (e = first; e; e = e->next_sharing_asm_name) 780 if (TREE_PUBLIC (e->decl)) 781 e->dump (dump_file); 782 } 783 784 /* Compute the symbol resolutions. This is a no-op when using the 785 linker plugin and resolution was decided by the linker. */ 786 prevailing = lto_symtab_resolve_symbols (first); 787 788 /* If there's not a prevailing symbol yet it's an external reference. 789 Happens a lot during ltrans. Choose the first symbol with a 790 cgraph or a varpool node. */ 791 if (!prevailing) 792 { 793 for (prevailing = first; 794 prevailing; prevailing = prevailing->next_sharing_asm_name) 795 if (lto_symtab_symbol_p (prevailing)) 796 break; 797 if (!prevailing) 798 return; 799 /* For variables chose with a priority variant with vnode 800 attached (i.e. from unit where external declaration of 801 variable is actually used). 802 When there are multiple variants, chose one with size. 803 This is needed for C++ typeinfos, for example in 804 lto/20081204-1 there are typeifos in both units, just 805 one of them do have size. */ 806 if (TREE_CODE (prevailing->decl) == VAR_DECL) 807 { 808 for (e = prevailing->next_sharing_asm_name; 809 e; e = e->next_sharing_asm_name) 810 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl)) 811 && COMPLETE_TYPE_P (TREE_TYPE (e->decl)) 812 && lto_symtab_symbol_p (e)) 813 prevailing = e; 814 } 815 /* For functions prefer the non-builtin if one is available. */ 816 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL) 817 { 818 for (e = first; e; e = e->next_sharing_asm_name) 819 if (TREE_CODE (e->decl) == FUNCTION_DECL 820 && !fndecl_built_in_p (e->decl) 821 && lto_symtab_symbol_p (e)) 822 { 823 prevailing = e; 824 break; 825 } 826 } 827 } 828 829 symtab->symtab_prevail_in_asm_name_hash (prevailing); 830 831 /* Diagnose mismatched objects. */ 832 for (e = prevailing->next_sharing_asm_name; 833 e; e = e->next_sharing_asm_name) 834 { 835 if (TREE_CODE (prevailing->decl) 836 == TREE_CODE (e->decl)) 837 continue; 838 if (!lto_symtab_symbol_p (e)) 839 continue; 840 841 switch (TREE_CODE (prevailing->decl)) 842 { 843 case VAR_DECL: 844 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL); 845 error_at (DECL_SOURCE_LOCATION (e->decl), 846 "variable %qD redeclared as function", 847 prevailing->decl); 848 break; 849 850 case FUNCTION_DECL: 851 gcc_assert (TREE_CODE (e->decl) == VAR_DECL); 852 error_at (DECL_SOURCE_LOCATION (e->decl), 853 "function %qD redeclared as variable", 854 prevailing->decl); 855 break; 856 857 default: 858 gcc_unreachable (); 859 } 860 861 diagnosed_p = true; 862 } 863 if (diagnosed_p) 864 inform (DECL_SOURCE_LOCATION (prevailing->decl), 865 "previously declared here"); 866 867 /* Merge the chain to the single prevailing decl and diagnose 868 mismatches. */ 869 lto_symtab_merge_decls_2 (prevailing, diagnosed_p); 870 871 if (dump_file) 872 { 873 fprintf (dump_file, "After resolution:\n"); 874 for (e = prevailing; e; e = e->next_sharing_asm_name) 875 e->dump (dump_file); 876 } 877 } 878 879 /* Resolve and merge all symbol table chains to a prevailing decl. */ 880 881 void 882 lto_symtab_merge_decls (void) 883 { 884 symtab_node *node; 885 886 gcc_assert (!dump_file); 887 dump_file = dump_begin (decl_merge_dump_id, NULL); 888 889 /* Populate assembler name hash. */ 890 symtab->symtab_initialize_asm_name_hash (); 891 892 FOR_EACH_SYMBOL (node) 893 if (!node->previous_sharing_asm_name 894 && node->next_sharing_asm_name) 895 lto_symtab_merge_decls_1 (node); 896 897 if (dump_file) 898 dump_end (decl_merge_dump_id, dump_file); 899 dump_file = NULL; 900 } 901 902 /* Helper to process the decl chain for the symbol table entry *SLOT. */ 903 904 static void 905 lto_symtab_merge_symbols_1 (symtab_node *prevailing) 906 { 907 symtab_node *e; 908 symtab_node *next; 909 910 prevailing->decl->decl_with_vis.symtab_node = prevailing; 911 912 /* Replace the cgraph node of each entry with the prevailing one. */ 913 for (e = prevailing->next_sharing_asm_name; e; 914 e = next) 915 { 916 next = e->next_sharing_asm_name; 917 cgraph_node *ce = dyn_cast <cgraph_node *> (e); 918 919 if ((!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl)) 920 || (ce != NULL && ce->inlined_to)) 921 continue; 922 symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl)); 923 924 /* No matter how we are going to deal with resolution, we will ultimately 925 use prevailing definition. */ 926 if (ce) 927 ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing), 928 dyn_cast<cgraph_node *> (e)); 929 930 /* If we decided to replace the node by TO, do it. */ 931 if (e != to) 932 { 933 if (ce) 934 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to)); 935 else if (varpool_node *ve = dyn_cast <varpool_node *> (e)) 936 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to)); 937 } 938 /* Watch out for duplicated symbols for a given declaration. */ 939 else if (!e->transparent_alias 940 || !e->definition || e->get_alias_target () != to) 941 { 942 /* We got a new declaration we do not want to merge. In this case 943 get rid of the existing definition and create a transparent 944 alias. */ 945 if (ce) 946 { 947 lto_free_function_in_decl_state_for_node (ce); 948 if (!ce->weakref) 949 ce->release_body (); 950 ce->reset (); 951 symtab->call_cgraph_removal_hooks (ce); 952 } 953 else 954 { 955 DECL_INITIAL (e->decl) = error_mark_node; 956 if (e->lto_file_data) 957 { 958 lto_free_function_in_decl_state_for_node (e); 959 e->lto_file_data = NULL; 960 } 961 symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e)); 962 } 963 e->remove_all_references (); 964 e->analyzed = e->body_removed = false; 965 e->resolve_alias (prevailing, true); 966 gcc_assert (e != prevailing); 967 } 968 } 969 970 return; 971 } 972 973 /* Merge cgraph nodes according to the symbol merging done by 974 lto_symtab_merge_decls. */ 975 976 void 977 lto_symtab_merge_symbols (void) 978 { 979 symtab_node *node; 980 981 if (!flag_ltrans) 982 { 983 symtab->symtab_initialize_asm_name_hash (); 984 985 /* Do the actual merging. 986 At this point we invalidate hash translating decls into symtab nodes 987 because after removing one of duplicate decls the hash is not correcly 988 updated to the ohter dupliate. */ 989 FOR_EACH_SYMBOL (node) 990 if (lto_symtab_symbol_p (node) 991 && node->next_sharing_asm_name 992 && !node->previous_sharing_asm_name) 993 lto_symtab_merge_symbols_1 (node); 994 995 /* Resolve weakref aliases whose target are now in the compilation unit. 996 also re-populate the hash translating decls into symtab nodes*/ 997 FOR_EACH_SYMBOL (node) 998 { 999 cgraph_node *cnode, *cnode2; 1000 varpool_node *vnode; 1001 symtab_node *node2; 1002 1003 if (!node->analyzed && node->alias_target) 1004 { 1005 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target); 1006 gcc_assert (node->weakref); 1007 if (tgt) 1008 node->resolve_alias (tgt, true); 1009 } 1010 /* If the symbol was preempted outside IR, see if we want to get rid 1011 of the definition. */ 1012 if (node->analyzed 1013 && !DECL_EXTERNAL (node->decl) 1014 && (node->resolution == LDPR_PREEMPTED_REG 1015 || node->resolution == LDPR_RESOLVED_IR 1016 || node->resolution == LDPR_RESOLVED_EXEC 1017 || node->resolution == LDPR_RESOLVED_DYN)) 1018 { 1019 DECL_EXTERNAL (node->decl) = 1; 1020 /* If alias to local symbol was preempted by external definition, 1021 we know it is not pointing to the local symbol. Remove it. */ 1022 if (node->alias 1023 && !node->weakref 1024 && !node->transparent_alias 1025 && node->get_alias_target ()->binds_to_current_def_p ()) 1026 { 1027 node->alias = false; 1028 node->remove_all_references (); 1029 node->definition = false; 1030 node->analyzed = false; 1031 node->cpp_implicit_alias = false; 1032 } 1033 else if (!node->alias 1034 && node->definition 1035 && node->get_availability () <= AVAIL_INTERPOSABLE) 1036 { 1037 if ((cnode = dyn_cast <cgraph_node *> (node)) != NULL) 1038 cnode->reset (); 1039 else 1040 { 1041 node->analyzed = node->definition = false; 1042 node->remove_all_references (); 1043 } 1044 } 1045 } 1046 1047 if (!(cnode = dyn_cast <cgraph_node *> (node)) 1048 || !cnode->clone_of 1049 || cnode->clone_of->decl != cnode->decl) 1050 { 1051 /* Builtins are not merged via decl merging. It is however 1052 possible that tree merging unified the declaration. We 1053 do not want duplicate entries in symbol table. */ 1054 if (cnode && fndecl_built_in_p (node->decl) 1055 && (cnode2 = cgraph_node::get (node->decl)) 1056 && cnode2 != cnode) 1057 lto_cgraph_replace_node (cnode2, cnode); 1058 1059 /* The user defined assembler variables are also not unified by their 1060 symbol name (since it is irrelevant), but we need to unify symbol 1061 nodes if tree merging occurred. */ 1062 if ((vnode = dyn_cast <varpool_node *> (node)) 1063 && DECL_HARD_REGISTER (vnode->decl) 1064 && (node2 = symtab_node::get (vnode->decl)) 1065 && node2 != node) 1066 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2), 1067 vnode); 1068 1069 1070 /* Abstract functions may have duplicated cgraph nodes attached; 1071 remove them. */ 1072 else if (cnode && DECL_ABSTRACT_P (cnode->decl) 1073 && (cnode2 = cgraph_node::get (node->decl)) 1074 && cnode2 != cnode) 1075 cnode2->remove (); 1076 1077 node->decl->decl_with_vis.symtab_node = node; 1078 } 1079 } 1080 } 1081 } 1082 1083 /* Virtual tables may matter for code generation even if they are not 1084 directly refernced by the code because they may be used for devirtualizaiton. 1085 For this reason it is important to merge even virtual tables that have no 1086 associated symbol table entries. Without doing so we lose optimization 1087 oppurtunities by losing track of the vtable constructor. 1088 FIXME: we probably ought to introduce explicit symbol table entries for 1089 those before streaming. */ 1090 1091 tree 1092 lto_symtab_prevailing_virtual_decl (tree decl) 1093 { 1094 if (DECL_ABSTRACT_P (decl)) 1095 return decl; 1096 1097 if (type_in_anonymous_namespace_p (DECL_CONTEXT (decl))) 1098 /* There can't be any other declarations. */ 1099 return decl; 1100 1101 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl)); 1102 1103 symtab_node *n = symtab_node::get_for_asmname 1104 (DECL_ASSEMBLER_NAME (decl)); 1105 while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl)) 1106 || !DECL_VIRTUAL_P (n->decl))) 1107 n = n->next_sharing_asm_name; 1108 if (n) 1109 { 1110 /* Merge decl state in both directions, we may still end up using 1111 the other decl. */ 1112 TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl); 1113 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl); 1114 1115 if (TREE_CODE (decl) == FUNCTION_DECL) 1116 { 1117 /* Merge decl state in both directions, we may still end up using 1118 the other decl. */ 1119 DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl); 1120 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl); 1121 } 1122 lto_symtab_prevail_decl (n->decl, decl); 1123 decl = n->decl; 1124 } 1125 else 1126 symtab_node::get_create (decl); 1127 1128 return decl; 1129 } 1130