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
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
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/>. */
23 #include "coretypes.h"
26 #include "tree-inline.h"
27 #include "langhooks.h"
31 #include "diagnostic.h"
33 #include "lto-streamer.h"
36 const char * const ld_plugin_symbol_resolution_names
[]=
41 "prevailing_def_ironly",
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. */
63 /* Returns a hash code for P. */
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. */
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. */
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. */
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. */
105 insert_to_assembler_name_hash (symtab_node node
)
107 if (is_a
<varpool_node
> (node
) && DECL_HARD_REGISTER (node
->symbol
.decl
))
109 gcc_checking_assert (!node
->symbol
.previous_sharing_asm_name
110 && !node
->symbol
.next_sharing_asm_name
);
111 if (assembler_name_hash
)
114 tree name
= DECL_ASSEMBLER_NAME (node
->symbol
.decl
);
116 aslot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
117 decl_assembler_name_hash (name
),
119 gcc_assert (*aslot
!= node
);
120 node
->symbol
.next_sharing_asm_name
= (symtab_node
)*aslot
;
122 ((symtab_node
)*aslot
)->symbol
.previous_sharing_asm_name
= node
;
128 /* Remove NODE from assembler name hash. */
131 unlink_from_assembler_name_hash (symtab_node node
)
133 if (assembler_name_hash
)
135 if (node
->symbol
.next_sharing_asm_name
)
136 node
->symbol
.next_sharing_asm_name
->symbol
.previous_sharing_asm_name
137 = node
->symbol
.previous_sharing_asm_name
;
138 if (node
->symbol
.previous_sharing_asm_name
)
140 node
->symbol
.previous_sharing_asm_name
->symbol
.next_sharing_asm_name
141 = node
->symbol
.next_sharing_asm_name
;
145 tree name
= DECL_ASSEMBLER_NAME (node
->symbol
.decl
);
147 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
148 decl_assembler_name_hash (name
),
150 gcc_assert (*slot
== node
);
151 if (!node
->symbol
.next_sharing_asm_name
)
152 htab_clear_slot (assembler_name_hash
, slot
);
154 *slot
= node
->symbol
.next_sharing_asm_name
;
156 node
->symbol
.next_sharing_asm_name
= NULL
;
157 node
->symbol
.previous_sharing_asm_name
= NULL
;
161 /* Arrange node to be first in its entry of assembler_name_hash. */
164 symtab_prevail_in_asm_name_hash (symtab_node node
)
166 unlink_from_assembler_name_hash (node
);
167 insert_to_assembler_name_hash (node
);
171 /* Add node into symbol table. This function is not used directly, but via
172 cgraph/varpool node creation routines. */
175 symtab_register_node (symtab_node node
)
177 struct symtab_node_base key
;
180 node
->symbol
.next
= symtab_nodes
;
181 node
->symbol
.previous
= NULL
;
183 symtab_nodes
->symbol
.previous
= node
;
187 symtab_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
188 key
.decl
= node
->symbol
.decl
;
189 slot
= (symtab_node
*) htab_find_slot (symtab_hash
, &key
, INSERT
);
193 ipa_empty_ref_list (&node
->symbol
.ref_list
);
195 node
->symbol
.order
= symtab_order
++;
197 /* Be sure to do this last; C++ FE might create new nodes via
198 DECL_ASSEMBLER_NAME langhook! */
199 insert_to_assembler_name_hash (node
);
202 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
206 symtab_insert_node_to_hashtable (symtab_node node
)
208 struct symtab_node_base key
;
212 symtab_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
213 key
.decl
= node
->symbol
.decl
;
214 slot
= (symtab_node
*) htab_find_slot (symtab_hash
, &key
, INSERT
);
218 /* Remove node from symbol table. This function is not used directly, but via
219 cgraph/varpool node removal routines. */
222 symtab_unregister_node (symtab_node node
)
225 ipa_remove_all_references (&node
->symbol
.ref_list
);
226 ipa_remove_all_referring (&node
->symbol
.ref_list
);
228 if (node
->symbol
.same_comdat_group
)
231 for (prev
= node
->symbol
.same_comdat_group
;
232 prev
->symbol
.same_comdat_group
!= node
;
233 prev
= prev
->symbol
.same_comdat_group
)
235 if (node
->symbol
.same_comdat_group
== prev
)
236 prev
->symbol
.same_comdat_group
= NULL
;
238 prev
->symbol
.same_comdat_group
= node
->symbol
.same_comdat_group
;
239 node
->symbol
.same_comdat_group
= NULL
;
242 if (node
->symbol
.previous
)
243 node
->symbol
.previous
->symbol
.next
= node
->symbol
.next
;
245 symtab_nodes
= node
->symbol
.next
;
246 if (node
->symbol
.next
)
247 node
->symbol
.next
->symbol
.previous
= node
->symbol
.previous
;
248 node
->symbol
.next
= NULL
;
249 node
->symbol
.previous
= NULL
;
251 slot
= htab_find_slot (symtab_hash
, node
, NO_INSERT
);
254 symtab_node replacement_node
= NULL
;
255 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
256 replacement_node
= (symtab_node
)cgraph_find_replacement_node (cnode
);
257 if (!replacement_node
)
258 htab_clear_slot (symtab_hash
, slot
);
260 *slot
= replacement_node
;
262 unlink_from_assembler_name_hash (node
);
265 /* Return symbol table node associated with DECL, if any,
266 and NULL otherwise. */
269 symtab_get_node (const_tree decl
)
272 struct symtab_node_base key
;
274 gcc_checking_assert (TREE_CODE (decl
) == FUNCTION_DECL
275 || (TREE_CODE (decl
) == VAR_DECL
276 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)
282 key
.decl
= CONST_CAST2 (tree
, const_tree
, decl
);
284 slot
= (symtab_node
*) htab_find_slot (symtab_hash
, &key
,
292 /* Remove symtab NODE from the symbol table. */
295 symtab_remove_node (symtab_node node
)
297 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
298 cgraph_remove_node (cnode
);
299 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
300 varpool_remove_node (vnode
);
303 /* Initalize asm name hash unless. */
306 symtab_initialize_asm_name_hash (void)
309 if (!assembler_name_hash
)
311 assembler_name_hash
=
312 htab_create_ggc (10, hash_node_by_assembler_name
, eq_assembler_name
,
314 FOR_EACH_SYMBOL (node
)
315 insert_to_assembler_name_hash (node
);
319 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
320 Return NULL if there's no such node. */
323 symtab_node_for_asm (const_tree asmname
)
328 symtab_initialize_asm_name_hash ();
329 slot
= htab_find_slot_with_hash (assembler_name_hash
, asmname
,
330 decl_assembler_name_hash (asmname
),
335 node
= (symtab_node
) *slot
;
341 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
344 change_decl_assembler_name (tree decl
, tree name
)
346 symtab_node node
= NULL
;
348 /* We can have user ASM names on things, like global register variables, that
349 are not in the symbol table. */
350 if ((TREE_CODE (decl
) == VAR_DECL
351 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
352 || TREE_CODE (decl
) == FUNCTION_DECL
)
353 node
= symtab_get_node (decl
);
354 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
356 SET_DECL_ASSEMBLER_NAME (decl
, name
);
358 insert_to_assembler_name_hash (node
);
362 if (name
== DECL_ASSEMBLER_NAME (decl
))
366 unlink_from_assembler_name_hash (node
);
367 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
368 && DECL_RTL_SET_P (decl
))
369 warning (0, "%D renamed after being referenced in assembly", decl
);
371 SET_DECL_ASSEMBLER_NAME (decl
, name
);
373 insert_to_assembler_name_hash (node
);
377 /* Add NEW_ to the same comdat group that OLD is in. */
380 symtab_add_to_same_comdat_group (symtab_node new_node
,
381 symtab_node old_node
)
383 gcc_assert (DECL_ONE_ONLY (old_node
->symbol
.decl
));
384 gcc_assert (!new_node
->symbol
.same_comdat_group
);
385 gcc_assert (new_node
!= old_node
);
387 DECL_COMDAT_GROUP (new_node
->symbol
.decl
) = DECL_COMDAT_GROUP (old_node
->symbol
.decl
);
388 new_node
->symbol
.same_comdat_group
= old_node
;
389 if (!old_node
->symbol
.same_comdat_group
)
390 old_node
->symbol
.same_comdat_group
= new_node
;
394 for (n
= old_node
->symbol
.same_comdat_group
;
395 n
->symbol
.same_comdat_group
!= old_node
;
396 n
= n
->symbol
.same_comdat_group
)
398 n
->symbol
.same_comdat_group
= new_node
;
402 /* Dissolve the same_comdat_group list in which NODE resides. */
405 symtab_dissolve_same_comdat_group_list (symtab_node node
)
407 symtab_node n
= node
, next
;
409 if (!node
->symbol
.same_comdat_group
)
413 next
= n
->symbol
.same_comdat_group
;
414 n
->symbol
.same_comdat_group
= NULL
;
420 /* Return printable assembler name of NODE.
421 This function is used only for debugging. When assembler name
422 is unknown go with identifier name. */
425 symtab_node_asm_name (symtab_node node
)
427 if (!DECL_ASSEMBLER_NAME_SET_P (node
->symbol
.decl
))
428 return lang_hooks
.decl_printable_name (node
->symbol
.decl
, 2);
429 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->symbol
.decl
));
432 /* Return printable identifier name. */
435 symtab_node_name (symtab_node node
)
437 return lang_hooks
.decl_printable_name (node
->symbol
.decl
, 2);
440 static const char * const symtab_type_names
[] = {"symbol", "function", "variable"};
442 /* Dump base fields of symtab nodes. Not to be used directly. */
445 dump_symtab_base (FILE *f
, symtab_node node
)
447 static const char * const visibility_types
[] = {
448 "default", "protected", "hidden", "internal"
451 fprintf (f
, "%s/%i (%s)",
452 symtab_node_asm_name (node
),
454 symtab_node_name (node
));
455 dump_addr (f
, " @", (void *)node
);
456 fprintf (f
, "\n Type: %s\n", symtab_type_names
[node
->symbol
.type
]);
457 fprintf (f
, " Visibility:");
459 if (node
->symbol
.in_other_partition
)
460 fprintf (f
, " in_other_partition");
461 if (node
->symbol
.used_from_other_partition
)
462 fprintf (f
, " used_from_other_partition");
463 if (node
->symbol
.force_output
)
464 fprintf (f
, " force_output");
465 if (node
->symbol
.resolution
!= LDPR_UNKNOWN
)
467 ld_plugin_symbol_resolution_names
[(int)node
->symbol
.resolution
]);
468 if (TREE_ASM_WRITTEN (node
->symbol
.decl
))
469 fprintf (f
, " asm_written");
470 if (DECL_EXTERNAL (node
->symbol
.decl
))
471 fprintf (f
, " external");
472 if (TREE_PUBLIC (node
->symbol
.decl
))
473 fprintf (f
, " public");
474 if (DECL_COMMON (node
->symbol
.decl
))
475 fprintf (f
, " common");
476 if (DECL_WEAK (node
->symbol
.decl
))
477 fprintf (f
, " weak");
478 if (DECL_DLLIMPORT_P (node
->symbol
.decl
))
479 fprintf (f
, " dll_import");
480 if (DECL_COMDAT (node
->symbol
.decl
))
481 fprintf (f
, " comdat");
482 if (DECL_COMDAT_GROUP (node
->symbol
.decl
))
483 fprintf (f
, " comdat_group:%s",
484 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node
->symbol
.decl
)));
485 if (DECL_ONE_ONLY (node
->symbol
.decl
))
486 fprintf (f
, " one_only");
487 if (DECL_SECTION_NAME (node
->symbol
.decl
))
488 fprintf (f
, " section_name:%s",
489 TREE_STRING_POINTER (DECL_SECTION_NAME (node
->symbol
.decl
)));
490 if (DECL_VISIBILITY_SPECIFIED (node
->symbol
.decl
))
491 fprintf (f
, " visibility_specified");
492 if (DECL_VISIBILITY (node
->symbol
.decl
))
493 fprintf (f
, " visibility:%s",
494 visibility_types
[DECL_VISIBILITY (node
->symbol
.decl
)]);
495 if (DECL_VIRTUAL_P (node
->symbol
.decl
))
496 fprintf (f
, " virtual");
497 if (DECL_ARTIFICIAL (node
->symbol
.decl
))
498 fprintf (f
, " artificial");
499 if (TREE_CODE (node
->symbol
.decl
) == FUNCTION_DECL
)
501 if (DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
))
502 fprintf (f
, " constructor");
503 if (DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
))
504 fprintf (f
, " destructor");
508 if (node
->symbol
.same_comdat_group
)
509 fprintf (f
, " Same comdat group as: %s/%i\n",
510 symtab_node_asm_name (node
->symbol
.same_comdat_group
),
511 node
->symbol
.same_comdat_group
->symbol
.order
);
512 if (node
->symbol
.next_sharing_asm_name
)
513 fprintf (f
, " next sharing asm name: %i\n",
514 node
->symbol
.next_sharing_asm_name
->symbol
.order
);
515 if (node
->symbol
.previous_sharing_asm_name
)
516 fprintf (f
, " previous sharing asm name: %i\n",
517 node
->symbol
.previous_sharing_asm_name
->symbol
.order
);
519 if (node
->symbol
.address_taken
)
520 fprintf (f
, " Address is taken.\n");
521 if (node
->symbol
.aux
)
523 fprintf (f
, " Aux:");
524 dump_addr (f
, " @", (void *)node
->symbol
.aux
);
527 fprintf (f
, " References: ");
528 ipa_dump_references (f
, &node
->symbol
.ref_list
);
529 fprintf (f
, " Referring: ");
530 ipa_dump_referring (f
, &node
->symbol
.ref_list
);
531 if (node
->symbol
.lto_file_data
)
532 fprintf (f
, " Read from file: %s\n",
533 node
->symbol
.lto_file_data
->file_name
);
536 /* Dump symtab node. */
539 dump_symtab_node (FILE *f
, symtab_node node
)
541 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
542 dump_cgraph_node (f
, cnode
);
543 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
544 dump_varpool_node (f
, vnode
);
547 /* Dump symbol table. */
550 dump_symtab (FILE *f
)
553 fprintf (f
, "Symbol table:\n\n");
554 FOR_EACH_SYMBOL (node
)
555 dump_symtab_node (f
, node
);
558 /* Dump symtab node NODE to stderr. */
561 debug_symtab_node (symtab_node node
)
563 dump_symtab_node (stderr
, node
);
566 /* Dump symbol table to stderr. */
571 dump_symtab (stderr
);
574 /* Verify common part of symtab nodes. */
577 verify_symtab_base (symtab_node node
)
579 bool error_found
= false;
580 symtab_node hashed_node
;
582 if (is_a
<cgraph_node
> (node
))
584 if (TREE_CODE (node
->symbol
.decl
) != FUNCTION_DECL
)
586 error ("function symbol is not function");
590 else if (is_a
<varpool_node
> (node
))
592 if (TREE_CODE (node
->symbol
.decl
) != VAR_DECL
)
594 error ("variable symbol is not variable");
600 error ("node has unknown type");
604 hashed_node
= symtab_get_node (node
->symbol
.decl
);
607 error ("node not found in symtab decl hashtable");
610 if (assembler_name_hash
)
612 hashed_node
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (node
->symbol
.decl
));
613 if (hashed_node
&& hashed_node
->symbol
.previous_sharing_asm_name
)
615 error ("assembler name hash list corrupted");
620 if (hashed_node
== node
)
622 hashed_node
= hashed_node
->symbol
.next_sharing_asm_name
;
625 && !(is_a
<varpool_node
> (node
)
626 || DECL_HARD_REGISTER (node
->symbol
.decl
)))
628 error ("node not found in symtab assembler name hash");
632 if (node
->symbol
.previous_sharing_asm_name
633 && node
->symbol
.previous_sharing_asm_name
->symbol
.next_sharing_asm_name
!= node
)
635 error ("double linked list of assembler names corrupted");
637 if (node
->symbol
.same_comdat_group
)
639 symtab_node n
= node
->symbol
.same_comdat_group
;
641 if (!DECL_ONE_ONLY (n
->symbol
.decl
))
643 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
646 if (n
->symbol
.type
!= node
->symbol
.type
)
648 error ("mixing different types of symbol in same comdat groups is not supported");
653 error ("node is alone in a comdat group");
658 if (!n
->symbol
.same_comdat_group
)
660 error ("same_comdat_group is not a circular list");
664 n
= n
->symbol
.same_comdat_group
;
671 /* Verify consistency of NODE. */
674 verify_symtab_node (symtab_node node
)
679 timevar_push (TV_CGRAPH_VERIFY
);
680 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
681 verify_cgraph_node (cnode
);
683 if (verify_symtab_base (node
))
685 dump_symtab_node (stderr
, node
);
686 internal_error ("verify_symtab_node failed");
688 timevar_pop (TV_CGRAPH_VERIFY
);
691 /* Verify symbol table for internal consistency. */
697 FOR_EACH_SYMBOL (node
)
698 verify_symtab_node (node
);
701 /* Return true when RESOLUTION indicate that linker will use
702 the symbol from non-LTO object files. */
705 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution
)
707 return (resolution
== LDPR_PREVAILING_DEF
708 || resolution
== LDPR_PREEMPTED_REG
709 || resolution
== LDPR_RESOLVED_EXEC
710 || resolution
== LDPR_RESOLVED_DYN
);
713 /* Return true when NODE is known to be used from other (non-LTO) object file.
714 Known only when doing LTO via linker plugin. */
717 symtab_used_from_object_file_p (symtab_node node
)
719 if (!TREE_PUBLIC (node
->symbol
.decl
) || DECL_EXTERNAL (node
->symbol
.decl
))
721 if (resolution_used_from_other_file_p (node
->symbol
.resolution
))
726 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
727 but other code such as notice_global_symbol generates rtl. */
729 symtab_make_decl_local (tree decl
)
733 if (TREE_CODE (decl
) == VAR_DECL
)
734 DECL_COMMON (decl
) = 0;
735 else gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
737 if (DECL_ONE_ONLY (decl
) || DECL_COMDAT (decl
))
739 /* It is possible that we are linking against library defining same COMDAT
740 function. To avoid conflict we need to rename our local name of the
741 function just in the case WHOPR partitioning decide to make it hidden
742 to avoid cross partition references. */
745 const char *old_name
;
746 symtab_node node
= symtab_get_node (decl
);
747 old_name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
748 change_decl_assembler_name (decl
,
749 clone_function_name (decl
, "local"));
750 if (node
->symbol
.lto_file_data
)
751 lto_record_renamed_decl (node
->symbol
.lto_file_data
,
754 (DECL_ASSEMBLER_NAME (decl
)));
756 DECL_SECTION_NAME (decl
) = 0;
757 DECL_COMDAT (decl
) = 0;
759 DECL_COMDAT_GROUP (decl
) = 0;
760 DECL_WEAK (decl
) = 0;
761 DECL_EXTERNAL (decl
) = 0;
762 DECL_VISIBILITY_SPECIFIED (decl
) = 0;
763 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
764 TREE_PUBLIC (decl
) = 0;
765 DECL_VISIBILITY_SPECIFIED (decl
) = 0;
766 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
767 if (!DECL_RTL_SET_P (decl
))
770 /* Update rtl flags. */
771 make_decl_rtl (decl
);
773 rtl
= DECL_RTL (decl
);
777 symbol
= XEXP (rtl
, 0);
778 if (GET_CODE (symbol
) != SYMBOL_REF
)
781 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
783 #include "gt-symtab.h"