Fix dot dump bug
[official-gcc.git] / gcc / symtab.c
blob8158acc5bda8acb9d04e7acf52bd0ee771ad7279
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"
46 const char * const ld_plugin_symbol_resolution_names[]=
48 "",
49 "undef",
50 "prevailing_def",
51 "prevailing_def_ironly",
52 "preempted_reg",
53 "preempted_ir",
54 "resolved_ir",
55 "resolved_exec",
56 "resolved_dyn",
57 "prevailing_def_ironly_exp"
61 /* Hash table used to hold sectoons. */
62 static GTY((param_is (section_hash_entry))) htab_t section_hash;
64 /* Hash table used to convert assembler names into nodes. */
65 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
67 /* Linked list of symbol table nodes. */
68 symtab_node *symtab_nodes;
70 /* The order index of the next symtab node to be created. This is
71 used so that we can sort the cgraph nodes in order by when we saw
72 them, to support -fno-toplevel-reorder. */
73 int symtab_order;
75 /* Hash asmnames ignoring the user specified marks. */
77 static hashval_t
78 decl_assembler_name_hash (const_tree asmname)
80 if (IDENTIFIER_POINTER (asmname)[0] == '*')
82 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
83 size_t ulp_len = strlen (user_label_prefix);
85 if (ulp_len == 0)
87 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
88 decl_str += ulp_len;
90 return htab_hash_string (decl_str);
93 return htab_hash_string (IDENTIFIER_POINTER (asmname));
97 /* Returns a hash code for P. */
99 static hashval_t
100 hash_node_by_assembler_name (const void *p)
102 const symtab_node *n = (const symtab_node *) p;
103 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
106 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
108 static bool
109 decl_assembler_name_equal (tree decl, const_tree asmname)
111 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
112 const char *decl_str;
113 const char *asmname_str;
114 bool test = false;
116 if (decl_asmname == asmname)
117 return true;
119 decl_str = IDENTIFIER_POINTER (decl_asmname);
120 asmname_str = IDENTIFIER_POINTER (asmname);
123 /* If the target assembler name was set by the user, things are trickier.
124 We have a leading '*' to begin with. After that, it's arguable what
125 is the correct thing to do with -fleading-underscore. Arguably, we've
126 historically been doing the wrong thing in assemble_alias by always
127 printing the leading underscore. Since we're not changing that, make
128 sure user_label_prefix follows the '*' before matching. */
129 if (decl_str[0] == '*')
131 size_t ulp_len = strlen (user_label_prefix);
133 decl_str ++;
135 if (ulp_len == 0)
136 test = true;
137 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
138 decl_str += ulp_len, test=true;
139 else
140 decl_str --;
142 if (asmname_str[0] == '*')
144 size_t ulp_len = strlen (user_label_prefix);
146 asmname_str ++;
148 if (ulp_len == 0)
149 test = true;
150 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
151 asmname_str += ulp_len, test=true;
152 else
153 asmname_str --;
156 if (!test)
157 return false;
158 return strcmp (decl_str, asmname_str) == 0;
162 /* Returns nonzero if P1 and P2 are equal. */
164 static int
165 eq_assembler_name (const void *p1, const void *p2)
167 const symtab_node *n1 = (const symtab_node *) p1;
168 const_tree name = (const_tree)p2;
169 return (decl_assembler_name_equal (n1->decl, name));
172 /* Insert NODE to assembler name hash. */
174 static void
175 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
177 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
178 return;
179 gcc_checking_assert (!node->previous_sharing_asm_name
180 && !node->next_sharing_asm_name);
181 if (assembler_name_hash)
183 void **aslot;
184 struct cgraph_node *cnode;
185 tree decl = node->decl;
187 tree name = DECL_ASSEMBLER_NAME (node->decl);
189 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
190 decl_assembler_name_hash (name),
191 INSERT);
192 gcc_assert (*aslot != node);
193 node->next_sharing_asm_name = (symtab_node *)*aslot;
194 if (*aslot != NULL)
195 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
196 *aslot = node;
198 /* Update also possible inline clones sharing a decl. */
199 cnode = dyn_cast <cgraph_node *> (node);
200 if (cnode && cnode->clones && with_clones)
201 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
202 if (cnode->decl == decl)
203 insert_to_assembler_name_hash (cnode, true);
208 /* Remove NODE from assembler name hash. */
210 static void
211 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
213 if (assembler_name_hash)
215 struct cgraph_node *cnode;
216 tree decl = node->decl;
218 if (node->next_sharing_asm_name)
219 node->next_sharing_asm_name->previous_sharing_asm_name
220 = node->previous_sharing_asm_name;
221 if (node->previous_sharing_asm_name)
223 node->previous_sharing_asm_name->next_sharing_asm_name
224 = node->next_sharing_asm_name;
226 else
228 tree name = DECL_ASSEMBLER_NAME (node->decl);
229 void **slot;
230 slot = htab_find_slot_with_hash (assembler_name_hash, name,
231 decl_assembler_name_hash (name),
232 NO_INSERT);
233 gcc_assert (*slot == node);
234 if (!node->next_sharing_asm_name)
235 htab_clear_slot (assembler_name_hash, slot);
236 else
237 *slot = node->next_sharing_asm_name;
239 node->next_sharing_asm_name = NULL;
240 node->previous_sharing_asm_name = NULL;
242 /* Update also possible inline clones sharing a decl. */
243 cnode = dyn_cast <cgraph_node *> (node);
244 if (cnode && cnode->clones && with_clones)
245 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
246 if (cnode->decl == decl)
247 unlink_from_assembler_name_hash (cnode, true);
251 /* Arrange node to be first in its entry of assembler_name_hash. */
253 void
254 symtab_prevail_in_asm_name_hash (symtab_node *node)
256 unlink_from_assembler_name_hash (node, false);
257 insert_to_assembler_name_hash (node, false);
261 /* Add node into symbol table. This function is not used directly, but via
262 cgraph/varpool node creation routines. */
264 void
265 symtab_register_node (symtab_node *node)
267 node->next = symtab_nodes;
268 node->previous = NULL;
269 if (symtab_nodes)
270 symtab_nodes->previous = node;
271 symtab_nodes = node;
273 if (!node->decl->decl_with_vis.symtab_node)
274 node->decl->decl_with_vis.symtab_node = node;
276 ipa_empty_ref_list (&node->ref_list);
278 node->order = symtab_order++;
280 /* Be sure to do this last; C++ FE might create new nodes via
281 DECL_ASSEMBLER_NAME langhook! */
282 insert_to_assembler_name_hash (node, false);
285 /* Remove NODE from same comdat group. */
287 void
288 symtab_remove_from_same_comdat_group (symtab_node *node)
290 if (node->same_comdat_group)
292 symtab_node *prev;
293 for (prev = node->same_comdat_group;
294 prev->same_comdat_group != node;
295 prev = prev->same_comdat_group)
297 if (node->same_comdat_group == prev)
298 prev->same_comdat_group = NULL;
299 else
300 prev->same_comdat_group = node->same_comdat_group;
301 node->same_comdat_group = NULL;
305 /* Remove node from symbol table. This function is not used directly, but via
306 cgraph/varpool node removal routines. */
308 void
309 symtab_unregister_node (symtab_node *node)
311 ipa_remove_all_references (&node->ref_list);
312 ipa_remove_all_referring (&node->ref_list);
314 /* Remove reference to section. */
315 node->set_section_for_node (NULL);
317 symtab_remove_from_same_comdat_group (node);
319 if (node->previous)
320 node->previous->next = node->next;
321 else
322 symtab_nodes = node->next;
323 if (node->next)
324 node->next->previous = node->previous;
325 node->next = NULL;
326 node->previous = NULL;
328 /* During LTO symtab merging we temporarily corrupt decl to symtab node
329 hash. */
330 gcc_assert (node->decl->decl_with_vis.symtab_node || in_lto_p);
331 if (node->decl->decl_with_vis.symtab_node == node)
333 symtab_node *replacement_node = NULL;
334 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
335 replacement_node = cgraph_find_replacement_node (cnode);
336 node->decl->decl_with_vis.symtab_node = replacement_node;
338 if (!is_a <varpool_node *> (node) || !DECL_HARD_REGISTER (node->decl))
339 unlink_from_assembler_name_hash (node, false);
343 /* Remove symtab NODE from the symbol table. */
345 void
346 symtab_remove_node (symtab_node *node)
348 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
349 cgraph_remove_node (cnode);
350 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
351 varpool_remove_node (vnode);
354 /* Initalize asm name hash unless. */
356 void
357 symtab_initialize_asm_name_hash (void)
359 symtab_node *node;
360 if (!assembler_name_hash)
362 assembler_name_hash =
363 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
364 NULL);
365 FOR_EACH_SYMBOL (node)
366 insert_to_assembler_name_hash (node, false);
370 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
371 Return NULL if there's no such node. */
373 symtab_node *
374 symtab_node_for_asm (const_tree asmname)
376 symtab_node *node;
377 void **slot;
379 symtab_initialize_asm_name_hash ();
380 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
381 decl_assembler_name_hash (asmname),
382 NO_INSERT);
384 if (slot)
386 node = (symtab_node *) *slot;
387 return node;
389 return NULL;
392 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
394 void
395 change_decl_assembler_name (tree decl, tree name)
397 symtab_node *node = NULL;
399 /* We can have user ASM names on things, like global register variables, that
400 are not in the symbol table. */
401 if ((TREE_CODE (decl) == VAR_DECL
402 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
403 || TREE_CODE (decl) == FUNCTION_DECL)
404 node = symtab_get_node (decl);
405 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
407 SET_DECL_ASSEMBLER_NAME (decl, name);
408 if (node)
409 insert_to_assembler_name_hash (node, true);
411 else
413 if (name == DECL_ASSEMBLER_NAME (decl))
414 return;
416 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
417 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
418 : NULL);
419 if (node)
420 unlink_from_assembler_name_hash (node, true);
421 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
422 && DECL_RTL_SET_P (decl))
423 warning (0, "%D renamed after being referenced in assembly", decl);
425 SET_DECL_ASSEMBLER_NAME (decl, name);
426 if (alias)
428 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
429 TREE_CHAIN (name) = alias;
431 if (node)
432 insert_to_assembler_name_hash (node, true);
436 /* Add NEW_ to the same comdat group that OLD is in. */
438 void
439 symtab_add_to_same_comdat_group (symtab_node *new_node,
440 symtab_node *old_node)
442 gcc_assert (old_node->get_comdat_group ());
443 gcc_assert (!new_node->same_comdat_group);
444 gcc_assert (new_node != old_node);
446 new_node->set_comdat_group (old_node->get_comdat_group ());
447 new_node->same_comdat_group = old_node;
448 if (!old_node->same_comdat_group)
449 old_node->same_comdat_group = new_node;
450 else
452 symtab_node *n;
453 for (n = old_node->same_comdat_group;
454 n->same_comdat_group != old_node;
455 n = n->same_comdat_group)
457 n->same_comdat_group = new_node;
461 /* Dissolve the same_comdat_group list in which NODE resides. */
463 void
464 symtab_dissolve_same_comdat_group_list (symtab_node *node)
466 symtab_node *n = node;
467 symtab_node *next;
469 if (!node->same_comdat_group)
470 return;
473 next = n->same_comdat_group;
474 n->same_comdat_group = NULL;
475 /* Clear comdat_group for comdat locals, since
476 make_decl_local doesn't. */
477 if (!TREE_PUBLIC (n->decl))
478 n->set_comdat_group (NULL);
479 n = next;
481 while (n != node);
484 /* Return printable assembler name of NODE.
485 This function is used only for debugging. When assembler name
486 is unknown go with identifier name. */
488 const char *
489 symtab_node::asm_name () const
491 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
492 return lang_hooks.decl_printable_name (decl, 2);
493 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
496 /* Return printable identifier name. */
498 const char *
499 symtab_node::name () const
501 return lang_hooks.decl_printable_name (decl, 2);
504 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
506 /* Dump base fields of symtab nodes. Not to be used directly. */
508 void
509 dump_symtab_base (FILE *f, symtab_node *node)
511 static const char * const visibility_types[] = {
512 "default", "protected", "hidden", "internal"
515 fprintf (f, "%s/%i (%s)",
516 node->asm_name (),
517 node->order,
518 node->name ());
519 dump_addr (f, " @", (void *)node);
520 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
522 if (node->definition)
523 fprintf (f, " definition");
524 if (node->analyzed)
525 fprintf (f, " analyzed");
526 if (node->alias)
527 fprintf (f, " alias");
528 if (node->weakref)
529 fprintf (f, " weakref");
530 if (node->cpp_implicit_alias)
531 fprintf (f, " cpp_implicit_alias");
532 if (node->alias_target)
533 fprintf (f, " target:%s",
534 DECL_P (node->alias_target)
535 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
536 (node->alias_target))
537 : IDENTIFIER_POINTER (node->alias_target));
538 if (node->body_removed)
539 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
540 fprintf (f, "\n Visibility:");
541 if (node->in_other_partition)
542 fprintf (f, " in_other_partition");
543 if (node->used_from_other_partition)
544 fprintf (f, " used_from_other_partition");
545 if (node->force_output)
546 fprintf (f, " force_output");
547 if (node->forced_by_abi)
548 fprintf (f, " forced_by_abi");
549 if (node->externally_visible)
550 fprintf (f, " externally_visible");
551 if (node->resolution != LDPR_UNKNOWN)
552 fprintf (f, " %s",
553 ld_plugin_symbol_resolution_names[(int)node->resolution]);
554 if (TREE_ASM_WRITTEN (node->decl))
555 fprintf (f, " asm_written");
556 if (DECL_EXTERNAL (node->decl))
557 fprintf (f, " external");
558 if (TREE_PUBLIC (node->decl))
559 fprintf (f, " public");
560 if (DECL_COMMON (node->decl))
561 fprintf (f, " common");
562 if (DECL_WEAK (node->decl))
563 fprintf (f, " weak");
564 if (DECL_DLLIMPORT_P (node->decl))
565 fprintf (f, " dll_import");
566 if (DECL_COMDAT (node->decl))
567 fprintf (f, " comdat");
568 if (node->get_comdat_group ())
569 fprintf (f, " comdat_group:%s",
570 IDENTIFIER_POINTER (node->get_comdat_group_id ()));
571 if (DECL_ONE_ONLY (node->decl))
572 fprintf (f, " one_only");
573 if (node->get_section ())
574 fprintf (f, " section:%s",
575 node->get_section ());
576 if (node->implicit_section)
577 fprintf (f," (implicit_section)");
578 if (DECL_VISIBILITY_SPECIFIED (node->decl))
579 fprintf (f, " visibility_specified");
580 if (DECL_VISIBILITY (node->decl))
581 fprintf (f, " visibility:%s",
582 visibility_types [DECL_VISIBILITY (node->decl)]);
583 if (DECL_VIRTUAL_P (node->decl))
584 fprintf (f, " virtual");
585 if (DECL_ARTIFICIAL (node->decl))
586 fprintf (f, " artificial");
587 if (TREE_CODE (node->decl) == FUNCTION_DECL)
589 if (DECL_STATIC_CONSTRUCTOR (node->decl))
590 fprintf (f, " constructor");
591 if (DECL_STATIC_DESTRUCTOR (node->decl))
592 fprintf (f, " destructor");
594 fprintf (f, "\n");
596 if (node->same_comdat_group)
597 fprintf (f, " Same comdat group as: %s/%i\n",
598 node->same_comdat_group->asm_name (),
599 node->same_comdat_group->order);
600 if (node->next_sharing_asm_name)
601 fprintf (f, " next sharing asm name: %i\n",
602 node->next_sharing_asm_name->order);
603 if (node->previous_sharing_asm_name)
604 fprintf (f, " previous sharing asm name: %i\n",
605 node->previous_sharing_asm_name->order);
607 if (node->address_taken)
608 fprintf (f, " Address is taken.\n");
609 if (node->aux)
611 fprintf (f, " Aux:");
612 dump_addr (f, " @", (void *)node->aux);
615 fprintf (f, " References: ");
616 ipa_dump_references (f, &node->ref_list);
617 fprintf (f, " Referring: ");
618 ipa_dump_referring (f, &node->ref_list);
619 if (node->lto_file_data)
620 fprintf (f, " Read from file: %s\n",
621 node->lto_file_data->file_name);
624 /* Dump symtab node. */
626 void
627 dump_symtab_node (FILE *f, symtab_node *node)
629 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
630 dump_cgraph_node (f, cnode);
631 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
632 dump_varpool_node (f, vnode);
635 /* Dump symbol table. */
637 void
638 dump_symtab (FILE *f)
640 symtab_node *node;
641 fprintf (f, "Symbol table:\n\n");
642 FOR_EACH_SYMBOL (node)
643 dump_symtab_node (f, node);
646 /* Dump symtab node NODE to stderr. */
648 DEBUG_FUNCTION void
649 debug_symtab_node (symtab_node *node)
651 dump_symtab_node (stderr, node);
654 /* Dump symbol table to stderr. */
656 DEBUG_FUNCTION void
657 debug_symtab (void)
659 dump_symtab (stderr);
662 /* Verify common part of symtab nodes. */
664 DEBUG_FUNCTION bool
665 verify_symtab_base (symtab_node *node)
667 bool error_found = false;
668 symtab_node *hashed_node;
670 if (is_a <cgraph_node *> (node))
672 if (TREE_CODE (node->decl) != FUNCTION_DECL)
674 error ("function symbol is not function");
675 error_found = true;
678 else if (is_a <varpool_node *> (node))
680 if (TREE_CODE (node->decl) != VAR_DECL)
682 error ("variable symbol is not variable");
683 error_found = true;
686 else
688 error ("node has unknown type");
689 error_found = true;
692 if (cgraph_state != CGRAPH_LTO_STREAMING)
694 hashed_node = symtab_get_node (node->decl);
695 if (!hashed_node)
697 error ("node not found node->decl->decl_with_vis.symtab_node");
698 error_found = true;
700 if (hashed_node != node
701 && (!is_a <cgraph_node *> (node)
702 || !dyn_cast <cgraph_node *> (node)->clone_of
703 || dyn_cast <cgraph_node *> (node)->clone_of->decl
704 != node->decl))
706 error ("node differs from node->decl->decl_with_vis.symtab_node");
707 error_found = true;
710 if (assembler_name_hash)
712 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
713 if (hashed_node && hashed_node->previous_sharing_asm_name)
715 error ("assembler name hash list corrupted");
716 error_found = true;
718 while (hashed_node)
720 if (hashed_node == node)
721 break;
722 hashed_node = hashed_node->next_sharing_asm_name;
724 if (!hashed_node
725 && !(is_a <varpool_node *> (node)
726 || DECL_HARD_REGISTER (node->decl)))
728 error ("node not found in symtab assembler name hash");
729 error_found = true;
732 if (node->previous_sharing_asm_name
733 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
735 error ("double linked list of assembler names corrupted");
736 error_found = true;
738 if (node->analyzed && !node->definition)
740 error ("node is analyzed byt it is not a definition");
741 error_found = true;
743 if (node->cpp_implicit_alias && !node->alias)
745 error ("node is alias but not implicit alias");
746 error_found = true;
748 if (node->alias && !node->definition
749 && !node->weakref)
751 error ("node is alias but not definition");
752 error_found = true;
754 if (node->weakref && !node->alias)
756 error ("node is weakref but not an alias");
757 error_found = true;
759 if (node->same_comdat_group)
761 symtab_node *n = node->same_comdat_group;
763 if (!n->get_comdat_group ())
765 error ("node is in same_comdat_group list but has no comdat_group");
766 error_found = true;
768 if (n->get_comdat_group () != node->get_comdat_group ())
770 error ("same_comdat_group list across different groups");
771 error_found = true;
773 if (!n->definition)
775 error ("Node has same_comdat_group but it is not a definition");
776 error_found = true;
778 if (n->type != node->type)
780 error ("mixing different types of symbol in same comdat groups is not supported");
781 error_found = true;
783 if (n == node)
785 error ("node is alone in a comdat group");
786 error_found = true;
790 if (!n->same_comdat_group)
792 error ("same_comdat_group is not a circular list");
793 error_found = true;
794 break;
796 n = n->same_comdat_group;
798 while (n != node);
799 if (symtab_comdat_local_p (node))
801 struct ipa_ref_list *refs = &node->ref_list;
802 struct ipa_ref *ref;
804 for (int i = 0; ipa_ref_list_referring_iterate (refs, i, ref); ++i)
806 if (!symtab_in_same_comdat_p (ref->referring, node))
808 error ("comdat-local symbol referred to by %s outside its "
809 "comdat",
810 identifier_to_locale (ref->referring->name()));
811 error_found = true;
816 if (node->implicit_section && !node->get_section ())
818 error ("implicit_section flag is set but section isn't");
819 error_found = true;
821 if (node->get_section () && node->get_comdat_group ()
822 && !node->implicit_section)
824 error ("Both section and comdat group is set");
825 error_found = true;
827 /* TODO: Add string table for sections, so we do not keep holding duplicated
828 strings. */
829 if (node->alias && node->definition
830 && node->get_section () != symtab_alias_target (node)->get_section ()
831 && (!node->get_section()
832 || !symtab_alias_target (node)->get_section ()
833 || strcmp (node->get_section(),
834 symtab_alias_target (node)->get_section ())))
836 error ("Alias and target's section differs");
837 dump_symtab_node (stderr, symtab_alias_target (node));
838 error_found = true;
840 if (node->alias && node->definition
841 && node->get_comdat_group () != symtab_alias_target (node)->get_comdat_group ())
843 error ("Alias and target's comdat groups differs");
844 dump_symtab_node (stderr, symtab_alias_target (node));
845 error_found = true;
848 return error_found;
851 /* Verify consistency of NODE. */
853 DEBUG_FUNCTION void
854 verify_symtab_node (symtab_node *node)
856 if (seen_error ())
857 return;
859 timevar_push (TV_CGRAPH_VERIFY);
860 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
861 verify_cgraph_node (cnode);
862 else
863 if (verify_symtab_base (node))
865 dump_symtab_node (stderr, node);
866 internal_error ("verify_symtab_node failed");
868 timevar_pop (TV_CGRAPH_VERIFY);
871 /* Verify symbol table for internal consistency. */
873 DEBUG_FUNCTION void
874 verify_symtab (void)
876 symtab_node *node;
877 pointer_map<symtab_node *> comdat_head_map;
879 FOR_EACH_SYMBOL (node)
881 verify_symtab_node (node);
882 if (node->get_comdat_group ())
884 symtab_node **entry, *s;
885 bool existed;
887 entry = comdat_head_map.insert (node->get_comdat_group (), &existed);
888 if (!existed)
889 *entry = node;
890 else
891 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
892 if (!s || s == *entry)
894 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
895 dump_symtab_node (stderr, *entry);
896 dump_symtab_node (stderr, s);
897 internal_error ("verify_symtab failed");
903 /* Return true when RESOLUTION indicate that linker will use
904 the symbol from non-LTO object files. */
906 bool
907 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
909 return (resolution == LDPR_PREVAILING_DEF
910 || resolution == LDPR_PREEMPTED_REG
911 || resolution == LDPR_RESOLVED_EXEC
912 || resolution == LDPR_RESOLVED_DYN);
915 /* Return true when NODE is known to be used from other (non-LTO) object file.
916 Known only when doing LTO via linker plugin. */
918 bool
919 symtab_used_from_object_file_p (symtab_node *node)
921 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
922 return false;
923 if (resolution_used_from_other_file_p (node->resolution))
924 return true;
925 return false;
928 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
929 but other code such as notice_global_symbol generates rtl. */
931 void
932 symtab_make_decl_local (tree decl)
934 rtx rtl, symbol;
936 /* Avoid clearing comdat_groups on comdat-local decls. */
937 if (TREE_PUBLIC (decl) == 0)
938 return;
940 if (TREE_CODE (decl) == VAR_DECL)
941 DECL_COMMON (decl) = 0;
942 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
944 DECL_COMDAT (decl) = 0;
945 DECL_WEAK (decl) = 0;
946 DECL_EXTERNAL (decl) = 0;
947 DECL_VISIBILITY_SPECIFIED (decl) = 0;
948 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
949 TREE_PUBLIC (decl) = 0;
950 if (!DECL_RTL_SET_P (decl))
951 return;
953 /* Update rtl flags. */
954 make_decl_rtl (decl);
956 rtl = DECL_RTL (decl);
957 if (!MEM_P (rtl))
958 return;
960 symbol = XEXP (rtl, 0);
961 if (GET_CODE (symbol) != SYMBOL_REF)
962 return;
964 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
967 /* Return availability of NODE. */
969 enum availability
970 symtab_node_availability (symtab_node *node)
972 if (is_a <cgraph_node *> (node))
973 return cgraph_function_body_availability (cgraph (node));
974 else
975 return cgraph_variable_initializer_availability (varpool (node));
978 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
979 If NODE is not an alias, return NODE.
980 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
982 symtab_node *
983 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
985 bool weakref_p = false;
987 if (!node->alias)
989 if (availability)
990 *availability = symtab_node_availability (node);
991 return node;
994 /* To determine visibility of the target, we follow ELF semantic of aliases.
995 Here alias is an alternative assembler name of a given definition. Its
996 availability prevails the availability of its target (i.e. static alias of
997 weak definition is available.
999 Weakref is a different animal (and not part of ELF per se). It is just
1000 alternative name of a given symbol used within one complation unit
1001 and is translated prior hitting the object file. It inherits the
1002 visibility of its target (i.e. weakref of non-overwritable definition
1003 is non-overwritable, while weakref of weak definition is weak).
1005 If we ever get into supporting targets with different semantics, a target
1006 hook will be needed here. */
1008 if (availability)
1010 weakref_p = node->weakref;
1011 if (!weakref_p)
1012 *availability = symtab_node_availability (node);
1013 else
1014 *availability = AVAIL_LOCAL;
1016 while (node)
1018 if (node->alias && node->analyzed)
1019 node = symtab_alias_target (node);
1020 else
1022 if (!availability)
1024 else if (node->analyzed)
1026 if (weakref_p)
1028 enum availability a = symtab_node_availability (node);
1029 if (a < *availability)
1030 *availability = a;
1033 else
1034 *availability = AVAIL_NOT_AVAILABLE;
1035 return node;
1037 if (node && availability && weakref_p)
1039 enum availability a = symtab_node_availability (node);
1040 if (a < *availability)
1041 *availability = a;
1042 weakref_p = node->weakref;
1045 if (availability)
1046 *availability = AVAIL_NOT_AVAILABLE;
1047 return NULL;
1050 /* C++ FE sometimes change linkage flags after producing same body aliases.
1052 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1053 are obviously equivalent. The way it is doing so is however somewhat
1054 kludgy and interferes with the visibility code. As a result we need to
1055 copy the visibility from the target to get things right. */
1057 void
1058 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1060 if (is_a <cgraph_node *> (node))
1062 DECL_DECLARED_INLINE_P (node->decl)
1063 = DECL_DECLARED_INLINE_P (target->decl);
1064 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1065 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1067 /* FIXME: It is not really clear why those flags should not be copied for
1068 functions, too. */
1069 else
1071 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1072 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1073 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1075 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1076 if (TREE_PUBLIC (node->decl))
1078 tree group;
1080 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1081 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1082 group = target->get_comdat_group ();
1083 node->set_comdat_group (group);
1084 if (group
1085 && !node->same_comdat_group)
1086 symtab_add_to_same_comdat_group (node, target);
1088 node->externally_visible = target->externally_visible;
1091 /* Hash sections by their names. */
1093 static hashval_t
1094 hash_section_hash_entry (const void *p)
1096 const section_hash_entry *n = (const section_hash_entry *) p;
1097 return htab_hash_string (n->name);
1100 /* Return true if section P1 name equals to P2. */
1102 static int
1103 eq_sections (const void *p1, const void *p2)
1105 const section_hash_entry *n1 = (const section_hash_entry *) p1;
1106 const char *name = (const char *)p2;
1107 return n1->name == name || !strcmp (n1->name, name);
1110 /* Set section, do not recurse into aliases.
1111 When one wants to change section of symbol and its aliases,
1112 use set_section */
1114 void
1115 symtab_node::set_section_for_node (const char *section)
1117 const char *current = get_section ();
1118 void **slot;
1120 if (current == section
1121 || (current && section
1122 && !strcmp (current, section)))
1123 return;
1125 if (current)
1127 x_section->ref_count--;
1128 if (!x_section->ref_count)
1130 slot = htab_find_slot_with_hash (section_hash, x_section->name,
1131 htab_hash_string (x_section->name),
1132 INSERT);
1133 ggc_free (x_section);
1134 htab_clear_slot (section_hash, slot);
1136 x_section = NULL;
1138 if (!section)
1140 implicit_section = false;
1141 return;
1143 if (!section_hash)
1144 section_hash = htab_create_ggc (10, hash_section_hash_entry,
1145 eq_sections, NULL);
1146 slot = htab_find_slot_with_hash (section_hash, section,
1147 htab_hash_string (section),
1148 INSERT);
1149 if (*slot)
1150 x_section = (section_hash_entry *)*slot;
1151 else
1153 int len = strlen (section);
1154 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1155 x_section->name = ggc_vec_alloc<char> (len + 1);
1156 memcpy (x_section->name, section, len + 1);
1158 x_section->ref_count++;
1161 /* Worker for set_section. */
1163 static bool
1164 set_section_1 (struct symtab_node *n, void *s)
1166 n->set_section_for_node ((char *)s);
1167 return false;
1170 /* Set section of symbol and its aliases. */
1172 void
1173 symtab_node::set_section (const char *section)
1175 gcc_assert (!this->alias);
1176 symtab_for_node_and_aliases (this, set_section_1, const_cast<char *>(section), true);
1179 /* Worker for symtab_resolve_alias. */
1181 static bool
1182 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
1184 n->implicit_section = true;
1185 return false;
1188 /* Add reference recording that NODE is alias of TARGET.
1189 The function can fail in the case of aliasing cycles; in this case
1190 it returns false. */
1192 bool
1193 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1195 symtab_node *n;
1197 gcc_assert (!node->analyzed
1198 && !vec_safe_length (node->ref_list.references));
1200 /* Never let cycles to creep into the symbol table alias references;
1201 those will make alias walkers to be infinite. */
1202 for (n = target; n && n->alias;
1203 n = n->analyzed ? symtab_alias_target (n) : NULL)
1204 if (n == node)
1206 if (is_a <cgraph_node *> (node))
1207 error ("function %q+D part of alias cycle", node->decl);
1208 else if (is_a <varpool_node *> (node))
1209 error ("variable %q+D part of alias cycle", node->decl);
1210 else
1211 gcc_unreachable ();
1212 node->alias = false;
1213 return false;
1216 /* "analyze" the node - i.e. mark the reference. */
1217 node->definition = true;
1218 node->alias = true;
1219 node->analyzed = true;
1220 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1222 /* Add alias into the comdat group of its target unless it is already there. */
1223 if (node->same_comdat_group)
1224 symtab_remove_from_same_comdat_group (node);
1225 node->set_comdat_group (NULL);
1226 if (target->get_comdat_group ())
1227 symtab_add_to_same_comdat_group (node, target);
1229 if ((node->get_section () != target->get_section ()
1230 || target->get_comdat_group ())
1231 && node->get_section () && !node->implicit_section)
1233 error ("section of alias %q+D must match section of its target",
1234 node->decl);
1236 symtab_for_node_and_aliases (node, set_section_1,
1237 const_cast<char *>(target->get_section ()), true);
1238 if (target->implicit_section)
1239 symtab_for_node_and_aliases (node,
1240 set_implicit_section, NULL, true);
1242 /* Alias targets become redundant after alias is resolved into an reference.
1243 We do not want to keep it around or we would have to mind updating them
1244 when renaming symbols. */
1245 node->alias_target = NULL;
1247 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1248 fixup_same_cpp_alias_visibility (node, target);
1250 /* If alias has address taken, so does the target. */
1251 if (node->address_taken)
1252 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1253 return true;
1256 /* Call calback on NODE and aliases associated to NODE.
1257 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1258 skipped. */
1260 bool
1261 symtab_for_node_and_aliases (symtab_node *node,
1262 bool (*callback) (symtab_node *, void *),
1263 void *data,
1264 bool include_overwritable)
1266 int i;
1267 struct ipa_ref *ref;
1269 if (callback (node, data))
1270 return true;
1271 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1272 if (ref->use == IPA_REF_ALIAS)
1274 symtab_node *alias = ref->referring;
1275 if (include_overwritable
1276 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1277 if (symtab_for_node_and_aliases (alias, callback, data,
1278 include_overwritable))
1279 return true;
1281 return false;
1284 /* Worker searching nonoverwritable alias. */
1286 static bool
1287 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1289 if (decl_binds_to_current_def_p (node->decl))
1291 *(symtab_node **)data = node;
1292 return true;
1294 return false;
1297 /* If NODE can not be overwriten by static or dynamic linker to point to different
1298 definition, return NODE. Otherwise look for alias with such property and if
1299 none exists, introduce new one. */
1301 symtab_node *
1302 symtab_nonoverwritable_alias (symtab_node *node)
1304 tree new_decl;
1305 symtab_node *new_node = NULL;
1307 /* First try to look up existing alias or base object
1308 (if that is already non-overwritable). */
1309 node = symtab_alias_ultimate_target (node, NULL);
1310 gcc_assert (!node->alias && !node->weakref);
1311 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1312 (void *)&new_node, true);
1313 if (new_node)
1314 return new_node;
1315 #ifndef ASM_OUTPUT_DEF
1316 /* If aliases aren't supported by the assembler, fail. */
1317 return NULL;
1318 #endif
1320 /* Otherwise create a new one. */
1321 new_decl = copy_node (node->decl);
1322 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1323 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1324 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1325 DECL_INITIAL (new_decl) = NULL;
1326 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1327 SET_DECL_RTL (new_decl, NULL);
1329 /* Update the properties. */
1330 DECL_EXTERNAL (new_decl) = 0;
1331 TREE_PUBLIC (new_decl) = 0;
1332 DECL_COMDAT (new_decl) = 0;
1333 DECL_WEAK (new_decl) = 0;
1335 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1336 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1337 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1339 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1340 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1341 new_node = cgraph_create_function_alias
1342 (new_decl, node->decl);
1344 else
1346 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1347 DECL_INITIAL (new_decl) = error_mark_node;
1348 new_node = varpool_create_variable_alias (new_decl, node->decl);
1350 symtab_resolve_alias (new_node, node);
1351 gcc_assert (decl_binds_to_current_def_p (new_decl)
1352 && targetm.binds_local_p (new_decl));
1353 return new_node;
1356 /* Return true if A and B represents semantically equivalent symbols. */
1358 bool
1359 symtab_semantically_equivalent_p (symtab_node *a,
1360 symtab_node *b)
1362 enum availability avail;
1363 symtab_node *ba;
1364 symtab_node *bb;
1366 /* Equivalent functions are equivalent. */
1367 if (a->decl == b->decl)
1368 return true;
1370 /* If symbol is not overwritable by different implementation,
1371 walk to the base object it defines. */
1372 ba = symtab_alias_ultimate_target (a, &avail);
1373 if (avail >= AVAIL_AVAILABLE)
1375 if (ba == b)
1376 return true;
1378 else
1379 ba = a;
1380 bb = symtab_alias_ultimate_target (b, &avail);
1381 if (avail >= AVAIL_AVAILABLE)
1383 if (a == bb)
1384 return true;
1386 else
1387 bb = b;
1388 return bb == ba;
1391 /* Classify symbol NODE for partitioning. */
1393 enum symbol_partitioning_class
1394 symtab_get_symbol_partitioning_class (symtab_node *node)
1396 /* Inline clones are always duplicated.
1397 This include external delcarations. */
1398 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1400 if (DECL_ABSTRACT (node->decl))
1401 return SYMBOL_EXTERNAL;
1403 if (cnode && cnode->global.inlined_to)
1404 return SYMBOL_DUPLICATE;
1406 /* Weakref aliases are always duplicated. */
1407 if (node->weakref)
1408 return SYMBOL_DUPLICATE;
1410 /* External declarations are external. */
1411 if (DECL_EXTERNAL (node->decl))
1412 return SYMBOL_EXTERNAL;
1414 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
1416 /* Constant pool references use local symbol names that can not
1417 be promoted global. We should never put into a constant pool
1418 objects that can not be duplicated across partitions. */
1419 if (DECL_IN_CONSTANT_POOL (node->decl))
1420 return SYMBOL_DUPLICATE;
1421 gcc_checking_assert (vnode->definition);
1423 /* Functions that are cloned may stay in callgraph even if they are unused.
1424 Handle them as external; compute_ltrans_boundary take care to make
1425 proper things to happen (i.e. to make them appear in the boundary but
1426 with body streamed, so clone can me materialized). */
1427 else if (!cgraph (node)->definition)
1428 return SYMBOL_EXTERNAL;
1430 /* Linker discardable symbols are duplicated to every use unless they are
1431 keyed. */
1432 if (DECL_ONE_ONLY (node->decl)
1433 && !node->force_output
1434 && !node->forced_by_abi
1435 && !symtab_used_from_object_file_p (node))
1436 return SYMBOL_DUPLICATE;
1438 return SYMBOL_PARTITION;
1440 #include "gt-symtab.h"