* lib/target-supports.exp (check_weak_available): Return true for AIX.
[official-gcc.git] / gcc / symtab.c
blob253ba981844310eac29d547ee305885afbf6ddc4
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 "tree-inline.h"
27 #include "langhooks.h"
28 #include "hashtab.h"
29 #include "ggc.h"
30 #include "cgraph.h"
31 #include "diagnostic.h"
32 #include "timevar.h"
33 #include "lto-streamer.h"
34 #include "rtl.h"
36 const char * const ld_plugin_symbol_resolution_names[]=
38 "",
39 "undef",
40 "prevailing_def",
41 "prevailing_def_ironly",
42 "preempted_reg",
43 "preempted_ir",
44 "resolved_ir",
45 "resolved_exec",
46 "resolved_dyn",
47 "prevailing_def_ironly_exp"
50 /* Hash table used to convert declarations into nodes. */
51 static GTY((param_is (union symtab_node_def))) htab_t symtab_hash;
52 /* Hash table used to convert assembler names into nodes. */
53 static GTY((param_is (union symtab_node_def))) htab_t assembler_name_hash;
55 /* Linked list of symbol table nodes. */
56 symtab_node symtab_nodes;
58 /* The order index of the next symtab node to be created. This is
59 used so that we can sort the cgraph nodes in order by when we saw
60 them, to support -fno-toplevel-reorder. */
61 int symtab_order;
63 /* Returns a hash code for P. */
65 static hashval_t
66 hash_node (const void *p)
68 const_symtab_node n = (const_symtab_node ) p;
69 return (hashval_t) DECL_UID (n->symbol.decl);
73 /* Returns nonzero if P1 and P2 are equal. */
75 static int
76 eq_node (const void *p1, const void *p2)
78 const_symtab_node n1 = (const_symtab_node) p1;
79 const_symtab_node n2 = (const_symtab_node) p2;
80 return DECL_UID (n1->symbol.decl) == DECL_UID (n2->symbol.decl);
83 /* Returns a hash code for P. */
85 static hashval_t
86 hash_node_by_assembler_name (const void *p)
88 const_symtab_node n = (const_symtab_node) p;
89 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->symbol.decl));
92 /* Returns nonzero if P1 and P2 are equal. */
94 static int
95 eq_assembler_name (const void *p1, const void *p2)
97 const_symtab_node n1 = (const_symtab_node) p1;
98 const_tree name = (const_tree)p2;
99 return (decl_assembler_name_equal (n1->symbol.decl, name));
102 /* Insert NODE to assembler name hash. */
104 static void
105 insert_to_assembler_name_hash (symtab_node node, bool with_clones)
107 if (is_a <varpool_node> (node) && DECL_HARD_REGISTER (node->symbol.decl))
108 return;
109 gcc_checking_assert (!node->symbol.previous_sharing_asm_name
110 && !node->symbol.next_sharing_asm_name);
111 if (assembler_name_hash)
113 void **aslot;
114 struct cgraph_node *cnode;
115 tree decl = node->symbol.decl;
117 tree name = DECL_ASSEMBLER_NAME (node->symbol.decl);
119 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
120 decl_assembler_name_hash (name),
121 INSERT);
122 gcc_assert (*aslot != node);
123 node->symbol.next_sharing_asm_name = (symtab_node)*aslot;
124 if (*aslot != NULL)
125 ((symtab_node)*aslot)->symbol.previous_sharing_asm_name = node;
126 *aslot = node;
128 /* Update also possible inline clones sharing a decl. */
129 cnode = dyn_cast <cgraph_node> (node);
130 if (cnode && cnode->clones && with_clones)
131 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
132 if (cnode->symbol.decl == decl)
133 insert_to_assembler_name_hash ((symtab_node) cnode, true);
138 /* Remove NODE from assembler name hash. */
140 static void
141 unlink_from_assembler_name_hash (symtab_node node, bool with_clones)
143 if (assembler_name_hash)
145 struct cgraph_node *cnode;
146 tree decl = node->symbol.decl;
148 if (node->symbol.next_sharing_asm_name)
149 node->symbol.next_sharing_asm_name->symbol.previous_sharing_asm_name
150 = node->symbol.previous_sharing_asm_name;
151 if (node->symbol.previous_sharing_asm_name)
153 node->symbol.previous_sharing_asm_name->symbol.next_sharing_asm_name
154 = node->symbol.next_sharing_asm_name;
156 else
158 tree name = DECL_ASSEMBLER_NAME (node->symbol.decl);
159 void **slot;
160 slot = htab_find_slot_with_hash (assembler_name_hash, name,
161 decl_assembler_name_hash (name),
162 NO_INSERT);
163 gcc_assert (*slot == node);
164 if (!node->symbol.next_sharing_asm_name)
165 htab_clear_slot (assembler_name_hash, slot);
166 else
167 *slot = node->symbol.next_sharing_asm_name;
169 node->symbol.next_sharing_asm_name = NULL;
170 node->symbol.previous_sharing_asm_name = NULL;
172 /* Update also possible inline clones sharing a decl. */
173 cnode = dyn_cast <cgraph_node> (node);
174 if (cnode && cnode->clones && with_clones)
175 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
176 if (cnode->symbol.decl == decl)
177 unlink_from_assembler_name_hash ((symtab_node) cnode, true);
181 /* Arrange node to be first in its entry of assembler_name_hash. */
183 void
184 symtab_prevail_in_asm_name_hash (symtab_node node)
186 unlink_from_assembler_name_hash (node, false);
187 insert_to_assembler_name_hash (node, false);
191 /* Add node into symbol table. This function is not used directly, but via
192 cgraph/varpool node creation routines. */
194 void
195 symtab_register_node (symtab_node node)
197 struct symtab_node_base key;
198 symtab_node *slot;
200 node->symbol.next = symtab_nodes;
201 node->symbol.previous = NULL;
202 if (symtab_nodes)
203 symtab_nodes->symbol.previous = node;
204 symtab_nodes = node;
206 if (!symtab_hash)
207 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
208 key.decl = node->symbol.decl;
209 slot = (symtab_node *) htab_find_slot (symtab_hash, &key, INSERT);
210 if (*slot == NULL)
211 *slot = node;
213 ipa_empty_ref_list (&node->symbol.ref_list);
215 node->symbol.order = symtab_order++;
217 /* Be sure to do this last; C++ FE might create new nodes via
218 DECL_ASSEMBLER_NAME langhook! */
219 insert_to_assembler_name_hash (node, false);
222 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
223 of inline clones. */
225 void
226 symtab_insert_node_to_hashtable (symtab_node node)
228 struct symtab_node_base key;
229 symtab_node *slot;
231 if (!symtab_hash)
232 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
233 key.decl = node->symbol.decl;
234 slot = (symtab_node *) htab_find_slot (symtab_hash, &key, INSERT);
235 *slot = node;
238 /* Remove node from symbol table. This function is not used directly, but via
239 cgraph/varpool node removal routines. */
241 void
242 symtab_unregister_node (symtab_node node)
244 void **slot;
245 ipa_remove_all_references (&node->symbol.ref_list);
246 ipa_remove_all_referring (&node->symbol.ref_list);
248 if (node->symbol.same_comdat_group)
250 symtab_node prev;
251 for (prev = node->symbol.same_comdat_group;
252 prev->symbol.same_comdat_group != node;
253 prev = prev->symbol.same_comdat_group)
255 if (node->symbol.same_comdat_group == prev)
256 prev->symbol.same_comdat_group = NULL;
257 else
258 prev->symbol.same_comdat_group = node->symbol.same_comdat_group;
259 node->symbol.same_comdat_group = NULL;
262 if (node->symbol.previous)
263 node->symbol.previous->symbol.next = node->symbol.next;
264 else
265 symtab_nodes = node->symbol.next;
266 if (node->symbol.next)
267 node->symbol.next->symbol.previous = node->symbol.previous;
268 node->symbol.next = NULL;
269 node->symbol.previous = NULL;
271 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
273 /* During LTO symtab merging we temporarily corrupt decl to symtab node
274 hash. */
275 gcc_assert ((slot && *slot) || in_lto_p);
276 if (slot && *slot && *slot == node)
278 symtab_node replacement_node = NULL;
279 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
280 replacement_node = (symtab_node)cgraph_find_replacement_node (cnode);
281 if (!replacement_node)
282 htab_clear_slot (symtab_hash, slot);
283 else
284 *slot = replacement_node;
286 if (!is_a <varpool_node> (node) || !DECL_HARD_REGISTER (node->symbol.decl))
287 unlink_from_assembler_name_hash (node, false);
290 /* Return symbol table node associated with DECL, if any,
291 and NULL otherwise. */
293 symtab_node
294 symtab_get_node (const_tree decl)
296 symtab_node *slot;
297 struct symtab_node_base key;
299 #ifdef ENABLE_CHECKING
300 /* Check that we are called for sane type of object - functions
301 and static or external variables. */
302 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
303 || (TREE_CODE (decl) == VAR_DECL
304 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
305 || in_lto_p)));
306 #endif
308 if (!symtab_hash)
309 return NULL;
311 key.decl = CONST_CAST2 (tree, const_tree, decl);
313 slot = (symtab_node *) htab_find_slot (symtab_hash, &key,
314 NO_INSERT);
316 if (slot)
317 return *slot;
318 return NULL;
321 /* Remove symtab NODE from the symbol table. */
323 void
324 symtab_remove_node (symtab_node node)
326 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
327 cgraph_remove_node (cnode);
328 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
329 varpool_remove_node (vnode);
332 /* Initalize asm name hash unless. */
334 void
335 symtab_initialize_asm_name_hash (void)
337 symtab_node node;
338 if (!assembler_name_hash)
340 assembler_name_hash =
341 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
342 NULL);
343 FOR_EACH_SYMBOL (node)
344 insert_to_assembler_name_hash (node, false);
348 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
349 Return NULL if there's no such node. */
351 symtab_node
352 symtab_node_for_asm (const_tree asmname)
354 symtab_node node;
355 void **slot;
357 symtab_initialize_asm_name_hash ();
358 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
359 decl_assembler_name_hash (asmname),
360 NO_INSERT);
362 if (slot)
364 node = (symtab_node) *slot;
365 return node;
367 return NULL;
370 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
372 void
373 change_decl_assembler_name (tree decl, tree name)
375 symtab_node node = NULL;
377 /* We can have user ASM names on things, like global register variables, that
378 are not in the symbol table. */
379 if ((TREE_CODE (decl) == VAR_DECL
380 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
381 || TREE_CODE (decl) == FUNCTION_DECL)
382 node = symtab_get_node (decl);
383 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
385 SET_DECL_ASSEMBLER_NAME (decl, name);
386 if (node)
387 insert_to_assembler_name_hash (node, true);
389 else
391 if (name == DECL_ASSEMBLER_NAME (decl))
392 return;
394 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
395 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
396 : NULL);
397 if (node)
398 unlink_from_assembler_name_hash (node, true);
399 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
400 && DECL_RTL_SET_P (decl))
401 warning (0, "%D renamed after being referenced in assembly", decl);
403 SET_DECL_ASSEMBLER_NAME (decl, name);
404 if (alias)
406 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
407 TREE_CHAIN (DECL_ASSEMBLER_NAME (name)) = alias;
409 if (node)
410 insert_to_assembler_name_hash (node, true);
414 /* Add NEW_ to the same comdat group that OLD is in. */
416 void
417 symtab_add_to_same_comdat_group (symtab_node new_node,
418 symtab_node old_node)
420 gcc_assert (DECL_ONE_ONLY (old_node->symbol.decl));
421 gcc_assert (!new_node->symbol.same_comdat_group);
422 gcc_assert (new_node != old_node);
424 DECL_COMDAT_GROUP (new_node->symbol.decl) = DECL_COMDAT_GROUP (old_node->symbol.decl);
425 new_node->symbol.same_comdat_group = old_node;
426 if (!old_node->symbol.same_comdat_group)
427 old_node->symbol.same_comdat_group = new_node;
428 else
430 symtab_node n;
431 for (n = old_node->symbol.same_comdat_group;
432 n->symbol.same_comdat_group != old_node;
433 n = n->symbol.same_comdat_group)
435 n->symbol.same_comdat_group = new_node;
439 /* Dissolve the same_comdat_group list in which NODE resides. */
441 void
442 symtab_dissolve_same_comdat_group_list (symtab_node node)
444 symtab_node n = node, next;
446 if (!node->symbol.same_comdat_group)
447 return;
450 next = n->symbol.same_comdat_group;
451 n->symbol.same_comdat_group = NULL;
452 n = next;
454 while (n != node);
457 /* Return printable assembler name of NODE.
458 This function is used only for debugging. When assembler name
459 is unknown go with identifier name. */
461 const char *
462 symtab_node_asm_name (symtab_node node)
464 if (!DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
465 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
466 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl));
469 /* Return printable identifier name. */
471 const char *
472 symtab_node_name (symtab_node node)
474 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
477 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
479 /* Dump base fields of symtab nodes. Not to be used directly. */
481 void
482 dump_symtab_base (FILE *f, symtab_node node)
484 static const char * const visibility_types[] = {
485 "default", "protected", "hidden", "internal"
488 fprintf (f, "%s/%i (%s)",
489 symtab_node_asm_name (node),
490 node->symbol.order,
491 symtab_node_name (node));
492 dump_addr (f, " @", (void *)node);
493 fprintf (f, "\n Type: %s", symtab_type_names[node->symbol.type]);
495 if (node->symbol.definition)
496 fprintf (f, " definition");
497 if (node->symbol.analyzed)
498 fprintf (f, " analyzed");
499 if (node->symbol.alias)
500 fprintf (f, " alias");
501 if (node->symbol.weakref)
502 fprintf (f, " weakref");
503 if (node->symbol.cpp_implicit_alias)
504 fprintf (f, " cpp_implicit_alias");
505 if (node->symbol.alias_target)
506 fprintf (f, " target:%s",
507 DECL_P (node->symbol.alias_target)
508 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
509 (node->symbol.alias_target))
510 : IDENTIFIER_POINTER (node->symbol.alias_target));
511 fprintf (f, "\n Visibility:");
512 if (node->symbol.in_other_partition)
513 fprintf (f, " in_other_partition");
514 if (node->symbol.used_from_other_partition)
515 fprintf (f, " used_from_other_partition");
516 if (node->symbol.force_output)
517 fprintf (f, " force_output");
518 if (node->symbol.forced_by_abi)
519 fprintf (f, " forced_by_abi");
520 if (node->symbol.externally_visible)
521 fprintf (f, " externally_visible");
522 if (node->symbol.resolution != LDPR_UNKNOWN)
523 fprintf (f, " %s",
524 ld_plugin_symbol_resolution_names[(int)node->symbol.resolution]);
525 if (TREE_ASM_WRITTEN (node->symbol.decl))
526 fprintf (f, " asm_written");
527 if (DECL_EXTERNAL (node->symbol.decl))
528 fprintf (f, " external");
529 if (TREE_PUBLIC (node->symbol.decl))
530 fprintf (f, " public");
531 if (DECL_COMMON (node->symbol.decl))
532 fprintf (f, " common");
533 if (DECL_WEAK (node->symbol.decl))
534 fprintf (f, " weak");
535 if (DECL_DLLIMPORT_P (node->symbol.decl))
536 fprintf (f, " dll_import");
537 if (DECL_COMDAT (node->symbol.decl))
538 fprintf (f, " comdat");
539 if (DECL_COMDAT_GROUP (node->symbol.decl))
540 fprintf (f, " comdat_group:%s",
541 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->symbol.decl)));
542 if (DECL_ONE_ONLY (node->symbol.decl))
543 fprintf (f, " one_only");
544 if (DECL_SECTION_NAME (node->symbol.decl))
545 fprintf (f, " section_name:%s",
546 TREE_STRING_POINTER (DECL_SECTION_NAME (node->symbol.decl)));
547 if (DECL_VISIBILITY_SPECIFIED (node->symbol.decl))
548 fprintf (f, " visibility_specified");
549 if (DECL_VISIBILITY (node->symbol.decl))
550 fprintf (f, " visibility:%s",
551 visibility_types [DECL_VISIBILITY (node->symbol.decl)]);
552 if (DECL_VIRTUAL_P (node->symbol.decl))
553 fprintf (f, " virtual");
554 if (DECL_ARTIFICIAL (node->symbol.decl))
555 fprintf (f, " artificial");
556 if (TREE_CODE (node->symbol.decl) == FUNCTION_DECL)
558 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
559 fprintf (f, " constructor");
560 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
561 fprintf (f, " destructor");
563 fprintf (f, "\n");
565 if (node->symbol.same_comdat_group)
566 fprintf (f, " Same comdat group as: %s/%i\n",
567 symtab_node_asm_name (node->symbol.same_comdat_group),
568 node->symbol.same_comdat_group->symbol.order);
569 if (node->symbol.next_sharing_asm_name)
570 fprintf (f, " next sharing asm name: %i\n",
571 node->symbol.next_sharing_asm_name->symbol.order);
572 if (node->symbol.previous_sharing_asm_name)
573 fprintf (f, " previous sharing asm name: %i\n",
574 node->symbol.previous_sharing_asm_name->symbol.order);
576 if (node->symbol.address_taken)
577 fprintf (f, " Address is taken.\n");
578 if (node->symbol.aux)
580 fprintf (f, " Aux:");
581 dump_addr (f, " @", (void *)node->symbol.aux);
584 fprintf (f, " References: ");
585 ipa_dump_references (f, &node->symbol.ref_list);
586 fprintf (f, " Referring: ");
587 ipa_dump_referring (f, &node->symbol.ref_list);
588 if (node->symbol.lto_file_data)
589 fprintf (f, " Read from file: %s\n",
590 node->symbol.lto_file_data->file_name);
593 /* Dump symtab node. */
595 void
596 dump_symtab_node (FILE *f, symtab_node node)
598 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
599 dump_cgraph_node (f, cnode);
600 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
601 dump_varpool_node (f, vnode);
604 /* Dump symbol table. */
606 void
607 dump_symtab (FILE *f)
609 symtab_node node;
610 fprintf (f, "Symbol table:\n\n");
611 FOR_EACH_SYMBOL (node)
612 dump_symtab_node (f, node);
615 /* Dump symtab node NODE to stderr. */
617 DEBUG_FUNCTION void
618 debug_symtab_node (symtab_node node)
620 dump_symtab_node (stderr, node);
623 /* Dump symbol table to stderr. */
625 DEBUG_FUNCTION void
626 debug_symtab (void)
628 dump_symtab (stderr);
631 /* Verify common part of symtab nodes. */
633 DEBUG_FUNCTION bool
634 verify_symtab_base (symtab_node node)
636 bool error_found = false;
637 symtab_node hashed_node;
639 if (is_a <cgraph_node> (node))
641 if (TREE_CODE (node->symbol.decl) != FUNCTION_DECL)
643 error ("function symbol is not function");
644 error_found = true;
647 else if (is_a <varpool_node> (node))
649 if (TREE_CODE (node->symbol.decl) != VAR_DECL)
651 error ("variable symbol is not variable");
652 error_found = true;
655 else
657 error ("node has unknown type");
658 error_found = true;
661 if (cgraph_state != CGRAPH_LTO_STREAMING)
663 hashed_node = symtab_get_node (node->symbol.decl);
664 if (!hashed_node)
666 error ("node not found in symtab decl hashtable");
667 error_found = true;
669 if (hashed_node != node
670 && (!is_a <cgraph_node> (node)
671 || !dyn_cast <cgraph_node> (node)->clone_of
672 || dyn_cast <cgraph_node> (node)->clone_of->symbol.decl
673 != node->symbol.decl))
675 error ("node differs from symtab decl hashtable");
676 error_found = true;
679 if (assembler_name_hash)
681 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->symbol.decl));
682 if (hashed_node && hashed_node->symbol.previous_sharing_asm_name)
684 error ("assembler name hash list corrupted");
685 error_found = true;
687 while (hashed_node)
689 if (hashed_node == node)
690 break;
691 hashed_node = hashed_node->symbol.next_sharing_asm_name;
693 if (!hashed_node
694 && !(is_a <varpool_node> (node)
695 || DECL_HARD_REGISTER (node->symbol.decl)))
697 error ("node not found in symtab assembler name hash");
698 error_found = true;
701 if (node->symbol.previous_sharing_asm_name
702 && node->symbol.previous_sharing_asm_name->symbol.next_sharing_asm_name != node)
704 error ("double linked list of assembler names corrupted");
705 error_found = true;
707 if (node->symbol.analyzed && !node->symbol.definition)
709 error ("node is analyzed byt it is not a definition");
710 error_found = true;
712 if (node->symbol.cpp_implicit_alias && !node->symbol.alias)
714 error ("node is alias but not implicit alias");
715 error_found = true;
717 if (node->symbol.alias && !node->symbol.definition
718 && !node->symbol.weakref)
720 error ("node is alias but not definition");
721 error_found = true;
723 if (node->symbol.weakref && !node->symbol.alias)
725 error ("node is weakref but not an alias");
726 error_found = true;
728 if (node->symbol.same_comdat_group)
730 symtab_node n = node->symbol.same_comdat_group;
732 if (!DECL_ONE_ONLY (n->symbol.decl))
734 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
735 error_found = true;
737 if (n->symbol.type != node->symbol.type)
739 error ("mixing different types of symbol in same comdat groups is not supported");
740 error_found = true;
742 if (n == node)
744 error ("node is alone in a comdat group");
745 error_found = true;
749 if (!n->symbol.same_comdat_group)
751 error ("same_comdat_group is not a circular list");
752 error_found = true;
753 break;
755 n = n->symbol.same_comdat_group;
757 while (n != node);
759 return error_found;
762 /* Verify consistency of NODE. */
764 DEBUG_FUNCTION void
765 verify_symtab_node (symtab_node node)
767 if (seen_error ())
768 return;
770 timevar_push (TV_CGRAPH_VERIFY);
771 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
772 verify_cgraph_node (cnode);
773 else
774 if (verify_symtab_base (node))
776 dump_symtab_node (stderr, node);
777 internal_error ("verify_symtab_node failed");
779 timevar_pop (TV_CGRAPH_VERIFY);
782 /* Verify symbol table for internal consistency. */
784 DEBUG_FUNCTION void
785 verify_symtab (void)
787 symtab_node node;
788 FOR_EACH_SYMBOL (node)
789 verify_symtab_node (node);
792 /* Return true when RESOLUTION indicate that linker will use
793 the symbol from non-LTO object files. */
795 bool
796 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
798 return (resolution == LDPR_PREVAILING_DEF
799 || resolution == LDPR_PREEMPTED_REG
800 || resolution == LDPR_RESOLVED_EXEC
801 || resolution == LDPR_RESOLVED_DYN);
804 /* Return true when NODE is known to be used from other (non-LTO) object file.
805 Known only when doing LTO via linker plugin. */
807 bool
808 symtab_used_from_object_file_p (symtab_node node)
810 if (!TREE_PUBLIC (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
811 return false;
812 if (resolution_used_from_other_file_p (node->symbol.resolution))
813 return true;
814 return false;
817 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
818 but other code such as notice_global_symbol generates rtl. */
820 void
821 symtab_make_decl_local (tree decl)
823 rtx rtl, symbol;
825 if (TREE_CODE (decl) == VAR_DECL)
826 DECL_COMMON (decl) = 0;
827 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
829 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
831 DECL_SECTION_NAME (decl) = 0;
832 DECL_COMDAT (decl) = 0;
834 DECL_COMDAT_GROUP (decl) = 0;
835 DECL_WEAK (decl) = 0;
836 DECL_EXTERNAL (decl) = 0;
837 DECL_VISIBILITY_SPECIFIED (decl) = 0;
838 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
839 TREE_PUBLIC (decl) = 0;
840 if (!DECL_RTL_SET_P (decl))
841 return;
843 /* Update rtl flags. */
844 make_decl_rtl (decl);
846 rtl = DECL_RTL (decl);
847 if (!MEM_P (rtl))
848 return;
850 symbol = XEXP (rtl, 0);
851 if (GET_CODE (symbol) != SYMBOL_REF)
852 return;
854 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
857 /* Return availability of NODE. */
859 enum availability
860 symtab_node_availability (symtab_node node)
862 if (is_a <cgraph_node> (node))
863 return cgraph_function_body_availability (cgraph (node));
864 else
865 return cgraph_variable_initializer_availability (varpool (node));
868 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
869 If NODE is not an alias, return NODE.
870 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
872 symtab_node
873 symtab_alias_ultimate_target (symtab_node node, enum availability *availability)
875 bool weakref_p = false;
877 if (!node->symbol.alias)
879 if (availability)
880 *availability = symtab_node_availability (node);
881 return node;
884 /* To determine visibility of the target, we follow ELF semantic of aliases.
885 Here alias is an alternative assembler name of a given definition. Its
886 availability prevails the availability of its target (i.e. static alias of
887 weak definition is available.
889 Weakref is a different animal (and not part of ELF per se). It is just
890 alternative name of a given symbol used within one complation unit
891 and is translated prior hitting the object file. It inherits the
892 visibility of its target (i.e. weakref of non-overwritable definition
893 is non-overwritable, while weakref of weak definition is weak).
895 If we ever get into supporting targets with different semantics, a target
896 hook will be needed here. */
898 if (availability)
900 weakref_p = node->symbol.weakref;
901 if (!weakref_p)
902 *availability = symtab_node_availability (node);
903 else
904 *availability = AVAIL_LOCAL;
906 while (node)
908 if (node->symbol.alias && node->symbol.analyzed)
909 node = symtab_alias_target (node);
910 else
912 if (!availability)
914 else if (node->symbol.analyzed)
916 if (weakref_p)
918 enum availability a = symtab_node_availability (node);
919 if (a < *availability)
920 *availability = a;
923 else
924 *availability = AVAIL_NOT_AVAILABLE;
925 return node;
927 if (node && availability && weakref_p)
929 enum availability a = symtab_node_availability (node);
930 if (a < *availability)
931 *availability = a;
932 weakref_p = node->symbol.weakref;
935 if (availability)
936 *availability = AVAIL_NOT_AVAILABLE;
937 return NULL;
940 /* C++ FE sometimes change linkage flags after producing same body aliases.
942 FIXME: C++ produce implicit aliases for virtual functions and vtables that
943 are obviously equivalent. The way it is doing so is however somewhat
944 kludgy and interferes with the visibility code. As a result we need to
945 copy the visibility from the target to get things right. */
947 void
948 fixup_same_cpp_alias_visibility (symtab_node node, symtab_node target)
950 if (is_a <cgraph_node> (node))
952 DECL_DECLARED_INLINE_P (node->symbol.decl)
953 = DECL_DECLARED_INLINE_P (target->symbol.decl);
954 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl)
955 = DECL_DISREGARD_INLINE_LIMITS (target->symbol.decl);
957 /* FIXME: It is not really clear why those flags should not be copied for
958 functions, too. */
959 else
961 DECL_WEAK (node->symbol.decl) = DECL_WEAK (target->symbol.decl);
962 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (target->symbol.decl);
963 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (target->symbol.decl);
965 DECL_VIRTUAL_P (node->symbol.decl) = DECL_VIRTUAL_P (target->symbol.decl);
966 if (TREE_PUBLIC (node->symbol.decl))
968 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (target->symbol.decl);
969 DECL_COMDAT (node->symbol.decl) = DECL_COMDAT (target->symbol.decl);
970 DECL_COMDAT_GROUP (node->symbol.decl)
971 = DECL_COMDAT_GROUP (target->symbol.decl);
972 if (DECL_ONE_ONLY (target->symbol.decl)
973 && !node->symbol.same_comdat_group)
974 symtab_add_to_same_comdat_group ((symtab_node)node, (symtab_node)target);
976 node->symbol.externally_visible = target->symbol.externally_visible;
979 /* Add reference recording that NODE is alias of TARGET.
980 The function can fail in the case of aliasing cycles; in this case
981 it returns false. */
983 bool
984 symtab_resolve_alias (symtab_node node, symtab_node target)
986 symtab_node n;
988 gcc_assert (!node->symbol.analyzed
989 && !vec_safe_length (node->symbol.ref_list.references));
991 /* Never let cycles to creep into the symbol table alias references;
992 those will make alias walkers to be infinite. */
993 for (n = target; n && n->symbol.alias;
994 n = n->symbol.analyzed ? symtab_alias_target (n) : NULL)
995 if (n == node)
997 if (is_a <cgraph_node> (node))
998 error ("function %q+D part of alias cycle", node->symbol.decl);
999 else if (is_a <varpool_node> (node))
1000 error ("variable %q+D part of alias cycle", node->symbol.decl);
1001 else
1002 gcc_unreachable ();
1003 node->symbol.alias = false;
1004 return false;
1007 /* "analyze" the node - i.e. mark the reference. */
1008 node->symbol.definition = true;
1009 node->symbol.alias = true;
1010 node->symbol.analyzed = true;
1011 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1013 /* Alias targets become reudndant after alias is resolved into an reference.
1014 We do not want to keep it around or we would have to mind updating them
1015 when renaming symbols. */
1016 node->symbol.alias_target = NULL;
1018 if (node->symbol.cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1019 fixup_same_cpp_alias_visibility (node, target);
1021 /* If alias has address taken, so does the target. */
1022 if (node->symbol.address_taken)
1023 symtab_alias_ultimate_target (target, NULL)->symbol.address_taken = true;
1024 return true;
1027 /* Call calback on NODE and aliases associated to NODE.
1028 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1029 skipped. */
1031 bool
1032 symtab_for_node_and_aliases (symtab_node node,
1033 bool (*callback) (symtab_node, void *),
1034 void *data,
1035 bool include_overwritable)
1037 int i;
1038 struct ipa_ref *ref;
1040 if (callback (node, data))
1041 return true;
1042 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1043 if (ref->use == IPA_REF_ALIAS)
1045 symtab_node alias = ref->referring;
1046 if (include_overwritable
1047 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1048 if (symtab_for_node_and_aliases (alias, callback, data,
1049 include_overwritable))
1050 return true;
1052 return false;
1055 /* Worker searching nonoverwritable alias. */
1057 static bool
1058 symtab_nonoverwritable_alias_1 (symtab_node node, void *data)
1060 if (decl_binds_to_current_def_p (node->symbol.decl))
1062 *(symtab_node *)data = node;
1063 return true;
1065 return false;
1068 /* If NODE can not be overwriten by static or dynamic linker to point to different
1069 definition, return NODE. Otherwise look for alias with such property and if
1070 none exists, introduce new one. */
1072 symtab_node
1073 symtab_nonoverwritable_alias (symtab_node node)
1075 tree new_decl;
1076 symtab_node new_node = NULL;
1078 /* First try to look up existing alias or base object
1079 (if that is already non-overwritable). */
1080 node = symtab_alias_ultimate_target (node, NULL);
1081 gcc_assert (!node->symbol.alias && !node->symbol.weakref);
1082 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1083 (void *)&new_node, true);
1084 if (new_node)
1085 return new_node;
1087 /* Otherwise create a new one. */
1088 new_decl = copy_node (node->symbol.decl);
1089 DECL_NAME (new_decl) = clone_function_name (node->symbol.decl, "localalias");
1090 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1091 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1092 DECL_INITIAL (new_decl) = NULL;
1093 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1094 SET_DECL_RTL (new_decl, NULL);
1096 /* Update the properties. */
1097 DECL_EXTERNAL (new_decl) = 0;
1098 if (DECL_ONE_ONLY (node->symbol.decl))
1099 DECL_SECTION_NAME (new_decl) = NULL;
1100 DECL_COMDAT_GROUP (new_decl) = 0;
1101 TREE_PUBLIC (new_decl) = 0;
1102 DECL_COMDAT (new_decl) = 0;
1103 DECL_WEAK (new_decl) = 0;
1104 DECL_VIRTUAL_P (new_decl) = 0;
1105 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1107 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1108 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1109 new_node = (symtab_node) cgraph_create_function_alias (new_decl, node->symbol.decl);
1111 else
1112 new_node = (symtab_node) varpool_create_variable_alias (new_decl, node->symbol.decl);
1113 symtab_resolve_alias (new_node, node);
1114 return new_node;
1116 #include "gt-symtab.h"