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