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