]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/symtab.c
Update copyright years.
[thirdparty/gcc.git] / gcc / symtab.c
1 /* Symbol table.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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"
41
42 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
43
44 const char * const ld_plugin_symbol_resolution_names[]=
45 {
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"
56 };
57
58 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at ALIAS
59 until we find an identifier that is not itself a transparent alias. */
60
61 static inline tree
62 ultimate_transparent_alias_target (tree alias)
63 {
64 tree target = alias;
65
66 while (IDENTIFIER_TRANSPARENT_ALIAS (target))
67 {
68 gcc_checking_assert (TREE_CHAIN (target));
69 target = TREE_CHAIN (target);
70 }
71 gcc_checking_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
72 && ! TREE_CHAIN (target));
73
74 return target;
75 }
76
77
78 /* Hash asmnames ignoring the user specified marks. */
79
80 hashval_t
81 symbol_table::decl_assembler_name_hash (const_tree asmname)
82 {
83 if (IDENTIFIER_POINTER (asmname)[0] == '*')
84 {
85 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
86 size_t ulp_len = strlen (user_label_prefix);
87
88 if (ulp_len == 0)
89 ;
90 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
91 decl_str += ulp_len;
92
93 return htab_hash_string (decl_str);
94 }
95
96 return htab_hash_string (IDENTIFIER_POINTER (asmname));
97 }
98
99 /* Return true if assembler names NAME1 and NAME2 leads to the same symbol
100 name. */
101
102 bool
103 symbol_table::assembler_names_equal_p (const char *name1, const char *name2)
104 {
105 if (name1 != name2)
106 {
107 if (name1[0] == '*')
108 {
109 size_t ulp_len = strlen (user_label_prefix);
110
111 name1 ++;
112
113 if (ulp_len == 0)
114 ;
115 else if (strncmp (name1, user_label_prefix, ulp_len) == 0)
116 name1 += ulp_len;
117 else
118 return false;
119 }
120 if (name2[0] == '*')
121 {
122 size_t ulp_len = strlen (user_label_prefix);
123
124 name2 ++;
125
126 if (ulp_len == 0)
127 ;
128 else if (strncmp (name2, user_label_prefix, ulp_len) == 0)
129 name2 += ulp_len;
130 else
131 return false;
132 }
133 return !strcmp (name1, name2);
134 }
135 return true;
136 }
137
138 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
139
140 bool
141 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
142 {
143 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
144 const char *decl_str;
145 const char *asmname_str;
146
147 if (decl_asmname == asmname)
148 return true;
149
150 decl_str = IDENTIFIER_POINTER (decl_asmname);
151 asmname_str = IDENTIFIER_POINTER (asmname);
152 return assembler_names_equal_p (decl_str, asmname_str);
153 }
154
155
156 /* Returns nonzero if P1 and P2 are equal. */
157
158 /* Insert NODE to assembler name hash. */
159
160 void
161 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
162 bool with_clones)
163 {
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)
169 {
170 symtab_node **aslot;
171 cgraph_node *cnode;
172 tree decl = node->decl;
173
174 tree name = DECL_ASSEMBLER_NAME (node->decl);
175
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;
180
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;
188
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);
195 }
196
197 }
198
199 /* Remove NODE from assembler name hash. */
200
201 void
202 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
203 bool with_clones)
204 {
205 if (assembler_name_hash)
206 {
207 cgraph_node *cnode;
208 tree decl = node->decl;
209
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)
214 {
215 node->previous_sharing_asm_name->next_sharing_asm_name
216 = node->next_sharing_asm_name;
217 }
218 else
219 {
220 tree name = DECL_ASSEMBLER_NAME (node->decl);
221 symtab_node **slot;
222
223 if (!name)
224 return;
225
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;
234 }
235 node->next_sharing_asm_name = NULL;
236 node->previous_sharing_asm_name = NULL;
237
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);
244 }
245 }
246
247 /* Arrange node to be first in its entry of assembler_name_hash. */
248
249 void
250 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
251 {
252 unlink_from_assembler_name_hash (node, false);
253 insert_to_assembler_name_hash (node, false);
254 }
255
256 /* Initialize asm name hash unless. */
257
258 void
259 symbol_table::symtab_initialize_asm_name_hash (void)
260 {
261 symtab_node *node;
262 if (!assembler_name_hash)
263 {
264 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
265 FOR_EACH_SYMBOL (node)
266 insert_to_assembler_name_hash (node, false);
267 }
268 }
269
270 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
271
272 void
273 symbol_table::change_decl_assembler_name (tree decl, tree name)
274 {
275 symtab_node *node = NULL;
276
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))
283 {
284 SET_DECL_ASSEMBLER_NAME (decl, name);
285 if (node)
286 insert_to_assembler_name_hash (node, true);
287 }
288 else
289 {
290 if (name == DECL_ASSEMBLER_NAME (decl))
291 return;
292
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);
298
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);
303
304 SET_DECL_ASSEMBLER_NAME (decl, name);
305 if (alias)
306 {
307 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
308 TREE_CHAIN (name) = alias;
309 }
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)
315 {
316 insert_to_assembler_name_hash (node, true);
317 ipa_ref *ref;
318 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
319 {
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))
328 {
329 gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl))
330 && IDENTIFIER_TRANSPARENT_ALIAS
331 (DECL_ASSEMBLER_NAME (alias->decl)));
332
333 TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) =
334 ultimate_transparent_alias_target
335 (DECL_ASSEMBLER_NAME (node->decl));
336 }
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
342 }
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))));
351 }
352 }
353 }
354
355 /* Hash sections by their names. */
356
357 hashval_t
358 section_name_hasher::hash (section_hash_entry *n)
359 {
360 return htab_hash_string (n->name);
361 }
362
363 /* Return true if section P1 name equals to P2. */
364
365 bool
366 section_name_hasher::equal (section_hash_entry *n1, const char *name)
367 {
368 return n1->name == name || !strcmp (n1->name, name);
369 }
370
371 /* Add node into symbol table. This function is not used directly, but via
372 cgraph/varpool node creation routines. */
373
374 void
375 symtab_node::register_symbol (void)
376 {
377 symtab->register_symbol (this);
378
379 if (!decl->decl_with_vis.symtab_node)
380 decl->decl_with_vis.symtab_node = this;
381
382 ref_list.clear ();
383
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);
387 }
388
389 /* Remove NODE from same comdat group. */
390
391 void
392 symtab_node::remove_from_same_comdat_group (void)
393 {
394 if (same_comdat_group)
395 {
396 symtab_node *prev;
397 for (prev = same_comdat_group;
398 prev->same_comdat_group != this;
399 prev = prev->same_comdat_group)
400 ;
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);
407 }
408 }
409
410 /* Remove node from symbol table. This function is not used directly, but via
411 cgraph/varpool node removal routines. */
412
413 void
414 symtab_node::unregister (void)
415 {
416 remove_all_references ();
417 remove_all_referring ();
418
419 /* Remove reference to section. */
420 set_section_for_node (NULL);
421
422 remove_from_same_comdat_group ();
423
424 symtab->unregister (this);
425
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)
430 {
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;
435 }
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);
440 }
441
442
443 /* Remove symbol from symbol table. */
444
445 void
446 symtab_node::remove (void)
447 {
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 ();
452 }
453
454 /* Add NEW_ to the same comdat group that OLD is in. */
455
456 void
457 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
458 {
459 gcc_assert (old_node->get_comdat_group ());
460 gcc_assert (!same_comdat_group);
461 gcc_assert (this != old_node);
462
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
468 {
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)
473 ;
474 n->same_comdat_group = this;
475 }
476 }
477
478 /* Dissolve the same_comdat_group list in which NODE resides. */
479
480 void
481 symtab_node::dissolve_same_comdat_group_list (void)
482 {
483 symtab_node *n = this;
484 symtab_node *next;
485
486 if (!same_comdat_group)
487 return;
488 do
489 {
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;
499 }
500 while (n != this);
501 }
502
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. */
506
507 const char *
508 symtab_node::asm_name () const
509 {
510 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
511 return name ();
512 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
513 }
514
515 /* Return printable identifier name. */
516
517 const char *
518 symtab_node::name () const
519 {
520 if (!DECL_NAME (decl))
521 {
522 if (DECL_ASSEMBLER_NAME_SET_P (decl))
523 return asm_name ();
524 else
525 return "<unnamed>";
526 }
527 return lang_hooks.decl_printable_name (decl, 2);
528 }
529
530 const char *
531 symtab_node::get_dump_name (bool asm_name_p) const
532 {
533 #define EXTRA 16
534 const char *fname = asm_name_p ? asm_name () : name ();
535 unsigned l = strlen (fname);
536
537 char *s = (char *)ggc_internal_cleared_alloc (l + EXTRA);
538 snprintf (s, l + EXTRA, "%s/%d", fname, order);
539
540 return s;
541 }
542
543 const char *
544 symtab_node::dump_name () const
545 {
546 return get_dump_name (false);
547 }
548
549 const char *
550 symtab_node::dump_asm_name () const
551 {
552 return get_dump_name (true);
553 }
554
555 /* Return ipa reference from this symtab_node to
556 REFERRED_NODE or REFERRED_VARPOOL_NODE. USE_TYPE specify type
557 of the use. */
558
559 ipa_ref *
560 symtab_node::create_reference (symtab_node *referred_node,
561 enum ipa_ref_use use_type)
562 {
563 return create_reference (referred_node, use_type, NULL);
564 }
565
566
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). */
570
571 ipa_ref *
572 symtab_node::create_reference (symtab_node *referred_node,
573 enum ipa_ref_use use_type, gimple *stmt)
574 {
575 ipa_ref *ref = NULL, *ref2 = NULL;
576 ipa_ref_list *list, *list2;
577 ipa_ref_t *old_references;
578
579 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
580 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
581
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 ();
586
587 list2 = &referred_node->ref_list;
588
589 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
590 if(use_type == IPA_REF_ALIAS)
591 {
592 list2->referring.safe_insert (0, ref);
593 ref->referred_index = 0;
594
595 for (unsigned int i = 1; i < list2->referring.length (); i++)
596 list2->referring[i]->referred_index = i;
597 }
598 else
599 {
600 list2->referring.safe_push (ref);
601 ref->referred_index = list2->referring.length () - 1;
602 }
603
604 ref->referring = this;
605 ref->referred = referred_node;
606 ref->stmt = stmt;
607 ref->lto_stmt_uid = 0;
608 ref->use = use_type;
609 ref->speculative = 0;
610
611 /* If vector was moved in memory, update pointers. */
612 if (old_references != list->references->address ())
613 {
614 int i;
615 for (i = 0; iterate_reference(i, ref2); i++)
616 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
617 }
618 return ref;
619 }
620
621 ipa_ref *
622 symtab_node::maybe_create_reference (tree val, gimple *stmt)
623 {
624 STRIP_NOPS (val);
625 ipa_ref_use use_type;
626
627 switch (TREE_CODE (val))
628 {
629 case VAR_DECL:
630 use_type = IPA_REF_LOAD;
631 break;
632 case ADDR_EXPR:
633 use_type = IPA_REF_ADDR;
634 break;
635 default:
636 gcc_assert (!handled_component_p (val));
637 return NULL;
638 }
639
640 val = get_base_var (val);
641 if (val && VAR_OR_FUNCTION_DECL_P (val))
642 {
643 symtab_node *referred = symtab_node::get (val);
644 gcc_checking_assert (referred);
645 return create_reference (referred, use_type, stmt);
646 }
647 return NULL;
648 }
649
650 /* Clone all references from symtab NODE to this symtab_node. */
651
652 void
653 symtab_node::clone_references (symtab_node *node)
654 {
655 ipa_ref *ref = NULL, *ref2 = NULL;
656 int i;
657 for (i = 0; node->iterate_reference (i, ref); i++)
658 {
659 bool speculative = ref->speculative;
660 unsigned int stmt_uid = ref->lto_stmt_uid;
661
662 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
663 ref2->speculative = speculative;
664 ref2->lto_stmt_uid = stmt_uid;
665 }
666 }
667
668 /* Clone all referring from symtab NODE to this symtab_node. */
669
670 void
671 symtab_node::clone_referring (symtab_node *node)
672 {
673 ipa_ref *ref = NULL, *ref2 = NULL;
674 int i;
675 for (i = 0; node->iterate_referring(i, ref); i++)
676 {
677 bool speculative = ref->speculative;
678 unsigned int stmt_uid = ref->lto_stmt_uid;
679
680 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
681 ref2->speculative = speculative;
682 ref2->lto_stmt_uid = stmt_uid;
683 }
684 }
685
686 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
687
688 ipa_ref *
689 symtab_node::clone_reference (ipa_ref *ref, gimple *stmt)
690 {
691 bool speculative = ref->speculative;
692 unsigned int stmt_uid = ref->lto_stmt_uid;
693 ipa_ref *ref2;
694
695 ref2 = create_reference (ref->referred, ref->use, stmt);
696 ref2->speculative = speculative;
697 ref2->lto_stmt_uid = stmt_uid;
698 return ref2;
699 }
700
701 /* Find the structure describing a reference to REFERRED_NODE
702 and associated with statement STMT. */
703
704 ipa_ref *
705 symtab_node::find_reference (symtab_node *referred_node,
706 gimple *stmt, unsigned int lto_stmt_uid)
707 {
708 ipa_ref *r = NULL;
709 int i;
710
711 for (i = 0; iterate_reference (i, r); i++)
712 if (r->referred == referred_node
713 && !r->speculative
714 && ((stmt && r->stmt == stmt)
715 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
716 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
717 return r;
718 return NULL;
719 }
720
721 /* Remove all references that are associated with statement STMT. */
722
723 void
724 symtab_node::remove_stmt_references (gimple *stmt)
725 {
726 ipa_ref *r = NULL;
727 int i = 0;
728
729 while (iterate_reference (i, r))
730 if (r->stmt == stmt)
731 r->remove_reference ();
732 else
733 i++;
734 }
735
736 /* Remove all stmt references in non-speculative references.
737 Those are not maintained during inlining & cloning.
738 The exception are speculative references that are updated along
739 with callgraph edges associated with them. */
740
741 void
742 symtab_node::clear_stmts_in_references (void)
743 {
744 ipa_ref *r = NULL;
745 int i;
746
747 for (i = 0; iterate_reference (i, r); i++)
748 if (!r->speculative)
749 {
750 r->stmt = NULL;
751 r->lto_stmt_uid = 0;
752 }
753 }
754
755 /* Remove all references in ref list. */
756
757 void
758 symtab_node::remove_all_references (void)
759 {
760 while (vec_safe_length (ref_list.references))
761 ref_list.references->last ().remove_reference ();
762 vec_free (ref_list.references);
763 }
764
765 /* Remove all referring items in ref list. */
766
767 void
768 symtab_node::remove_all_referring (void)
769 {
770 while (ref_list.referring.length ())
771 ref_list.referring.last ()->remove_reference ();
772 ref_list.referring.release ();
773 }
774
775 /* Dump references in ref list to FILE. */
776
777 void
778 symtab_node::dump_references (FILE *file)
779 {
780 ipa_ref *ref = NULL;
781 int i;
782 for (i = 0; iterate_reference (i, ref); i++)
783 {
784 fprintf (file, "%s (%s)",
785 ref->referred->dump_asm_name (),
786 ipa_ref_use_name [ref->use]);
787 if (ref->speculative)
788 fprintf (file, " (speculative)");
789 }
790 fprintf (file, "\n");
791 }
792
793 /* Dump referring in list to FILE. */
794
795 void
796 symtab_node::dump_referring (FILE *file)
797 {
798 ipa_ref *ref = NULL;
799 int i;
800 for (i = 0; iterate_referring(i, ref); i++)
801 {
802 fprintf (file, "%s (%s)",
803 ref->referring->dump_asm_name (),
804 ipa_ref_use_name [ref->use]);
805 if (ref->speculative)
806 fprintf (file, " (speculative)");
807 }
808 fprintf (file, "\n");
809 }
810
811 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
812
813 /* Dump the visibility of the symbol. */
814
815 const char *
816 symtab_node::get_visibility_string () const
817 {
818 static const char * const visibility_types[]
819 = { "default", "protected", "hidden", "internal" };
820 return visibility_types[DECL_VISIBILITY (decl)];
821 }
822
823 /* Dump the type_name of the symbol. */
824 const char *
825 symtab_node::get_symtab_type_string () const
826 {
827 return symtab_type_names[type];
828 }
829
830 /* Dump base fields of symtab nodes to F. Not to be used directly. */
831
832 void
833 symtab_node::dump_base (FILE *f)
834 {
835 static const char * const visibility_types[] = {
836 "default", "protected", "hidden", "internal"
837 };
838
839 fprintf (f, "%s (%s)", dump_asm_name (), name ());
840 dump_addr (f, " @", (void *)this);
841 fprintf (f, "\n Type: %s", symtab_type_names[type]);
842
843 if (definition)
844 fprintf (f, " definition");
845 if (analyzed)
846 fprintf (f, " analyzed");
847 if (alias)
848 fprintf (f, " alias");
849 if (transparent_alias)
850 fprintf (f, " transparent_alias");
851 if (weakref)
852 fprintf (f, " weakref");
853 if (symver)
854 fprintf (f, " symver");
855 if (cpp_implicit_alias)
856 fprintf (f, " cpp_implicit_alias");
857 if (alias_target)
858 fprintf (f, " target:%s",
859 DECL_P (alias_target)
860 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
861 (alias_target))
862 : IDENTIFIER_POINTER (alias_target));
863 if (body_removed)
864 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
865 fprintf (f, "\n Visibility:");
866 if (in_other_partition)
867 fprintf (f, " in_other_partition");
868 if (used_from_other_partition)
869 fprintf (f, " used_from_other_partition");
870 if (force_output)
871 fprintf (f, " force_output");
872 if (forced_by_abi)
873 fprintf (f, " forced_by_abi");
874 if (externally_visible)
875 fprintf (f, " externally_visible");
876 if (no_reorder)
877 fprintf (f, " no_reorder");
878 if (resolution != LDPR_UNKNOWN)
879 fprintf (f, " %s",
880 ld_plugin_symbol_resolution_names[(int)resolution]);
881 if (TREE_ASM_WRITTEN (decl))
882 fprintf (f, " asm_written");
883 if (DECL_EXTERNAL (decl))
884 fprintf (f, " external");
885 if (TREE_PUBLIC (decl))
886 fprintf (f, " public");
887 if (DECL_COMMON (decl))
888 fprintf (f, " common");
889 if (DECL_WEAK (decl))
890 fprintf (f, " weak");
891 if (DECL_DLLIMPORT_P (decl))
892 fprintf (f, " dll_import");
893 if (DECL_COMDAT (decl))
894 fprintf (f, " comdat");
895 if (get_comdat_group ())
896 fprintf (f, " comdat_group:%s",
897 IDENTIFIER_POINTER (get_comdat_group_id ()));
898 if (DECL_ONE_ONLY (decl))
899 fprintf (f, " one_only");
900 if (get_section ())
901 fprintf (f, " section:%s",
902 get_section ());
903 if (implicit_section)
904 fprintf (f," (implicit_section)");
905 if (DECL_VISIBILITY_SPECIFIED (decl))
906 fprintf (f, " visibility_specified");
907 if (DECL_VISIBILITY (decl))
908 fprintf (f, " visibility:%s",
909 visibility_types [DECL_VISIBILITY (decl)]);
910 if (DECL_VIRTUAL_P (decl))
911 fprintf (f, " virtual");
912 if (DECL_ARTIFICIAL (decl))
913 fprintf (f, " artificial");
914 if (TREE_CODE (decl) == FUNCTION_DECL)
915 {
916 if (DECL_STATIC_CONSTRUCTOR (decl))
917 fprintf (f, " constructor");
918 if (DECL_STATIC_DESTRUCTOR (decl))
919 fprintf (f, " destructor");
920 }
921 if (ifunc_resolver)
922 fprintf (f, " ifunc_resolver");
923 fprintf (f, "\n");
924
925 if (same_comdat_group)
926 fprintf (f, " Same comdat group as: %s\n",
927 same_comdat_group->dump_asm_name ());
928 if (next_sharing_asm_name)
929 fprintf (f, " next sharing asm name: %i\n",
930 next_sharing_asm_name->order);
931 if (previous_sharing_asm_name)
932 fprintf (f, " previous sharing asm name: %i\n",
933 previous_sharing_asm_name->order);
934
935 if (address_taken)
936 fprintf (f, " Address is taken.\n");
937 if (aux)
938 {
939 fprintf (f, " Aux:");
940 dump_addr (f, " @", (void *)aux);
941 fprintf (f, "\n");
942 }
943
944 fprintf (f, " References: ");
945 dump_references (f);
946 fprintf (f, " Referring: ");
947 dump_referring (f);
948 if (lto_file_data)
949 fprintf (f, " Read from file: %s\n",
950 lto_file_data->file_name);
951 }
952
953 /* Dump symtab node to F. */
954
955 void
956 symtab_node::dump (FILE *f)
957 {
958 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
959 cnode->dump (f);
960 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
961 vnode->dump (f);
962 }
963
964 void
965 symtab_node::dump_graphviz (FILE *f)
966 {
967 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
968 cnode->dump_graphviz (f);
969 }
970
971 void
972 symbol_table::dump (FILE *f)
973 {
974 symtab_node *node;
975 fprintf (f, "Symbol table:\n\n");
976 FOR_EACH_SYMBOL (node)
977 node->dump (f);
978 }
979
980 void
981 symbol_table::dump_graphviz (FILE *f)
982 {
983 symtab_node *node;
984 fprintf (f, "digraph symtab {\n");
985 FOR_EACH_SYMBOL (node)
986 node->dump_graphviz (f);
987 fprintf (f, "}\n");
988 }
989
990 DEBUG_FUNCTION void
991 symbol_table::debug (void)
992 {
993 dump (stderr);
994 }
995
996 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
997 Return NULL if there's no such node. */
998
999 symtab_node *
1000 symtab_node::get_for_asmname (const_tree asmname)
1001 {
1002 symtab_node *node;
1003
1004 symtab->symtab_initialize_asm_name_hash ();
1005 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
1006 symtab_node **slot
1007 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
1008 NO_INSERT);
1009
1010 if (slot)
1011 {
1012 node = *slot;
1013 return node;
1014 }
1015 return NULL;
1016 }
1017
1018 /* Dump symtab node NODE to stderr. */
1019
1020 DEBUG_FUNCTION void
1021 symtab_node::debug (void)
1022 {
1023 dump (stderr);
1024 }
1025
1026 /* Verify common part of symtab nodes. */
1027
1028 #if __GNUC__ >= 10
1029 /* Disable warnings about missing quoting in GCC diagnostics for
1030 the verification errors. Their format strings don't follow GCC
1031 diagnostic conventions and the calls are ultimately followed by
1032 one to internal_error. */
1033 # pragma GCC diagnostic push
1034 # pragma GCC diagnostic ignored "-Wformat-diag"
1035 #endif
1036
1037 DEBUG_FUNCTION bool
1038 symtab_node::verify_base (void)
1039 {
1040 bool error_found = false;
1041 symtab_node *hashed_node;
1042
1043 if (is_a <cgraph_node *> (this))
1044 {
1045 if (TREE_CODE (decl) != FUNCTION_DECL)
1046 {
1047 error ("function symbol is not function");
1048 error_found = true;
1049 }
1050 else if ((lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl))
1051 != NULL)
1052 != dyn_cast <cgraph_node *> (this)->ifunc_resolver)
1053 {
1054 error ("inconsistent %<ifunc%> attribute");
1055 error_found = true;
1056 }
1057 }
1058 else if (is_a <varpool_node *> (this))
1059 {
1060 if (!VAR_P (decl))
1061 {
1062 error ("variable symbol is not variable");
1063 error_found = true;
1064 }
1065 }
1066 else
1067 {
1068 error ("node has unknown type");
1069 error_found = true;
1070 }
1071
1072 if (symtab->state != LTO_STREAMING)
1073 {
1074 hashed_node = symtab_node::get (decl);
1075 if (!hashed_node)
1076 {
1077 error ("node not found node->decl->decl_with_vis.symtab_node");
1078 error_found = true;
1079 }
1080 if (hashed_node != this
1081 && (!is_a <cgraph_node *> (this)
1082 || !dyn_cast <cgraph_node *> (this)->clone_of
1083 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
1084 {
1085 error ("node differs from node->decl->decl_with_vis.symtab_node");
1086 error_found = true;
1087 }
1088 }
1089 if (symtab->assembler_name_hash)
1090 {
1091 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
1092 if (hashed_node)
1093 {
1094 if (hashed_node->previous_sharing_asm_name)
1095 {
1096 error ("assembler name hash list corrupted");
1097 error_found = true;
1098 }
1099 else if (previous_sharing_asm_name == NULL)
1100 {
1101 if (hashed_node != this)
1102 {
1103 error ("assembler name hash list corrupted");
1104 error_found = true;
1105 }
1106 }
1107 else if (!(is_a <varpool_node *> (this) && DECL_HARD_REGISTER (decl)))
1108 {
1109 if (!asmname_hasher::equal (previous_sharing_asm_name,
1110 DECL_ASSEMBLER_NAME (decl)))
1111 {
1112 error ("node not found in symtab assembler name hash");
1113 error_found = true;
1114 }
1115 }
1116 }
1117 }
1118 if (previous_sharing_asm_name
1119 && previous_sharing_asm_name->next_sharing_asm_name != this)
1120 {
1121 error ("double linked list of assembler names corrupted");
1122 error_found = true;
1123 }
1124 if (body_removed && definition)
1125 {
1126 error ("node has body_removed but is definition");
1127 error_found = true;
1128 }
1129 if (analyzed && !definition)
1130 {
1131 error ("node is analyzed but it is not a definition");
1132 error_found = true;
1133 }
1134 if (cpp_implicit_alias && !alias)
1135 {
1136 error ("node is alias but not implicit alias");
1137 error_found = true;
1138 }
1139 if (alias && !definition && !weakref)
1140 {
1141 error ("node is alias but not definition");
1142 error_found = true;
1143 }
1144 if (weakref && !transparent_alias)
1145 {
1146 error ("node is weakref but not an transparent_alias");
1147 error_found = true;
1148 }
1149 if (transparent_alias && !alias)
1150 {
1151 error ("node is transparent_alias but not an alias");
1152 error_found = true;
1153 }
1154 if (symver && !alias)
1155 {
1156 error ("node is symver but not alias");
1157 error_found = true;
1158 }
1159 /* Limitation of gas requires us to output targets of symver aliases as
1160 global symbols. This is binutils PR 25295. */
1161 if (symver
1162 && (!TREE_PUBLIC (get_alias_target ()->decl)
1163 || DECL_VISIBILITY (get_alias_target ()->decl) != VISIBILITY_DEFAULT))
1164 {
1165 error ("symver target is not exported with default visibility");
1166 error_found = true;
1167 }
1168 if (symver
1169 && (!TREE_PUBLIC (decl)
1170 || DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT))
1171 {
1172 error ("symver is not exported with default visibility");
1173 error_found = true;
1174 }
1175 if (same_comdat_group)
1176 {
1177 symtab_node *n = same_comdat_group;
1178
1179 if (!n->get_comdat_group ())
1180 {
1181 error ("node is in same_comdat_group list but has no comdat_group");
1182 error_found = true;
1183 }
1184 if (n->get_comdat_group () != get_comdat_group ())
1185 {
1186 error ("same_comdat_group list across different groups");
1187 error_found = true;
1188 }
1189 if (n->type != type)
1190 {
1191 error ("mixing different types of symbol in same comdat groups is not supported");
1192 error_found = true;
1193 }
1194 if (n == this)
1195 {
1196 error ("node is alone in a comdat group");
1197 error_found = true;
1198 }
1199 do
1200 {
1201 if (!n->same_comdat_group)
1202 {
1203 error ("same_comdat_group is not a circular list");
1204 error_found = true;
1205 break;
1206 }
1207 n = n->same_comdat_group;
1208 }
1209 while (n != this);
1210 if (comdat_local_p ())
1211 {
1212 ipa_ref *ref = NULL;
1213
1214 for (int i = 0; iterate_referring (i, ref); ++i)
1215 {
1216 if (!in_same_comdat_group_p (ref->referring))
1217 {
1218 error ("comdat-local symbol referred to by %s outside its "
1219 "comdat",
1220 identifier_to_locale (ref->referring->name()));
1221 error_found = true;
1222 }
1223 }
1224 }
1225 }
1226 if (implicit_section && !get_section ())
1227 {
1228 error ("implicit_section flag is set but section isn%'t");
1229 error_found = true;
1230 }
1231 if (get_section () && get_comdat_group ()
1232 && !implicit_section
1233 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1234 {
1235 error ("Both section and comdat group is set");
1236 error_found = true;
1237 }
1238 /* TODO: Add string table for sections, so we do not keep holding duplicated
1239 strings. */
1240 if (alias && definition
1241 && get_section () != get_alias_target ()->get_section ()
1242 && (!get_section()
1243 || !get_alias_target ()->get_section ()
1244 || strcmp (get_section(),
1245 get_alias_target ()->get_section ())))
1246 {
1247 error ("Alias and target%'s section differs");
1248 get_alias_target ()->dump (stderr);
1249 error_found = true;
1250 }
1251 if (alias && definition
1252 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1253 {
1254 error ("Alias and target%'s comdat groups differs");
1255 get_alias_target ()->dump (stderr);
1256 error_found = true;
1257 }
1258 if (transparent_alias && definition && !weakref)
1259 {
1260 symtab_node *to = get_alias_target ();
1261 const char *name1
1262 = IDENTIFIER_POINTER (
1263 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl)));
1264 const char *name2
1265 = IDENTIFIER_POINTER (
1266 ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to->decl)));
1267 if (!symbol_table::assembler_names_equal_p (name1, name2))
1268 {
1269 error ("Transparent alias and target%'s assembler names differs");
1270 get_alias_target ()->dump (stderr);
1271 error_found = true;
1272 }
1273 }
1274 if (transparent_alias && definition
1275 && get_alias_target()->transparent_alias && get_alias_target()->analyzed)
1276 {
1277 error ("Chained transparent aliases");
1278 get_alias_target ()->dump (stderr);
1279 error_found = true;
1280 }
1281
1282 return error_found;
1283 }
1284
1285 /* Verify consistency of NODE. */
1286
1287 DEBUG_FUNCTION void
1288 symtab_node::verify (void)
1289 {
1290 if (seen_error ())
1291 return;
1292
1293 timevar_push (TV_CGRAPH_VERIFY);
1294 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1295 node->verify_node ();
1296 else
1297 if (verify_base ())
1298 {
1299 debug ();
1300 internal_error ("symtab_node::verify failed");
1301 }
1302 timevar_pop (TV_CGRAPH_VERIFY);
1303 }
1304
1305 /* Verify symbol table for internal consistency. */
1306
1307 DEBUG_FUNCTION void
1308 symtab_node::verify_symtab_nodes (void)
1309 {
1310 symtab_node *node;
1311 hash_map<tree, symtab_node *> comdat_head_map (251);
1312
1313 FOR_EACH_SYMBOL (node)
1314 {
1315 node->verify ();
1316 if (node->get_comdat_group ())
1317 {
1318 symtab_node **entry, *s;
1319 bool existed;
1320
1321 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1322 &existed);
1323 if (!existed)
1324 *entry = node;
1325 else if (!DECL_EXTERNAL (node->decl))
1326 {
1327 for (s = (*entry)->same_comdat_group;
1328 s != NULL && s != node && s != *entry;
1329 s = s->same_comdat_group)
1330 ;
1331 if (!s || s == *entry)
1332 {
1333 error ("Two symbols with same comdat_group are not linked by "
1334 "the same_comdat_group list.");
1335 (*entry)->debug ();
1336 node->debug ();
1337 internal_error ("symtab_node::verify failed");
1338 }
1339 }
1340 }
1341 }
1342 }
1343
1344 #if __GNUC__ >= 10
1345 # pragma GCC diagnostic pop
1346 #endif
1347
1348 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1349 but other code such as notice_global_symbol generates rtl. */
1350
1351 void
1352 symtab_node::make_decl_local (void)
1353 {
1354 rtx rtl, symbol;
1355
1356 if (weakref)
1357 {
1358 weakref = false;
1359 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) = 0;
1360 TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) = NULL_TREE;
1361 symtab->change_decl_assembler_name
1362 (decl, DECL_ASSEMBLER_NAME (get_alias_target ()->decl));
1363 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1364 DECL_ATTRIBUTES (decl));
1365 }
1366 /* Avoid clearing comdat_groups on comdat-local decls. */
1367 else if (TREE_PUBLIC (decl) == 0)
1368 return;
1369
1370 /* Localizing a symbol also make all its transparent aliases local. */
1371 ipa_ref *ref;
1372 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1373 {
1374 struct symtab_node *alias = ref->referring;
1375 if (alias->transparent_alias)
1376 alias->make_decl_local ();
1377 }
1378
1379 if (VAR_P (decl))
1380 {
1381 DECL_COMMON (decl) = 0;
1382 /* ADDRESSABLE flag is not defined for public symbols. */
1383 TREE_ADDRESSABLE (decl) = 1;
1384 TREE_STATIC (decl) = 1;
1385 }
1386 else
1387 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1388
1389 DECL_COMDAT (decl) = 0;
1390 DECL_WEAK (decl) = 0;
1391 DECL_EXTERNAL (decl) = 0;
1392 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1393 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1394 TREE_PUBLIC (decl) = 0;
1395 DECL_DLLIMPORT_P (decl) = 0;
1396 if (!DECL_RTL_SET_P (decl))
1397 return;
1398
1399 /* Update rtl flags. */
1400 make_decl_rtl (decl);
1401
1402 rtl = DECL_RTL (decl);
1403 if (!MEM_P (rtl))
1404 return;
1405
1406 symbol = XEXP (rtl, 0);
1407 if (GET_CODE (symbol) != SYMBOL_REF)
1408 return;
1409
1410 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1411 }
1412
1413 /* Copy visibility from N.
1414 This is useful when THIS becomes a transparent alias of N. */
1415
1416 void
1417 symtab_node::copy_visibility_from (symtab_node *n)
1418 {
1419 gcc_checking_assert (n->weakref == weakref);
1420
1421 ipa_ref *ref;
1422 for (unsigned i = 0; iterate_direct_aliases (i, ref); i++)
1423 {
1424 struct symtab_node *alias = ref->referring;
1425 if (alias->transparent_alias)
1426 alias->copy_visibility_from (n);
1427 }
1428
1429 if (VAR_P (decl))
1430 {
1431 DECL_COMMON (decl) = DECL_COMMON (n->decl);
1432 /* ADDRESSABLE flag is not defined for public symbols. */
1433 if (TREE_PUBLIC (decl) && !TREE_PUBLIC (n->decl))
1434 TREE_ADDRESSABLE (decl) = 1;
1435 TREE_STATIC (decl) = TREE_STATIC (n->decl);
1436 }
1437 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1438
1439 DECL_COMDAT (decl) = DECL_COMDAT (n->decl);
1440 DECL_WEAK (decl) = DECL_WEAK (n->decl);
1441 DECL_EXTERNAL (decl) = DECL_EXTERNAL (n->decl);
1442 DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (n->decl);
1443 DECL_VISIBILITY (decl) = DECL_VISIBILITY (n->decl);
1444 TREE_PUBLIC (decl) = TREE_PUBLIC (n->decl);
1445 DECL_DLLIMPORT_P (decl) = DECL_DLLIMPORT_P (n->decl);
1446 resolution = n->resolution;
1447 set_comdat_group (n->get_comdat_group ());
1448 call_for_symbol_and_aliases (symtab_node::set_section,
1449 const_cast<char *>(n->get_section ()), true);
1450 externally_visible = n->externally_visible;
1451 if (!DECL_RTL_SET_P (decl))
1452 return;
1453
1454 /* Update rtl flags. */
1455 make_decl_rtl (decl);
1456
1457 rtx rtl = DECL_RTL (decl);
1458 if (!MEM_P (rtl))
1459 return;
1460
1461 rtx symbol = XEXP (rtl, 0);
1462 if (GET_CODE (symbol) != SYMBOL_REF)
1463 return;
1464
1465 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1466 }
1467
1468 /* Walk the alias chain to return the symbol NODE is alias of.
1469 If NODE is not an alias, return NODE.
1470 Assumes NODE is known to be alias. */
1471
1472 symtab_node *
1473 symtab_node::ultimate_alias_target_1 (enum availability *availability,
1474 symtab_node *ref)
1475 {
1476 bool transparent_p = false;
1477
1478 /* To determine visibility of the target, we follow ELF semantic of aliases.
1479 Here alias is an alternative assembler name of a given definition. Its
1480 availability prevails the availability of its target (i.e. static alias of
1481 weak definition is available.
1482
1483 Transparent alias is just alternative name of a given symbol used within
1484 one compilation unit and is translated prior hitting the object file. It
1485 inherits the visibility of its target.
1486 Weakref is a different animal (and noweak definition is weak).
1487
1488 If we ever get into supporting targets with different semantics, a target
1489 hook will be needed here. */
1490
1491 if (availability)
1492 {
1493 transparent_p = transparent_alias;
1494 if (!transparent_p)
1495 *availability = get_availability (ref);
1496 else
1497 *availability = AVAIL_NOT_AVAILABLE;
1498 }
1499
1500 symtab_node *node = this;
1501 while (node)
1502 {
1503 if (node->alias && node->analyzed)
1504 node = node->get_alias_target ();
1505 else
1506 {
1507 if (!availability || (!transparent_p && node->analyzed))
1508 ;
1509 else if (node->analyzed && !node->transparent_alias)
1510 *availability = node->get_availability (ref);
1511 else
1512 *availability = AVAIL_NOT_AVAILABLE;
1513 return node;
1514 }
1515 if (node && availability && transparent_p
1516 && node->transparent_alias)
1517 {
1518 *availability = node->get_availability (ref);
1519 transparent_p = false;
1520 }
1521 }
1522 if (availability)
1523 *availability = AVAIL_NOT_AVAILABLE;
1524 return NULL;
1525 }
1526
1527 /* C++ FE sometimes change linkage flags after producing same body aliases.
1528
1529 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1530 are obviously equivalent. The way it is doing so is however somewhat
1531 kludgy and interferes with the visibility code. As a result we need to
1532 copy the visibility from the target to get things right. */
1533
1534 void
1535 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1536 {
1537 if (is_a <cgraph_node *> (this))
1538 {
1539 DECL_DECLARED_INLINE_P (decl)
1540 = DECL_DECLARED_INLINE_P (target->decl);
1541 DECL_DISREGARD_INLINE_LIMITS (decl)
1542 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1543 }
1544 /* FIXME: It is not really clear why those flags should not be copied for
1545 functions, too. */
1546 else
1547 {
1548 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1549 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1550 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1551 }
1552 if (TREE_PUBLIC (decl))
1553 {
1554 tree group;
1555
1556 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1557 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1558 group = target->get_comdat_group ();
1559 set_comdat_group (group);
1560 if (group && !same_comdat_group)
1561 add_to_same_comdat_group (target);
1562 }
1563 externally_visible = target->externally_visible;
1564 }
1565
1566 /* Set section, do not recurse into aliases.
1567 When one wants to change section of a symbol and its aliases,
1568 use set_section. */
1569
1570 void
1571 symtab_node::set_section_for_node (const char *section)
1572 {
1573 const char *current = get_section ();
1574 section_hash_entry **slot;
1575
1576 if (current == section
1577 || (current && section
1578 && !strcmp (current, section)))
1579 return;
1580
1581 if (current)
1582 {
1583 x_section->ref_count--;
1584 if (!x_section->ref_count)
1585 {
1586 hashval_t hash = htab_hash_string (x_section->name);
1587 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1588 hash, INSERT);
1589 ggc_free (x_section);
1590 symtab->section_hash->clear_slot (slot);
1591 }
1592 x_section = NULL;
1593 }
1594 if (!section)
1595 {
1596 implicit_section = false;
1597 return;
1598 }
1599 if (!symtab->section_hash)
1600 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1601 slot = symtab->section_hash->find_slot_with_hash (section,
1602 htab_hash_string (section),
1603 INSERT);
1604 if (*slot)
1605 x_section = (section_hash_entry *)*slot;
1606 else
1607 {
1608 int len = strlen (section);
1609 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1610 x_section->name = ggc_vec_alloc<char> (len + 1);
1611 memcpy (x_section->name, section, len + 1);
1612 }
1613 x_section->ref_count++;
1614 }
1615
1616 /* Worker for set_section. */
1617
1618 bool
1619 symtab_node::set_section (symtab_node *n, void *s)
1620 {
1621 n->set_section_for_node ((char *)s);
1622 return false;
1623 }
1624
1625 /* Set section of symbol and its aliases. */
1626
1627 void
1628 symtab_node::set_section (const char *section)
1629 {
1630 gcc_assert (!this->alias || !this->analyzed);
1631 call_for_symbol_and_aliases
1632 (symtab_node::set_section, const_cast<char *>(section), true);
1633 }
1634
1635 /* Return the initialization priority. */
1636
1637 priority_type
1638 symtab_node::get_init_priority ()
1639 {
1640 if (!this->in_init_priority_hash)
1641 return DEFAULT_INIT_PRIORITY;
1642
1643 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1644 return h ? h->init : DEFAULT_INIT_PRIORITY;
1645 }
1646
1647 /* Return the finalization priority. */
1648
1649 priority_type
1650 cgraph_node::get_fini_priority ()
1651 {
1652 if (!this->in_init_priority_hash)
1653 return DEFAULT_INIT_PRIORITY;
1654 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1655 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1656 }
1657
1658 /* Return the initialization and finalization priority information for
1659 DECL. If there is no previous priority information, a freshly
1660 allocated structure is returned. */
1661
1662 symbol_priority_map *
1663 symtab_node::priority_info (void)
1664 {
1665 if (!symtab->init_priority_hash)
1666 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1667
1668 bool existed;
1669 symbol_priority_map *h
1670 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1671 if (!existed)
1672 {
1673 h->init = DEFAULT_INIT_PRIORITY;
1674 h->fini = DEFAULT_INIT_PRIORITY;
1675 in_init_priority_hash = true;
1676 }
1677
1678 return h;
1679 }
1680
1681 /* Set initialization priority to PRIORITY. */
1682
1683 void
1684 symtab_node::set_init_priority (priority_type priority)
1685 {
1686 symbol_priority_map *h;
1687
1688 if (is_a <cgraph_node *> (this))
1689 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1690
1691 if (priority == DEFAULT_INIT_PRIORITY)
1692 {
1693 gcc_assert (get_init_priority() == priority);
1694 return;
1695 }
1696 h = priority_info ();
1697 h->init = priority;
1698 }
1699
1700 /* Set finalization priority to PRIORITY. */
1701
1702 void
1703 cgraph_node::set_fini_priority (priority_type priority)
1704 {
1705 symbol_priority_map *h;
1706
1707 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1708
1709 if (priority == DEFAULT_INIT_PRIORITY)
1710 {
1711 gcc_assert (get_fini_priority() == priority);
1712 return;
1713 }
1714 h = priority_info ();
1715 h->fini = priority;
1716 }
1717
1718 /* Worker for symtab_resolve_alias. */
1719
1720 bool
1721 symtab_node::set_implicit_section (symtab_node *n,
1722 void *data ATTRIBUTE_UNUSED)
1723 {
1724 n->implicit_section = true;
1725 return false;
1726 }
1727
1728 /* Add reference recording that symtab node is alias of TARGET.
1729 The function can fail in the case of aliasing cycles; in this case
1730 it returns false. */
1731
1732 bool
1733 symtab_node::resolve_alias (symtab_node *target, bool transparent)
1734 {
1735 symtab_node *n;
1736
1737 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1738
1739 /* Never let cycles to creep into the symbol table alias references;
1740 those will make alias walkers to be infinite. */
1741 for (n = target; n && n->alias;
1742 n = n->analyzed ? n->get_alias_target () : NULL)
1743 if (n == this)
1744 {
1745 if (is_a <cgraph_node *> (this))
1746 error ("function %q+D part of alias cycle", decl);
1747 else if (is_a <varpool_node *> (this))
1748 error ("variable %q+D part of alias cycle", decl);
1749 else
1750 gcc_unreachable ();
1751 alias = false;
1752 return false;
1753 }
1754
1755 /* "analyze" the node - i.e. mark the reference. */
1756 definition = true;
1757 alias = true;
1758 analyzed = true;
1759 transparent |= transparent_alias;
1760 transparent_alias = transparent;
1761 if (transparent)
1762 while (target->transparent_alias && target->analyzed)
1763 target = target->get_alias_target ();
1764 create_reference (target, IPA_REF_ALIAS, NULL);
1765
1766 /* Add alias into the comdat group of its target unless it is already there. */
1767 if (same_comdat_group)
1768 remove_from_same_comdat_group ();
1769 set_comdat_group (NULL);
1770 if (target->get_comdat_group ())
1771 add_to_same_comdat_group (target);
1772
1773 if ((get_section () != target->get_section ()
1774 || target->get_comdat_group ()) && get_section () && !implicit_section)
1775 {
1776 error ("section of alias %q+D must match section of its target", decl);
1777 }
1778 call_for_symbol_and_aliases (symtab_node::set_section,
1779 const_cast<char *>(target->get_section ()), true);
1780 if (target->implicit_section)
1781 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1782
1783 /* Alias targets become redundant after alias is resolved into an reference.
1784 We do not want to keep it around or we would have to mind updating them
1785 when renaming symbols. */
1786 alias_target = NULL;
1787
1788 if (!transparent && cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1789 fixup_same_cpp_alias_visibility (target);
1790
1791 /* If alias has address taken, so does the target. */
1792 if (address_taken)
1793 target->ultimate_alias_target ()->address_taken = true;
1794
1795 /* All non-transparent aliases of THIS are now in fact aliases of TARGET.
1796 If alias is transparent, also all transparent aliases of THIS are now
1797 aliases of TARGET.
1798 Also merge same comdat group lists. */
1799 ipa_ref *ref;
1800 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1801 {
1802 struct symtab_node *alias_alias = ref->referring;
1803 if (alias_alias->get_comdat_group ())
1804 {
1805 alias_alias->remove_from_same_comdat_group ();
1806 alias_alias->set_comdat_group (NULL);
1807 if (target->get_comdat_group ())
1808 alias_alias->add_to_same_comdat_group (target);
1809 }
1810 if ((!alias_alias->transparent_alias
1811 && !alias_alias->symver)
1812 || transparent)
1813 {
1814 alias_alias->remove_all_references ();
1815 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1816 }
1817 else i++;
1818 }
1819 return true;
1820 }
1821
1822 /* Worker searching noninterposable alias. */
1823
1824 bool
1825 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1826 {
1827 if (!node->transparent_alias && decl_binds_to_current_def_p (node->decl))
1828 {
1829 symtab_node *fn = node->ultimate_alias_target ();
1830
1831 /* Ensure that the alias is well formed this may not be the case
1832 of user defined aliases and currently it is not always the case
1833 of C++ same body aliases (that is a bug). */
1834 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1835 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1836 || (TREE_CODE (node->decl) == FUNCTION_DECL
1837 && flags_from_decl_or_type (node->decl)
1838 != flags_from_decl_or_type (fn->decl))
1839 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1840 return false;
1841 *(symtab_node **)data = node;
1842 return true;
1843 }
1844 return false;
1845 }
1846
1847 /* If node cannot be overwriten by static or dynamic linker to point to
1848 different definition, return NODE. Otherwise look for alias with such
1849 property and if none exists, introduce new one. */
1850
1851 symtab_node *
1852 symtab_node::noninterposable_alias (void)
1853 {
1854 tree new_decl;
1855 symtab_node *new_node = NULL;
1856
1857 /* First try to look up existing alias or base object
1858 (if that is already non-overwritable). */
1859 symtab_node *node = ultimate_alias_target ();
1860 gcc_assert (!node->alias && !node->weakref);
1861 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1862 (void *)&new_node, true);
1863 if (new_node)
1864 return new_node;
1865
1866 /* If aliases aren't supported by the assembler, fail. */
1867 if (!TARGET_SUPPORTS_ALIASES)
1868 return NULL;
1869
1870 /* Otherwise create a new one. */
1871 new_decl = copy_node (node->decl);
1872 DECL_DLLIMPORT_P (new_decl) = 0;
1873 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1874 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1875 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1876 DECL_INITIAL (new_decl) = NULL;
1877 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1878 SET_DECL_RTL (new_decl, NULL);
1879
1880 /* Update the properties. */
1881 DECL_EXTERNAL (new_decl) = 0;
1882 TREE_PUBLIC (new_decl) = 0;
1883 DECL_COMDAT (new_decl) = 0;
1884 DECL_WEAK (new_decl) = 0;
1885
1886 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1887 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1888 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1889 {
1890 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1891 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1892 new_node = cgraph_node::create_alias (new_decl, node->decl);
1893
1894 cgraph_node *new_cnode = dyn_cast <cgraph_node *> (new_node),
1895 *cnode = dyn_cast <cgraph_node *> (node);
1896
1897 new_cnode->unit_id = cnode->unit_id;
1898 new_cnode->merged_comdat = cnode->merged_comdat;
1899 new_cnode->merged_extern_inline = cnode->merged_extern_inline;
1900 }
1901 else
1902 {
1903 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1904 DECL_INITIAL (new_decl) = error_mark_node;
1905 new_node = varpool_node::create_alias (new_decl, node->decl);
1906 }
1907 new_node->resolve_alias (node);
1908 gcc_assert (decl_binds_to_current_def_p (new_decl)
1909 && targetm.binds_local_p (new_decl));
1910 return new_node;
1911 }
1912
1913 /* Return true if symtab node and TARGET represents
1914 semantically equivalent symbols. */
1915
1916 bool
1917 symtab_node::semantically_equivalent_p (symtab_node *target)
1918 {
1919 enum availability avail;
1920 symtab_node *ba;
1921 symtab_node *bb;
1922
1923 /* Equivalent functions are equivalent. */
1924 if (decl == target->decl)
1925 return true;
1926
1927 /* If symbol is not overwritable by different implementation,
1928 walk to the base object it defines. */
1929 ba = ultimate_alias_target (&avail);
1930 if (avail >= AVAIL_AVAILABLE)
1931 {
1932 if (target == ba)
1933 return true;
1934 }
1935 else
1936 ba = this;
1937 bb = target->ultimate_alias_target (&avail);
1938 if (avail >= AVAIL_AVAILABLE)
1939 {
1940 if (this == bb)
1941 return true;
1942 }
1943 else
1944 bb = target;
1945 return bb == ba;
1946 }
1947
1948 /* Classify symbol symtab node for partitioning. */
1949
1950 enum symbol_partitioning_class
1951 symtab_node::get_partitioning_class (void)
1952 {
1953 /* Inline clones are always duplicated.
1954 This include external declarations. */
1955 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1956
1957 if (DECL_ABSTRACT_P (decl))
1958 return SYMBOL_EXTERNAL;
1959
1960 if (cnode && cnode->inlined_to)
1961 return SYMBOL_DUPLICATE;
1962
1963 /* Transparent aliases are always duplicated. */
1964 if (transparent_alias)
1965 return definition ? SYMBOL_DUPLICATE : SYMBOL_EXTERNAL;
1966
1967 /* External declarations are external. */
1968 if (DECL_EXTERNAL (decl))
1969 return SYMBOL_EXTERNAL;
1970
1971 /* Even static aliases of external functions as external. Those can happen
1972 when COMDAT got resolved to non-IL implementation. */
1973 if (alias && DECL_EXTERNAL (ultimate_alias_target ()->decl))
1974 return SYMBOL_EXTERNAL;
1975
1976 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1977 {
1978 if (alias && definition && !ultimate_alias_target ()->definition)
1979 return SYMBOL_EXTERNAL;
1980 /* Constant pool references use local symbol names that cannot
1981 be promoted global. We should never put into a constant pool
1982 objects that cannot be duplicated across partitions. */
1983 if (DECL_IN_CONSTANT_POOL (decl))
1984 return SYMBOL_DUPLICATE;
1985 if (DECL_HARD_REGISTER (decl))
1986 return SYMBOL_DUPLICATE;
1987 gcc_checking_assert (vnode->definition);
1988 }
1989 /* Functions that are cloned may stay in callgraph even if they are unused.
1990 Handle them as external; compute_ltrans_boundary take care to make
1991 proper things to happen (i.e. to make them appear in the boundary but
1992 with body streamed, so clone can me materialized). */
1993 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1994 return SYMBOL_EXTERNAL;
1995
1996 /* Linker discardable symbols are duplicated to every use unless they are
1997 keyed. */
1998 if (DECL_ONE_ONLY (decl)
1999 && !force_output
2000 && !forced_by_abi
2001 && !used_from_object_file_p ())
2002 return SYMBOL_DUPLICATE;
2003
2004 return SYMBOL_PARTITION;
2005 }
2006
2007 /* Return true when symbol is known to be non-zero. */
2008
2009 bool
2010 symtab_node::nonzero_address ()
2011 {
2012 /* Weakrefs may be NULL when their target is not defined. */
2013 if (alias && weakref)
2014 {
2015 if (analyzed)
2016 {
2017 symtab_node *target = ultimate_alias_target ();
2018
2019 if (target->alias && target->weakref)
2020 return false;
2021 /* We cannot recurse to target::nonzero. It is possible that the
2022 target is used only via the alias.
2023 We may walk references and look for strong use, but we do not know
2024 if this strong use will survive to final binary, so be
2025 conservative here.
2026 ??? Maybe we could do the lookup during late optimization that
2027 could be useful to eliminate the NULL pointer checks in LTO
2028 programs. */
2029 if (target->definition && !DECL_EXTERNAL (target->decl))
2030 return true;
2031 if (target->resolution != LDPR_UNKNOWN
2032 && target->resolution != LDPR_UNDEF
2033 && !target->can_be_discarded_p ()
2034 && flag_delete_null_pointer_checks)
2035 return true;
2036 return false;
2037 }
2038 else
2039 return false;
2040 }
2041
2042 /* With !flag_delete_null_pointer_checks we assume that symbols may
2043 bind to NULL. This is on by default on embedded targets only.
2044
2045 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
2046 linking fails. Important case of WEAK we want to do well are comdats.
2047 Those are handled by later check for definition.
2048
2049 When parsing, beware the cases when WEAK attribute is added later. */
2050 if (!DECL_WEAK (decl)
2051 && flag_delete_null_pointer_checks)
2052 {
2053 refuse_visibility_changes = true;
2054 return true;
2055 }
2056
2057 /* If target is defined and either comdat or not extern, we know it will be
2058 output and thus it will bind to non-NULL.
2059 Play safe for flag_delete_null_pointer_checks where weak definition may
2060 be re-defined by NULL. */
2061 if (definition && (!DECL_EXTERNAL (decl) || DECL_COMDAT (decl))
2062 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
2063 {
2064 if (!DECL_WEAK (decl))
2065 refuse_visibility_changes = true;
2066 return true;
2067 }
2068
2069 /* As the last resort, check the resolution info. */
2070 if (resolution != LDPR_UNKNOWN
2071 && resolution != LDPR_UNDEF
2072 && !can_be_discarded_p ()
2073 && flag_delete_null_pointer_checks)
2074 return true;
2075 return false;
2076 }
2077
2078 /* Return 0 if symbol is known to have different address than S2,
2079 Return 1 if symbol is known to have same address as S2,
2080 return -1 otherwise.
2081
2082 If MEMORY_ACCESSED is true, assume that both memory pointer to THIS
2083 and S2 is going to be accessed. This eliminates the situations when
2084 either THIS or S2 is NULL and is useful for comparing bases when deciding
2085 about memory aliasing. */
2086 int
2087 symtab_node::equal_address_to (symtab_node *s2, bool memory_accessed)
2088 {
2089 enum availability avail1, avail2;
2090
2091 /* A Shortcut: equivalent symbols are always equivalent. */
2092 if (this == s2)
2093 return 1;
2094
2095 /* Unwind transparent aliases first; those are always equal to their
2096 target. */
2097 if (this->transparent_alias && this->analyzed)
2098 return this->get_alias_target ()->equal_address_to (s2);
2099 while (s2->transparent_alias && s2->analyzed)
2100 s2 = s2->get_alias_target();
2101
2102 if (this == s2)
2103 return 1;
2104
2105 /* For non-interposable aliases, lookup and compare their actual definitions.
2106 Also check if the symbol needs to bind to given definition. */
2107 symtab_node *rs1 = ultimate_alias_target (&avail1);
2108 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
2109 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
2110 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
2111 bool really_binds_local1 = binds_local1;
2112 bool really_binds_local2 = binds_local2;
2113
2114 /* Addresses of vtables and virtual functions cannot be used by user
2115 code and are used only within speculation. In this case we may make
2116 symbol equivalent to its alias even if interposition may break this
2117 rule. Doing so will allow us to turn speculative inlining into
2118 non-speculative more aggressively. */
2119 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
2120 binds_local1 = true;
2121 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
2122 binds_local2 = true;
2123
2124 /* If both definitions are available we know that even if they are bound
2125 to other unit they must be defined same way and therefore we can use
2126 equivalence test. */
2127 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
2128 binds_local1 = binds_local2 = true;
2129
2130 if (binds_local1 && binds_local2 && rs1 == rs2)
2131 {
2132 /* We made use of the fact that alias is not weak. */
2133 if (rs1 != this)
2134 refuse_visibility_changes = true;
2135 if (rs2 != s2)
2136 s2->refuse_visibility_changes = true;
2137 return 1;
2138 }
2139
2140 /* If both symbols may resolve to NULL, we cannot really prove them
2141 different. */
2142 if (!memory_accessed && !nonzero_address () && !s2->nonzero_address ())
2143 return -1;
2144
2145 /* Except for NULL, functions and variables never overlap. */
2146 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
2147 return 0;
2148
2149 /* If one of the symbols is unresolved alias, punt. */
2150 if (rs1->alias || rs2->alias)
2151 return -1;
2152
2153 /* If we have a non-interposale definition of at least one of the symbols
2154 and the other symbol is different, we know other unit cannot interpose
2155 it to the first symbol; all aliases of the definition needs to be
2156 present in the current unit. */
2157 if (((really_binds_local1 || really_binds_local2)
2158 /* If we have both definitions and they are different, we know they
2159 will be different even in units they binds to. */
2160 || (binds_local1 && binds_local2))
2161 && rs1 != rs2)
2162 {
2163 /* We make use of the fact that one symbol is not alias of the other
2164 and that the definition is non-interposable. */
2165 refuse_visibility_changes = true;
2166 s2->refuse_visibility_changes = true;
2167 rs1->refuse_visibility_changes = true;
2168 rs2->refuse_visibility_changes = true;
2169 return 0;
2170 }
2171
2172 /* TODO: Alias oracle basically assume that addresses of global variables
2173 are different unless they are declared as alias of one to another while
2174 the code folding comparisons doesn't.
2175 We probably should be consistent and use this fact here, too, but for
2176 the moment return false only when we are called from the alias oracle. */
2177
2178 return memory_accessed && rs1 != rs2 ? 0 : -1;
2179 }
2180
2181 /* Worker for call_for_symbol_and_aliases. */
2182
2183 bool
2184 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
2185 void *),
2186 void *data,
2187 bool include_overwritable)
2188 {
2189 ipa_ref *ref;
2190 FOR_EACH_ALIAS (this, ref)
2191 {
2192 symtab_node *alias = ref->referring;
2193 if (include_overwritable
2194 || alias->get_availability () > AVAIL_INTERPOSABLE)
2195 if (alias->call_for_symbol_and_aliases (callback, data,
2196 include_overwritable))
2197 return true;
2198 }
2199 return false;
2200 }
2201
2202 /* Return true if address of N is possibly compared. */
2203
2204 static bool
2205 address_matters_1 (symtab_node *n, void *)
2206 {
2207 struct ipa_ref *ref;
2208
2209 if (!n->address_can_be_compared_p ())
2210 return false;
2211 if (n->externally_visible || n->force_output)
2212 return true;
2213
2214 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
2215 if (ref->address_matters_p ())
2216 return true;
2217 return false;
2218 }
2219
2220 /* Return true if symbol's address may possibly be compared to other
2221 symbol's address. */
2222
2223 bool
2224 symtab_node::address_matters_p ()
2225 {
2226 gcc_assert (!alias);
2227 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
2228 }
2229
2230 /* Return true if symbol's alignment may be increased. */
2231
2232 bool
2233 symtab_node::can_increase_alignment_p (void)
2234 {
2235 symtab_node *target = ultimate_alias_target ();
2236
2237 /* For now support only variables. */
2238 if (!VAR_P (decl))
2239 return false;
2240
2241 /* With -fno-toplevel-reorder we may have already output the constant. */
2242 if (TREE_ASM_WRITTEN (target->decl))
2243 return false;
2244
2245 /* If target is already placed in an anchor, we cannot touch its
2246 alignment. */
2247 if (DECL_RTL_SET_P (target->decl)
2248 && MEM_P (DECL_RTL (target->decl))
2249 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
2250 return false;
2251
2252 /* Constant pool entries may be shared. */
2253 if (DECL_IN_CONSTANT_POOL (target->decl))
2254 return false;
2255
2256 /* We cannot change alignment of symbols that may bind to symbols
2257 in other translation unit that may contain a definition with lower
2258 alignment. */
2259 if (!decl_binds_to_current_def_p (decl))
2260 return false;
2261
2262 /* When compiling partition, be sure the symbol is not output by other
2263 partition. */
2264 if (flag_ltrans
2265 && (target->in_other_partition
2266 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
2267 return false;
2268
2269 /* Do not override the alignment as specified by the ABI when the used
2270 attribute is set. */
2271 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
2272 return false;
2273
2274 /* Do not override explicit alignment set by the user when an explicit
2275 section name is also used. This is a common idiom used by many
2276 software projects. */
2277 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
2278 return false;
2279
2280 return true;
2281 }
2282
2283 /* Worker for symtab_node::increase_alignment. */
2284
2285 static bool
2286 increase_alignment_1 (symtab_node *n, void *v)
2287 {
2288 unsigned int align = (size_t)v;
2289 if (DECL_ALIGN (n->decl) < align
2290 && n->can_increase_alignment_p ())
2291 {
2292 SET_DECL_ALIGN (n->decl, align);
2293 DECL_USER_ALIGN (n->decl) = 1;
2294 }
2295 return false;
2296 }
2297
2298 /* Increase alignment of THIS to ALIGN. */
2299
2300 void
2301 symtab_node::increase_alignment (unsigned int align)
2302 {
2303 gcc_assert (can_increase_alignment_p () && align <= MAX_OFILE_ALIGNMENT);
2304 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
2305 (void *)(size_t) align,
2306 true);
2307 gcc_assert (DECL_ALIGN (decl) >= align);
2308 }
2309
2310 /* Helper for symtab_node::definition_alignment. */
2311
2312 static bool
2313 get_alignment_1 (symtab_node *n, void *v)
2314 {
2315 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
2316 return false;
2317 }
2318
2319 /* Return desired alignment of the definition. This is NOT alignment useful
2320 to access THIS, because THIS may be interposable and DECL_ALIGN should
2321 be used instead. It however must be guaranteed when output definition
2322 of THIS. */
2323
2324 unsigned int
2325 symtab_node::definition_alignment ()
2326 {
2327 unsigned int align = 0;
2328 gcc_assert (!alias);
2329 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2330 return align;
2331 }
2332
2333 /* Return symbol used to separate symbol name from suffix. */
2334
2335 char
2336 symbol_table::symbol_suffix_separator ()
2337 {
2338 #ifndef NO_DOT_IN_LABEL
2339 return '.';
2340 #elif !defined NO_DOLLAR_IN_LABEL
2341 return '$';
2342 #else
2343 return '_';
2344 #endif
2345 }
2346
2347 /* Return true when references to this symbol from REF must bind to current
2348 definition in final executable. */
2349
2350 bool
2351 symtab_node::binds_to_current_def_p (symtab_node *ref)
2352 {
2353 if (!definition)
2354 return false;
2355 if (transparent_alias)
2356 return definition
2357 && get_alias_target()->binds_to_current_def_p (ref);
2358 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
2359 if (cnode && cnode->ifunc_resolver)
2360 return false;
2361 if (decl_binds_to_current_def_p (decl))
2362 return true;
2363
2364 /* Inline clones always binds locally. */
2365 if (cnode && cnode->inlined_to)
2366 return true;
2367
2368 if (DECL_EXTERNAL (decl))
2369 return false;
2370
2371 gcc_assert (externally_visible);
2372
2373 if (ref)
2374 {
2375 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2376 if (cref)
2377 ref = cref->inlined_to;
2378 }
2379
2380 /* If this is a reference from symbol itself and there are no aliases, we
2381 may be sure that the symbol was not interposed by something else because
2382 the symbol itself would be unreachable otherwise. This is important
2383 to optimize recursive functions well.
2384
2385 This assumption may be broken by inlining: if symbol is interposable
2386 but the body is available (i.e. declared inline), inliner may make
2387 the body reachable even with interposition. */
2388 if (this == ref && !has_aliases_p ()
2389 && (!cnode
2390 || symtab->state >= IPA_SSA_AFTER_INLINING
2391 || get_availability () >= AVAIL_INTERPOSABLE))
2392 return true;
2393
2394
2395 /* References within one comdat group are always bound in a group. */
2396 if (ref
2397 && symtab->state >= IPA_SSA_AFTER_INLINING
2398 && get_comdat_group ()
2399 && get_comdat_group () == ref->get_comdat_group ())
2400 return true;
2401
2402 return false;
2403 }
2404
2405 /* Return true if symbol should be output to the symbol table. */
2406
2407 bool
2408 symtab_node::output_to_lto_symbol_table_p (void)
2409 {
2410 /* Only externally visible symbols matter. */
2411 if (!TREE_PUBLIC (decl))
2412 return false;
2413 if (!real_symbol_p ())
2414 return false;
2415 /* FIXME: variables probably should not be considered as real symbols at
2416 first place. */
2417 if (VAR_P (decl) && DECL_HARD_REGISTER (decl))
2418 return false;
2419 if (TREE_CODE (decl) == FUNCTION_DECL && !definition
2420 && fndecl_built_in_p (decl))
2421 {
2422 /* Builtins like those for most math functions have actual implementations
2423 in libraries so make sure to output references into the symbol table to
2424 make those libraries referenced. Note this is incomplete handling for
2425 now and only covers math functions. */
2426 if (builtin_with_linkage_p (decl))
2427 return true;
2428 else
2429 return false;
2430 }
2431
2432 /* We have real symbol that should be in symbol table. However try to trim
2433 down the references to libraries bit more because linker will otherwise
2434 bring unnecessary object files into the final link.
2435 FIXME: The following checks can easily be confused i.e. by self recursive
2436 function or self-referring variable. */
2437
2438 /* We keep external functions in symtab for sake of inlining
2439 and devirtualization. We do not want to see them in symbol table as
2440 references unless they are really used. */
2441 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
2442 if (cnode && (!definition || DECL_EXTERNAL (decl))
2443 && cnode->callers)
2444 return true;
2445
2446 /* Ignore all references from external vars initializers - they are not really
2447 part of the compilation unit until they are used by folding. Some symbols,
2448 like references to external construction vtables cannot be referred to at
2449 all. We decide this at can_refer_decl_in_current_unit_p. */
2450 if (!definition || DECL_EXTERNAL (decl))
2451 {
2452 int i;
2453 struct ipa_ref *ref;
2454 for (i = 0; iterate_referring (i, ref); i++)
2455 {
2456 if (ref->use == IPA_REF_ALIAS)
2457 continue;
2458 if (is_a <cgraph_node *> (ref->referring))
2459 return true;
2460 if (!DECL_EXTERNAL (ref->referring->decl))
2461 return true;
2462 }
2463 return false;
2464 }
2465 return true;
2466 }