/cp
[official-gcc.git] / gcc / symtab.c
blob90924c3180365ffeec490914cb809199638bd4f8
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"
37 const char * const ld_plugin_symbol_resolution_names[]=
39 "",
40 "undef",
41 "prevailing_def",
42 "prevailing_def_ironly",
43 "preempted_reg",
44 "preempted_ir",
45 "resolved_ir",
46 "resolved_exec",
47 "resolved_dyn",
48 "prevailing_def_ironly_exp"
51 /* Hash table used to convert declarations into nodes. */
52 static GTY((param_is (symtab_node))) htab_t symtab_hash;
53 /* Hash table used to convert assembler names into nodes. */
54 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
56 /* Linked list of symbol table nodes. */
57 symtab_node *symtab_nodes;
59 /* The order index of the next symtab node to be created. This is
60 used so that we can sort the cgraph nodes in order by when we saw
61 them, to support -fno-toplevel-reorder. */
62 int symtab_order;
64 /* Returns a hash code for P. */
66 static hashval_t
67 hash_node (const void *p)
69 const symtab_node *n = (const symtab_node *) p;
70 return (hashval_t) DECL_UID (n->decl);
74 /* Returns nonzero if P1 and P2 are equal. */
76 static int
77 eq_node (const void *p1, const void *p2)
79 const symtab_node *n1 = (const symtab_node *) p1;
80 const symtab_node *n2 = (const symtab_node *) p2;
81 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
84 /* Returns a hash code for P. */
86 static hashval_t
87 hash_node_by_assembler_name (const void *p)
89 const symtab_node *n = (const symtab_node *) p;
90 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
93 /* Returns nonzero if P1 and P2 are equal. */
95 static int
96 eq_assembler_name (const void *p1, const void *p2)
98 const symtab_node *n1 = (const symtab_node *) p1;
99 const_tree name = (const_tree)p2;
100 return (decl_assembler_name_equal (n1->decl, name));
103 /* Insert NODE to assembler name hash. */
105 static void
106 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
108 if (is_a <varpool_node> (node) && DECL_HARD_REGISTER (node->decl))
109 return;
110 gcc_checking_assert (!node->previous_sharing_asm_name
111 && !node->next_sharing_asm_name);
112 if (assembler_name_hash)
114 void **aslot;
115 struct cgraph_node *cnode;
116 tree decl = node->decl;
118 tree name = DECL_ASSEMBLER_NAME (node->decl);
120 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
121 decl_assembler_name_hash (name),
122 INSERT);
123 gcc_assert (*aslot != node);
124 node->next_sharing_asm_name = (symtab_node *)*aslot;
125 if (*aslot != NULL)
126 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
127 *aslot = node;
129 /* Update also possible inline clones sharing a decl. */
130 cnode = dyn_cast <cgraph_node> (node);
131 if (cnode && cnode->clones && with_clones)
132 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
133 if (cnode->decl == decl)
134 insert_to_assembler_name_hash (cnode, true);
139 /* Remove NODE from assembler name hash. */
141 static void
142 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
144 if (assembler_name_hash)
146 struct cgraph_node *cnode;
147 tree decl = node->decl;
149 if (node->next_sharing_asm_name)
150 node->next_sharing_asm_name->previous_sharing_asm_name
151 = node->previous_sharing_asm_name;
152 if (node->previous_sharing_asm_name)
154 node->previous_sharing_asm_name->next_sharing_asm_name
155 = node->next_sharing_asm_name;
157 else
159 tree name = DECL_ASSEMBLER_NAME (node->decl);
160 void **slot;
161 slot = htab_find_slot_with_hash (assembler_name_hash, name,
162 decl_assembler_name_hash (name),
163 NO_INSERT);
164 gcc_assert (*slot == node);
165 if (!node->next_sharing_asm_name)
166 htab_clear_slot (assembler_name_hash, slot);
167 else
168 *slot = node->next_sharing_asm_name;
170 node->next_sharing_asm_name = NULL;
171 node->previous_sharing_asm_name = NULL;
173 /* Update also possible inline clones sharing a decl. */
174 cnode = dyn_cast <cgraph_node> (node);
175 if (cnode && cnode->clones && with_clones)
176 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
177 if (cnode->decl == decl)
178 unlink_from_assembler_name_hash (cnode, true);
182 /* Arrange node to be first in its entry of assembler_name_hash. */
184 void
185 symtab_prevail_in_asm_name_hash (symtab_node *node)
187 unlink_from_assembler_name_hash (node, false);
188 insert_to_assembler_name_hash (node, false);
192 /* Add node into symbol table. This function is not used directly, but via
193 cgraph/varpool node creation routines. */
195 void
196 symtab_register_node (symtab_node *node)
198 struct symtab_node key;
199 symtab_node **slot;
201 node->next = symtab_nodes;
202 node->previous = NULL;
203 if (symtab_nodes)
204 symtab_nodes->previous = node;
205 symtab_nodes = node;
207 if (!symtab_hash)
208 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
209 key.decl = node->decl;
210 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
211 if (*slot == NULL)
212 *slot = node;
214 ipa_empty_ref_list (&node->ref_list);
216 node->order = symtab_order++;
218 /* Be sure to do this last; C++ FE might create new nodes via
219 DECL_ASSEMBLER_NAME langhook! */
220 insert_to_assembler_name_hash (node, false);
223 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
224 of inline clones. */
226 void
227 symtab_insert_node_to_hashtable (symtab_node *node)
229 struct symtab_node key;
230 symtab_node **slot;
232 if (!symtab_hash)
233 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
234 key.decl = node->decl;
235 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
236 *slot = node;
239 /* Remove node from symbol table. This function is not used directly, but via
240 cgraph/varpool node removal routines. */
242 void
243 symtab_unregister_node (symtab_node *node)
245 void **slot;
246 ipa_remove_all_references (&node->ref_list);
247 ipa_remove_all_referring (&node->ref_list);
249 if (node->same_comdat_group)
251 symtab_node *prev;
252 for (prev = node->same_comdat_group;
253 prev->same_comdat_group != node;
254 prev = prev->same_comdat_group)
256 if (node->same_comdat_group == prev)
257 prev->same_comdat_group = NULL;
258 else
259 prev->same_comdat_group = node->same_comdat_group;
260 node->same_comdat_group = NULL;
263 if (node->previous)
264 node->previous->next = node->next;
265 else
266 symtab_nodes = node->next;
267 if (node->next)
268 node->next->previous = node->previous;
269 node->next = NULL;
270 node->previous = NULL;
272 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
274 /* During LTO symtab merging we temporarily corrupt decl to symtab node
275 hash. */
276 gcc_assert ((slot && *slot) || in_lto_p);
277 if (slot && *slot && *slot == node)
279 symtab_node *replacement_node = NULL;
280 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
281 replacement_node = cgraph_find_replacement_node (cnode);
282 if (!replacement_node)
283 htab_clear_slot (symtab_hash, slot);
284 else
285 *slot = replacement_node;
287 if (!is_a <varpool_node> (node) || !DECL_HARD_REGISTER (node->decl))
288 unlink_from_assembler_name_hash (node, false);
291 /* Return symbol table node associated with DECL, if any,
292 and NULL otherwise. */
294 symtab_node *
295 symtab_get_node (const_tree decl)
297 symtab_node **slot;
298 struct symtab_node key;
300 #ifdef ENABLE_CHECKING
301 /* Check that we are called for sane type of object - functions
302 and static or external variables. */
303 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
304 || (TREE_CODE (decl) == VAR_DECL
305 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
306 || in_lto_p)));
307 #endif
309 if (!symtab_hash)
310 return NULL;
312 key.decl = CONST_CAST2 (tree, const_tree, decl);
314 slot = (symtab_node **) htab_find_slot (symtab_hash, &key,
315 NO_INSERT);
317 if (slot)
318 return *slot;
319 return NULL;
322 /* Remove symtab NODE from the symbol table. */
324 void
325 symtab_remove_node (symtab_node *node)
327 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
328 cgraph_remove_node (cnode);
329 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
330 varpool_remove_node (vnode);
333 /* Initalize asm name hash unless. */
335 void
336 symtab_initialize_asm_name_hash (void)
338 symtab_node *node;
339 if (!assembler_name_hash)
341 assembler_name_hash =
342 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
343 NULL);
344 FOR_EACH_SYMBOL (node)
345 insert_to_assembler_name_hash (node, false);
349 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
350 Return NULL if there's no such node. */
352 symtab_node *
353 symtab_node_for_asm (const_tree asmname)
355 symtab_node *node;
356 void **slot;
358 symtab_initialize_asm_name_hash ();
359 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
360 decl_assembler_name_hash (asmname),
361 NO_INSERT);
363 if (slot)
365 node = (symtab_node *) *slot;
366 return node;
368 return NULL;
371 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
373 void
374 change_decl_assembler_name (tree decl, tree name)
376 symtab_node *node = NULL;
378 /* We can have user ASM names on things, like global register variables, that
379 are not in the symbol table. */
380 if ((TREE_CODE (decl) == VAR_DECL
381 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
382 || TREE_CODE (decl) == FUNCTION_DECL)
383 node = symtab_get_node (decl);
384 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
386 SET_DECL_ASSEMBLER_NAME (decl, name);
387 if (node)
388 insert_to_assembler_name_hash (node, true);
390 else
392 if (name == DECL_ASSEMBLER_NAME (decl))
393 return;
395 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
396 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
397 : NULL);
398 if (node)
399 unlink_from_assembler_name_hash (node, true);
400 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
401 && DECL_RTL_SET_P (decl))
402 warning (0, "%D renamed after being referenced in assembly", decl);
404 SET_DECL_ASSEMBLER_NAME (decl, name);
405 if (alias)
407 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
408 TREE_CHAIN (DECL_ASSEMBLER_NAME (name)) = alias;
410 if (node)
411 insert_to_assembler_name_hash (node, true);
415 /* Add NEW_ to the same comdat group that OLD is in. */
417 void
418 symtab_add_to_same_comdat_group (symtab_node *new_node,
419 symtab_node *old_node)
421 gcc_assert (DECL_ONE_ONLY (old_node->decl));
422 gcc_assert (!new_node->same_comdat_group);
423 gcc_assert (new_node != old_node);
425 DECL_COMDAT_GROUP (new_node->decl) = DECL_COMDAT_GROUP (old_node->decl);
426 new_node->same_comdat_group = old_node;
427 if (!old_node->same_comdat_group)
428 old_node->same_comdat_group = new_node;
429 else
431 symtab_node *n;
432 for (n = old_node->same_comdat_group;
433 n->same_comdat_group != old_node;
434 n = n->same_comdat_group)
436 n->same_comdat_group = new_node;
440 /* Dissolve the same_comdat_group list in which NODE resides. */
442 void
443 symtab_dissolve_same_comdat_group_list (symtab_node *node)
445 symtab_node *n = node;
446 symtab_node *next;
448 if (!node->same_comdat_group)
449 return;
452 next = n->same_comdat_group;
453 n->same_comdat_group = NULL;
454 n = next;
456 while (n != node);
459 /* Return printable assembler name of NODE.
460 This function is used only for debugging. When assembler name
461 is unknown go with identifier name. */
463 const char *
464 symtab_node_asm_name (symtab_node *node)
466 if (!DECL_ASSEMBLER_NAME_SET_P (node->decl))
467 return lang_hooks.decl_printable_name (node->decl, 2);
468 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl));
471 /* Return printable identifier name. */
473 const char *
474 symtab_node_name (symtab_node *node)
476 return lang_hooks.decl_printable_name (node->decl, 2);
479 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
481 /* Dump base fields of symtab nodes. Not to be used directly. */
483 void
484 dump_symtab_base (FILE *f, symtab_node *node)
486 static const char * const visibility_types[] = {
487 "default", "protected", "hidden", "internal"
490 fprintf (f, "%s/%i (%s)",
491 symtab_node_asm_name (node),
492 node->order,
493 symtab_node_name (node));
494 dump_addr (f, " @", (void *)node);
495 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
497 if (node->definition)
498 fprintf (f, " definition");
499 if (node->analyzed)
500 fprintf (f, " analyzed");
501 if (node->alias)
502 fprintf (f, " alias");
503 if (node->weakref)
504 fprintf (f, " weakref");
505 if (node->cpp_implicit_alias)
506 fprintf (f, " cpp_implicit_alias");
507 if (node->alias_target)
508 fprintf (f, " target:%s",
509 DECL_P (node->alias_target)
510 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
511 (node->alias_target))
512 : IDENTIFIER_POINTER (node->alias_target));
513 fprintf (f, "\n Visibility:");
514 if (node->in_other_partition)
515 fprintf (f, " in_other_partition");
516 if (node->used_from_other_partition)
517 fprintf (f, " used_from_other_partition");
518 if (node->force_output)
519 fprintf (f, " force_output");
520 if (node->forced_by_abi)
521 fprintf (f, " forced_by_abi");
522 if (node->externally_visible)
523 fprintf (f, " externally_visible");
524 if (node->resolution != LDPR_UNKNOWN)
525 fprintf (f, " %s",
526 ld_plugin_symbol_resolution_names[(int)node->resolution]);
527 if (TREE_ASM_WRITTEN (node->decl))
528 fprintf (f, " asm_written");
529 if (DECL_EXTERNAL (node->decl))
530 fprintf (f, " external");
531 if (TREE_PUBLIC (node->decl))
532 fprintf (f, " public");
533 if (DECL_COMMON (node->decl))
534 fprintf (f, " common");
535 if (DECL_WEAK (node->decl))
536 fprintf (f, " weak");
537 if (DECL_DLLIMPORT_P (node->decl))
538 fprintf (f, " dll_import");
539 if (DECL_COMDAT (node->decl))
540 fprintf (f, " comdat");
541 if (DECL_COMDAT_GROUP (node->decl))
542 fprintf (f, " comdat_group:%s",
543 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->decl)));
544 if (DECL_ONE_ONLY (node->decl))
545 fprintf (f, " one_only");
546 if (DECL_SECTION_NAME (node->decl))
547 fprintf (f, " section_name:%s",
548 TREE_STRING_POINTER (DECL_SECTION_NAME (node->decl)));
549 if (DECL_VISIBILITY_SPECIFIED (node->decl))
550 fprintf (f, " visibility_specified");
551 if (DECL_VISIBILITY (node->decl))
552 fprintf (f, " visibility:%s",
553 visibility_types [DECL_VISIBILITY (node->decl)]);
554 if (DECL_VIRTUAL_P (node->decl))
555 fprintf (f, " virtual");
556 if (DECL_ARTIFICIAL (node->decl))
557 fprintf (f, " artificial");
558 if (TREE_CODE (node->decl) == FUNCTION_DECL)
560 if (DECL_STATIC_CONSTRUCTOR (node->decl))
561 fprintf (f, " constructor");
562 if (DECL_STATIC_DESTRUCTOR (node->decl))
563 fprintf (f, " destructor");
565 fprintf (f, "\n");
567 if (node->same_comdat_group)
568 fprintf (f, " Same comdat group as: %s/%i\n",
569 symtab_node_asm_name (node->same_comdat_group),
570 node->same_comdat_group->order);
571 if (node->next_sharing_asm_name)
572 fprintf (f, " next sharing asm name: %i\n",
573 node->next_sharing_asm_name->order);
574 if (node->previous_sharing_asm_name)
575 fprintf (f, " previous sharing asm name: %i\n",
576 node->previous_sharing_asm_name->order);
578 if (node->address_taken)
579 fprintf (f, " Address is taken.\n");
580 if (node->aux)
582 fprintf (f, " Aux:");
583 dump_addr (f, " @", (void *)node->aux);
586 fprintf (f, " References: ");
587 ipa_dump_references (f, &node->ref_list);
588 fprintf (f, " Referring: ");
589 ipa_dump_referring (f, &node->ref_list);
590 if (node->lto_file_data)
591 fprintf (f, " Read from file: %s\n",
592 node->lto_file_data->file_name);
595 /* Dump symtab node. */
597 void
598 dump_symtab_node (FILE *f, symtab_node *node)
600 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
601 dump_cgraph_node (f, cnode);
602 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
603 dump_varpool_node (f, vnode);
606 /* Dump symbol table. */
608 void
609 dump_symtab (FILE *f)
611 symtab_node *node;
612 fprintf (f, "Symbol table:\n\n");
613 FOR_EACH_SYMBOL (node)
614 dump_symtab_node (f, node);
617 /* Dump symtab node NODE to stderr. */
619 DEBUG_FUNCTION void
620 debug_symtab_node (symtab_node *node)
622 dump_symtab_node (stderr, node);
625 /* Dump symbol table to stderr. */
627 DEBUG_FUNCTION void
628 debug_symtab (void)
630 dump_symtab (stderr);
633 /* Verify common part of symtab nodes. */
635 DEBUG_FUNCTION bool
636 verify_symtab_base (symtab_node *node)
638 bool error_found = false;
639 symtab_node *hashed_node;
641 if (is_a <cgraph_node> (node))
643 if (TREE_CODE (node->decl) != FUNCTION_DECL)
645 error ("function symbol is not function");
646 error_found = true;
649 else if (is_a <varpool_node> (node))
651 if (TREE_CODE (node->decl) != VAR_DECL)
653 error ("variable symbol is not variable");
654 error_found = true;
657 else
659 error ("node has unknown type");
660 error_found = true;
663 if (cgraph_state != CGRAPH_LTO_STREAMING)
665 hashed_node = symtab_get_node (node->decl);
666 if (!hashed_node)
668 error ("node not found in symtab decl hashtable");
669 error_found = true;
671 if (hashed_node != node
672 && (!is_a <cgraph_node> (node)
673 || !dyn_cast <cgraph_node> (node)->clone_of
674 || dyn_cast <cgraph_node> (node)->clone_of->decl
675 != node->decl))
677 error ("node differs from symtab decl hashtable");
678 error_found = true;
681 if (assembler_name_hash)
683 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
684 if (hashed_node && hashed_node->previous_sharing_asm_name)
686 error ("assembler name hash list corrupted");
687 error_found = true;
689 while (hashed_node)
691 if (hashed_node == node)
692 break;
693 hashed_node = hashed_node->next_sharing_asm_name;
695 if (!hashed_node
696 && !(is_a <varpool_node> (node)
697 || DECL_HARD_REGISTER (node->decl)))
699 error ("node not found in symtab assembler name hash");
700 error_found = true;
703 if (node->previous_sharing_asm_name
704 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
706 error ("double linked list of assembler names corrupted");
707 error_found = true;
709 if (node->analyzed && !node->definition)
711 error ("node is analyzed byt it is not a definition");
712 error_found = true;
714 if (node->cpp_implicit_alias && !node->alias)
716 error ("node is alias but not implicit alias");
717 error_found = true;
719 if (node->alias && !node->definition
720 && !node->weakref)
722 error ("node is alias but not definition");
723 error_found = true;
725 if (node->weakref && !node->alias)
727 error ("node is weakref but not an alias");
728 error_found = true;
730 if (node->same_comdat_group)
732 symtab_node *n = node->same_comdat_group;
734 if (!DECL_ONE_ONLY (n->decl))
736 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
737 error_found = true;
739 if (n->type != node->type)
741 error ("mixing different types of symbol in same comdat groups is not supported");
742 error_found = true;
744 if (n == node)
746 error ("node is alone in a comdat group");
747 error_found = true;
751 if (!n->same_comdat_group)
753 error ("same_comdat_group is not a circular list");
754 error_found = true;
755 break;
757 n = n->same_comdat_group;
759 while (n != node);
761 return error_found;
764 /* Verify consistency of NODE. */
766 DEBUG_FUNCTION void
767 verify_symtab_node (symtab_node *node)
769 if (seen_error ())
770 return;
772 timevar_push (TV_CGRAPH_VERIFY);
773 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
774 verify_cgraph_node (cnode);
775 else
776 if (verify_symtab_base (node))
778 dump_symtab_node (stderr, node);
779 internal_error ("verify_symtab_node failed");
781 timevar_pop (TV_CGRAPH_VERIFY);
784 /* Verify symbol table for internal consistency. */
786 DEBUG_FUNCTION void
787 verify_symtab (void)
789 symtab_node *node;
790 FOR_EACH_SYMBOL (node)
791 verify_symtab_node (node);
794 /* Return true when RESOLUTION indicate that linker will use
795 the symbol from non-LTO object files. */
797 bool
798 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
800 return (resolution == LDPR_PREVAILING_DEF
801 || resolution == LDPR_PREEMPTED_REG
802 || resolution == LDPR_RESOLVED_EXEC
803 || resolution == LDPR_RESOLVED_DYN);
806 /* Return true when NODE is known to be used from other (non-LTO) object file.
807 Known only when doing LTO via linker plugin. */
809 bool
810 symtab_used_from_object_file_p (symtab_node *node)
812 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
813 return false;
814 if (resolution_used_from_other_file_p (node->resolution))
815 return true;
816 return false;
819 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
820 but other code such as notice_global_symbol generates rtl. */
822 void
823 symtab_make_decl_local (tree decl)
825 rtx rtl, symbol;
827 if (TREE_CODE (decl) == VAR_DECL)
828 DECL_COMMON (decl) = 0;
829 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
831 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
833 DECL_SECTION_NAME (decl) = 0;
834 DECL_COMDAT (decl) = 0;
836 DECL_COMDAT_GROUP (decl) = 0;
837 DECL_WEAK (decl) = 0;
838 DECL_EXTERNAL (decl) = 0;
839 DECL_VISIBILITY_SPECIFIED (decl) = 0;
840 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
841 TREE_PUBLIC (decl) = 0;
842 if (!DECL_RTL_SET_P (decl))
843 return;
845 /* Update rtl flags. */
846 make_decl_rtl (decl);
848 rtl = DECL_RTL (decl);
849 if (!MEM_P (rtl))
850 return;
852 symbol = XEXP (rtl, 0);
853 if (GET_CODE (symbol) != SYMBOL_REF)
854 return;
856 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
859 /* Return availability of NODE. */
861 enum availability
862 symtab_node_availability (symtab_node *node)
864 if (is_a <cgraph_node> (node))
865 return cgraph_function_body_availability (cgraph (node));
866 else
867 return cgraph_variable_initializer_availability (varpool (node));
870 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
871 If NODE is not an alias, return NODE.
872 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
874 symtab_node *
875 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
877 bool weakref_p = false;
879 if (!node->alias)
881 if (availability)
882 *availability = symtab_node_availability (node);
883 return node;
886 /* To determine visibility of the target, we follow ELF semantic of aliases.
887 Here alias is an alternative assembler name of a given definition. Its
888 availability prevails the availability of its target (i.e. static alias of
889 weak definition is available.
891 Weakref is a different animal (and not part of ELF per se). It is just
892 alternative name of a given symbol used within one complation unit
893 and is translated prior hitting the object file. It inherits the
894 visibility of its target (i.e. weakref of non-overwritable definition
895 is non-overwritable, while weakref of weak definition is weak).
897 If we ever get into supporting targets with different semantics, a target
898 hook will be needed here. */
900 if (availability)
902 weakref_p = node->weakref;
903 if (!weakref_p)
904 *availability = symtab_node_availability (node);
905 else
906 *availability = AVAIL_LOCAL;
908 while (node)
910 if (node->alias && node->analyzed)
911 node = symtab_alias_target (node);
912 else
914 if (!availability)
916 else if (node->analyzed)
918 if (weakref_p)
920 enum availability a = symtab_node_availability (node);
921 if (a < *availability)
922 *availability = a;
925 else
926 *availability = AVAIL_NOT_AVAILABLE;
927 return node;
929 if (node && availability && weakref_p)
931 enum availability a = symtab_node_availability (node);
932 if (a < *availability)
933 *availability = a;
934 weakref_p = node->weakref;
937 if (availability)
938 *availability = AVAIL_NOT_AVAILABLE;
939 return NULL;
942 /* C++ FE sometimes change linkage flags after producing same body aliases.
944 FIXME: C++ produce implicit aliases for virtual functions and vtables that
945 are obviously equivalent. The way it is doing so is however somewhat
946 kludgy and interferes with the visibility code. As a result we need to
947 copy the visibility from the target to get things right. */
949 void
950 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
952 if (is_a <cgraph_node> (node))
954 DECL_DECLARED_INLINE_P (node->decl)
955 = DECL_DECLARED_INLINE_P (target->decl);
956 DECL_DISREGARD_INLINE_LIMITS (node->decl)
957 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
959 /* FIXME: It is not really clear why those flags should not be copied for
960 functions, too. */
961 else
963 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
964 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
965 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
967 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
968 if (TREE_PUBLIC (node->decl))
970 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
971 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
972 DECL_COMDAT_GROUP (node->decl)
973 = DECL_COMDAT_GROUP (target->decl);
974 if (DECL_ONE_ONLY (target->decl)
975 && !node->same_comdat_group)
976 symtab_add_to_same_comdat_group (node, target);
978 node->externally_visible = target->externally_visible;
981 /* Add reference recording that NODE is alias of TARGET.
982 The function can fail in the case of aliasing cycles; in this case
983 it returns false. */
985 bool
986 symtab_resolve_alias (symtab_node *node, symtab_node *target)
988 symtab_node *n;
990 gcc_assert (!node->analyzed
991 && !vec_safe_length (node->ref_list.references));
993 /* Never let cycles to creep into the symbol table alias references;
994 those will make alias walkers to be infinite. */
995 for (n = target; n && n->alias;
996 n = n->analyzed ? symtab_alias_target (n) : NULL)
997 if (n == node)
999 if (is_a <cgraph_node> (node))
1000 error ("function %q+D part of alias cycle", node->decl);
1001 else if (is_a <varpool_node> (node))
1002 error ("variable %q+D part of alias cycle", node->decl);
1003 else
1004 gcc_unreachable ();
1005 node->alias = false;
1006 return false;
1009 /* "analyze" the node - i.e. mark the reference. */
1010 node->definition = true;
1011 node->alias = true;
1012 node->analyzed = true;
1013 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1015 /* Alias targets become reudndant after alias is resolved into an reference.
1016 We do not want to keep it around or we would have to mind updating them
1017 when renaming symbols. */
1018 node->alias_target = NULL;
1020 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1021 fixup_same_cpp_alias_visibility (node, target);
1023 /* If alias has address taken, so does the target. */
1024 if (node->address_taken)
1025 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1026 return true;
1029 /* Call calback on NODE and aliases associated to NODE.
1030 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1031 skipped. */
1033 bool
1034 symtab_for_node_and_aliases (symtab_node *node,
1035 bool (*callback) (symtab_node *, void *),
1036 void *data,
1037 bool include_overwritable)
1039 int i;
1040 struct ipa_ref *ref;
1042 if (callback (node, data))
1043 return true;
1044 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1045 if (ref->use == IPA_REF_ALIAS)
1047 symtab_node *alias = ref->referring;
1048 if (include_overwritable
1049 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1050 if (symtab_for_node_and_aliases (alias, callback, data,
1051 include_overwritable))
1052 return true;
1054 return false;
1057 /* Worker searching nonoverwritable alias. */
1059 static bool
1060 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1062 if (decl_binds_to_current_def_p (node->decl))
1064 *(symtab_node **)data = node;
1065 return true;
1067 return false;
1070 /* If NODE can not be overwriten by static or dynamic linker to point to different
1071 definition, return NODE. Otherwise look for alias with such property and if
1072 none exists, introduce new one. */
1074 symtab_node *
1075 symtab_nonoverwritable_alias (symtab_node *node)
1077 tree new_decl;
1078 symtab_node *new_node = NULL;
1080 /* First try to look up existing alias or base object
1081 (if that is already non-overwritable). */
1082 node = symtab_alias_ultimate_target (node, NULL);
1083 gcc_assert (!node->alias && !node->weakref);
1084 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1085 (void *)&new_node, true);
1086 if (new_node)
1087 return new_node;
1088 #ifndef ASM_OUTPUT_DEF
1089 /* If aliases aren't supported by the assembler, fail. */
1090 return NULL;
1091 #endif
1093 /* Otherwise create a new one. */
1094 new_decl = copy_node (node->decl);
1095 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1096 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1097 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1098 DECL_INITIAL (new_decl) = NULL;
1099 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1100 SET_DECL_RTL (new_decl, NULL);
1102 /* Update the properties. */
1103 DECL_EXTERNAL (new_decl) = 0;
1104 if (DECL_ONE_ONLY (node->decl))
1105 DECL_SECTION_NAME (new_decl) = NULL;
1106 DECL_COMDAT_GROUP (new_decl) = 0;
1107 TREE_PUBLIC (new_decl) = 0;
1108 DECL_COMDAT (new_decl) = 0;
1109 DECL_WEAK (new_decl) = 0;
1110 DECL_VIRTUAL_P (new_decl) = 0;
1111 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1113 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1114 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1115 new_node = cgraph_create_function_alias
1116 (new_decl, node->decl);
1118 else
1119 new_node = varpool_create_variable_alias (new_decl,
1120 node->decl);
1121 symtab_resolve_alias (new_node, node);
1122 gcc_assert (decl_binds_to_current_def_p (new_decl));
1123 return new_node;
1126 /* Return true if A and B represents semantically equivalent symbols. */
1128 bool
1129 symtab_semantically_equivalent_p (symtab_node *a,
1130 symtab_node *b)
1132 enum availability avail;
1133 symtab_node *ba;
1134 symtab_node *bb;
1136 /* Equivalent functions are equivalent. */
1137 if (a->decl == b->decl)
1138 return true;
1140 /* If symbol is not overwritable by different implementation,
1141 walk to the base object it defines. */
1142 ba = symtab_alias_ultimate_target (a, &avail);
1143 if (avail >= AVAIL_AVAILABLE)
1145 if (ba == b)
1146 return true;
1148 else
1149 ba = a;
1150 bb = symtab_alias_ultimate_target (b, &avail);
1151 if (avail >= AVAIL_AVAILABLE)
1153 if (a == bb)
1154 return true;
1156 else
1157 bb = b;
1158 return bb == ba;
1160 #include "gt-symtab.h"