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
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 gcc_checking_assert (!node
->symbol
.previous_sharing_asm_name
108 && !node
->symbol
.next_sharing_asm_name
);
109 if (assembler_name_hash
)
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
),
117 gcc_assert (*aslot
!= node
);
118 node
->symbol
.next_sharing_asm_name
= (symtab_node
)*aslot
;
120 ((symtab_node
)*aslot
)->symbol
.previous_sharing_asm_name
= node
;
126 /* Remove NODE from assembler name hash. */
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
;
143 tree name
= DECL_ASSEMBLER_NAME (node
->symbol
.decl
);
145 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
146 decl_assembler_name_hash (name
),
148 gcc_assert (*slot
== node
);
149 if (!node
->symbol
.next_sharing_asm_name
)
150 htab_clear_slot (assembler_name_hash
, slot
);
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. */
162 symtab_register_node (symtab_node node
)
164 struct symtab_node_base key
;
167 node
->symbol
.next
= symtab_nodes
;
168 node
->symbol
.previous
= NULL
;
170 symtab_nodes
->symbol
.previous
= node
;
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
);
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
191 symtab_insert_node_to_hashtable (symtab_node node
)
193 struct symtab_node_base key
;
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
);
203 /* Remove node from symbol table. This function is not used directly, but via
204 cgraph/varpool node removal routines. */
207 symtab_unregister_node (symtab_node node
)
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
)
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
;
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
;
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
);
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
);
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. */
254 symtab_get_node (const_tree decl
)
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
)
267 key
.decl
= CONST_CAST2 (tree
, const_tree
, decl
);
269 slot
= (symtab_node
*) htab_find_slot (symtab_hash
, &key
,
277 /* Remove symtab NODE from the symbol table. */
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. */
292 symtab_node_for_asm (const_tree asmname
)
297 if (!assembler_name_hash
)
299 assembler_name_hash
=
300 htab_create_ggc (10, hash_node_by_assembler_name
, eq_assembler_name
,
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
),
312 node
= (symtab_node
) *slot
;
318 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
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
);
335 insert_to_assembler_name_hash (node
);
339 if (name
== DECL_ASSEMBLER_NAME (decl
))
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
);
350 insert_to_assembler_name_hash (node
);
354 /* Add NEW_ to the same comdat group that OLD is in. */
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
;
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. */
382 symtab_dissolve_same_comdat_group_list (symtab_node node
)
384 symtab_node n
= node
, next
;
386 if (!node
->symbol
.same_comdat_group
)
390 next
= n
->symbol
.same_comdat_group
;
391 n
->symbol
.same_comdat_group
= NULL
;
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. */
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. */
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. */
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
),
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
)
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");
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. */
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. */
524 dump_symtab (FILE *f
)
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. */
535 debug_symtab_node (symtab_node node
)
537 dump_symtab_node (stderr
, node
);
540 /* Dump symbol table to stderr. */
545 dump_symtab (stderr
);
548 /* Verify common part of symtab nodes. */
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");
564 else if (symtab_variable_p (node
))
566 if (TREE_CODE (node
->symbol
.decl
) != VAR_DECL
)
568 error ("variable symbol is not variable");
574 error ("node has unknown type");
578 hashed_node
= symtab_get_node (node
->symbol
.decl
);
581 error ("node not found in symtab decl hashtable");
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");
594 if (hashed_node
== node
)
596 hashed_node
= hashed_node
->symbol
.next_sharing_asm_name
;
600 error ("node not found in symtab assembler name hash");
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");
618 if (n
->symbol
.type
!= node
->symbol
.type
)
620 error ("mixing different types of symbol in same comdat groups is not supported");
625 error ("node is alone in a comdat group");
630 if (!n
->symbol
.same_comdat_group
)
632 error ("same_comdat_group is not a circular list");
636 n
= n
->symbol
.same_comdat_group
;
643 /* Verify consistency of NODE. */
646 verify_symtab_node (symtab_node node
)
651 timevar_push (TV_CGRAPH_VERIFY
);
652 if (symtab_function_p (node
))
653 verify_cgraph_node (cgraph (node
));
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. */
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. */
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. */
689 symtab_used_from_object_file_p (symtab_node node
)
691 if (!TREE_PUBLIC (node
->symbol
.decl
) || DECL_EXTERNAL (node
->symbol
.decl
))
693 if (resolution_used_from_other_file_p (node
->symbol
.resolution
))
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. */
701 symtab_make_decl_local (tree decl
)
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. */
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
,
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
))
738 /* Update rtl flags. */
739 make_decl_rtl (decl
);
741 rtl
= DECL_RTL (decl
);
745 symbol
= XEXP (rtl
, 0);
746 if (GET_CODE (symbol
) != SYMBOL_REF
)
749 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
751 #include "gt-symtab.h"