2 Copyright (C) 2012-2014 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"
31 #include "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
37 #include "tree-inline.h"
38 #include "langhooks.h"
41 #include "diagnostic.h"
43 #include "lto-streamer.h"
46 const char * const ld_plugin_symbol_resolution_names
[]=
51 "prevailing_def_ironly",
57 "prevailing_def_ironly_exp"
61 /* Hash table used to hold sectoons. */
62 static GTY((param_is (section_hash_entry
))) htab_t section_hash
;
64 /* Hash table used to convert assembler names into nodes. */
65 static GTY((param_is (symtab_node
))) htab_t assembler_name_hash
;
67 /* Linked list of symbol table nodes. */
68 symtab_node
*symtab_nodes
;
70 /* The order index of the next symtab node to be created. This is
71 used so that we can sort the cgraph nodes in order by when we saw
72 them, to support -fno-toplevel-reorder. */
75 /* Hash asmnames ignoring the user specified marks. */
78 decl_assembler_name_hash (const_tree asmname
)
80 if (IDENTIFIER_POINTER (asmname
)[0] == '*')
82 const char *decl_str
= IDENTIFIER_POINTER (asmname
) + 1;
83 size_t ulp_len
= strlen (user_label_prefix
);
87 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
90 return htab_hash_string (decl_str
);
93 return htab_hash_string (IDENTIFIER_POINTER (asmname
));
97 /* Returns a hash code for P. */
100 hash_node_by_assembler_name (const void *p
)
102 const symtab_node
*n
= (const symtab_node
*) p
;
103 return (hashval_t
) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n
->decl
));
106 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
109 decl_assembler_name_equal (tree decl
, const_tree asmname
)
111 tree decl_asmname
= DECL_ASSEMBLER_NAME (decl
);
112 const char *decl_str
;
113 const char *asmname_str
;
116 if (decl_asmname
== asmname
)
119 decl_str
= IDENTIFIER_POINTER (decl_asmname
);
120 asmname_str
= IDENTIFIER_POINTER (asmname
);
123 /* If the target assembler name was set by the user, things are trickier.
124 We have a leading '*' to begin with. After that, it's arguable what
125 is the correct thing to do with -fleading-underscore. Arguably, we've
126 historically been doing the wrong thing in assemble_alias by always
127 printing the leading underscore. Since we're not changing that, make
128 sure user_label_prefix follows the '*' before matching. */
129 if (decl_str
[0] == '*')
131 size_t ulp_len
= strlen (user_label_prefix
);
137 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
138 decl_str
+= ulp_len
, test
=true;
142 if (asmname_str
[0] == '*')
144 size_t ulp_len
= strlen (user_label_prefix
);
150 else if (strncmp (asmname_str
, user_label_prefix
, ulp_len
) == 0)
151 asmname_str
+= ulp_len
, test
=true;
158 return strcmp (decl_str
, asmname_str
) == 0;
162 /* Returns nonzero if P1 and P2 are equal. */
165 eq_assembler_name (const void *p1
, const void *p2
)
167 const symtab_node
*n1
= (const symtab_node
*) p1
;
168 const_tree name
= (const_tree
)p2
;
169 return (decl_assembler_name_equal (n1
->decl
, name
));
172 /* Insert NODE to assembler name hash. */
175 insert_to_assembler_name_hash (symtab_node
*node
, bool with_clones
)
177 if (is_a
<varpool_node
*> (node
) && DECL_HARD_REGISTER (node
->decl
))
179 gcc_checking_assert (!node
->previous_sharing_asm_name
180 && !node
->next_sharing_asm_name
);
181 if (assembler_name_hash
)
184 struct cgraph_node
*cnode
;
185 tree decl
= node
->decl
;
187 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
189 aslot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
190 decl_assembler_name_hash (name
),
192 gcc_assert (*aslot
!= node
);
193 node
->next_sharing_asm_name
= (symtab_node
*)*aslot
;
195 ((symtab_node
*)*aslot
)->previous_sharing_asm_name
= node
;
198 /* Update also possible inline clones sharing a decl. */
199 cnode
= dyn_cast
<cgraph_node
*> (node
);
200 if (cnode
&& cnode
->clones
&& with_clones
)
201 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
202 if (cnode
->decl
== decl
)
203 insert_to_assembler_name_hash (cnode
, true);
208 /* Remove NODE from assembler name hash. */
211 unlink_from_assembler_name_hash (symtab_node
*node
, bool with_clones
)
213 if (assembler_name_hash
)
215 struct cgraph_node
*cnode
;
216 tree decl
= node
->decl
;
218 if (node
->next_sharing_asm_name
)
219 node
->next_sharing_asm_name
->previous_sharing_asm_name
220 = node
->previous_sharing_asm_name
;
221 if (node
->previous_sharing_asm_name
)
223 node
->previous_sharing_asm_name
->next_sharing_asm_name
224 = node
->next_sharing_asm_name
;
228 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
230 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
231 decl_assembler_name_hash (name
),
233 gcc_assert (*slot
== node
);
234 if (!node
->next_sharing_asm_name
)
235 htab_clear_slot (assembler_name_hash
, slot
);
237 *slot
= node
->next_sharing_asm_name
;
239 node
->next_sharing_asm_name
= NULL
;
240 node
->previous_sharing_asm_name
= NULL
;
242 /* Update also possible inline clones sharing a decl. */
243 cnode
= dyn_cast
<cgraph_node
*> (node
);
244 if (cnode
&& cnode
->clones
&& with_clones
)
245 for (cnode
= cnode
->clones
; cnode
; cnode
= cnode
->next_sibling_clone
)
246 if (cnode
->decl
== decl
)
247 unlink_from_assembler_name_hash (cnode
, true);
251 /* Arrange node to be first in its entry of assembler_name_hash. */
254 symtab_prevail_in_asm_name_hash (symtab_node
*node
)
256 unlink_from_assembler_name_hash (node
, false);
257 insert_to_assembler_name_hash (node
, false);
261 /* Add node into symbol table. This function is not used directly, but via
262 cgraph/varpool node creation routines. */
265 symtab_register_node (symtab_node
*node
)
267 node
->next
= symtab_nodes
;
268 node
->previous
= NULL
;
270 symtab_nodes
->previous
= node
;
273 if (!node
->decl
->decl_with_vis
.symtab_node
)
274 node
->decl
->decl_with_vis
.symtab_node
= node
;
276 ipa_empty_ref_list (&node
->ref_list
);
278 node
->order
= symtab_order
++;
280 /* Be sure to do this last; C++ FE might create new nodes via
281 DECL_ASSEMBLER_NAME langhook! */
282 insert_to_assembler_name_hash (node
, false);
285 /* Remove NODE from same comdat group. */
288 symtab_remove_from_same_comdat_group (symtab_node
*node
)
290 if (node
->same_comdat_group
)
293 for (prev
= node
->same_comdat_group
;
294 prev
->same_comdat_group
!= node
;
295 prev
= prev
->same_comdat_group
)
297 if (node
->same_comdat_group
== prev
)
298 prev
->same_comdat_group
= NULL
;
300 prev
->same_comdat_group
= node
->same_comdat_group
;
301 node
->same_comdat_group
= NULL
;
305 /* Remove node from symbol table. This function is not used directly, but via
306 cgraph/varpool node removal routines. */
309 symtab_unregister_node (symtab_node
*node
)
311 ipa_remove_all_references (&node
->ref_list
);
312 ipa_remove_all_referring (&node
->ref_list
);
314 /* Remove reference to section. */
315 node
->set_section_for_node (NULL
);
317 symtab_remove_from_same_comdat_group (node
);
320 node
->previous
->next
= node
->next
;
322 symtab_nodes
= node
->next
;
324 node
->next
->previous
= node
->previous
;
326 node
->previous
= NULL
;
328 /* During LTO symtab merging we temporarily corrupt decl to symtab node
330 gcc_assert (node
->decl
->decl_with_vis
.symtab_node
|| in_lto_p
);
331 if (node
->decl
->decl_with_vis
.symtab_node
== node
)
333 symtab_node
*replacement_node
= NULL
;
334 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
335 replacement_node
= cgraph_find_replacement_node (cnode
);
336 node
->decl
->decl_with_vis
.symtab_node
= replacement_node
;
338 if (!is_a
<varpool_node
*> (node
) || !DECL_HARD_REGISTER (node
->decl
))
339 unlink_from_assembler_name_hash (node
, false);
343 /* Remove symtab NODE from the symbol table. */
346 symtab_remove_node (symtab_node
*node
)
348 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
349 cgraph_remove_node (cnode
);
350 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
351 varpool_remove_node (vnode
);
354 /* Initalize asm name hash unless. */
357 symtab_initialize_asm_name_hash (void)
360 if (!assembler_name_hash
)
362 assembler_name_hash
=
363 htab_create_ggc (10, hash_node_by_assembler_name
, eq_assembler_name
,
365 FOR_EACH_SYMBOL (node
)
366 insert_to_assembler_name_hash (node
, false);
370 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
371 Return NULL if there's no such node. */
374 symtab_node_for_asm (const_tree asmname
)
379 symtab_initialize_asm_name_hash ();
380 slot
= htab_find_slot_with_hash (assembler_name_hash
, asmname
,
381 decl_assembler_name_hash (asmname
),
386 node
= (symtab_node
*) *slot
;
392 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
395 change_decl_assembler_name (tree decl
, tree name
)
397 symtab_node
*node
= NULL
;
399 /* We can have user ASM names on things, like global register variables, that
400 are not in the symbol table. */
401 if ((TREE_CODE (decl
) == VAR_DECL
402 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
403 || TREE_CODE (decl
) == FUNCTION_DECL
)
404 node
= symtab_get_node (decl
);
405 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
407 SET_DECL_ASSEMBLER_NAME (decl
, name
);
409 insert_to_assembler_name_hash (node
, true);
413 if (name
== DECL_ASSEMBLER_NAME (decl
))
416 tree alias
= (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl
))
417 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl
))
420 unlink_from_assembler_name_hash (node
, true);
421 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
422 && DECL_RTL_SET_P (decl
))
423 warning (0, "%D renamed after being referenced in assembly", decl
);
425 SET_DECL_ASSEMBLER_NAME (decl
, name
);
428 IDENTIFIER_TRANSPARENT_ALIAS (name
) = 1;
429 TREE_CHAIN (name
) = alias
;
432 insert_to_assembler_name_hash (node
, true);
436 /* Add NEW_ to the same comdat group that OLD is in. */
439 symtab_add_to_same_comdat_group (symtab_node
*new_node
,
440 symtab_node
*old_node
)
442 gcc_assert (old_node
->get_comdat_group ());
443 gcc_assert (!new_node
->same_comdat_group
);
444 gcc_assert (new_node
!= old_node
);
446 new_node
->set_comdat_group (old_node
->get_comdat_group ());
447 new_node
->same_comdat_group
= old_node
;
448 if (!old_node
->same_comdat_group
)
449 old_node
->same_comdat_group
= new_node
;
453 for (n
= old_node
->same_comdat_group
;
454 n
->same_comdat_group
!= old_node
;
455 n
= n
->same_comdat_group
)
457 n
->same_comdat_group
= new_node
;
461 /* Dissolve the same_comdat_group list in which NODE resides. */
464 symtab_dissolve_same_comdat_group_list (symtab_node
*node
)
466 symtab_node
*n
= node
;
469 if (!node
->same_comdat_group
)
473 next
= n
->same_comdat_group
;
474 n
->same_comdat_group
= NULL
;
475 /* Clear comdat_group for comdat locals, since
476 make_decl_local doesn't. */
477 if (!TREE_PUBLIC (n
->decl
))
478 n
->set_comdat_group (NULL
);
484 /* Return printable assembler name of NODE.
485 This function is used only for debugging. When assembler name
486 is unknown go with identifier name. */
489 symtab_node::asm_name () const
491 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
492 return lang_hooks
.decl_printable_name (decl
, 2);
493 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
496 /* Return printable identifier name. */
499 symtab_node::name () const
501 return lang_hooks
.decl_printable_name (decl
, 2);
504 static const char * const symtab_type_names
[] = {"symbol", "function", "variable"};
506 /* Dump base fields of symtab nodes. Not to be used directly. */
509 dump_symtab_base (FILE *f
, symtab_node
*node
)
511 static const char * const visibility_types
[] = {
512 "default", "protected", "hidden", "internal"
515 fprintf (f
, "%s/%i (%s)",
519 dump_addr (f
, " @", (void *)node
);
520 fprintf (f
, "\n Type: %s", symtab_type_names
[node
->type
]);
522 if (node
->definition
)
523 fprintf (f
, " definition");
525 fprintf (f
, " analyzed");
527 fprintf (f
, " alias");
529 fprintf (f
, " weakref");
530 if (node
->cpp_implicit_alias
)
531 fprintf (f
, " cpp_implicit_alias");
532 if (node
->alias_target
)
533 fprintf (f
, " target:%s",
534 DECL_P (node
->alias_target
)
535 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
536 (node
->alias_target
))
537 : IDENTIFIER_POINTER (node
->alias_target
));
538 if (node
->body_removed
)
539 fprintf (f
, "\n Body removed by symtab_remove_unreachable_nodes");
540 fprintf (f
, "\n Visibility:");
541 if (node
->in_other_partition
)
542 fprintf (f
, " in_other_partition");
543 if (node
->used_from_other_partition
)
544 fprintf (f
, " used_from_other_partition");
545 if (node
->force_output
)
546 fprintf (f
, " force_output");
547 if (node
->forced_by_abi
)
548 fprintf (f
, " forced_by_abi");
549 if (node
->externally_visible
)
550 fprintf (f
, " externally_visible");
551 if (node
->resolution
!= LDPR_UNKNOWN
)
553 ld_plugin_symbol_resolution_names
[(int)node
->resolution
]);
554 if (TREE_ASM_WRITTEN (node
->decl
))
555 fprintf (f
, " asm_written");
556 if (DECL_EXTERNAL (node
->decl
))
557 fprintf (f
, " external");
558 if (TREE_PUBLIC (node
->decl
))
559 fprintf (f
, " public");
560 if (DECL_COMMON (node
->decl
))
561 fprintf (f
, " common");
562 if (DECL_WEAK (node
->decl
))
563 fprintf (f
, " weak");
564 if (DECL_DLLIMPORT_P (node
->decl
))
565 fprintf (f
, " dll_import");
566 if (DECL_COMDAT (node
->decl
))
567 fprintf (f
, " comdat");
568 if (node
->get_comdat_group ())
569 fprintf (f
, " comdat_group:%s",
570 IDENTIFIER_POINTER (node
->get_comdat_group_id ()));
571 if (DECL_ONE_ONLY (node
->decl
))
572 fprintf (f
, " one_only");
573 if (node
->get_section ())
574 fprintf (f
, " section:%s",
575 node
->get_section ());
576 if (node
->implicit_section
)
577 fprintf (f
," (implicit_section)");
578 if (DECL_VISIBILITY_SPECIFIED (node
->decl
))
579 fprintf (f
, " visibility_specified");
580 if (DECL_VISIBILITY (node
->decl
))
581 fprintf (f
, " visibility:%s",
582 visibility_types
[DECL_VISIBILITY (node
->decl
)]);
583 if (DECL_VIRTUAL_P (node
->decl
))
584 fprintf (f
, " virtual");
585 if (DECL_ARTIFICIAL (node
->decl
))
586 fprintf (f
, " artificial");
587 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
)
589 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
590 fprintf (f
, " constructor");
591 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
592 fprintf (f
, " destructor");
596 if (node
->same_comdat_group
)
597 fprintf (f
, " Same comdat group as: %s/%i\n",
598 node
->same_comdat_group
->asm_name (),
599 node
->same_comdat_group
->order
);
600 if (node
->next_sharing_asm_name
)
601 fprintf (f
, " next sharing asm name: %i\n",
602 node
->next_sharing_asm_name
->order
);
603 if (node
->previous_sharing_asm_name
)
604 fprintf (f
, " previous sharing asm name: %i\n",
605 node
->previous_sharing_asm_name
->order
);
607 if (node
->address_taken
)
608 fprintf (f
, " Address is taken.\n");
611 fprintf (f
, " Aux:");
612 dump_addr (f
, " @", (void *)node
->aux
);
615 fprintf (f
, " References: ");
616 ipa_dump_references (f
, &node
->ref_list
);
617 fprintf (f
, " Referring: ");
618 ipa_dump_referring (f
, &node
->ref_list
);
619 if (node
->lto_file_data
)
620 fprintf (f
, " Read from file: %s\n",
621 node
->lto_file_data
->file_name
);
624 /* Dump symtab node. */
627 dump_symtab_node (FILE *f
, symtab_node
*node
)
629 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
630 dump_cgraph_node (f
, cnode
);
631 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
632 dump_varpool_node (f
, vnode
);
635 /* Dump symbol table. */
638 dump_symtab (FILE *f
)
641 fprintf (f
, "Symbol table:\n\n");
642 FOR_EACH_SYMBOL (node
)
643 dump_symtab_node (f
, node
);
646 /* Dump symtab node NODE to stderr. */
649 debug_symtab_node (symtab_node
*node
)
651 dump_symtab_node (stderr
, node
);
654 /* Dump symbol table to stderr. */
659 dump_symtab (stderr
);
662 /* Verify common part of symtab nodes. */
665 verify_symtab_base (symtab_node
*node
)
667 bool error_found
= false;
668 symtab_node
*hashed_node
;
670 if (is_a
<cgraph_node
*> (node
))
672 if (TREE_CODE (node
->decl
) != FUNCTION_DECL
)
674 error ("function symbol is not function");
678 else if (is_a
<varpool_node
*> (node
))
680 if (TREE_CODE (node
->decl
) != VAR_DECL
)
682 error ("variable symbol is not variable");
688 error ("node has unknown type");
692 if (cgraph_state
!= CGRAPH_LTO_STREAMING
)
694 hashed_node
= symtab_get_node (node
->decl
);
697 error ("node not found node->decl->decl_with_vis.symtab_node");
700 if (hashed_node
!= node
701 && (!is_a
<cgraph_node
*> (node
)
702 || !dyn_cast
<cgraph_node
*> (node
)->clone_of
703 || dyn_cast
<cgraph_node
*> (node
)->clone_of
->decl
706 error ("node differs from node->decl->decl_with_vis.symtab_node");
710 if (assembler_name_hash
)
712 hashed_node
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (node
->decl
));
713 if (hashed_node
&& hashed_node
->previous_sharing_asm_name
)
715 error ("assembler name hash list corrupted");
720 if (hashed_node
== node
)
722 hashed_node
= hashed_node
->next_sharing_asm_name
;
725 && !(is_a
<varpool_node
*> (node
)
726 || DECL_HARD_REGISTER (node
->decl
)))
728 error ("node not found in symtab assembler name hash");
732 if (node
->previous_sharing_asm_name
733 && node
->previous_sharing_asm_name
->next_sharing_asm_name
!= node
)
735 error ("double linked list of assembler names corrupted");
738 if (node
->analyzed
&& !node
->definition
)
740 error ("node is analyzed byt it is not a definition");
743 if (node
->cpp_implicit_alias
&& !node
->alias
)
745 error ("node is alias but not implicit alias");
748 if (node
->alias
&& !node
->definition
751 error ("node is alias but not definition");
754 if (node
->weakref
&& !node
->alias
)
756 error ("node is weakref but not an alias");
759 if (node
->same_comdat_group
)
761 symtab_node
*n
= node
->same_comdat_group
;
763 if (!n
->get_comdat_group ())
765 error ("node is in same_comdat_group list but has no comdat_group");
768 if (n
->get_comdat_group () != node
->get_comdat_group ())
770 error ("same_comdat_group list across different groups");
775 error ("Node has same_comdat_group but it is not a definition");
778 if (n
->type
!= node
->type
)
780 error ("mixing different types of symbol in same comdat groups is not supported");
785 error ("node is alone in a comdat group");
790 if (!n
->same_comdat_group
)
792 error ("same_comdat_group is not a circular list");
796 n
= n
->same_comdat_group
;
799 if (symtab_comdat_local_p (node
))
801 struct ipa_ref_list
*refs
= &node
->ref_list
;
804 for (int i
= 0; ipa_ref_list_referring_iterate (refs
, i
, ref
); ++i
)
806 if (!symtab_in_same_comdat_p (ref
->referring
, node
))
808 error ("comdat-local symbol referred to by %s outside its "
810 identifier_to_locale (ref
->referring
->name()));
816 if (node
->implicit_section
&& !node
->get_section ())
818 error ("implicit_section flag is set but section isn't");
821 if (node
->get_section () && node
->get_comdat_group ()
822 && !node
->implicit_section
)
824 error ("Both section and comdat group is set");
827 /* TODO: Add string table for sections, so we do not keep holding duplicated
829 if (node
->alias
&& node
->definition
830 && node
->get_section () != symtab_alias_target (node
)->get_section ()
831 && (!node
->get_section()
832 || !symtab_alias_target (node
)->get_section ()
833 || strcmp (node
->get_section(),
834 symtab_alias_target (node
)->get_section ())))
836 error ("Alias and target's section differs");
837 dump_symtab_node (stderr
, symtab_alias_target (node
));
840 if (node
->alias
&& node
->definition
841 && node
->get_comdat_group () != symtab_alias_target (node
)->get_comdat_group ())
843 error ("Alias and target's comdat groups differs");
844 dump_symtab_node (stderr
, symtab_alias_target (node
));
851 /* Verify consistency of NODE. */
854 verify_symtab_node (symtab_node
*node
)
859 timevar_push (TV_CGRAPH_VERIFY
);
860 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
861 verify_cgraph_node (cnode
);
863 if (verify_symtab_base (node
))
865 dump_symtab_node (stderr
, node
);
866 internal_error ("verify_symtab_node failed");
868 timevar_pop (TV_CGRAPH_VERIFY
);
871 /* Verify symbol table for internal consistency. */
877 pointer_map
<symtab_node
*> comdat_head_map
;
879 FOR_EACH_SYMBOL (node
)
881 verify_symtab_node (node
);
882 if (node
->get_comdat_group ())
884 symtab_node
**entry
, *s
;
887 entry
= comdat_head_map
.insert (node
->get_comdat_group (), &existed
);
891 for (s
= (*entry
)->same_comdat_group
; s
!= NULL
&& s
!= node
; s
= s
->same_comdat_group
)
892 if (!s
|| s
== *entry
)
894 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
895 dump_symtab_node (stderr
, *entry
);
896 dump_symtab_node (stderr
, s
);
897 internal_error ("verify_symtab failed");
903 /* Return true when RESOLUTION indicate that linker will use
904 the symbol from non-LTO object files. */
907 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution
)
909 return (resolution
== LDPR_PREVAILING_DEF
910 || resolution
== LDPR_PREEMPTED_REG
911 || resolution
== LDPR_RESOLVED_EXEC
912 || resolution
== LDPR_RESOLVED_DYN
);
915 /* Return true when NODE is known to be used from other (non-LTO) object file.
916 Known only when doing LTO via linker plugin. */
919 symtab_used_from_object_file_p (symtab_node
*node
)
921 if (!TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
))
923 if (resolution_used_from_other_file_p (node
->resolution
))
928 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
929 but other code such as notice_global_symbol generates rtl. */
932 symtab_make_decl_local (tree decl
)
936 /* Avoid clearing comdat_groups on comdat-local decls. */
937 if (TREE_PUBLIC (decl
) == 0)
940 if (TREE_CODE (decl
) == VAR_DECL
)
941 DECL_COMMON (decl
) = 0;
942 else gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
944 DECL_COMDAT (decl
) = 0;
945 DECL_WEAK (decl
) = 0;
946 DECL_EXTERNAL (decl
) = 0;
947 DECL_VISIBILITY_SPECIFIED (decl
) = 0;
948 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
949 TREE_PUBLIC (decl
) = 0;
950 if (!DECL_RTL_SET_P (decl
))
953 /* Update rtl flags. */
954 make_decl_rtl (decl
);
956 rtl
= DECL_RTL (decl
);
960 symbol
= XEXP (rtl
, 0);
961 if (GET_CODE (symbol
) != SYMBOL_REF
)
964 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
967 /* Return availability of NODE. */
970 symtab_node_availability (symtab_node
*node
)
972 if (is_a
<cgraph_node
*> (node
))
973 return cgraph_function_body_availability (cgraph (node
));
975 return cgraph_variable_initializer_availability (varpool (node
));
978 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
979 If NODE is not an alias, return NODE.
980 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
983 symtab_alias_ultimate_target (symtab_node
*node
, enum availability
*availability
)
985 bool weakref_p
= false;
990 *availability
= symtab_node_availability (node
);
994 /* To determine visibility of the target, we follow ELF semantic of aliases.
995 Here alias is an alternative assembler name of a given definition. Its
996 availability prevails the availability of its target (i.e. static alias of
997 weak definition is available.
999 Weakref is a different animal (and not part of ELF per se). It is just
1000 alternative name of a given symbol used within one complation unit
1001 and is translated prior hitting the object file. It inherits the
1002 visibility of its target (i.e. weakref of non-overwritable definition
1003 is non-overwritable, while weakref of weak definition is weak).
1005 If we ever get into supporting targets with different semantics, a target
1006 hook will be needed here. */
1010 weakref_p
= node
->weakref
;
1012 *availability
= symtab_node_availability (node
);
1014 *availability
= AVAIL_LOCAL
;
1018 if (node
->alias
&& node
->analyzed
)
1019 node
= symtab_alias_target (node
);
1024 else if (node
->analyzed
)
1028 enum availability a
= symtab_node_availability (node
);
1029 if (a
< *availability
)
1034 *availability
= AVAIL_NOT_AVAILABLE
;
1037 if (node
&& availability
&& weakref_p
)
1039 enum availability a
= symtab_node_availability (node
);
1040 if (a
< *availability
)
1042 weakref_p
= node
->weakref
;
1046 *availability
= AVAIL_NOT_AVAILABLE
;
1050 /* C++ FE sometimes change linkage flags after producing same body aliases.
1052 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1053 are obviously equivalent. The way it is doing so is however somewhat
1054 kludgy and interferes with the visibility code. As a result we need to
1055 copy the visibility from the target to get things right. */
1058 fixup_same_cpp_alias_visibility (symtab_node
*node
, symtab_node
*target
)
1060 if (is_a
<cgraph_node
*> (node
))
1062 DECL_DECLARED_INLINE_P (node
->decl
)
1063 = DECL_DECLARED_INLINE_P (target
->decl
);
1064 DECL_DISREGARD_INLINE_LIMITS (node
->decl
)
1065 = DECL_DISREGARD_INLINE_LIMITS (target
->decl
);
1067 /* FIXME: It is not really clear why those flags should not be copied for
1071 DECL_WEAK (node
->decl
) = DECL_WEAK (target
->decl
);
1072 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (target
->decl
);
1073 DECL_VISIBILITY (node
->decl
) = DECL_VISIBILITY (target
->decl
);
1075 DECL_VIRTUAL_P (node
->decl
) = DECL_VIRTUAL_P (target
->decl
);
1076 if (TREE_PUBLIC (node
->decl
))
1080 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (target
->decl
);
1081 DECL_COMDAT (node
->decl
) = DECL_COMDAT (target
->decl
);
1082 group
= target
->get_comdat_group ();
1083 node
->set_comdat_group (group
);
1085 && !node
->same_comdat_group
)
1086 symtab_add_to_same_comdat_group (node
, target
);
1088 node
->externally_visible
= target
->externally_visible
;
1091 /* Hash sections by their names. */
1094 hash_section_hash_entry (const void *p
)
1096 const section_hash_entry
*n
= (const section_hash_entry
*) p
;
1097 return htab_hash_string (n
->name
);
1100 /* Return true if section P1 name equals to P2. */
1103 eq_sections (const void *p1
, const void *p2
)
1105 const section_hash_entry
*n1
= (const section_hash_entry
*) p1
;
1106 const char *name
= (const char *)p2
;
1107 return n1
->name
== name
|| !strcmp (n1
->name
, name
);
1110 /* Set section, do not recurse into aliases.
1111 When one wants to change section of symbol and its aliases,
1115 symtab_node::set_section_for_node (const char *section
)
1117 const char *current
= get_section ();
1120 if (current
== section
1121 || (current
&& section
1122 && !strcmp (current
, section
)))
1127 x_section
->ref_count
--;
1128 if (!x_section
->ref_count
)
1130 slot
= htab_find_slot_with_hash (section_hash
, x_section
->name
,
1131 htab_hash_string (x_section
->name
),
1133 ggc_free (x_section
);
1134 htab_clear_slot (section_hash
, slot
);
1140 implicit_section
= false;
1144 section_hash
= htab_create_ggc (10, hash_section_hash_entry
,
1146 slot
= htab_find_slot_with_hash (section_hash
, section
,
1147 htab_hash_string (section
),
1150 x_section
= (section_hash_entry
*)*slot
;
1153 int len
= strlen (section
);
1154 *slot
= x_section
= ggc_cleared_alloc
<section_hash_entry
> ();
1155 x_section
->name
= ggc_vec_alloc
<char> (len
+ 1);
1156 memcpy (x_section
->name
, section
, len
+ 1);
1158 x_section
->ref_count
++;
1161 /* Worker for set_section. */
1164 set_section_1 (struct symtab_node
*n
, void *s
)
1166 n
->set_section_for_node ((char *)s
);
1170 /* Set section of symbol and its aliases. */
1173 symtab_node::set_section (const char *section
)
1175 gcc_assert (!this->alias
);
1176 symtab_for_node_and_aliases (this, set_section_1
, const_cast<char *>(section
), true);
1179 /* Reset section of NODE. That is when NODE is being brought local
1180 we may want to clear section produced for comdat group and depending
1181 on function-sections produce now, local, unique section for it. */
1184 symtab_node::reset_section ()
1186 if (!this->implicit_section
)
1188 this->set_section (NULL
);
1189 resolve_unique_section (this->decl
, 0,
1190 is_a
<cgraph_node
*> (this)
1191 ? flag_function_sections
: flag_data_sections
);
1194 /* Worker for symtab_resolve_alias. */
1197 set_implicit_section (struct symtab_node
*n
, void *data ATTRIBUTE_UNUSED
)
1199 n
->implicit_section
= true;
1203 /* Add reference recording that NODE is alias of TARGET.
1204 The function can fail in the case of aliasing cycles; in this case
1205 it returns false. */
1208 symtab_resolve_alias (symtab_node
*node
, symtab_node
*target
)
1212 gcc_assert (!node
->analyzed
1213 && !vec_safe_length (node
->ref_list
.references
));
1215 /* Never let cycles to creep into the symbol table alias references;
1216 those will make alias walkers to be infinite. */
1217 for (n
= target
; n
&& n
->alias
;
1218 n
= n
->analyzed
? symtab_alias_target (n
) : NULL
)
1221 if (is_a
<cgraph_node
*> (node
))
1222 error ("function %q+D part of alias cycle", node
->decl
);
1223 else if (is_a
<varpool_node
*> (node
))
1224 error ("variable %q+D part of alias cycle", node
->decl
);
1227 node
->alias
= false;
1231 /* "analyze" the node - i.e. mark the reference. */
1232 node
->definition
= true;
1234 node
->analyzed
= true;
1235 ipa_record_reference (node
, target
, IPA_REF_ALIAS
, NULL
);
1237 /* Add alias into the comdat group of its target unless it is already there. */
1238 if (node
->same_comdat_group
)
1239 symtab_remove_from_same_comdat_group (node
);
1240 node
->set_comdat_group (NULL
);
1241 if (target
->get_comdat_group ())
1242 symtab_add_to_same_comdat_group (node
, target
);
1244 if ((node
->get_section () != target
->get_section ()
1245 || target
->get_comdat_group ())
1246 && node
->get_section () && !node
->implicit_section
)
1248 error ("section of alias %q+D must match section of its target",
1251 symtab_for_node_and_aliases (node
, set_section_1
,
1252 const_cast<char *>(target
->get_section ()), true);
1253 if (target
->implicit_section
)
1254 symtab_for_node_and_aliases (node
,
1255 set_implicit_section
, NULL
, true);
1257 /* Alias targets become redundant after alias is resolved into an reference.
1258 We do not want to keep it around or we would have to mind updating them
1259 when renaming symbols. */
1260 node
->alias_target
= NULL
;
1262 if (node
->cpp_implicit_alias
&& cgraph_state
>= CGRAPH_STATE_CONSTRUCTION
)
1263 fixup_same_cpp_alias_visibility (node
, target
);
1265 /* If alias has address taken, so does the target. */
1266 if (node
->address_taken
)
1267 symtab_alias_ultimate_target (target
, NULL
)->address_taken
= true;
1271 /* Call calback on NODE and aliases associated to NODE.
1272 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1276 symtab_for_node_and_aliases (symtab_node
*node
,
1277 bool (*callback
) (symtab_node
*, void *),
1279 bool include_overwritable
)
1282 struct ipa_ref
*ref
;
1284 if (callback (node
, data
))
1286 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
1287 if (ref
->use
== IPA_REF_ALIAS
)
1289 symtab_node
*alias
= ref
->referring
;
1290 if (include_overwritable
1291 || symtab_node_availability (alias
) > AVAIL_OVERWRITABLE
)
1292 if (symtab_for_node_and_aliases (alias
, callback
, data
,
1293 include_overwritable
))
1299 /* Worker searching nonoverwritable alias. */
1302 symtab_nonoverwritable_alias_1 (symtab_node
*node
, void *data
)
1304 if (decl_binds_to_current_def_p (node
->decl
))
1306 *(symtab_node
**)data
= node
;
1312 /* If NODE can not be overwriten by static or dynamic linker to point to different
1313 definition, return NODE. Otherwise look for alias with such property and if
1314 none exists, introduce new one. */
1317 symtab_nonoverwritable_alias (symtab_node
*node
)
1320 symtab_node
*new_node
= NULL
;
1322 /* First try to look up existing alias or base object
1323 (if that is already non-overwritable). */
1324 node
= symtab_alias_ultimate_target (node
, NULL
);
1325 gcc_assert (!node
->alias
&& !node
->weakref
);
1326 symtab_for_node_and_aliases (node
, symtab_nonoverwritable_alias_1
,
1327 (void *)&new_node
, true);
1330 #ifndef ASM_OUTPUT_DEF
1331 /* If aliases aren't supported by the assembler, fail. */
1335 /* Otherwise create a new one. */
1336 new_decl
= copy_node (node
->decl
);
1337 DECL_NAME (new_decl
) = clone_function_name (node
->decl
, "localalias");
1338 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1339 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
1340 DECL_INITIAL (new_decl
) = NULL
;
1341 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
1342 SET_DECL_RTL (new_decl
, NULL
);
1344 /* Update the properties. */
1345 DECL_EXTERNAL (new_decl
) = 0;
1346 TREE_PUBLIC (new_decl
) = 0;
1347 DECL_COMDAT (new_decl
) = 0;
1348 DECL_WEAK (new_decl
) = 0;
1350 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1351 DECL_VIRTUAL_P (new_decl
) = DECL_VIRTUAL_P (node
->decl
);
1352 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1354 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
1355 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
1356 new_node
= cgraph_create_function_alias
1357 (new_decl
, node
->decl
);
1361 TREE_READONLY (new_decl
) = TREE_READONLY (node
->decl
);
1362 DECL_INITIAL (new_decl
) = error_mark_node
;
1363 new_node
= varpool_create_variable_alias (new_decl
, node
->decl
);
1365 symtab_resolve_alias (new_node
, node
);
1366 gcc_assert (decl_binds_to_current_def_p (new_decl
)
1367 && targetm
.binds_local_p (new_decl
));
1371 /* Return true if A and B represents semantically equivalent symbols. */
1374 symtab_semantically_equivalent_p (symtab_node
*a
,
1377 enum availability avail
;
1381 /* Equivalent functions are equivalent. */
1382 if (a
->decl
== b
->decl
)
1385 /* If symbol is not overwritable by different implementation,
1386 walk to the base object it defines. */
1387 ba
= symtab_alias_ultimate_target (a
, &avail
);
1388 if (avail
>= AVAIL_AVAILABLE
)
1395 bb
= symtab_alias_ultimate_target (b
, &avail
);
1396 if (avail
>= AVAIL_AVAILABLE
)
1406 /* Classify symbol NODE for partitioning. */
1408 enum symbol_partitioning_class
1409 symtab_get_symbol_partitioning_class (symtab_node
*node
)
1411 /* Inline clones are always duplicated.
1412 This include external delcarations. */
1413 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1415 if (DECL_ABSTRACT (node
->decl
))
1416 return SYMBOL_EXTERNAL
;
1418 if (cnode
&& cnode
->global
.inlined_to
)
1419 return SYMBOL_DUPLICATE
;
1421 /* Weakref aliases are always duplicated. */
1423 return SYMBOL_DUPLICATE
;
1425 /* External declarations are external. */
1426 if (DECL_EXTERNAL (node
->decl
))
1427 return SYMBOL_EXTERNAL
;
1429 if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
1431 /* Constant pool references use local symbol names that can not
1432 be promoted global. We should never put into a constant pool
1433 objects that can not be duplicated across partitions. */
1434 if (DECL_IN_CONSTANT_POOL (node
->decl
))
1435 return SYMBOL_DUPLICATE
;
1436 gcc_checking_assert (vnode
->definition
);
1438 /* Functions that are cloned may stay in callgraph even if they are unused.
1439 Handle them as external; compute_ltrans_boundary take care to make
1440 proper things to happen (i.e. to make them appear in the boundary but
1441 with body streamed, so clone can me materialized). */
1442 else if (!cgraph (node
)->definition
)
1443 return SYMBOL_EXTERNAL
;
1445 /* Linker discardable symbols are duplicated to every use unless they are
1447 if (DECL_ONE_ONLY (node
->decl
)
1448 && !node
->force_output
1449 && !node
->forced_by_abi
1450 && !symtab_used_from_object_file_p (node
))
1451 return SYMBOL_DUPLICATE
;
1453 return SYMBOL_PARTITION
;
1455 #include "gt-symtab.h"