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