PR c++/18747
[official-gcc.git] / gcc / symtab.c
blob665ceae41e06221bd129cec0502078a0a3dfbaa7
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 ipa_empty_ref_list (&node->symbol.ref_list);
182 node->symbol.order = symtab_order++;
184 /* Be sure to do this last; C++ FE might create new nodes via
185 DECL_ASSEMBLER_NAME langhook! */
186 insert_to_assembler_name_hash (node);
189 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
190 of inline clones. */
192 void
193 symtab_insert_node_to_hashtable (symtab_node node)
195 struct symtab_node_base key;
196 symtab_node *slot;
198 if (!symtab_hash)
199 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
200 key.decl = node->symbol.decl;
201 slot = (symtab_node *) htab_find_slot (symtab_hash, &key, INSERT);
202 *slot = node;
205 /* Remove node from symbol table. This function is not used directly, but via
206 cgraph/varpool node removal routines. */
208 void
209 symtab_unregister_node (symtab_node node)
211 void **slot;
212 ipa_remove_all_references (&node->symbol.ref_list);
213 ipa_remove_all_referring (&node->symbol.ref_list);
215 if (node->symbol.same_comdat_group)
217 symtab_node prev;
218 for (prev = node->symbol.same_comdat_group;
219 prev->symbol.same_comdat_group != node;
220 prev = prev->symbol.same_comdat_group)
222 if (node->symbol.same_comdat_group == prev)
223 prev->symbol.same_comdat_group = NULL;
224 else
225 prev->symbol.same_comdat_group = node->symbol.same_comdat_group;
226 node->symbol.same_comdat_group = NULL;
229 if (node->symbol.previous)
230 node->symbol.previous->symbol.next = node->symbol.next;
231 else
232 symtab_nodes = node->symbol.next;
233 if (node->symbol.next)
234 node->symbol.next->symbol.previous = node->symbol.previous;
235 node->symbol.next = NULL;
236 node->symbol.previous = NULL;
238 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
239 if (*slot == node)
241 symtab_node replacement_node = NULL;
242 if (symtab_function_p (node))
243 replacement_node = (symtab_node)cgraph_find_replacement_node (cgraph (node));
244 if (!replacement_node)
245 htab_clear_slot (symtab_hash, slot);
246 else
247 *slot = replacement_node;
249 unlink_from_assembler_name_hash (node);
252 /* Return symbol table node associated with DECL, if any,
253 and NULL otherwise. */
255 symtab_node
256 symtab_get_node (const_tree decl)
258 symtab_node *slot;
259 struct symtab_node_base key;
261 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
262 || (TREE_CODE (decl) == VAR_DECL
263 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
264 || in_lto_p)));
266 if (!symtab_hash)
267 return NULL;
269 key.decl = CONST_CAST2 (tree, const_tree, decl);
271 slot = (symtab_node *) htab_find_slot (symtab_hash, &key,
272 NO_INSERT);
274 if (slot)
275 return *slot;
276 return NULL;
279 /* Remove symtab NODE from the symbol table. */
281 void
282 symtab_remove_node (symtab_node node)
284 if (symtab_function_p (node))
285 cgraph_remove_node (cgraph (node));
286 else if (symtab_variable_p (node))
287 varpool_remove_node (varpool (node));
290 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
291 Return NULL if there's no such node. */
293 symtab_node
294 symtab_node_for_asm (const_tree asmname)
296 symtab_node node;
297 void **slot;
299 if (!assembler_name_hash)
301 assembler_name_hash =
302 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
303 NULL);
304 FOR_EACH_SYMBOL (node)
305 insert_to_assembler_name_hash (node);
308 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
309 decl_assembler_name_hash (asmname),
310 NO_INSERT);
312 if (slot)
314 node = (symtab_node) *slot;
315 return node;
317 return NULL;
320 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
322 void
323 change_decl_assembler_name (tree decl, tree name)
325 symtab_node node = NULL;
327 /* We can have user ASM names on things, like global register variables, that
328 are not in the symbol table. */
329 if ((TREE_CODE (decl) == VAR_DECL
330 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
331 || TREE_CODE (decl) == FUNCTION_DECL)
332 node = symtab_get_node (decl);
333 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
335 SET_DECL_ASSEMBLER_NAME (decl, name);
336 if (node)
337 insert_to_assembler_name_hash (node);
339 else
341 if (name == DECL_ASSEMBLER_NAME (decl))
342 return;
344 if (node)
345 unlink_from_assembler_name_hash (node);
346 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
347 && DECL_RTL_SET_P (decl))
348 warning (0, "%D renamed after being referenced in assembly", decl);
350 SET_DECL_ASSEMBLER_NAME (decl, name);
351 if (node)
352 insert_to_assembler_name_hash (node);
356 /* Add NEW_ to the same comdat group that OLD is in. */
358 void
359 symtab_add_to_same_comdat_group (symtab_node new_node,
360 symtab_node old_node)
362 gcc_assert (DECL_ONE_ONLY (old_node->symbol.decl));
363 gcc_assert (!new_node->symbol.same_comdat_group);
364 gcc_assert (new_node != old_node);
366 DECL_COMDAT_GROUP (new_node->symbol.decl) = DECL_COMDAT_GROUP (old_node->symbol.decl);
367 new_node->symbol.same_comdat_group = old_node;
368 if (!old_node->symbol.same_comdat_group)
369 old_node->symbol.same_comdat_group = new_node;
370 else
372 symtab_node n;
373 for (n = old_node->symbol.same_comdat_group;
374 n->symbol.same_comdat_group != old_node;
375 n = n->symbol.same_comdat_group)
377 n->symbol.same_comdat_group = new_node;
381 /* Dissolve the same_comdat_group list in which NODE resides. */
383 void
384 symtab_dissolve_same_comdat_group_list (symtab_node node)
386 symtab_node n = node, next;
388 if (!node->symbol.same_comdat_group)
389 return;
392 next = n->symbol.same_comdat_group;
393 n->symbol.same_comdat_group = NULL;
394 n = next;
396 while (n != node);
399 /* Return printable assembler name of NODE.
400 This function is used only for debugging. When assembler name
401 is unknown go with identifier name. */
403 const char *
404 symtab_node_asm_name (symtab_node node)
406 if (!DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
407 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
408 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl));
411 /* Return printable identifier name. */
413 const char *
414 symtab_node_name (symtab_node node)
416 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
419 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
421 /* Dump base fields of symtab nodes. Not to be used directly. */
423 void
424 dump_symtab_base (FILE *f, symtab_node node)
426 static const char * const visibility_types[] = {
427 "default", "protected", "hidden", "internal"
430 fprintf (f, "%s/%i (%s)",
431 symtab_node_asm_name (node),
432 node->symbol.order,
433 symtab_node_name (node));
434 dump_addr (f, " @", (void *)node);
435 fprintf (f, "\n Type: %s\n", symtab_type_names[node->symbol.type]);
436 fprintf (f, " Visibility:");
438 if (node->symbol.in_other_partition)
439 fprintf (f, " in_other_partition");
440 if (node->symbol.used_from_other_partition)
441 fprintf (f, " used_from_other_partition");
442 if (node->symbol.force_output)
443 fprintf (f, " force_output");
444 if (node->symbol.resolution != LDPR_UNKNOWN)
445 fprintf (f, " %s",
446 ld_plugin_symbol_resolution_names[(int)node->symbol.resolution]);
447 if (TREE_ASM_WRITTEN (node->symbol.decl))
448 fprintf (f, " asm_written");
449 if (DECL_EXTERNAL (node->symbol.decl))
450 fprintf (f, " external");
451 if (TREE_PUBLIC (node->symbol.decl))
452 fprintf (f, " public");
453 if (DECL_COMMON (node->symbol.decl))
454 fprintf (f, " common");
455 if (DECL_WEAK (node->symbol.decl))
456 fprintf (f, " weak");
457 if (DECL_DLLIMPORT_P (node->symbol.decl))
458 fprintf (f, " dll_import");
459 if (DECL_COMDAT (node->symbol.decl))
460 fprintf (f, " comdat");
461 if (DECL_COMDAT_GROUP (node->symbol.decl))
462 fprintf (f, " comdat_group:%s",
463 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->symbol.decl)));
464 if (DECL_ONE_ONLY (node->symbol.decl))
465 fprintf (f, " one_only");
466 if (DECL_SECTION_NAME (node->symbol.decl))
467 fprintf (f, " section_name:%s",
468 TREE_STRING_POINTER (DECL_SECTION_NAME (node->symbol.decl)));
469 if (DECL_VISIBILITY_SPECIFIED (node->symbol.decl))
470 fprintf (f, " visibility_specified");
471 if (DECL_VISIBILITY (node->symbol.decl))
472 fprintf (f, " visibility:%s",
473 visibility_types [DECL_VISIBILITY (node->symbol.decl)]);
474 if (DECL_VIRTUAL_P (node->symbol.decl))
475 fprintf (f, " virtual");
476 if (DECL_ARTIFICIAL (node->symbol.decl))
477 fprintf (f, " artificial");
478 if (TREE_CODE (node->symbol.decl) == FUNCTION_DECL)
480 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
481 fprintf (f, " constructor");
482 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
483 fprintf (f, " destructor");
485 fprintf (f, "\n");
487 if (node->symbol.same_comdat_group)
488 fprintf (f, " Same comdat group as: %s/%i\n",
489 symtab_node_asm_name (node->symbol.same_comdat_group),
490 node->symbol.same_comdat_group->symbol.order);
491 if (node->symbol.next_sharing_asm_name)
492 fprintf (f, " next sharing asm name: %i\n",
493 node->symbol.next_sharing_asm_name->symbol.order);
494 if (node->symbol.previous_sharing_asm_name)
495 fprintf (f, " previous sharing asm name: %i\n",
496 node->symbol.previous_sharing_asm_name->symbol.order);
498 if (node->symbol.address_taken)
499 fprintf (f, " Address is taken.\n");
500 if (node->symbol.aux)
502 fprintf (f, " Aux:");
503 dump_addr (f, " @", (void *)node->symbol.aux);
506 fprintf (f, " References: ");
507 ipa_dump_references (f, &node->symbol.ref_list);
508 fprintf (f, " Referring: ");
509 ipa_dump_referring (f, &node->symbol.ref_list);
512 /* Dump symtab node. */
514 void
515 dump_symtab_node (FILE *f, symtab_node node)
517 if (symtab_function_p (node))
518 dump_cgraph_node (f, cgraph (node));
519 else if (symtab_variable_p (node))
520 dump_varpool_node (f, varpool (node));
523 /* Dump symbol table. */
525 void
526 dump_symtab (FILE *f)
528 symtab_node node;
529 fprintf (f, "Symbol table:\n\n");
530 FOR_EACH_SYMBOL (node)
531 dump_symtab_node (f, node);
534 /* Dump symtab node NODE to stderr. */
536 DEBUG_FUNCTION void
537 debug_symtab_node (symtab_node node)
539 dump_symtab_node (stderr, node);
542 /* Dump symbol table to stderr. */
544 DEBUG_FUNCTION void
545 debug_symtab (void)
547 dump_symtab (stderr);
550 /* Verify common part of symtab nodes. */
552 DEBUG_FUNCTION bool
553 verify_symtab_base (symtab_node node)
555 bool error_found = false;
556 symtab_node hashed_node;
558 if (symtab_function_p (node))
560 if (TREE_CODE (node->symbol.decl) != FUNCTION_DECL)
562 error ("function symbol is not function");
563 error_found = true;
566 else if (symtab_variable_p (node))
568 if (TREE_CODE (node->symbol.decl) != VAR_DECL)
570 error ("variable symbol is not variable");
571 error_found = true;
574 else
576 error ("node has unknown type");
577 error_found = true;
580 hashed_node = symtab_get_node (node->symbol.decl);
581 if (!hashed_node)
583 error ("node not found in symtab decl hashtable");
584 error_found = true;
586 if (assembler_name_hash)
588 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->symbol.decl));
589 if (hashed_node && hashed_node->symbol.previous_sharing_asm_name)
591 error ("assembler name hash list corrupted");
592 error_found = true;
594 while (hashed_node)
596 if (hashed_node == node)
597 break;
598 hashed_node = hashed_node->symbol.next_sharing_asm_name;
600 if (!hashed_node)
602 error ("node not found in symtab assembler name hash");
603 error_found = true;
606 if (node->symbol.previous_sharing_asm_name
607 && node->symbol.previous_sharing_asm_name->symbol.next_sharing_asm_name != node)
609 error ("double linked list of assembler names corrupted");
611 if (node->symbol.same_comdat_group)
613 symtab_node n = node->symbol.same_comdat_group;
615 if (!DECL_ONE_ONLY (n->symbol.decl))
617 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
618 error_found = true;
620 if (n->symbol.type != node->symbol.type)
622 error ("mixing different types of symbol in same comdat groups is not supported");
623 error_found = true;
625 if (n == node)
627 error ("node is alone in a comdat group");
628 error_found = true;
632 if (!n->symbol.same_comdat_group)
634 error ("same_comdat_group is not a circular list");
635 error_found = true;
636 break;
638 n = n->symbol.same_comdat_group;
640 while (n != node);
642 return error_found;
645 /* Verify consistency of NODE. */
647 DEBUG_FUNCTION void
648 verify_symtab_node (symtab_node node)
650 if (seen_error ())
651 return;
653 timevar_push (TV_CGRAPH_VERIFY);
654 if (symtab_function_p (node))
655 verify_cgraph_node (cgraph (node));
656 else
657 if (verify_symtab_base (node))
659 dump_symtab_node (stderr, node);
660 internal_error ("verify_symtab_node failed");
662 timevar_pop (TV_CGRAPH_VERIFY);
665 /* Verify symbol table for internal consistency. */
667 DEBUG_FUNCTION void
668 verify_symtab (void)
670 symtab_node node;
671 FOR_EACH_SYMBOL (node)
672 verify_symtab_node (node);
675 /* Return true when RESOLUTION indicate that linker will use
676 the symbol from non-LTO object files. */
678 bool
679 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
681 return (resolution == LDPR_PREVAILING_DEF
682 || resolution == LDPR_PREEMPTED_REG
683 || resolution == LDPR_RESOLVED_EXEC
684 || resolution == LDPR_RESOLVED_DYN);
687 /* Return true when NODE is known to be used from other (non-LTO) object file.
688 Known only when doing LTO via linker plugin. */
690 bool
691 symtab_used_from_object_file_p (symtab_node node)
693 if (!TREE_PUBLIC (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
694 return false;
695 if (resolution_used_from_other_file_p (node->symbol.resolution))
696 return true;
697 return false;
700 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
701 but other code such as notice_global_symbol generates rtl. */
702 void
703 symtab_make_decl_local (tree decl)
705 rtx rtl, symbol;
707 if (TREE_CODE (decl) == VAR_DECL)
708 DECL_COMMON (decl) = 0;
709 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
711 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
713 /* It is possible that we are linking against library defining same COMDAT
714 function. To avoid conflict we need to rename our local name of the
715 function just in the case WHOPR partitioning decide to make it hidden
716 to avoid cross partition references. */
717 if (flag_wpa)
719 const char *old_name;
720 symtab_node node = symtab_get_node (decl);
721 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
722 change_decl_assembler_name (decl,
723 clone_function_name (decl, "local"));
724 if (node->symbol.lto_file_data)
725 lto_record_renamed_decl (node->symbol.lto_file_data,
726 old_name,
727 IDENTIFIER_POINTER
728 (DECL_ASSEMBLER_NAME (decl)));
730 DECL_SECTION_NAME (decl) = 0;
731 DECL_COMDAT (decl) = 0;
733 DECL_COMDAT_GROUP (decl) = 0;
734 DECL_WEAK (decl) = 0;
735 DECL_EXTERNAL (decl) = 0;
736 TREE_PUBLIC (decl) = 0;
737 if (!DECL_RTL_SET_P (decl))
738 return;
740 /* Update rtl flags. */
741 make_decl_rtl (decl);
743 rtl = DECL_RTL (decl);
744 if (!MEM_P (rtl))
745 return;
747 symbol = XEXP (rtl, 0);
748 if (GET_CODE (symbol) != SYMBOL_REF)
749 return;
751 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
753 #include "gt-symtab.h"