PR c/61852
[official-gcc.git] / gcc / symtab.c
blob005057384e37201d41e41c439d8807b0e3b96eb7
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"
46 #include "calls.h"
48 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
50 const char * const ld_plugin_symbol_resolution_names[]=
52 "",
53 "undef",
54 "prevailing_def",
55 "prevailing_def_ironly",
56 "preempted_reg",
57 "preempted_ir",
58 "resolved_ir",
59 "resolved_exec",
60 "resolved_dyn",
61 "prevailing_def_ironly_exp"
65 /* Hash table used to hold sectoons. */
66 static GTY((param_is (section_hash_entry))) htab_t section_hash;
68 /* Hash table used to convert assembler names into nodes. */
69 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
71 /* Map from a symbol to initialization/finalization priorities. */
72 struct GTY(()) symbol_priority_map {
73 symtab_node *symbol;
74 priority_type init;
75 priority_type fini;
78 /* Hash table used to hold init priorities. */
79 static GTY ((param_is (struct symbol_priority_map)))
80 htab_t init_priority_hash;
82 /* Linked list of symbol table nodes. */
83 symtab_node *symtab_nodes;
85 /* The order index of the next symtab node to be created. This is
86 used so that we can sort the cgraph nodes in order by when we saw
87 them, to support -fno-toplevel-reorder. */
88 int symtab_order;
90 /* Hash asmnames ignoring the user specified marks. */
92 static hashval_t
93 decl_assembler_name_hash (const_tree asmname)
95 if (IDENTIFIER_POINTER (asmname)[0] == '*')
97 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
98 size_t ulp_len = strlen (user_label_prefix);
100 if (ulp_len == 0)
102 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
103 decl_str += ulp_len;
105 return htab_hash_string (decl_str);
108 return htab_hash_string (IDENTIFIER_POINTER (asmname));
112 /* Returns a hash code for P. */
114 static hashval_t
115 hash_node_by_assembler_name (const void *p)
117 const symtab_node *n = (const symtab_node *) p;
118 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
121 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
123 static bool
124 decl_assembler_name_equal (tree decl, const_tree asmname)
126 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
127 const char *decl_str;
128 const char *asmname_str;
129 bool test = false;
131 if (decl_asmname == asmname)
132 return true;
134 decl_str = IDENTIFIER_POINTER (decl_asmname);
135 asmname_str = IDENTIFIER_POINTER (asmname);
138 /* If the target assembler name was set by the user, things are trickier.
139 We have a leading '*' to begin with. After that, it's arguable what
140 is the correct thing to do with -fleading-underscore. Arguably, we've
141 historically been doing the wrong thing in assemble_alias by always
142 printing the leading underscore. Since we're not changing that, make
143 sure user_label_prefix follows the '*' before matching. */
144 if (decl_str[0] == '*')
146 size_t ulp_len = strlen (user_label_prefix);
148 decl_str ++;
150 if (ulp_len == 0)
151 test = true;
152 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
153 decl_str += ulp_len, test=true;
154 else
155 decl_str --;
157 if (asmname_str[0] == '*')
159 size_t ulp_len = strlen (user_label_prefix);
161 asmname_str ++;
163 if (ulp_len == 0)
164 test = true;
165 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
166 asmname_str += ulp_len, test=true;
167 else
168 asmname_str --;
171 if (!test)
172 return false;
173 return strcmp (decl_str, asmname_str) == 0;
177 /* Returns nonzero if P1 and P2 are equal. */
179 static int
180 eq_assembler_name (const void *p1, const void *p2)
182 const symtab_node *n1 = (const symtab_node *) p1;
183 const_tree name = (const_tree)p2;
184 return (decl_assembler_name_equal (n1->decl, name));
187 /* Insert NODE to assembler name hash. */
189 static void
190 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
192 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
193 return;
194 gcc_checking_assert (!node->previous_sharing_asm_name
195 && !node->next_sharing_asm_name);
196 if (assembler_name_hash)
198 void **aslot;
199 struct cgraph_node *cnode;
200 tree decl = node->decl;
202 tree name = DECL_ASSEMBLER_NAME (node->decl);
204 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
205 decl_assembler_name_hash (name),
206 INSERT);
207 gcc_assert (*aslot != node);
208 node->next_sharing_asm_name = (symtab_node *)*aslot;
209 if (*aslot != NULL)
210 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
211 *aslot = node;
213 /* Update also possible inline clones sharing a decl. */
214 cnode = dyn_cast <cgraph_node *> (node);
215 if (cnode && cnode->clones && with_clones)
216 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
217 if (cnode->decl == decl)
218 insert_to_assembler_name_hash (cnode, true);
223 /* Remove NODE from assembler name hash. */
225 static void
226 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
228 if (assembler_name_hash)
230 struct cgraph_node *cnode;
231 tree decl = node->decl;
233 if (node->next_sharing_asm_name)
234 node->next_sharing_asm_name->previous_sharing_asm_name
235 = node->previous_sharing_asm_name;
236 if (node->previous_sharing_asm_name)
238 node->previous_sharing_asm_name->next_sharing_asm_name
239 = node->next_sharing_asm_name;
241 else
243 tree name = DECL_ASSEMBLER_NAME (node->decl);
244 void **slot;
245 slot = htab_find_slot_with_hash (assembler_name_hash, name,
246 decl_assembler_name_hash (name),
247 NO_INSERT);
248 gcc_assert (*slot == node);
249 if (!node->next_sharing_asm_name)
250 htab_clear_slot (assembler_name_hash, slot);
251 else
252 *slot = node->next_sharing_asm_name;
254 node->next_sharing_asm_name = NULL;
255 node->previous_sharing_asm_name = NULL;
257 /* Update also possible inline clones sharing a decl. */
258 cnode = dyn_cast <cgraph_node *> (node);
259 if (cnode && cnode->clones && with_clones)
260 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
261 if (cnode->decl == decl)
262 unlink_from_assembler_name_hash (cnode, true);
266 /* Arrange node to be first in its entry of assembler_name_hash. */
268 void
269 symtab_prevail_in_asm_name_hash (symtab_node *node)
271 unlink_from_assembler_name_hash (node, false);
272 insert_to_assembler_name_hash (node, false);
276 /* Add node into symbol table. This function is not used directly, but via
277 cgraph/varpool node creation routines. */
279 void
280 symtab_register_node (symtab_node *node)
282 node->next = symtab_nodes;
283 node->previous = NULL;
284 if (symtab_nodes)
285 symtab_nodes->previous = node;
286 symtab_nodes = node;
288 if (!node->decl->decl_with_vis.symtab_node)
289 node->decl->decl_with_vis.symtab_node = node;
291 node->ref_list.clear ();
293 node->order = symtab_order++;
295 /* Be sure to do this last; C++ FE might create new nodes via
296 DECL_ASSEMBLER_NAME langhook! */
297 insert_to_assembler_name_hash (node, false);
300 /* Remove NODE from same comdat group. */
302 void
303 symtab_remove_from_same_comdat_group (symtab_node *node)
305 if (node->same_comdat_group)
307 symtab_node *prev;
308 for (prev = node->same_comdat_group;
309 prev->same_comdat_group != node;
310 prev = prev->same_comdat_group)
312 if (node->same_comdat_group == prev)
313 prev->same_comdat_group = NULL;
314 else
315 prev->same_comdat_group = node->same_comdat_group;
316 node->same_comdat_group = NULL;
317 node->set_comdat_group (NULL_TREE);
321 /* Remove node from symbol table. This function is not used directly, but via
322 cgraph/varpool node removal routines. */
324 void
325 symtab_unregister_node (symtab_node *node)
327 node->remove_all_references ();
328 node->remove_all_referring ();
330 /* Remove reference to section. */
331 node->set_section_for_node (NULL);
333 symtab_remove_from_same_comdat_group (node);
335 if (node->previous)
336 node->previous->next = node->next;
337 else
338 symtab_nodes = node->next;
339 if (node->next)
340 node->next->previous = node->previous;
341 node->next = NULL;
342 node->previous = NULL;
344 /* During LTO symtab merging we temporarily corrupt decl to symtab node
345 hash. */
346 gcc_assert (node->decl->decl_with_vis.symtab_node || in_lto_p);
347 if (node->decl->decl_with_vis.symtab_node == node)
349 symtab_node *replacement_node = NULL;
350 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
351 replacement_node = cgraph_find_replacement_node (cnode);
352 node->decl->decl_with_vis.symtab_node = replacement_node;
354 if (!is_a <varpool_node *> (node) || !DECL_HARD_REGISTER (node->decl))
355 unlink_from_assembler_name_hash (node, false);
356 if (node->in_init_priority_hash)
358 struct symbol_priority_map in;
359 void **slot;
360 in.symbol = node;
362 slot = htab_find_slot (init_priority_hash, &in, NO_INSERT);
363 if (slot)
364 htab_clear_slot (init_priority_hash, slot);
369 /* Remove symtab NODE from the symbol table. */
371 void
372 symtab_remove_node (symtab_node *node)
374 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
375 cgraph_remove_node (cnode);
376 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
377 varpool_remove_node (vnode);
380 /* Initalize asm name hash unless. */
382 void
383 symtab_initialize_asm_name_hash (void)
385 symtab_node *node;
386 if (!assembler_name_hash)
388 assembler_name_hash =
389 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
390 NULL);
391 FOR_EACH_SYMBOL (node)
392 insert_to_assembler_name_hash (node, false);
396 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
397 Return NULL if there's no such node. */
399 symtab_node *
400 symtab_node_for_asm (const_tree asmname)
402 symtab_node *node;
403 void **slot;
405 symtab_initialize_asm_name_hash ();
406 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
407 decl_assembler_name_hash (asmname),
408 NO_INSERT);
410 if (slot)
412 node = (symtab_node *) *slot;
413 return node;
415 return NULL;
418 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
420 void
421 change_decl_assembler_name (tree decl, tree name)
423 symtab_node *node = NULL;
425 /* We can have user ASM names on things, like global register variables, that
426 are not in the symbol table. */
427 if ((TREE_CODE (decl) == VAR_DECL
428 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
429 || TREE_CODE (decl) == FUNCTION_DECL)
430 node = symtab_get_node (decl);
431 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
433 SET_DECL_ASSEMBLER_NAME (decl, name);
434 if (node)
435 insert_to_assembler_name_hash (node, true);
437 else
439 if (name == DECL_ASSEMBLER_NAME (decl))
440 return;
442 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
443 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
444 : NULL);
445 if (node)
446 unlink_from_assembler_name_hash (node, true);
447 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
448 && DECL_RTL_SET_P (decl))
449 warning (0, "%D renamed after being referenced in assembly", decl);
451 SET_DECL_ASSEMBLER_NAME (decl, name);
452 if (alias)
454 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
455 TREE_CHAIN (name) = alias;
457 if (node)
458 insert_to_assembler_name_hash (node, true);
462 /* Add NEW_ to the same comdat group that OLD is in. */
464 void
465 symtab_add_to_same_comdat_group (symtab_node *new_node,
466 symtab_node *old_node)
468 gcc_assert (old_node->get_comdat_group ());
469 gcc_assert (!new_node->same_comdat_group);
470 gcc_assert (new_node != old_node);
472 new_node->set_comdat_group (old_node->get_comdat_group ());
473 new_node->same_comdat_group = old_node;
474 if (!old_node->same_comdat_group)
475 old_node->same_comdat_group = new_node;
476 else
478 symtab_node *n;
479 for (n = old_node->same_comdat_group;
480 n->same_comdat_group != old_node;
481 n = n->same_comdat_group)
483 n->same_comdat_group = new_node;
487 /* Dissolve the same_comdat_group list in which NODE resides. */
489 void
490 symtab_dissolve_same_comdat_group_list (symtab_node *node)
492 symtab_node *n = node;
493 symtab_node *next;
495 if (!node->same_comdat_group)
496 return;
499 next = n->same_comdat_group;
500 n->same_comdat_group = NULL;
501 /* Clear comdat_group for comdat locals, since
502 make_decl_local doesn't. */
503 if (!TREE_PUBLIC (n->decl))
504 n->set_comdat_group (NULL);
505 n = next;
507 while (n != node);
510 /* Return printable assembler name of NODE.
511 This function is used only for debugging. When assembler name
512 is unknown go with identifier name. */
514 const char *
515 symtab_node::asm_name () const
517 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
518 return lang_hooks.decl_printable_name (decl, 2);
519 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
522 /* Return printable identifier name. */
524 const char *
525 symtab_node::name () const
527 return lang_hooks.decl_printable_name (decl, 2);
530 /* Return ipa reference from this symtab_node to
531 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
532 of the use. */
534 struct ipa_ref *
535 symtab_node::add_reference (symtab_node *referred_node,
536 enum ipa_ref_use use_type)
538 return add_reference (referred_node, use_type, NULL);
542 /* Return ipa reference from this symtab_node to
543 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
544 of the use and STMT the statement (if it exists). */
546 struct ipa_ref *
547 symtab_node::add_reference (symtab_node *referred_node,
548 enum ipa_ref_use use_type, gimple stmt)
550 struct ipa_ref *ref = NULL, *ref2 = NULL;
551 struct ipa_ref_list *list, *list2;
552 ipa_ref_t *old_references;
554 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
555 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
557 list = &ref_list;
558 old_references = vec_safe_address (list->references);
559 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
560 ref = &list->references->last ();
562 list2 = &referred_node->ref_list;
564 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
565 if(use_type == IPA_REF_ALIAS)
567 list2->referring.safe_insert (0, ref);
568 ref->referred_index = 0;
570 for (unsigned int i = 1; i < list2->referring.length (); i++)
571 list2->referring[i]->referred_index = i;
573 else
575 list2->referring.safe_push (ref);
576 ref->referred_index = list2->referring.length () - 1;
579 ref->referring = this;
580 ref->referred = referred_node;
581 ref->stmt = stmt;
582 ref->lto_stmt_uid = 0;
583 ref->use = use_type;
584 ref->speculative = 0;
586 /* If vector was moved in memory, update pointers. */
587 if (old_references != list->references->address ())
589 int i;
590 for (i = 0; iterate_reference(i, ref2); i++)
591 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
593 return ref;
596 /* If VAL is a reference to a function or a variable, add a reference from
597 this symtab_node to the corresponding symbol table node. USE_TYPE specify
598 type of the use and STMT the statement (if it exists). Return the new
599 reference or NULL if none was created. */
601 struct ipa_ref *
602 symtab_node::maybe_add_reference (tree val, enum ipa_ref_use use_type,
603 gimple stmt)
605 STRIP_NOPS (val);
606 if (TREE_CODE (val) != ADDR_EXPR)
607 return NULL;
608 val = get_base_var (val);
609 if (val && (TREE_CODE (val) == FUNCTION_DECL
610 || TREE_CODE (val) == VAR_DECL))
612 symtab_node *referred = symtab_get_node (val);
613 gcc_checking_assert (referred);
614 return add_reference (referred, use_type, stmt);
616 return NULL;
619 /* Clone all references from symtab NODE to this symtab_node. */
621 void
622 symtab_node::clone_references (struct symtab_node *node)
624 struct ipa_ref *ref = NULL, *ref2 = NULL;
625 int i;
626 for (i = 0; node->iterate_reference (i, ref); i++)
628 bool speculative = ref->speculative;
629 unsigned int stmt_uid = ref->lto_stmt_uid;
631 ref2 = add_reference (ref->referred, ref->use, ref->stmt);
632 ref2->speculative = speculative;
633 ref2->lto_stmt_uid = stmt_uid;
637 /* Clone all referring from symtab NODE to this symtab_node. */
639 void
640 symtab_node::clone_referring (struct symtab_node *node)
642 struct ipa_ref *ref = NULL, *ref2 = NULL;
643 int i;
644 for (i = 0; node->iterate_referring(i, ref); i++)
646 bool speculative = ref->speculative;
647 unsigned int stmt_uid = ref->lto_stmt_uid;
649 ref2 = ref->referring->add_reference (this, ref->use, ref->stmt);
650 ref2->speculative = speculative;
651 ref2->lto_stmt_uid = stmt_uid;
655 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
657 struct ipa_ref *
658 symtab_node::clone_reference (struct ipa_ref *ref, gimple stmt)
660 bool speculative = ref->speculative;
661 unsigned int stmt_uid = ref->lto_stmt_uid;
662 struct ipa_ref *ref2;
664 ref2 = add_reference (ref->referred, ref->use, stmt);
665 ref2->speculative = speculative;
666 ref2->lto_stmt_uid = stmt_uid;
667 return ref2;
670 /* Find the structure describing a reference to REFERRED_NODE
671 and associated with statement STMT. */
673 struct ipa_ref *
674 symtab_node::find_reference (symtab_node *referred_node,
675 gimple stmt, unsigned int lto_stmt_uid)
677 struct ipa_ref *r = NULL;
678 int i;
680 for (i = 0; iterate_reference (i, r); i++)
681 if (r->referred == referred_node
682 && !r->speculative
683 && ((stmt && r->stmt == stmt)
684 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
685 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
686 return r;
687 return NULL;
690 /* Remove all references that are associated with statement STMT. */
692 void
693 symtab_node::remove_stmt_references (gimple stmt)
695 struct ipa_ref *r = NULL;
696 int i = 0;
698 while (iterate_reference (i, r))
699 if (r->stmt == stmt)
700 r->remove_reference ();
701 else
702 i++;
705 /* Remove all stmt references in non-speculative references.
706 Those are not maintained during inlining & clonning.
707 The exception are speculative references that are updated along
708 with callgraph edges associated with them. */
710 void
711 symtab_node::clear_stmts_in_references (void)
713 struct ipa_ref *r = NULL;
714 int i;
716 for (i = 0; iterate_reference (i, r); i++)
717 if (!r->speculative)
719 r->stmt = NULL;
720 r->lto_stmt_uid = 0;
724 /* Remove all references in ref list. */
726 void
727 symtab_node::remove_all_references (void)
729 while (vec_safe_length (ref_list.references))
730 ref_list.references->last ().remove_reference ();
731 vec_free (ref_list.references);
734 /* Remove all referring items in ref list. */
736 void
737 symtab_node::remove_all_referring (void)
739 while (ref_list.referring.length ())
740 ref_list.referring.last ()->remove_reference ();
741 ref_list.referring.release ();
744 /* Dump references in ref list to FILE. */
746 void
747 symtab_node::dump_references (FILE *file)
749 struct ipa_ref *ref = NULL;
750 int i;
751 for (i = 0; iterate_reference (i, ref); i++)
753 fprintf (file, "%s/%i (%s)",
754 ref->referred->asm_name (),
755 ref->referred->order,
756 ipa_ref_use_name [ref->use]);
757 if (ref->speculative)
758 fprintf (file, " (speculative)");
760 fprintf (file, "\n");
763 /* Dump referring in list to FILE. */
765 void
766 symtab_node::dump_referring (FILE *file)
768 struct ipa_ref *ref = NULL;
769 int i;
770 for (i = 0; iterate_referring(i, ref); i++)
772 fprintf (file, "%s/%i (%s)",
773 ref->referring->asm_name (),
774 ref->referring->order,
775 ipa_ref_use_name [ref->use]);
776 if (ref->speculative)
777 fprintf (file, " (speculative)");
779 fprintf (file, "\n");
782 /* Return true if list contains an alias. */
783 bool
784 symtab_node::has_aliases_p (void)
786 struct ipa_ref *ref = NULL;
787 int i;
789 for (i = 0; iterate_referring (i, ref); i++)
790 if (ref->use == IPA_REF_ALIAS)
791 return true;
792 return false;
795 /* Iterates I-th reference in the list, REF is also set. */
797 struct ipa_ref *
798 symtab_node::iterate_reference (unsigned i, struct ipa_ref *&ref)
800 vec_safe_iterate (ref_list.references, i, &ref);
802 return ref;
805 /* Iterates I-th referring item in the list, REF is also set. */
807 struct ipa_ref *
808 symtab_node::iterate_referring (unsigned i, struct ipa_ref *&ref)
810 ref_list.referring.iterate (i, &ref);
812 return ref;
815 /* Iterates I-th referring alias item in the list, REF is also set. */
817 struct ipa_ref *
818 symtab_node::iterate_direct_aliases (unsigned i, struct ipa_ref *&ref)
820 ref_list.referring.iterate (i, &ref);
822 if (ref && ref->use != IPA_REF_ALIAS)
823 return NULL;
825 return ref;
829 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
831 /* Dump base fields of symtab nodes. Not to be used directly. */
833 void
834 dump_symtab_base (FILE *f, symtab_node *node)
836 static const char * const visibility_types[] = {
837 "default", "protected", "hidden", "internal"
840 fprintf (f, "%s/%i (%s)",
841 node->asm_name (),
842 node->order,
843 node->name ());
844 dump_addr (f, " @", (void *)node);
845 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
847 if (node->definition)
848 fprintf (f, " definition");
849 if (node->analyzed)
850 fprintf (f, " analyzed");
851 if (node->alias)
852 fprintf (f, " alias");
853 if (node->weakref)
854 fprintf (f, " weakref");
855 if (node->cpp_implicit_alias)
856 fprintf (f, " cpp_implicit_alias");
857 if (node->alias_target)
858 fprintf (f, " target:%s",
859 DECL_P (node->alias_target)
860 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
861 (node->alias_target))
862 : IDENTIFIER_POINTER (node->alias_target));
863 if (node->body_removed)
864 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
865 fprintf (f, "\n Visibility:");
866 if (node->in_other_partition)
867 fprintf (f, " in_other_partition");
868 if (node->used_from_other_partition)
869 fprintf (f, " used_from_other_partition");
870 if (node->force_output)
871 fprintf (f, " force_output");
872 if (node->forced_by_abi)
873 fprintf (f, " forced_by_abi");
874 if (node->externally_visible)
875 fprintf (f, " externally_visible");
876 if (node->resolution != LDPR_UNKNOWN)
877 fprintf (f, " %s",
878 ld_plugin_symbol_resolution_names[(int)node->resolution]);
879 if (TREE_ASM_WRITTEN (node->decl))
880 fprintf (f, " asm_written");
881 if (DECL_EXTERNAL (node->decl))
882 fprintf (f, " external");
883 if (TREE_PUBLIC (node->decl))
884 fprintf (f, " public");
885 if (DECL_COMMON (node->decl))
886 fprintf (f, " common");
887 if (DECL_WEAK (node->decl))
888 fprintf (f, " weak");
889 if (DECL_DLLIMPORT_P (node->decl))
890 fprintf (f, " dll_import");
891 if (DECL_COMDAT (node->decl))
892 fprintf (f, " comdat");
893 if (node->get_comdat_group ())
894 fprintf (f, " comdat_group:%s",
895 IDENTIFIER_POINTER (node->get_comdat_group_id ()));
896 if (DECL_ONE_ONLY (node->decl))
897 fprintf (f, " one_only");
898 if (node->get_section ())
899 fprintf (f, " section:%s",
900 node->get_section ());
901 if (node->implicit_section)
902 fprintf (f," (implicit_section)");
903 if (DECL_VISIBILITY_SPECIFIED (node->decl))
904 fprintf (f, " visibility_specified");
905 if (DECL_VISIBILITY (node->decl))
906 fprintf (f, " visibility:%s",
907 visibility_types [DECL_VISIBILITY (node->decl)]);
908 if (DECL_VIRTUAL_P (node->decl))
909 fprintf (f, " virtual");
910 if (DECL_ARTIFICIAL (node->decl))
911 fprintf (f, " artificial");
912 if (TREE_CODE (node->decl) == FUNCTION_DECL)
914 if (DECL_STATIC_CONSTRUCTOR (node->decl))
915 fprintf (f, " constructor");
916 if (DECL_STATIC_DESTRUCTOR (node->decl))
917 fprintf (f, " destructor");
919 fprintf (f, "\n");
921 if (node->same_comdat_group)
922 fprintf (f, " Same comdat group as: %s/%i\n",
923 node->same_comdat_group->asm_name (),
924 node->same_comdat_group->order);
925 if (node->next_sharing_asm_name)
926 fprintf (f, " next sharing asm name: %i\n",
927 node->next_sharing_asm_name->order);
928 if (node->previous_sharing_asm_name)
929 fprintf (f, " previous sharing asm name: %i\n",
930 node->previous_sharing_asm_name->order);
932 if (node->address_taken)
933 fprintf (f, " Address is taken.\n");
934 if (node->aux)
936 fprintf (f, " Aux:");
937 dump_addr (f, " @", (void *)node->aux);
940 fprintf (f, " References: ");
941 node->dump_references (f);
942 fprintf (f, " Referring: ");
943 node->dump_referring (f);
944 if (node->lto_file_data)
945 fprintf (f, " Read from file: %s\n",
946 node->lto_file_data->file_name);
949 /* Dump symtab node. */
951 void
952 dump_symtab_node (FILE *f, symtab_node *node)
954 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
955 dump_cgraph_node (f, cnode);
956 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
957 dump_varpool_node (f, vnode);
960 /* Dump symbol table. */
962 void
963 dump_symtab (FILE *f)
965 symtab_node *node;
966 fprintf (f, "Symbol table:\n\n");
967 FOR_EACH_SYMBOL (node)
968 dump_symtab_node (f, node);
971 /* Dump symtab node NODE to stderr. */
973 DEBUG_FUNCTION void
974 debug_symtab_node (symtab_node *node)
976 dump_symtab_node (stderr, node);
979 /* Dump symbol table to stderr. */
981 DEBUG_FUNCTION void
982 debug_symtab (void)
984 dump_symtab (stderr);
987 /* Verify common part of symtab nodes. */
989 DEBUG_FUNCTION bool
990 verify_symtab_base (symtab_node *node)
992 bool error_found = false;
993 symtab_node *hashed_node;
995 if (is_a <cgraph_node *> (node))
997 if (TREE_CODE (node->decl) != FUNCTION_DECL)
999 error ("function symbol is not function");
1000 error_found = true;
1003 else if (is_a <varpool_node *> (node))
1005 if (TREE_CODE (node->decl) != VAR_DECL)
1007 error ("variable symbol is not variable");
1008 error_found = true;
1011 else
1013 error ("node has unknown type");
1014 error_found = true;
1017 if (cgraph_state != CGRAPH_LTO_STREAMING)
1019 hashed_node = symtab_get_node (node->decl);
1020 if (!hashed_node)
1022 error ("node not found node->decl->decl_with_vis.symtab_node");
1023 error_found = true;
1025 if (hashed_node != node
1026 && (!is_a <cgraph_node *> (node)
1027 || !dyn_cast <cgraph_node *> (node)->clone_of
1028 || dyn_cast <cgraph_node *> (node)->clone_of->decl
1029 != node->decl))
1031 error ("node differs from node->decl->decl_with_vis.symtab_node");
1032 error_found = true;
1035 if (assembler_name_hash)
1037 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
1038 if (hashed_node && hashed_node->previous_sharing_asm_name)
1040 error ("assembler name hash list corrupted");
1041 error_found = true;
1043 while (hashed_node)
1045 if (hashed_node == node)
1046 break;
1047 hashed_node = hashed_node->next_sharing_asm_name;
1049 if (!hashed_node
1050 && !(is_a <varpool_node *> (node)
1051 || DECL_HARD_REGISTER (node->decl)))
1053 error ("node not found in symtab assembler name hash");
1054 error_found = true;
1057 if (node->previous_sharing_asm_name
1058 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
1060 error ("double linked list of assembler names corrupted");
1061 error_found = true;
1063 if (node->analyzed && !node->definition)
1065 error ("node is analyzed byt it is not a definition");
1066 error_found = true;
1068 if (node->cpp_implicit_alias && !node->alias)
1070 error ("node is alias but not implicit alias");
1071 error_found = true;
1073 if (node->alias && !node->definition
1074 && !node->weakref)
1076 error ("node is alias but not definition");
1077 error_found = true;
1079 if (node->weakref && !node->alias)
1081 error ("node is weakref but not an alias");
1082 error_found = true;
1084 if (node->same_comdat_group)
1086 symtab_node *n = node->same_comdat_group;
1088 if (!n->get_comdat_group ())
1090 error ("node is in same_comdat_group list but has no comdat_group");
1091 error_found = true;
1093 if (n->get_comdat_group () != node->get_comdat_group ())
1095 error ("same_comdat_group list across different groups");
1096 error_found = true;
1098 if (!n->definition)
1100 error ("Node has same_comdat_group but it is not a definition");
1101 error_found = true;
1103 if (n->type != node->type)
1105 error ("mixing different types of symbol in same comdat groups is not supported");
1106 error_found = true;
1108 if (n == node)
1110 error ("node is alone in a comdat group");
1111 error_found = true;
1115 if (!n->same_comdat_group)
1117 error ("same_comdat_group is not a circular list");
1118 error_found = true;
1119 break;
1121 n = n->same_comdat_group;
1123 while (n != node);
1124 if (symtab_comdat_local_p (node))
1126 struct ipa_ref *ref = NULL;
1128 for (int i = 0; node->iterate_referring (i, ref); ++i)
1130 if (!symtab_in_same_comdat_p (ref->referring, node))
1132 error ("comdat-local symbol referred to by %s outside its "
1133 "comdat",
1134 identifier_to_locale (ref->referring->name()));
1135 error_found = true;
1140 if (node->implicit_section && !node->get_section ())
1142 error ("implicit_section flag is set but section isn't");
1143 error_found = true;
1145 if (node->get_section () && node->get_comdat_group ()
1146 && !node->implicit_section)
1148 error ("Both section and comdat group is set");
1149 error_found = true;
1151 /* TODO: Add string table for sections, so we do not keep holding duplicated
1152 strings. */
1153 if (node->alias && node->definition
1154 && node->get_section () != symtab_alias_target (node)->get_section ()
1155 && (!node->get_section()
1156 || !symtab_alias_target (node)->get_section ()
1157 || strcmp (node->get_section(),
1158 symtab_alias_target (node)->get_section ())))
1160 error ("Alias and target's section differs");
1161 dump_symtab_node (stderr, symtab_alias_target (node));
1162 error_found = true;
1164 if (node->alias && node->definition
1165 && node->get_comdat_group () != symtab_alias_target (node)->get_comdat_group ())
1167 error ("Alias and target's comdat groups differs");
1168 dump_symtab_node (stderr, symtab_alias_target (node));
1169 error_found = true;
1172 return error_found;
1175 /* Verify consistency of NODE. */
1177 DEBUG_FUNCTION void
1178 verify_symtab_node (symtab_node *node)
1180 if (seen_error ())
1181 return;
1183 timevar_push (TV_CGRAPH_VERIFY);
1184 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1185 verify_cgraph_node (cnode);
1186 else
1187 if (verify_symtab_base (node))
1189 dump_symtab_node (stderr, node);
1190 internal_error ("verify_symtab_node failed");
1192 timevar_pop (TV_CGRAPH_VERIFY);
1195 /* Verify symbol table for internal consistency. */
1197 DEBUG_FUNCTION void
1198 verify_symtab (void)
1200 symtab_node *node;
1201 hash_map<tree, symtab_node *> comdat_head_map (251);
1203 FOR_EACH_SYMBOL (node)
1205 verify_symtab_node (node);
1206 if (node->get_comdat_group ())
1208 symtab_node **entry, *s;
1209 bool existed;
1211 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1212 &existed);
1213 if (!existed)
1214 *entry = node;
1215 else
1216 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1217 if (!s || s == *entry)
1219 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1220 dump_symtab_node (stderr, *entry);
1221 dump_symtab_node (stderr, node);
1222 internal_error ("verify_symtab failed");
1228 /* Return true when RESOLUTION indicate that linker will use
1229 the symbol from non-LTO object files. */
1231 bool
1232 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
1234 return (resolution == LDPR_PREVAILING_DEF
1235 || resolution == LDPR_PREEMPTED_REG
1236 || resolution == LDPR_RESOLVED_EXEC
1237 || resolution == LDPR_RESOLVED_DYN);
1240 /* Return true when NODE is known to be used from other (non-LTO) object file.
1241 Known only when doing LTO via linker plugin. */
1243 bool
1244 symtab_used_from_object_file_p (symtab_node *node)
1246 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1247 return false;
1248 if (resolution_used_from_other_file_p (node->resolution))
1249 return true;
1250 return false;
1253 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1254 but other code such as notice_global_symbol generates rtl. */
1256 void
1257 symtab_make_decl_local (tree decl)
1259 rtx rtl, symbol;
1261 /* Avoid clearing comdat_groups on comdat-local decls. */
1262 if (TREE_PUBLIC (decl) == 0)
1263 return;
1265 if (TREE_CODE (decl) == VAR_DECL)
1266 DECL_COMMON (decl) = 0;
1267 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1269 DECL_COMDAT (decl) = 0;
1270 DECL_WEAK (decl) = 0;
1271 DECL_EXTERNAL (decl) = 0;
1272 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1273 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1274 TREE_PUBLIC (decl) = 0;
1275 if (!DECL_RTL_SET_P (decl))
1276 return;
1278 /* Update rtl flags. */
1279 make_decl_rtl (decl);
1281 rtl = DECL_RTL (decl);
1282 if (!MEM_P (rtl))
1283 return;
1285 symbol = XEXP (rtl, 0);
1286 if (GET_CODE (symbol) != SYMBOL_REF)
1287 return;
1289 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1292 /* Return availability of NODE. */
1294 enum availability
1295 symtab_node_availability (symtab_node *node)
1297 if (is_a <cgraph_node *> (node))
1298 return cgraph_function_body_availability (cgraph (node));
1299 else
1300 return cgraph_variable_initializer_availability (varpool (node));
1303 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
1304 If NODE is not an alias, return NODE.
1305 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1307 symtab_node *
1308 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
1310 bool weakref_p = false;
1312 if (!node->alias)
1314 if (availability)
1315 *availability = symtab_node_availability (node);
1316 return node;
1319 /* To determine visibility of the target, we follow ELF semantic of aliases.
1320 Here alias is an alternative assembler name of a given definition. Its
1321 availability prevails the availability of its target (i.e. static alias of
1322 weak definition is available.
1324 Weakref is a different animal (and not part of ELF per se). It is just
1325 alternative name of a given symbol used within one complation unit
1326 and is translated prior hitting the object file. It inherits the
1327 visibility of its target (i.e. weakref of non-overwritable definition
1328 is non-overwritable, while weakref of weak definition is weak).
1330 If we ever get into supporting targets with different semantics, a target
1331 hook will be needed here. */
1333 if (availability)
1335 weakref_p = node->weakref;
1336 if (!weakref_p)
1337 *availability = symtab_node_availability (node);
1338 else
1339 *availability = AVAIL_LOCAL;
1341 while (node)
1343 if (node->alias && node->analyzed)
1344 node = symtab_alias_target (node);
1345 else
1347 if (!availability)
1349 else if (node->analyzed)
1351 if (weakref_p)
1353 enum availability a = symtab_node_availability (node);
1354 if (a < *availability)
1355 *availability = a;
1358 else
1359 *availability = AVAIL_NOT_AVAILABLE;
1360 return node;
1362 if (node && availability && weakref_p)
1364 enum availability a = symtab_node_availability (node);
1365 if (a < *availability)
1366 *availability = a;
1367 weakref_p = node->weakref;
1370 if (availability)
1371 *availability = AVAIL_NOT_AVAILABLE;
1372 return NULL;
1375 /* C++ FE sometimes change linkage flags after producing same body aliases.
1377 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1378 are obviously equivalent. The way it is doing so is however somewhat
1379 kludgy and interferes with the visibility code. As a result we need to
1380 copy the visibility from the target to get things right. */
1382 void
1383 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1385 if (is_a <cgraph_node *> (node))
1387 DECL_DECLARED_INLINE_P (node->decl)
1388 = DECL_DECLARED_INLINE_P (target->decl);
1389 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1390 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1392 /* FIXME: It is not really clear why those flags should not be copied for
1393 functions, too. */
1394 else
1396 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1397 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1398 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1400 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1401 if (TREE_PUBLIC (node->decl))
1403 tree group;
1405 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1406 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1407 group = target->get_comdat_group ();
1408 node->set_comdat_group (group);
1409 if (group
1410 && !node->same_comdat_group)
1411 symtab_add_to_same_comdat_group (node, target);
1413 node->externally_visible = target->externally_visible;
1416 /* Hash sections by their names. */
1418 static hashval_t
1419 hash_section_hash_entry (const void *p)
1421 const section_hash_entry *n = (const section_hash_entry *) p;
1422 return htab_hash_string (n->name);
1425 /* Return true if section P1 name equals to P2. */
1427 static int
1428 eq_sections (const void *p1, const void *p2)
1430 const section_hash_entry *n1 = (const section_hash_entry *) p1;
1431 const char *name = (const char *)p2;
1432 return n1->name == name || !strcmp (n1->name, name);
1435 /* Set section, do not recurse into aliases.
1436 When one wants to change section of symbol and its aliases,
1437 use set_section */
1439 void
1440 symtab_node::set_section_for_node (const char *section)
1442 const char *current = get_section ();
1443 void **slot;
1445 if (current == section
1446 || (current && section
1447 && !strcmp (current, section)))
1448 return;
1450 if (current)
1452 x_section->ref_count--;
1453 if (!x_section->ref_count)
1455 slot = htab_find_slot_with_hash (section_hash, x_section->name,
1456 htab_hash_string (x_section->name),
1457 INSERT);
1458 ggc_free (x_section);
1459 htab_clear_slot (section_hash, slot);
1461 x_section = NULL;
1463 if (!section)
1465 implicit_section = false;
1466 return;
1468 if (!section_hash)
1469 section_hash = htab_create_ggc (10, hash_section_hash_entry,
1470 eq_sections, NULL);
1471 slot = htab_find_slot_with_hash (section_hash, section,
1472 htab_hash_string (section),
1473 INSERT);
1474 if (*slot)
1475 x_section = (section_hash_entry *)*slot;
1476 else
1478 int len = strlen (section);
1479 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1480 x_section->name = ggc_vec_alloc<char> (len + 1);
1481 memcpy (x_section->name, section, len + 1);
1483 x_section->ref_count++;
1486 /* Worker for set_section. */
1488 static bool
1489 set_section_1 (struct symtab_node *n, void *s)
1491 n->set_section_for_node ((char *)s);
1492 return false;
1495 /* Set section of symbol and its aliases. */
1497 void
1498 symtab_node::set_section (const char *section)
1500 gcc_assert (!this->alias);
1501 symtab_for_node_and_aliases (this, set_section_1, const_cast<char *>(section), true);
1504 /* Return the initialization priority. */
1506 priority_type
1507 symtab_node::get_init_priority ()
1509 struct symbol_priority_map *h;
1510 struct symbol_priority_map in;
1512 if (!this->in_init_priority_hash)
1513 return DEFAULT_INIT_PRIORITY;
1514 in.symbol = this;
1515 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1516 return h ? h->init : DEFAULT_INIT_PRIORITY;
1519 /* Return the finalization priority. */
1521 priority_type
1522 cgraph_node::get_fini_priority ()
1524 struct symbol_priority_map *h;
1525 struct symbol_priority_map in;
1527 if (!this->in_init_priority_hash)
1528 return DEFAULT_INIT_PRIORITY;
1529 in.symbol = this;
1530 h = (struct symbol_priority_map *) htab_find (init_priority_hash, &in);
1531 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1534 /* Return true if the from tree in both priority maps are equal. */
1537 symbol_priority_map_eq (const void *va, const void *vb)
1539 const struct symbol_priority_map *const a = (const struct symbol_priority_map *) va,
1540 *const b = (const struct symbol_priority_map *) vb;
1541 return (a->symbol == b->symbol);
1544 /* Hash a from symbol in a symbol_priority_map. */
1546 unsigned int
1547 symbol_priority_map_hash (const void *item)
1549 return htab_hash_pointer (((const struct symbol_priority_map *)item)->symbol);
1552 /* Return the initialization and finalization priority information for
1553 DECL. If there is no previous priority information, a freshly
1554 allocated structure is returned. */
1556 static struct symbol_priority_map *
1557 symbol_priority_info (struct symtab_node *symbol)
1559 struct symbol_priority_map in;
1560 struct symbol_priority_map *h;
1561 void **loc;
1563 in.symbol = symbol;
1564 if (!init_priority_hash)
1565 init_priority_hash = htab_create_ggc (512, symbol_priority_map_hash,
1566 symbol_priority_map_eq, 0);
1568 loc = htab_find_slot (init_priority_hash, &in, INSERT);
1569 h = (struct symbol_priority_map *) *loc;
1570 if (!h)
1572 h = ggc_cleared_alloc<symbol_priority_map> ();
1573 *loc = h;
1574 h->symbol = symbol;
1575 h->init = DEFAULT_INIT_PRIORITY;
1576 h->fini = DEFAULT_INIT_PRIORITY;
1577 symbol->in_init_priority_hash = true;
1580 return h;
1583 /* Set initialization priority to PRIORITY. */
1585 void
1586 symtab_node::set_init_priority (priority_type priority)
1588 struct symbol_priority_map *h;
1590 if (is_a <cgraph_node *> (this))
1591 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1593 if (priority == DEFAULT_INIT_PRIORITY)
1595 gcc_assert (get_init_priority() == priority);
1596 return;
1598 h = symbol_priority_info (this);
1599 h->init = priority;
1602 /* Set fialization priority to PRIORITY. */
1604 void
1605 cgraph_node::set_fini_priority (priority_type priority)
1607 struct symbol_priority_map *h;
1609 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1611 if (priority == DEFAULT_INIT_PRIORITY)
1613 gcc_assert (get_fini_priority() == priority);
1614 return;
1616 h = symbol_priority_info (this);
1617 h->fini = priority;
1620 /* Worker for symtab_resolve_alias. */
1622 static bool
1623 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
1625 n->implicit_section = true;
1626 return false;
1629 /* Add reference recording that NODE is alias of TARGET.
1630 The function can fail in the case of aliasing cycles; in this case
1631 it returns false. */
1633 bool
1634 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1636 symtab_node *n;
1638 gcc_assert (!node->analyzed
1639 && !vec_safe_length (node->ref_list.references));
1641 /* Never let cycles to creep into the symbol table alias references;
1642 those will make alias walkers to be infinite. */
1643 for (n = target; n && n->alias;
1644 n = n->analyzed ? symtab_alias_target (n) : NULL)
1645 if (n == node)
1647 if (is_a <cgraph_node *> (node))
1648 error ("function %q+D part of alias cycle", node->decl);
1649 else if (is_a <varpool_node *> (node))
1650 error ("variable %q+D part of alias cycle", node->decl);
1651 else
1652 gcc_unreachable ();
1653 node->alias = false;
1654 return false;
1657 /* "analyze" the node - i.e. mark the reference. */
1658 node->definition = true;
1659 node->alias = true;
1660 node->analyzed = true;
1661 node->add_reference (target, IPA_REF_ALIAS, NULL);
1663 /* Add alias into the comdat group of its target unless it is already there. */
1664 if (node->same_comdat_group)
1665 symtab_remove_from_same_comdat_group (node);
1666 node->set_comdat_group (NULL);
1667 if (target->get_comdat_group ())
1668 symtab_add_to_same_comdat_group (node, target);
1670 if ((node->get_section () != target->get_section ()
1671 || target->get_comdat_group ())
1672 && node->get_section () && !node->implicit_section)
1674 error ("section of alias %q+D must match section of its target",
1675 node->decl);
1677 symtab_for_node_and_aliases (node, set_section_1,
1678 const_cast<char *>(target->get_section ()), true);
1679 if (target->implicit_section)
1680 symtab_for_node_and_aliases (node,
1681 set_implicit_section, NULL, true);
1683 /* Alias targets become redundant after alias is resolved into an reference.
1684 We do not want to keep it around or we would have to mind updating them
1685 when renaming symbols. */
1686 node->alias_target = NULL;
1688 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1689 fixup_same_cpp_alias_visibility (node, target);
1691 /* If alias has address taken, so does the target. */
1692 if (node->address_taken)
1693 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1694 return true;
1697 /* Call calback on NODE and aliases associated to NODE.
1698 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1699 skipped. */
1701 bool
1702 symtab_for_node_and_aliases (symtab_node *node,
1703 bool (*callback) (symtab_node *, void *),
1704 void *data,
1705 bool include_overwritable)
1707 int i;
1708 struct ipa_ref *ref;
1710 if (callback (node, data))
1711 return true;
1712 for (i = 0; node->iterate_referring (i, ref); i++)
1713 if (ref->use == IPA_REF_ALIAS)
1715 symtab_node *alias = ref->referring;
1716 if (include_overwritable
1717 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1718 if (symtab_for_node_and_aliases (alias, callback, data,
1719 include_overwritable))
1720 return true;
1722 return false;
1725 /* Worker searching nonoverwritable alias. */
1727 static bool
1728 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1730 if (decl_binds_to_current_def_p (node->decl))
1732 symtab_node *fn = symtab_alias_ultimate_target (node);
1734 /* Ensure that the alias is well formed this may not be the case
1735 of user defined aliases and currently it is not always the case
1736 of C++ same body aliases (that is a bug). */
1737 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1738 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1739 || (TREE_CODE (node->decl) == FUNCTION_DECL
1740 && flags_from_decl_or_type (node->decl)
1741 != flags_from_decl_or_type (fn->decl))
1742 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1743 return false;
1745 *(symtab_node **)data = node;
1746 return true;
1748 return false;
1751 /* If NODE can not be overwriten by static or dynamic linker to point to different
1752 definition, return NODE. Otherwise look for alias with such property and if
1753 none exists, introduce new one. */
1755 symtab_node *
1756 symtab_nonoverwritable_alias (symtab_node *node)
1758 tree new_decl;
1759 symtab_node *new_node = NULL;
1761 /* First try to look up existing alias or base object
1762 (if that is already non-overwritable). */
1763 node = symtab_alias_ultimate_target (node, NULL);
1764 gcc_assert (!node->alias && !node->weakref);
1765 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1766 (void *)&new_node, true);
1767 if (new_node)
1768 return new_node;
1769 #ifndef ASM_OUTPUT_DEF
1770 /* If aliases aren't supported by the assembler, fail. */
1771 return NULL;
1772 #endif
1774 /* Otherwise create a new one. */
1775 new_decl = copy_node (node->decl);
1776 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1777 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1778 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1779 DECL_INITIAL (new_decl) = NULL;
1780 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1781 SET_DECL_RTL (new_decl, NULL);
1783 /* Update the properties. */
1784 DECL_EXTERNAL (new_decl) = 0;
1785 TREE_PUBLIC (new_decl) = 0;
1786 DECL_COMDAT (new_decl) = 0;
1787 DECL_WEAK (new_decl) = 0;
1789 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1790 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1791 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1793 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1794 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1795 new_node = cgraph_create_function_alias
1796 (new_decl, node->decl);
1798 else
1800 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1801 DECL_INITIAL (new_decl) = error_mark_node;
1802 new_node = varpool_create_variable_alias (new_decl, node->decl);
1804 symtab_resolve_alias (new_node, node);
1805 gcc_assert (decl_binds_to_current_def_p (new_decl)
1806 && targetm.binds_local_p (new_decl));
1807 return new_node;
1810 /* Return true if A and B represents semantically equivalent symbols. */
1812 bool
1813 symtab_semantically_equivalent_p (symtab_node *a,
1814 symtab_node *b)
1816 enum availability avail;
1817 symtab_node *ba;
1818 symtab_node *bb;
1820 /* Equivalent functions are equivalent. */
1821 if (a->decl == b->decl)
1822 return true;
1824 /* If symbol is not overwritable by different implementation,
1825 walk to the base object it defines. */
1826 ba = symtab_alias_ultimate_target (a, &avail);
1827 if (avail >= AVAIL_AVAILABLE)
1829 if (ba == b)
1830 return true;
1832 else
1833 ba = a;
1834 bb = symtab_alias_ultimate_target (b, &avail);
1835 if (avail >= AVAIL_AVAILABLE)
1837 if (a == bb)
1838 return true;
1840 else
1841 bb = b;
1842 return bb == ba;
1845 /* Classify symbol NODE for partitioning. */
1847 enum symbol_partitioning_class
1848 symtab_get_symbol_partitioning_class (symtab_node *node)
1850 /* Inline clones are always duplicated.
1851 This include external delcarations. */
1852 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1854 if (DECL_ABSTRACT (node->decl))
1855 return SYMBOL_EXTERNAL;
1857 if (cnode && cnode->global.inlined_to)
1858 return SYMBOL_DUPLICATE;
1860 /* Weakref aliases are always duplicated. */
1861 if (node->weakref)
1862 return SYMBOL_DUPLICATE;
1864 /* External declarations are external. */
1865 if (DECL_EXTERNAL (node->decl))
1866 return SYMBOL_EXTERNAL;
1868 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
1870 /* Constant pool references use local symbol names that can not
1871 be promoted global. We should never put into a constant pool
1872 objects that can not be duplicated across partitions. */
1873 if (DECL_IN_CONSTANT_POOL (node->decl))
1874 return SYMBOL_DUPLICATE;
1875 gcc_checking_assert (vnode->definition);
1877 /* Functions that are cloned may stay in callgraph even if they are unused.
1878 Handle them as external; compute_ltrans_boundary take care to make
1879 proper things to happen (i.e. to make them appear in the boundary but
1880 with body streamed, so clone can me materialized). */
1881 else if (!cgraph (node)->definition)
1882 return SYMBOL_EXTERNAL;
1884 /* Linker discardable symbols are duplicated to every use unless they are
1885 keyed. */
1886 if (DECL_ONE_ONLY (node->decl)
1887 && !node->force_output
1888 && !node->forced_by_abi
1889 && !symtab_used_from_object_file_p (node))
1890 return SYMBOL_DUPLICATE;
1892 return SYMBOL_PARTITION;
1895 /* Return true when symbol is known to be non-zero. */
1897 bool
1898 symtab_node::nonzero_address ()
1900 /* Weakrefs may be NULL when their target is not defined. */
1901 if (this->alias && this->weakref)
1903 if (this->analyzed)
1905 symtab_node *target = symtab_alias_ultimate_target (this);
1907 if (target->alias && target->weakref)
1908 return false;
1909 /* We can not recurse to target::nonzero. It is possible that the
1910 target is used only via the alias.
1911 We may walk references and look for strong use, but we do not know
1912 if this strong use will survive to final binary, so be
1913 conservative here.
1914 ??? Maybe we could do the lookup during late optimization that
1915 could be useful to eliminate the NULL pointer checks in LTO
1916 programs. */
1917 if (target->definition && !DECL_EXTERNAL (target->decl))
1918 return true;
1919 if (target->resolution != LDPR_UNKNOWN
1920 && target->resolution != LDPR_UNDEF
1921 && flag_delete_null_pointer_checks)
1922 return true;
1923 return false;
1925 else
1926 return false;
1929 /* With !flag_delete_null_pointer_checks we assume that symbols may
1930 bind to NULL. This is on by default on embedded targets only.
1932 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1933 linking fails. Important case of WEAK we want to do well are comdats.
1934 Those are handled by later check for definition.
1936 When parsing, beware the cases when WEAK attribute is added later. */
1937 if (!DECL_WEAK (this->decl)
1938 && flag_delete_null_pointer_checks
1939 && cgraph_state > CGRAPH_STATE_PARSING)
1940 return true;
1942 /* If target is defined and not extern, we know it will be output and thus
1943 it will bind to non-NULL.
1944 Play safe for flag_delete_null_pointer_checks where weak definition maye
1945 be re-defined by NULL. */
1946 if (this->definition && !DECL_EXTERNAL (this->decl)
1947 && (flag_delete_null_pointer_checks || !DECL_WEAK (this->decl)))
1948 return true;
1950 /* As the last resort, check the resolution info. */
1951 if (this->resolution != LDPR_UNKNOWN
1952 && this->resolution != LDPR_UNDEF
1953 && flag_delete_null_pointer_checks)
1954 return true;
1955 return false;
1957 #include "gt-symtab.h"