2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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
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
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/>. */
23 #include "coretypes.h"
27 #include "print-tree.h"
32 #include "tree-inline.h"
33 #include "langhooks.h"
37 #include "diagnostic.h"
39 #include "lto-streamer.h"
42 const char * const ld_plugin_symbol_resolution_names
[]=
47 "prevailing_def_ironly",
53 "prevailing_def_ironly_exp"
56 /* Hash table used to convert declarations into nodes. */
57 static GTY((param_is (symtab_node
))) htab_t symtab_hash
;
58 /* Hash table used to convert assembler names into nodes. */
59 static GTY((param_is (symtab_node
))) htab_t assembler_name_hash
;
61 /* Linked list of symbol table nodes. */
62 symtab_node
*symtab_nodes
;
64 /* The order index of the next symtab node to be created. This is
65 used so that we can sort the cgraph nodes in order by when we saw
66 them, to support -fno-toplevel-reorder. */
69 /* Returns a hash code for P. */
72 hash_node (const void *p
)
74 const symtab_node
*n
= (const symtab_node
*) p
;
75 return (hashval_t
) DECL_UID (n
->decl
);
79 /* Returns nonzero if P1 and P2 are equal. */
82 eq_node (const void *p1
, const void *p2
)
84 const symtab_node
*n1
= (const symtab_node
*) p1
;
85 const symtab_node
*n2
= (const symtab_node
*) p2
;
86 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
89 /* Hash asmnames ignoring the user specified marks. */
92 decl_assembler_name_hash (const_tree asmname
)
94 if (IDENTIFIER_POINTER (asmname
)[0] == '*')
96 const char *decl_str
= IDENTIFIER_POINTER (asmname
) + 1;
97 size_t ulp_len
= strlen (user_label_prefix
);
101 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
104 return htab_hash_string (decl_str
);
107 return htab_hash_string (IDENTIFIER_POINTER (asmname
));
111 /* Returns a hash code for P. */
114 hash_node_by_assembler_name (const void *p
)
116 const symtab_node
*n
= (const symtab_node
*) p
;
117 return (hashval_t
) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n
->decl
));
120 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
123 decl_assembler_name_equal (tree decl
, const_tree asmname
)
125 tree decl_asmname
= DECL_ASSEMBLER_NAME (decl
);
126 const char *decl_str
;
127 const char *asmname_str
;
130 if (decl_asmname
== asmname
)
133 decl_str
= IDENTIFIER_POINTER (decl_asmname
);
134 asmname_str
= IDENTIFIER_POINTER (asmname
);
137 /* If the target assembler name was set by the user, things are trickier.
138 We have a leading '*' to begin with. After that, it's arguable what
139 is the correct thing to do with -fleading-underscore. Arguably, we've
140 historically been doing the wrong thing in assemble_alias by always
141 printing the leading underscore. Since we're not changing that, make
142 sure user_label_prefix follows the '*' before matching. */
143 if (decl_str
[0] == '*')
145 size_t ulp_len
= strlen (user_label_prefix
);
151 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
152 decl_str
+= ulp_len
, test
=true;
156 if (asmname_str
[0] == '*')
158 size_t ulp_len
= strlen (user_label_prefix
);
164 else if (strncmp (asmname_str
, user_label_prefix
, ulp_len
) == 0)
165 asmname_str
+= ulp_len
, test
=true;
172 return strcmp (decl_str
, asmname_str
) == 0;
176 /* Returns nonzero if P1 and P2 are equal. */
179 eq_assembler_name (const void *p1
, const void *p2
)
181 const symtab_node
*n1
= (const symtab_node
*) p1
;
182 const_tree name
= (const_tree
)p2
;
183 return (decl_assembler_name_equal (n1
->decl
, name
));
186 /* Insert NODE to assembler name hash. */
189 insert_to_assembler_name_hash (symtab_node
*node
, bool with_clones
)
191 if (is_a
<varpool_node
> (node
) && DECL_HARD_REGISTER (node
->decl
))
193 gcc_checking_assert (!node
->previous_sharing_asm_name
194 && !node
->next_sharing_asm_name
);
195 if (assembler_name_hash
)
198 struct cgraph_node
*cnode
;
199 tree decl
= node
->decl
;
201 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
203 aslot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
204 decl_assembler_name_hash (name
),
206 gcc_assert (*aslot
!= node
);
207 node
->next_sharing_asm_name
= (symtab_node
*)*aslot
;
209 ((symtab_node
*)*aslot
)->previous_sharing_asm_name
= node
;
212 /* Update also possible inline clones sharing a decl. */
213 cnode
= dyn_cast
<cgraph_node
> (node
);
214 if (cnode
&& cnode
->clones
&& with_clones
)
215 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
216 if (cnode
->decl
== decl
)
217 insert_to_assembler_name_hash (cnode
, true);
222 /* Remove NODE from assembler name hash. */
225 unlink_from_assembler_name_hash (symtab_node
*node
, bool with_clones
)
227 if (assembler_name_hash
)
229 struct cgraph_node
*cnode
;
230 tree decl
= node
->decl
;
232 if (node
->next_sharing_asm_name
)
233 node
->next_sharing_asm_name
->previous_sharing_asm_name
234 = node
->previous_sharing_asm_name
;
235 if (node
->previous_sharing_asm_name
)
237 node
->previous_sharing_asm_name
->next_sharing_asm_name
238 = node
->next_sharing_asm_name
;
242 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
244 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
245 decl_assembler_name_hash (name
),
247 gcc_assert (*slot
== node
);
248 if (!node
->next_sharing_asm_name
)
249 htab_clear_slot (assembler_name_hash
, slot
);
251 *slot
= node
->next_sharing_asm_name
;
253 node
->next_sharing_asm_name
= NULL
;
254 node
->previous_sharing_asm_name
= NULL
;
256 /* Update also possible inline clones sharing a decl. */
257 cnode
= dyn_cast
<cgraph_node
> (node
);
258 if (cnode
&& cnode
->clones
&& with_clones
)
259 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
260 if (cnode
->decl
== decl
)
261 unlink_from_assembler_name_hash (cnode
, true);
265 /* Arrange node to be first in its entry of assembler_name_hash. */
268 symtab_prevail_in_asm_name_hash (symtab_node
*node
)
270 unlink_from_assembler_name_hash (node
, false);
271 insert_to_assembler_name_hash (node
, false);
275 /* Add node into symbol table. This function is not used directly, but via
276 cgraph/varpool node creation routines. */
279 symtab_register_node (symtab_node
*node
)
281 struct symtab_node key
;
284 node
->next
= symtab_nodes
;
285 node
->previous
= NULL
;
287 symtab_nodes
->previous
= node
;
291 symtab_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
292 key
.decl
= node
->decl
;
293 slot
= (symtab_node
**) htab_find_slot (symtab_hash
, &key
, INSERT
);
297 ipa_empty_ref_list (&node
->ref_list
);
299 node
->order
= symtab_order
++;
301 /* Be sure to do this last; C++ FE might create new nodes via
302 DECL_ASSEMBLER_NAME langhook! */
303 insert_to_assembler_name_hash (node
, false);
306 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
310 symtab_insert_node_to_hashtable (symtab_node
*node
)
312 struct symtab_node key
;
316 symtab_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
317 key
.decl
= node
->decl
;
318 slot
= (symtab_node
**) htab_find_slot (symtab_hash
, &key
, INSERT
);
322 /* Remove node from symbol table. This function is not used directly, but via
323 cgraph/varpool node removal routines. */
326 symtab_unregister_node (symtab_node
*node
)
329 ipa_remove_all_references (&node
->ref_list
);
330 ipa_remove_all_referring (&node
->ref_list
);
332 if (node
->same_comdat_group
)
335 for (prev
= node
->same_comdat_group
;
336 prev
->same_comdat_group
!= node
;
337 prev
= prev
->same_comdat_group
)
339 if (node
->same_comdat_group
== prev
)
340 prev
->same_comdat_group
= NULL
;
342 prev
->same_comdat_group
= node
->same_comdat_group
;
343 node
->same_comdat_group
= NULL
;
347 node
->previous
->next
= node
->next
;
349 symtab_nodes
= node
->next
;
351 node
->next
->previous
= node
->previous
;
353 node
->previous
= NULL
;
355 slot
= htab_find_slot (symtab_hash
, node
, NO_INSERT
);
357 /* During LTO symtab merging we temporarily corrupt decl to symtab node
359 gcc_assert ((slot
&& *slot
) || in_lto_p
);
360 if (slot
&& *slot
&& *slot
== node
)
362 symtab_node
*replacement_node
= NULL
;
363 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
364 replacement_node
= cgraph_find_replacement_node (cnode
);
365 if (!replacement_node
)
366 htab_clear_slot (symtab_hash
, slot
);
368 *slot
= replacement_node
;
370 if (!is_a
<varpool_node
> (node
) || !DECL_HARD_REGISTER (node
->decl
))
371 unlink_from_assembler_name_hash (node
, false);
374 /* Return symbol table node associated with DECL, if any,
375 and NULL otherwise. */
378 symtab_get_node (const_tree decl
)
381 struct symtab_node key
;
383 #ifdef ENABLE_CHECKING
384 /* Check that we are called for sane type of object - functions
385 and static or external variables. */
386 gcc_checking_assert (TREE_CODE (decl
) == FUNCTION_DECL
387 || (TREE_CODE (decl
) == VAR_DECL
388 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)
395 key
.decl
= CONST_CAST2 (tree
, const_tree
, decl
);
397 slot
= (symtab_node
**) htab_find_slot (symtab_hash
, &key
,
405 /* Remove symtab NODE from the symbol table. */
408 symtab_remove_node (symtab_node
*node
)
410 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
411 cgraph_remove_node (cnode
);
412 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
413 varpool_remove_node (vnode
);
416 /* Initalize asm name hash unless. */
419 symtab_initialize_asm_name_hash (void)
422 if (!assembler_name_hash
)
424 assembler_name_hash
=
425 htab_create_ggc (10, hash_node_by_assembler_name
, eq_assembler_name
,
427 FOR_EACH_SYMBOL (node
)
428 insert_to_assembler_name_hash (node
, false);
432 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
433 Return NULL if there's no such node. */
436 symtab_node_for_asm (const_tree asmname
)
441 symtab_initialize_asm_name_hash ();
442 slot
= htab_find_slot_with_hash (assembler_name_hash
, asmname
,
443 decl_assembler_name_hash (asmname
),
448 node
= (symtab_node
*) *slot
;
454 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
457 change_decl_assembler_name (tree decl
, tree name
)
459 symtab_node
*node
= NULL
;
461 /* We can have user ASM names on things, like global register variables, that
462 are not in the symbol table. */
463 if ((TREE_CODE (decl
) == VAR_DECL
464 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
465 || TREE_CODE (decl
) == FUNCTION_DECL
)
466 node
= symtab_get_node (decl
);
467 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
469 SET_DECL_ASSEMBLER_NAME (decl
, name
);
471 insert_to_assembler_name_hash (node
, true);
475 if (name
== DECL_ASSEMBLER_NAME (decl
))
478 tree alias
= (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl
))
479 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl
))
482 unlink_from_assembler_name_hash (node
, true);
483 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
484 && DECL_RTL_SET_P (decl
))
485 warning (0, "%D renamed after being referenced in assembly", decl
);
487 SET_DECL_ASSEMBLER_NAME (decl
, name
);
490 IDENTIFIER_TRANSPARENT_ALIAS (name
) = 1;
491 TREE_CHAIN (DECL_ASSEMBLER_NAME (name
)) = alias
;
494 insert_to_assembler_name_hash (node
, true);
498 /* Add NEW_ to the same comdat group that OLD is in. */
501 symtab_add_to_same_comdat_group (symtab_node
*new_node
,
502 symtab_node
*old_node
)
504 gcc_assert (DECL_ONE_ONLY (old_node
->decl
));
505 gcc_assert (!new_node
->same_comdat_group
);
506 gcc_assert (new_node
!= old_node
);
508 DECL_COMDAT_GROUP (new_node
->decl
) = DECL_COMDAT_GROUP (old_node
->decl
);
509 new_node
->same_comdat_group
= old_node
;
510 if (!old_node
->same_comdat_group
)
511 old_node
->same_comdat_group
= new_node
;
515 for (n
= old_node
->same_comdat_group
;
516 n
->same_comdat_group
!= old_node
;
517 n
= n
->same_comdat_group
)
519 n
->same_comdat_group
= new_node
;
523 /* Dissolve the same_comdat_group list in which NODE resides. */
526 symtab_dissolve_same_comdat_group_list (symtab_node
*node
)
528 symtab_node
*n
= node
;
531 if (!node
->same_comdat_group
)
535 next
= n
->same_comdat_group
;
536 n
->same_comdat_group
= NULL
;
542 /* Return printable assembler name of NODE.
543 This function is used only for debugging. When assembler name
544 is unknown go with identifier name. */
547 symtab_node::asm_name () const
549 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
550 return lang_hooks
.decl_printable_name (decl
, 2);
551 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
554 /* Return printable identifier name. */
557 symtab_node::name () const
559 return lang_hooks
.decl_printable_name (decl
, 2);
562 static const char * const symtab_type_names
[] = {"symbol", "function", "variable"};
564 /* Dump base fields of symtab nodes. Not to be used directly. */
567 dump_symtab_base (FILE *f
, symtab_node
*node
)
569 static const char * const visibility_types
[] = {
570 "default", "protected", "hidden", "internal"
573 fprintf (f
, "%s/%i (%s)",
577 dump_addr (f
, " @", (void *)node
);
578 fprintf (f
, "\n Type: %s", symtab_type_names
[node
->type
]);
580 if (node
->definition
)
581 fprintf (f
, " definition");
583 fprintf (f
, " analyzed");
585 fprintf (f
, " alias");
587 fprintf (f
, " weakref");
588 if (node
->cpp_implicit_alias
)
589 fprintf (f
, " cpp_implicit_alias");
590 if (node
->alias_target
)
591 fprintf (f
, " target:%s",
592 DECL_P (node
->alias_target
)
593 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
594 (node
->alias_target
))
595 : IDENTIFIER_POINTER (node
->alias_target
));
596 fprintf (f
, "\n Visibility:");
597 if (node
->in_other_partition
)
598 fprintf (f
, " in_other_partition");
599 if (node
->used_from_other_partition
)
600 fprintf (f
, " used_from_other_partition");
601 if (node
->force_output
)
602 fprintf (f
, " force_output");
603 if (node
->forced_by_abi
)
604 fprintf (f
, " forced_by_abi");
605 if (node
->externally_visible
)
606 fprintf (f
, " externally_visible");
607 if (node
->resolution
!= LDPR_UNKNOWN
)
609 ld_plugin_symbol_resolution_names
[(int)node
->resolution
]);
610 if (TREE_ASM_WRITTEN (node
->decl
))
611 fprintf (f
, " asm_written");
612 if (DECL_EXTERNAL (node
->decl
))
613 fprintf (f
, " external");
614 if (TREE_PUBLIC (node
->decl
))
615 fprintf (f
, " public");
616 if (DECL_COMMON (node
->decl
))
617 fprintf (f
, " common");
618 if (DECL_WEAK (node
->decl
))
619 fprintf (f
, " weak");
620 if (DECL_DLLIMPORT_P (node
->decl
))
621 fprintf (f
, " dll_import");
622 if (DECL_COMDAT (node
->decl
))
623 fprintf (f
, " comdat");
624 if (DECL_COMDAT_GROUP (node
->decl
))
625 fprintf (f
, " comdat_group:%s",
626 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node
->decl
)));
627 if (DECL_ONE_ONLY (node
->decl
))
628 fprintf (f
, " one_only");
629 if (DECL_SECTION_NAME (node
->decl
))
630 fprintf (f
, " section_name:%s",
631 TREE_STRING_POINTER (DECL_SECTION_NAME (node
->decl
)));
632 if (DECL_VISIBILITY_SPECIFIED (node
->decl
))
633 fprintf (f
, " visibility_specified");
634 if (DECL_VISIBILITY (node
->decl
))
635 fprintf (f
, " visibility:%s",
636 visibility_types
[DECL_VISIBILITY (node
->decl
)]);
637 if (DECL_VIRTUAL_P (node
->decl
))
638 fprintf (f
, " virtual");
639 if (DECL_ARTIFICIAL (node
->decl
))
640 fprintf (f
, " artificial");
641 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
)
643 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
644 fprintf (f
, " constructor");
645 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
646 fprintf (f
, " destructor");
650 if (node
->same_comdat_group
)
651 fprintf (f
, " Same comdat group as: %s/%i\n",
652 node
->same_comdat_group
->asm_name (),
653 node
->same_comdat_group
->order
);
654 if (node
->next_sharing_asm_name
)
655 fprintf (f
, " next sharing asm name: %i\n",
656 node
->next_sharing_asm_name
->order
);
657 if (node
->previous_sharing_asm_name
)
658 fprintf (f
, " previous sharing asm name: %i\n",
659 node
->previous_sharing_asm_name
->order
);
661 if (node
->address_taken
)
662 fprintf (f
, " Address is taken.\n");
665 fprintf (f
, " Aux:");
666 dump_addr (f
, " @", (void *)node
->aux
);
669 fprintf (f
, " References: ");
670 ipa_dump_references (f
, &node
->ref_list
);
671 fprintf (f
, " Referring: ");
672 ipa_dump_referring (f
, &node
->ref_list
);
673 if (node
->lto_file_data
)
674 fprintf (f
, " Read from file: %s\n",
675 node
->lto_file_data
->file_name
);
678 /* Dump symtab node. */
681 dump_symtab_node (FILE *f
, symtab_node
*node
)
683 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
684 dump_cgraph_node (f
, cnode
);
685 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
686 dump_varpool_node (f
, vnode
);
689 /* Dump symbol table. */
692 dump_symtab (FILE *f
)
695 fprintf (f
, "Symbol table:\n\n");
696 FOR_EACH_SYMBOL (node
)
697 dump_symtab_node (f
, node
);
700 /* Dump symtab node NODE to stderr. */
703 debug_symtab_node (symtab_node
*node
)
705 dump_symtab_node (stderr
, node
);
708 /* Dump symbol table to stderr. */
713 dump_symtab (stderr
);
716 /* Verify common part of symtab nodes. */
719 verify_symtab_base (symtab_node
*node
)
721 bool error_found
= false;
722 symtab_node
*hashed_node
;
724 if (is_a
<cgraph_node
> (node
))
726 if (TREE_CODE (node
->decl
) != FUNCTION_DECL
)
728 error ("function symbol is not function");
732 else if (is_a
<varpool_node
> (node
))
734 if (TREE_CODE (node
->decl
) != VAR_DECL
)
736 error ("variable symbol is not variable");
742 error ("node has unknown type");
746 if (cgraph_state
!= CGRAPH_LTO_STREAMING
)
748 hashed_node
= symtab_get_node (node
->decl
);
751 error ("node not found in symtab decl hashtable");
754 if (hashed_node
!= node
755 && (!is_a
<cgraph_node
> (node
)
756 || !dyn_cast
<cgraph_node
> (node
)->clone_of
757 || dyn_cast
<cgraph_node
> (node
)->clone_of
->decl
760 error ("node differs from symtab decl hashtable");
764 if (assembler_name_hash
)
766 hashed_node
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (node
->decl
));
767 if (hashed_node
&& hashed_node
->previous_sharing_asm_name
)
769 error ("assembler name hash list corrupted");
774 if (hashed_node
== node
)
776 hashed_node
= hashed_node
->next_sharing_asm_name
;
779 && !(is_a
<varpool_node
> (node
)
780 || DECL_HARD_REGISTER (node
->decl
)))
782 error ("node not found in symtab assembler name hash");
786 if (node
->previous_sharing_asm_name
787 && node
->previous_sharing_asm_name
->next_sharing_asm_name
!= node
)
789 error ("double linked list of assembler names corrupted");
792 if (node
->analyzed
&& !node
->definition
)
794 error ("node is analyzed byt it is not a definition");
797 if (node
->cpp_implicit_alias
&& !node
->alias
)
799 error ("node is alias but not implicit alias");
802 if (node
->alias
&& !node
->definition
805 error ("node is alias but not definition");
808 if (node
->weakref
&& !node
->alias
)
810 error ("node is weakref but not an alias");
813 if (node
->same_comdat_group
)
815 symtab_node
*n
= node
->same_comdat_group
;
817 if (!DECL_ONE_ONLY (n
->decl
))
819 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
822 if (n
->type
!= node
->type
)
824 error ("mixing different types of symbol in same comdat groups is not supported");
829 error ("node is alone in a comdat group");
834 if (!n
->same_comdat_group
)
836 error ("same_comdat_group is not a circular list");
840 n
= n
->same_comdat_group
;
847 /* Verify consistency of NODE. */
850 verify_symtab_node (symtab_node
*node
)
855 timevar_push (TV_CGRAPH_VERIFY
);
856 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
857 verify_cgraph_node (cnode
);
859 if (verify_symtab_base (node
))
861 dump_symtab_node (stderr
, node
);
862 internal_error ("verify_symtab_node failed");
864 timevar_pop (TV_CGRAPH_VERIFY
);
867 /* Verify symbol table for internal consistency. */
873 FOR_EACH_SYMBOL (node
)
874 verify_symtab_node (node
);
877 /* Return true when RESOLUTION indicate that linker will use
878 the symbol from non-LTO object files. */
881 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution
)
883 return (resolution
== LDPR_PREVAILING_DEF
884 || resolution
== LDPR_PREEMPTED_REG
885 || resolution
== LDPR_RESOLVED_EXEC
886 || resolution
== LDPR_RESOLVED_DYN
);
889 /* Return true when NODE is known to be used from other (non-LTO) object file.
890 Known only when doing LTO via linker plugin. */
893 symtab_used_from_object_file_p (symtab_node
*node
)
895 if (!TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
))
897 if (resolution_used_from_other_file_p (node
->resolution
))
902 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
903 but other code such as notice_global_symbol generates rtl. */
906 symtab_make_decl_local (tree decl
)
910 if (TREE_CODE (decl
) == VAR_DECL
)
911 DECL_COMMON (decl
) = 0;
912 else gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
914 if (DECL_ONE_ONLY (decl
) || DECL_COMDAT (decl
))
916 DECL_SECTION_NAME (decl
) = 0;
917 DECL_COMDAT (decl
) = 0;
919 DECL_COMDAT_GROUP (decl
) = 0;
920 DECL_WEAK (decl
) = 0;
921 DECL_EXTERNAL (decl
) = 0;
922 DECL_VISIBILITY_SPECIFIED (decl
) = 0;
923 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
924 TREE_PUBLIC (decl
) = 0;
925 if (!DECL_RTL_SET_P (decl
))
928 /* Update rtl flags. */
929 make_decl_rtl (decl
);
931 rtl
= DECL_RTL (decl
);
935 symbol
= XEXP (rtl
, 0);
936 if (GET_CODE (symbol
) != SYMBOL_REF
)
939 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
942 /* Return availability of NODE. */
945 symtab_node_availability (symtab_node
*node
)
947 if (is_a
<cgraph_node
> (node
))
948 return cgraph_function_body_availability (cgraph (node
));
950 return cgraph_variable_initializer_availability (varpool (node
));
953 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
954 If NODE is not an alias, return NODE.
955 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
958 symtab_alias_ultimate_target (symtab_node
*node
, enum availability
*availability
)
960 bool weakref_p
= false;
965 *availability
= symtab_node_availability (node
);
969 /* To determine visibility of the target, we follow ELF semantic of aliases.
970 Here alias is an alternative assembler name of a given definition. Its
971 availability prevails the availability of its target (i.e. static alias of
972 weak definition is available.
974 Weakref is a different animal (and not part of ELF per se). It is just
975 alternative name of a given symbol used within one complation unit
976 and is translated prior hitting the object file. It inherits the
977 visibility of its target (i.e. weakref of non-overwritable definition
978 is non-overwritable, while weakref of weak definition is weak).
980 If we ever get into supporting targets with different semantics, a target
981 hook will be needed here. */
985 weakref_p
= node
->weakref
;
987 *availability
= symtab_node_availability (node
);
989 *availability
= AVAIL_LOCAL
;
993 if (node
->alias
&& node
->analyzed
)
994 node
= symtab_alias_target (node
);
999 else if (node
->analyzed
)
1003 enum availability a
= symtab_node_availability (node
);
1004 if (a
< *availability
)
1009 *availability
= AVAIL_NOT_AVAILABLE
;
1012 if (node
&& availability
&& weakref_p
)
1014 enum availability a
= symtab_node_availability (node
);
1015 if (a
< *availability
)
1017 weakref_p
= node
->weakref
;
1021 *availability
= AVAIL_NOT_AVAILABLE
;
1025 /* C++ FE sometimes change linkage flags after producing same body aliases.
1027 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1028 are obviously equivalent. The way it is doing so is however somewhat
1029 kludgy and interferes with the visibility code. As a result we need to
1030 copy the visibility from the target to get things right. */
1033 fixup_same_cpp_alias_visibility (symtab_node
*node
, symtab_node
*target
)
1035 if (is_a
<cgraph_node
> (node
))
1037 DECL_DECLARED_INLINE_P (node
->decl
)
1038 = DECL_DECLARED_INLINE_P (target
->decl
);
1039 DECL_DISREGARD_INLINE_LIMITS (node
->decl
)
1040 = DECL_DISREGARD_INLINE_LIMITS (target
->decl
);
1042 /* FIXME: It is not really clear why those flags should not be copied for
1046 DECL_WEAK (node
->decl
) = DECL_WEAK (target
->decl
);
1047 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (target
->decl
);
1048 DECL_VISIBILITY (node
->decl
) = DECL_VISIBILITY (target
->decl
);
1050 DECL_VIRTUAL_P (node
->decl
) = DECL_VIRTUAL_P (target
->decl
);
1051 if (TREE_PUBLIC (node
->decl
))
1053 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (target
->decl
);
1054 DECL_COMDAT (node
->decl
) = DECL_COMDAT (target
->decl
);
1055 DECL_COMDAT_GROUP (node
->decl
)
1056 = DECL_COMDAT_GROUP (target
->decl
);
1057 if (DECL_ONE_ONLY (target
->decl
)
1058 && !node
->same_comdat_group
)
1059 symtab_add_to_same_comdat_group (node
, target
);
1061 node
->externally_visible
= target
->externally_visible
;
1064 /* Add reference recording that NODE is alias of TARGET.
1065 The function can fail in the case of aliasing cycles; in this case
1066 it returns false. */
1069 symtab_resolve_alias (symtab_node
*node
, symtab_node
*target
)
1073 gcc_assert (!node
->analyzed
1074 && !vec_safe_length (node
->ref_list
.references
));
1076 /* Never let cycles to creep into the symbol table alias references;
1077 those will make alias walkers to be infinite. */
1078 for (n
= target
; n
&& n
->alias
;
1079 n
= n
->analyzed
? symtab_alias_target (n
) : NULL
)
1082 if (is_a
<cgraph_node
> (node
))
1083 error ("function %q+D part of alias cycle", node
->decl
);
1084 else if (is_a
<varpool_node
> (node
))
1085 error ("variable %q+D part of alias cycle", node
->decl
);
1088 node
->alias
= false;
1092 /* "analyze" the node - i.e. mark the reference. */
1093 node
->definition
= true;
1095 node
->analyzed
= true;
1096 ipa_record_reference (node
, target
, IPA_REF_ALIAS
, NULL
);
1098 /* Alias targets become reudndant after alias is resolved into an reference.
1099 We do not want to keep it around or we would have to mind updating them
1100 when renaming symbols. */
1101 node
->alias_target
= NULL
;
1103 if (node
->cpp_implicit_alias
&& cgraph_state
>= CGRAPH_STATE_CONSTRUCTION
)
1104 fixup_same_cpp_alias_visibility (node
, target
);
1106 /* If alias has address taken, so does the target. */
1107 if (node
->address_taken
)
1108 symtab_alias_ultimate_target (target
, NULL
)->address_taken
= true;
1112 /* Call calback on NODE and aliases associated to NODE.
1113 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1117 symtab_for_node_and_aliases (symtab_node
*node
,
1118 bool (*callback
) (symtab_node
*, void *),
1120 bool include_overwritable
)
1123 struct ipa_ref
*ref
;
1125 if (callback (node
, data
))
1127 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
1128 if (ref
->use
== IPA_REF_ALIAS
)
1130 symtab_node
*alias
= ref
->referring
;
1131 if (include_overwritable
1132 || symtab_node_availability (alias
) > AVAIL_OVERWRITABLE
)
1133 if (symtab_for_node_and_aliases (alias
, callback
, data
,
1134 include_overwritable
))
1140 /* Worker searching nonoverwritable alias. */
1143 symtab_nonoverwritable_alias_1 (symtab_node
*node
, void *data
)
1145 if (decl_binds_to_current_def_p (node
->decl
))
1147 *(symtab_node
**)data
= node
;
1153 /* If NODE can not be overwriten by static or dynamic linker to point to different
1154 definition, return NODE. Otherwise look for alias with such property and if
1155 none exists, introduce new one. */
1158 symtab_nonoverwritable_alias (symtab_node
*node
)
1161 symtab_node
*new_node
= NULL
;
1163 /* First try to look up existing alias or base object
1164 (if that is already non-overwritable). */
1165 node
= symtab_alias_ultimate_target (node
, NULL
);
1166 gcc_assert (!node
->alias
&& !node
->weakref
);
1167 symtab_for_node_and_aliases (node
, symtab_nonoverwritable_alias_1
,
1168 (void *)&new_node
, true);
1171 #ifndef ASM_OUTPUT_DEF
1172 /* If aliases aren't supported by the assembler, fail. */
1176 /* Otherwise create a new one. */
1177 new_decl
= copy_node (node
->decl
);
1178 DECL_NAME (new_decl
) = clone_function_name (node
->decl
, "localalias");
1179 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1180 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
1181 DECL_INITIAL (new_decl
) = NULL
;
1182 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
1183 SET_DECL_RTL (new_decl
, NULL
);
1185 /* Update the properties. */
1186 DECL_EXTERNAL (new_decl
) = 0;
1187 if (DECL_ONE_ONLY (node
->decl
))
1188 DECL_SECTION_NAME (new_decl
) = NULL
;
1189 DECL_COMDAT_GROUP (new_decl
) = 0;
1190 TREE_PUBLIC (new_decl
) = 0;
1191 DECL_COMDAT (new_decl
) = 0;
1192 DECL_WEAK (new_decl
) = 0;
1193 DECL_VIRTUAL_P (new_decl
) = 0;
1194 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1196 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
1197 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
1198 new_node
= cgraph_create_function_alias
1199 (new_decl
, node
->decl
);
1202 new_node
= varpool_create_variable_alias (new_decl
,
1204 symtab_resolve_alias (new_node
, node
);
1205 gcc_assert (decl_binds_to_current_def_p (new_decl
));
1209 /* Return true if A and B represents semantically equivalent symbols. */
1212 symtab_semantically_equivalent_p (symtab_node
*a
,
1215 enum availability avail
;
1219 /* Equivalent functions are equivalent. */
1220 if (a
->decl
== b
->decl
)
1223 /* If symbol is not overwritable by different implementation,
1224 walk to the base object it defines. */
1225 ba
= symtab_alias_ultimate_target (a
, &avail
);
1226 if (avail
>= AVAIL_AVAILABLE
)
1233 bb
= symtab_alias_ultimate_target (b
, &avail
);
1234 if (avail
>= AVAIL_AVAILABLE
)
1243 #include "gt-symtab.h"