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