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
, bool with_clones
)
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 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
),
122 gcc_assert (*aslot
!= node
);
123 node
->symbol
.next_sharing_asm_name
= (symtab_node
)*aslot
;
125 ((symtab_node
)*aslot
)->symbol
.previous_sharing_asm_name
= 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. */
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
;
158 tree name
= DECL_ASSEMBLER_NAME (node
->symbol
.decl
);
160 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
161 decl_assembler_name_hash (name
),
163 gcc_assert (*slot
== node
);
164 if (!node
->symbol
.next_sharing_asm_name
)
165 htab_clear_slot (assembler_name_hash
, slot
);
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. */
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. */
195 symtab_register_node (symtab_node node
)
197 struct symtab_node_base key
;
200 node
->symbol
.next
= symtab_nodes
;
201 node
->symbol
.previous
= NULL
;
203 symtab_nodes
->symbol
.previous
= node
;
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
);
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
226 symtab_insert_node_to_hashtable (symtab_node node
)
228 struct symtab_node_base key
;
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
);
238 /* Remove node from symbol table. This function is not used directly, but via
239 cgraph/varpool node removal routines. */
242 symtab_unregister_node (symtab_node node
)
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
)
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
;
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
;
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
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
);
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. */
294 symtab_get_node (const_tree decl
)
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
)
311 key
.decl
= CONST_CAST2 (tree
, const_tree
, decl
);
313 slot
= (symtab_node
*) htab_find_slot (symtab_hash
, &key
,
321 /* Remove symtab NODE from the symbol table. */
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. */
335 symtab_initialize_asm_name_hash (void)
338 if (!assembler_name_hash
)
340 assembler_name_hash
=
341 htab_create_ggc (10, hash_node_by_assembler_name
, eq_assembler_name
,
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. */
352 symtab_node_for_asm (const_tree asmname
)
357 symtab_initialize_asm_name_hash ();
358 slot
= htab_find_slot_with_hash (assembler_name_hash
, asmname
,
359 decl_assembler_name_hash (asmname
),
364 node
= (symtab_node
) *slot
;
370 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
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
);
387 insert_to_assembler_name_hash (node
, true);
391 if (name
== DECL_ASSEMBLER_NAME (decl
))
394 tree alias
= (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl
))
395 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl
))
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
);
406 IDENTIFIER_TRANSPARENT_ALIAS (name
) = 1;
407 TREE_CHAIN (DECL_ASSEMBLER_NAME (name
)) = alias
;
410 insert_to_assembler_name_hash (node
, true);
414 /* Add NEW_ to the same comdat group that OLD is in. */
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
;
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. */
442 symtab_dissolve_same_comdat_group_list (symtab_node node
)
444 symtab_node n
= node
, next
;
446 if (!node
->symbol
.same_comdat_group
)
450 next
= n
->symbol
.same_comdat_group
;
451 n
->symbol
.same_comdat_group
= NULL
;
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. */
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. */
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. */
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
),
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
)
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");
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. */
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. */
607 dump_symtab (FILE *f
)
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. */
618 debug_symtab_node (symtab_node node
)
620 dump_symtab_node (stderr
, node
);
623 /* Dump symbol table to stderr. */
628 dump_symtab (stderr
);
631 /* Verify common part of symtab nodes. */
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");
647 else if (is_a
<varpool_node
> (node
))
649 if (TREE_CODE (node
->symbol
.decl
) != VAR_DECL
)
651 error ("variable symbol is not variable");
657 error ("node has unknown type");
661 if (cgraph_state
!= CGRAPH_LTO_STREAMING
)
663 hashed_node
= symtab_get_node (node
->symbol
.decl
);
666 error ("node not found in symtab decl hashtable");
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");
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");
689 if (hashed_node
== node
)
691 hashed_node
= hashed_node
->symbol
.next_sharing_asm_name
;
694 && !(is_a
<varpool_node
> (node
)
695 || DECL_HARD_REGISTER (node
->symbol
.decl
)))
697 error ("node not found in symtab assembler name hash");
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");
707 if (node
->symbol
.analyzed
&& !node
->symbol
.definition
)
709 error ("node is analyzed byt it is not a definition");
712 if (node
->symbol
.cpp_implicit_alias
&& !node
->symbol
.alias
)
714 error ("node is alias but not implicit alias");
717 if (node
->symbol
.alias
&& !node
->symbol
.definition
718 && !node
->symbol
.weakref
)
720 error ("node is alias but not definition");
723 if (node
->symbol
.weakref
&& !node
->symbol
.alias
)
725 error ("node is weakref but not an alias");
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");
737 if (n
->symbol
.type
!= node
->symbol
.type
)
739 error ("mixing different types of symbol in same comdat groups is not supported");
744 error ("node is alone in a comdat group");
749 if (!n
->symbol
.same_comdat_group
)
751 error ("same_comdat_group is not a circular list");
755 n
= n
->symbol
.same_comdat_group
;
762 /* Verify consistency of NODE. */
765 verify_symtab_node (symtab_node node
)
770 timevar_push (TV_CGRAPH_VERIFY
);
771 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
772 verify_cgraph_node (cnode
);
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. */
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. */
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. */
808 symtab_used_from_object_file_p (symtab_node node
)
810 if (!TREE_PUBLIC (node
->symbol
.decl
) || DECL_EXTERNAL (node
->symbol
.decl
))
812 if (resolution_used_from_other_file_p (node
->symbol
.resolution
))
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. */
821 symtab_make_decl_local (tree decl
)
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
))
843 /* Update rtl flags. */
844 make_decl_rtl (decl
);
846 rtl
= DECL_RTL (decl
);
850 symbol
= XEXP (rtl
, 0);
851 if (GET_CODE (symbol
) != SYMBOL_REF
)
854 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
857 /* Return availability of NODE. */
860 symtab_node_availability (symtab_node node
)
862 if (is_a
<cgraph_node
> (node
))
863 return cgraph_function_body_availability (cgraph (node
));
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. */
873 symtab_alias_ultimate_target (symtab_node node
, enum availability
*availability
)
875 bool weakref_p
= false;
877 if (!node
->symbol
.alias
)
880 *availability
= symtab_node_availability (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. */
900 weakref_p
= node
->symbol
.weakref
;
902 *availability
= symtab_node_availability (node
);
904 *availability
= AVAIL_LOCAL
;
908 if (node
->symbol
.alias
&& node
->symbol
.analyzed
)
909 node
= symtab_alias_target (node
);
914 else if (node
->symbol
.analyzed
)
918 enum availability a
= symtab_node_availability (node
);
919 if (a
< *availability
)
924 *availability
= AVAIL_NOT_AVAILABLE
;
927 if (node
&& availability
&& weakref_p
)
929 enum availability a
= symtab_node_availability (node
);
930 if (a
< *availability
)
932 weakref_p
= node
->symbol
.weakref
;
936 *availability
= AVAIL_NOT_AVAILABLE
;
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. */
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
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
984 symtab_resolve_alias (symtab_node node
, symtab_node target
)
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
)
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
);
1003 node
->symbol
.alias
= 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;
1027 /* Call calback on NODE and aliases associated to NODE.
1028 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1032 symtab_for_node_and_aliases (symtab_node node
,
1033 bool (*callback
) (symtab_node
, void *),
1035 bool include_overwritable
)
1038 struct ipa_ref
*ref
;
1040 if (callback (node
, data
))
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
))
1055 /* Worker searching nonoverwritable alias. */
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
;
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. */
1073 symtab_nonoverwritable_alias (symtab_node node
)
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);
1086 #ifndef ASM_OUTPUT_DEF
1087 /* If aliases aren't supported by the assembler, fail. */
1091 /* Otherwise create a new one. */
1092 new_decl
= copy_node (node
->symbol
.decl
);
1093 DECL_NAME (new_decl
) = clone_function_name (node
->symbol
.decl
, "localalias");
1094 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1095 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
1096 DECL_INITIAL (new_decl
) = NULL
;
1097 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
1098 SET_DECL_RTL (new_decl
, NULL
);
1100 /* Update the properties. */
1101 DECL_EXTERNAL (new_decl
) = 0;
1102 if (DECL_ONE_ONLY (node
->symbol
.decl
))
1103 DECL_SECTION_NAME (new_decl
) = NULL
;
1104 DECL_COMDAT_GROUP (new_decl
) = 0;
1105 TREE_PUBLIC (new_decl
) = 0;
1106 DECL_COMDAT (new_decl
) = 0;
1107 DECL_WEAK (new_decl
) = 0;
1108 DECL_VIRTUAL_P (new_decl
) = 0;
1109 if (TREE_CODE (new_decl
) == FUNCTION_DECL
)
1111 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
1112 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
1113 new_node
= (symtab_node
) cgraph_create_function_alias
1114 (new_decl
, node
->symbol
.decl
);
1117 new_node
= (symtab_node
) varpool_create_variable_alias (new_decl
,
1119 symtab_resolve_alias (new_node
, node
);
1120 gcc_assert (decl_binds_to_current_def_p (new_decl
));
1124 /* Return true if A and B represents semantically equivalent symbols. */
1127 symtab_semantically_equivalent_p (symtab_node a
,
1130 enum availability avail
;
1133 /* Equivalent functions are equivalent. */
1134 if (a
->symbol
.decl
== b
->symbol
.decl
)
1137 /* If symbol is not overwritable by different implementation,
1138 walk to the base object it defines. */
1139 ba
= symtab_alias_ultimate_target (a
, &avail
);
1140 if (avail
>= AVAIL_AVAILABLE
)
1147 bb
= symtab_alias_ultimate_target (b
, &avail
);
1148 if (avail
>= AVAIL_AVAILABLE
)
1157 #include "gt-symtab.h"