cgraph.c (ld_plugin_symbol_resolution_names): Move to symtab.c
[official-gcc.git] / gcc / symtab.c
blob1d9fdd881085755152e97bdf63e0d97486b30676
1 /* Symbol table.
2 Copyright (C) 2012 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)
107 gcc_checking_assert (!node->symbol.previous_sharing_asm_name
108 && !node->symbol.next_sharing_asm_name);
109 if (assembler_name_hash)
111 void **aslot;
112 tree name = DECL_ASSEMBLER_NAME (node->symbol.decl);
114 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
115 decl_assembler_name_hash (name),
116 INSERT);
117 gcc_assert (*aslot != node);
118 node->symbol.next_sharing_asm_name = (symtab_node)*aslot;
119 if (*aslot != NULL)
120 ((symtab_node)*aslot)->symbol.previous_sharing_asm_name = node;
121 *aslot = node;
126 /* Remove NODE from assembler name hash. */
128 static void
129 unlink_from_assembler_name_hash (symtab_node node)
131 if (assembler_name_hash)
133 if (node->symbol.next_sharing_asm_name)
134 node->symbol.next_sharing_asm_name->symbol.previous_sharing_asm_name
135 = node->symbol.previous_sharing_asm_name;
136 if (node->symbol.previous_sharing_asm_name)
138 node->symbol.previous_sharing_asm_name->symbol.next_sharing_asm_name
139 = node->symbol.next_sharing_asm_name;
141 else
143 tree name = DECL_ASSEMBLER_NAME (node->symbol.decl);
144 void **slot;
145 slot = htab_find_slot_with_hash (assembler_name_hash, name,
146 decl_assembler_name_hash (name),
147 NO_INSERT);
148 gcc_assert (*slot == node);
149 if (!node->symbol.next_sharing_asm_name)
150 htab_clear_slot (assembler_name_hash, slot);
151 else
152 *slot = node->symbol.next_sharing_asm_name;
158 /* Add node into symbol table. This function is not used directly, but via
159 cgraph/varpool node creation routines. */
161 void
162 symtab_register_node (symtab_node node)
164 struct symtab_node_base key;
165 symtab_node *slot;
167 node->symbol.next = symtab_nodes;
168 node->symbol.previous = NULL;
169 if (symtab_nodes)
170 symtab_nodes->symbol.previous = node;
171 symtab_nodes = node;
173 if (!symtab_hash)
174 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
175 key.decl = node->symbol.decl;
176 slot = (symtab_node *) htab_find_slot (symtab_hash, &key, INSERT);
177 if (*slot == NULL)
178 *slot = node;
180 insert_to_assembler_name_hash (node);
182 node->symbol.order = symtab_order++;
184 ipa_empty_ref_list (&node->symbol.ref_list);
187 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
188 of inline clones. */
190 void
191 symtab_insert_node_to_hashtable (symtab_node node)
193 struct symtab_node_base key;
194 symtab_node *slot;
196 if (!symtab_hash)
197 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
198 key.decl = node->symbol.decl;
199 slot = (symtab_node *) htab_find_slot (symtab_hash, &key, INSERT);
200 *slot = node;
203 /* Remove node from symbol table. This function is not used directly, but via
204 cgraph/varpool node removal routines. */
206 void
207 symtab_unregister_node (symtab_node node)
209 void **slot;
210 ipa_remove_all_references (&node->symbol.ref_list);
211 ipa_remove_all_referring (&node->symbol.ref_list);
213 if (node->symbol.same_comdat_group)
215 symtab_node prev;
216 for (prev = node->symbol.same_comdat_group;
217 prev->symbol.same_comdat_group != node;
218 prev = prev->symbol.same_comdat_group)
220 if (node->symbol.same_comdat_group == prev)
221 prev->symbol.same_comdat_group = NULL;
222 else
223 prev->symbol.same_comdat_group = node->symbol.same_comdat_group;
224 node->symbol.same_comdat_group = NULL;
227 if (node->symbol.previous)
228 node->symbol.previous->symbol.next = node->symbol.next;
229 else
230 symtab_nodes = node->symbol.next;
231 if (node->symbol.next)
232 node->symbol.next->symbol.previous = node->symbol.previous;
233 node->symbol.next = NULL;
234 node->symbol.previous = NULL;
236 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
237 if (*slot == node)
239 symtab_node replacement_node = NULL;
240 if (symtab_function_p (node))
241 replacement_node = (symtab_node)cgraph_find_replacement_node (cgraph (node));
242 if (!replacement_node)
243 htab_clear_slot (symtab_hash, slot);
244 else
245 *slot = replacement_node;
247 unlink_from_assembler_name_hash (node);
250 /* Return symbol table node associated with DECL, if any,
251 and NULL otherwise. */
253 symtab_node
254 symtab_get_node (const_tree decl)
256 symtab_node *slot;
257 struct symtab_node_base key;
259 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
260 || (TREE_CODE (decl) == VAR_DECL
261 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
262 || in_lto_p)));
264 if (!symtab_hash)
265 return NULL;
267 key.decl = CONST_CAST2 (tree, const_tree, decl);
269 slot = (symtab_node *) htab_find_slot (symtab_hash, &key,
270 NO_INSERT);
272 if (slot)
273 return *slot;
274 return NULL;
277 /* Remove symtab NODE from the symbol table. */
279 void
280 symtab_remove_node (symtab_node node)
282 if (symtab_function_p (node))
283 cgraph_remove_node (cgraph (node));
284 else if (symtab_variable_p (node))
285 varpool_remove_node (varpool (node));
288 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
289 Return NULL if there's no such node. */
291 symtab_node
292 symtab_node_for_asm (const_tree asmname)
294 symtab_node node;
295 void **slot;
297 if (!assembler_name_hash)
299 assembler_name_hash =
300 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
301 NULL);
302 FOR_EACH_SYMBOL (node)
303 insert_to_assembler_name_hash (node);
306 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
307 decl_assembler_name_hash (asmname),
308 NO_INSERT);
310 if (slot)
312 node = (symtab_node) *slot;
313 return node;
315 return NULL;
318 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
320 void
321 change_decl_assembler_name (tree decl, tree name)
323 symtab_node node = NULL;
325 /* We can have user ASM names on things, like global register variables, that
326 are not in the symbol table. */
327 if ((TREE_CODE (decl) == VAR_DECL
328 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
329 || TREE_CODE (decl) == FUNCTION_DECL)
330 node = symtab_get_node (decl);
331 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
333 SET_DECL_ASSEMBLER_NAME (decl, name);
334 if (node)
335 insert_to_assembler_name_hash (node);
337 else
339 if (name == DECL_ASSEMBLER_NAME (decl))
340 return;
342 if (node)
343 unlink_from_assembler_name_hash (node);
344 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
345 && DECL_RTL_SET_P (decl))
346 warning (0, "%D renamed after being referenced in assembly", decl);
348 SET_DECL_ASSEMBLER_NAME (decl, name);
349 if (node)
350 insert_to_assembler_name_hash (node);
354 /* Add NEW_ to the same comdat group that OLD is in. */
356 void
357 symtab_add_to_same_comdat_group (symtab_node new_node,
358 symtab_node old_node)
360 gcc_assert (DECL_ONE_ONLY (old_node->symbol.decl));
361 gcc_assert (!new_node->symbol.same_comdat_group);
362 gcc_assert (new_node != old_node);
364 DECL_COMDAT_GROUP (new_node->symbol.decl) = DECL_COMDAT_GROUP (old_node->symbol.decl);
365 new_node->symbol.same_comdat_group = old_node;
366 if (!old_node->symbol.same_comdat_group)
367 old_node->symbol.same_comdat_group = new_node;
368 else
370 symtab_node n;
371 for (n = old_node->symbol.same_comdat_group;
372 n->symbol.same_comdat_group != old_node;
373 n = n->symbol.same_comdat_group)
375 n->symbol.same_comdat_group = new_node;
379 /* Dissolve the same_comdat_group list in which NODE resides. */
381 void
382 symtab_dissolve_same_comdat_group_list (symtab_node node)
384 symtab_node n = node, next;
386 if (!node->symbol.same_comdat_group)
387 return;
390 next = n->symbol.same_comdat_group;
391 n->symbol.same_comdat_group = NULL;
392 n = next;
394 while (n != node);
397 /* Return printable assembler name of NODE.
398 This function is used only for debugging. When assembler name
399 is unknown go with identifier name. */
401 const char *
402 symtab_node_asm_name (symtab_node node)
404 if (!DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
405 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
406 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl));
409 /* Return printable identifier name. */
411 const char *
412 symtab_node_name (symtab_node node)
414 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
417 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
419 /* Dump base fields of symtab nodes. Not to be used directly. */
421 void
422 dump_symtab_base (FILE *f, symtab_node node)
424 static const char * const visibility_types[] = {
425 "default", "protected", "hidden", "internal"
428 fprintf (f, "%s/%i (%s)",
429 symtab_node_asm_name (node),
430 node->symbol.order,
431 symtab_node_name (node));
432 dump_addr (f, " @", (void *)node);
433 fprintf (f, "\n Type: %s\n", symtab_type_names[node->symbol.type]);
434 fprintf (f, " Visibility:");
436 if (node->symbol.in_other_partition)
437 fprintf (f, " in_other_partition");
438 if (node->symbol.used_from_other_partition)
439 fprintf (f, " used_from_other_partition");
440 if (node->symbol.force_output)
441 fprintf (f, " force_output");
442 if (node->symbol.resolution != LDPR_UNKNOWN)
443 fprintf (f, " %s",
444 ld_plugin_symbol_resolution_names[(int)node->symbol.resolution]);
445 if (TREE_ASM_WRITTEN (node->symbol.decl))
446 fprintf (f, " asm_written");
447 if (DECL_EXTERNAL (node->symbol.decl))
448 fprintf (f, " external");
449 if (TREE_PUBLIC (node->symbol.decl))
450 fprintf (f, " public");
451 if (DECL_COMMON (node->symbol.decl))
452 fprintf (f, " common");
453 if (DECL_WEAK (node->symbol.decl))
454 fprintf (f, " weak");
455 if (DECL_DLLIMPORT_P (node->symbol.decl))
456 fprintf (f, " dll_import");
457 if (DECL_COMDAT (node->symbol.decl))
458 fprintf (f, " comdat");
459 if (DECL_COMDAT_GROUP (node->symbol.decl))
460 fprintf (f, " comdat_group:%s",
461 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->symbol.decl)));
462 if (DECL_ONE_ONLY (node->symbol.decl))
463 fprintf (f, " one_only");
464 if (DECL_SECTION_NAME (node->symbol.decl))
465 fprintf (f, " section_name:%s",
466 TREE_STRING_POINTER (DECL_SECTION_NAME (node->symbol.decl)));
467 if (DECL_VISIBILITY_SPECIFIED (node->symbol.decl))
468 fprintf (f, " visibility_specified");
469 if (DECL_VISIBILITY (node->symbol.decl))
470 fprintf (f, " visibility:%s",
471 visibility_types [DECL_VISIBILITY (node->symbol.decl)]);
472 if (DECL_VIRTUAL_P (node->symbol.decl))
473 fprintf (f, " virtual");
474 if (DECL_ARTIFICIAL (node->symbol.decl))
475 fprintf (f, " artificial");
476 if (TREE_CODE (node->symbol.decl) == FUNCTION_DECL)
478 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
479 fprintf (f, " constructor");
480 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
481 fprintf (f, " destructor");
483 fprintf (f, "\n");
485 if (node->symbol.same_comdat_group)
486 fprintf (f, " Same comdat group as: %s/%i\n",
487 symtab_node_asm_name (node->symbol.same_comdat_group),
488 node->symbol.same_comdat_group->symbol.order);
489 if (node->symbol.next_sharing_asm_name)
490 fprintf (f, " next sharing asm name: %i\n",
491 node->symbol.next_sharing_asm_name->symbol.order);
492 if (node->symbol.previous_sharing_asm_name)
493 fprintf (f, " previous sharing asm name: %i\n",
494 node->symbol.previous_sharing_asm_name->symbol.order);
496 if (node->symbol.address_taken)
497 fprintf (f, " Address is taken.\n");
498 if (node->symbol.aux)
500 fprintf (f, " Aux:");
501 dump_addr (f, " @", (void *)node->symbol.aux);
504 fprintf (f, " References: ");
505 ipa_dump_references (f, &node->symbol.ref_list);
506 fprintf (f, " Referring: ");
507 ipa_dump_referring (f, &node->symbol.ref_list);
510 /* Dump symtab node. */
512 void
513 dump_symtab_node (FILE *f, symtab_node node)
515 if (symtab_function_p (node))
516 dump_cgraph_node (f, cgraph (node));
517 else if (symtab_variable_p (node))
518 dump_varpool_node (f, varpool (node));
521 /* Dump symbol table. */
523 void
524 dump_symtab (FILE *f)
526 symtab_node node;
527 fprintf (f, "Symbol table:\n\n");
528 FOR_EACH_SYMBOL (node)
529 dump_symtab_node (f, node);
532 /* Dump symtab node NODE to stderr. */
534 DEBUG_FUNCTION void
535 debug_symtab_node (symtab_node node)
537 dump_symtab_node (stderr, node);
540 /* Dump symbol table to stderr. */
542 DEBUG_FUNCTION void
543 debug_symtab (void)
545 dump_symtab (stderr);
548 /* Verify common part of symtab nodes. */
550 DEBUG_FUNCTION bool
551 verify_symtab_base (symtab_node node)
553 bool error_found = false;
554 symtab_node hashed_node;
556 if (symtab_function_p (node))
558 if (TREE_CODE (node->symbol.decl) != FUNCTION_DECL)
560 error ("function symbol is not function");
561 error_found = true;
564 else if (symtab_variable_p (node))
566 if (TREE_CODE (node->symbol.decl) != VAR_DECL)
568 error ("variable symbol is not variable");
569 error_found = true;
572 else
574 error ("node has unknown type");
575 error_found = true;
578 hashed_node = symtab_get_node (node->symbol.decl);
579 if (!hashed_node)
581 error ("node not found in symtab decl hashtable");
582 error_found = true;
584 if (assembler_name_hash)
586 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->symbol.decl));
587 if (hashed_node && hashed_node->symbol.previous_sharing_asm_name)
589 error ("assembler name hash list corrupted");
590 error_found = true;
592 while (hashed_node)
594 if (hashed_node == node)
595 break;
596 hashed_node = hashed_node->symbol.next_sharing_asm_name;
598 if (!hashed_node)
600 error ("node not found in symtab assembler name hash");
601 error_found = true;
604 if (node->symbol.previous_sharing_asm_name
605 && node->symbol.previous_sharing_asm_name->symbol.next_sharing_asm_name != node)
607 error ("double linked list of assembler names corrupted");
609 if (node->symbol.same_comdat_group)
611 symtab_node n = node->symbol.same_comdat_group;
613 if (!DECL_ONE_ONLY (n->symbol.decl))
615 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
616 error_found = true;
618 if (n->symbol.type != node->symbol.type)
620 error ("mixing different types of symbol in same comdat groups is not supported");
621 error_found = true;
623 if (n == node)
625 error ("node is alone in a comdat group");
626 error_found = true;
630 if (!n->symbol.same_comdat_group)
632 error ("same_comdat_group is not a circular list");
633 error_found = true;
634 break;
636 n = n->symbol.same_comdat_group;
638 while (n != node);
640 return error_found;
643 /* Verify consistency of NODE. */
645 DEBUG_FUNCTION void
646 verify_symtab_node (symtab_node node)
648 if (seen_error ())
649 return;
651 timevar_push (TV_CGRAPH_VERIFY);
652 if (symtab_function_p (node))
653 verify_cgraph_node (cgraph (node));
654 else
655 if (verify_symtab_base (node))
657 dump_symtab_node (stderr, node);
658 internal_error ("verify_symtab_node failed");
660 timevar_pop (TV_CGRAPH_VERIFY);
663 /* Verify symbol table for internal consistency. */
665 DEBUG_FUNCTION void
666 verify_symtab (void)
668 symtab_node node;
669 FOR_EACH_SYMBOL (node)
670 verify_symtab_node (node);
673 /* Return true when RESOLUTION indicate that linker will use
674 the symbol from non-LTO object files. */
676 bool
677 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
679 return (resolution == LDPR_PREVAILING_DEF
680 || resolution == LDPR_PREEMPTED_REG
681 || resolution == LDPR_RESOLVED_EXEC
682 || resolution == LDPR_RESOLVED_DYN);
685 /* Return true when NODE is known to be used from other (non-LTO) object file.
686 Known only when doing LTO via linker plugin. */
688 bool
689 symtab_used_from_object_file_p (symtab_node node)
691 if (!TREE_PUBLIC (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
692 return false;
693 if (resolution_used_from_other_file_p (node->symbol.resolution))
694 return true;
695 return false;
698 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
699 but other code such as notice_global_symbol generates rtl. */
700 void
701 symtab_make_decl_local (tree decl)
703 rtx rtl, symbol;
705 if (TREE_CODE (decl) == VAR_DECL)
706 DECL_COMMON (decl) = 0;
707 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
709 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
711 /* It is possible that we are linking against library defining same COMDAT
712 function. To avoid conflict we need to rename our local name of the
713 function just in the case WHOPR partitioning decide to make it hidden
714 to avoid cross partition references. */
715 if (flag_wpa)
717 const char *old_name;
718 symtab_node node = symtab_get_node (decl);
719 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
720 change_decl_assembler_name (decl,
721 clone_function_name (decl, "local"));
722 if (node->symbol.lto_file_data)
723 lto_record_renamed_decl (node->symbol.lto_file_data,
724 old_name,
725 IDENTIFIER_POINTER
726 (DECL_ASSEMBLER_NAME (decl)));
728 DECL_SECTION_NAME (decl) = 0;
729 DECL_COMDAT (decl) = 0;
731 DECL_COMDAT_GROUP (decl) = 0;
732 DECL_WEAK (decl) = 0;
733 DECL_EXTERNAL (decl) = 0;
734 TREE_PUBLIC (decl) = 0;
735 if (!DECL_RTL_SET_P (decl))
736 return;
738 /* Update rtl flags. */
739 make_decl_rtl (decl);
741 rtl = DECL_RTL (decl);
742 if (!MEM_P (rtl))
743 return;
745 symbol = XEXP (rtl, 0);
746 if (GET_CODE (symbol) != SYMBOL_REF)
747 return;
749 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
751 #include "gt-symtab.h"