]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/symtab.c
d99546f7e986e8688f2c3877a488cb0ae1ce2a83
[thirdparty/gcc.git] / gcc / symtab.c
1 /* Symbol table.
2 Copyright (C) 2012-2014 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 "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "print-tree.h"
28 #include "varasm.h"
29 #include "function.h"
30 #include "emit-rtl.h"
31 #include "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "tree-inline.h"
38 #include "langhooks.h"
39 #include "hashtab.h"
40 #include "cgraph.h"
41 #include "diagnostic.h"
42 #include "timevar.h"
43 #include "lto-streamer.h"
44 #include "output.h"
45 #include "ipa-utils.h"
46 #include "calls.h"
47
48 static const char *ipa_ref_use_name[] = {"read","write","addr","alias"};
49
50 const char * const ld_plugin_symbol_resolution_names[]=
51 {
52 "",
53 "undef",
54 "prevailing_def",
55 "prevailing_def_ironly",
56 "preempted_reg",
57 "preempted_ir",
58 "resolved_ir",
59 "resolved_exec",
60 "resolved_dyn",
61 "prevailing_def_ironly_exp"
62 };
63
64 /* Hash asmnames ignoring the user specified marks. */
65
66 hashval_t
67 symbol_table::decl_assembler_name_hash (const_tree asmname)
68 {
69 if (IDENTIFIER_POINTER (asmname)[0] == '*')
70 {
71 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
72 size_t ulp_len = strlen (user_label_prefix);
73
74 if (ulp_len == 0)
75 ;
76 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
77 decl_str += ulp_len;
78
79 return htab_hash_string (decl_str);
80 }
81
82 return htab_hash_string (IDENTIFIER_POINTER (asmname));
83 }
84
85
86 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
87
88 bool
89 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
90 {
91 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
92 const char *decl_str;
93 const char *asmname_str;
94 bool test = false;
95
96 if (decl_asmname == asmname)
97 return true;
98
99 decl_str = IDENTIFIER_POINTER (decl_asmname);
100 asmname_str = IDENTIFIER_POINTER (asmname);
101
102
103 /* If the target assembler name was set by the user, things are trickier.
104 We have a leading '*' to begin with. After that, it's arguable what
105 is the correct thing to do with -fleading-underscore. Arguably, we've
106 historically been doing the wrong thing in assemble_alias by always
107 printing the leading underscore. Since we're not changing that, make
108 sure user_label_prefix follows the '*' before matching. */
109 if (decl_str[0] == '*')
110 {
111 size_t ulp_len = strlen (user_label_prefix);
112
113 decl_str ++;
114
115 if (ulp_len == 0)
116 test = true;
117 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
118 decl_str += ulp_len, test=true;
119 else
120 decl_str --;
121 }
122 if (asmname_str[0] == '*')
123 {
124 size_t ulp_len = strlen (user_label_prefix);
125
126 asmname_str ++;
127
128 if (ulp_len == 0)
129 test = true;
130 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
131 asmname_str += ulp_len, test=true;
132 else
133 asmname_str --;
134 }
135
136 if (!test)
137 return false;
138 return strcmp (decl_str, asmname_str) == 0;
139 }
140
141
142 /* Returns nonzero if P1 and P2 are equal. */
143
144 /* Insert NODE to assembler name hash. */
145
146 void
147 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
148 bool with_clones)
149 {
150 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
151 return;
152 gcc_checking_assert (!node->previous_sharing_asm_name
153 && !node->next_sharing_asm_name);
154 if (assembler_name_hash)
155 {
156 symtab_node **aslot;
157 cgraph_node *cnode;
158 tree decl = node->decl;
159
160 tree name = DECL_ASSEMBLER_NAME (node->decl);
161
162 hashval_t hash = decl_assembler_name_hash (name);
163 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
164 gcc_assert (*aslot != node);
165 node->next_sharing_asm_name = (symtab_node *)*aslot;
166 if (*aslot != NULL)
167 (*aslot)->previous_sharing_asm_name = node;
168 *aslot = node;
169
170 /* Update also possible inline clones sharing a decl. */
171 cnode = dyn_cast <cgraph_node *> (node);
172 if (cnode && cnode->clones && with_clones)
173 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
174 if (cnode->decl == decl)
175 insert_to_assembler_name_hash (cnode, true);
176 }
177
178 }
179
180 /* Remove NODE from assembler name hash. */
181
182 void
183 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
184 bool with_clones)
185 {
186 if (assembler_name_hash)
187 {
188 cgraph_node *cnode;
189 tree decl = node->decl;
190
191 if (node->next_sharing_asm_name)
192 node->next_sharing_asm_name->previous_sharing_asm_name
193 = node->previous_sharing_asm_name;
194 if (node->previous_sharing_asm_name)
195 {
196 node->previous_sharing_asm_name->next_sharing_asm_name
197 = node->next_sharing_asm_name;
198 }
199 else
200 {
201 tree name = DECL_ASSEMBLER_NAME (node->decl);
202 symtab_node **slot;
203 hashval_t hash = decl_assembler_name_hash (name);
204 slot = assembler_name_hash->find_slot_with_hash (name, hash,
205 NO_INSERT);
206 gcc_assert (*slot == node);
207 if (!node->next_sharing_asm_name)
208 assembler_name_hash->clear_slot (slot);
209 else
210 *slot = node->next_sharing_asm_name;
211 }
212 node->next_sharing_asm_name = NULL;
213 node->previous_sharing_asm_name = NULL;
214
215 /* Update also possible inline clones sharing a decl. */
216 cnode = dyn_cast <cgraph_node *> (node);
217 if (cnode && cnode->clones && with_clones)
218 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
219 if (cnode->decl == decl)
220 unlink_from_assembler_name_hash (cnode, true);
221 }
222 }
223
224 /* Arrange node to be first in its entry of assembler_name_hash. */
225
226 void
227 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
228 {
229 unlink_from_assembler_name_hash (node, false);
230 insert_to_assembler_name_hash (node, false);
231 }
232
233 /* Initalize asm name hash unless. */
234
235 void
236 symbol_table::symtab_initialize_asm_name_hash (void)
237 {
238 symtab_node *node;
239 if (!assembler_name_hash)
240 {
241 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
242 FOR_EACH_SYMBOL (node)
243 insert_to_assembler_name_hash (node, false);
244 }
245 }
246
247 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
248
249 void
250 symbol_table::change_decl_assembler_name (tree decl, tree name)
251 {
252 symtab_node *node = NULL;
253
254 /* We can have user ASM names on things, like global register variables, that
255 are not in the symbol table. */
256 if ((TREE_CODE (decl) == VAR_DECL
257 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
258 || TREE_CODE (decl) == FUNCTION_DECL)
259 node = symtab_node::get (decl);
260 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
261 {
262 SET_DECL_ASSEMBLER_NAME (decl, name);
263 if (node)
264 insert_to_assembler_name_hash (node, true);
265 }
266 else
267 {
268 if (name == DECL_ASSEMBLER_NAME (decl))
269 return;
270
271 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
272 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
273 : NULL);
274 if (node)
275 unlink_from_assembler_name_hash (node, true);
276 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
277 && DECL_RTL_SET_P (decl))
278 warning (0, "%D renamed after being referenced in assembly", decl);
279
280 SET_DECL_ASSEMBLER_NAME (decl, name);
281 if (alias)
282 {
283 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
284 TREE_CHAIN (name) = alias;
285 }
286 if (node)
287 insert_to_assembler_name_hash (node, true);
288 }
289 }
290
291 /* Return true when RESOLUTION indicate that linker will use
292 the symbol from non-LTO object files. */
293
294 bool
295 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
296 {
297 return (resolution == LDPR_PREVAILING_DEF
298 || resolution == LDPR_PREEMPTED_REG
299 || resolution == LDPR_RESOLVED_EXEC
300 || resolution == LDPR_RESOLVED_DYN);
301 }
302
303 /* Hash sections by their names. */
304
305 hashval_t
306 section_name_hasher::hash (section_hash_entry *n)
307 {
308 return htab_hash_string (n->name);
309 }
310
311 /* Return true if section P1 name equals to P2. */
312
313 bool
314 section_name_hasher::equal (section_hash_entry *n1, const char *name)
315 {
316 return n1->name == name || !strcmp (n1->name, name);
317 }
318
319 /* Add node into symbol table. This function is not used directly, but via
320 cgraph/varpool node creation routines. */
321
322 void
323 symtab_node::register_symbol (void)
324 {
325 symtab->register_symbol (this);
326
327 if (!decl->decl_with_vis.symtab_node)
328 decl->decl_with_vis.symtab_node = this;
329
330 ref_list.clear ();
331
332 /* Be sure to do this last; C++ FE might create new nodes via
333 DECL_ASSEMBLER_NAME langhook! */
334 symtab->insert_to_assembler_name_hash (this, false);
335 }
336
337 /* Remove NODE from same comdat group. */
338
339 void
340 symtab_node::remove_from_same_comdat_group (void)
341 {
342 if (same_comdat_group)
343 {
344 symtab_node *prev;
345 for (prev = same_comdat_group;
346 prev->same_comdat_group != this;
347 prev = prev->same_comdat_group)
348 ;
349 if (same_comdat_group == prev)
350 prev->same_comdat_group = NULL;
351 else
352 prev->same_comdat_group = same_comdat_group;
353 same_comdat_group = NULL;
354 set_comdat_group (NULL);
355 }
356 }
357
358 /* Remove node from symbol table. This function is not used directly, but via
359 cgraph/varpool node removal routines. */
360
361 void
362 symtab_node::unregister (void)
363 {
364 remove_all_references ();
365 remove_all_referring ();
366
367 /* Remove reference to section. */
368 set_section_for_node (NULL);
369
370 remove_from_same_comdat_group ();
371
372 symtab->unregister (this);
373
374 /* During LTO symtab merging we temporarily corrupt decl to symtab node
375 hash. */
376 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
377 if (decl->decl_with_vis.symtab_node == this)
378 {
379 symtab_node *replacement_node = NULL;
380 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
381 replacement_node = cnode->find_replacement ();
382 decl->decl_with_vis.symtab_node = replacement_node;
383 }
384 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
385 symtab->unlink_from_assembler_name_hash (this, false);
386 if (in_init_priority_hash)
387 symtab->init_priority_hash->remove (this);
388 }
389
390
391 /* Remove symbol from symbol table. */
392
393 void
394 symtab_node::remove (void)
395 {
396 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
397 cnode->remove ();
398 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
399 vnode->remove ();
400 }
401
402 /* Add NEW_ to the same comdat group that OLD is in. */
403
404 void
405 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
406 {
407 gcc_assert (old_node->get_comdat_group ());
408 gcc_assert (!same_comdat_group);
409 gcc_assert (this != old_node);
410
411 set_comdat_group (old_node->get_comdat_group ());
412 same_comdat_group = old_node;
413 if (!old_node->same_comdat_group)
414 old_node->same_comdat_group = this;
415 else
416 {
417 symtab_node *n;
418 for (n = old_node->same_comdat_group;
419 n->same_comdat_group != old_node;
420 n = n->same_comdat_group)
421 ;
422 n->same_comdat_group = this;
423 }
424 }
425
426 /* Dissolve the same_comdat_group list in which NODE resides. */
427
428 void
429 symtab_node::dissolve_same_comdat_group_list (void)
430 {
431 symtab_node *n = this;
432 symtab_node *next;
433
434 if (!same_comdat_group)
435 return;
436 do
437 {
438 next = n->same_comdat_group;
439 n->same_comdat_group = NULL;
440 /* Clear comdat_group for comdat locals, since
441 make_decl_local doesn't. */
442 if (!TREE_PUBLIC (n->decl))
443 n->set_comdat_group (NULL);
444 n = next;
445 }
446 while (n != this);
447 }
448
449 /* Return printable assembler name of NODE.
450 This function is used only for debugging. When assembler name
451 is unknown go with identifier name. */
452
453 const char *
454 symtab_node::asm_name () const
455 {
456 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
457 return lang_hooks.decl_printable_name (decl, 2);
458 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
459 }
460
461 /* Return printable identifier name. */
462
463 const char *
464 symtab_node::name () const
465 {
466 return lang_hooks.decl_printable_name (decl, 2);
467 }
468
469 /* Return ipa reference from this symtab_node to
470 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
471 of the use. */
472
473 ipa_ref *
474 symtab_node::create_reference (symtab_node *referred_node,
475 enum ipa_ref_use use_type)
476 {
477 return create_reference (referred_node, use_type, NULL);
478 }
479
480
481 /* Return ipa reference from this symtab_node to
482 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
483 of the use and STMT the statement (if it exists). */
484
485 ipa_ref *
486 symtab_node::create_reference (symtab_node *referred_node,
487 enum ipa_ref_use use_type, gimple stmt)
488 {
489 ipa_ref *ref = NULL, *ref2 = NULL;
490 ipa_ref_list *list, *list2;
491 ipa_ref_t *old_references;
492
493 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
494 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
495
496 list = &ref_list;
497 old_references = vec_safe_address (list->references);
498 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
499 ref = &list->references->last ();
500
501 list2 = &referred_node->ref_list;
502
503 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
504 if(use_type == IPA_REF_ALIAS)
505 {
506 list2->referring.safe_insert (0, ref);
507 ref->referred_index = 0;
508
509 for (unsigned int i = 1; i < list2->referring.length (); i++)
510 list2->referring[i]->referred_index = i;
511 }
512 else
513 {
514 list2->referring.safe_push (ref);
515 ref->referred_index = list2->referring.length () - 1;
516 }
517
518 ref->referring = this;
519 ref->referred = referred_node;
520 ref->stmt = stmt;
521 ref->lto_stmt_uid = 0;
522 ref->use = use_type;
523 ref->speculative = 0;
524
525 /* If vector was moved in memory, update pointers. */
526 if (old_references != list->references->address ())
527 {
528 int i;
529 for (i = 0; iterate_reference(i, ref2); i++)
530 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
531 }
532 return ref;
533 }
534
535 /* If VAL is a reference to a function or a variable, add a reference from
536 this symtab_node to the corresponding symbol table node. USE_TYPE specify
537 type of the use and STMT the statement (if it exists). Return the new
538 reference or NULL if none was created. */
539
540 ipa_ref *
541 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
542 gimple stmt)
543 {
544 STRIP_NOPS (val);
545 if (TREE_CODE (val) != ADDR_EXPR)
546 return NULL;
547 val = get_base_var (val);
548 if (val && (TREE_CODE (val) == FUNCTION_DECL
549 || TREE_CODE (val) == VAR_DECL))
550 {
551 symtab_node *referred = symtab_node::get (val);
552 gcc_checking_assert (referred);
553 return create_reference (referred, use_type, stmt);
554 }
555 return NULL;
556 }
557
558 /* Clone all references from symtab NODE to this symtab_node. */
559
560 void
561 symtab_node::clone_references (symtab_node *node)
562 {
563 ipa_ref *ref = NULL, *ref2 = NULL;
564 int i;
565 for (i = 0; node->iterate_reference (i, ref); i++)
566 {
567 bool speculative = ref->speculative;
568 unsigned int stmt_uid = ref->lto_stmt_uid;
569
570 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
571 ref2->speculative = speculative;
572 ref2->lto_stmt_uid = stmt_uid;
573 }
574 }
575
576 /* Clone all referring from symtab NODE to this symtab_node. */
577
578 void
579 symtab_node::clone_referring (symtab_node *node)
580 {
581 ipa_ref *ref = NULL, *ref2 = NULL;
582 int i;
583 for (i = 0; node->iterate_referring(i, ref); i++)
584 {
585 bool speculative = ref->speculative;
586 unsigned int stmt_uid = ref->lto_stmt_uid;
587
588 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
589 ref2->speculative = speculative;
590 ref2->lto_stmt_uid = stmt_uid;
591 }
592 }
593
594 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
595
596 ipa_ref *
597 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
598 {
599 bool speculative = ref->speculative;
600 unsigned int stmt_uid = ref->lto_stmt_uid;
601 ipa_ref *ref2;
602
603 ref2 = create_reference (ref->referred, ref->use, stmt);
604 ref2->speculative = speculative;
605 ref2->lto_stmt_uid = stmt_uid;
606 return ref2;
607 }
608
609 /* Find the structure describing a reference to REFERRED_NODE
610 and associated with statement STMT. */
611
612 ipa_ref *
613 symtab_node::find_reference (symtab_node *referred_node,
614 gimple stmt, unsigned int lto_stmt_uid)
615 {
616 ipa_ref *r = NULL;
617 int i;
618
619 for (i = 0; iterate_reference (i, r); i++)
620 if (r->referred == referred_node
621 && !r->speculative
622 && ((stmt && r->stmt == stmt)
623 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
624 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
625 return r;
626 return NULL;
627 }
628
629 /* Remove all references that are associated with statement STMT. */
630
631 void
632 symtab_node::remove_stmt_references (gimple stmt)
633 {
634 ipa_ref *r = NULL;
635 int i = 0;
636
637 while (iterate_reference (i, r))
638 if (r->stmt == stmt)
639 r->remove_reference ();
640 else
641 i++;
642 }
643
644 /* Remove all stmt references in non-speculative references.
645 Those are not maintained during inlining & clonning.
646 The exception are speculative references that are updated along
647 with callgraph edges associated with them. */
648
649 void
650 symtab_node::clear_stmts_in_references (void)
651 {
652 ipa_ref *r = NULL;
653 int i;
654
655 for (i = 0; iterate_reference (i, r); i++)
656 if (!r->speculative)
657 {
658 r->stmt = NULL;
659 r->lto_stmt_uid = 0;
660 }
661 }
662
663 /* Remove all references in ref list. */
664
665 void
666 symtab_node::remove_all_references (void)
667 {
668 while (vec_safe_length (ref_list.references))
669 ref_list.references->last ().remove_reference ();
670 vec_free (ref_list.references);
671 }
672
673 /* Remove all referring items in ref list. */
674
675 void
676 symtab_node::remove_all_referring (void)
677 {
678 while (ref_list.referring.length ())
679 ref_list.referring.last ()->remove_reference ();
680 ref_list.referring.release ();
681 }
682
683 /* Dump references in ref list to FILE. */
684
685 void
686 symtab_node::dump_references (FILE *file)
687 {
688 ipa_ref *ref = NULL;
689 int i;
690 for (i = 0; iterate_reference (i, ref); i++)
691 {
692 fprintf (file, "%s/%i (%s)",
693 ref->referred->asm_name (),
694 ref->referred->order,
695 ipa_ref_use_name [ref->use]);
696 if (ref->speculative)
697 fprintf (file, " (speculative)");
698 }
699 fprintf (file, "\n");
700 }
701
702 /* Dump referring in list to FILE. */
703
704 void
705 symtab_node::dump_referring (FILE *file)
706 {
707 ipa_ref *ref = NULL;
708 int i;
709 for (i = 0; iterate_referring(i, ref); i++)
710 {
711 fprintf (file, "%s/%i (%s)",
712 ref->referring->asm_name (),
713 ref->referring->order,
714 ipa_ref_use_name [ref->use]);
715 if (ref->speculative)
716 fprintf (file, " (speculative)");
717 }
718 fprintf (file, "\n");
719 }
720
721 /* Return true if list contains an alias. */
722 bool
723 symtab_node::has_aliases_p (void)
724 {
725 ipa_ref *ref = NULL;
726 int i;
727
728 for (i = 0; iterate_referring (i, ref); i++)
729 if (ref->use == IPA_REF_ALIAS)
730 return true;
731 return false;
732 }
733
734 /* Iterates I-th reference in the list, REF is also set. */
735
736 ipa_ref *
737 symtab_node::iterate_reference (unsigned i, ipa_ref *&ref)
738 {
739 vec_safe_iterate (ref_list.references, i, &ref);
740
741 return ref;
742 }
743
744 /* Iterates I-th referring item in the list, REF is also set. */
745
746 ipa_ref *
747 symtab_node::iterate_referring (unsigned i, ipa_ref *&ref)
748 {
749 ref_list.referring.iterate (i, &ref);
750
751 return ref;
752 }
753
754 /* Iterates I-th referring alias item in the list, REF is also set. */
755
756 ipa_ref *
757 symtab_node::iterate_direct_aliases (unsigned i, ipa_ref *&ref)
758 {
759 ref_list.referring.iterate (i, &ref);
760
761 if (ref && ref->use != IPA_REF_ALIAS)
762 return NULL;
763
764 return ref;
765 }
766
767 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
768
769 /* Dump base fields of symtab nodes to F. Not to be used directly. */
770
771 void
772 symtab_node::dump_base (FILE *f)
773 {
774 static const char * const visibility_types[] = {
775 "default", "protected", "hidden", "internal"
776 };
777
778 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
779 dump_addr (f, " @", (void *)this);
780 fprintf (f, "\n Type: %s", symtab_type_names[type]);
781
782 if (definition)
783 fprintf (f, " definition");
784 if (analyzed)
785 fprintf (f, " analyzed");
786 if (alias)
787 fprintf (f, " alias");
788 if (weakref)
789 fprintf (f, " weakref");
790 if (cpp_implicit_alias)
791 fprintf (f, " cpp_implicit_alias");
792 if (alias_target)
793 fprintf (f, " target:%s",
794 DECL_P (alias_target)
795 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
796 (alias_target))
797 : IDENTIFIER_POINTER (alias_target));
798 if (body_removed)
799 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
800 fprintf (f, "\n Visibility:");
801 if (in_other_partition)
802 fprintf (f, " in_other_partition");
803 if (used_from_other_partition)
804 fprintf (f, " used_from_other_partition");
805 if (force_output)
806 fprintf (f, " force_output");
807 if (forced_by_abi)
808 fprintf (f, " forced_by_abi");
809 if (externally_visible)
810 fprintf (f, " externally_visible");
811 if (no_reorder)
812 fprintf (f, " no_reorder");
813 if (resolution != LDPR_UNKNOWN)
814 fprintf (f, " %s",
815 ld_plugin_symbol_resolution_names[(int)resolution]);
816 if (TREE_ASM_WRITTEN (decl))
817 fprintf (f, " asm_written");
818 if (DECL_EXTERNAL (decl))
819 fprintf (f, " external");
820 if (TREE_PUBLIC (decl))
821 fprintf (f, " public");
822 if (DECL_COMMON (decl))
823 fprintf (f, " common");
824 if (DECL_WEAK (decl))
825 fprintf (f, " weak");
826 if (DECL_DLLIMPORT_P (decl))
827 fprintf (f, " dll_import");
828 if (DECL_COMDAT (decl))
829 fprintf (f, " comdat");
830 if (get_comdat_group ())
831 fprintf (f, " comdat_group:%s",
832 IDENTIFIER_POINTER (get_comdat_group_id ()));
833 if (DECL_ONE_ONLY (decl))
834 fprintf (f, " one_only");
835 if (get_section ())
836 fprintf (f, " section:%s",
837 get_section ());
838 if (implicit_section)
839 fprintf (f," (implicit_section)");
840 if (DECL_VISIBILITY_SPECIFIED (decl))
841 fprintf (f, " visibility_specified");
842 if (DECL_VISIBILITY (decl))
843 fprintf (f, " visibility:%s",
844 visibility_types [DECL_VISIBILITY (decl)]);
845 if (DECL_VIRTUAL_P (decl))
846 fprintf (f, " virtual");
847 if (DECL_ARTIFICIAL (decl))
848 fprintf (f, " artificial");
849 if (TREE_CODE (decl) == FUNCTION_DECL)
850 {
851 if (DECL_STATIC_CONSTRUCTOR (decl))
852 fprintf (f, " constructor");
853 if (DECL_STATIC_DESTRUCTOR (decl))
854 fprintf (f, " destructor");
855 }
856 fprintf (f, "\n");
857
858 if (same_comdat_group)
859 fprintf (f, " Same comdat group as: %s/%i\n",
860 same_comdat_group->asm_name (),
861 same_comdat_group->order);
862 if (next_sharing_asm_name)
863 fprintf (f, " next sharing asm name: %i\n",
864 next_sharing_asm_name->order);
865 if (previous_sharing_asm_name)
866 fprintf (f, " previous sharing asm name: %i\n",
867 previous_sharing_asm_name->order);
868
869 if (address_taken)
870 fprintf (f, " Address is taken.\n");
871 if (aux)
872 {
873 fprintf (f, " Aux:");
874 dump_addr (f, " @", (void *)aux);
875 }
876
877 fprintf (f, " References: ");
878 dump_references (f);
879 fprintf (f, " Referring: ");
880 dump_referring (f);
881 if (lto_file_data)
882 fprintf (f, " Read from file: %s\n",
883 lto_file_data->file_name);
884 }
885
886 /* Dump symtab node to F. */
887
888 void
889 symtab_node::dump (FILE *f)
890 {
891 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
892 cnode->dump (f);
893 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
894 vnode->dump (f);
895 }
896
897 /* Dump symbol table to F. */
898
899 void
900 symtab_node::dump_table (FILE *f)
901 {
902 symtab_node *node;
903 fprintf (f, "Symbol table:\n\n");
904 FOR_EACH_SYMBOL (node)
905 node->dump (f);
906 }
907
908
909 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
910 Return NULL if there's no such node. */
911
912 symtab_node *
913 symtab_node::get_for_asmname (const_tree asmname)
914 {
915 symtab_node *node;
916
917 symtab->symtab_initialize_asm_name_hash ();
918 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
919 symtab_node **slot
920 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
921 NO_INSERT);
922
923 if (slot)
924 {
925 node = *slot;
926 return node;
927 }
928 return NULL;
929 }
930
931 /* Dump symtab node NODE to stderr. */
932
933 DEBUG_FUNCTION void
934 symtab_node::debug (void)
935 {
936 dump (stderr);
937 }
938
939 /* Verify common part of symtab nodes. */
940
941 DEBUG_FUNCTION bool
942 symtab_node::verify_base (void)
943 {
944 bool error_found = false;
945 symtab_node *hashed_node;
946
947 if (is_a <cgraph_node *> (this))
948 {
949 if (TREE_CODE (decl) != FUNCTION_DECL)
950 {
951 error ("function symbol is not function");
952 error_found = true;
953 }
954 }
955 else if (is_a <varpool_node *> (this))
956 {
957 if (TREE_CODE (decl) != VAR_DECL)
958 {
959 error ("variable symbol is not variable");
960 error_found = true;
961 }
962 }
963 else
964 {
965 error ("node has unknown type");
966 error_found = true;
967 }
968
969 if (symtab->state != LTO_STREAMING)
970 {
971 hashed_node = symtab_node::get (decl);
972 if (!hashed_node)
973 {
974 error ("node not found node->decl->decl_with_vis.symtab_node");
975 error_found = true;
976 }
977 if (hashed_node != this
978 && (!is_a <cgraph_node *> (this)
979 || !dyn_cast <cgraph_node *> (this)->clone_of
980 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
981 {
982 error ("node differs from node->decl->decl_with_vis.symtab_node");
983 error_found = true;
984 }
985 }
986 if (symtab->assembler_name_hash)
987 {
988 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
989 if (hashed_node && hashed_node->previous_sharing_asm_name)
990 {
991 error ("assembler name hash list corrupted");
992 error_found = true;
993 }
994 while (hashed_node)
995 {
996 if (hashed_node == this)
997 break;
998 hashed_node = hashed_node->next_sharing_asm_name;
999 }
1000 if (!hashed_node
1001 && !(is_a <varpool_node *> (this)
1002 || DECL_HARD_REGISTER (decl)))
1003 {
1004 error ("node not found in symtab assembler name hash");
1005 error_found = true;
1006 }
1007 }
1008 if (previous_sharing_asm_name
1009 && previous_sharing_asm_name->next_sharing_asm_name != this)
1010 {
1011 error ("double linked list of assembler names corrupted");
1012 error_found = true;
1013 }
1014 if (analyzed && !definition)
1015 {
1016 error ("node is analyzed byt it is not a definition");
1017 error_found = true;
1018 }
1019 if (cpp_implicit_alias && !alias)
1020 {
1021 error ("node is alias but not implicit alias");
1022 error_found = true;
1023 }
1024 if (alias && !definition && !weakref)
1025 {
1026 error ("node is alias but not definition");
1027 error_found = true;
1028 }
1029 if (weakref && !alias)
1030 {
1031 error ("node is weakref but not an alias");
1032 error_found = true;
1033 }
1034 if (same_comdat_group)
1035 {
1036 symtab_node *n = same_comdat_group;
1037
1038 if (!n->get_comdat_group ())
1039 {
1040 error ("node is in same_comdat_group list but has no comdat_group");
1041 error_found = true;
1042 }
1043 if (n->get_comdat_group () != get_comdat_group ())
1044 {
1045 error ("same_comdat_group list across different groups");
1046 error_found = true;
1047 }
1048 if (!n->definition)
1049 {
1050 error ("Node has same_comdat_group but it is not a definition");
1051 error_found = true;
1052 }
1053 if (n->type != type)
1054 {
1055 error ("mixing different types of symbol in same comdat groups is not supported");
1056 error_found = true;
1057 }
1058 if (n == this)
1059 {
1060 error ("node is alone in a comdat group");
1061 error_found = true;
1062 }
1063 do
1064 {
1065 if (!n->same_comdat_group)
1066 {
1067 error ("same_comdat_group is not a circular list");
1068 error_found = true;
1069 break;
1070 }
1071 n = n->same_comdat_group;
1072 }
1073 while (n != this);
1074 if (comdat_local_p ())
1075 {
1076 ipa_ref *ref = NULL;
1077
1078 for (int i = 0; iterate_referring (i, ref); ++i)
1079 {
1080 if (!in_same_comdat_group_p (ref->referring))
1081 {
1082 error ("comdat-local symbol referred to by %s outside its "
1083 "comdat",
1084 identifier_to_locale (ref->referring->name()));
1085 error_found = true;
1086 }
1087 }
1088 }
1089 }
1090 if (implicit_section && !get_section ())
1091 {
1092 error ("implicit_section flag is set but section isn't");
1093 error_found = true;
1094 }
1095 if (get_section () && get_comdat_group ()
1096 && !implicit_section)
1097 {
1098 error ("Both section and comdat group is set");
1099 error_found = true;
1100 }
1101 /* TODO: Add string table for sections, so we do not keep holding duplicated
1102 strings. */
1103 if (alias && definition
1104 && get_section () != get_alias_target ()->get_section ()
1105 && (!get_section()
1106 || !get_alias_target ()->get_section ()
1107 || strcmp (get_section(),
1108 get_alias_target ()->get_section ())))
1109 {
1110 error ("Alias and target's section differs");
1111 get_alias_target ()->dump (stderr);
1112 error_found = true;
1113 }
1114 if (alias && definition
1115 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1116 {
1117 error ("Alias and target's comdat groups differs");
1118 get_alias_target ()->dump (stderr);
1119 error_found = true;
1120 }
1121
1122 return error_found;
1123 }
1124
1125 /* Verify consistency of NODE. */
1126
1127 DEBUG_FUNCTION void
1128 symtab_node::verify (void)
1129 {
1130 if (seen_error ())
1131 return;
1132
1133 timevar_push (TV_CGRAPH_VERIFY);
1134 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1135 node->verify_node ();
1136 else
1137 if (verify_base ())
1138 {
1139 debug ();
1140 internal_error ("symtab_node::verify failed");
1141 }
1142 timevar_pop (TV_CGRAPH_VERIFY);
1143 }
1144
1145 /* Verify symbol table for internal consistency. */
1146
1147 DEBUG_FUNCTION void
1148 symtab_node::verify_symtab_nodes (void)
1149 {
1150 symtab_node *node;
1151 hash_map<tree, symtab_node *> comdat_head_map (251);
1152
1153 FOR_EACH_SYMBOL (node)
1154 {
1155 node->verify ();
1156 if (node->get_comdat_group ())
1157 {
1158 symtab_node **entry, *s;
1159 bool existed;
1160
1161 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1162 &existed);
1163 if (!existed)
1164 *entry = node;
1165 else
1166 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
1167 if (!s || s == *entry)
1168 {
1169 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
1170 (*entry)->debug ();
1171 node->debug ();
1172 internal_error ("symtab_node::verify failed");
1173 }
1174 }
1175 }
1176 }
1177
1178 /* Return true when NODE is known to be used from other (non-LTO)
1179 object file. Known only when doing LTO via linker plugin. */
1180
1181 bool
1182 symtab_node::used_from_object_file_p_worker (symtab_node *node)
1183 {
1184 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1185 return false;
1186 if (resolution_used_from_other_file_p (node->resolution))
1187 return true;
1188 return false;
1189 }
1190
1191
1192 /* Return true when symtab_node is known to be used from other (non-LTO)
1193 object file. Known only when doing LTO via linker plugin. */
1194
1195 bool
1196 symtab_node::used_from_object_file_p (void)
1197 {
1198 return symtab_node::used_from_object_file_p_worker (this);
1199 }
1200
1201 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1202 but other code such as notice_global_symbol generates rtl. */
1203
1204 void
1205 symtab_node::make_decl_local (void)
1206 {
1207 rtx rtl, symbol;
1208
1209 /* Avoid clearing comdat_groups on comdat-local decls. */
1210 if (TREE_PUBLIC (decl) == 0)
1211 return;
1212
1213 if (TREE_CODE (decl) == VAR_DECL)
1214 DECL_COMMON (decl) = 0;
1215 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1216
1217 DECL_COMDAT (decl) = 0;
1218 DECL_WEAK (decl) = 0;
1219 DECL_EXTERNAL (decl) = 0;
1220 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1221 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1222 TREE_PUBLIC (decl) = 0;
1223 if (!DECL_RTL_SET_P (decl))
1224 return;
1225
1226 /* Update rtl flags. */
1227 make_decl_rtl (decl);
1228
1229 rtl = DECL_RTL (decl);
1230 if (!MEM_P (rtl))
1231 return;
1232
1233 symbol = XEXP (rtl, 0);
1234 if (GET_CODE (symbol) != SYMBOL_REF)
1235 return;
1236
1237 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1238 }
1239
1240 /* Walk the alias chain to return the symbol NODE is alias of.
1241 If NODE is not an alias, return NODE.
1242 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1243
1244 symtab_node *
1245 symtab_node::ultimate_alias_target (enum availability *availability)
1246 {
1247 bool weakref_p = false;
1248
1249 if (!alias)
1250 {
1251 if (availability)
1252 *availability = get_availability ();
1253 return this;
1254 }
1255
1256 /* To determine visibility of the target, we follow ELF semantic of aliases.
1257 Here alias is an alternative assembler name of a given definition. Its
1258 availability prevails the availability of its target (i.e. static alias of
1259 weak definition is available.
1260
1261 Weakref is a different animal (and not part of ELF per se). It is just
1262 alternative name of a given symbol used within one complation unit
1263 and is translated prior hitting the object file. It inherits the
1264 visibility of its target (i.e. weakref of non-overwritable definition
1265 is non-overwritable, while weakref of weak definition is weak).
1266
1267 If we ever get into supporting targets with different semantics, a target
1268 hook will be needed here. */
1269
1270 if (availability)
1271 {
1272 weakref_p = weakref;
1273 if (!weakref_p)
1274 *availability = get_availability ();
1275 else
1276 *availability = AVAIL_LOCAL;
1277 }
1278
1279 symtab_node *node = this;
1280 while (node)
1281 {
1282 if (node->alias && node->analyzed)
1283 node = node->get_alias_target ();
1284 else
1285 {
1286 if (!availability)
1287 ;
1288 else if (node->analyzed)
1289 {
1290 if (weakref_p)
1291 {
1292 enum availability a = node->get_availability ();
1293 if (a < *availability)
1294 *availability = a;
1295 }
1296 }
1297 else
1298 *availability = AVAIL_NOT_AVAILABLE;
1299 return node;
1300 }
1301 if (node && availability && weakref_p)
1302 {
1303 enum availability a = node->get_availability ();
1304 if (a < *availability)
1305 *availability = a;
1306 weakref_p = node->weakref;
1307 }
1308 }
1309 if (availability)
1310 *availability = AVAIL_NOT_AVAILABLE;
1311 return NULL;
1312 }
1313
1314 /* C++ FE sometimes change linkage flags after producing same body aliases.
1315
1316 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1317 are obviously equivalent. The way it is doing so is however somewhat
1318 kludgy and interferes with the visibility code. As a result we need to
1319 copy the visibility from the target to get things right. */
1320
1321 void
1322 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1323 {
1324 if (is_a <cgraph_node *> (this))
1325 {
1326 DECL_DECLARED_INLINE_P (decl)
1327 = DECL_DECLARED_INLINE_P (target->decl);
1328 DECL_DISREGARD_INLINE_LIMITS (decl)
1329 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1330 }
1331 /* FIXME: It is not really clear why those flags should not be copied for
1332 functions, too. */
1333 else
1334 {
1335 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1336 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1337 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1338 }
1339 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1340 if (TREE_PUBLIC (decl))
1341 {
1342 tree group;
1343
1344 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1345 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1346 group = target->get_comdat_group ();
1347 set_comdat_group (group);
1348 if (group && !same_comdat_group)
1349 add_to_same_comdat_group (target);
1350 }
1351 externally_visible = target->externally_visible;
1352 }
1353
1354 /* Set section, do not recurse into aliases.
1355 When one wants to change section of symbol and its aliases,
1356 use set_section. */
1357
1358 void
1359 symtab_node::set_section_for_node (const char *section)
1360 {
1361 const char *current = get_section ();
1362 section_hash_entry **slot;
1363
1364 if (current == section
1365 || (current && section
1366 && !strcmp (current, section)))
1367 return;
1368
1369 if (current)
1370 {
1371 x_section->ref_count--;
1372 if (!x_section->ref_count)
1373 {
1374 hashval_t hash = htab_hash_string (x_section->name);
1375 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1376 hash, INSERT);
1377 ggc_free (x_section);
1378 symtab->section_hash->clear_slot (slot);
1379 }
1380 x_section = NULL;
1381 }
1382 if (!section)
1383 {
1384 implicit_section = false;
1385 return;
1386 }
1387 if (!symtab->section_hash)
1388 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1389 slot = symtab->section_hash->find_slot_with_hash (section,
1390 htab_hash_string (section),
1391 INSERT);
1392 if (*slot)
1393 x_section = (section_hash_entry *)*slot;
1394 else
1395 {
1396 int len = strlen (section);
1397 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1398 x_section->name = ggc_vec_alloc<char> (len + 1);
1399 memcpy (x_section->name, section, len + 1);
1400 }
1401 x_section->ref_count++;
1402 }
1403
1404 /* Worker for set_section. */
1405
1406 bool
1407 symtab_node::set_section (symtab_node *n, void *s)
1408 {
1409 n->set_section_for_node ((char *)s);
1410 return false;
1411 }
1412
1413 /* Set section of symbol and its aliases. */
1414
1415 void
1416 symtab_node::set_section (const char *section)
1417 {
1418 gcc_assert (!this->alias);
1419 call_for_symbol_and_aliases
1420 (symtab_node::set_section, const_cast<char *>(section), true);
1421 }
1422
1423 /* Return the initialization priority. */
1424
1425 priority_type
1426 symtab_node::get_init_priority ()
1427 {
1428 if (!this->in_init_priority_hash)
1429 return DEFAULT_INIT_PRIORITY;
1430
1431 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1432 return h ? h->init : DEFAULT_INIT_PRIORITY;
1433 }
1434
1435 /* Return availability of NODE. */
1436 enum availability symtab_node::get_availability (void)
1437 {
1438 if (is_a <cgraph_node *> (this))
1439 return dyn_cast <cgraph_node *> (this)->get_availability ();
1440 else
1441 return dyn_cast <varpool_node *> (this)->get_availability ();;
1442 }
1443
1444
1445 /* Return the finalization priority. */
1446
1447 priority_type
1448 cgraph_node::get_fini_priority ()
1449 {
1450 if (!this->in_init_priority_hash)
1451 return DEFAULT_INIT_PRIORITY;
1452 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1453 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1454 }
1455
1456 /* Return the initialization and finalization priority information for
1457 DECL. If there is no previous priority information, a freshly
1458 allocated structure is returned. */
1459
1460 symbol_priority_map *
1461 symtab_node::priority_info (void)
1462 {
1463 if (!symtab->init_priority_hash)
1464 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1465
1466 bool existed;
1467 symbol_priority_map *h
1468 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1469 if (!existed)
1470 {
1471 h->init = DEFAULT_INIT_PRIORITY;
1472 h->fini = DEFAULT_INIT_PRIORITY;
1473 in_init_priority_hash = true;
1474 }
1475
1476 return h;
1477 }
1478
1479 /* Set initialization priority to PRIORITY. */
1480
1481 void
1482 symtab_node::set_init_priority (priority_type priority)
1483 {
1484 symbol_priority_map *h;
1485
1486 if (is_a <cgraph_node *> (this))
1487 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1488
1489 if (priority == DEFAULT_INIT_PRIORITY)
1490 {
1491 gcc_assert (get_init_priority() == priority);
1492 return;
1493 }
1494 h = priority_info ();
1495 h->init = priority;
1496 }
1497
1498 /* Set fialization priority to PRIORITY. */
1499
1500 void
1501 cgraph_node::set_fini_priority (priority_type priority)
1502 {
1503 symbol_priority_map *h;
1504
1505 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1506
1507 if (priority == DEFAULT_INIT_PRIORITY)
1508 {
1509 gcc_assert (get_fini_priority() == priority);
1510 return;
1511 }
1512 h = priority_info ();
1513 h->fini = priority;
1514 }
1515
1516 /* Worker for symtab_resolve_alias. */
1517
1518 bool
1519 symtab_node::set_implicit_section (symtab_node *n,
1520 void *data ATTRIBUTE_UNUSED)
1521 {
1522 n->implicit_section = true;
1523 return false;
1524 }
1525
1526 /* Add reference recording that symtab node is alias of TARGET.
1527 The function can fail in the case of aliasing cycles; in this case
1528 it returns false. */
1529
1530 bool
1531 symtab_node::resolve_alias (symtab_node *target)
1532 {
1533 symtab_node *n;
1534
1535 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1536
1537 /* Never let cycles to creep into the symbol table alias references;
1538 those will make alias walkers to be infinite. */
1539 for (n = target; n && n->alias;
1540 n = n->analyzed ? n->get_alias_target () : NULL)
1541 if (n == this)
1542 {
1543 if (is_a <cgraph_node *> (this))
1544 error ("function %q+D part of alias cycle", decl);
1545 else if (is_a <varpool_node *> (this))
1546 error ("variable %q+D part of alias cycle", decl);
1547 else
1548 gcc_unreachable ();
1549 alias = false;
1550 return false;
1551 }
1552
1553 /* "analyze" the node - i.e. mark the reference. */
1554 definition = true;
1555 alias = true;
1556 analyzed = true;
1557 create_reference (target, IPA_REF_ALIAS, NULL);
1558
1559 /* Add alias into the comdat group of its target unless it is already there. */
1560 if (same_comdat_group)
1561 remove_from_same_comdat_group ();
1562 set_comdat_group (NULL);
1563 if (target->get_comdat_group ())
1564 add_to_same_comdat_group (target);
1565
1566 if ((get_section () != target->get_section ()
1567 || target->get_comdat_group ()) && get_section () && !implicit_section)
1568 {
1569 error ("section of alias %q+D must match section of its target", decl);
1570 }
1571 call_for_symbol_and_aliases (symtab_node::set_section,
1572 const_cast<char *>(target->get_section ()), true);
1573 if (target->implicit_section)
1574 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1575
1576 /* Alias targets become redundant after alias is resolved into an reference.
1577 We do not want to keep it around or we would have to mind updating them
1578 when renaming symbols. */
1579 alias_target = NULL;
1580
1581 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1582 fixup_same_cpp_alias_visibility (target);
1583
1584 /* If alias has address taken, so does the target. */
1585 if (address_taken)
1586 target->ultimate_alias_target ()->address_taken = true;
1587 return true;
1588 }
1589
1590 /* Call calback on symtab node and aliases associated to this node.
1591 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1592 skipped. */
1593
1594 bool
1595 symtab_node::call_for_symbol_and_aliases (bool (*callback) (symtab_node *,
1596 void *),
1597 void *data, bool include_overwritable)
1598 {
1599 int i;
1600 ipa_ref *ref;
1601
1602 if (callback (this, data))
1603 return true;
1604 for (i = 0; iterate_referring (i, ref); i++)
1605 if (ref->use == IPA_REF_ALIAS)
1606 {
1607 symtab_node *alias = ref->referring;
1608 if (include_overwritable
1609 || alias->get_availability () > AVAIL_INTERPOSABLE)
1610 if (alias->call_for_symbol_and_aliases (callback, data,
1611 include_overwritable))
1612 return true;
1613 }
1614 return false;
1615 }
1616
1617 /* Worker searching noninterposable alias. */
1618
1619 bool
1620 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1621 {
1622 if (decl_binds_to_current_def_p (node->decl))
1623 {
1624 symtab_node *fn = node->ultimate_alias_target ();
1625
1626 /* Ensure that the alias is well formed this may not be the case
1627 of user defined aliases and currently it is not always the case
1628 of C++ same body aliases (that is a bug). */
1629 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1630 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1631 || (TREE_CODE (node->decl) == FUNCTION_DECL
1632 && flags_from_decl_or_type (node->decl)
1633 != flags_from_decl_or_type (fn->decl))
1634 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1635 return false;
1636
1637 *(symtab_node **)data = node;
1638 return true;
1639 }
1640 return false;
1641 }
1642
1643 /* If node can not be overwriten by static or dynamic linker to point to
1644 different definition, return NODE. Otherwise look for alias with such
1645 property and if none exists, introduce new one. */
1646
1647 symtab_node *
1648 symtab_node::noninterposable_alias (void)
1649 {
1650 tree new_decl;
1651 symtab_node *new_node = NULL;
1652
1653 /* First try to look up existing alias or base object
1654 (if that is already non-overwritable). */
1655 symtab_node *node = ultimate_alias_target ();
1656 gcc_assert (!node->alias && !node->weakref);
1657 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1658 (void *)&new_node, true);
1659 if (new_node)
1660 return new_node;
1661 #ifndef ASM_OUTPUT_DEF
1662 /* If aliases aren't supported by the assembler, fail. */
1663 return NULL;
1664 #endif
1665
1666 /* Otherwise create a new one. */
1667 new_decl = copy_node (node->decl);
1668 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1669 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1670 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1671 DECL_INITIAL (new_decl) = NULL;
1672 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1673 SET_DECL_RTL (new_decl, NULL);
1674
1675 /* Update the properties. */
1676 DECL_EXTERNAL (new_decl) = 0;
1677 TREE_PUBLIC (new_decl) = 0;
1678 DECL_COMDAT (new_decl) = 0;
1679 DECL_WEAK (new_decl) = 0;
1680
1681 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1682 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1683 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1684 {
1685 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1686 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1687 new_node = cgraph_node::create_alias (new_decl, node->decl);
1688 }
1689 else
1690 {
1691 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1692 DECL_INITIAL (new_decl) = error_mark_node;
1693 new_node = varpool_node::create_alias (new_decl, node->decl);
1694 }
1695 new_node->resolve_alias (node);
1696 gcc_assert (decl_binds_to_current_def_p (new_decl)
1697 && targetm.binds_local_p (new_decl));
1698 return new_node;
1699 }
1700
1701 /* Return true if symtab node and TARGET represents
1702 semantically equivalent symbols. */
1703
1704 bool
1705 symtab_node::semantically_equivalent_p (symtab_node *target)
1706 {
1707 enum availability avail;
1708 symtab_node *ba;
1709 symtab_node *bb;
1710
1711 /* Equivalent functions are equivalent. */
1712 if (decl == target->decl)
1713 return true;
1714
1715 /* If symbol is not overwritable by different implementation,
1716 walk to the base object it defines. */
1717 ba = ultimate_alias_target (&avail);
1718 if (avail >= AVAIL_AVAILABLE)
1719 {
1720 if (target == ba)
1721 return true;
1722 }
1723 else
1724 ba = this;
1725 bb = target->ultimate_alias_target (&avail);
1726 if (avail >= AVAIL_AVAILABLE)
1727 {
1728 if (this == bb)
1729 return true;
1730 }
1731 else
1732 bb = target;
1733 return bb == ba;
1734 }
1735
1736 /* Classify symbol symtab node for partitioning. */
1737
1738 enum symbol_partitioning_class
1739 symtab_node::get_partitioning_class (void)
1740 {
1741 /* Inline clones are always duplicated.
1742 This include external delcarations. */
1743 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1744
1745 if (DECL_ABSTRACT_P (decl))
1746 return SYMBOL_EXTERNAL;
1747
1748 if (cnode && cnode->global.inlined_to)
1749 return SYMBOL_DUPLICATE;
1750
1751 /* Weakref aliases are always duplicated. */
1752 if (weakref)
1753 return SYMBOL_DUPLICATE;
1754
1755 /* External declarations are external. */
1756 if (DECL_EXTERNAL (decl))
1757 return SYMBOL_EXTERNAL;
1758
1759 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1760 {
1761 /* Constant pool references use local symbol names that can not
1762 be promoted global. We should never put into a constant pool
1763 objects that can not be duplicated across partitions. */
1764 if (DECL_IN_CONSTANT_POOL (decl))
1765 return SYMBOL_DUPLICATE;
1766 gcc_checking_assert (vnode->definition);
1767 }
1768 /* Functions that are cloned may stay in callgraph even if they are unused.
1769 Handle them as external; compute_ltrans_boundary take care to make
1770 proper things to happen (i.e. to make them appear in the boundary but
1771 with body streamed, so clone can me materialized). */
1772 else if (!dyn_cast <cgraph_node *> (this)->definition)
1773 return SYMBOL_EXTERNAL;
1774
1775 /* Linker discardable symbols are duplicated to every use unless they are
1776 keyed. */
1777 if (DECL_ONE_ONLY (decl)
1778 && !force_output
1779 && !forced_by_abi
1780 && !used_from_object_file_p ())
1781 return SYMBOL_DUPLICATE;
1782
1783 return SYMBOL_PARTITION;
1784 }
1785
1786 /* Return true when symbol is known to be non-zero. */
1787
1788 bool
1789 symtab_node::nonzero_address ()
1790 {
1791 /* Weakrefs may be NULL when their target is not defined. */
1792 if (alias && weakref)
1793 {
1794 if (analyzed)
1795 {
1796 symtab_node *target = ultimate_alias_target ();
1797
1798 if (target->alias && target->weakref)
1799 return false;
1800 /* We can not recurse to target::nonzero. It is possible that the
1801 target is used only via the alias.
1802 We may walk references and look for strong use, but we do not know
1803 if this strong use will survive to final binary, so be
1804 conservative here.
1805 ??? Maybe we could do the lookup during late optimization that
1806 could be useful to eliminate the NULL pointer checks in LTO
1807 programs. */
1808 if (target->definition && !DECL_EXTERNAL (target->decl))
1809 return true;
1810 if (target->resolution != LDPR_UNKNOWN
1811 && target->resolution != LDPR_UNDEF
1812 && flag_delete_null_pointer_checks)
1813 return true;
1814 return false;
1815 }
1816 else
1817 return false;
1818 }
1819
1820 /* With !flag_delete_null_pointer_checks we assume that symbols may
1821 bind to NULL. This is on by default on embedded targets only.
1822
1823 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1824 linking fails. Important case of WEAK we want to do well are comdats.
1825 Those are handled by later check for definition.
1826
1827 When parsing, beware the cases when WEAK attribute is added later. */
1828 if (!DECL_WEAK (decl)
1829 && flag_delete_null_pointer_checks)
1830 {
1831 refuse_visibility_changes = true;
1832 return true;
1833 }
1834
1835 /* If target is defined and not extern, we know it will be output and thus
1836 it will bind to non-NULL.
1837 Play safe for flag_delete_null_pointer_checks where weak definition maye
1838 be re-defined by NULL. */
1839 if (definition && !DECL_EXTERNAL (decl)
1840 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1841 {
1842 if (!DECL_WEAK (decl))
1843 refuse_visibility_changes = true;
1844 return true;
1845 }
1846
1847 /* As the last resort, check the resolution info. */
1848 if (resolution != LDPR_UNKNOWN
1849 && resolution != LDPR_UNDEF
1850 && flag_delete_null_pointer_checks)
1851 return true;
1852 return false;
1853 }