2014-06-28 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / symtab.c
blob89591ee3ac71367bcb1fb5eaf87141d93481685c
1 /* Symbol table.
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
10 version.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "print-tree.h"
28 #include "varasm.h"
29 #include "function.h"
30 #include "emit-rtl.h"
31 #include "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "tree-inline.h"
38 #include "langhooks.h"
39 #include "hashtab.h"
40 #include "cgraph.h"
41 #include "diagnostic.h"
42 #include "timevar.h"
43 #include "lto-streamer.h"
44 #include "output.h"
45 #include "ipa-utils.h"
47 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
49 const char * const ld_plugin_symbol_resolution_names[]=
51 "",
52 "undef",
53 "prevailing_def",
54 "prevailing_def_ironly",
55 "preempted_reg",
56 "preempted_ir",
57 "resolved_ir",
58 "resolved_exec",
59 "resolved_dyn",
60 "prevailing_def_ironly_exp"
64 /* Hash table used to hold sectoons. */
65 static GTY((param_is (section_hash_entry))) htab_t section_hash;
67 /* Hash table used to convert assembler names into nodes. */
68 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
70 /* Map from a symbol to initialization/finalization priorities. */
71 struct GTY(()) symbol_priority_map {
72 symtab_node *symbol;
73 priority_type init;
74 priority_type fini;
77 /* Hash table used to hold init priorities. */
78 static GTY ((param_is (struct symbol_priority_map)))
79 htab_t init_priority_hash;
81 /* Linked list of symbol table nodes. */
82 symtab_node *symtab_nodes;
84 /* The order index of the next symtab node to be created. This is
85 used so that we can sort the cgraph nodes in order by when we saw
86 them, to support -fno-toplevel-reorder. */
87 int symtab_order;
89 /* Hash asmnames ignoring the user specified marks. */
91 static hashval_t
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);
99 if (ulp_len == 0)
101 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
102 decl_str += ulp_len;
104 return htab_hash_string (decl_str);
107 return htab_hash_string (IDENTIFIER_POINTER (asmname));
111 /* Returns a hash code for P. */
113 static hashval_t
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. */
122 static bool
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;
128 bool test = false;
130 if (decl_asmname == asmname)
131 return true;
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);
147 decl_str ++;
149 if (ulp_len == 0)
150 test = true;
151 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
152 decl_str += ulp_len, test=true;
153 else
154 decl_str --;
156 if (asmname_str[0] == '*')
158 size_t ulp_len = strlen (user_label_prefix);
160 asmname_str ++;
162 if (ulp_len == 0)
163 test = true;
164 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
165 asmname_str += ulp_len, test=true;
166 else
167 asmname_str --;
170 if (!test)
171 return false;
172 return strcmp (decl_str, asmname_str) == 0;
176 /* Returns nonzero if P1 and P2 are equal. */
178 static int
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. */
188 static void
189 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
191 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
192 return;
193 gcc_checking_assert (!node->previous_sharing_asm_name
194 && !node->next_sharing_asm_name);
195 if (assembler_name_hash)
197 void **aslot;
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),
205 INSERT);
206 gcc_assert (*aslot != node);
207 node->next_sharing_asm_name = (symtab_node *)*aslot;
208 if (*aslot != NULL)
209 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
210 *aslot = 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. */
224 static void
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;
240 else
242 tree name = DECL_ASSEMBLER_NAME (node->decl);
243 void **slot;
244 slot = htab_find_slot_with_hash (assembler_name_hash, name,
245 decl_assembler_name_hash (name),
246 NO_INSERT);
247 gcc_assert (*slot == node);
248 if (!node->next_sharing_asm_name)
249 htab_clear_slot (assembler_name_hash, slot);
250 else
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. */
267 void
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. */
278 void
279 symtab_register_node (symtab_node *node)
281 node->next = symtab_nodes;
282 node->previous = NULL;
283 if (symtab_nodes)
284 symtab_nodes->previous = node;
285 symtab_nodes = node;
287 if (!node->decl->decl_with_vis.symtab_node)
288 node->decl->decl_with_vis.symtab_node = node;
290 node->ref_list.clear ();
292 node->order = symtab_order++;
294 /* Be sure to do this last; C++ FE might create new nodes via
295 DECL_ASSEMBLER_NAME langhook! */
296 insert_to_assembler_name_hash (node, false);
299 /* Remove NODE from same comdat group. */
301 void
302 symtab_remove_from_same_comdat_group (symtab_node *node)
304 if (node->same_comdat_group)
306 symtab_node *prev;
307 for (prev = node->same_comdat_group;
308 prev->same_comdat_group != node;
309 prev = prev->same_comdat_group)
311 if (node->same_comdat_group == prev)
312 prev->same_comdat_group = NULL;
313 else
314 prev->same_comdat_group = node->same_comdat_group;
315 node->same_comdat_group = NULL;
319 /* Remove node from symbol table. This function is not used directly, but via
320 cgraph/varpool node removal routines. */
322 void
323 symtab_unregister_node (symtab_node *node)
325 node->remove_all_references ();
326 node->remove_all_referring ();
328 /* Remove reference to section. */
329 node->set_section_for_node (NULL);
331 symtab_remove_from_same_comdat_group (node);
333 if (node->previous)
334 node->previous->next = node->next;
335 else
336 symtab_nodes = node->next;
337 if (node->next)
338 node->next->previous = node->previous;
339 node->next = NULL;
340 node->previous = NULL;
342 /* During LTO symtab merging we temporarily corrupt decl to symtab node
343 hash. */
344 gcc_assert (node->decl->decl_with_vis.symtab_node || in_lto_p);
345 if (node->decl->decl_with_vis.symtab_node == node)
347 symtab_node *replacement_node = NULL;
348 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
349 replacement_node = cgraph_find_replacement_node (cnode);
350 node->decl->decl_with_vis.symtab_node = replacement_node;
352 if (!is_a <varpool_node *> (node) || !DECL_HARD_REGISTER (node->decl))
353 unlink_from_assembler_name_hash (node, false);
354 if (node->in_init_priority_hash)
356 struct symbol_priority_map in;
357 void **slot;
358 in.symbol = node;
360 slot = htab_find_slot (init_priority_hash, &in, NO_INSERT);
361 if (slot)
362 htab_clear_slot (init_priority_hash, slot);
367 /* Remove symtab NODE from the symbol table. */
369 void
370 symtab_remove_node (symtab_node *node)
372 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
373 cgraph_remove_node (cnode);
374 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
375 varpool_remove_node (vnode);
378 /* Initalize asm name hash unless. */
380 void
381 symtab_initialize_asm_name_hash (void)
383 symtab_node *node;
384 if (!assembler_name_hash)
386 assembler_name_hash =
387 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
388 NULL);
389 FOR_EACH_SYMBOL (node)
390 insert_to_assembler_name_hash (node, false);
394 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
395 Return NULL if there's no such node. */
397 symtab_node *
398 symtab_node_for_asm (const_tree asmname)
400 symtab_node *node;
401 void **slot;
403 symtab_initialize_asm_name_hash ();
404 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
405 decl_assembler_name_hash (asmname),
406 NO_INSERT);
408 if (slot)
410 node = (symtab_node *) *slot;
411 return node;
413 return NULL;
416 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
418 void
419 change_decl_assembler_name (tree decl, tree name)
421 symtab_node *node = NULL;
423 /* We can have user ASM names on things, like global register variables, that
424 are not in the symbol table. */
425 if ((TREE_CODE (decl) == VAR_DECL
426 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
427 || TREE_CODE (decl) == FUNCTION_DECL)
428 node = symtab_get_node (decl);
429 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
431 SET_DECL_ASSEMBLER_NAME (decl, name);
432 if (node)
433 insert_to_assembler_name_hash (node, true);
435 else
437 if (name == DECL_ASSEMBLER_NAME (decl))
438 return;
440 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
441 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
442 : NULL);
443 if (node)
444 unlink_from_assembler_name_hash (node, true);
445 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
446 && DECL_RTL_SET_P (decl))
447 warning (0, "%D renamed after being referenced in assembly", decl);
449 SET_DECL_ASSEMBLER_NAME (decl, name);
450 if (alias)
452 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
453 TREE_CHAIN (name) = alias;
455 if (node)
456 insert_to_assembler_name_hash (node, true);
460 /* Add NEW_ to the same comdat group that OLD is in. */
462 void
463 symtab_add_to_same_comdat_group (symtab_node *new_node,
464 symtab_node *old_node)
466 gcc_assert (old_node->get_comdat_group ());
467 gcc_assert (!new_node->same_comdat_group);
468 gcc_assert (new_node != old_node);
470 new_node->set_comdat_group (old_node->get_comdat_group ());
471 new_node->same_comdat_group = old_node;
472 if (!old_node->same_comdat_group)
473 old_node->same_comdat_group = new_node;
474 else
476 symtab_node *n;
477 for (n = old_node->same_comdat_group;
478 n->same_comdat_group != old_node;
479 n = n->same_comdat_group)
481 n->same_comdat_group = new_node;
485 /* Dissolve the same_comdat_group list in which NODE resides. */
487 void
488 symtab_dissolve_same_comdat_group_list (symtab_node *node)
490 symtab_node *n = node;
491 symtab_node *next;
493 if (!node->same_comdat_group)
494 return;
497 next = n->same_comdat_group;
498 n->same_comdat_group = NULL;
499 /* Clear comdat_group for comdat locals, since
500 make_decl_local doesn't. */
501 if (!TREE_PUBLIC (n->decl))
502 n->set_comdat_group (NULL);
503 n = next;
505 while (n != node);
508 /* Return printable assembler name of NODE.
509 This function is used only for debugging. When assembler name
510 is unknown go with identifier name. */
512 const char *
513 symtab_node::asm_name () const
515 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
516 return lang_hooks.decl_printable_name (decl, 2);
517 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
520 /* Return printable identifier name. */
522 const char *
523 symtab_node::name () const
525 return lang_hooks.decl_printable_name (decl, 2);
528 /* Return ipa reference from this symtab_node to
529 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
530 of the use. */
532 struct ipa_ref *
533 symtab_node::add_reference (symtab_node *referred_node,
534 enum ipa_ref_use use_type)
536 return add_reference (referred_node, use_type, NULL);
540 /* Return ipa reference from this symtab_node to
541 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
542 of the use and STMT the statement (if it exists). */
544 struct ipa_ref *
545 symtab_node::add_reference (symtab_node *referred_node,
546 enum ipa_ref_use use_type, gimple stmt)
548 struct ipa_ref *ref = NULL, *ref2 = NULL;
549 struct ipa_ref_list *list, *list2;
550 ipa_ref_t *old_references;
552 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
553 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
555 list = &ref_list;
556 old_references = vec_safe_address (list->references);
557 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
558 ref = &list->references->last ();
560 list2 = &referred_node->ref_list;
561 list2->referring.safe_push (ref);
562 ref->referred_index = list2->referring.length () - 1;
563 ref->referring = this;
564 ref->referred = referred_node;
565 ref->stmt = stmt;
566 ref->lto_stmt_uid = 0;
567 ref->use = use_type;
568 ref->speculative = 0;
570 /* If vector was moved in memory, update pointers. */
571 if (old_references != list->references->address ())
573 int i;
574 for (i = 0; iterate_reference(i, ref2); i++)
575 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
577 return ref;
580 /* If VAL is a reference to a function or a variable, add a reference from
581 this symtab_node to the corresponding symbol table node. USE_TYPE specify
582 type of the use and STMT the statement (if it exists). Return the new
583 reference or NULL if none was created. */
585 struct ipa_ref *
586 symtab_node::maybe_add_reference (tree val, enum ipa_ref_use use_type,
587 gimple stmt)
589 STRIP_NOPS (val);
590 if (TREE_CODE (val) != ADDR_EXPR)
591 return NULL;
592 val = get_base_var (val);
593 if (val && (TREE_CODE (val) == FUNCTION_DECL
594 || TREE_CODE (val) == VAR_DECL))
596 symtab_node *referred = symtab_get_node (val);
597 gcc_checking_assert (referred);
598 return add_reference (referred, use_type, stmt);
600 return NULL;
603 /* Clone all references from symtab NODE to this symtab_node. */
605 void
606 symtab_node::clone_references (struct symtab_node *node)
608 struct ipa_ref *ref = NULL, *ref2 = NULL;
609 int i;
610 for (i = 0; node->iterate_reference (i, ref); i++)
612 bool speculative = ref->speculative;
613 unsigned int stmt_uid = ref->lto_stmt_uid;
615 ref2 = add_reference (ref->referred, ref->use, ref->stmt);
616 ref2->speculative = speculative;
617 ref2->lto_stmt_uid = stmt_uid;
621 /* Clone all referring from symtab NODE to this symtab_node. */
623 void
624 symtab_node::clone_referring (struct symtab_node *node)
626 struct ipa_ref *ref = NULL, *ref2 = NULL;
627 int i;
628 for (i = 0; node->iterate_referring(i, ref); i++)
630 bool speculative = ref->speculative;
631 unsigned int stmt_uid = ref->lto_stmt_uid;
633 ref2 = ref->referring->add_reference (this, ref->use, ref->stmt);
634 ref2->speculative = speculative;
635 ref2->lto_stmt_uid = stmt_uid;
639 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
641 struct ipa_ref *
642 symtab_node::clone_reference (struct ipa_ref *ref, gimple stmt)
644 bool speculative = ref->speculative;
645 unsigned int stmt_uid = ref->lto_stmt_uid;
646 struct ipa_ref *ref2;
648 ref2 = add_reference (ref->referred, ref->use, stmt);
649 ref2->speculative = speculative;
650 ref2->lto_stmt_uid = stmt_uid;
651 return ref2;
654 /* Find the structure describing a reference to REFERRED_NODE
655 and associated with statement STMT. */
657 struct ipa_ref *
658 symtab_node::find_reference (symtab_node *referred_node,
659 gimple stmt, unsigned int lto_stmt_uid)
661 struct ipa_ref *r = NULL;
662 int i;
664 for (i = 0; iterate_reference (i, r); i++)
665 if (r->referred == referred_node
666 && !r->speculative
667 && ((stmt && r->stmt == stmt)
668 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
669 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
670 return r;
671 return NULL;
674 /* Remove all references that are associated with statement STMT. */
676 void
677 symtab_node::remove_stmt_references (gimple stmt)
679 struct ipa_ref *r = NULL;
680 int i = 0;
682 while (iterate_reference (i, r))
683 if (r->stmt == stmt)
684 r->remove_reference ();
685 else
686 i++;
689 /* Remove all stmt references in non-speculative references.
690 Those are not maintained during inlining & clonning.
691 The exception are speculative references that are updated along
692 with callgraph edges associated with them. */
694 void
695 symtab_node::clear_stmts_in_references (void)
697 struct ipa_ref *r = NULL;
698 int i;
700 for (i = 0; iterate_reference (i, r); i++)
701 if (!r->speculative)
703 r->stmt = NULL;
704 r->lto_stmt_uid = 0;
708 /* Remove all references in ref list. */
710 void
711 symtab_node::remove_all_references (void)
713 while (vec_safe_length (ref_list.references))
714 ref_list.references->last ().remove_reference ();
715 vec_free (ref_list.references);
718 /* Remove all referring items in ref list. */
720 void
721 symtab_node::remove_all_referring (void)
723 while (ref_list.referring.length ())
724 ref_list.referring.last ()->remove_reference ();
725 ref_list.referring.release ();
728 /* Dump references in ref list to FILE. */
730 void
731 symtab_node::dump_references (FILE *file)
733 struct ipa_ref *ref = NULL;
734 int i;
735 for (i = 0; iterate_reference (i, ref); i++)
737 fprintf (file, "%s/%i (%s)",
738 ref->referred->asm_name (),
739 ref->referred->order,
740 ipa_ref_use_name [ref->use]);
741 if (ref->speculative)
742 fprintf (file, " (speculative)");
744 fprintf (file, "\n");
747 /* Dump referring in list to FILE. */
749 void
750 symtab_node::dump_referring (FILE *file)
752 struct ipa_ref *ref = NULL;
753 int i;
754 for (i = 0; iterate_referring(i, ref); i++)
756 fprintf (file, "%s/%i (%s)",
757 ref->referring->asm_name (),
758 ref->referring->order,
759 ipa_ref_use_name [ref->use]);
760 if (ref->speculative)
761 fprintf (file, " (speculative)");
763 fprintf (file, "\n");
766 /* Return true if list contains an alias. */
767 bool
768 symtab_node::has_aliases_p (void)
770 struct ipa_ref *ref = NULL;
771 int i;
773 for (i = 0; iterate_referring (i, ref); i++)
774 if (ref->use == IPA_REF_ALIAS)
775 return true;
776 return false;
779 /* Iterates I-th reference in the list, REF is also set. */
781 struct ipa_ref *
782 symtab_node::iterate_reference (unsigned i, struct ipa_ref *&ref)
784 vec_safe_iterate (ref_list.references, i, &ref);
786 return ref;
789 /* Iterates I-th referring item in the list, REF is also set. */
791 struct ipa_ref *
792 symtab_node::iterate_referring (unsigned i, struct ipa_ref *&ref)
794 ref_list.referring.iterate (i, &ref);
796 return ref;
799 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
801 /* Dump base fields of symtab nodes. Not to be used directly. */
803 void
804 dump_symtab_base (FILE *f, symtab_node *node)
806 static const char * const visibility_types[] = {
807 "default", "protected", "hidden", "internal"
810 fprintf (f, "%s/%i (%s)",
811 node->asm_name (),
812 node->order,
813 node->name ());
814 dump_addr (f, " @", (void *)node);
815 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
817 if (node->definition)
818 fprintf (f, " definition");
819 if (node->analyzed)
820 fprintf (f, " analyzed");
821 if (node->alias)
822 fprintf (f, " alias");
823 if (node->weakref)
824 fprintf (f, " weakref");
825 if (node->cpp_implicit_alias)
826 fprintf (f, " cpp_implicit_alias");
827 if (node->alias_target)
828 fprintf (f, " target:%s",
829 DECL_P (node->alias_target)
830 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
831 (node->alias_target))
832 : IDENTIFIER_POINTER (node->alias_target));
833 if (node->body_removed)
834 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
835 fprintf (f, "\n Visibility:");
836 if (node->in_other_partition)
837 fprintf (f, " in_other_partition");
838 if (node->used_from_other_partition)
839 fprintf (f, " used_from_other_partition");
840 if (node->force_output)
841 fprintf (f, " force_output");
842 if (node->forced_by_abi)
843 fprintf (f, " forced_by_abi");
844 if (node->externally_visible)
845 fprintf (f, " externally_visible");
846 if (node->resolution != LDPR_UNKNOWN)
847 fprintf (f, " %s",
848 ld_plugin_symbol_resolution_names[(int)node->resolution]);
849 if (TREE_ASM_WRITTEN (node->decl))
850 fprintf (f, " asm_written");
851 if (DECL_EXTERNAL (node->decl))
852 fprintf (f, " external");
853 if (TREE_PUBLIC (node->decl))
854 fprintf (f, " public");
855 if (DECL_COMMON (node->decl))
856 fprintf (f, " common");
857 if (DECL_WEAK (node->decl))
858 fprintf (f, " weak");
859 if (DECL_DLLIMPORT_P (node->decl))
860 fprintf (f, " dll_import");
861 if (DECL_COMDAT (node->decl))
862 fprintf (f, " comdat");
863 if (node->get_comdat_group ())
864 fprintf (f, " comdat_group:%s",
865 IDENTIFIER_POINTER (node->get_comdat_group_id ()));
866 if (DECL_ONE_ONLY (node->decl))
867 fprintf (f, " one_only");
868 if (node->get_section ())
869 fprintf (f, " section:%s",
870 node->get_section ());
871 if (node->implicit_section)
872 fprintf (f," (implicit_section)");
873 if (DECL_VISIBILITY_SPECIFIED (node->decl))
874 fprintf (f, " visibility_specified");
875 if (DECL_VISIBILITY (node->decl))
876 fprintf (f, " visibility:%s",
877 visibility_types [DECL_VISIBILITY (node->decl)]);
878 if (DECL_VIRTUAL_P (node->decl))
879 fprintf (f, " virtual");
880 if (DECL_ARTIFICIAL (node->decl))
881 fprintf (f, " artificial");
882 if (TREE_CODE (node->decl) == FUNCTION_DECL)
884 if (DECL_STATIC_CONSTRUCTOR (node->decl))
885 fprintf (f, " constructor");
886 if (DECL_STATIC_DESTRUCTOR (node->decl))
887 fprintf (f, " destructor");
889 fprintf (f, "\n");
891 if (node->same_comdat_group)
892 fprintf (f, " Same comdat group as: %s/%i\n",
893 node->same_comdat_group->asm_name (),
894 node->same_comdat_group->order);
895 if (node->next_sharing_asm_name)
896 fprintf (f, " next sharing asm name: %i\n",
897 node->next_sharing_asm_name->order);
898 if (node->previous_sharing_asm_name)
899 fprintf (f, " previous sharing asm name: %i\n",
900 node->previous_sharing_asm_name->order);
902 if (node->address_taken)
903 fprintf (f, " Address is taken.\n");
904 if (node->aux)
906 fprintf (f, " Aux:");
907 dump_addr (f, " @", (void *)node->aux);
910 fprintf (f, " References: ");
911 node->dump_references (f);
912 fprintf (f, " Referring: ");
913 node->dump_referring (f);
914 if (node->lto_file_data)
915 fprintf (f, " Read from file: %s\n",
916 node->lto_file_data->file_name);
919 /* Dump symtab node. */
921 void
922 dump_symtab_node (FILE *f, symtab_node *node)
924 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
925 dump_cgraph_node (f, cnode);
926 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
927 dump_varpool_node (f, vnode);
930 /* Dump symbol table. */
932 void
933 dump_symtab (FILE *f)
935 symtab_node *node;
936 fprintf (f, "Symbol table:\n\n");
937 FOR_EACH_SYMBOL (node)
938 dump_symtab_node (f, node);
941 /* Dump symtab node NODE to stderr. */
943 DEBUG_FUNCTION void
944 debug_symtab_node (symtab_node *node)
946 dump_symtab_node (stderr, node);
949 /* Dump symbol table to stderr. */
951 DEBUG_FUNCTION void
952 debug_symtab (void)
954 dump_symtab (stderr);
957 /* Verify common part of symtab nodes. */
959 DEBUG_FUNCTION bool
960 verify_symtab_base (symtab_node *node)
962 bool error_found = false;
963 symtab_node *hashed_node;
965 if (is_a <cgraph_node *> (node))
967 if (TREE_CODE (node->decl) != FUNCTION_DECL)
969 error ("function symbol is not function");
970 error_found = true;
973 else if (is_a <varpool_node *> (node))
975 if (TREE_CODE (node->decl) != VAR_DECL)
977 error ("variable symbol is not variable");
978 error_found = true;
981 else
983 error ("node has unknown type");
984 error_found = true;
987 if (cgraph_state != CGRAPH_LTO_STREAMING)
989 hashed_node = symtab_get_node (node->decl);
990 if (!hashed_node)
992 error ("node not found node->decl->decl_with_vis.symtab_node");
993 error_found = true;
995 if (hashed_node != node
996 && (!is_a <cgraph_node *> (node)
997 || !dyn_cast <cgraph_node *> (node)->clone_of
998 || dyn_cast <cgraph_node *> (node)->clone_of->decl
999 != node->decl))
1001 error ("node differs from node->decl->decl_with_vis.symtab_node");
1002 error_found = true;
1005 if (assembler_name_hash)
1007 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
1008 if (hashed_node && hashed_node->previous_sharing_asm_name)
1010 error ("assembler name hash list corrupted");
1011 error_found = true;
1013 while (hashed_node)
1015 if (hashed_node == node)
1016 break;
1017 hashed_node = hashed_node->next_sharing_asm_name;
1019 if (!hashed_node
1020 && !(is_a <varpool_node *> (node)
1021 || DECL_HARD_REGISTER (node->decl)))
1023 error ("node not found in symtab assembler name hash");
1024 error_found = true;
1027 if (node->previous_sharing_asm_name
1028 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
1030 error ("double linked list of assembler names corrupted");
1031 error_found = true;
1033 if (node->analyzed && !node->definition)
1035 error ("node is analyzed byt it is not a definition");
1036 error_found = true;
1038 if (node->cpp_implicit_alias && !node->alias)
1040 error ("node is alias but not implicit alias");
1041 error_found = true;
1043 if (node->alias && !node->definition
1044 && !node->weakref)
1046 error ("node is alias but not definition");
1047 error_found = true;
1049 if (node->weakref && !node->alias)
1051 error ("node is weakref but not an alias");
1052 error_found = true;
1054 if (node->same_comdat_group)
1056 symtab_node *n = node->same_comdat_group;
1058 if (!n->get_comdat_group ())
1060 error ("node is in same_comdat_group list but has no comdat_group");
1061 error_found = true;
1063 if (n->get_comdat_group () != node->get_comdat_group ())
1065 error ("same_comdat_group list across different groups");
1066 error_found = true;
1068 if (!n->definition)
1070 error ("Node has same_comdat_group but it is not a definition");
1071 error_found = true;
1073 if (n->type != node->type)
1075 error ("mixing different types of symbol in same comdat groups is not supported");
1076 error_found = true;
1078 if (n == node)
1080 error ("node is alone in a comdat group");
1081 error_found = true;
1085 if (!n->same_comdat_group)
1087 error ("same_comdat_group is not a circular list");
1088 error_found = true;
1089 break;
1091 n = n->same_comdat_group;
1093 while (n != node);
1094 if (symtab_comdat_local_p (node))
1096 struct ipa_ref *ref = NULL;
1098 for (int i = 0; node->iterate_referring (i, ref); ++i)
1100 if (!symtab_in_same_comdat_p (ref->referring, node))
1102 error ("comdat-local symbol referred to by %s outside its "
1103 "comdat",
1104 identifier_to_locale (ref->referring->name()));
1105 error_found = true;
1110 if (node->implicit_section && !node->get_section ())
1112 error ("implicit_section flag is set but section isn't");
1113 error_found = true;
1115 if (node->get_section () && node->get_comdat_group ()
1116 && !node->implicit_section)
1118 error ("Both section and comdat group is set");
1119 error_found = true;
1121 /* TODO: Add string table for sections, so we do not keep holding duplicated
1122 strings. */
1123 if (node->alias && node->definition
1124 && node->get_section () != symtab_alias_target (node)->get_section ()
1125 && (!node->get_section()
1126 || !symtab_alias_target (node)->get_section ()
1127 || strcmp (node->get_section(),
1128 symtab_alias_target (node)->get_section ())))
1130 error ("Alias and target's section differs");
1131 dump_symtab_node (stderr, symtab_alias_target (node));
1132 error_found = true;
1134 if (node->alias && node->definition
1135 && node->get_comdat_group () != symtab_alias_target (node)->get_comdat_group ())
1137 error ("Alias and target's comdat groups differs");
1138 dump_symtab_node (stderr, symtab_alias_target (node));
1139 error_found = true;
1142 return error_found;
1145 /* Verify consistency of NODE. */
1147 DEBUG_FUNCTION void
1148 verify_symtab_node (symtab_node *node)
1150 if (seen_error ())
1151 return;
1153 timevar_push (TV_CGRAPH_VERIFY);
1154 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1155 verify_cgraph_node (cnode);
1156 else
1157 if (verify_symtab_base (node))
1159 dump_symtab_node (stderr, node);
1160 internal_error ("verify_symtab_node failed");
1162 timevar_pop (TV_CGRAPH_VERIFY);
1165 /* Verify symbol table for internal consistency. */
1167 DEBUG_FUNCTION void
1168 verify_symtab (void)
1170 symtab_node *node;
1171 hash_map<tree, symtab_node *> comdat_head_map (251);
1173 FOR_EACH_SYMBOL (node)
1175 verify_symtab_node (node);
1176 if (node->get_comdat_group ())
1178 symtab_node **entry, *s;
1179 bool existed;
1181 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1182 &existed);
1183 if (!existed)
1184 *entry = node;
1185 else
1186 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1187 if (!s || s == *entry)
1189 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1190 dump_symtab_node (stderr, *entry);
1191 dump_symtab_node (stderr, s);
1192 internal_error ("verify_symtab failed");
1198 /* Return true when RESOLUTION indicate that linker will use
1199 the symbol from non-LTO object files. */
1201 bool
1202 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
1204 return (resolution == LDPR_PREVAILING_DEF
1205 || resolution == LDPR_PREEMPTED_REG
1206 || resolution == LDPR_RESOLVED_EXEC
1207 || resolution == LDPR_RESOLVED_DYN);
1210 /* Return true when NODE is known to be used from other (non-LTO) object file.
1211 Known only when doing LTO via linker plugin. */
1213 bool
1214 symtab_used_from_object_file_p (symtab_node *node)
1216 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1217 return false;
1218 if (resolution_used_from_other_file_p (node->resolution))
1219 return true;
1220 return false;
1223 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1224 but other code such as notice_global_symbol generates rtl. */
1226 void
1227 symtab_make_decl_local (tree decl)
1229 rtx rtl, symbol;
1231 /* Avoid clearing comdat_groups on comdat-local decls. */
1232 if (TREE_PUBLIC (decl) == 0)
1233 return;
1235 if (TREE_CODE (decl) == VAR_DECL)
1236 DECL_COMMON (decl) = 0;
1237 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1239 DECL_COMDAT (decl) = 0;
1240 DECL_WEAK (decl) = 0;
1241 DECL_EXTERNAL (decl) = 0;
1242 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1243 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1244 TREE_PUBLIC (decl) = 0;
1245 if (!DECL_RTL_SET_P (decl))
1246 return;
1248 /* Update rtl flags. */
1249 make_decl_rtl (decl);
1251 rtl = DECL_RTL (decl);
1252 if (!MEM_P (rtl))
1253 return;
1255 symbol = XEXP (rtl, 0);
1256 if (GET_CODE (symbol) != SYMBOL_REF)
1257 return;
1259 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1262 /* Return availability of NODE. */
1264 enum availability
1265 symtab_node_availability (symtab_node *node)
1267 if (is_a <cgraph_node *> (node))
1268 return cgraph_function_body_availability (cgraph (node));
1269 else
1270 return cgraph_variable_initializer_availability (varpool (node));
1273 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
1274 If NODE is not an alias, return NODE.
1275 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1277 symtab_node *
1278 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
1280 bool weakref_p = false;
1282 if (!node->alias)
1284 if (availability)
1285 *availability = symtab_node_availability (node);
1286 return node;
1289 /* To determine visibility of the target, we follow ELF semantic of aliases.
1290 Here alias is an alternative assembler name of a given definition. Its
1291 availability prevails the availability of its target (i.e. static alias of
1292 weak definition is available.
1294 Weakref is a different animal (and not part of ELF per se). It is just
1295 alternative name of a given symbol used within one complation unit
1296 and is translated prior hitting the object file. It inherits the
1297 visibility of its target (i.e. weakref of non-overwritable definition
1298 is non-overwritable, while weakref of weak definition is weak).
1300 If we ever get into supporting targets with different semantics, a target
1301 hook will be needed here. */
1303 if (availability)
1305 weakref_p = node->weakref;
1306 if (!weakref_p)
1307 *availability = symtab_node_availability (node);
1308 else
1309 *availability = AVAIL_LOCAL;
1311 while (node)
1313 if (node->alias && node->analyzed)
1314 node = symtab_alias_target (node);
1315 else
1317 if (!availability)
1319 else if (node->analyzed)
1321 if (weakref_p)
1323 enum availability a = symtab_node_availability (node);
1324 if (a < *availability)
1325 *availability = a;
1328 else
1329 *availability = AVAIL_NOT_AVAILABLE;
1330 return node;
1332 if (node && availability && weakref_p)
1334 enum availability a = symtab_node_availability (node);
1335 if (a < *availability)
1336 *availability = a;
1337 weakref_p = node->weakref;
1340 if (availability)
1341 *availability = AVAIL_NOT_AVAILABLE;
1342 return NULL;
1345 /* C++ FE sometimes change linkage flags after producing same body aliases.
1347 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1348 are obviously equivalent. The way it is doing so is however somewhat
1349 kludgy and interferes with the visibility code. As a result we need to
1350 copy the visibility from the target to get things right. */
1352 void
1353 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1355 if (is_a <cgraph_node *> (node))
1357 DECL_DECLARED_INLINE_P (node->decl)
1358 = DECL_DECLARED_INLINE_P (target->decl);
1359 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1360 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1362 /* FIXME: It is not really clear why those flags should not be copied for
1363 functions, too. */
1364 else
1366 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1367 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1368 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1370 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1371 if (TREE_PUBLIC (node->decl))
1373 tree group;
1375 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1376 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1377 group = target->get_comdat_group ();
1378 node->set_comdat_group (group);
1379 if (group
1380 && !node->same_comdat_group)
1381 symtab_add_to_same_comdat_group (node, target);
1383 node->externally_visible = target->externally_visible;
1386 /* Hash sections by their names. */
1388 static hashval_t
1389 hash_section_hash_entry (const void *p)
1391 const section_hash_entry *n = (const section_hash_entry *) p;
1392 return htab_hash_string (n->name);
1395 /* Return true if section P1 name equals to P2. */
1397 static int
1398 eq_sections (const void *p1, const void *p2)
1400 const section_hash_entry *n1 = (const section_hash_entry *) p1;
1401 const char *name = (const char *)p2;
1402 return n1->name == name || !strcmp (n1->name, name);
1405 /* Set section, do not recurse into aliases.
1406 When one wants to change section of symbol and its aliases,
1407 use set_section */
1409 void
1410 symtab_node::set_section_for_node (const char *section)
1412 const char *current = get_section ();
1413 void **slot;
1415 if (current == section
1416 || (current && section
1417 && !strcmp (current, section)))
1418 return;
1420 if (current)
1422 x_section->ref_count--;
1423 if (!x_section->ref_count)
1425 slot = htab_find_slot_with_hash (section_hash, x_section->name,
1426 htab_hash_string (x_section->name),
1427 INSERT);
1428 ggc_free (x_section);
1429 htab_clear_slot (section_hash, slot);
1431 x_section = NULL;
1433 if (!section)
1435 implicit_section = false;
1436 return;
1438 if (!section_hash)
1439 section_hash = htab_create_ggc (10, hash_section_hash_entry,
1440 eq_sections, NULL);
1441 slot = htab_find_slot_with_hash (section_hash, section,
1442 htab_hash_string (section),
1443 INSERT);
1444 if (*slot)
1445 x_section = (section_hash_entry *)*slot;
1446 else
1448 int len = strlen (section);
1449 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1450 x_section->name = ggc_vec_alloc<char> (len + 1);
1451 memcpy (x_section->name, section, len + 1);
1453 x_section->ref_count++;
1456 /* Worker for set_section. */
1458 static bool
1459 set_section_1 (struct symtab_node *n, void *s)
1461 n->set_section_for_node ((char *)s);
1462 return false;
1465 /* Set section of symbol and its aliases. */
1467 void
1468 symtab_node::set_section (const char *section)
1470 gcc_assert (!this->alias);
1471 symtab_for_node_and_aliases (this, set_section_1, const_cast<char *>(section), true);
1474 /* Return the initialization priority. */
1476 priority_type
1477 symtab_node::get_init_priority ()
1479 struct symbol_priority_map *h;
1480 struct symbol_priority_map in;
1482 if (!this->in_init_priority_hash)
1483 return DEFAULT_INIT_PRIORITY;
1484 in.symbol = this;
1485 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1486 return h ? h->init : DEFAULT_INIT_PRIORITY;
1489 /* Return the finalization priority. */
1491 priority_type
1492 cgraph_node::get_fini_priority ()
1494 struct symbol_priority_map *h;
1495 struct symbol_priority_map in;
1497 if (!this->in_init_priority_hash)
1498 return DEFAULT_INIT_PRIORITY;
1499 in.symbol = this;
1500 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1501 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1504 /* Return true if the from tree in both priority maps are equal. */
1507 symbol_priority_map_eq (const void *va, const void *vb)
1509 const struct symbol_priority_map *const a = (const struct symbol_priority_map *) va,
1510 *const b = (const struct symbol_priority_map *) vb;
1511 return (a->symbol == b->symbol);
1514 /* Hash a from symbol in a symbol_priority_map. */
1516 unsigned int
1517 symbol_priority_map_hash (const void *item)
1519 return htab_hash_pointer (((const struct symbol_priority_map *)item)->symbol);
1522 /* Return the initialization and finalization priority information for
1523 DECL. If there is no previous priority information, a freshly
1524 allocated structure is returned. */
1526 static struct symbol_priority_map *
1527 symbol_priority_info (struct symtab_node *symbol)
1529 struct symbol_priority_map in;
1530 struct symbol_priority_map *h;
1531 void **loc;
1533 in.symbol = symbol;
1534 if (!init_priority_hash)
1535 init_priority_hash = htab_create_ggc (512, symbol_priority_map_hash,
1536 symbol_priority_map_eq, 0);
1538 loc = htab_find_slot (init_priority_hash, &in, INSERT);
1539 h = (struct symbol_priority_map *) *loc;
1540 if (!h)
1542 h = ggc_cleared_alloc<symbol_priority_map> ();
1543 *loc = h;
1544 h->symbol = symbol;
1545 h->init = DEFAULT_INIT_PRIORITY;
1546 h->fini = DEFAULT_INIT_PRIORITY;
1547 symbol->in_init_priority_hash = true;
1550 return h;
1553 /* Set initialization priority to PRIORITY. */
1555 void
1556 symtab_node::set_init_priority (priority_type priority)
1558 struct symbol_priority_map *h;
1560 if (is_a <cgraph_node *> (this))
1561 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1563 if (priority == DEFAULT_INIT_PRIORITY)
1565 gcc_assert (get_init_priority() == priority);
1566 return;
1568 h = symbol_priority_info (this);
1569 h->init = priority;
1572 /* Set fialization priority to PRIORITY. */
1574 void
1575 cgraph_node::set_fini_priority (priority_type priority)
1577 struct symbol_priority_map *h;
1579 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1581 if (priority == DEFAULT_INIT_PRIORITY)
1583 gcc_assert (get_fini_priority() == priority);
1584 return;
1586 h = symbol_priority_info (this);
1587 h->fini = priority;
1590 /* Worker for symtab_resolve_alias. */
1592 static bool
1593 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
1595 n->implicit_section = true;
1596 return false;
1599 /* Add reference recording that NODE is alias of TARGET.
1600 The function can fail in the case of aliasing cycles; in this case
1601 it returns false. */
1603 bool
1604 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1606 symtab_node *n;
1608 gcc_assert (!node->analyzed
1609 && !vec_safe_length (node->ref_list.references));
1611 /* Never let cycles to creep into the symbol table alias references;
1612 those will make alias walkers to be infinite. */
1613 for (n = target; n && n->alias;
1614 n = n->analyzed ? symtab_alias_target (n) : NULL)
1615 if (n == node)
1617 if (is_a <cgraph_node *> (node))
1618 error ("function %q+D part of alias cycle", node->decl);
1619 else if (is_a <varpool_node *> (node))
1620 error ("variable %q+D part of alias cycle", node->decl);
1621 else
1622 gcc_unreachable ();
1623 node->alias = false;
1624 return false;
1627 /* "analyze" the node - i.e. mark the reference. */
1628 node->definition = true;
1629 node->alias = true;
1630 node->analyzed = true;
1631 node->add_reference (target, IPA_REF_ALIAS, NULL);
1633 /* Add alias into the comdat group of its target unless it is already there. */
1634 if (node->same_comdat_group)
1635 symtab_remove_from_same_comdat_group (node);
1636 node->set_comdat_group (NULL);
1637 if (target->get_comdat_group ())
1638 symtab_add_to_same_comdat_group (node, target);
1640 if ((node->get_section () != target->get_section ()
1641 || target->get_comdat_group ())
1642 && node->get_section () && !node->implicit_section)
1644 error ("section of alias %q+D must match section of its target",
1645 node->decl);
1647 symtab_for_node_and_aliases (node, set_section_1,
1648 const_cast<char *>(target->get_section ()), true);
1649 if (target->implicit_section)
1650 symtab_for_node_and_aliases (node,
1651 set_implicit_section, NULL, true);
1653 /* Alias targets become redundant after alias is resolved into an reference.
1654 We do not want to keep it around or we would have to mind updating them
1655 when renaming symbols. */
1656 node->alias_target = NULL;
1658 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1659 fixup_same_cpp_alias_visibility (node, target);
1661 /* If alias has address taken, so does the target. */
1662 if (node->address_taken)
1663 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1664 return true;
1667 /* Call calback on NODE and aliases associated to NODE.
1668 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1669 skipped. */
1671 bool
1672 symtab_for_node_and_aliases (symtab_node *node,
1673 bool (*callback) (symtab_node *, void *),
1674 void *data,
1675 bool include_overwritable)
1677 int i;
1678 struct ipa_ref *ref;
1680 if (callback (node, data))
1681 return true;
1682 for (i = 0; node->iterate_referring (i, ref); i++)
1683 if (ref->use == IPA_REF_ALIAS)
1685 symtab_node *alias = ref->referring;
1686 if (include_overwritable
1687 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1688 if (symtab_for_node_and_aliases (alias, callback, data,
1689 include_overwritable))
1690 return true;
1692 return false;
1695 /* Worker searching nonoverwritable alias. */
1697 static bool
1698 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1700 if (decl_binds_to_current_def_p (node->decl))
1702 *(symtab_node **)data = node;
1703 return true;
1705 return false;
1708 /* If NODE can not be overwriten by static or dynamic linker to point to different
1709 definition, return NODE. Otherwise look for alias with such property and if
1710 none exists, introduce new one. */
1712 symtab_node *
1713 symtab_nonoverwritable_alias (symtab_node *node)
1715 tree new_decl;
1716 symtab_node *new_node = NULL;
1718 /* First try to look up existing alias or base object
1719 (if that is already non-overwritable). */
1720 node = symtab_alias_ultimate_target (node, NULL);
1721 gcc_assert (!node->alias && !node->weakref);
1722 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1723 (void *)&new_node, true);
1724 if (new_node)
1725 return new_node;
1726 #ifndef ASM_OUTPUT_DEF
1727 /* If aliases aren't supported by the assembler, fail. */
1728 return NULL;
1729 #endif
1731 /* Otherwise create a new one. */
1732 new_decl = copy_node (node->decl);
1733 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1734 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1735 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1736 DECL_INITIAL (new_decl) = NULL;
1737 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1738 SET_DECL_RTL (new_decl, NULL);
1740 /* Update the properties. */
1741 DECL_EXTERNAL (new_decl) = 0;
1742 TREE_PUBLIC (new_decl) = 0;
1743 DECL_COMDAT (new_decl) = 0;
1744 DECL_WEAK (new_decl) = 0;
1746 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1747 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1748 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1750 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1751 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1752 new_node = cgraph_create_function_alias
1753 (new_decl, node->decl);
1755 else
1757 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1758 DECL_INITIAL (new_decl) = error_mark_node;
1759 new_node = varpool_create_variable_alias (new_decl, node->decl);
1761 symtab_resolve_alias (new_node, node);
1762 gcc_assert (decl_binds_to_current_def_p (new_decl)
1763 && targetm.binds_local_p (new_decl));
1764 return new_node;
1767 /* Return true if A and B represents semantically equivalent symbols. */
1769 bool
1770 symtab_semantically_equivalent_p (symtab_node *a,
1771 symtab_node *b)
1773 enum availability avail;
1774 symtab_node *ba;
1775 symtab_node *bb;
1777 /* Equivalent functions are equivalent. */
1778 if (a->decl == b->decl)
1779 return true;
1781 /* If symbol is not overwritable by different implementation,
1782 walk to the base object it defines. */
1783 ba = symtab_alias_ultimate_target (a, &avail);
1784 if (avail >= AVAIL_AVAILABLE)
1786 if (ba == b)
1787 return true;
1789 else
1790 ba = a;
1791 bb = symtab_alias_ultimate_target (b, &avail);
1792 if (avail >= AVAIL_AVAILABLE)
1794 if (a == bb)
1795 return true;
1797 else
1798 bb = b;
1799 return bb == ba;
1802 /* Classify symbol NODE for partitioning. */
1804 enum symbol_partitioning_class
1805 symtab_get_symbol_partitioning_class (symtab_node *node)
1807 /* Inline clones are always duplicated.
1808 This include external delcarations. */
1809 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1811 if (DECL_ABSTRACT (node->decl))
1812 return SYMBOL_EXTERNAL;
1814 if (cnode && cnode->global.inlined_to)
1815 return SYMBOL_DUPLICATE;
1817 /* Weakref aliases are always duplicated. */
1818 if (node->weakref)
1819 return SYMBOL_DUPLICATE;
1821 /* External declarations are external. */
1822 if (DECL_EXTERNAL (node->decl))
1823 return SYMBOL_EXTERNAL;
1825 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
1827 /* Constant pool references use local symbol names that can not
1828 be promoted global. We should never put into a constant pool
1829 objects that can not be duplicated across partitions. */
1830 if (DECL_IN_CONSTANT_POOL (node->decl))
1831 return SYMBOL_DUPLICATE;
1832 gcc_checking_assert (vnode->definition);
1834 /* Functions that are cloned may stay in callgraph even if they are unused.
1835 Handle them as external; compute_ltrans_boundary take care to make
1836 proper things to happen (i.e. to make them appear in the boundary but
1837 with body streamed, so clone can me materialized). */
1838 else if (!cgraph (node)->definition)
1839 return SYMBOL_EXTERNAL;
1841 /* Linker discardable symbols are duplicated to every use unless they are
1842 keyed. */
1843 if (DECL_ONE_ONLY (node->decl)
1844 && !node->force_output
1845 && !node->forced_by_abi
1846 && !symtab_used_from_object_file_p (node))
1847 return SYMBOL_DUPLICATE;
1849 return SYMBOL_PARTITION;
1851 #include "gt-symtab.h"