* varasm.c (set_implicit_section): New function.
[official-gcc.git] / gcc / symtab.c
blobd7977f1e8d41c65c15fc1efb6ac3eeb4b64fe64e
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"
60 /* Hash table used to convert assembler names into nodes. */
61 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
63 /* Linked list of symbol table nodes. */
64 symtab_node *symtab_nodes;
66 /* The order index of the next symtab node to be created. This is
67 used so that we can sort the cgraph nodes in order by when we saw
68 them, to support -fno-toplevel-reorder. */
69 int symtab_order;
71 /* Hash asmnames ignoring the user specified marks. */
73 static hashval_t
74 decl_assembler_name_hash (const_tree asmname)
76 if (IDENTIFIER_POINTER (asmname)[0] == '*')
78 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
79 size_t ulp_len = strlen (user_label_prefix);
81 if (ulp_len == 0)
83 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
84 decl_str += ulp_len;
86 return htab_hash_string (decl_str);
89 return htab_hash_string (IDENTIFIER_POINTER (asmname));
93 /* Returns a hash code for P. */
95 static hashval_t
96 hash_node_by_assembler_name (const void *p)
98 const symtab_node *n = (const symtab_node *) p;
99 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
102 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
104 static bool
105 decl_assembler_name_equal (tree decl, const_tree asmname)
107 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
108 const char *decl_str;
109 const char *asmname_str;
110 bool test = false;
112 if (decl_asmname == asmname)
113 return true;
115 decl_str = IDENTIFIER_POINTER (decl_asmname);
116 asmname_str = IDENTIFIER_POINTER (asmname);
119 /* If the target assembler name was set by the user, things are trickier.
120 We have a leading '*' to begin with. After that, it's arguable what
121 is the correct thing to do with -fleading-underscore. Arguably, we've
122 historically been doing the wrong thing in assemble_alias by always
123 printing the leading underscore. Since we're not changing that, make
124 sure user_label_prefix follows the '*' before matching. */
125 if (decl_str[0] == '*')
127 size_t ulp_len = strlen (user_label_prefix);
129 decl_str ++;
131 if (ulp_len == 0)
132 test = true;
133 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
134 decl_str += ulp_len, test=true;
135 else
136 decl_str --;
138 if (asmname_str[0] == '*')
140 size_t ulp_len = strlen (user_label_prefix);
142 asmname_str ++;
144 if (ulp_len == 0)
145 test = true;
146 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
147 asmname_str += ulp_len, test=true;
148 else
149 asmname_str --;
152 if (!test)
153 return false;
154 return strcmp (decl_str, asmname_str) == 0;
158 /* Returns nonzero if P1 and P2 are equal. */
160 static int
161 eq_assembler_name (const void *p1, const void *p2)
163 const symtab_node *n1 = (const symtab_node *) p1;
164 const_tree name = (const_tree)p2;
165 return (decl_assembler_name_equal (n1->decl, name));
168 /* Insert NODE to assembler name hash. */
170 static void
171 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
173 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
174 return;
175 gcc_checking_assert (!node->previous_sharing_asm_name
176 && !node->next_sharing_asm_name);
177 if (assembler_name_hash)
179 void **aslot;
180 struct cgraph_node *cnode;
181 tree decl = node->decl;
183 tree name = DECL_ASSEMBLER_NAME (node->decl);
185 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
186 decl_assembler_name_hash (name),
187 INSERT);
188 gcc_assert (*aslot != node);
189 node->next_sharing_asm_name = (symtab_node *)*aslot;
190 if (*aslot != NULL)
191 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
192 *aslot = node;
194 /* Update also possible inline clones sharing a decl. */
195 cnode = dyn_cast <cgraph_node *> (node);
196 if (cnode && cnode->clones && with_clones)
197 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
198 if (cnode->decl == decl)
199 insert_to_assembler_name_hash (cnode, true);
204 /* Remove NODE from assembler name hash. */
206 static void
207 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
209 if (assembler_name_hash)
211 struct cgraph_node *cnode;
212 tree decl = node->decl;
214 if (node->next_sharing_asm_name)
215 node->next_sharing_asm_name->previous_sharing_asm_name
216 = node->previous_sharing_asm_name;
217 if (node->previous_sharing_asm_name)
219 node->previous_sharing_asm_name->next_sharing_asm_name
220 = node->next_sharing_asm_name;
222 else
224 tree name = DECL_ASSEMBLER_NAME (node->decl);
225 void **slot;
226 slot = htab_find_slot_with_hash (assembler_name_hash, name,
227 decl_assembler_name_hash (name),
228 NO_INSERT);
229 gcc_assert (*slot == node);
230 if (!node->next_sharing_asm_name)
231 htab_clear_slot (assembler_name_hash, slot);
232 else
233 *slot = node->next_sharing_asm_name;
235 node->next_sharing_asm_name = NULL;
236 node->previous_sharing_asm_name = NULL;
238 /* Update also possible inline clones sharing a decl. */
239 cnode = dyn_cast <cgraph_node *> (node);
240 if (cnode && cnode->clones && with_clones)
241 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
242 if (cnode->decl == decl)
243 unlink_from_assembler_name_hash (cnode, true);
247 /* Arrange node to be first in its entry of assembler_name_hash. */
249 void
250 symtab_prevail_in_asm_name_hash (symtab_node *node)
252 unlink_from_assembler_name_hash (node, false);
253 insert_to_assembler_name_hash (node, false);
257 /* Add node into symbol table. This function is not used directly, but via
258 cgraph/varpool node creation routines. */
260 void
261 symtab_register_node (symtab_node *node)
263 node->next = symtab_nodes;
264 node->previous = NULL;
265 if (symtab_nodes)
266 symtab_nodes->previous = node;
267 symtab_nodes = node;
269 if (!node->decl->decl_with_vis.symtab_node)
270 node->decl->decl_with_vis.symtab_node = node;
272 ipa_empty_ref_list (&node->ref_list);
274 node->order = symtab_order++;
276 /* Be sure to do this last; C++ FE might create new nodes via
277 DECL_ASSEMBLER_NAME langhook! */
278 insert_to_assembler_name_hash (node, false);
281 /* Remove NODE from same comdat group. */
283 void
284 symtab_remove_from_same_comdat_group (symtab_node *node)
286 if (node->same_comdat_group)
288 symtab_node *prev;
289 for (prev = node->same_comdat_group;
290 prev->same_comdat_group != node;
291 prev = prev->same_comdat_group)
293 if (node->same_comdat_group == prev)
294 prev->same_comdat_group = NULL;
295 else
296 prev->same_comdat_group = node->same_comdat_group;
297 node->same_comdat_group = NULL;
301 /* Remove node from symbol table. This function is not used directly, but via
302 cgraph/varpool node removal routines. */
304 void
305 symtab_unregister_node (symtab_node *node)
307 ipa_remove_all_references (&node->ref_list);
308 ipa_remove_all_referring (&node->ref_list);
310 symtab_remove_from_same_comdat_group (node);
312 if (node->previous)
313 node->previous->next = node->next;
314 else
315 symtab_nodes = node->next;
316 if (node->next)
317 node->next->previous = node->previous;
318 node->next = NULL;
319 node->previous = NULL;
321 /* During LTO symtab merging we temporarily corrupt decl to symtab node
322 hash. */
323 gcc_assert (node->decl->decl_with_vis.symtab_node || in_lto_p);
324 if (node->decl->decl_with_vis.symtab_node == node)
326 symtab_node *replacement_node = NULL;
327 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
328 replacement_node = cgraph_find_replacement_node (cnode);
329 node->decl->decl_with_vis.symtab_node = replacement_node;
331 if (!is_a <varpool_node *> (node) || !DECL_HARD_REGISTER (node->decl))
332 unlink_from_assembler_name_hash (node, false);
336 /* Remove symtab NODE from the symbol table. */
338 void
339 symtab_remove_node (symtab_node *node)
341 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
342 cgraph_remove_node (cnode);
343 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
344 varpool_remove_node (vnode);
347 /* Initalize asm name hash unless. */
349 void
350 symtab_initialize_asm_name_hash (void)
352 symtab_node *node;
353 if (!assembler_name_hash)
355 assembler_name_hash =
356 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
357 NULL);
358 FOR_EACH_SYMBOL (node)
359 insert_to_assembler_name_hash (node, false);
363 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
364 Return NULL if there's no such node. */
366 symtab_node *
367 symtab_node_for_asm (const_tree asmname)
369 symtab_node *node;
370 void **slot;
372 symtab_initialize_asm_name_hash ();
373 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
374 decl_assembler_name_hash (asmname),
375 NO_INSERT);
377 if (slot)
379 node = (symtab_node *) *slot;
380 return node;
382 return NULL;
385 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
387 void
388 change_decl_assembler_name (tree decl, tree name)
390 symtab_node *node = NULL;
392 /* We can have user ASM names on things, like global register variables, that
393 are not in the symbol table. */
394 if ((TREE_CODE (decl) == VAR_DECL
395 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
396 || TREE_CODE (decl) == FUNCTION_DECL)
397 node = symtab_get_node (decl);
398 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
400 SET_DECL_ASSEMBLER_NAME (decl, name);
401 if (node)
402 insert_to_assembler_name_hash (node, true);
404 else
406 if (name == DECL_ASSEMBLER_NAME (decl))
407 return;
409 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
410 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
411 : NULL);
412 if (node)
413 unlink_from_assembler_name_hash (node, true);
414 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
415 && DECL_RTL_SET_P (decl))
416 warning (0, "%D renamed after being referenced in assembly", decl);
418 SET_DECL_ASSEMBLER_NAME (decl, name);
419 if (alias)
421 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
422 TREE_CHAIN (name) = alias;
424 if (node)
425 insert_to_assembler_name_hash (node, true);
429 /* Add NEW_ to the same comdat group that OLD is in. */
431 void
432 symtab_add_to_same_comdat_group (symtab_node *new_node,
433 symtab_node *old_node)
435 gcc_assert (old_node->get_comdat_group ());
436 gcc_assert (!new_node->same_comdat_group);
437 gcc_assert (new_node != old_node);
439 new_node->set_comdat_group (old_node->get_comdat_group ());
440 new_node->same_comdat_group = old_node;
441 if (!old_node->same_comdat_group)
442 old_node->same_comdat_group = new_node;
443 else
445 symtab_node *n;
446 for (n = old_node->same_comdat_group;
447 n->same_comdat_group != old_node;
448 n = n->same_comdat_group)
450 n->same_comdat_group = new_node;
454 /* Dissolve the same_comdat_group list in which NODE resides. */
456 void
457 symtab_dissolve_same_comdat_group_list (symtab_node *node)
459 symtab_node *n = node;
460 symtab_node *next;
462 if (!node->same_comdat_group)
463 return;
466 next = n->same_comdat_group;
467 n->same_comdat_group = NULL;
468 /* Clear comdat_group for comdat locals, since
469 make_decl_local doesn't. */
470 if (!TREE_PUBLIC (n->decl))
471 n->set_comdat_group (NULL);
472 n = next;
474 while (n != node);
477 /* Return printable assembler name of NODE.
478 This function is used only for debugging. When assembler name
479 is unknown go with identifier name. */
481 const char *
482 symtab_node::asm_name () const
484 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
485 return lang_hooks.decl_printable_name (decl, 2);
486 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
489 /* Return printable identifier name. */
491 const char *
492 symtab_node::name () const
494 return lang_hooks.decl_printable_name (decl, 2);
497 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
499 /* Dump base fields of symtab nodes. Not to be used directly. */
501 void
502 dump_symtab_base (FILE *f, symtab_node *node)
504 static const char * const visibility_types[] = {
505 "default", "protected", "hidden", "internal"
508 fprintf (f, "%s/%i (%s)",
509 node->asm_name (),
510 node->order,
511 node->name ());
512 dump_addr (f, " @", (void *)node);
513 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
515 if (node->definition)
516 fprintf (f, " definition");
517 if (node->analyzed)
518 fprintf (f, " analyzed");
519 if (node->alias)
520 fprintf (f, " alias");
521 if (node->weakref)
522 fprintf (f, " weakref");
523 if (node->cpp_implicit_alias)
524 fprintf (f, " cpp_implicit_alias");
525 if (node->alias_target)
526 fprintf (f, " target:%s",
527 DECL_P (node->alias_target)
528 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
529 (node->alias_target))
530 : IDENTIFIER_POINTER (node->alias_target));
531 if (node->body_removed)
532 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
533 fprintf (f, "\n Visibility:");
534 if (node->in_other_partition)
535 fprintf (f, " in_other_partition");
536 if (node->used_from_other_partition)
537 fprintf (f, " used_from_other_partition");
538 if (node->force_output)
539 fprintf (f, " force_output");
540 if (node->forced_by_abi)
541 fprintf (f, " forced_by_abi");
542 if (node->externally_visible)
543 fprintf (f, " externally_visible");
544 if (node->resolution != LDPR_UNKNOWN)
545 fprintf (f, " %s",
546 ld_plugin_symbol_resolution_names[(int)node->resolution]);
547 if (TREE_ASM_WRITTEN (node->decl))
548 fprintf (f, " asm_written");
549 if (DECL_EXTERNAL (node->decl))
550 fprintf (f, " external");
551 if (TREE_PUBLIC (node->decl))
552 fprintf (f, " public");
553 if (DECL_COMMON (node->decl))
554 fprintf (f, " common");
555 if (DECL_WEAK (node->decl))
556 fprintf (f, " weak");
557 if (DECL_DLLIMPORT_P (node->decl))
558 fprintf (f, " dll_import");
559 if (DECL_COMDAT (node->decl))
560 fprintf (f, " comdat");
561 if (node->get_comdat_group ())
562 fprintf (f, " comdat_group:%s",
563 IDENTIFIER_POINTER (node->get_comdat_group_id ()));
564 if (DECL_ONE_ONLY (node->decl))
565 fprintf (f, " one_only");
566 if (node->get_section ())
567 fprintf (f, " section:%s",
568 node->get_section ());
569 if (node->implicit_section)
570 fprintf (f," (implicit_section)");
571 if (DECL_VISIBILITY_SPECIFIED (node->decl))
572 fprintf (f, " visibility_specified");
573 if (DECL_VISIBILITY (node->decl))
574 fprintf (f, " visibility:%s",
575 visibility_types [DECL_VISIBILITY (node->decl)]);
576 if (DECL_VIRTUAL_P (node->decl))
577 fprintf (f, " virtual");
578 if (DECL_ARTIFICIAL (node->decl))
579 fprintf (f, " artificial");
580 if (TREE_CODE (node->decl) == FUNCTION_DECL)
582 if (DECL_STATIC_CONSTRUCTOR (node->decl))
583 fprintf (f, " constructor");
584 if (DECL_STATIC_DESTRUCTOR (node->decl))
585 fprintf (f, " destructor");
587 fprintf (f, "\n");
589 if (node->same_comdat_group)
590 fprintf (f, " Same comdat group as: %s/%i\n",
591 node->same_comdat_group->asm_name (),
592 node->same_comdat_group->order);
593 if (node->next_sharing_asm_name)
594 fprintf (f, " next sharing asm name: %i\n",
595 node->next_sharing_asm_name->order);
596 if (node->previous_sharing_asm_name)
597 fprintf (f, " previous sharing asm name: %i\n",
598 node->previous_sharing_asm_name->order);
600 if (node->address_taken)
601 fprintf (f, " Address is taken.\n");
602 if (node->aux)
604 fprintf (f, " Aux:");
605 dump_addr (f, " @", (void *)node->aux);
608 fprintf (f, " References: ");
609 ipa_dump_references (f, &node->ref_list);
610 fprintf (f, " Referring: ");
611 ipa_dump_referring (f, &node->ref_list);
612 if (node->lto_file_data)
613 fprintf (f, " Read from file: %s\n",
614 node->lto_file_data->file_name);
617 /* Dump symtab node. */
619 void
620 dump_symtab_node (FILE *f, symtab_node *node)
622 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
623 dump_cgraph_node (f, cnode);
624 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
625 dump_varpool_node (f, vnode);
628 /* Dump symbol table. */
630 void
631 dump_symtab (FILE *f)
633 symtab_node *node;
634 fprintf (f, "Symbol table:\n\n");
635 FOR_EACH_SYMBOL (node)
636 dump_symtab_node (f, node);
639 /* Dump symtab node NODE to stderr. */
641 DEBUG_FUNCTION void
642 debug_symtab_node (symtab_node *node)
644 dump_symtab_node (stderr, node);
647 /* Dump symbol table to stderr. */
649 DEBUG_FUNCTION void
650 debug_symtab (void)
652 dump_symtab (stderr);
655 /* Verify common part of symtab nodes. */
657 DEBUG_FUNCTION bool
658 verify_symtab_base (symtab_node *node)
660 bool error_found = false;
661 symtab_node *hashed_node;
663 if (is_a <cgraph_node *> (node))
665 if (TREE_CODE (node->decl) != FUNCTION_DECL)
667 error ("function symbol is not function");
668 error_found = true;
671 else if (is_a <varpool_node *> (node))
673 if (TREE_CODE (node->decl) != VAR_DECL)
675 error ("variable symbol is not variable");
676 error_found = true;
679 else
681 error ("node has unknown type");
682 error_found = true;
685 if (cgraph_state != CGRAPH_LTO_STREAMING)
687 hashed_node = symtab_get_node (node->decl);
688 if (!hashed_node)
690 error ("node not found node->decl->decl_with_vis.symtab_node");
691 error_found = true;
693 if (hashed_node != node
694 && (!is_a <cgraph_node *> (node)
695 || !dyn_cast <cgraph_node *> (node)->clone_of
696 || dyn_cast <cgraph_node *> (node)->clone_of->decl
697 != node->decl))
699 error ("node differs from node->decl->decl_with_vis.symtab_node");
700 error_found = true;
703 if (assembler_name_hash)
705 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
706 if (hashed_node && hashed_node->previous_sharing_asm_name)
708 error ("assembler name hash list corrupted");
709 error_found = true;
711 while (hashed_node)
713 if (hashed_node == node)
714 break;
715 hashed_node = hashed_node->next_sharing_asm_name;
717 if (!hashed_node
718 && !(is_a <varpool_node *> (node)
719 || DECL_HARD_REGISTER (node->decl)))
721 error ("node not found in symtab assembler name hash");
722 error_found = true;
725 if (node->previous_sharing_asm_name
726 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
728 error ("double linked list of assembler names corrupted");
729 error_found = true;
731 if (node->analyzed && !node->definition)
733 error ("node is analyzed byt it is not a definition");
734 error_found = true;
736 if (node->cpp_implicit_alias && !node->alias)
738 error ("node is alias but not implicit alias");
739 error_found = true;
741 if (node->alias && !node->definition
742 && !node->weakref)
744 error ("node is alias but not definition");
745 error_found = true;
747 if (node->weakref && !node->alias)
749 error ("node is weakref but not an alias");
750 error_found = true;
752 if (node->same_comdat_group)
754 symtab_node *n = node->same_comdat_group;
756 if (!n->get_comdat_group ())
758 error ("node is in same_comdat_group list but has no comdat_group");
759 error_found = true;
761 if (n->get_comdat_group () != node->get_comdat_group ())
763 error ("same_comdat_group list across different groups");
764 error_found = true;
766 if (!n->definition)
768 error ("Node has same_comdat_group but it is not a definition");
769 error_found = true;
771 if (n->type != node->type)
773 error ("mixing different types of symbol in same comdat groups is not supported");
774 error_found = true;
776 if (n == node)
778 error ("node is alone in a comdat group");
779 error_found = true;
783 if (!n->same_comdat_group)
785 error ("same_comdat_group is not a circular list");
786 error_found = true;
787 break;
789 n = n->same_comdat_group;
791 while (n != node);
792 if (symtab_comdat_local_p (node))
794 struct ipa_ref_list *refs = &node->ref_list;
795 struct ipa_ref *ref;
797 for (int i = 0; ipa_ref_list_referring_iterate (refs, i, ref); ++i)
799 if (!symtab_in_same_comdat_p (ref->referring, node))
801 error ("comdat-local symbol referred to by %s outside its "
802 "comdat",
803 identifier_to_locale (ref->referring->name()));
804 error_found = true;
809 if (node->implicit_section && !node->get_section ())
811 error ("implicit_section flag is set but section isn't");
812 error_found = true;
814 if (node->get_section () && node->get_comdat_group ()
815 && !node->implicit_section)
817 error ("Both section and comdat group is set");
818 error_found = true;
820 /* TODO: Add string table for sections, so we do not keep holding duplicated
821 strings. */
822 if (node->alias && node->definition
823 && node->get_section () != symtab_alias_target (node)->get_section ()
824 && (!node->get_section()
825 || !symtab_alias_target (node)->get_section ()
826 || strcmp (node->get_section(),
827 symtab_alias_target (node)->get_section ())))
829 error ("Alias and target's section differs");
830 dump_symtab_node (stderr, symtab_alias_target (node));
831 error_found = true;
833 if (node->alias && node->definition
834 && node->get_comdat_group () != symtab_alias_target (node)->get_comdat_group ())
836 error ("Alias and target's comdat groups differs");
837 dump_symtab_node (stderr, symtab_alias_target (node));
838 error_found = true;
841 return error_found;
844 /* Verify consistency of NODE. */
846 DEBUG_FUNCTION void
847 verify_symtab_node (symtab_node *node)
849 if (seen_error ())
850 return;
852 timevar_push (TV_CGRAPH_VERIFY);
853 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
854 verify_cgraph_node (cnode);
855 else
856 if (verify_symtab_base (node))
858 dump_symtab_node (stderr, node);
859 internal_error ("verify_symtab_node failed");
861 timevar_pop (TV_CGRAPH_VERIFY);
864 /* Verify symbol table for internal consistency. */
866 DEBUG_FUNCTION void
867 verify_symtab (void)
869 symtab_node *node;
870 pointer_map<symtab_node *> comdat_head_map;
872 FOR_EACH_SYMBOL (node)
874 verify_symtab_node (node);
875 if (node->get_comdat_group ())
877 symtab_node **entry, *s;
878 bool existed;
880 entry = comdat_head_map.insert (node->get_comdat_group (), &existed);
881 if (!existed)
882 *entry = node;
883 else
884 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
885 if (!s || s == *entry)
887 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
888 dump_symtab_node (stderr, *entry);
889 dump_symtab_node (stderr, s);
890 internal_error ("verify_symtab failed");
896 /* Return true when RESOLUTION indicate that linker will use
897 the symbol from non-LTO object files. */
899 bool
900 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
902 return (resolution == LDPR_PREVAILING_DEF
903 || resolution == LDPR_PREEMPTED_REG
904 || resolution == LDPR_RESOLVED_EXEC
905 || resolution == LDPR_RESOLVED_DYN);
908 /* Return true when NODE is known to be used from other (non-LTO) object file.
909 Known only when doing LTO via linker plugin. */
911 bool
912 symtab_used_from_object_file_p (symtab_node *node)
914 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
915 return false;
916 if (resolution_used_from_other_file_p (node->resolution))
917 return true;
918 return false;
921 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
922 but other code such as notice_global_symbol generates rtl. */
924 void
925 symtab_make_decl_local (tree decl)
927 rtx rtl, symbol;
929 /* Avoid clearing comdat_groups on comdat-local decls. */
930 if (TREE_PUBLIC (decl) == 0)
931 return;
933 if (TREE_CODE (decl) == VAR_DECL)
934 DECL_COMMON (decl) = 0;
935 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
937 DECL_COMDAT (decl) = 0;
938 DECL_WEAK (decl) = 0;
939 DECL_EXTERNAL (decl) = 0;
940 DECL_VISIBILITY_SPECIFIED (decl) = 0;
941 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
942 TREE_PUBLIC (decl) = 0;
943 if (!DECL_RTL_SET_P (decl))
944 return;
946 /* Update rtl flags. */
947 make_decl_rtl (decl);
949 rtl = DECL_RTL (decl);
950 if (!MEM_P (rtl))
951 return;
953 symbol = XEXP (rtl, 0);
954 if (GET_CODE (symbol) != SYMBOL_REF)
955 return;
957 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
960 /* Return availability of NODE. */
962 enum availability
963 symtab_node_availability (symtab_node *node)
965 if (is_a <cgraph_node *> (node))
966 return cgraph_function_body_availability (cgraph (node));
967 else
968 return cgraph_variable_initializer_availability (varpool (node));
971 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
972 If NODE is not an alias, return NODE.
973 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
975 symtab_node *
976 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
978 bool weakref_p = false;
980 if (!node->alias)
982 if (availability)
983 *availability = symtab_node_availability (node);
984 return node;
987 /* To determine visibility of the target, we follow ELF semantic of aliases.
988 Here alias is an alternative assembler name of a given definition. Its
989 availability prevails the availability of its target (i.e. static alias of
990 weak definition is available.
992 Weakref is a different animal (and not part of ELF per se). It is just
993 alternative name of a given symbol used within one complation unit
994 and is translated prior hitting the object file. It inherits the
995 visibility of its target (i.e. weakref of non-overwritable definition
996 is non-overwritable, while weakref of weak definition is weak).
998 If we ever get into supporting targets with different semantics, a target
999 hook will be needed here. */
1001 if (availability)
1003 weakref_p = node->weakref;
1004 if (!weakref_p)
1005 *availability = symtab_node_availability (node);
1006 else
1007 *availability = AVAIL_LOCAL;
1009 while (node)
1011 if (node->alias && node->analyzed)
1012 node = symtab_alias_target (node);
1013 else
1015 if (!availability)
1017 else if (node->analyzed)
1019 if (weakref_p)
1021 enum availability a = symtab_node_availability (node);
1022 if (a < *availability)
1023 *availability = a;
1026 else
1027 *availability = AVAIL_NOT_AVAILABLE;
1028 return node;
1030 if (node && availability && weakref_p)
1032 enum availability a = symtab_node_availability (node);
1033 if (a < *availability)
1034 *availability = a;
1035 weakref_p = node->weakref;
1038 if (availability)
1039 *availability = AVAIL_NOT_AVAILABLE;
1040 return NULL;
1043 /* C++ FE sometimes change linkage flags after producing same body aliases.
1045 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1046 are obviously equivalent. The way it is doing so is however somewhat
1047 kludgy and interferes with the visibility code. As a result we need to
1048 copy the visibility from the target to get things right. */
1050 void
1051 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1053 if (is_a <cgraph_node *> (node))
1055 DECL_DECLARED_INLINE_P (node->decl)
1056 = DECL_DECLARED_INLINE_P (target->decl);
1057 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1058 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1060 /* FIXME: It is not really clear why those flags should not be copied for
1061 functions, too. */
1062 else
1064 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1065 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1066 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1068 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1069 if (TREE_PUBLIC (node->decl))
1071 tree group;
1073 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1074 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1075 group = target->get_comdat_group ();
1076 node->set_comdat_group (group);
1077 if (group
1078 && !node->same_comdat_group)
1079 symtab_add_to_same_comdat_group (node, target);
1081 node->externally_visible = target->externally_visible;
1084 /* Worker for set_section. */
1086 static bool
1087 set_section_1 (struct symtab_node *n, void *s)
1089 n->set_section_for_node ((tree)s);
1090 return false;
1093 /* Set section of symbol and its aliases. */
1095 void
1096 symtab_node::set_section (tree section)
1098 gcc_assert (!this->alias);
1099 symtab_for_node_and_aliases (this, set_section_1, section, true);
1102 /* Worker for symtab_resolve_alias. */
1104 static bool
1105 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
1107 n->implicit_section = true;
1108 return false;
1111 /* Add reference recording that NODE is alias of TARGET.
1112 The function can fail in the case of aliasing cycles; in this case
1113 it returns false. */
1115 bool
1116 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1118 symtab_node *n;
1120 gcc_assert (!node->analyzed
1121 && !vec_safe_length (node->ref_list.references));
1123 /* Never let cycles to creep into the symbol table alias references;
1124 those will make alias walkers to be infinite. */
1125 for (n = target; n && n->alias;
1126 n = n->analyzed ? symtab_alias_target (n) : NULL)
1127 if (n == node)
1129 if (is_a <cgraph_node *> (node))
1130 error ("function %q+D part of alias cycle", node->decl);
1131 else if (is_a <varpool_node *> (node))
1132 error ("variable %q+D part of alias cycle", node->decl);
1133 else
1134 gcc_unreachable ();
1135 node->alias = false;
1136 return false;
1139 /* "analyze" the node - i.e. mark the reference. */
1140 node->definition = true;
1141 node->alias = true;
1142 node->analyzed = true;
1143 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1145 /* Add alias into the comdat group of its target unless it is already there. */
1146 if (node->same_comdat_group)
1147 symtab_remove_from_same_comdat_group (node);
1148 node->set_comdat_group (NULL);
1149 if (target->get_comdat_group ())
1150 symtab_add_to_same_comdat_group (node, target);
1152 if ((node->get_section () != target->get_section ()
1153 || target->get_comdat_group ())
1154 && node->get_section () && !node->implicit_section)
1156 error ("section of alias %q+D must match section of its target",
1157 node->decl);
1159 symtab_for_node_and_aliases (node, set_section_1, target->get_section_name (), true);
1160 if (target->implicit_section)
1161 symtab_for_node_and_aliases (node,
1162 set_implicit_section, NULL, true);
1164 /* Alias targets become redundant after alias is resolved into an reference.
1165 We do not want to keep it around or we would have to mind updating them
1166 when renaming symbols. */
1167 node->alias_target = NULL;
1169 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1170 fixup_same_cpp_alias_visibility (node, target);
1172 /* If alias has address taken, so does the target. */
1173 if (node->address_taken)
1174 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1175 return true;
1178 /* Call calback on NODE and aliases associated to NODE.
1179 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1180 skipped. */
1182 bool
1183 symtab_for_node_and_aliases (symtab_node *node,
1184 bool (*callback) (symtab_node *, void *),
1185 void *data,
1186 bool include_overwritable)
1188 int i;
1189 struct ipa_ref *ref;
1191 if (callback (node, data))
1192 return true;
1193 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1194 if (ref->use == IPA_REF_ALIAS)
1196 symtab_node *alias = ref->referring;
1197 if (include_overwritable
1198 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1199 if (symtab_for_node_and_aliases (alias, callback, data,
1200 include_overwritable))
1201 return true;
1203 return false;
1206 /* Worker searching nonoverwritable alias. */
1208 static bool
1209 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1211 if (decl_binds_to_current_def_p (node->decl))
1213 *(symtab_node **)data = node;
1214 return true;
1216 return false;
1219 /* If NODE can not be overwriten by static or dynamic linker to point to different
1220 definition, return NODE. Otherwise look for alias with such property and if
1221 none exists, introduce new one. */
1223 symtab_node *
1224 symtab_nonoverwritable_alias (symtab_node *node)
1226 tree new_decl;
1227 symtab_node *new_node = NULL;
1229 /* First try to look up existing alias or base object
1230 (if that is already non-overwritable). */
1231 node = symtab_alias_ultimate_target (node, NULL);
1232 gcc_assert (!node->alias && !node->weakref);
1233 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1234 (void *)&new_node, true);
1235 if (new_node)
1236 return new_node;
1237 #ifndef ASM_OUTPUT_DEF
1238 /* If aliases aren't supported by the assembler, fail. */
1239 return NULL;
1240 #endif
1242 /* Otherwise create a new one. */
1243 new_decl = copy_node (node->decl);
1244 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1245 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1246 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1247 DECL_INITIAL (new_decl) = NULL;
1248 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1249 SET_DECL_RTL (new_decl, NULL);
1251 /* Update the properties. */
1252 DECL_EXTERNAL (new_decl) = 0;
1253 TREE_PUBLIC (new_decl) = 0;
1254 DECL_COMDAT (new_decl) = 0;
1255 DECL_WEAK (new_decl) = 0;
1257 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1258 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1259 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1261 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1262 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1263 new_node = cgraph_create_function_alias
1264 (new_decl, node->decl);
1266 else
1268 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1269 DECL_INITIAL (new_decl) = error_mark_node;
1270 new_node = varpool_create_variable_alias (new_decl, node->decl);
1272 symtab_resolve_alias (new_node, node);
1273 gcc_assert (decl_binds_to_current_def_p (new_decl)
1274 && targetm.binds_local_p (new_decl));
1275 return new_node;
1278 /* Return true if A and B represents semantically equivalent symbols. */
1280 bool
1281 symtab_semantically_equivalent_p (symtab_node *a,
1282 symtab_node *b)
1284 enum availability avail;
1285 symtab_node *ba;
1286 symtab_node *bb;
1288 /* Equivalent functions are equivalent. */
1289 if (a->decl == b->decl)
1290 return true;
1292 /* If symbol is not overwritable by different implementation,
1293 walk to the base object it defines. */
1294 ba = symtab_alias_ultimate_target (a, &avail);
1295 if (avail >= AVAIL_AVAILABLE)
1297 if (ba == b)
1298 return true;
1300 else
1301 ba = a;
1302 bb = symtab_alias_ultimate_target (b, &avail);
1303 if (avail >= AVAIL_AVAILABLE)
1305 if (a == bb)
1306 return true;
1308 else
1309 bb = b;
1310 return bb == ba;
1313 /* Classify symbol NODE for partitioning. */
1315 enum symbol_partitioning_class
1316 symtab_get_symbol_partitioning_class (symtab_node *node)
1318 /* Inline clones are always duplicated.
1319 This include external delcarations. */
1320 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1322 if (DECL_ABSTRACT (node->decl))
1323 return SYMBOL_EXTERNAL;
1325 if (cnode && cnode->global.inlined_to)
1326 return SYMBOL_DUPLICATE;
1328 /* Weakref aliases are always duplicated. */
1329 if (node->weakref)
1330 return SYMBOL_DUPLICATE;
1332 /* External declarations are external. */
1333 if (DECL_EXTERNAL (node->decl))
1334 return SYMBOL_EXTERNAL;
1336 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
1338 /* Constant pool references use local symbol names that can not
1339 be promoted global. We should never put into a constant pool
1340 objects that can not be duplicated across partitions. */
1341 if (DECL_IN_CONSTANT_POOL (node->decl))
1342 return SYMBOL_DUPLICATE;
1343 gcc_checking_assert (vnode->definition);
1345 /* Functions that are cloned may stay in callgraph even if they are unused.
1346 Handle them as external; compute_ltrans_boundary take care to make
1347 proper things to happen (i.e. to make them appear in the boundary but
1348 with body streamed, so clone can me materialized). */
1349 else if (!cgraph (node)->definition)
1350 return SYMBOL_EXTERNAL;
1352 /* Linker discardable symbols are duplicated to every use unless they are
1353 keyed. */
1354 if (DECL_ONE_ONLY (node->decl)
1355 && !node->force_output
1356 && !node->forced_by_abi
1357 && !symtab_used_from_object_file_p (node))
1358 return SYMBOL_DUPLICATE;
1360 return SYMBOL_PARTITION;
1362 #include "gt-symtab.h"