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