Reverting merge from trunk
[official-gcc.git] / gcc / symtab.c
blob9426f75399dae5c0956ede1a5eca0106b685ed23
1 /* Symbol table.
2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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 "tree.h"
26 #include "gimple.h"
27 #include "tree-inline.h"
28 #include "langhooks.h"
29 #include "hashtab.h"
30 #include "ggc.h"
31 #include "cgraph.h"
32 #include "diagnostic.h"
33 #include "timevar.h"
34 #include "lto-streamer.h"
35 #include "rtl.h"
36 #include "output.h"
38 const char * const ld_plugin_symbol_resolution_names[]=
40 "",
41 "undef",
42 "prevailing_def",
43 "prevailing_def_ironly",
44 "preempted_reg",
45 "preempted_ir",
46 "resolved_ir",
47 "resolved_exec",
48 "resolved_dyn",
49 "prevailing_def_ironly_exp"
52 /* Hash table used to convert declarations into nodes. */
53 static GTY((param_is (symtab_node))) htab_t symtab_hash;
54 /* Hash table used to convert assembler names into nodes. */
55 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
57 /* Linked list of symbol table nodes. */
58 symtab_node *symtab_nodes;
60 /* The order index of the next symtab node to be created. This is
61 used so that we can sort the cgraph nodes in order by when we saw
62 them, to support -fno-toplevel-reorder. */
63 int symtab_order;
65 /* Returns a hash code for P. */
67 static hashval_t
68 hash_node (const void *p)
70 const symtab_node *n = (const symtab_node *) p;
71 return (hashval_t) DECL_UID (n->decl);
75 /* Returns nonzero if P1 and P2 are equal. */
77 static int
78 eq_node (const void *p1, const void *p2)
80 const symtab_node *n1 = (const symtab_node *) p1;
81 const symtab_node *n2 = (const symtab_node *) p2;
82 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
85 /* Hash asmnames ignoring the user specified marks. */
87 static hashval_t
88 decl_assembler_name_hash (const_tree asmname)
90 if (IDENTIFIER_POINTER (asmname)[0] == '*')
92 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
93 size_t ulp_len = strlen (user_label_prefix);
95 if (ulp_len == 0)
97 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
98 decl_str += ulp_len;
100 return htab_hash_string (decl_str);
103 return htab_hash_string (IDENTIFIER_POINTER (asmname));
107 /* Returns a hash code for P. */
109 static hashval_t
110 hash_node_by_assembler_name (const void *p)
112 const symtab_node *n = (const symtab_node *) p;
113 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
116 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
118 static bool
119 decl_assembler_name_equal (tree decl, const_tree asmname)
121 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
122 const char *decl_str;
123 const char *asmname_str;
124 bool test = false;
126 if (decl_asmname == asmname)
127 return true;
129 decl_str = IDENTIFIER_POINTER (decl_asmname);
130 asmname_str = IDENTIFIER_POINTER (asmname);
133 /* If the target assembler name was set by the user, things are trickier.
134 We have a leading '*' to begin with. After that, it's arguable what
135 is the correct thing to do with -fleading-underscore. Arguably, we've
136 historically been doing the wrong thing in assemble_alias by always
137 printing the leading underscore. Since we're not changing that, make
138 sure user_label_prefix follows the '*' before matching. */
139 if (decl_str[0] == '*')
141 size_t ulp_len = strlen (user_label_prefix);
143 decl_str ++;
145 if (ulp_len == 0)
146 test = true;
147 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
148 decl_str += ulp_len, test=true;
149 else
150 decl_str --;
152 if (asmname_str[0] == '*')
154 size_t ulp_len = strlen (user_label_prefix);
156 asmname_str ++;
158 if (ulp_len == 0)
159 test = true;
160 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
161 asmname_str += ulp_len, test=true;
162 else
163 asmname_str --;
166 if (!test)
167 return false;
168 return strcmp (decl_str, asmname_str) == 0;
172 /* Returns nonzero if P1 and P2 are equal. */
174 static int
175 eq_assembler_name (const void *p1, const void *p2)
177 const symtab_node *n1 = (const symtab_node *) p1;
178 const_tree name = (const_tree)p2;
179 return (decl_assembler_name_equal (n1->decl, name));
182 /* Insert NODE to assembler name hash. */
184 static void
185 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
187 if (is_a <varpool_node> (node) && DECL_HARD_REGISTER (node->decl))
188 return;
189 gcc_checking_assert (!node->previous_sharing_asm_name
190 && !node->next_sharing_asm_name);
191 if (assembler_name_hash)
193 void **aslot;
194 struct cgraph_node *cnode;
195 tree decl = node->decl;
197 tree name = DECL_ASSEMBLER_NAME (node->decl);
199 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
200 decl_assembler_name_hash (name),
201 INSERT);
202 gcc_assert (*aslot != node);
203 node->next_sharing_asm_name = (symtab_node *)*aslot;
204 if (*aslot != NULL)
205 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
206 *aslot = node;
208 /* Update also possible inline clones sharing a decl. */
209 cnode = dyn_cast <cgraph_node> (node);
210 if (cnode && cnode->clones && with_clones)
211 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
212 if (cnode->decl == decl)
213 insert_to_assembler_name_hash (cnode, true);
218 /* Remove NODE from assembler name hash. */
220 static void
221 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
223 if (assembler_name_hash)
225 struct cgraph_node *cnode;
226 tree decl = node->decl;
228 if (node->next_sharing_asm_name)
229 node->next_sharing_asm_name->previous_sharing_asm_name
230 = node->previous_sharing_asm_name;
231 if (node->previous_sharing_asm_name)
233 node->previous_sharing_asm_name->next_sharing_asm_name
234 = node->next_sharing_asm_name;
236 else
238 tree name = DECL_ASSEMBLER_NAME (node->decl);
239 void **slot;
240 slot = htab_find_slot_with_hash (assembler_name_hash, name,
241 decl_assembler_name_hash (name),
242 NO_INSERT);
243 gcc_assert (*slot == node);
244 if (!node->next_sharing_asm_name)
245 htab_clear_slot (assembler_name_hash, slot);
246 else
247 *slot = node->next_sharing_asm_name;
249 node->next_sharing_asm_name = NULL;
250 node->previous_sharing_asm_name = NULL;
252 /* Update also possible inline clones sharing a decl. */
253 cnode = dyn_cast <cgraph_node> (node);
254 if (cnode && cnode->clones && with_clones)
255 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
256 if (cnode->decl == decl)
257 unlink_from_assembler_name_hash (cnode, true);
261 /* Arrange node to be first in its entry of assembler_name_hash. */
263 void
264 symtab_prevail_in_asm_name_hash (symtab_node *node)
266 unlink_from_assembler_name_hash (node, false);
267 insert_to_assembler_name_hash (node, false);
271 /* Add node into symbol table. This function is not used directly, but via
272 cgraph/varpool node creation routines. */
274 void
275 symtab_register_node (symtab_node *node)
277 struct symtab_node key;
278 symtab_node **slot;
280 node->next = symtab_nodes;
281 node->previous = NULL;
282 if (symtab_nodes)
283 symtab_nodes->previous = node;
284 symtab_nodes = node;
286 if (!symtab_hash)
287 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
288 key.decl = node->decl;
289 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
290 if (*slot == NULL)
291 *slot = node;
293 ipa_empty_ref_list (&node->ref_list);
295 node->order = symtab_order++;
297 /* Be sure to do this last; C++ FE might create new nodes via
298 DECL_ASSEMBLER_NAME langhook! */
299 insert_to_assembler_name_hash (node, false);
302 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
303 of inline clones. */
305 void
306 symtab_insert_node_to_hashtable (symtab_node *node)
308 struct symtab_node key;
309 symtab_node **slot;
311 if (!symtab_hash)
312 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
313 key.decl = node->decl;
314 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
315 *slot = node;
318 /* Remove node from symbol table. This function is not used directly, but via
319 cgraph/varpool node removal routines. */
321 void
322 symtab_unregister_node (symtab_node *node)
324 void **slot;
325 ipa_remove_all_references (&node->ref_list);
326 ipa_remove_all_referring (&node->ref_list);
328 if (node->same_comdat_group)
330 symtab_node *prev;
331 for (prev = node->same_comdat_group;
332 prev->same_comdat_group != node;
333 prev = prev->same_comdat_group)
335 if (node->same_comdat_group == prev)
336 prev->same_comdat_group = NULL;
337 else
338 prev->same_comdat_group = node->same_comdat_group;
339 node->same_comdat_group = NULL;
342 if (node->previous)
343 node->previous->next = node->next;
344 else
345 symtab_nodes = node->next;
346 if (node->next)
347 node->next->previous = node->previous;
348 node->next = NULL;
349 node->previous = NULL;
351 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
353 /* During LTO symtab merging we temporarily corrupt decl to symtab node
354 hash. */
355 gcc_assert ((slot && *slot) || in_lto_p);
356 if (slot && *slot && *slot == node)
358 symtab_node *replacement_node = NULL;
359 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
360 replacement_node = cgraph_find_replacement_node (cnode);
361 if (!replacement_node)
362 htab_clear_slot (symtab_hash, slot);
363 else
364 *slot = replacement_node;
366 if (!is_a <varpool_node> (node) || !DECL_HARD_REGISTER (node->decl))
367 unlink_from_assembler_name_hash (node, false);
370 /* Return symbol table node associated with DECL, if any,
371 and NULL otherwise. */
373 symtab_node *
374 symtab_get_node (const_tree decl)
376 symtab_node **slot;
377 struct symtab_node key;
379 #ifdef ENABLE_CHECKING
380 /* Check that we are called for sane type of object - functions
381 and static or external variables. */
382 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
383 || (TREE_CODE (decl) == VAR_DECL
384 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
385 || in_lto_p)));
386 #endif
388 if (!symtab_hash)
389 return NULL;
391 key.decl = CONST_CAST2 (tree, const_tree, decl);
393 slot = (symtab_node **) htab_find_slot (symtab_hash, &key,
394 NO_INSERT);
396 if (slot)
397 return *slot;
398 return NULL;
401 /* Remove symtab NODE from the symbol table. */
403 void
404 symtab_remove_node (symtab_node *node)
406 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
407 cgraph_remove_node (cnode);
408 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
409 varpool_remove_node (vnode);
412 /* Initalize asm name hash unless. */
414 void
415 symtab_initialize_asm_name_hash (void)
417 symtab_node *node;
418 if (!assembler_name_hash)
420 assembler_name_hash =
421 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
422 NULL);
423 FOR_EACH_SYMBOL (node)
424 insert_to_assembler_name_hash (node, false);
428 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
429 Return NULL if there's no such node. */
431 symtab_node *
432 symtab_node_for_asm (const_tree asmname)
434 symtab_node *node;
435 void **slot;
437 symtab_initialize_asm_name_hash ();
438 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
439 decl_assembler_name_hash (asmname),
440 NO_INSERT);
442 if (slot)
444 node = (symtab_node *) *slot;
445 return node;
447 return NULL;
450 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
452 void
453 change_decl_assembler_name (tree decl, tree name)
455 symtab_node *node = NULL;
457 /* We can have user ASM names on things, like global register variables, that
458 are not in the symbol table. */
459 if ((TREE_CODE (decl) == VAR_DECL
460 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
461 || TREE_CODE (decl) == FUNCTION_DECL)
462 node = symtab_get_node (decl);
463 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
465 SET_DECL_ASSEMBLER_NAME (decl, name);
466 if (node)
467 insert_to_assembler_name_hash (node, true);
469 else
471 if (name == DECL_ASSEMBLER_NAME (decl))
472 return;
474 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
475 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
476 : NULL);
477 if (node)
478 unlink_from_assembler_name_hash (node, true);
479 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
480 && DECL_RTL_SET_P (decl))
481 warning (0, "%D renamed after being referenced in assembly", decl);
483 SET_DECL_ASSEMBLER_NAME (decl, name);
484 if (alias)
486 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
487 TREE_CHAIN (DECL_ASSEMBLER_NAME (name)) = alias;
489 if (node)
490 insert_to_assembler_name_hash (node, true);
494 /* Add NEW_ to the same comdat group that OLD is in. */
496 void
497 symtab_add_to_same_comdat_group (symtab_node *new_node,
498 symtab_node *old_node)
500 gcc_assert (DECL_ONE_ONLY (old_node->decl));
501 gcc_assert (!new_node->same_comdat_group);
502 gcc_assert (new_node != old_node);
504 DECL_COMDAT_GROUP (new_node->decl) = DECL_COMDAT_GROUP (old_node->decl);
505 new_node->same_comdat_group = old_node;
506 if (!old_node->same_comdat_group)
507 old_node->same_comdat_group = new_node;
508 else
510 symtab_node *n;
511 for (n = old_node->same_comdat_group;
512 n->same_comdat_group != old_node;
513 n = n->same_comdat_group)
515 n->same_comdat_group = new_node;
519 /* Dissolve the same_comdat_group list in which NODE resides. */
521 void
522 symtab_dissolve_same_comdat_group_list (symtab_node *node)
524 symtab_node *n = node;
525 symtab_node *next;
527 if (!node->same_comdat_group)
528 return;
531 next = n->same_comdat_group;
532 n->same_comdat_group = NULL;
533 n = next;
535 while (n != node);
538 /* Return printable assembler name of NODE.
539 This function is used only for debugging. When assembler name
540 is unknown go with identifier name. */
542 const char *
543 symtab_node::asm_name () const
545 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
546 return lang_hooks.decl_printable_name (decl, 2);
547 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
550 /* Return printable identifier name. */
552 const char *
553 symtab_node::name () const
555 return lang_hooks.decl_printable_name (decl, 2);
558 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
560 /* Dump base fields of symtab nodes. Not to be used directly. */
562 void
563 dump_symtab_base (FILE *f, symtab_node *node)
565 static const char * const visibility_types[] = {
566 "default", "protected", "hidden", "internal"
569 fprintf (f, "%s/%i (%s)",
570 node->asm_name (),
571 node->order,
572 node->name ());
573 dump_addr (f, " @", (void *)node);
574 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
576 if (node->definition)
577 fprintf (f, " definition");
578 if (node->analyzed)
579 fprintf (f, " analyzed");
580 if (node->alias)
581 fprintf (f, " alias");
582 if (node->weakref)
583 fprintf (f, " weakref");
584 if (node->cpp_implicit_alias)
585 fprintf (f, " cpp_implicit_alias");
586 if (node->alias_target)
587 fprintf (f, " target:%s",
588 DECL_P (node->alias_target)
589 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
590 (node->alias_target))
591 : IDENTIFIER_POINTER (node->alias_target));
592 fprintf (f, "\n Visibility:");
593 if (node->in_other_partition)
594 fprintf (f, " in_other_partition");
595 if (node->used_from_other_partition)
596 fprintf (f, " used_from_other_partition");
597 if (node->force_output)
598 fprintf (f, " force_output");
599 if (node->forced_by_abi)
600 fprintf (f, " forced_by_abi");
601 if (node->externally_visible)
602 fprintf (f, " externally_visible");
603 if (node->resolution != LDPR_UNKNOWN)
604 fprintf (f, " %s",
605 ld_plugin_symbol_resolution_names[(int)node->resolution]);
606 if (TREE_ASM_WRITTEN (node->decl))
607 fprintf (f, " asm_written");
608 if (DECL_EXTERNAL (node->decl))
609 fprintf (f, " external");
610 if (TREE_PUBLIC (node->decl))
611 fprintf (f, " public");
612 if (DECL_COMMON (node->decl))
613 fprintf (f, " common");
614 if (DECL_WEAK (node->decl))
615 fprintf (f, " weak");
616 if (DECL_DLLIMPORT_P (node->decl))
617 fprintf (f, " dll_import");
618 if (DECL_COMDAT (node->decl))
619 fprintf (f, " comdat");
620 if (DECL_COMDAT_GROUP (node->decl))
621 fprintf (f, " comdat_group:%s",
622 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->decl)));
623 if (DECL_ONE_ONLY (node->decl))
624 fprintf (f, " one_only");
625 if (DECL_SECTION_NAME (node->decl))
626 fprintf (f, " section_name:%s",
627 TREE_STRING_POINTER (DECL_SECTION_NAME (node->decl)));
628 if (DECL_VISIBILITY_SPECIFIED (node->decl))
629 fprintf (f, " visibility_specified");
630 if (DECL_VISIBILITY (node->decl))
631 fprintf (f, " visibility:%s",
632 visibility_types [DECL_VISIBILITY (node->decl)]);
633 if (DECL_VIRTUAL_P (node->decl))
634 fprintf (f, " virtual");
635 if (DECL_ARTIFICIAL (node->decl))
636 fprintf (f, " artificial");
637 if (TREE_CODE (node->decl) == FUNCTION_DECL)
639 if (DECL_STATIC_CONSTRUCTOR (node->decl))
640 fprintf (f, " constructor");
641 if (DECL_STATIC_DESTRUCTOR (node->decl))
642 fprintf (f, " destructor");
644 fprintf (f, "\n");
646 if (node->same_comdat_group)
647 fprintf (f, " Same comdat group as: %s/%i\n",
648 node->same_comdat_group->asm_name (),
649 node->same_comdat_group->order);
650 if (node->next_sharing_asm_name)
651 fprintf (f, " next sharing asm name: %i\n",
652 node->next_sharing_asm_name->order);
653 if (node->previous_sharing_asm_name)
654 fprintf (f, " previous sharing asm name: %i\n",
655 node->previous_sharing_asm_name->order);
657 if (node->address_taken)
658 fprintf (f, " Address is taken.\n");
659 if (node->aux)
661 fprintf (f, " Aux:");
662 dump_addr (f, " @", (void *)node->aux);
665 fprintf (f, " References: ");
666 ipa_dump_references (f, &node->ref_list);
667 fprintf (f, " Referring: ");
668 ipa_dump_referring (f, &node->ref_list);
669 if (node->lto_file_data)
670 fprintf (f, " Read from file: %s\n",
671 node->lto_file_data->file_name);
674 /* Dump symtab node. */
676 void
677 dump_symtab_node (FILE *f, symtab_node *node)
679 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
680 dump_cgraph_node (f, cnode);
681 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
682 dump_varpool_node (f, vnode);
685 /* Dump symbol table. */
687 void
688 dump_symtab (FILE *f)
690 symtab_node *node;
691 fprintf (f, "Symbol table:\n\n");
692 FOR_EACH_SYMBOL (node)
693 dump_symtab_node (f, node);
696 /* Dump symtab node NODE to stderr. */
698 DEBUG_FUNCTION void
699 debug_symtab_node (symtab_node *node)
701 dump_symtab_node (stderr, node);
704 /* Dump symbol table to stderr. */
706 DEBUG_FUNCTION void
707 debug_symtab (void)
709 dump_symtab (stderr);
712 /* Verify common part of symtab nodes. */
714 DEBUG_FUNCTION bool
715 verify_symtab_base (symtab_node *node)
717 bool error_found = false;
718 symtab_node *hashed_node;
720 if (is_a <cgraph_node> (node))
722 if (TREE_CODE (node->decl) != FUNCTION_DECL)
724 error ("function symbol is not function");
725 error_found = true;
728 else if (is_a <varpool_node> (node))
730 if (TREE_CODE (node->decl) != VAR_DECL)
732 error ("variable symbol is not variable");
733 error_found = true;
736 else
738 error ("node has unknown type");
739 error_found = true;
742 if (cgraph_state != CGRAPH_LTO_STREAMING)
744 hashed_node = symtab_get_node (node->decl);
745 if (!hashed_node)
747 error ("node not found in symtab decl hashtable");
748 error_found = true;
750 if (hashed_node != node
751 && (!is_a <cgraph_node> (node)
752 || !dyn_cast <cgraph_node> (node)->clone_of
753 || dyn_cast <cgraph_node> (node)->clone_of->decl
754 != node->decl))
756 error ("node differs from symtab decl hashtable");
757 error_found = true;
760 if (assembler_name_hash)
762 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
763 if (hashed_node && hashed_node->previous_sharing_asm_name)
765 error ("assembler name hash list corrupted");
766 error_found = true;
768 while (hashed_node)
770 if (hashed_node == node)
771 break;
772 hashed_node = hashed_node->next_sharing_asm_name;
774 if (!hashed_node
775 && !(is_a <varpool_node> (node)
776 || DECL_HARD_REGISTER (node->decl)))
778 error ("node not found in symtab assembler name hash");
779 error_found = true;
782 if (node->previous_sharing_asm_name
783 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
785 error ("double linked list of assembler names corrupted");
786 error_found = true;
788 if (node->analyzed && !node->definition)
790 error ("node is analyzed byt it is not a definition");
791 error_found = true;
793 if (node->cpp_implicit_alias && !node->alias)
795 error ("node is alias but not implicit alias");
796 error_found = true;
798 if (node->alias && !node->definition
799 && !node->weakref)
801 error ("node is alias but not definition");
802 error_found = true;
804 if (node->weakref && !node->alias)
806 error ("node is weakref but not an alias");
807 error_found = true;
809 if (node->same_comdat_group)
811 symtab_node *n = node->same_comdat_group;
813 if (!DECL_ONE_ONLY (n->decl))
815 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
816 error_found = true;
818 if (n->type != node->type)
820 error ("mixing different types of symbol in same comdat groups is not supported");
821 error_found = true;
823 if (n == node)
825 error ("node is alone in a comdat group");
826 error_found = true;
830 if (!n->same_comdat_group)
832 error ("same_comdat_group is not a circular list");
833 error_found = true;
834 break;
836 n = n->same_comdat_group;
838 while (n != node);
840 return error_found;
843 /* Verify consistency of NODE. */
845 DEBUG_FUNCTION void
846 verify_symtab_node (symtab_node *node)
848 if (seen_error ())
849 return;
851 timevar_push (TV_CGRAPH_VERIFY);
852 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
853 verify_cgraph_node (cnode);
854 else
855 if (verify_symtab_base (node))
857 dump_symtab_node (stderr, node);
858 internal_error ("verify_symtab_node failed");
860 timevar_pop (TV_CGRAPH_VERIFY);
863 /* Verify symbol table for internal consistency. */
865 DEBUG_FUNCTION void
866 verify_symtab (void)
868 symtab_node *node;
869 FOR_EACH_SYMBOL (node)
870 verify_symtab_node (node);
873 /* Return true when RESOLUTION indicate that linker will use
874 the symbol from non-LTO object files. */
876 bool
877 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
879 return (resolution == LDPR_PREVAILING_DEF
880 || resolution == LDPR_PREEMPTED_REG
881 || resolution == LDPR_RESOLVED_EXEC
882 || resolution == LDPR_RESOLVED_DYN);
885 /* Return true when NODE is known to be used from other (non-LTO) object file.
886 Known only when doing LTO via linker plugin. */
888 bool
889 symtab_used_from_object_file_p (symtab_node *node)
891 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
892 return false;
893 if (resolution_used_from_other_file_p (node->resolution))
894 return true;
895 return false;
898 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
899 but other code such as notice_global_symbol generates rtl. */
901 void
902 symtab_make_decl_local (tree decl)
904 rtx rtl, symbol;
906 if (TREE_CODE (decl) == VAR_DECL)
907 DECL_COMMON (decl) = 0;
908 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
910 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
912 DECL_SECTION_NAME (decl) = 0;
913 DECL_COMDAT (decl) = 0;
915 DECL_COMDAT_GROUP (decl) = 0;
916 DECL_WEAK (decl) = 0;
917 DECL_EXTERNAL (decl) = 0;
918 DECL_VISIBILITY_SPECIFIED (decl) = 0;
919 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
920 TREE_PUBLIC (decl) = 0;
921 if (!DECL_RTL_SET_P (decl))
922 return;
924 /* Update rtl flags. */
925 make_decl_rtl (decl);
927 rtl = DECL_RTL (decl);
928 if (!MEM_P (rtl))
929 return;
931 symbol = XEXP (rtl, 0);
932 if (GET_CODE (symbol) != SYMBOL_REF)
933 return;
935 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
938 /* Return availability of NODE. */
940 enum availability
941 symtab_node_availability (symtab_node *node)
943 if (is_a <cgraph_node> (node))
944 return cgraph_function_body_availability (cgraph (node));
945 else
946 return cgraph_variable_initializer_availability (varpool (node));
949 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
950 If NODE is not an alias, return NODE.
951 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
953 symtab_node *
954 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
956 bool weakref_p = false;
958 if (!node->alias)
960 if (availability)
961 *availability = symtab_node_availability (node);
962 return node;
965 /* To determine visibility of the target, we follow ELF semantic of aliases.
966 Here alias is an alternative assembler name of a given definition. Its
967 availability prevails the availability of its target (i.e. static alias of
968 weak definition is available.
970 Weakref is a different animal (and not part of ELF per se). It is just
971 alternative name of a given symbol used within one complation unit
972 and is translated prior hitting the object file. It inherits the
973 visibility of its target (i.e. weakref of non-overwritable definition
974 is non-overwritable, while weakref of weak definition is weak).
976 If we ever get into supporting targets with different semantics, a target
977 hook will be needed here. */
979 if (availability)
981 weakref_p = node->weakref;
982 if (!weakref_p)
983 *availability = symtab_node_availability (node);
984 else
985 *availability = AVAIL_LOCAL;
987 while (node)
989 if (node->alias && node->analyzed)
990 node = symtab_alias_target (node);
991 else
993 if (!availability)
995 else if (node->analyzed)
997 if (weakref_p)
999 enum availability a = symtab_node_availability (node);
1000 if (a < *availability)
1001 *availability = a;
1004 else
1005 *availability = AVAIL_NOT_AVAILABLE;
1006 return node;
1008 if (node && availability && weakref_p)
1010 enum availability a = symtab_node_availability (node);
1011 if (a < *availability)
1012 *availability = a;
1013 weakref_p = node->weakref;
1016 if (availability)
1017 *availability = AVAIL_NOT_AVAILABLE;
1018 return NULL;
1021 /* C++ FE sometimes change linkage flags after producing same body aliases.
1023 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1024 are obviously equivalent. The way it is doing so is however somewhat
1025 kludgy and interferes with the visibility code. As a result we need to
1026 copy the visibility from the target to get things right. */
1028 void
1029 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1031 if (is_a <cgraph_node> (node))
1033 DECL_DECLARED_INLINE_P (node->decl)
1034 = DECL_DECLARED_INLINE_P (target->decl);
1035 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1036 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1038 /* FIXME: It is not really clear why those flags should not be copied for
1039 functions, too. */
1040 else
1042 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1043 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1044 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1046 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1047 if (TREE_PUBLIC (node->decl))
1049 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1050 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1051 DECL_COMDAT_GROUP (node->decl)
1052 = DECL_COMDAT_GROUP (target->decl);
1053 if (DECL_ONE_ONLY (target->decl)
1054 && !node->same_comdat_group)
1055 symtab_add_to_same_comdat_group (node, target);
1057 node->externally_visible = target->externally_visible;
1060 /* Add reference recording that NODE is alias of TARGET.
1061 The function can fail in the case of aliasing cycles; in this case
1062 it returns false. */
1064 bool
1065 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1067 symtab_node *n;
1069 gcc_assert (!node->analyzed
1070 && !vec_safe_length (node->ref_list.references));
1072 /* Never let cycles to creep into the symbol table alias references;
1073 those will make alias walkers to be infinite. */
1074 for (n = target; n && n->alias;
1075 n = n->analyzed ? symtab_alias_target (n) : NULL)
1076 if (n == node)
1078 if (is_a <cgraph_node> (node))
1079 error ("function %q+D part of alias cycle", node->decl);
1080 else if (is_a <varpool_node> (node))
1081 error ("variable %q+D part of alias cycle", node->decl);
1082 else
1083 gcc_unreachable ();
1084 node->alias = false;
1085 return false;
1088 /* "analyze" the node - i.e. mark the reference. */
1089 node->definition = true;
1090 node->alias = true;
1091 node->analyzed = true;
1092 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1094 /* Alias targets become reudndant after alias is resolved into an reference.
1095 We do not want to keep it around or we would have to mind updating them
1096 when renaming symbols. */
1097 node->alias_target = NULL;
1099 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1100 fixup_same_cpp_alias_visibility (node, target);
1102 /* If alias has address taken, so does the target. */
1103 if (node->address_taken)
1104 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1105 return true;
1108 /* Call calback on NODE and aliases associated to NODE.
1109 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1110 skipped. */
1112 bool
1113 symtab_for_node_and_aliases (symtab_node *node,
1114 bool (*callback) (symtab_node *, void *),
1115 void *data,
1116 bool include_overwritable)
1118 int i;
1119 struct ipa_ref *ref;
1121 if (callback (node, data))
1122 return true;
1123 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1124 if (ref->use == IPA_REF_ALIAS)
1126 symtab_node *alias = ref->referring;
1127 if (include_overwritable
1128 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1129 if (symtab_for_node_and_aliases (alias, callback, data,
1130 include_overwritable))
1131 return true;
1133 return false;
1136 /* Worker searching nonoverwritable alias. */
1138 static bool
1139 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1141 if (decl_binds_to_current_def_p (node->decl))
1143 *(symtab_node **)data = node;
1144 return true;
1146 return false;
1149 /* If NODE can not be overwriten by static or dynamic linker to point to different
1150 definition, return NODE. Otherwise look for alias with such property and if
1151 none exists, introduce new one. */
1153 symtab_node *
1154 symtab_nonoverwritable_alias (symtab_node *node)
1156 tree new_decl;
1157 symtab_node *new_node = NULL;
1159 /* First try to look up existing alias or base object
1160 (if that is already non-overwritable). */
1161 node = symtab_alias_ultimate_target (node, NULL);
1162 gcc_assert (!node->alias && !node->weakref);
1163 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1164 (void *)&new_node, true);
1165 if (new_node)
1166 return new_node;
1167 #ifndef ASM_OUTPUT_DEF
1168 /* If aliases aren't supported by the assembler, fail. */
1169 return NULL;
1170 #endif
1172 /* Otherwise create a new one. */
1173 new_decl = copy_node (node->decl);
1174 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1175 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1176 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1177 DECL_INITIAL (new_decl) = NULL;
1178 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1179 SET_DECL_RTL (new_decl, NULL);
1181 /* Update the properties. */
1182 DECL_EXTERNAL (new_decl) = 0;
1183 if (DECL_ONE_ONLY (node->decl))
1184 DECL_SECTION_NAME (new_decl) = NULL;
1185 DECL_COMDAT_GROUP (new_decl) = 0;
1186 TREE_PUBLIC (new_decl) = 0;
1187 DECL_COMDAT (new_decl) = 0;
1188 DECL_WEAK (new_decl) = 0;
1189 DECL_VIRTUAL_P (new_decl) = 0;
1190 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1192 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1193 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1194 new_node = cgraph_create_function_alias
1195 (new_decl, node->decl);
1197 else
1198 new_node = varpool_create_variable_alias (new_decl,
1199 node->decl);
1200 symtab_resolve_alias (new_node, node);
1201 gcc_assert (decl_binds_to_current_def_p (new_decl));
1202 return new_node;
1205 /* Return true if A and B represents semantically equivalent symbols. */
1207 bool
1208 symtab_semantically_equivalent_p (symtab_node *a,
1209 symtab_node *b)
1211 enum availability avail;
1212 symtab_node *ba;
1213 symtab_node *bb;
1215 /* Equivalent functions are equivalent. */
1216 if (a->decl == b->decl)
1217 return true;
1219 /* If symbol is not overwritable by different implementation,
1220 walk to the base object it defines. */
1221 ba = symtab_alias_ultimate_target (a, &avail);
1222 if (avail >= AVAIL_AVAILABLE)
1224 if (ba == b)
1225 return true;
1227 else
1228 ba = a;
1229 bb = symtab_alias_ultimate_target (b, &avail);
1230 if (avail >= AVAIL_AVAILABLE)
1232 if (a == bb)
1233 return true;
1235 else
1236 bb = b;
1237 return bb == ba;
1239 #include "gt-symtab.h"