Fix missing SCNuMAX defines in inttypes.h on hpux11.[01]*
[official-gcc.git] / gcc / symtab.c
blobf141200a452159269cc35ce5af087e5a70f60280
1 /* Symbol table.
2 Copyright (C) 2012-2020 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 "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "timevar.h"
30 #include "cgraph.h"
31 #include "lto-streamer.h"
32 #include "print-tree.h"
33 #include "varasm.h"
34 #include "langhooks.h"
35 #include "output.h"
36 #include "ipa-utils.h"
37 #include "calls.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "builtins.h"
42 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
44 const char * const ld_plugin_symbol_resolution_names[]=
46 "",
47 "undef",
48 "prevailing_def",
49 "prevailing_def_ironly",
50 "preempted_reg",
51 "preempted_ir",
52 "resolved_ir",
53 "resolved_exec",
54 "resolved_dyn",
55 "prevailing_def_ironly_exp"
58 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at ALIAS
59 until we find an identifier that is not itself a transparent alias. */
61 static inline tree
62 ultimate_transparent_alias_target (tree alias)
64 tree target = alias;
66 while (IDENTIFIER_TRANSPARENT_ALIAS (target))
68 gcc_checking_assert (TREE_CHAIN (target));
69 target = TREE_CHAIN (target);
71 gcc_checking_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
72 && ! TREE_CHAIN (target));
74 return target;
78 /* Hash asmnames ignoring the user specified marks. */
80 hashval_t
81 symbol_table::decl_assembler_name_hash (const_tree asmname)
83 if (IDENTIFIER_POINTER (asmname)[0] == '*')
85 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
86 size_t ulp_len = strlen (user_label_prefix);
88 if (ulp_len == 0)
90 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
91 decl_str += ulp_len;
93 return htab_hash_string (decl_str);
96 return htab_hash_string (IDENTIFIER_POINTER (asmname));
99 /* Return true if assembler names NAME1 and NAME2 leads to the same symbol
100 name. */
102 bool
103 symbol_table::assembler_names_equal_p (const char *name1, const char *name2)
105 if (name1 != name2)
107 if (name1[0] == '*')
109 size_t ulp_len = strlen (user_label_prefix);
111 name1 ++;
113 if (ulp_len == 0)
115 else if (strncmp (name1, user_label_prefix, ulp_len) == 0)
116 name1 += ulp_len;
117 else
118 return false;
120 if (name2[0] == '*')
122 size_t ulp_len = strlen (user_label_prefix);
124 name2 ++;
126 if (ulp_len == 0)
128 else if (strncmp (name2, user_label_prefix, ulp_len) == 0)
129 name2 += ulp_len;
130 else
131 return false;
133 return !strcmp (name1, name2);
135 return true;
138 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
140 bool
141 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
143 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
144 const char *decl_str;
145 const char *asmname_str;
147 if (decl_asmname == asmname)
148 return true;
150 decl_str = IDENTIFIER_POINTER (decl_asmname);
151 asmname_str = IDENTIFIER_POINTER (asmname);
152 return assembler_names_equal_p (decl_str, asmname_str);
156 /* Returns nonzero if P1 and P2 are equal. */
158 /* Insert NODE to assembler name hash. */
160 void
161 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
162 bool with_clones)
164 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
165 return;
166 gcc_checking_assert (!node->previous_sharing_asm_name
167 && !node->next_sharing_asm_name);
168 if (assembler_name_hash)
170 symtab_node **aslot;
171 cgraph_node *cnode;
172 tree decl = node->decl;
174 tree name = DECL_ASSEMBLER_NAME (node->decl);
176 /* C++ FE can produce decls without associated assembler name and insert
177 them to symtab to hold section or TLS information. */
178 if (!name)
179 return;
181 hashval_t hash = decl_assembler_name_hash (name);
182 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
183 gcc_assert (*aslot != node);
184 node->next_sharing_asm_name = (symtab_node *)*aslot;
185 if (*aslot != NULL)
186 (*aslot)->previous_sharing_asm_name = node;
187 *aslot = node;
189 /* Update also possible inline clones sharing a decl. */
190 cnode = dyn_cast <cgraph_node *> (node);
191 if (cnode && cnode->clones && with_clones)
192 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
193 if (cnode->decl == decl)
194 insert_to_assembler_name_hash (cnode, true);
199 /* Remove NODE from assembler name hash. */
201 void
202 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
203 bool with_clones)
205 if (assembler_name_hash)
207 cgraph_node *cnode;
208 tree decl = node->decl;
210 if (node->next_sharing_asm_name)
211 node->next_sharing_asm_name->previous_sharing_asm_name
212 = node->previous_sharing_asm_name;
213 if (node->previous_sharing_asm_name)
215 node->previous_sharing_asm_name->next_sharing_asm_name
216 = node->next_sharing_asm_name;
218 else
220 tree name = DECL_ASSEMBLER_NAME (node->decl);
221 symtab_node **slot;
223 if (!name)
224 return;
226 hashval_t hash = decl_assembler_name_hash (name);
227 slot = assembler_name_hash->find_slot_with_hash (name, hash,
228 NO_INSERT);
229 gcc_assert (*slot == node);
230 if (!node->next_sharing_asm_name)
231 assembler_name_hash->clear_slot (slot);
232 else
233 *slot = node->next_sharing_asm_name;
235 node->next_sharing_asm_name = NULL;
236 node->previous_sharing_asm_name = NULL;
238 /* Update also possible inline clones sharing a decl. */
239 cnode = dyn_cast <cgraph_node *> (node);
240 if (cnode && cnode->clones && with_clones)
241 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
242 if (cnode->decl == decl)
243 unlink_from_assembler_name_hash (cnode, true);
247 /* Arrange node to be first in its entry of assembler_name_hash. */
249 void
250 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
252 unlink_from_assembler_name_hash (node, false);
253 insert_to_assembler_name_hash (node, false);
256 /* Initialize asm name hash unless. */
258 void
259 symbol_table::symtab_initialize_asm_name_hash (void)
261 symtab_node *node;
262 if (!assembler_name_hash)
264 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
265 FOR_EACH_SYMBOL (node)
266 insert_to_assembler_name_hash (node, false);
270 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
272 void
273 symbol_table::change_decl_assembler_name (tree decl, tree name)
275 symtab_node *node = NULL;
277 /* We can have user ASM names on things, like global register variables, that
278 are not in the symbol table. */
279 if ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
280 || TREE_CODE (decl) == FUNCTION_DECL)
281 node = symtab_node::get (decl);
282 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
284 SET_DECL_ASSEMBLER_NAME (decl, name);
285 if (node)
286 insert_to_assembler_name_hash (node, true);
288 else
290 if (name == DECL_ASSEMBLER_NAME (decl))
291 return;
293 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
294 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
295 : NULL);
296 if (node)
297 unlink_from_assembler_name_hash (node, true);
299 const char *old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
300 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
301 && DECL_RTL_SET_P (decl))
302 warning (0, "%qD renamed after being referenced in assembly", decl);
304 SET_DECL_ASSEMBLER_NAME (decl, name);
305 if (alias)
307 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
308 TREE_CHAIN (name) = alias;
310 /* If we change assembler name, also all transparent aliases must
311 be updated. There are three kinds - those having same assembler name,
312 those being renamed in varasm.c and weakref being renamed by the
313 assembler. */
314 if (node)
316 insert_to_assembler_name_hash (node, true);
317 ipa_ref *ref;
318 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
320 struct symtab_node *alias = ref->referring;
321 if (alias->transparent_alias && !alias->weakref
322 && symbol_table::assembler_names_equal_p
323 (old_name, IDENTIFIER_POINTER (
324 DECL_ASSEMBLER_NAME (alias->decl))))
325 change_decl_assembler_name (alias->decl, name);
326 else if (alias->transparent_alias
327 && IDENTIFIER_TRANSPARENT_ALIAS (alias->decl))
329 gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl))
330 && IDENTIFIER_TRANSPARENT_ALIAS
331 (DECL_ASSEMBLER_NAME (alias->decl)));
333 TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) =
334 ultimate_transparent_alias_target
335 (DECL_ASSEMBLER_NAME (node->decl));
337 #ifdef ASM_OUTPUT_WEAKREF
338 else gcc_assert (!alias->transparent_alias || alias->weakref);
339 #else
340 else gcc_assert (!alias->transparent_alias);
341 #endif
343 gcc_assert (!node->transparent_alias || !node->definition
344 || node->weakref
345 || TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
346 || symbol_table::assembler_names_equal_p
347 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
348 IDENTIFIER_POINTER
349 (DECL_ASSEMBLER_NAME
350 (node->get_alias_target ()->decl))));
355 /* Hash sections by their names. */
357 hashval_t
358 section_name_hasher::hash (section_hash_entry *n)
360 return htab_hash_string (n->name);
363 /* Return true if section P1 name equals to P2. */
365 bool
366 section_name_hasher::equal (section_hash_entry *n1, const char *name)
368 return n1->name == name || !strcmp (n1->name, name);
371 /* Add node into symbol table. This function is not used directly, but via
372 cgraph/varpool node creation routines. */
374 void
375 symtab_node::register_symbol (void)
377 symtab->register_symbol (this);
379 if (!decl->decl_with_vis.symtab_node)
380 decl->decl_with_vis.symtab_node = this;
382 ref_list.clear ();
384 /* Be sure to do this last; C++ FE might create new nodes via
385 DECL_ASSEMBLER_NAME langhook! */
386 symtab->insert_to_assembler_name_hash (this, false);
389 /* Remove NODE from same comdat group. */
391 void
392 symtab_node::remove_from_same_comdat_group (void)
394 if (same_comdat_group)
396 symtab_node *prev;
397 for (prev = same_comdat_group;
398 prev->same_comdat_group != this;
399 prev = prev->same_comdat_group)
401 if (same_comdat_group == prev)
402 prev->same_comdat_group = NULL;
403 else
404 prev->same_comdat_group = same_comdat_group;
405 same_comdat_group = NULL;
406 set_comdat_group (NULL);
410 /* Remove node from symbol table. This function is not used directly, but via
411 cgraph/varpool node removal routines. */
413 void
414 symtab_node::unregister (void)
416 remove_all_references ();
417 remove_all_referring ();
419 /* Remove reference to section. */
420 set_section_for_node (NULL);
422 remove_from_same_comdat_group ();
424 symtab->unregister (this);
426 /* During LTO symtab merging we temporarily corrupt decl to symtab node
427 hash. */
428 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
429 if (decl->decl_with_vis.symtab_node == this)
431 symtab_node *replacement_node = NULL;
432 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
433 replacement_node = cnode->find_replacement ();
434 decl->decl_with_vis.symtab_node = replacement_node;
436 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
437 symtab->unlink_from_assembler_name_hash (this, false);
438 if (in_init_priority_hash)
439 symtab->init_priority_hash->remove (this);
443 /* Remove symbol from symbol table. */
445 void
446 symtab_node::remove (void)
448 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
449 cnode->remove ();
450 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
451 vnode->remove ();
454 /* Add NEW_ to the same comdat group that OLD is in. */
456 void
457 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
459 gcc_assert (old_node->get_comdat_group ());
460 gcc_assert (!same_comdat_group);
461 gcc_assert (this != old_node);
463 set_comdat_group (old_node->get_comdat_group ());
464 same_comdat_group = old_node;
465 if (!old_node->same_comdat_group)
466 old_node->same_comdat_group = this;
467 else
469 symtab_node *n;
470 for (n = old_node->same_comdat_group;
471 n->same_comdat_group != old_node;
472 n = n->same_comdat_group)
474 n->same_comdat_group = this;
478 /* Dissolve the same_comdat_group list in which NODE resides. */
480 void
481 symtab_node::dissolve_same_comdat_group_list (void)
483 symtab_node *n = this;
484 symtab_node *next;
486 if (!same_comdat_group)
487 return;
490 next = n->same_comdat_group;
491 n->same_comdat_group = NULL;
492 if (dyn_cast <cgraph_node *> (n))
493 dyn_cast <cgraph_node *> (n)->calls_comdat_local = false;
494 /* Clear comdat_group for comdat locals, since
495 make_decl_local doesn't. */
496 if (!TREE_PUBLIC (n->decl))
497 n->set_comdat_group (NULL);
498 n = next;
500 while (n != this);
503 /* Return printable assembler name of NODE.
504 This function is used only for debugging. When assembler name
505 is unknown go with identifier name. */
507 const char *
508 symtab_node::asm_name () const
510 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
511 return name ();
512 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
515 /* Return printable identifier name. */
517 const char *
518 symtab_node::name () const
520 if (!DECL_NAME (decl))
522 if (DECL_ASSEMBLER_NAME_SET_P (decl))
523 return asm_name ();
524 else
525 return "<unnamed>";
527 return lang_hooks.decl_printable_name (decl, 2);
530 const char *
531 symtab_node::get_dump_name (bool asm_name_p) const
533 #define EXTRA 16
534 const char *fname = asm_name_p ? asm_name () : name ();
535 unsigned l = strlen (fname);
537 char *s = (char *)ggc_internal_cleared_alloc (l + EXTRA);
538 snprintf (s, l + EXTRA, "%s/%d", fname, order);
540 return s;
543 const char *
544 symtab_node::dump_name () const
546 return get_dump_name (false);
549 const char *
550 symtab_node::dump_asm_name () const
552 return get_dump_name (true);
555 /* Return ipa reference from this symtab_node to
556 REFERRED_NODE or REFERRED_VARPOOL_NODE. USE_TYPE specify type
557 of the use. */
559 ipa_ref *
560 symtab_node::create_reference (symtab_node *referred_node,
561 enum ipa_ref_use use_type)
563 return create_reference (referred_node, use_type, NULL);
567 /* Return ipa reference from this symtab_node to
568 REFERRED_NODE or REFERRED_VARPOOL_NODE. USE_TYPE specify type
569 of the use and STMT the statement (if it exists). */
571 ipa_ref *
572 symtab_node::create_reference (symtab_node *referred_node,
573 enum ipa_ref_use use_type, gimple *stmt)
575 ipa_ref *ref = NULL, *ref2 = NULL;
576 ipa_ref_list *list, *list2;
577 ipa_ref_t *old_references;
579 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
580 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
582 list = &ref_list;
583 old_references = vec_safe_address (list->references);
584 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
585 ref = &list->references->last ();
587 list2 = &referred_node->ref_list;
589 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
590 if(use_type == IPA_REF_ALIAS)
592 list2->referring.safe_insert (0, ref);
593 ref->referred_index = 0;
595 for (unsigned int i = 1; i < list2->referring.length (); i++)
596 list2->referring[i]->referred_index = i;
598 else
600 list2->referring.safe_push (ref);
601 ref->referred_index = list2->referring.length () - 1;
604 ref->referring = this;
605 ref->referred = referred_node;
606 ref->stmt = stmt;
607 ref->lto_stmt_uid = 0;
608 ref->speculative_id = 0;
609 ref->use = use_type;
610 ref->speculative = 0;
612 /* If vector was moved in memory, update pointers. */
613 if (old_references != list->references->address ())
615 int i;
616 for (i = 0; iterate_reference(i, ref2); i++)
617 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
619 return ref;
622 ipa_ref *
623 symtab_node::maybe_create_reference (tree val, gimple *stmt)
625 STRIP_NOPS (val);
626 ipa_ref_use use_type;
628 switch (TREE_CODE (val))
630 case VAR_DECL:
631 use_type = IPA_REF_LOAD;
632 break;
633 case ADDR_EXPR:
634 use_type = IPA_REF_ADDR;
635 break;
636 default:
637 gcc_assert (!handled_component_p (val));
638 return NULL;
641 val = get_base_var (val);
642 if (val && VAR_OR_FUNCTION_DECL_P (val))
644 symtab_node *referred = symtab_node::get (val);
645 gcc_checking_assert (referred);
646 return create_reference (referred, use_type, stmt);
648 return NULL;
651 /* Clone all references from symtab NODE to this symtab_node. */
653 void
654 symtab_node::clone_references (symtab_node *node)
656 ipa_ref *ref = NULL, *ref2 = NULL;
657 int i;
658 for (i = 0; node->iterate_reference (i, ref); i++)
660 bool speculative = ref->speculative;
661 unsigned int stmt_uid = ref->lto_stmt_uid;
663 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
664 ref2->speculative = speculative;
665 ref2->lto_stmt_uid = stmt_uid;
666 ref2->speculative_id = ref->speculative_id;
670 /* Clone all referring from symtab NODE to this symtab_node. */
672 void
673 symtab_node::clone_referring (symtab_node *node)
675 ipa_ref *ref = NULL, *ref2 = NULL;
676 int i;
677 for (i = 0; node->iterate_referring(i, ref); i++)
679 bool speculative = ref->speculative;
680 unsigned int stmt_uid = ref->lto_stmt_uid;
682 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
683 ref2->speculative = speculative;
684 ref2->lto_stmt_uid = stmt_uid;
685 ref2->speculative_id = ref->speculative_id;
689 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
691 ipa_ref *
692 symtab_node::clone_reference (ipa_ref *ref, gimple *stmt)
694 bool speculative = ref->speculative;
695 unsigned int stmt_uid = ref->lto_stmt_uid;
696 ipa_ref *ref2;
698 ref2 = create_reference (ref->referred, ref->use, stmt);
699 ref2->speculative = speculative;
700 ref2->lto_stmt_uid = stmt_uid;
701 ref2->speculative_id = ref->speculative_id;
702 return ref2;
705 /* Find the structure describing a reference to REFERRED_NODE
706 and associated with statement STMT. */
708 ipa_ref *
709 symtab_node::find_reference (symtab_node *referred_node,
710 gimple *stmt, unsigned int lto_stmt_uid)
712 ipa_ref *r = NULL;
713 int i;
715 for (i = 0; iterate_reference (i, r); i++)
716 if (r->referred == referred_node
717 && !r->speculative
718 && ((stmt && r->stmt == stmt)
719 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
720 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
721 return r;
722 return NULL;
725 /* Remove all references that are associated with statement STMT. */
727 void
728 symtab_node::remove_stmt_references (gimple *stmt)
730 ipa_ref *r = NULL;
731 int i = 0;
733 while (iterate_reference (i, r))
734 if (r->stmt == stmt)
735 r->remove_reference ();
736 else
737 i++;
740 /* Remove all stmt references in non-speculative references.
741 Those are not maintained during inlining & cloning.
742 The exception are speculative references that are updated along
743 with callgraph edges associated with them. */
745 void
746 symtab_node::clear_stmts_in_references (void)
748 ipa_ref *r = NULL;
749 int i;
751 for (i = 0; iterate_reference (i, r); i++)
752 if (!r->speculative)
754 r->stmt = NULL;
755 r->lto_stmt_uid = 0;
756 r->speculative_id = 0;
760 /* Remove all references in ref list. */
762 void
763 symtab_node::remove_all_references (void)
765 while (vec_safe_length (ref_list.references))
766 ref_list.references->last ().remove_reference ();
767 vec_free (ref_list.references);
770 /* Remove all referring items in ref list. */
772 void
773 symtab_node::remove_all_referring (void)
775 while (ref_list.referring.length ())
776 ref_list.referring.last ()->remove_reference ();
777 ref_list.referring.release ();
780 /* Dump references in ref list to FILE. */
782 void
783 symtab_node::dump_references (FILE *file)
785 ipa_ref *ref = NULL;
786 int i;
787 for (i = 0; iterate_reference (i, ref); i++)
789 fprintf (file, "%s (%s)",
790 ref->referred->dump_asm_name (),
791 ipa_ref_use_name [ref->use]);
792 if (ref->speculative)
793 fprintf (file, " (speculative)");
795 fprintf (file, "\n");
798 /* Dump referring in list to FILE. */
800 void
801 symtab_node::dump_referring (FILE *file)
803 ipa_ref *ref = NULL;
804 int i;
805 for (i = 0; iterate_referring(i, ref); i++)
807 fprintf (file, "%s (%s)",
808 ref->referring->dump_asm_name (),
809 ipa_ref_use_name [ref->use]);
810 if (ref->speculative)
811 fprintf (file, " (speculative)");
813 fprintf (file, "\n");
816 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
818 /* Dump the visibility of the symbol. */
820 const char *
821 symtab_node::get_visibility_string () const
823 static const char * const visibility_types[]
824 = { "default", "protected", "hidden", "internal" };
825 return visibility_types[DECL_VISIBILITY (decl)];
828 /* Dump the type_name of the symbol. */
829 const char *
830 symtab_node::get_symtab_type_string () const
832 return symtab_type_names[type];
835 /* Dump base fields of symtab nodes to F. Not to be used directly. */
837 void
838 symtab_node::dump_base (FILE *f)
840 static const char * const visibility_types[] = {
841 "default", "protected", "hidden", "internal"
844 fprintf (f, "%s (%s)", dump_asm_name (), name ());
845 dump_addr (f, " @", (void *)this);
846 fprintf (f, "\n Type: %s", symtab_type_names[type]);
848 if (definition)
849 fprintf (f, " definition");
850 if (analyzed)
851 fprintf (f, " analyzed");
852 if (alias)
853 fprintf (f, " alias");
854 if (transparent_alias)
855 fprintf (f, " transparent_alias");
856 if (weakref)
857 fprintf (f, " weakref");
858 if (symver)
859 fprintf (f, " symver");
860 if (cpp_implicit_alias)
861 fprintf (f, " cpp_implicit_alias");
862 if (alias_target)
863 fprintf (f, " target:%s",
864 DECL_P (alias_target)
865 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
866 (alias_target))
867 : IDENTIFIER_POINTER (alias_target));
868 if (body_removed)
869 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
870 fprintf (f, "\n Visibility:");
871 if (in_other_partition)
872 fprintf (f, " in_other_partition");
873 if (used_from_other_partition)
874 fprintf (f, " used_from_other_partition");
875 if (force_output)
876 fprintf (f, " force_output");
877 if (forced_by_abi)
878 fprintf (f, " forced_by_abi");
879 if (externally_visible)
880 fprintf (f, " externally_visible");
881 if (no_reorder)
882 fprintf (f, " no_reorder");
883 if (resolution != LDPR_UNKNOWN)
884 fprintf (f, " %s",
885 ld_plugin_symbol_resolution_names[(int)resolution]);
886 if (TREE_ASM_WRITTEN (decl))
887 fprintf (f, " asm_written");
888 if (DECL_EXTERNAL (decl))
889 fprintf (f, " external");
890 if (TREE_PUBLIC (decl))
891 fprintf (f, " public");
892 if (DECL_COMMON (decl))
893 fprintf (f, " common");
894 if (DECL_WEAK (decl))
895 fprintf (f, " weak");
896 if (DECL_DLLIMPORT_P (decl))
897 fprintf (f, " dll_import");
898 if (DECL_COMDAT (decl))
899 fprintf (f, " comdat");
900 if (get_comdat_group ())
901 fprintf (f, " comdat_group:%s",
902 IDENTIFIER_POINTER (get_comdat_group_id ()));
903 if (DECL_ONE_ONLY (decl))
904 fprintf (f, " one_only");
905 if (get_section ())
906 fprintf (f, " section:%s",
907 get_section ());
908 if (implicit_section)
909 fprintf (f," (implicit_section)");
910 if (DECL_VISIBILITY_SPECIFIED (decl))
911 fprintf (f, " visibility_specified");
912 if (DECL_VISIBILITY (decl))
913 fprintf (f, " visibility:%s",
914 visibility_types [DECL_VISIBILITY (decl)]);
915 if (DECL_VIRTUAL_P (decl))
916 fprintf (f, " virtual");
917 if (DECL_ARTIFICIAL (decl))
918 fprintf (f, " artificial");
919 if (TREE_CODE (decl) == FUNCTION_DECL)
921 if (DECL_STATIC_CONSTRUCTOR (decl))
922 fprintf (f, " constructor");
923 if (DECL_STATIC_DESTRUCTOR (decl))
924 fprintf (f, " destructor");
926 if (ifunc_resolver)
927 fprintf (f, " ifunc_resolver");
928 fprintf (f, "\n");
930 if (same_comdat_group)
931 fprintf (f, " Same comdat group as: %s\n",
932 same_comdat_group->dump_asm_name ());
933 if (next_sharing_asm_name)
934 fprintf (f, " next sharing asm name: %i\n",
935 next_sharing_asm_name->order);
936 if (previous_sharing_asm_name)
937 fprintf (f, " previous sharing asm name: %i\n",
938 previous_sharing_asm_name->order);
940 if (address_taken)
941 fprintf (f, " Address is taken.\n");
942 if (aux)
944 fprintf (f, " Aux:");
945 dump_addr (f, " @", (void *)aux);
946 fprintf (f, "\n");
949 fprintf (f, " References: ");
950 dump_references (f);
951 fprintf (f, " Referring: ");
952 dump_referring (f);
953 if (lto_file_data)
954 fprintf (f, " Read from file: %s\n",
955 lto_file_data->file_name);
958 /* Dump symtab node to F. */
960 void
961 symtab_node::dump (FILE *f)
963 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
964 cnode->dump (f);
965 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
966 vnode->dump (f);
969 void
970 symtab_node::dump_graphviz (FILE *f)
972 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
973 cnode->dump_graphviz (f);
976 void
977 symbol_table::dump (FILE *f)
979 symtab_node *node;
980 fprintf (f, "Symbol table:\n\n");
981 FOR_EACH_SYMBOL (node)
982 node->dump (f);
985 void
986 symbol_table::dump_graphviz (FILE *f)
988 symtab_node *node;
989 fprintf (f, "digraph symtab {\n");
990 FOR_EACH_SYMBOL (node)
991 node->dump_graphviz (f);
992 fprintf (f, "}\n");
995 DEBUG_FUNCTION void
996 symbol_table::debug (void)
998 dump (stderr);
1001 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
1002 Return NULL if there's no such node. */
1004 symtab_node *
1005 symtab_node::get_for_asmname (const_tree asmname)
1007 symtab_node *node;
1009 symtab->symtab_initialize_asm_name_hash ();
1010 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
1011 symtab_node **slot
1012 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
1013 NO_INSERT);
1015 if (slot)
1017 node = *slot;
1018 return node;
1020 return NULL;
1023 /* Dump symtab node NODE to stderr. */
1025 DEBUG_FUNCTION void
1026 symtab_node::debug (void)
1028 dump (stderr);
1031 /* Verify common part of symtab nodes. */
1033 #if __GNUC__ >= 10
1034 /* Disable warnings about missing quoting in GCC diagnostics for
1035 the verification errors. Their format strings don't follow GCC
1036 diagnostic conventions and the calls are ultimately followed by
1037 one to internal_error. */
1038 # pragma GCC diagnostic push
1039 # pragma GCC diagnostic ignored "-Wformat-diag"
1040 #endif
1042 DEBUG_FUNCTION bool
1043 symtab_node::verify_base (void)
1045 bool error_found = false;
1046 symtab_node *hashed_node;
1048 if (is_a <cgraph_node *> (this))
1050 if (TREE_CODE (decl) != FUNCTION_DECL)
1052 error ("function symbol is not function");
1053 error_found = true;
1055 else if ((lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl))
1056 != NULL)
1057 != dyn_cast <cgraph_node *> (this)->ifunc_resolver)
1059 error ("inconsistent %<ifunc%> attribute");
1060 error_found = true;
1063 else if (is_a <varpool_node *> (this))
1065 if (!VAR_P (decl))
1067 error ("variable symbol is not variable");
1068 error_found = true;
1071 else
1073 error ("node has unknown type");
1074 error_found = true;
1077 if (symtab->state != LTO_STREAMING)
1079 hashed_node = symtab_node::get (decl);
1080 if (!hashed_node)
1082 error ("node not found node->decl->decl_with_vis.symtab_node");
1083 error_found = true;
1085 if (hashed_node != this
1086 && (!is_a <cgraph_node *> (this)
1087 || !dyn_cast <cgraph_node *> (this)->clone_of
1088 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
1090 error ("node differs from node->decl->decl_with_vis.symtab_node");
1091 error_found = true;
1094 if (symtab->assembler_name_hash)
1096 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
1097 if (hashed_node)
1099 if (hashed_node->previous_sharing_asm_name)
1101 error ("assembler name hash list corrupted");
1102 error_found = true;
1104 else if (previous_sharing_asm_name == NULL)
1106 if (hashed_node != this)
1108 error ("assembler name hash list corrupted");
1109 error_found = true;
1112 else if (!(is_a <varpool_node *> (this) && DECL_HARD_REGISTER (decl)))
1114 if (!asmname_hasher::equal (previous_sharing_asm_name,
1115 DECL_ASSEMBLER_NAME (decl)))
1117 error ("node not found in symtab assembler name hash");
1118 error_found = true;
1123 if (previous_sharing_asm_name
1124 && previous_sharing_asm_name->next_sharing_asm_name != this)
1126 error ("double linked list of assembler names corrupted");
1127 error_found = true;
1129 if (body_removed && definition)
1131 error ("node has body_removed but is definition");
1132 error_found = true;
1134 if (analyzed && !definition)
1136 error ("node is analyzed but it is not a definition");
1137 error_found = true;
1139 if (cpp_implicit_alias && !alias)
1141 error ("node is alias but not implicit alias");
1142 error_found = true;
1144 if (alias && !definition && !weakref)
1146 error ("node is alias but not definition");
1147 error_found = true;
1149 if (weakref && !transparent_alias)
1151 error ("node is weakref but not an transparent_alias");
1152 error_found = true;
1154 if (transparent_alias && !alias)
1156 error ("node is transparent_alias but not an alias");
1157 error_found = true;
1159 if (symver && !alias)
1161 error ("node is symver but not alias");
1162 error_found = true;
1164 /* Limitation of gas requires us to output targets of symver aliases as
1165 global symbols. This is binutils PR 25295. */
1166 if (symver
1167 && (!TREE_PUBLIC (get_alias_target ()->decl)
1168 || DECL_VISIBILITY (get_alias_target ()->decl) != VISIBILITY_DEFAULT))
1170 error ("symver target is not exported with default visibility");
1171 error_found = true;
1173 if (symver
1174 && (!TREE_PUBLIC (decl)
1175 || DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT))
1177 error ("symver is not exported with default visibility");
1178 error_found = true;
1180 if (same_comdat_group)
1182 symtab_node *n = same_comdat_group;
1184 if (!n->get_comdat_group ())
1186 error ("node is in same_comdat_group list but has no comdat_group");
1187 error_found = true;
1189 if (n->get_comdat_group () != get_comdat_group ())
1191 error ("same_comdat_group list across different groups");
1192 error_found = true;
1194 if (n->type != type)
1196 error ("mixing different types of symbol in same comdat groups is not supported");
1197 error_found = true;
1199 if (n == this)
1201 error ("node is alone in a comdat group");
1202 error_found = true;
1206 if (!n->same_comdat_group)
1208 error ("same_comdat_group is not a circular list");
1209 error_found = true;
1210 break;
1212 n = n->same_comdat_group;
1214 while (n != this);
1215 if (comdat_local_p ())
1217 ipa_ref *ref = NULL;
1219 for (int i = 0; iterate_referring (i, ref); ++i)
1221 if (!in_same_comdat_group_p (ref->referring))
1223 error ("comdat-local symbol referred to by %s outside its "
1224 "comdat",
1225 identifier_to_locale (ref->referring->name()));
1226 error_found = true;
1231 if (implicit_section && !get_section ())
1233 error ("implicit_section flag is set but section isn%'t");
1234 error_found = true;
1236 if (get_section () && get_comdat_group ()
1237 && !implicit_section
1238 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1240 error ("Both section and comdat group is set");
1241 error_found = true;
1243 /* TODO: Add string table for sections, so we do not keep holding duplicated
1244 strings. */
1245 if (alias && definition
1246 && get_section () != get_alias_target ()->get_section ()
1247 && (!get_section()
1248 || !get_alias_target ()->get_section ()
1249 || strcmp (get_section(),
1250 get_alias_target ()->get_section ())))
1252 error ("Alias and target%'s section differs");
1253 get_alias_target ()->dump (stderr);
1254 error_found = true;
1256 if (alias && definition
1257 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1259 error ("Alias and target%'s comdat groups differs");
1260 get_alias_target ()->dump (stderr);
1261 error_found = true;
1263 if (transparent_alias && definition && !weakref)
1265 symtab_node *to = get_alias_target ();
1266 const char *name1
1267 = IDENTIFIER_POINTER (
1268 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl)));
1269 const char *name2
1270 = IDENTIFIER_POINTER (
1271 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to->decl)));
1272 if (!symbol_table::assembler_names_equal_p (name1, name2))
1274 error ("Transparent alias and target%'s assembler names differs");
1275 get_alias_target ()->dump (stderr);
1276 error_found = true;
1279 if (transparent_alias && definition
1280 && get_alias_target()->transparent_alias && get_alias_target()->analyzed)
1282 error ("Chained transparent aliases");
1283 get_alias_target ()->dump (stderr);
1284 error_found = true;
1287 return error_found;
1290 /* Verify consistency of NODE. */
1292 DEBUG_FUNCTION void
1293 symtab_node::verify (void)
1295 if (seen_error ())
1296 return;
1298 timevar_push (TV_CGRAPH_VERIFY);
1299 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1300 node->verify_node ();
1301 else
1302 if (verify_base ())
1304 debug ();
1305 internal_error ("symtab_node::verify failed");
1307 timevar_pop (TV_CGRAPH_VERIFY);
1310 /* Verify symbol table for internal consistency. */
1312 DEBUG_FUNCTION void
1313 symtab_node::verify_symtab_nodes (void)
1315 symtab_node *node;
1316 hash_map<tree, symtab_node *> comdat_head_map (251);
1318 FOR_EACH_SYMBOL (node)
1320 node->verify ();
1321 if (node->get_comdat_group ())
1323 symtab_node **entry, *s;
1324 bool existed;
1326 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1327 &existed);
1328 if (!existed)
1329 *entry = node;
1330 else if (!DECL_EXTERNAL (node->decl))
1332 for (s = (*entry)->same_comdat_group;
1333 s != NULL && s != node && s != *entry;
1334 s = s->same_comdat_group)
1336 if (!s || s == *entry)
1338 error ("Two symbols with same comdat_group are not linked by "
1339 "the same_comdat_group list.");
1340 (*entry)->debug ();
1341 node->debug ();
1342 internal_error ("symtab_node::verify failed");
1349 #if __GNUC__ >= 10
1350 # pragma GCC diagnostic pop
1351 #endif
1353 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1354 but other code such as notice_global_symbol generates rtl. */
1356 void
1357 symtab_node::make_decl_local (void)
1359 rtx rtl, symbol;
1361 if (weakref)
1363 weakref = false;
1364 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) = 0;
1365 TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) = NULL_TREE;
1366 symtab->change_decl_assembler_name
1367 (decl, DECL_ASSEMBLER_NAME (get_alias_target ()->decl));
1368 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1369 DECL_ATTRIBUTES (decl));
1371 /* Avoid clearing comdat_groups on comdat-local decls. */
1372 else if (TREE_PUBLIC (decl) == 0)
1373 return;
1375 /* Localizing a symbol also make all its transparent aliases local. */
1376 ipa_ref *ref;
1377 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1379 struct symtab_node *alias = ref->referring;
1380 if (alias->transparent_alias)
1381 alias->make_decl_local ();
1384 if (VAR_P (decl))
1386 DECL_COMMON (decl) = 0;
1387 /* ADDRESSABLE flag is not defined for public symbols. */
1388 TREE_ADDRESSABLE (decl) = 1;
1389 TREE_STATIC (decl) = 1;
1391 else
1392 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1394 DECL_COMDAT (decl) = 0;
1395 DECL_WEAK (decl) = 0;
1396 DECL_EXTERNAL (decl) = 0;
1397 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1398 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1399 TREE_PUBLIC (decl) = 0;
1400 DECL_DLLIMPORT_P (decl) = 0;
1401 if (!DECL_RTL_SET_P (decl))
1402 return;
1404 /* Update rtl flags. */
1405 make_decl_rtl (decl);
1407 rtl = DECL_RTL (decl);
1408 if (!MEM_P (rtl))
1409 return;
1411 symbol = XEXP (rtl, 0);
1412 if (GET_CODE (symbol) != SYMBOL_REF)
1413 return;
1415 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1418 /* Copy visibility from N.
1419 This is useful when THIS becomes a transparent alias of N. */
1421 void
1422 symtab_node::copy_visibility_from (symtab_node *n)
1424 gcc_checking_assert (n->weakref == weakref);
1426 ipa_ref *ref;
1427 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1429 struct symtab_node *alias = ref->referring;
1430 if (alias->transparent_alias)
1431 alias->copy_visibility_from (n);
1434 if (VAR_P (decl))
1436 DECL_COMMON (decl) = DECL_COMMON (n->decl);
1437 /* ADDRESSABLE flag is not defined for public symbols. */
1438 if (TREE_PUBLIC (decl) && !TREE_PUBLIC (n->decl))
1439 TREE_ADDRESSABLE (decl) = 1;
1440 TREE_STATIC (decl) = TREE_STATIC (n->decl);
1442 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1444 DECL_COMDAT (decl) = DECL_COMDAT (n->decl);
1445 DECL_WEAK (decl) = DECL_WEAK (n->decl);
1446 DECL_EXTERNAL (decl) = DECL_EXTERNAL (n->decl);
1447 DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (n->decl);
1448 DECL_VISIBILITY (decl) = DECL_VISIBILITY (n->decl);
1449 TREE_PUBLIC (decl) = TREE_PUBLIC (n->decl);
1450 DECL_DLLIMPORT_P (decl) = DECL_DLLIMPORT_P (n->decl);
1451 resolution = n->resolution;
1452 set_comdat_group (n->get_comdat_group ());
1453 call_for_symbol_and_aliases (symtab_node::set_section,
1454 const_cast<char *>(n->get_section ()), true);
1455 externally_visible = n->externally_visible;
1456 if (!DECL_RTL_SET_P (decl))
1457 return;
1459 /* Update rtl flags. */
1460 make_decl_rtl (decl);
1462 rtx rtl = DECL_RTL (decl);
1463 if (!MEM_P (rtl))
1464 return;
1466 rtx symbol = XEXP (rtl, 0);
1467 if (GET_CODE (symbol) != SYMBOL_REF)
1468 return;
1470 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1473 /* Walk the alias chain to return the symbol NODE is alias of.
1474 If NODE is not an alias, return NODE.
1475 Assumes NODE is known to be alias. */
1477 symtab_node *
1478 symtab_node::ultimate_alias_target_1 (enum availability *availability,
1479 symtab_node *ref)
1481 bool transparent_p = false;
1483 /* To determine visibility of the target, we follow ELF semantic of aliases.
1484 Here alias is an alternative assembler name of a given definition. Its
1485 availability prevails the availability of its target (i.e. static alias of
1486 weak definition is available.
1488 Transparent alias is just alternative name of a given symbol used within
1489 one compilation unit and is translated prior hitting the object file. It
1490 inherits the visibility of its target.
1491 Weakref is a different animal (and noweak definition is weak).
1493 If we ever get into supporting targets with different semantics, a target
1494 hook will be needed here. */
1496 if (availability)
1498 transparent_p = transparent_alias;
1499 if (!transparent_p)
1500 *availability = get_availability (ref);
1501 else
1502 *availability = AVAIL_NOT_AVAILABLE;
1505 symtab_node *node = this;
1506 while (node)
1508 if (node->alias && node->analyzed)
1509 node = node->get_alias_target ();
1510 else
1512 if (!availability || (!transparent_p && node->analyzed))
1514 else if (node->analyzed && !node->transparent_alias)
1515 *availability = node->get_availability (ref);
1516 else
1517 *availability = AVAIL_NOT_AVAILABLE;
1518 return node;
1520 if (node && availability && transparent_p
1521 && node->transparent_alias)
1523 *availability = node->get_availability (ref);
1524 transparent_p = false;
1527 if (availability)
1528 *availability = AVAIL_NOT_AVAILABLE;
1529 return NULL;
1532 /* C++ FE sometimes change linkage flags after producing same body aliases.
1534 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1535 are obviously equivalent. The way it is doing so is however somewhat
1536 kludgy and interferes with the visibility code. As a result we need to
1537 copy the visibility from the target to get things right. */
1539 void
1540 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1542 if (is_a <cgraph_node *> (this))
1544 DECL_DECLARED_INLINE_P (decl)
1545 = DECL_DECLARED_INLINE_P (target->decl);
1546 DECL_DISREGARD_INLINE_LIMITS (decl)
1547 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1549 /* FIXME: It is not really clear why those flags should not be copied for
1550 functions, too. */
1551 else
1553 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1554 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1555 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1557 if (TREE_PUBLIC (decl))
1559 tree group;
1561 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1562 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1563 group = target->get_comdat_group ();
1564 set_comdat_group (group);
1565 if (group && !same_comdat_group)
1566 add_to_same_comdat_group (target);
1568 externally_visible = target->externally_visible;
1571 /* Set section, do not recurse into aliases.
1572 When one wants to change section of a symbol and its aliases,
1573 use set_section. */
1575 void
1576 symtab_node::set_section_for_node (const char *section)
1578 const char *current = get_section ();
1579 section_hash_entry **slot;
1581 if (current == section
1582 || (current && section
1583 && !strcmp (current, section)))
1584 return;
1586 if (current)
1588 x_section->ref_count--;
1589 if (!x_section->ref_count)
1591 hashval_t hash = htab_hash_string (x_section->name);
1592 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1593 hash, INSERT);
1594 ggc_free (x_section);
1595 symtab->section_hash->clear_slot (slot);
1597 x_section = NULL;
1599 if (!section)
1601 implicit_section = false;
1602 return;
1604 if (!symtab->section_hash)
1605 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1606 slot = symtab->section_hash->find_slot_with_hash (section,
1607 htab_hash_string (section),
1608 INSERT);
1609 if (*slot)
1610 x_section = (section_hash_entry *)*slot;
1611 else
1613 int len = strlen (section);
1614 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1615 x_section->name = ggc_vec_alloc<char> (len + 1);
1616 memcpy (x_section->name, section, len + 1);
1618 x_section->ref_count++;
1621 /* Worker for set_section. */
1623 bool
1624 symtab_node::set_section (symtab_node *n, void *s)
1626 n->set_section_for_node ((char *)s);
1627 return false;
1630 /* Set section of symbol and its aliases. */
1632 void
1633 symtab_node::set_section (const char *section)
1635 gcc_assert (!this->alias || !this->analyzed);
1636 call_for_symbol_and_aliases
1637 (symtab_node::set_section, const_cast<char *>(section), true);
1640 /* Return the initialization priority. */
1642 priority_type
1643 symtab_node::get_init_priority ()
1645 if (!this->in_init_priority_hash)
1646 return DEFAULT_INIT_PRIORITY;
1648 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1649 return h ? h->init : DEFAULT_INIT_PRIORITY;
1652 /* Return the finalization priority. */
1654 priority_type
1655 cgraph_node::get_fini_priority ()
1657 if (!this->in_init_priority_hash)
1658 return DEFAULT_INIT_PRIORITY;
1659 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1660 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1663 /* Return the initialization and finalization priority information for
1664 DECL. If there is no previous priority information, a freshly
1665 allocated structure is returned. */
1667 symbol_priority_map *
1668 symtab_node::priority_info (void)
1670 if (!symtab->init_priority_hash)
1671 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1673 bool existed;
1674 symbol_priority_map *h
1675 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1676 if (!existed)
1678 h->init = DEFAULT_INIT_PRIORITY;
1679 h->fini = DEFAULT_INIT_PRIORITY;
1680 in_init_priority_hash = true;
1683 return h;
1686 /* Set initialization priority to PRIORITY. */
1688 void
1689 symtab_node::set_init_priority (priority_type priority)
1691 symbol_priority_map *h;
1693 if (is_a <cgraph_node *> (this))
1694 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1696 if (priority == DEFAULT_INIT_PRIORITY)
1698 gcc_assert (get_init_priority() == priority);
1699 return;
1701 h = priority_info ();
1702 h->init = priority;
1705 /* Set finalization priority to PRIORITY. */
1707 void
1708 cgraph_node::set_fini_priority (priority_type priority)
1710 symbol_priority_map *h;
1712 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1714 if (priority == DEFAULT_INIT_PRIORITY)
1716 gcc_assert (get_fini_priority() == priority);
1717 return;
1719 h = priority_info ();
1720 h->fini = priority;
1723 /* Worker for symtab_resolve_alias. */
1725 bool
1726 symtab_node::set_implicit_section (symtab_node *n,
1727 void *data ATTRIBUTE_UNUSED)
1729 n->implicit_section = true;
1730 return false;
1733 /* Add reference recording that symtab node is alias of TARGET.
1734 The function can fail in the case of aliasing cycles; in this case
1735 it returns false. */
1737 bool
1738 symtab_node::resolve_alias (symtab_node *target, bool transparent)
1740 symtab_node *n;
1742 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1744 /* Never let cycles to creep into the symbol table alias references;
1745 those will make alias walkers to be infinite. */
1746 for (n = target; n && n->alias;
1747 n = n->analyzed ? n->get_alias_target () : NULL)
1748 if (n == this)
1750 if (is_a <cgraph_node *> (this))
1751 error ("function %q+D part of alias cycle", decl);
1752 else if (is_a <varpool_node *> (this))
1753 error ("variable %q+D part of alias cycle", decl);
1754 else
1755 gcc_unreachable ();
1756 alias = false;
1757 return false;
1760 /* "analyze" the node - i.e. mark the reference. */
1761 definition = true;
1762 alias = true;
1763 analyzed = true;
1764 transparent |= transparent_alias;
1765 transparent_alias = transparent;
1766 if (transparent)
1767 while (target->transparent_alias && target->analyzed)
1768 target = target->get_alias_target ();
1769 create_reference (target, IPA_REF_ALIAS, NULL);
1771 /* Add alias into the comdat group of its target unless it is already there. */
1772 if (same_comdat_group)
1773 remove_from_same_comdat_group ();
1774 set_comdat_group (NULL);
1775 if (target->get_comdat_group ())
1776 add_to_same_comdat_group (target);
1778 if ((get_section () != target->get_section ()
1779 || target->get_comdat_group ()) && get_section () && !implicit_section)
1781 error ("section of alias %q+D must match section of its target", decl);
1783 call_for_symbol_and_aliases (symtab_node::set_section,
1784 const_cast<char *>(target->get_section ()), true);
1785 if (target->implicit_section)
1786 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1788 /* Alias targets become redundant after alias is resolved into an reference.
1789 We do not want to keep it around or we would have to mind updating them
1790 when renaming symbols. */
1791 alias_target = NULL;
1793 if (!transparent && cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1794 fixup_same_cpp_alias_visibility (target);
1796 /* If alias has address taken, so does the target. */
1797 if (address_taken)
1798 target->ultimate_alias_target ()->address_taken = true;
1800 /* All non-transparent aliases of THIS are now in fact aliases of TARGET.
1801 If alias is transparent, also all transparent aliases of THIS are now
1802 aliases of TARGET.
1803 Also merge same comdat group lists. */
1804 ipa_ref *ref;
1805 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1807 struct symtab_node *alias_alias = ref->referring;
1808 if (alias_alias->get_comdat_group ())
1810 alias_alias->remove_from_same_comdat_group ();
1811 alias_alias->set_comdat_group (NULL);
1812 if (target->get_comdat_group ())
1813 alias_alias->add_to_same_comdat_group (target);
1815 if ((!alias_alias->transparent_alias
1816 && !alias_alias->symver)
1817 || transparent)
1819 alias_alias->remove_all_references ();
1820 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1822 else i++;
1824 return true;
1827 /* Worker searching noninterposable alias. */
1829 bool
1830 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1832 if (!node->transparent_alias && decl_binds_to_current_def_p (node->decl))
1834 symtab_node *fn = node->ultimate_alias_target ();
1836 /* Ensure that the alias is well formed this may not be the case
1837 of user defined aliases and currently it is not always the case
1838 of C++ same body aliases (that is a bug). */
1839 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1840 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1841 || (TREE_CODE (node->decl) == FUNCTION_DECL
1842 && flags_from_decl_or_type (node->decl)
1843 != flags_from_decl_or_type (fn->decl))
1844 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1845 return false;
1846 *(symtab_node **)data = node;
1847 return true;
1849 return false;
1852 /* If node cannot be overwriten by static or dynamic linker to point to
1853 different definition, return NODE. Otherwise look for alias with such
1854 property and if none exists, introduce new one. */
1856 symtab_node *
1857 symtab_node::noninterposable_alias (void)
1859 tree new_decl;
1860 symtab_node *new_node = NULL;
1862 /* First try to look up existing alias or base object
1863 (if that is already non-overwritable). */
1864 symtab_node *node = ultimate_alias_target ();
1865 gcc_assert (!node->alias && !node->weakref);
1866 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1867 (void *)&new_node, true);
1868 if (new_node)
1869 return new_node;
1871 /* If aliases aren't supported by the assembler, fail. */
1872 if (!TARGET_SUPPORTS_ALIASES)
1873 return NULL;
1875 /* Otherwise create a new one. */
1876 new_decl = copy_node (node->decl);
1877 DECL_DLLIMPORT_P (new_decl) = 0;
1878 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1879 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1880 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1881 DECL_INITIAL (new_decl) = NULL;
1882 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1883 SET_DECL_RTL (new_decl, NULL);
1885 /* Update the properties. */
1886 DECL_EXTERNAL (new_decl) = 0;
1887 TREE_PUBLIC (new_decl) = 0;
1888 DECL_COMDAT (new_decl) = 0;
1889 DECL_WEAK (new_decl) = 0;
1891 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1892 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1893 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1895 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1896 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1897 new_node = cgraph_node::create_alias (new_decl, node->decl);
1899 cgraph_node *new_cnode = dyn_cast <cgraph_node *> (new_node),
1900 *cnode = dyn_cast <cgraph_node *> (node);
1902 new_cnode->unit_id = cnode->unit_id;
1903 new_cnode->merged_comdat = cnode->merged_comdat;
1904 new_cnode->merged_extern_inline = cnode->merged_extern_inline;
1906 else
1908 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1909 DECL_INITIAL (new_decl) = error_mark_node;
1910 new_node = varpool_node::create_alias (new_decl, node->decl);
1912 new_node->resolve_alias (node);
1913 gcc_assert (decl_binds_to_current_def_p (new_decl)
1914 && targetm.binds_local_p (new_decl));
1915 return new_node;
1918 /* Return true if symtab node and TARGET represents
1919 semantically equivalent symbols. */
1921 bool
1922 symtab_node::semantically_equivalent_p (symtab_node *target)
1924 enum availability avail;
1925 symtab_node *ba;
1926 symtab_node *bb;
1928 /* Equivalent functions are equivalent. */
1929 if (decl == target->decl)
1930 return true;
1932 /* If symbol is not overwritable by different implementation,
1933 walk to the base object it defines. */
1934 ba = ultimate_alias_target (&avail);
1935 if (avail >= AVAIL_AVAILABLE)
1937 if (target == ba)
1938 return true;
1940 else
1941 ba = this;
1942 bb = target->ultimate_alias_target (&avail);
1943 if (avail >= AVAIL_AVAILABLE)
1945 if (this == bb)
1946 return true;
1948 else
1949 bb = target;
1950 return bb == ba;
1953 /* Classify symbol symtab node for partitioning. */
1955 enum symbol_partitioning_class
1956 symtab_node::get_partitioning_class (void)
1958 /* Inline clones are always duplicated.
1959 This include external declarations. */
1960 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1962 if (DECL_ABSTRACT_P (decl))
1963 return SYMBOL_EXTERNAL;
1965 if (cnode && cnode->inlined_to)
1966 return SYMBOL_DUPLICATE;
1968 /* Transparent aliases are always duplicated. */
1969 if (transparent_alias)
1970 return definition ? SYMBOL_DUPLICATE : SYMBOL_EXTERNAL;
1972 /* External declarations are external. */
1973 if (DECL_EXTERNAL (decl))
1974 return SYMBOL_EXTERNAL;
1976 /* Even static aliases of external functions as external. Those can happen
1977 when COMDAT got resolved to non-IL implementation. */
1978 if (alias && DECL_EXTERNAL (ultimate_alias_target ()->decl))
1979 return SYMBOL_EXTERNAL;
1981 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1983 if (alias && definition && !ultimate_alias_target ()->definition)
1984 return SYMBOL_EXTERNAL;
1985 /* Constant pool references use local symbol names that cannot
1986 be promoted global. We should never put into a constant pool
1987 objects that cannot be duplicated across partitions. */
1988 if (DECL_IN_CONSTANT_POOL (decl))
1989 return SYMBOL_DUPLICATE;
1990 if (DECL_HARD_REGISTER (decl))
1991 return SYMBOL_DUPLICATE;
1992 gcc_checking_assert (vnode->definition);
1994 /* Functions that are cloned may stay in callgraph even if they are unused.
1995 Handle them as external; compute_ltrans_boundary take care to make
1996 proper things to happen (i.e. to make them appear in the boundary but
1997 with body streamed, so clone can me materialized). */
1998 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1999 return SYMBOL_EXTERNAL;
2001 /* Linker discardable symbols are duplicated to every use unless they are
2002 keyed. */
2003 if (DECL_ONE_ONLY (decl)
2004 && !force_output
2005 && !forced_by_abi
2006 && !used_from_object_file_p ())
2007 return SYMBOL_DUPLICATE;
2009 return SYMBOL_PARTITION;
2012 /* Return true when symbol is known to be non-zero. */
2014 bool
2015 symtab_node::nonzero_address ()
2017 /* Weakrefs may be NULL when their target is not defined. */
2018 if (alias && weakref)
2020 if (analyzed)
2022 symtab_node *target = ultimate_alias_target ();
2024 if (target->alias && target->weakref)
2025 return false;
2026 /* We cannot recurse to target::nonzero. It is possible that the
2027 target is used only via the alias.
2028 We may walk references and look for strong use, but we do not know
2029 if this strong use will survive to final binary, so be
2030 conservative here.
2031 ??? Maybe we could do the lookup during late optimization that
2032 could be useful to eliminate the NULL pointer checks in LTO
2033 programs. */
2034 if (target->definition && !DECL_EXTERNAL (target->decl))
2035 return true;
2036 if (target->resolution != LDPR_UNKNOWN
2037 && target->resolution != LDPR_UNDEF
2038 && !target->can_be_discarded_p ()
2039 && flag_delete_null_pointer_checks)
2040 return true;
2041 return false;
2043 else
2044 return false;
2047 /* With !flag_delete_null_pointer_checks we assume that symbols may
2048 bind to NULL. This is on by default on embedded targets only.
2050 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
2051 linking fails. Important case of WEAK we want to do well are comdats.
2052 Those are handled by later check for definition.
2054 When parsing, beware the cases when WEAK attribute is added later. */
2055 if (!DECL_WEAK (decl)
2056 && flag_delete_null_pointer_checks)
2058 refuse_visibility_changes = true;
2059 return true;
2062 /* If target is defined and either comdat or not extern, we know it will be
2063 output and thus it will bind to non-NULL.
2064 Play safe for flag_delete_null_pointer_checks where weak definition may
2065 be re-defined by NULL. */
2066 if (definition && (!DECL_EXTERNAL (decl) || DECL_COMDAT (decl))
2067 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
2069 if (!DECL_WEAK (decl))
2070 refuse_visibility_changes = true;
2071 return true;
2074 /* As the last resort, check the resolution info. */
2075 if (resolution != LDPR_UNKNOWN
2076 && resolution != LDPR_UNDEF
2077 && !can_be_discarded_p ()
2078 && flag_delete_null_pointer_checks)
2079 return true;
2080 return false;
2083 /* Return 0 if symbol is known to have different address than S2,
2084 Return 1 if symbol is known to have same address as S2,
2085 return -1 otherwise.
2087 If MEMORY_ACCESSED is true, assume that both memory pointer to THIS
2088 and S2 is going to be accessed. This eliminates the situations when
2089 either THIS or S2 is NULL and is useful for comparing bases when deciding
2090 about memory aliasing. */
2092 symtab_node::equal_address_to (symtab_node *s2, bool memory_accessed)
2094 enum availability avail1, avail2;
2096 /* A Shortcut: equivalent symbols are always equivalent. */
2097 if (this == s2)
2098 return 1;
2100 /* Unwind transparent aliases first; those are always equal to their
2101 target. */
2102 if (this->transparent_alias && this->analyzed)
2103 return this->get_alias_target ()->equal_address_to (s2);
2104 while (s2->transparent_alias && s2->analyzed)
2105 s2 = s2->get_alias_target();
2107 if (this == s2)
2108 return 1;
2110 /* For non-interposable aliases, lookup and compare their actual definitions.
2111 Also check if the symbol needs to bind to given definition. */
2112 symtab_node *rs1 = ultimate_alias_target (&avail1);
2113 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
2114 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
2115 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
2116 bool really_binds_local1 = binds_local1;
2117 bool really_binds_local2 = binds_local2;
2119 /* Addresses of vtables and virtual functions cannot be used by user
2120 code and are used only within speculation. In this case we may make
2121 symbol equivalent to its alias even if interposition may break this
2122 rule. Doing so will allow us to turn speculative inlining into
2123 non-speculative more aggressively. */
2124 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
2125 binds_local1 = true;
2126 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
2127 binds_local2 = true;
2129 /* If both definitions are available we know that even if they are bound
2130 to other unit they must be defined same way and therefore we can use
2131 equivalence test. */
2132 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
2133 binds_local1 = binds_local2 = true;
2135 if (binds_local1 && binds_local2 && rs1 == rs2)
2137 /* We made use of the fact that alias is not weak. */
2138 if (rs1 != this)
2139 refuse_visibility_changes = true;
2140 if (rs2 != s2)
2141 s2->refuse_visibility_changes = true;
2142 return 1;
2145 /* If both symbols may resolve to NULL, we cannot really prove them
2146 different. */
2147 if (!memory_accessed && !nonzero_address () && !s2->nonzero_address ())
2148 return -1;
2150 /* Except for NULL, functions and variables never overlap. */
2151 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
2152 return 0;
2154 /* If one of the symbols is unresolved alias, punt. */
2155 if (rs1->alias || rs2->alias)
2156 return -1;
2158 /* If we have a non-interposale definition of at least one of the symbols
2159 and the other symbol is different, we know other unit cannot interpose
2160 it to the first symbol; all aliases of the definition needs to be
2161 present in the current unit. */
2162 if (((really_binds_local1 || really_binds_local2)
2163 /* If we have both definitions and they are different, we know they
2164 will be different even in units they binds to. */
2165 || (binds_local1 && binds_local2))
2166 && rs1 != rs2)
2168 /* We make use of the fact that one symbol is not alias of the other
2169 and that the definition is non-interposable. */
2170 refuse_visibility_changes = true;
2171 s2->refuse_visibility_changes = true;
2172 rs1->refuse_visibility_changes = true;
2173 rs2->refuse_visibility_changes = true;
2174 return 0;
2177 /* TODO: Alias oracle basically assume that addresses of global variables
2178 are different unless they are declared as alias of one to another while
2179 the code folding comparisons doesn't.
2180 We probably should be consistent and use this fact here, too, but for
2181 the moment return false only when we are called from the alias oracle. */
2183 return memory_accessed && rs1 != rs2 ? 0 : -1;
2186 /* Worker for call_for_symbol_and_aliases. */
2188 bool
2189 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
2190 void *),
2191 void *data,
2192 bool include_overwritable)
2194 ipa_ref *ref;
2195 FOR_EACH_ALIAS (this, ref)
2197 symtab_node *alias = ref->referring;
2198 if (include_overwritable
2199 || alias->get_availability () > AVAIL_INTERPOSABLE)
2200 if (alias->call_for_symbol_and_aliases (callback, data,
2201 include_overwritable))
2202 return true;
2204 return false;
2207 /* Return true if address of N is possibly compared. */
2209 static bool
2210 address_matters_1 (symtab_node *n, void *)
2212 struct ipa_ref *ref;
2214 if (!n->address_can_be_compared_p ())
2215 return false;
2216 if (n->externally_visible || n->force_output)
2217 return true;
2219 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
2220 if (ref->address_matters_p ())
2221 return true;
2222 return false;
2225 /* Return true if symbol's address may possibly be compared to other
2226 symbol's address. */
2228 bool
2229 symtab_node::address_matters_p ()
2231 gcc_assert (!alias);
2232 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
2235 /* Return true if symbol's alignment may be increased. */
2237 bool
2238 symtab_node::can_increase_alignment_p (void)
2240 symtab_node *target = ultimate_alias_target ();
2242 /* For now support only variables. */
2243 if (!VAR_P (decl))
2244 return false;
2246 /* With -fno-toplevel-reorder we may have already output the constant. */
2247 if (TREE_ASM_WRITTEN (target->decl))
2248 return false;
2250 /* If target is already placed in an anchor, we cannot touch its
2251 alignment. */
2252 if (DECL_RTL_SET_P (target->decl)
2253 && MEM_P (DECL_RTL (target->decl))
2254 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
2255 return false;
2257 /* Constant pool entries may be shared. */
2258 if (DECL_IN_CONSTANT_POOL (target->decl))
2259 return false;
2261 /* We cannot change alignment of symbols that may bind to symbols
2262 in other translation unit that may contain a definition with lower
2263 alignment. */
2264 if (!decl_binds_to_current_def_p (decl))
2265 return false;
2267 /* When compiling partition, be sure the symbol is not output by other
2268 partition. */
2269 if (flag_ltrans
2270 && (target->in_other_partition
2271 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
2272 return false;
2274 /* Do not override the alignment as specified by the ABI when the used
2275 attribute is set. */
2276 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
2277 return false;
2279 /* Do not override explicit alignment set by the user when an explicit
2280 section name is also used. This is a common idiom used by many
2281 software projects. */
2282 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
2283 return false;
2285 return true;
2288 /* Worker for symtab_node::increase_alignment. */
2290 static bool
2291 increase_alignment_1 (symtab_node *n, void *v)
2293 unsigned int align = (size_t)v;
2294 if (DECL_ALIGN (n->decl) < align
2295 && n->can_increase_alignment_p ())
2297 SET_DECL_ALIGN (n->decl, align);
2298 DECL_USER_ALIGN (n->decl) = 1;
2300 return false;
2303 /* Increase alignment of THIS to ALIGN. */
2305 void
2306 symtab_node::increase_alignment (unsigned int align)
2308 gcc_assert (can_increase_alignment_p () && align <= MAX_OFILE_ALIGNMENT);
2309 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
2310 (void *)(size_t) align,
2311 true);
2312 gcc_assert (DECL_ALIGN (decl) >= align);
2315 /* Helper for symtab_node::definition_alignment. */
2317 static bool
2318 get_alignment_1 (symtab_node *n, void *v)
2320 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
2321 return false;
2324 /* Return desired alignment of the definition. This is NOT alignment useful
2325 to access THIS, because THIS may be interposable and DECL_ALIGN should
2326 be used instead. It however must be guaranteed when output definition
2327 of THIS. */
2329 unsigned int
2330 symtab_node::definition_alignment ()
2332 unsigned int align = 0;
2333 gcc_assert (!alias);
2334 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2335 return align;
2338 /* Return symbol used to separate symbol name from suffix. */
2340 char
2341 symbol_table::symbol_suffix_separator ()
2343 #ifndef NO_DOT_IN_LABEL
2344 return '.';
2345 #elif !defined NO_DOLLAR_IN_LABEL
2346 return '$';
2347 #else
2348 return '_';
2349 #endif
2352 /* Return true when references to this symbol from REF must bind to current
2353 definition in final executable. */
2355 bool
2356 symtab_node::binds_to_current_def_p (symtab_node *ref)
2358 if (!definition)
2359 return false;
2360 if (transparent_alias)
2361 return definition
2362 && get_alias_target()->binds_to_current_def_p (ref);
2363 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
2364 if (cnode && cnode->ifunc_resolver)
2365 return false;
2366 if (decl_binds_to_current_def_p (decl))
2367 return true;
2369 /* Inline clones always binds locally. */
2370 if (cnode && cnode->inlined_to)
2371 return true;
2373 if (DECL_EXTERNAL (decl))
2374 return false;
2376 gcc_assert (externally_visible);
2378 if (ref)
2380 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2381 if (cref)
2382 ref = cref->inlined_to;
2385 /* If this is a reference from symbol itself and there are no aliases, we
2386 may be sure that the symbol was not interposed by something else because
2387 the symbol itself would be unreachable otherwise. This is important
2388 to optimize recursive functions well.
2390 This assumption may be broken by inlining: if symbol is interposable
2391 but the body is available (i.e. declared inline), inliner may make
2392 the body reachable even with interposition. */
2393 if (this == ref && !has_aliases_p ()
2394 && (!cnode
2395 || symtab->state >= IPA_SSA_AFTER_INLINING
2396 || get_availability () >= AVAIL_INTERPOSABLE))
2397 return true;
2400 /* References within one comdat group are always bound in a group. */
2401 if (ref
2402 && symtab->state >= IPA_SSA_AFTER_INLINING
2403 && get_comdat_group ()
2404 && get_comdat_group () == ref->get_comdat_group ())
2405 return true;
2407 return false;
2410 /* Return true if symbol should be output to the symbol table. */
2412 bool
2413 symtab_node::output_to_lto_symbol_table_p (void)
2415 /* Only externally visible symbols matter. */
2416 if (!TREE_PUBLIC (decl))
2417 return false;
2418 if (!real_symbol_p ())
2419 return false;
2420 /* FIXME: variables probably should not be considered as real symbols at
2421 first place. */
2422 if (VAR_P (decl) && DECL_HARD_REGISTER (decl))
2423 return false;
2424 if (TREE_CODE (decl) == FUNCTION_DECL && !definition
2425 && fndecl_built_in_p (decl))
2427 /* Builtins like those for most math functions have actual implementations
2428 in libraries so make sure to output references into the symbol table to
2429 make those libraries referenced. Note this is incomplete handling for
2430 now and only covers math functions. */
2431 if (builtin_with_linkage_p (decl))
2432 return true;
2433 else
2434 return false;
2437 /* We have real symbol that should be in symbol table. However try to trim
2438 down the references to libraries bit more because linker will otherwise
2439 bring unnecessary object files into the final link.
2440 FIXME: The following checks can easily be confused i.e. by self recursive
2441 function or self-referring variable. */
2443 /* We keep external functions in symtab for sake of inlining
2444 and devirtualization. We do not want to see them in symbol table as
2445 references unless they are really used. */
2446 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
2447 if (cnode && (!definition || DECL_EXTERNAL (decl))
2448 && cnode->callers)
2449 return true;
2451 /* Ignore all references from external vars initializers - they are not really
2452 part of the compilation unit until they are used by folding. Some symbols,
2453 like references to external construction vtables cannot be referred to at
2454 all. We decide this at can_refer_decl_in_current_unit_p. */
2455 if (!definition || DECL_EXTERNAL (decl))
2457 int i;
2458 struct ipa_ref *ref;
2459 for (i = 0; iterate_referring (i, ref); i++)
2461 if (ref->use == IPA_REF_ALIAS)
2462 continue;
2463 if (is_a <cgraph_node *> (ref->referring))
2464 return true;
2465 if (!DECL_EXTERNAL (ref->referring->decl))
2466 return true;
2468 return false;
2470 return true;