1 /* Symbol table. 2 Copyright (C) 2012-2017 Free Software Foundation, Inc. 3 Contributed by Jan Hubicka 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 "backend.h" 25 #include "target.h" 26 #include "rtl.h" 27 #include "tree.h" 28 #include "gimple.h" 29 #include "timevar.h" 30 #include "cgraph.h" 31 #include "lto-streamer.h" 32 #include "print-tree.h" 33 #include "varasm.h" 34 #include "langhooks.h" 35 #include "output.h" 36 #include "ipa-utils.h" 37 #include "calls.h" 38 #include "builtins.h" 39 40 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"}; 41 42 const char * const ld_plugin_symbol_resolution_names[]= 43 { 44 "", 45 "undef", 46 "prevailing_def", 47 "prevailing_def_ironly", 48 "preempted_reg", 49 "preempted_ir", 50 "resolved_ir", 51 "resolved_exec", 52 "resolved_dyn", 53 "prevailing_def_ironly_exp" 54 }; 55 56 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at ALIAS 57 until we find an identifier that is not itself a transparent alias. */ 58 59 static inline tree 60 ultimate_transparent_alias_target (tree alias) 61 { 62 tree target = alias; 63 64 while (IDENTIFIER_TRANSPARENT_ALIAS (target)) 65 { 66 gcc_checking_assert (TREE_CHAIN (target)); 67 target = TREE_CHAIN (target); 68 } 69 gcc_checking_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target) 70 && ! TREE_CHAIN (target)); 71 72 return target; 73 } 74 75 76 /* Hash asmnames ignoring the user specified marks. */ 77 78 hashval_t 79 symbol_table::decl_assembler_name_hash (const_tree asmname) 80 { 81 if (IDENTIFIER_POINTER (asmname)[0] == '*') 82 { 83 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1; 84 size_t ulp_len = strlen (user_label_prefix); 85 86 if (ulp_len == 0) 87 ; 88 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0) 89 decl_str += ulp_len; 90 91 return htab_hash_string (decl_str); 92 } 93 94 return htab_hash_string (IDENTIFIER_POINTER (asmname)); 95 } 96 97 /* Return true if assembler names NAME1 and NAME2 leads to the same symbol 98 name. */ 99 100 bool 101 symbol_table::assembler_names_equal_p (const char *name1, const char *name2) 102 { 103 if (name1 != name2) 104 { 105 if (name1[0] == '*') 106 { 107 size_t ulp_len = strlen (user_label_prefix); 108 109 name1 ++; 110 111 if (ulp_len == 0) 112 ; 113 else if (strncmp (name1, user_label_prefix, ulp_len) == 0) 114 name1 += ulp_len; 115 else 116 return false; 117 } 118 if (name2[0] == '*') 119 { 120 size_t ulp_len = strlen (user_label_prefix); 121 122 name2 ++; 123 124 if (ulp_len == 0) 125 ; 126 else if (strncmp (name2, user_label_prefix, ulp_len) == 0) 127 name2 += ulp_len; 128 else 129 return false; 130 } 131 return !strcmp (name1, name2); 132 } 133 return true; 134 } 135 136 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */ 137 138 bool 139 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname) 140 { 141 tree decl_asmname = DECL_ASSEMBLER_NAME (decl); 142 const char *decl_str; 143 const char *asmname_str; 144 145 if (decl_asmname == asmname) 146 return true; 147 148 decl_str = IDENTIFIER_POINTER (decl_asmname); 149 asmname_str = IDENTIFIER_POINTER (asmname); 150 return assembler_names_equal_p (decl_str, asmname_str); 151 } 152 153 154 /* Returns nonzero if P1 and P2 are equal. */ 155 156 /* Insert NODE to assembler name hash. */ 157 158 void 159 symbol_table::insert_to_assembler_name_hash (symtab_node *node, 160 bool with_clones) 161 { 162 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl)) 163 return; 164 gcc_checking_assert (!node->previous_sharing_asm_name 165 && !node->next_sharing_asm_name); 166 if (assembler_name_hash) 167 { 168 symtab_node **aslot; 169 cgraph_node *cnode; 170 tree decl = node->decl; 171 172 tree name = DECL_ASSEMBLER_NAME (node->decl); 173 174 /* C++ FE can produce decls without associated assembler name and insert 175 them to symtab to hold section or TLS information. */ 176 if (!name) 177 return; 178 179 hashval_t hash = decl_assembler_name_hash (name); 180 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT); 181 gcc_assert (*aslot != node); 182 node->next_sharing_asm_name = (symtab_node *)*aslot; 183 if (*aslot != NULL) 184 (*aslot)->previous_sharing_asm_name = node; 185 *aslot = node; 186 187 /* Update also possible inline clones sharing a decl. */ 188 cnode = dyn_cast <cgraph_node *> (node); 189 if (cnode && cnode->clones && with_clones) 190 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone) 191 if (cnode->decl == decl) 192 insert_to_assembler_name_hash (cnode, true); 193 } 194 195 } 196 197 /* Remove NODE from assembler name hash. */ 198 199 void 200 symbol_table::unlink_from_assembler_name_hash (symtab_node *node, 201 bool with_clones) 202 { 203 if (assembler_name_hash) 204 { 205 cgraph_node *cnode; 206 tree decl = node->decl; 207 208 if (node->next_sharing_asm_name) 209 node->next_sharing_asm_name->previous_sharing_asm_name 210 = node->previous_sharing_asm_name; 211 if (node->previous_sharing_asm_name) 212 { 213 node->previous_sharing_asm_name->next_sharing_asm_name 214 = node->next_sharing_asm_name; 215 } 216 else 217 { 218 tree name = DECL_ASSEMBLER_NAME (node->decl); 219 symtab_node **slot; 220 221 if (!name) 222 return; 223 224 hashval_t hash = decl_assembler_name_hash (name); 225 slot = assembler_name_hash->find_slot_with_hash (name, hash, 226 NO_INSERT); 227 gcc_assert (*slot == node); 228 if (!node->next_sharing_asm_name) 229 assembler_name_hash->clear_slot (slot); 230 else 231 *slot = node->next_sharing_asm_name; 232 } 233 node->next_sharing_asm_name = NULL; 234 node->previous_sharing_asm_name = NULL; 235 236 /* Update also possible inline clones sharing a decl. */ 237 cnode = dyn_cast <cgraph_node *> (node); 238 if (cnode && cnode->clones && with_clones) 239 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone) 240 if (cnode->decl == decl) 241 unlink_from_assembler_name_hash (cnode, true); 242 } 243 } 244 245 /* Arrange node to be first in its entry of assembler_name_hash. */ 246 247 void 248 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node) 249 { 250 unlink_from_assembler_name_hash (node, false); 251 insert_to_assembler_name_hash (node, false); 252 } 253 254 /* Initalize asm name hash unless. */ 255 256 void 257 symbol_table::symtab_initialize_asm_name_hash (void) 258 { 259 symtab_node *node; 260 if (!assembler_name_hash) 261 { 262 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10); 263 FOR_EACH_SYMBOL (node) 264 insert_to_assembler_name_hash (node, false); 265 } 266 } 267 268 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */ 269 270 void 271 symbol_table::change_decl_assembler_name (tree decl, tree name) 272 { 273 symtab_node *node = NULL; 274 275 /* We can have user ASM names on things, like global register variables, that 276 are not in the symbol table. */ 277 if ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) 278 || TREE_CODE (decl) == FUNCTION_DECL) 279 node = symtab_node::get (decl); 280 if (!DECL_ASSEMBLER_NAME_SET_P (decl)) 281 { 282 SET_DECL_ASSEMBLER_NAME (decl, name); 283 if (node) 284 insert_to_assembler_name_hash (node, true); 285 } 286 else 287 { 288 if (name == DECL_ASSEMBLER_NAME (decl)) 289 return; 290 291 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) 292 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) 293 : NULL); 294 if (node) 295 unlink_from_assembler_name_hash (node, true); 296 297 const char *old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); 298 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) 299 && DECL_RTL_SET_P (decl)) 300 warning (0, "%D renamed after being referenced in assembly", decl); 301 302 SET_DECL_ASSEMBLER_NAME (decl, name); 303 if (alias) 304 { 305 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1; 306 TREE_CHAIN (name) = alias; 307 } 308 /* If we change assembler name, also all transparent aliases must 309 be updated. There are three kinds - those having same assembler name, 310 those being renamed in varasm.c and weakref being renamed by the 311 assembler. */ 312 if (node) 313 { 314 insert_to_assembler_name_hash (node, true); 315 ipa_ref *ref; 316 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++) 317 { 318 struct symtab_node *alias = ref->referring; 319 if (alias->transparent_alias && !alias->weakref 320 && symbol_table::assembler_names_equal_p 321 (old_name, IDENTIFIER_POINTER ( 322 DECL_ASSEMBLER_NAME (alias->decl)))) 323 change_decl_assembler_name (alias->decl, name); 324 else if (alias->transparent_alias 325 && IDENTIFIER_TRANSPARENT_ALIAS (alias->decl)) 326 { 327 gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) 328 && IDENTIFIER_TRANSPARENT_ALIAS 329 (DECL_ASSEMBLER_NAME (alias->decl))); 330 331 TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) = 332 ultimate_transparent_alias_target 333 (DECL_ASSEMBLER_NAME (node->decl)); 334 } 335 #ifdef ASM_OUTPUT_WEAKREF 336 else gcc_assert (!alias->transparent_alias || alias->weakref); 337 #else 338 else gcc_assert (!alias->transparent_alias); 339 #endif 340 } 341 gcc_assert (!node->transparent_alias || !node->definition 342 || node->weakref 343 || TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) 344 || symbol_table::assembler_names_equal_p 345 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), 346 IDENTIFIER_POINTER 347 (DECL_ASSEMBLER_NAME 348 (node->get_alias_target ()->decl)))); 349 } 350 } 351 } 352 353 /* Hash sections by their names. */ 354 355 hashval_t 356 section_name_hasher::hash (section_hash_entry *n) 357 { 358 return htab_hash_string (n->name); 359 } 360 361 /* Return true if section P1 name equals to P2. */ 362 363 bool 364 section_name_hasher::equal (section_hash_entry *n1, const char *name) 365 { 366 return n1->name == name || !strcmp (n1->name, name); 367 } 368 369 /* Add node into symbol table. This function is not used directly, but via 370 cgraph/varpool node creation routines. */ 371 372 void 373 symtab_node::register_symbol (void) 374 { 375 symtab->register_symbol (this); 376 377 if (!decl->decl_with_vis.symtab_node) 378 decl->decl_with_vis.symtab_node = this; 379 380 ref_list.clear (); 381 382 /* Be sure to do this last; C++ FE might create new nodes via 383 DECL_ASSEMBLER_NAME langhook! */ 384 symtab->insert_to_assembler_name_hash (this, false); 385 } 386 387 /* Remove NODE from same comdat group. */ 388 389 void 390 symtab_node::remove_from_same_comdat_group (void) 391 { 392 if (same_comdat_group) 393 { 394 symtab_node *prev; 395 for (prev = same_comdat_group; 396 prev->same_comdat_group != this; 397 prev = prev->same_comdat_group) 398 ; 399 if (same_comdat_group == prev) 400 prev->same_comdat_group = NULL; 401 else 402 prev->same_comdat_group = same_comdat_group; 403 same_comdat_group = NULL; 404 set_comdat_group (NULL); 405 } 406 } 407 408 /* Remove node from symbol table. This function is not used directly, but via 409 cgraph/varpool node removal routines. */ 410 411 void 412 symtab_node::unregister (void) 413 { 414 remove_all_references (); 415 remove_all_referring (); 416 417 /* Remove reference to section. */ 418 set_section_for_node (NULL); 419 420 remove_from_same_comdat_group (); 421 422 symtab->unregister (this); 423 424 /* During LTO symtab merging we temporarily corrupt decl to symtab node 425 hash. */ 426 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p); 427 if (decl->decl_with_vis.symtab_node == this) 428 { 429 symtab_node *replacement_node = NULL; 430 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) 431 replacement_node = cnode->find_replacement (); 432 decl->decl_with_vis.symtab_node = replacement_node; 433 } 434 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl)) 435 symtab->unlink_from_assembler_name_hash (this, false); 436 if (in_init_priority_hash) 437 symtab->init_priority_hash->remove (this); 438 } 439 440 441 /* Remove symbol from symbol table. */ 442 443 void 444 symtab_node::remove (void) 445 { 446 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) 447 cnode->remove (); 448 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) 449 vnode->remove (); 450 } 451 452 /* Add NEW_ to the same comdat group that OLD is in. */ 453 454 void 455 symtab_node::add_to_same_comdat_group (symtab_node *old_node) 456 { 457 gcc_assert (old_node->get_comdat_group ()); 458 gcc_assert (!same_comdat_group); 459 gcc_assert (this != old_node); 460 461 set_comdat_group (old_node->get_comdat_group ()); 462 same_comdat_group = old_node; 463 if (!old_node->same_comdat_group) 464 old_node->same_comdat_group = this; 465 else 466 { 467 symtab_node *n; 468 for (n = old_node->same_comdat_group; 469 n->same_comdat_group != old_node; 470 n = n->same_comdat_group) 471 ; 472 n->same_comdat_group = this; 473 } 474 } 475 476 /* Dissolve the same_comdat_group list in which NODE resides. */ 477 478 void 479 symtab_node::dissolve_same_comdat_group_list (void) 480 { 481 symtab_node *n = this; 482 symtab_node *next; 483 484 if (!same_comdat_group) 485 return; 486 do 487 { 488 next = n->same_comdat_group; 489 n->same_comdat_group = NULL; 490 /* Clear comdat_group for comdat locals, since 491 make_decl_local doesn't. */ 492 if (!TREE_PUBLIC (n->decl)) 493 n->set_comdat_group (NULL); 494 n = next; 495 } 496 while (n != this); 497 } 498 499 /* Return printable assembler name of NODE. 500 This function is used only for debugging. When assembler name 501 is unknown go with identifier name. */ 502 503 const char * 504 symtab_node::asm_name () const 505 { 506 if (!DECL_ASSEMBLER_NAME_SET_P (decl)) 507 return name (); 508 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); 509 } 510 511 /* Return printable identifier name. */ 512 513 const char * 514 symtab_node::name () const 515 { 516 if (!DECL_NAME (decl)) 517 { 518 if (DECL_ASSEMBLER_NAME_SET_P (decl)) 519 return asm_name (); 520 else 521 return "<unnamed>"; 522 } 523 return lang_hooks.decl_printable_name (decl, 2); 524 } 525 526 /* Return ipa reference from this symtab_node to 527 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type 528 of the use. */ 529 530 ipa_ref * 531 symtab_node::create_reference (symtab_node *referred_node, 532 enum ipa_ref_use use_type) 533 { 534 return create_reference (referred_node, use_type, NULL); 535 } 536 537 538 /* Return ipa reference from this symtab_node to 539 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type 540 of the use and STMT the statement (if it exists). */ 541 542 ipa_ref * 543 symtab_node::create_reference (symtab_node *referred_node, 544 enum ipa_ref_use use_type, gimple *stmt) 545 { 546 ipa_ref *ref = NULL, *ref2 = NULL; 547 ipa_ref_list *list, *list2; 548 ipa_ref_t *old_references; 549 550 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this)); 551 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt); 552 553 list = &ref_list; 554 old_references = vec_safe_address (list->references); 555 vec_safe_grow (list->references, vec_safe_length (list->references) + 1); 556 ref = &list->references->last (); 557 558 list2 = &referred_node->ref_list; 559 560 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */ 561 if(use_type == IPA_REF_ALIAS) 562 { 563 list2->referring.safe_insert (0, ref); 564 ref->referred_index = 0; 565 566 for (unsigned int i = 1; i < list2->referring.length (); i++) 567 list2->referring[i]->referred_index = i; 568 } 569 else 570 { 571 list2->referring.safe_push (ref); 572 ref->referred_index = list2->referring.length () - 1; 573 } 574 575 ref->referring = this; 576 ref->referred = referred_node; 577 ref->stmt = stmt; 578 ref->lto_stmt_uid = 0; 579 ref->use = use_type; 580 ref->speculative = 0; 581 582 /* If vector was moved in memory, update pointers. */ 583 if (old_references != list->references->address ()) 584 { 585 int i; 586 for (i = 0; iterate_reference(i, ref2); i++) 587 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2; 588 } 589 return ref; 590 } 591 592 ipa_ref * 593 symtab_node::maybe_create_reference (tree val, gimple *stmt) 594 { 595 STRIP_NOPS (val); 596 ipa_ref_use use_type; 597 598 switch (TREE_CODE (val)) 599 { 600 case VAR_DECL: 601 use_type = IPA_REF_LOAD; 602 break; 603 case ADDR_EXPR: 604 use_type = IPA_REF_ADDR; 605 break; 606 default: 607 gcc_assert (!handled_component_p (val)); 608 return NULL; 609 } 610 611 val = get_base_var (val); 612 if (val && VAR_OR_FUNCTION_DECL_P (val)) 613 { 614 symtab_node *referred = symtab_node::get (val); 615 gcc_checking_assert (referred); 616 return create_reference (referred, use_type, stmt); 617 } 618 return NULL; 619 } 620 621 /* Clone all references from symtab NODE to this symtab_node. */ 622 623 void 624 symtab_node::clone_references (symtab_node *node) 625 { 626 ipa_ref *ref = NULL, *ref2 = NULL; 627 int i; 628 for (i = 0; node->iterate_reference (i, ref); i++) 629 { 630 bool speculative = ref->speculative; 631 unsigned int stmt_uid = ref->lto_stmt_uid; 632 633 ref2 = create_reference (ref->referred, ref->use, ref->stmt); 634 ref2->speculative = speculative; 635 ref2->lto_stmt_uid = stmt_uid; 636 } 637 } 638 639 /* Clone all referring from symtab NODE to this symtab_node. */ 640 641 void 642 symtab_node::clone_referring (symtab_node *node) 643 { 644 ipa_ref *ref = NULL, *ref2 = NULL; 645 int i; 646 for (i = 0; node->iterate_referring(i, ref); i++) 647 { 648 bool speculative = ref->speculative; 649 unsigned int stmt_uid = ref->lto_stmt_uid; 650 651 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt); 652 ref2->speculative = speculative; 653 ref2->lto_stmt_uid = stmt_uid; 654 } 655 } 656 657 /* Clone reference REF to this symtab_node and set its stmt to STMT. */ 658 659 ipa_ref * 660 symtab_node::clone_reference (ipa_ref *ref, gimple *stmt) 661 { 662 bool speculative = ref->speculative; 663 unsigned int stmt_uid = ref->lto_stmt_uid; 664 ipa_ref *ref2; 665 666 ref2 = create_reference (ref->referred, ref->use, stmt); 667 ref2->speculative = speculative; 668 ref2->lto_stmt_uid = stmt_uid; 669 return ref2; 670 } 671 672 /* Find the structure describing a reference to REFERRED_NODE 673 and associated with statement STMT. */ 674 675 ipa_ref * 676 symtab_node::find_reference (symtab_node *referred_node, 677 gimple *stmt, unsigned int lto_stmt_uid) 678 { 679 ipa_ref *r = NULL; 680 int i; 681 682 for (i = 0; iterate_reference (i, r); i++) 683 if (r->referred == referred_node 684 && !r->speculative 685 && ((stmt && r->stmt == stmt) 686 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid) 687 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid))) 688 return r; 689 return NULL; 690 } 691 692 /* Remove all references that are associated with statement STMT. */ 693 694 void 695 symtab_node::remove_stmt_references (gimple *stmt) 696 { 697 ipa_ref *r = NULL; 698 int i = 0; 699 700 while (iterate_reference (i, r)) 701 if (r->stmt == stmt) 702 r->remove_reference (); 703 else 704 i++; 705 } 706 707 /* Remove all stmt references in non-speculative references. 708 Those are not maintained during inlining & clonning. 709 The exception are speculative references that are updated along 710 with callgraph edges associated with them. */ 711 712 void 713 symtab_node::clear_stmts_in_references (void) 714 { 715 ipa_ref *r = NULL; 716 int i; 717 718 for (i = 0; iterate_reference (i, r); i++) 719 if (!r->speculative) 720 { 721 r->stmt = NULL; 722 r->lto_stmt_uid = 0; 723 } 724 } 725 726 /* Remove all references in ref list. */ 727 728 void 729 symtab_node::remove_all_references (void) 730 { 731 while (vec_safe_length (ref_list.references)) 732 ref_list.references->last ().remove_reference (); 733 vec_free (ref_list.references); 734 } 735 736 /* Remove all referring items in ref list. */ 737 738 void 739 symtab_node::remove_all_referring (void) 740 { 741 while (ref_list.referring.length ()) 742 ref_list.referring.last ()->remove_reference (); 743 ref_list.referring.release (); 744 } 745 746 /* Dump references in ref list to FILE. */ 747 748 void 749 symtab_node::dump_references (FILE *file) 750 { 751 ipa_ref *ref = NULL; 752 int i; 753 for (i = 0; iterate_reference (i, ref); i++) 754 { 755 fprintf (file, "%s/%i (%s)", 756 ref->referred->asm_name (), 757 ref->referred->order, 758 ipa_ref_use_name [ref->use]); 759 if (ref->speculative) 760 fprintf (file, " (speculative)"); 761 } 762 fprintf (file, "\n"); 763 } 764 765 /* Dump referring in list to FILE. */ 766 767 void 768 symtab_node::dump_referring (FILE *file) 769 { 770 ipa_ref *ref = NULL; 771 int i; 772 for (i = 0; iterate_referring(i, ref); i++) 773 { 774 fprintf (file, "%s/%i (%s)", 775 ref->referring->asm_name (), 776 ref->referring->order, 777 ipa_ref_use_name [ref->use]); 778 if (ref->speculative) 779 fprintf (file, " (speculative)"); 780 } 781 fprintf (file, "\n"); 782 } 783 784 static const char * const symtab_type_names[] = {"symbol", "function", "variable"}; 785 786 /* Dump base fields of symtab nodes to F. Not to be used directly. */ 787 788 void 789 symtab_node::dump_base (FILE *f) 790 { 791 static const char * const visibility_types[] = { 792 "default", "protected", "hidden", "internal" 793 }; 794 795 fprintf (f, "%s/%i (%s)", asm_name (), order, name ()); 796 dump_addr (f, " @", (void *)this); 797 fprintf (f, "\n Type: %s", symtab_type_names[type]); 798 799 if (definition) 800 fprintf (f, " definition"); 801 if (analyzed) 802 fprintf (f, " analyzed"); 803 if (alias) 804 fprintf (f, " alias"); 805 if (transparent_alias) 806 fprintf (f, " transparent_alias"); 807 if (weakref) 808 fprintf (f, " weakref"); 809 if (cpp_implicit_alias) 810 fprintf (f, " cpp_implicit_alias"); 811 if (alias_target) 812 fprintf (f, " target:%s", 813 DECL_P (alias_target) 814 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME 815 (alias_target)) 816 : IDENTIFIER_POINTER (alias_target)); 817 if (body_removed) 818 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes"); 819 fprintf (f, "\n Visibility:"); 820 if (in_other_partition) 821 fprintf (f, " in_other_partition"); 822 if (used_from_other_partition) 823 fprintf (f, " used_from_other_partition"); 824 if (force_output) 825 fprintf (f, " force_output"); 826 if (forced_by_abi) 827 fprintf (f, " forced_by_abi"); 828 if (externally_visible) 829 fprintf (f, " externally_visible"); 830 if (no_reorder) 831 fprintf (f, " no_reorder"); 832 if (resolution != LDPR_UNKNOWN) 833 fprintf (f, " %s", 834 ld_plugin_symbol_resolution_names[(int)resolution]); 835 if (TREE_ASM_WRITTEN (decl)) 836 fprintf (f, " asm_written"); 837 if (DECL_EXTERNAL (decl)) 838 fprintf (f, " external"); 839 if (TREE_PUBLIC (decl)) 840 fprintf (f, " public"); 841 if (DECL_COMMON (decl)) 842 fprintf (f, " common"); 843 if (DECL_WEAK (decl)) 844 fprintf (f, " weak"); 845 if (DECL_DLLIMPORT_P (decl)) 846 fprintf (f, " dll_import"); 847 if (DECL_COMDAT (decl)) 848 fprintf (f, " comdat"); 849 if (get_comdat_group ()) 850 fprintf (f, " comdat_group:%s", 851 IDENTIFIER_POINTER (get_comdat_group_id ())); 852 if (DECL_ONE_ONLY (decl)) 853 fprintf (f, " one_only"); 854 if (get_section ()) 855 fprintf (f, " section:%s", 856 get_section ()); 857 if (implicit_section) 858 fprintf (f," (implicit_section)"); 859 if (DECL_VISIBILITY_SPECIFIED (decl)) 860 fprintf (f, " visibility_specified"); 861 if (DECL_VISIBILITY (decl)) 862 fprintf (f, " visibility:%s", 863 visibility_types [DECL_VISIBILITY (decl)]); 864 if (DECL_VIRTUAL_P (decl)) 865 fprintf (f, " virtual"); 866 if (DECL_ARTIFICIAL (decl)) 867 fprintf (f, " artificial"); 868 if (TREE_CODE (decl) == FUNCTION_DECL) 869 { 870 if (DECL_STATIC_CONSTRUCTOR (decl)) 871 fprintf (f, " constructor"); 872 if (DECL_STATIC_DESTRUCTOR (decl)) 873 fprintf (f, " destructor"); 874 } 875 fprintf (f, "\n"); 876 877 if (same_comdat_group) 878 fprintf (f, " Same comdat group as: %s/%i\n", 879 same_comdat_group->asm_name (), 880 same_comdat_group->order); 881 if (next_sharing_asm_name) 882 fprintf (f, " next sharing asm name: %i\n", 883 next_sharing_asm_name->order); 884 if (previous_sharing_asm_name) 885 fprintf (f, " previous sharing asm name: %i\n", 886 previous_sharing_asm_name->order); 887 888 if (address_taken) 889 fprintf (f, " Address is taken.\n"); 890 if (aux) 891 { 892 fprintf (f, " Aux:"); 893 dump_addr (f, " @", (void *)aux); 894 fprintf (f, "\n"); 895 } 896 897 fprintf (f, " References: "); 898 dump_references (f); 899 fprintf (f, " Referring: "); 900 dump_referring (f); 901 if (lto_file_data) 902 fprintf (f, " Read from file: %s\n", 903 lto_file_data->file_name); 904 } 905 906 /* Dump symtab node to F. */ 907 908 void 909 symtab_node::dump (FILE *f) 910 { 911 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) 912 cnode->dump (f); 913 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) 914 vnode->dump (f); 915 } 916 917 /* Dump symbol table to F. */ 918 919 void 920 symtab_node::dump_table (FILE *f) 921 { 922 symtab_node *node; 923 fprintf (f, "Symbol table:\n\n"); 924 FOR_EACH_SYMBOL (node) 925 node->dump (f); 926 } 927 928 /* Dump symbol table to stderr. */ 929 930 DEBUG_FUNCTION void 931 symtab_node::debug_symtab (void) 932 { 933 dump_table (stderr); 934 } 935 936 937 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME. 938 Return NULL if there's no such node. */ 939 940 symtab_node * 941 symtab_node::get_for_asmname (const_tree asmname) 942 { 943 symtab_node *node; 944 945 symtab->symtab_initialize_asm_name_hash (); 946 hashval_t hash = symtab->decl_assembler_name_hash (asmname); 947 symtab_node **slot 948 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash, 949 NO_INSERT); 950 951 if (slot) 952 { 953 node = *slot; 954 return node; 955 } 956 return NULL; 957 } 958 959 /* Dump symtab node NODE to stderr. */ 960 961 DEBUG_FUNCTION void 962 symtab_node::debug (void) 963 { 964 dump (stderr); 965 } 966 967 /* Verify common part of symtab nodes. */ 968 969 DEBUG_FUNCTION bool 970 symtab_node::verify_base (void) 971 { 972 bool error_found = false; 973 symtab_node *hashed_node; 974 975 if (is_a <cgraph_node *> (this)) 976 { 977 if (TREE_CODE (decl) != FUNCTION_DECL) 978 { 979 error ("function symbol is not function"); 980 error_found = true; 981 } 982 } 983 else if (is_a <varpool_node *> (this)) 984 { 985 if (!VAR_P (decl)) 986 { 987 error ("variable symbol is not variable"); 988 error_found = true; 989 } 990 } 991 else 992 { 993 error ("node has unknown type"); 994 error_found = true; 995 } 996 997 if (symtab->state != LTO_STREAMING) 998 { 999 hashed_node = symtab_node::get (decl); 1000 if (!hashed_node) 1001 { 1002 error ("node not found node->decl->decl_with_vis.symtab_node"); 1003 error_found = true; 1004 } 1005 if (hashed_node != this 1006 && (!is_a <cgraph_node *> (this) 1007 || !dyn_cast <cgraph_node *> (this)->clone_of 1008 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl)) 1009 { 1010 error ("node differs from node->decl->decl_with_vis.symtab_node"); 1011 error_found = true; 1012 } 1013 } 1014 if (symtab->assembler_name_hash) 1015 { 1016 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl)); 1017 if (hashed_node && hashed_node->previous_sharing_asm_name) 1018 { 1019 error ("assembler name hash list corrupted"); 1020 error_found = true; 1021 } 1022 while (hashed_node) 1023 { 1024 if (hashed_node == this) 1025 break; 1026 hashed_node = hashed_node->next_sharing_asm_name; 1027 } 1028 if (!hashed_node 1029 && !(is_a <varpool_node *> (this) 1030 && DECL_HARD_REGISTER (decl))) 1031 { 1032 error ("node not found in symtab assembler name hash"); 1033 error_found = true; 1034 } 1035 } 1036 if (previous_sharing_asm_name 1037 && previous_sharing_asm_name->next_sharing_asm_name != this) 1038 { 1039 error ("double linked list of assembler names corrupted"); 1040 error_found = true; 1041 } 1042 if (body_removed && definition) 1043 { 1044 error ("node has body_removed but is definition"); 1045 error_found = true; 1046 } 1047 if (analyzed && !definition) 1048 { 1049 error ("node is analyzed but it is not a definition"); 1050 error_found = true; 1051 } 1052 if (cpp_implicit_alias && !alias) 1053 { 1054 error ("node is alias but not implicit alias"); 1055 error_found = true; 1056 } 1057 if (alias && !definition && !weakref) 1058 { 1059 error ("node is alias but not definition"); 1060 error_found = true; 1061 } 1062 if (weakref && !transparent_alias) 1063 { 1064 error ("node is weakref but not an transparent_alias"); 1065 error_found = true; 1066 } 1067 if (transparent_alias && !alias) 1068 { 1069 error ("node is transparent_alias but not an alias"); 1070 error_found = true; 1071 } 1072 if (same_comdat_group) 1073 { 1074 symtab_node *n = same_comdat_group; 1075 1076 if (!n->get_comdat_group ()) 1077 { 1078 error ("node is in same_comdat_group list but has no comdat_group"); 1079 error_found = true; 1080 } 1081 if (n->get_comdat_group () != get_comdat_group ()) 1082 { 1083 error ("same_comdat_group list across different groups"); 1084 error_found = true; 1085 } 1086 if (n->type != type) 1087 { 1088 error ("mixing different types of symbol in same comdat groups is not supported"); 1089 error_found = true; 1090 } 1091 if (n == this) 1092 { 1093 error ("node is alone in a comdat group"); 1094 error_found = true; 1095 } 1096 do 1097 { 1098 if (!n->same_comdat_group) 1099 { 1100 error ("same_comdat_group is not a circular list"); 1101 error_found = true; 1102 break; 1103 } 1104 n = n->same_comdat_group; 1105 } 1106 while (n != this); 1107 if (comdat_local_p ()) 1108 { 1109 ipa_ref *ref = NULL; 1110 1111 for (int i = 0; iterate_referring (i, ref); ++i) 1112 { 1113 if (!in_same_comdat_group_p (ref->referring)) 1114 { 1115 error ("comdat-local symbol referred to by %s outside its " 1116 "comdat", 1117 identifier_to_locale (ref->referring->name())); 1118 error_found = true; 1119 } 1120 } 1121 } 1122 } 1123 if (implicit_section && !get_section ()) 1124 { 1125 error ("implicit_section flag is set but section isn't"); 1126 error_found = true; 1127 } 1128 if (get_section () && get_comdat_group () 1129 && !implicit_section 1130 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl))) 1131 { 1132 error ("Both section and comdat group is set"); 1133 error_found = true; 1134 } 1135 /* TODO: Add string table for sections, so we do not keep holding duplicated 1136 strings. */ 1137 if (alias && definition 1138 && get_section () != get_alias_target ()->get_section () 1139 && (!get_section() 1140 || !get_alias_target ()->get_section () 1141 || strcmp (get_section(), 1142 get_alias_target ()->get_section ()))) 1143 { 1144 error ("Alias and target's section differs"); 1145 get_alias_target ()->dump (stderr); 1146 error_found = true; 1147 } 1148 if (alias && definition 1149 && get_comdat_group () != get_alias_target ()->get_comdat_group ()) 1150 { 1151 error ("Alias and target's comdat groups differs"); 1152 get_alias_target ()->dump (stderr); 1153 error_found = true; 1154 } 1155 if (transparent_alias && definition && !weakref) 1156 { 1157 symtab_node *to = get_alias_target (); 1158 const char *name1 1159 = IDENTIFIER_POINTER ( 1160 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl))); 1161 const char *name2 1162 = IDENTIFIER_POINTER ( 1163 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to->decl))); 1164 if (!symbol_table::assembler_names_equal_p (name1, name2)) 1165 { 1166 error ("Transparent alias and target's assembler names differs"); 1167 get_alias_target ()->dump (stderr); 1168 error_found = true; 1169 } 1170 } 1171 if (transparent_alias && definition 1172 && get_alias_target()->transparent_alias && get_alias_target()->analyzed) 1173 { 1174 error ("Chained transparent aliases"); 1175 get_alias_target ()->dump (stderr); 1176 error_found = true; 1177 } 1178 1179 return error_found; 1180 } 1181 1182 /* Verify consistency of NODE. */ 1183 1184 DEBUG_FUNCTION void 1185 symtab_node::verify (void) 1186 { 1187 if (seen_error ()) 1188 return; 1189 1190 timevar_push (TV_CGRAPH_VERIFY); 1191 if (cgraph_node *node = dyn_cast <cgraph_node *> (this)) 1192 node->verify_node (); 1193 else 1194 if (verify_base ()) 1195 { 1196 debug (); 1197 internal_error ("symtab_node::verify failed"); 1198 } 1199 timevar_pop (TV_CGRAPH_VERIFY); 1200 } 1201 1202 /* Verify symbol table for internal consistency. */ 1203 1204 DEBUG_FUNCTION void 1205 symtab_node::verify_symtab_nodes (void) 1206 { 1207 symtab_node *node; 1208 hash_map<tree, symtab_node *> comdat_head_map (251); 1209 1210 FOR_EACH_SYMBOL (node) 1211 { 1212 node->verify (); 1213 if (node->get_comdat_group ()) 1214 { 1215 symtab_node **entry, *s; 1216 bool existed; 1217 1218 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (), 1219 &existed); 1220 if (!existed) 1221 *entry = node; 1222 else if (!DECL_EXTERNAL (node->decl)) 1223 { 1224 for (s = (*entry)->same_comdat_group; 1225 s != NULL && s != node && s != *entry; 1226 s = s->same_comdat_group) 1227 ; 1228 if (!s || s == *entry) 1229 { 1230 error ("Two symbols with same comdat_group are not linked by " 1231 "the same_comdat_group list."); 1232 (*entry)->debug (); 1233 node->debug (); 1234 internal_error ("symtab_node::verify failed"); 1235 } 1236 } 1237 } 1238 } 1239 } 1240 1241 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early, 1242 but other code such as notice_global_symbol generates rtl. */ 1243 1244 void 1245 symtab_node::make_decl_local (void) 1246 { 1247 rtx rtl, symbol; 1248 1249 if (weakref) 1250 { 1251 weakref = false; 1252 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) = 0; 1253 TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) = NULL_TREE; 1254 symtab->change_decl_assembler_name 1255 (decl, DECL_ASSEMBLER_NAME (get_alias_target ()->decl)); 1256 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", 1257 DECL_ATTRIBUTES (decl)); 1258 } 1259 /* Avoid clearing comdat_groups on comdat-local decls. */ 1260 else if (TREE_PUBLIC (decl) == 0) 1261 return; 1262 1263 /* Localizing a symbol also make all its transparent aliases local. */ 1264 ipa_ref *ref; 1265 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++) 1266 { 1267 struct symtab_node *alias = ref->referring; 1268 if (alias->transparent_alias) 1269 alias->make_decl_local (); 1270 } 1271 1272 if (VAR_P (decl)) 1273 { 1274 DECL_COMMON (decl) = 0; 1275 /* ADDRESSABLE flag is not defined for public symbols. */ 1276 TREE_ADDRESSABLE (decl) = 1; 1277 TREE_STATIC (decl) = 1; 1278 } 1279 else 1280 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); 1281 1282 DECL_COMDAT (decl) = 0; 1283 DECL_WEAK (decl) = 0; 1284 DECL_EXTERNAL (decl) = 0; 1285 DECL_VISIBILITY_SPECIFIED (decl) = 0; 1286 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; 1287 TREE_PUBLIC (decl) = 0; 1288 DECL_DLLIMPORT_P (decl) = 0; 1289 if (!DECL_RTL_SET_P (decl)) 1290 return; 1291 1292 /* Update rtl flags. */ 1293 make_decl_rtl (decl); 1294 1295 rtl = DECL_RTL (decl); 1296 if (!MEM_P (rtl)) 1297 return; 1298 1299 symbol = XEXP (rtl, 0); 1300 if (GET_CODE (symbol) != SYMBOL_REF) 1301 return; 1302 1303 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl); 1304 } 1305 1306 /* Copy visibility from N. 1307 This is useful when THIS becomes a transparent alias of N. */ 1308 1309 void 1310 symtab_node::copy_visibility_from (symtab_node *n) 1311 { 1312 gcc_checking_assert (n->weakref == weakref); 1313 1314 ipa_ref *ref; 1315 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++) 1316 { 1317 struct symtab_node *alias = ref->referring; 1318 if (alias->transparent_alias) 1319 alias->copy_visibility_from (n); 1320 } 1321 1322 if (VAR_P (decl)) 1323 { 1324 DECL_COMMON (decl) = DECL_COMMON (n->decl); 1325 /* ADDRESSABLE flag is not defined for public symbols. */ 1326 if (TREE_PUBLIC (decl) && !TREE_PUBLIC (n->decl)) 1327 TREE_ADDRESSABLE (decl) = 1; 1328 TREE_STATIC (decl) = TREE_STATIC (n->decl); 1329 } 1330 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); 1331 1332 DECL_COMDAT (decl) = DECL_COMDAT (n->decl); 1333 DECL_WEAK (decl) = DECL_WEAK (n->decl); 1334 DECL_EXTERNAL (decl) = DECL_EXTERNAL (n->decl); 1335 DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (n->decl); 1336 DECL_VISIBILITY (decl) = DECL_VISIBILITY (n->decl); 1337 TREE_PUBLIC (decl) = TREE_PUBLIC (n->decl); 1338 DECL_DLLIMPORT_P (decl) = DECL_DLLIMPORT_P (n->decl); 1339 resolution = n->resolution; 1340 set_comdat_group (n->get_comdat_group ()); 1341 call_for_symbol_and_aliases (symtab_node::set_section, 1342 const_cast<char *>(n->get_section ()), true); 1343 externally_visible = n->externally_visible; 1344 if (!DECL_RTL_SET_P (decl)) 1345 return; 1346 1347 /* Update rtl flags. */ 1348 make_decl_rtl (decl); 1349 1350 rtx rtl = DECL_RTL (decl); 1351 if (!MEM_P (rtl)) 1352 return; 1353 1354 rtx symbol = XEXP (rtl, 0); 1355 if (GET_CODE (symbol) != SYMBOL_REF) 1356 return; 1357 1358 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl); 1359 } 1360 1361 /* Walk the alias chain to return the symbol NODE is alias of. 1362 If NODE is not an alias, return NODE. 1363 Assumes NODE is known to be alias. */ 1364 1365 symtab_node * 1366 symtab_node::ultimate_alias_target_1 (enum availability *availability, 1367 symtab_node *ref) 1368 { 1369 bool transparent_p = false; 1370 1371 /* To determine visibility of the target, we follow ELF semantic of aliases. 1372 Here alias is an alternative assembler name of a given definition. Its 1373 availability prevails the availability of its target (i.e. static alias of 1374 weak definition is available. 1375 1376 Transaparent alias is just alternative anme of a given symbol used within 1377 one compilation unit and is translated prior hitting the object file. It 1378 inherits the visibility of its target. 1379 Weakref is a different animal (and noweak definition is weak). 1380 1381 If we ever get into supporting targets with different semantics, a target 1382 hook will be needed here. */ 1383 1384 if (availability) 1385 { 1386 transparent_p = transparent_alias; 1387 if (!transparent_p) 1388 *availability = get_availability (ref); 1389 else 1390 *availability = AVAIL_NOT_AVAILABLE; 1391 } 1392 1393 symtab_node *node = this; 1394 while (node) 1395 { 1396 if (node->alias && node->analyzed) 1397 node = node->get_alias_target (); 1398 else 1399 { 1400 if (!availability || (!transparent_p && node->analyzed)) 1401 ; 1402 else if (node->analyzed && !node->transparent_alias) 1403 *availability = node->get_availability (ref); 1404 else 1405 *availability = AVAIL_NOT_AVAILABLE; 1406 return node; 1407 } 1408 if (node && availability && transparent_p 1409 && node->transparent_alias) 1410 { 1411 *availability = node->get_availability (ref); 1412 transparent_p = false; 1413 } 1414 } 1415 if (availability) 1416 *availability = AVAIL_NOT_AVAILABLE; 1417 return NULL; 1418 } 1419 1420 /* C++ FE sometimes change linkage flags after producing same body aliases. 1421 1422 FIXME: C++ produce implicit aliases for virtual functions and vtables that 1423 are obviously equivalent. The way it is doing so is however somewhat 1424 kludgy and interferes with the visibility code. As a result we need to 1425 copy the visibility from the target to get things right. */ 1426 1427 void 1428 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target) 1429 { 1430 if (is_a <cgraph_node *> (this)) 1431 { 1432 DECL_DECLARED_INLINE_P (decl) 1433 = DECL_DECLARED_INLINE_P (target->decl); 1434 DECL_DISREGARD_INLINE_LIMITS (decl) 1435 = DECL_DISREGARD_INLINE_LIMITS (target->decl); 1436 } 1437 /* FIXME: It is not really clear why those flags should not be copied for 1438 functions, too. */ 1439 else 1440 { 1441 DECL_WEAK (decl) = DECL_WEAK (target->decl); 1442 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl); 1443 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl); 1444 } 1445 if (TREE_PUBLIC (decl)) 1446 { 1447 tree group; 1448 1449 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl); 1450 DECL_COMDAT (decl) = DECL_COMDAT (target->decl); 1451 group = target->get_comdat_group (); 1452 set_comdat_group (group); 1453 if (group && !same_comdat_group) 1454 add_to_same_comdat_group (target); 1455 } 1456 externally_visible = target->externally_visible; 1457 } 1458 1459 /* Set section, do not recurse into aliases. 1460 When one wants to change section of a symbol and its aliases, 1461 use set_section. */ 1462 1463 void 1464 symtab_node::set_section_for_node (const char *section) 1465 { 1466 const char *current = get_section (); 1467 section_hash_entry **slot; 1468 1469 if (current == section 1470 || (current && section 1471 && !strcmp (current, section))) 1472 return; 1473 1474 if (current) 1475 { 1476 x_section->ref_count--; 1477 if (!x_section->ref_count) 1478 { 1479 hashval_t hash = htab_hash_string (x_section->name); 1480 slot = symtab->section_hash->find_slot_with_hash (x_section->name, 1481 hash, INSERT); 1482 ggc_free (x_section); 1483 symtab->section_hash->clear_slot (slot); 1484 } 1485 x_section = NULL; 1486 } 1487 if (!section) 1488 { 1489 implicit_section = false; 1490 return; 1491 } 1492 if (!symtab->section_hash) 1493 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10); 1494 slot = symtab->section_hash->find_slot_with_hash (section, 1495 htab_hash_string (section), 1496 INSERT); 1497 if (*slot) 1498 x_section = (section_hash_entry *)*slot; 1499 else 1500 { 1501 int len = strlen (section); 1502 *slot = x_section = ggc_cleared_alloc<section_hash_entry> (); 1503 x_section->name = ggc_vec_alloc<char> (len + 1); 1504 memcpy (x_section->name, section, len + 1); 1505 } 1506 x_section->ref_count++; 1507 } 1508 1509 /* Worker for set_section. */ 1510 1511 bool 1512 symtab_node::set_section (symtab_node *n, void *s) 1513 { 1514 n->set_section_for_node ((char *)s); 1515 return false; 1516 } 1517 1518 /* Set section of symbol and its aliases. */ 1519 1520 void 1521 symtab_node::set_section (const char *section) 1522 { 1523 gcc_assert (!this->alias); 1524 call_for_symbol_and_aliases 1525 (symtab_node::set_section, const_cast<char *>(section), true); 1526 } 1527 1528 /* Return the initialization priority. */ 1529 1530 priority_type 1531 symtab_node::get_init_priority () 1532 { 1533 if (!this->in_init_priority_hash) 1534 return DEFAULT_INIT_PRIORITY; 1535 1536 symbol_priority_map *h = symtab->init_priority_hash->get (this); 1537 return h ? h->init : DEFAULT_INIT_PRIORITY; 1538 } 1539 1540 /* Return the finalization priority. */ 1541 1542 priority_type 1543 cgraph_node::get_fini_priority () 1544 { 1545 if (!this->in_init_priority_hash) 1546 return DEFAULT_INIT_PRIORITY; 1547 symbol_priority_map *h = symtab->init_priority_hash->get (this); 1548 return h ? h->fini : DEFAULT_INIT_PRIORITY; 1549 } 1550 1551 /* Return the initialization and finalization priority information for 1552 DECL. If there is no previous priority information, a freshly 1553 allocated structure is returned. */ 1554 1555 symbol_priority_map * 1556 symtab_node::priority_info (void) 1557 { 1558 if (!symtab->init_priority_hash) 1559 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13); 1560 1561 bool existed; 1562 symbol_priority_map *h 1563 = &symtab->init_priority_hash->get_or_insert (this, &existed); 1564 if (!existed) 1565 { 1566 h->init = DEFAULT_INIT_PRIORITY; 1567 h->fini = DEFAULT_INIT_PRIORITY; 1568 in_init_priority_hash = true; 1569 } 1570 1571 return h; 1572 } 1573 1574 /* Set initialization priority to PRIORITY. */ 1575 1576 void 1577 symtab_node::set_init_priority (priority_type priority) 1578 { 1579 symbol_priority_map *h; 1580 1581 if (is_a <cgraph_node *> (this)) 1582 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl)); 1583 1584 if (priority == DEFAULT_INIT_PRIORITY) 1585 { 1586 gcc_assert (get_init_priority() == priority); 1587 return; 1588 } 1589 h = priority_info (); 1590 h->init = priority; 1591 } 1592 1593 /* Set fialization priority to PRIORITY. */ 1594 1595 void 1596 cgraph_node::set_fini_priority (priority_type priority) 1597 { 1598 symbol_priority_map *h; 1599 1600 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl)); 1601 1602 if (priority == DEFAULT_INIT_PRIORITY) 1603 { 1604 gcc_assert (get_fini_priority() == priority); 1605 return; 1606 } 1607 h = priority_info (); 1608 h->fini = priority; 1609 } 1610 1611 /* Worker for symtab_resolve_alias. */ 1612 1613 bool 1614 symtab_node::set_implicit_section (symtab_node *n, 1615 void *data ATTRIBUTE_UNUSED) 1616 { 1617 n->implicit_section = true; 1618 return false; 1619 } 1620 1621 /* Add reference recording that symtab node is alias of TARGET. 1622 The function can fail in the case of aliasing cycles; in this case 1623 it returns false. */ 1624 1625 bool 1626 symtab_node::resolve_alias (symtab_node *target, bool transparent) 1627 { 1628 symtab_node *n; 1629 1630 gcc_assert (!analyzed && !vec_safe_length (ref_list.references)); 1631 1632 /* Never let cycles to creep into the symbol table alias references; 1633 those will make alias walkers to be infinite. */ 1634 for (n = target; n && n->alias; 1635 n = n->analyzed ? n->get_alias_target () : NULL) 1636 if (n == this) 1637 { 1638 if (is_a <cgraph_node *> (this)) 1639 error ("function %q+D part of alias cycle", decl); 1640 else if (is_a <varpool_node *> (this)) 1641 error ("variable %q+D part of alias cycle", decl); 1642 else 1643 gcc_unreachable (); 1644 alias = false; 1645 return false; 1646 } 1647 1648 /* "analyze" the node - i.e. mark the reference. */ 1649 definition = true; 1650 alias = true; 1651 analyzed = true; 1652 transparent |= transparent_alias; 1653 transparent_alias = transparent; 1654 if (transparent) 1655 while (target->transparent_alias && target->analyzed) 1656 target = target->get_alias_target (); 1657 create_reference (target, IPA_REF_ALIAS, NULL); 1658 1659 /* Add alias into the comdat group of its target unless it is already there. */ 1660 if (same_comdat_group) 1661 remove_from_same_comdat_group (); 1662 set_comdat_group (NULL); 1663 if (target->get_comdat_group ()) 1664 add_to_same_comdat_group (target); 1665 1666 if ((get_section () != target->get_section () 1667 || target->get_comdat_group ()) && get_section () && !implicit_section) 1668 { 1669 error ("section of alias %q+D must match section of its target", decl); 1670 } 1671 call_for_symbol_and_aliases (symtab_node::set_section, 1672 const_cast<char *>(target->get_section ()), true); 1673 if (target->implicit_section) 1674 call_for_symbol_and_aliases (set_implicit_section, NULL, true); 1675 1676 /* Alias targets become redundant after alias is resolved into an reference. 1677 We do not want to keep it around or we would have to mind updating them 1678 when renaming symbols. */ 1679 alias_target = NULL; 1680 1681 if (!transparent && cpp_implicit_alias && symtab->state >= CONSTRUCTION) 1682 fixup_same_cpp_alias_visibility (target); 1683 1684 /* If alias has address taken, so does the target. */ 1685 if (address_taken) 1686 target->ultimate_alias_target ()->address_taken = true; 1687 1688 /* All non-transparent aliases of THIS are now in fact aliases of TARGET. 1689 If alias is transparent, also all transparent aliases of THIS are now 1690 aliases of TARGET. 1691 Also merge same comdat group lists. */ 1692 ipa_ref *ref; 1693 for (unsigned i = 0; iterate_direct_aliases (i, ref);) 1694 { 1695 struct symtab_node *alias_alias = ref->referring; 1696 if (alias_alias->get_comdat_group ()) 1697 { 1698 alias_alias->remove_from_same_comdat_group (); 1699 alias_alias->set_comdat_group (NULL); 1700 if (target->get_comdat_group ()) 1701 alias_alias->add_to_same_comdat_group (target); 1702 } 1703 if (!alias_alias->transparent_alias || transparent) 1704 { 1705 alias_alias->remove_all_references (); 1706 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL); 1707 } 1708 else i++; 1709 } 1710 return true; 1711 } 1712 1713 /* Worker searching noninterposable alias. */ 1714 1715 bool 1716 symtab_node::noninterposable_alias (symtab_node *node, void *data) 1717 { 1718 if (!node->transparent_alias && decl_binds_to_current_def_p (node->decl)) 1719 { 1720 symtab_node *fn = node->ultimate_alias_target (); 1721 1722 /* Ensure that the alias is well formed this may not be the case 1723 of user defined aliases and currently it is not always the case 1724 of C++ same body aliases (that is a bug). */ 1725 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl) 1726 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl) 1727 || (TREE_CODE (node->decl) == FUNCTION_DECL 1728 && flags_from_decl_or_type (node->decl) 1729 != flags_from_decl_or_type (fn->decl)) 1730 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl)) 1731 return false; 1732 *(symtab_node **)data = node; 1733 return true; 1734 } 1735 return false; 1736 } 1737 1738 /* If node can not be overwriten by static or dynamic linker to point to 1739 different definition, return NODE. Otherwise look for alias with such 1740 property and if none exists, introduce new one. */ 1741 1742 symtab_node * 1743 symtab_node::noninterposable_alias (void) 1744 { 1745 tree new_decl; 1746 symtab_node *new_node = NULL; 1747 1748 /* First try to look up existing alias or base object 1749 (if that is already non-overwritable). */ 1750 symtab_node *node = ultimate_alias_target (); 1751 gcc_assert (!node->alias && !node->weakref); 1752 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias, 1753 (void *)&new_node, true); 1754 if (new_node) 1755 return new_node; 1756 #ifndef ASM_OUTPUT_DEF 1757 /* If aliases aren't supported by the assembler, fail. */ 1758 return NULL; 1759 #endif 1760 1761 /* Otherwise create a new one. */ 1762 new_decl = copy_node (node->decl); 1763 DECL_DLLIMPORT_P (new_decl) = 0; 1764 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias"); 1765 if (TREE_CODE (new_decl) == FUNCTION_DECL) 1766 DECL_STRUCT_FUNCTION (new_decl) = NULL; 1767 DECL_INITIAL (new_decl) = NULL; 1768 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); 1769 SET_DECL_RTL (new_decl, NULL); 1770 1771 /* Update the properties. */ 1772 DECL_EXTERNAL (new_decl) = 0; 1773 TREE_PUBLIC (new_decl) = 0; 1774 DECL_COMDAT (new_decl) = 0; 1775 DECL_WEAK (new_decl) = 0; 1776 1777 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */ 1778 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl); 1779 if (TREE_CODE (new_decl) == FUNCTION_DECL) 1780 { 1781 DECL_STATIC_CONSTRUCTOR (new_decl) = 0; 1782 DECL_STATIC_DESTRUCTOR (new_decl) = 0; 1783 new_node = cgraph_node::create_alias (new_decl, node->decl); 1784 } 1785 else 1786 { 1787 TREE_READONLY (new_decl) = TREE_READONLY (node->decl); 1788 DECL_INITIAL (new_decl) = error_mark_node; 1789 new_node = varpool_node::create_alias (new_decl, node->decl); 1790 } 1791 new_node->resolve_alias (node); 1792 gcc_assert (decl_binds_to_current_def_p (new_decl) 1793 && targetm.binds_local_p (new_decl)); 1794 return new_node; 1795 } 1796 1797 /* Return true if symtab node and TARGET represents 1798 semantically equivalent symbols. */ 1799 1800 bool 1801 symtab_node::semantically_equivalent_p (symtab_node *target) 1802 { 1803 enum availability avail; 1804 symtab_node *ba; 1805 symtab_node *bb; 1806 1807 /* Equivalent functions are equivalent. */ 1808 if (decl == target->decl) 1809 return true; 1810 1811 /* If symbol is not overwritable by different implementation, 1812 walk to the base object it defines. */ 1813 ba = ultimate_alias_target (&avail); 1814 if (avail >= AVAIL_AVAILABLE) 1815 { 1816 if (target == ba) 1817 return true; 1818 } 1819 else 1820 ba = this; 1821 bb = target->ultimate_alias_target (&avail); 1822 if (avail >= AVAIL_AVAILABLE) 1823 { 1824 if (this == bb) 1825 return true; 1826 } 1827 else 1828 bb = target; 1829 return bb == ba; 1830 } 1831 1832 /* Classify symbol symtab node for partitioning. */ 1833 1834 enum symbol_partitioning_class 1835 symtab_node::get_partitioning_class (void) 1836 { 1837 /* Inline clones are always duplicated. 1838 This include external delcarations. */ 1839 cgraph_node *cnode = dyn_cast <cgraph_node *> (this); 1840 1841 if (DECL_ABSTRACT_P (decl)) 1842 return SYMBOL_EXTERNAL; 1843 1844 if (cnode && cnode->global.inlined_to) 1845 return SYMBOL_DUPLICATE; 1846 1847 /* Transparent aliases are always duplicated. */ 1848 if (transparent_alias) 1849 return definition ? SYMBOL_DUPLICATE : SYMBOL_EXTERNAL; 1850 1851 /* External declarations are external. */ 1852 if (DECL_EXTERNAL (decl)) 1853 return SYMBOL_EXTERNAL; 1854 1855 if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) 1856 { 1857 if (alias && definition && !ultimate_alias_target ()->definition) 1858 return SYMBOL_EXTERNAL; 1859 /* Constant pool references use local symbol names that can not 1860 be promoted global. We should never put into a constant pool 1861 objects that can not be duplicated across partitions. */ 1862 if (DECL_IN_CONSTANT_POOL (decl)) 1863 return SYMBOL_DUPLICATE; 1864 if (DECL_HARD_REGISTER (decl)) 1865 return SYMBOL_DUPLICATE; 1866 gcc_checking_assert (vnode->definition); 1867 } 1868 /* Functions that are cloned may stay in callgraph even if they are unused. 1869 Handle them as external; compute_ltrans_boundary take care to make 1870 proper things to happen (i.e. to make them appear in the boundary but 1871 with body streamed, so clone can me materialized). */ 1872 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition) 1873 return SYMBOL_EXTERNAL; 1874 1875 /* Linker discardable symbols are duplicated to every use unless they are 1876 keyed. */ 1877 if (DECL_ONE_ONLY (decl) 1878 && !force_output 1879 && !forced_by_abi 1880 && !used_from_object_file_p ()) 1881 return SYMBOL_DUPLICATE; 1882 1883 return SYMBOL_PARTITION; 1884 } 1885 1886 /* Return true when symbol is known to be non-zero. */ 1887 1888 bool 1889 symtab_node::nonzero_address () 1890 { 1891 /* Weakrefs may be NULL when their target is not defined. */ 1892 if (alias && weakref) 1893 { 1894 if (analyzed) 1895 { 1896 symtab_node *target = ultimate_alias_target (); 1897 1898 if (target->alias && target->weakref) 1899 return false; 1900 /* We can not recurse to target::nonzero. It is possible that the 1901 target is used only via the alias. 1902 We may walk references and look for strong use, but we do not know 1903 if this strong use will survive to final binary, so be 1904 conservative here. 1905 ??? Maybe we could do the lookup during late optimization that 1906 could be useful to eliminate the NULL pointer checks in LTO 1907 programs. */ 1908 if (target->definition && !DECL_EXTERNAL (target->decl)) 1909 return true; 1910 if (target->resolution != LDPR_UNKNOWN 1911 && target->resolution != LDPR_UNDEF 1912 && !target->can_be_discarded_p () 1913 && flag_delete_null_pointer_checks) 1914 return true; 1915 return false; 1916 } 1917 else 1918 return false; 1919 } 1920 1921 /* With !flag_delete_null_pointer_checks we assume that symbols may 1922 bind to NULL. This is on by default on embedded targets only. 1923 1924 Otherwise all non-WEAK symbols must be defined and thus non-NULL or 1925 linking fails. Important case of WEAK we want to do well are comdats. 1926 Those are handled by later check for definition. 1927 1928 When parsing, beware the cases when WEAK attribute is added later. */ 1929 if (!DECL_WEAK (decl) 1930 && flag_delete_null_pointer_checks) 1931 { 1932 refuse_visibility_changes = true; 1933 return true; 1934 } 1935 1936 /* If target is defined and not extern, we know it will be output and thus 1937 it will bind to non-NULL. 1938 Play safe for flag_delete_null_pointer_checks where weak definition maye 1939 be re-defined by NULL. */ 1940 if (definition && !DECL_EXTERNAL (decl) 1941 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl))) 1942 { 1943 if (!DECL_WEAK (decl)) 1944 refuse_visibility_changes = true; 1945 return true; 1946 } 1947 1948 /* As the last resort, check the resolution info. */ 1949 if (resolution != LDPR_UNKNOWN 1950 && resolution != LDPR_UNDEF 1951 && !can_be_discarded_p () 1952 && flag_delete_null_pointer_checks) 1953 return true; 1954 return false; 1955 } 1956 1957 /* Return 0 if symbol is known to have different address than S2, 1958 Return 1 if symbol is known to have same address as S2, 1959 return -1 otherwise. 1960 1961 If MEMORY_ACCESSED is true, assume that both memory pointer to THIS 1962 and S2 is going to be accessed. This eliminates the situations when 1963 either THIS or S2 is NULL and is seful for comparing bases when deciding 1964 about memory aliasing. */ 1965 int 1966 symtab_node::equal_address_to (symtab_node *s2, bool memory_accessed) 1967 { 1968 enum availability avail1, avail2; 1969 1970 /* A Shortcut: equivalent symbols are always equivalent. */ 1971 if (this == s2) 1972 return 1; 1973 1974 /* Unwind transparent aliases first; those are always equal to their 1975 target. */ 1976 if (this->transparent_alias && this->analyzed) 1977 return this->get_alias_target ()->equal_address_to (s2); 1978 while (s2->transparent_alias && s2->analyzed) 1979 s2 = s2->get_alias_target(); 1980 1981 if (this == s2) 1982 return 1; 1983 1984 /* For non-interposable aliases, lookup and compare their actual definitions. 1985 Also check if the symbol needs to bind to given definition. */ 1986 symtab_node *rs1 = ultimate_alias_target (&avail1); 1987 symtab_node *rs2 = s2->ultimate_alias_target (&avail2); 1988 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl); 1989 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl); 1990 bool really_binds_local1 = binds_local1; 1991 bool really_binds_local2 = binds_local2; 1992 1993 /* Addresses of vtables and virtual functions can not be used by user 1994 code and are used only within speculation. In this case we may make 1995 symbol equivalent to its alias even if interposition may break this 1996 rule. Doing so will allow us to turn speculative inlining into 1997 non-speculative more agressively. */ 1998 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE) 1999 binds_local1 = true; 2000 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE) 2001 binds_local2 = true; 2002 2003 /* If both definitions are available we know that even if they are bound 2004 to other unit they must be defined same way and therefore we can use 2005 equivalence test. */ 2006 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE) 2007 binds_local1 = binds_local2 = true; 2008 2009 if (binds_local1 && binds_local2 && rs1 == rs2) 2010 { 2011 /* We made use of the fact that alias is not weak. */ 2012 if (rs1 != this) 2013 refuse_visibility_changes = true; 2014 if (rs2 != s2) 2015 s2->refuse_visibility_changes = true; 2016 return 1; 2017 } 2018 2019 /* If both symbols may resolve to NULL, we can not really prove them 2020 different. */ 2021 if (!memory_accessed && !nonzero_address () && !s2->nonzero_address ()) 2022 return -1; 2023 2024 /* Except for NULL, functions and variables never overlap. */ 2025 if (TREE_CODE (decl) != TREE_CODE (s2->decl)) 2026 return 0; 2027 2028 /* If one of the symbols is unresolved alias, punt. */ 2029 if (rs1->alias || rs2->alias) 2030 return -1; 2031 2032 /* If we have a non-interposale definition of at least one of the symbols 2033 and the other symbol is different, we know other unit can not interpose 2034 it to the first symbol; all aliases of the definition needs to be 2035 present in the current unit. */ 2036 if (((really_binds_local1 || really_binds_local2) 2037 /* If we have both definitions and they are different, we know they 2038 will be different even in units they binds to. */ 2039 || (binds_local1 && binds_local2)) 2040 && rs1 != rs2) 2041 { 2042 /* We make use of the fact that one symbol is not alias of the other 2043 and that the definition is non-interposable. */ 2044 refuse_visibility_changes = true; 2045 s2->refuse_visibility_changes = true; 2046 rs1->refuse_visibility_changes = true; 2047 rs2->refuse_visibility_changes = true; 2048 return 0; 2049 } 2050 2051 /* TODO: Alias oracle basically assume that addresses of global variables 2052 are different unless they are declared as alias of one to another while 2053 the code folding comparsions doesn't. 2054 We probably should be consistent and use this fact here, too, but for 2055 the moment return false only when we are called from the alias oracle. */ 2056 2057 return memory_accessed && rs1 != rs2 ? 0 : -1; 2058 } 2059 2060 /* Worker for call_for_symbol_and_aliases. */ 2061 2062 bool 2063 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *, 2064 void *), 2065 void *data, 2066 bool include_overwritable) 2067 { 2068 ipa_ref *ref; 2069 FOR_EACH_ALIAS (this, ref) 2070 { 2071 symtab_node *alias = ref->referring; 2072 if (include_overwritable 2073 || alias->get_availability () > AVAIL_INTERPOSABLE) 2074 if (alias->call_for_symbol_and_aliases (callback, data, 2075 include_overwritable)) 2076 return true; 2077 } 2078 return false; 2079 } 2080 2081 /* Return true if address of N is possibly compared. */ 2082 2083 static bool 2084 address_matters_1 (symtab_node *n, void *) 2085 { 2086 struct ipa_ref *ref; 2087 2088 if (!n->address_can_be_compared_p ()) 2089 return false; 2090 if (n->externally_visible || n->force_output) 2091 return true; 2092 2093 for (unsigned int i = 0; n->iterate_referring (i, ref); i++) 2094 if (ref->address_matters_p ()) 2095 return true; 2096 return false; 2097 } 2098 2099 /* Return true if symbol's address may possibly be compared to other 2100 symbol's address. */ 2101 2102 bool 2103 symtab_node::address_matters_p () 2104 { 2105 gcc_assert (!alias); 2106 return call_for_symbol_and_aliases (address_matters_1, NULL, true); 2107 } 2108 2109 /* Return true if symbol's alignment may be increased. */ 2110 2111 bool 2112 symtab_node::can_increase_alignment_p (void) 2113 { 2114 symtab_node *target = ultimate_alias_target (); 2115 2116 /* For now support only variables. */ 2117 if (!VAR_P (decl)) 2118 return false; 2119 2120 /* With -fno-toplevel-reorder we may have already output the constant. */ 2121 if (TREE_ASM_WRITTEN (target->decl)) 2122 return false; 2123 2124 /* If target is already placed in an anchor, we can not touch its 2125 alignment. */ 2126 if (DECL_RTL_SET_P (target->decl) 2127 && MEM_P (DECL_RTL (target->decl)) 2128 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0))) 2129 return false; 2130 2131 /* Constant pool entries may be shared. */ 2132 if (DECL_IN_CONSTANT_POOL (target->decl)) 2133 return false; 2134 2135 /* We cannot change alignment of symbols that may bind to symbols 2136 in other translation unit that may contain a definition with lower 2137 alignment. */ 2138 if (!decl_binds_to_current_def_p (decl)) 2139 return false; 2140 2141 /* When compiling partition, be sure the symbol is not output by other 2142 partition. */ 2143 if (flag_ltrans 2144 && (target->in_other_partition 2145 || target->get_partitioning_class () == SYMBOL_DUPLICATE)) 2146 return false; 2147 2148 /* Do not override the alignment as specified by the ABI when the used 2149 attribute is set. */ 2150 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl)) 2151 return false; 2152 2153 /* Do not override explicit alignment set by the user when an explicit 2154 section name is also used. This is a common idiom used by many 2155 software projects. */ 2156 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section) 2157 return false; 2158 2159 return true; 2160 } 2161 2162 /* Worker for symtab_node::increase_alignment. */ 2163 2164 static bool 2165 increase_alignment_1 (symtab_node *n, void *v) 2166 { 2167 unsigned int align = (size_t)v; 2168 if (DECL_ALIGN (n->decl) < align 2169 && n->can_increase_alignment_p ()) 2170 { 2171 SET_DECL_ALIGN (n->decl, align); 2172 DECL_USER_ALIGN (n->decl) = 1; 2173 } 2174 return false; 2175 } 2176 2177 /* Increase alignment of THIS to ALIGN. */ 2178 2179 void 2180 symtab_node::increase_alignment (unsigned int align) 2181 { 2182 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT); 2183 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1, 2184 (void *)(size_t) align, 2185 true); 2186 gcc_assert (DECL_ALIGN (decl) >= align); 2187 } 2188 2189 /* Helper for symtab_node::definition_alignment. */ 2190 2191 static bool 2192 get_alignment_1 (symtab_node *n, void *v) 2193 { 2194 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl)); 2195 return false; 2196 } 2197 2198 /* Return desired alignment of the definition. This is NOT alignment useful 2199 to access THIS, because THIS may be interposable and DECL_ALIGN should 2200 be used instead. It however must be guaranteed when output definition 2201 of THIS. */ 2202 2203 unsigned int 2204 symtab_node::definition_alignment () 2205 { 2206 unsigned int align = 0; 2207 gcc_assert (!alias); 2208 call_for_symbol_and_aliases (get_alignment_1, &align, true); 2209 return align; 2210 } 2211 2212 /* Return symbol used to separate symbol name from suffix. */ 2213 2214 char 2215 symbol_table::symbol_suffix_separator () 2216 { 2217 #ifndef NO_DOT_IN_LABEL 2218 return '.'; 2219 #elif !defined NO_DOLLAR_IN_LABEL 2220 return '$'; 2221 #else 2222 return '_'; 2223 #endif 2224 } 2225 2226 /* Return true when references to this symbol from REF must bind to current 2227 definition in final executable. */ 2228 2229 bool 2230 symtab_node::binds_to_current_def_p (symtab_node *ref) 2231 { 2232 if (!definition) 2233 return false; 2234 if (transparent_alias) 2235 return definition 2236 && get_alias_target()->binds_to_current_def_p (ref); 2237 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl))) 2238 return false; 2239 if (decl_binds_to_current_def_p (decl)) 2240 return true; 2241 2242 /* Inline clones always binds locally. */ 2243 cgraph_node *cnode = dyn_cast <cgraph_node *> (this); 2244 if (cnode && cnode->global.inlined_to) 2245 return true; 2246 2247 if (DECL_EXTERNAL (decl)) 2248 return false; 2249 2250 gcc_assert (externally_visible); 2251 2252 if (ref) 2253 { 2254 cgraph_node *cref = dyn_cast <cgraph_node *> (ref); 2255 if (cref) 2256 ref = cref->global.inlined_to; 2257 } 2258 2259 /* If this is a reference from symbol itself and there are no aliases, we 2260 may be sure that the symbol was not interposed by something else because 2261 the symbol itself would be unreachable otherwise. This is important 2262 to optimize recursive functions well. 2263 2264 This assumption may be broken by inlining: if symbol is interposable 2265 but the body is available (i.e. declared inline), inliner may make 2266 the body reachable even with interposition. */ 2267 if (this == ref && !has_aliases_p () 2268 && (!cnode 2269 || symtab->state >= IPA_SSA_AFTER_INLINING 2270 || get_availability () >= AVAIL_INTERPOSABLE)) 2271 return true; 2272 2273 2274 /* References within one comdat group are always bound in a group. */ 2275 if (ref 2276 && symtab->state >= IPA_SSA_AFTER_INLINING 2277 && get_comdat_group () 2278 && get_comdat_group () == ref->get_comdat_group ()) 2279 return true; 2280 2281 return false; 2282 } 2283 2284 /* Return true if symbol should be output to the symbol table. */ 2285 2286 bool 2287 symtab_node::output_to_lto_symbol_table_p (void) 2288 { 2289 /* Only externally visible symbols matter. */ 2290 if (!TREE_PUBLIC (decl)) 2291 return false; 2292 if (!real_symbol_p ()) 2293 return false; 2294 /* FIXME: variables probably should not be considered as real symbols at 2295 first place. */ 2296 if (VAR_P (decl) && DECL_HARD_REGISTER (decl)) 2297 return false; 2298 /* FIXME: Builtins corresponding to real functions probably should have 2299 symbol table entries. */ 2300 if (is_builtin_fn (decl)) 2301 return false; 2302 2303 /* We have real symbol that should be in symbol table. However try to trim 2304 down the refernces to libraries bit more because linker will otherwise 2305 bring unnecesary object files into the final link. 2306 FIXME: The following checks can easily be confused i.e. by self recursive 2307 function or self-referring variable. */ 2308 2309 /* We keep external functions in symtab for sake of inlining 2310 and devirtualization. We do not want to see them in symbol table as 2311 references unless they are really used. */ 2312 cgraph_node *cnode = dyn_cast <cgraph_node *> (this); 2313 if (cnode && (!definition || DECL_EXTERNAL (decl)) 2314 && cnode->callers) 2315 return true; 2316 2317 /* Ignore all references from external vars initializers - they are not really 2318 part of the compilation unit until they are used by folding. Some symbols, 2319 like references to external construction vtables can not be referred to at 2320 all. We decide this at can_refer_decl_in_current_unit_p. */ 2321 if (!definition || DECL_EXTERNAL (decl)) 2322 { 2323 int i; 2324 struct ipa_ref *ref; 2325 for (i = 0; iterate_referring (i, ref); i++) 2326 { 2327 if (ref->use == IPA_REF_ALIAS) 2328 continue; 2329 if (is_a <cgraph_node *> (ref->referring)) 2330 return true; 2331 if (!DECL_EXTERNAL (ref->referring->decl)) 2332 return true; 2333 } 2334 return false; 2335 } 2336 return true; 2337 } 2338