]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/symtab.c
Automated part of renaming of symtab_node_base to symtab_node.
[thirdparty/gcc.git] / gcc / symtab.c
1 /* Symbol table.
2 Copyright (C) 2012-2013 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 "tree.h"
26 #include "gimple.h"
27 #include "tree-inline.h"
28 #include "langhooks.h"
29 #include "hashtab.h"
30 #include "ggc.h"
31 #include "cgraph.h"
32 #include "diagnostic.h"
33 #include "timevar.h"
34 #include "lto-streamer.h"
35 #include "rtl.h"
36
37 const char * const ld_plugin_symbol_resolution_names[]=
38 {
39 "",
40 "undef",
41 "prevailing_def",
42 "prevailing_def_ironly",
43 "preempted_reg",
44 "preempted_ir",
45 "resolved_ir",
46 "resolved_exec",
47 "resolved_dyn",
48 "prevailing_def_ironly_exp"
49 };
50
51 /* Hash table used to convert declarations into nodes. */
52 static GTY((param_is (symtab_node))) htab_t symtab_hash;
53 /* Hash table used to convert assembler names into nodes. */
54 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
55
56 /* Linked list of symbol table nodes. */
57 symtab_node *symtab_nodes;
58
59 /* The order index of the next symtab node to be created. This is
60 used so that we can sort the cgraph nodes in order by when we saw
61 them, to support -fno-toplevel-reorder. */
62 int symtab_order;
63
64 /* Returns a hash code for P. */
65
66 static hashval_t
67 hash_node (const void *p)
68 {
69 const symtab_node *n = (const symtab_node *) p;
70 return (hashval_t) DECL_UID (n->decl);
71 }
72
73
74 /* Returns nonzero if P1 and P2 are equal. */
75
76 static int
77 eq_node (const void *p1, const void *p2)
78 {
79 const symtab_node *n1 = (const symtab_node *) p1;
80 const symtab_node *n2 = (const symtab_node *) p2;
81 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
82 }
83
84 /* Returns a hash code for P. */
85
86 static hashval_t
87 hash_node_by_assembler_name (const void *p)
88 {
89 const symtab_node *n = (const symtab_node *) p;
90 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
91 }
92
93 /* Returns nonzero if P1 and P2 are equal. */
94
95 static int
96 eq_assembler_name (const void *p1, const void *p2)
97 {
98 const symtab_node *n1 = (const symtab_node *) p1;
99 const_tree name = (const_tree)p2;
100 return (decl_assembler_name_equal (n1->decl, name));
101 }
102
103 /* Insert NODE to assembler name hash. */
104
105 static void
106 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
107 {
108 if (is_a <varpool_node> (node) && DECL_HARD_REGISTER (node->decl))
109 return;
110 gcc_checking_assert (!node->previous_sharing_asm_name
111 && !node->next_sharing_asm_name);
112 if (assembler_name_hash)
113 {
114 void **aslot;
115 struct cgraph_node *cnode;
116 tree decl = node->decl;
117
118 tree name = DECL_ASSEMBLER_NAME (node->decl);
119
120 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
121 decl_assembler_name_hash (name),
122 INSERT);
123 gcc_assert (*aslot != node);
124 node->next_sharing_asm_name = (symtab_node *)*aslot;
125 if (*aslot != NULL)
126 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
127 *aslot = node;
128
129 /* Update also possible inline clones sharing a decl. */
130 cnode = dyn_cast <cgraph_node> (node);
131 if (cnode && cnode->clones && with_clones)
132 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
133 if (cnode->decl == decl)
134 insert_to_assembler_name_hash (cnode, true);
135 }
136
137 }
138
139 /* Remove NODE from assembler name hash. */
140
141 static void
142 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
143 {
144 if (assembler_name_hash)
145 {
146 struct cgraph_node *cnode;
147 tree decl = node->decl;
148
149 if (node->next_sharing_asm_name)
150 node->next_sharing_asm_name->previous_sharing_asm_name
151 = node->previous_sharing_asm_name;
152 if (node->previous_sharing_asm_name)
153 {
154 node->previous_sharing_asm_name->next_sharing_asm_name
155 = node->next_sharing_asm_name;
156 }
157 else
158 {
159 tree name = DECL_ASSEMBLER_NAME (node->decl);
160 void **slot;
161 slot = htab_find_slot_with_hash (assembler_name_hash, name,
162 decl_assembler_name_hash (name),
163 NO_INSERT);
164 gcc_assert (*slot == node);
165 if (!node->next_sharing_asm_name)
166 htab_clear_slot (assembler_name_hash, slot);
167 else
168 *slot = node->next_sharing_asm_name;
169 }
170 node->next_sharing_asm_name = NULL;
171 node->previous_sharing_asm_name = NULL;
172
173 /* Update also possible inline clones sharing a decl. */
174 cnode = dyn_cast <cgraph_node> (node);
175 if (cnode && cnode->clones && with_clones)
176 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
177 if (cnode->decl == decl)
178 unlink_from_assembler_name_hash (cnode, true);
179 }
180 }
181
182 /* Arrange node to be first in its entry of assembler_name_hash. */
183
184 void
185 symtab_prevail_in_asm_name_hash (symtab_node *node)
186 {
187 unlink_from_assembler_name_hash (node, false);
188 insert_to_assembler_name_hash (node, false);
189 }
190
191
192 /* Add node into symbol table. This function is not used directly, but via
193 cgraph/varpool node creation routines. */
194
195 void
196 symtab_register_node (symtab_node *node)
197 {
198 struct symtab_node key;
199 symtab_node **slot;
200
201 node->next = symtab_nodes;
202 node->previous = NULL;
203 if (symtab_nodes)
204 symtab_nodes->previous = node;
205 symtab_nodes = node;
206
207 if (!symtab_hash)
208 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
209 key.decl = node->decl;
210 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
211 if (*slot == NULL)
212 *slot = node;
213
214 ipa_empty_ref_list (&node->ref_list);
215
216 node->order = symtab_order++;
217
218 /* Be sure to do this last; C++ FE might create new nodes via
219 DECL_ASSEMBLER_NAME langhook! */
220 insert_to_assembler_name_hash (node, false);
221 }
222
223 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
224 of inline clones. */
225
226 void
227 symtab_insert_node_to_hashtable (symtab_node *node)
228 {
229 struct symtab_node key;
230 symtab_node **slot;
231
232 if (!symtab_hash)
233 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
234 key.decl = node->decl;
235 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
236 *slot = node;
237 }
238
239 /* Remove node from symbol table. This function is not used directly, but via
240 cgraph/varpool node removal routines. */
241
242 void
243 symtab_unregister_node (symtab_node *node)
244 {
245 void **slot;
246 ipa_remove_all_references (&node->ref_list);
247 ipa_remove_all_referring (&node->ref_list);
248
249 if (node->same_comdat_group)
250 {
251 symtab_node *prev;
252 for (prev = node->same_comdat_group;
253 prev->same_comdat_group != node;
254 prev = prev->same_comdat_group)
255 ;
256 if (node->same_comdat_group == prev)
257 prev->same_comdat_group = NULL;
258 else
259 prev->same_comdat_group = node->same_comdat_group;
260 node->same_comdat_group = NULL;
261 }
262
263 if (node->previous)
264 node->previous->next = node->next;
265 else
266 symtab_nodes = node->next;
267 if (node->next)
268 node->next->previous = node->previous;
269 node->next = NULL;
270 node->previous = NULL;
271
272 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
273
274 /* During LTO symtab merging we temporarily corrupt decl to symtab node
275 hash. */
276 gcc_assert ((slot && *slot) || in_lto_p);
277 if (slot && *slot && *slot == node)
278 {
279 symtab_node *replacement_node = NULL;
280 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
281 replacement_node = cgraph_find_replacement_node (cnode);
282 if (!replacement_node)
283 htab_clear_slot (symtab_hash, slot);
284 else
285 *slot = replacement_node;
286 }
287 if (!is_a <varpool_node> (node) || !DECL_HARD_REGISTER (node->decl))
288 unlink_from_assembler_name_hash (node, false);
289 }
290
291 /* Return symbol table node associated with DECL, if any,
292 and NULL otherwise. */
293
294 symtab_node *
295 symtab_get_node (const_tree decl)
296 {
297 symtab_node **slot;
298 struct symtab_node key;
299
300 #ifdef ENABLE_CHECKING
301 /* Check that we are called for sane type of object - functions
302 and static or external variables. */
303 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
304 || (TREE_CODE (decl) == VAR_DECL
305 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
306 || in_lto_p)));
307 #endif
308
309 if (!symtab_hash)
310 return NULL;
311
312 key.decl = CONST_CAST2 (tree, const_tree, decl);
313
314 slot = (symtab_node **) htab_find_slot (symtab_hash, &key,
315 NO_INSERT);
316
317 if (slot)
318 return *slot;
319 return NULL;
320 }
321
322 /* Remove symtab NODE from the symbol table. */
323
324 void
325 symtab_remove_node (symtab_node *node)
326 {
327 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
328 cgraph_remove_node (cnode);
329 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
330 varpool_remove_node (vnode);
331 }
332
333 /* Initalize asm name hash unless. */
334
335 void
336 symtab_initialize_asm_name_hash (void)
337 {
338 symtab_node *node;
339 if (!assembler_name_hash)
340 {
341 assembler_name_hash =
342 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
343 NULL);
344 FOR_EACH_SYMBOL (node)
345 insert_to_assembler_name_hash (node, false);
346 }
347 }
348
349 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
350 Return NULL if there's no such node. */
351
352 symtab_node *
353 symtab_node_for_asm (const_tree asmname)
354 {
355 symtab_node *node;
356 void **slot;
357
358 symtab_initialize_asm_name_hash ();
359 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
360 decl_assembler_name_hash (asmname),
361 NO_INSERT);
362
363 if (slot)
364 {
365 node = (symtab_node *) *slot;
366 return node;
367 }
368 return NULL;
369 }
370
371 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
372
373 void
374 change_decl_assembler_name (tree decl, tree name)
375 {
376 symtab_node *node = NULL;
377
378 /* We can have user ASM names on things, like global register variables, that
379 are not in the symbol table. */
380 if ((TREE_CODE (decl) == VAR_DECL
381 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
382 || TREE_CODE (decl) == FUNCTION_DECL)
383 node = symtab_get_node (decl);
384 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
385 {
386 SET_DECL_ASSEMBLER_NAME (decl, name);
387 if (node)
388 insert_to_assembler_name_hash (node, true);
389 }
390 else
391 {
392 if (name == DECL_ASSEMBLER_NAME (decl))
393 return;
394
395 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
396 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
397 : NULL);
398 if (node)
399 unlink_from_assembler_name_hash (node, true);
400 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
401 && DECL_RTL_SET_P (decl))
402 warning (0, "%D renamed after being referenced in assembly", decl);
403
404 SET_DECL_ASSEMBLER_NAME (decl, name);
405 if (alias)
406 {
407 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
408 TREE_CHAIN (DECL_ASSEMBLER_NAME (name)) = alias;
409 }
410 if (node)
411 insert_to_assembler_name_hash (node, true);
412 }
413 }
414
415 /* Add NEW_ to the same comdat group that OLD is in. */
416
417 void
418 symtab_add_to_same_comdat_group (symtab_node *new_node,
419 symtab_node *old_node)
420 {
421 gcc_assert (DECL_ONE_ONLY (old_node->decl));
422 gcc_assert (!new_node->same_comdat_group);
423 gcc_assert (new_node != old_node);
424
425 DECL_COMDAT_GROUP (new_node->decl) = DECL_COMDAT_GROUP (old_node->decl);
426 new_node->same_comdat_group = old_node;
427 if (!old_node->same_comdat_group)
428 old_node->same_comdat_group = new_node;
429 else
430 {
431 symtab_node *n;
432 for (n = old_node->same_comdat_group;
433 n->same_comdat_group != old_node;
434 n = n->same_comdat_group)
435 ;
436 n->same_comdat_group = new_node;
437 }
438 }
439
440 /* Dissolve the same_comdat_group list in which NODE resides. */
441
442 void
443 symtab_dissolve_same_comdat_group_list (symtab_node *node)
444 {
445 symtab_node *n = node;
446 symtab_node *next;
447
448 if (!node->same_comdat_group)
449 return;
450 do
451 {
452 next = n->same_comdat_group;
453 n->same_comdat_group = NULL;
454 n = next;
455 }
456 while (n != node);
457 }
458
459 /* Return printable assembler name of NODE.
460 This function is used only for debugging. When assembler name
461 is unknown go with identifier name. */
462
463 const char *
464 symtab_node_asm_name (symtab_node *node)
465 {
466 if (!DECL_ASSEMBLER_NAME_SET_P (node->decl))
467 return lang_hooks.decl_printable_name (node->decl, 2);
468 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl));
469 }
470
471 /* Return printable identifier name. */
472
473 const char *
474 symtab_node_name (symtab_node *node)
475 {
476 return lang_hooks.decl_printable_name (node->decl, 2);
477 }
478
479 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
480
481 /* Dump base fields of symtab nodes. Not to be used directly. */
482
483 void
484 dump_symtab_base (FILE *f, symtab_node *node)
485 {
486 static const char * const visibility_types[] = {
487 "default", "protected", "hidden", "internal"
488 };
489
490 fprintf (f, "%s/%i (%s)",
491 symtab_node_asm_name (node),
492 node->order,
493 symtab_node_name (node));
494 dump_addr (f, " @", (void *)node);
495 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
496
497 if (node->definition)
498 fprintf (f, " definition");
499 if (node->analyzed)
500 fprintf (f, " analyzed");
501 if (node->alias)
502 fprintf (f, " alias");
503 if (node->weakref)
504 fprintf (f, " weakref");
505 if (node->cpp_implicit_alias)
506 fprintf (f, " cpp_implicit_alias");
507 if (node->alias_target)
508 fprintf (f, " target:%s",
509 DECL_P (node->alias_target)
510 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
511 (node->alias_target))
512 : IDENTIFIER_POINTER (node->alias_target));
513 fprintf (f, "\n Visibility:");
514 if (node->in_other_partition)
515 fprintf (f, " in_other_partition");
516 if (node->used_from_other_partition)
517 fprintf (f, " used_from_other_partition");
518 if (node->force_output)
519 fprintf (f, " force_output");
520 if (node->forced_by_abi)
521 fprintf (f, " forced_by_abi");
522 if (node->externally_visible)
523 fprintf (f, " externally_visible");
524 if (node->resolution != LDPR_UNKNOWN)
525 fprintf (f, " %s",
526 ld_plugin_symbol_resolution_names[(int)node->resolution]);
527 if (TREE_ASM_WRITTEN (node->decl))
528 fprintf (f, " asm_written");
529 if (DECL_EXTERNAL (node->decl))
530 fprintf (f, " external");
531 if (TREE_PUBLIC (node->decl))
532 fprintf (f, " public");
533 if (DECL_COMMON (node->decl))
534 fprintf (f, " common");
535 if (DECL_WEAK (node->decl))
536 fprintf (f, " weak");
537 if (DECL_DLLIMPORT_P (node->decl))
538 fprintf (f, " dll_import");
539 if (DECL_COMDAT (node->decl))
540 fprintf (f, " comdat");
541 if (DECL_COMDAT_GROUP (node->decl))
542 fprintf (f, " comdat_group:%s",
543 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->decl)));
544 if (DECL_ONE_ONLY (node->decl))
545 fprintf (f, " one_only");
546 if (DECL_SECTION_NAME (node->decl))
547 fprintf (f, " section_name:%s",
548 TREE_STRING_POINTER (DECL_SECTION_NAME (node->decl)));
549 if (DECL_VISIBILITY_SPECIFIED (node->decl))
550 fprintf (f, " visibility_specified");
551 if (DECL_VISIBILITY (node->decl))
552 fprintf (f, " visibility:%s",
553 visibility_types [DECL_VISIBILITY (node->decl)]);
554 if (DECL_VIRTUAL_P (node->decl))
555 fprintf (f, " virtual");
556 if (DECL_ARTIFICIAL (node->decl))
557 fprintf (f, " artificial");
558 if (TREE_CODE (node->decl) == FUNCTION_DECL)
559 {
560 if (DECL_STATIC_CONSTRUCTOR (node->decl))
561 fprintf (f, " constructor");
562 if (DECL_STATIC_DESTRUCTOR (node->decl))
563 fprintf (f, " destructor");
564 }
565 fprintf (f, "\n");
566
567 if (node->same_comdat_group)
568 fprintf (f, " Same comdat group as: %s/%i\n",
569 symtab_node_asm_name (node->same_comdat_group),
570 node->same_comdat_group->order);
571 if (node->next_sharing_asm_name)
572 fprintf (f, " next sharing asm name: %i\n",
573 node->next_sharing_asm_name->order);
574 if (node->previous_sharing_asm_name)
575 fprintf (f, " previous sharing asm name: %i\n",
576 node->previous_sharing_asm_name->order);
577
578 if (node->address_taken)
579 fprintf (f, " Address is taken.\n");
580 if (node->aux)
581 {
582 fprintf (f, " Aux:");
583 dump_addr (f, " @", (void *)node->aux);
584 }
585
586 fprintf (f, " References: ");
587 ipa_dump_references (f, &node->ref_list);
588 fprintf (f, " Referring: ");
589 ipa_dump_referring (f, &node->ref_list);
590 if (node->lto_file_data)
591 fprintf (f, " Read from file: %s\n",
592 node->lto_file_data->file_name);
593 }
594
595 /* Dump symtab node. */
596
597 void
598 dump_symtab_node (FILE *f, symtab_node *node)
599 {
600 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
601 dump_cgraph_node (f, cnode);
602 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
603 dump_varpool_node (f, vnode);
604 }
605
606 /* Dump symbol table. */
607
608 void
609 dump_symtab (FILE *f)
610 {
611 symtab_node *node;
612 fprintf (f, "Symbol table:\n\n");
613 FOR_EACH_SYMBOL (node)
614 dump_symtab_node (f, node);
615 }
616
617 /* Dump symtab node NODE to stderr. */
618
619 DEBUG_FUNCTION void
620 debug_symtab_node (symtab_node *node)
621 {
622 dump_symtab_node (stderr, node);
623 }
624
625 /* Dump symbol table to stderr. */
626
627 DEBUG_FUNCTION void
628 debug_symtab (void)
629 {
630 dump_symtab (stderr);
631 }
632
633 /* Verify common part of symtab nodes. */
634
635 DEBUG_FUNCTION bool
636 verify_symtab_base (symtab_node *node)
637 {
638 bool error_found = false;
639 symtab_node *hashed_node;
640
641 if (is_a <cgraph_node> (node))
642 {
643 if (TREE_CODE (node->decl) != FUNCTION_DECL)
644 {
645 error ("function symbol is not function");
646 error_found = true;
647 }
648 }
649 else if (is_a <varpool_node> (node))
650 {
651 if (TREE_CODE (node->decl) != VAR_DECL)
652 {
653 error ("variable symbol is not variable");
654 error_found = true;
655 }
656 }
657 else
658 {
659 error ("node has unknown type");
660 error_found = true;
661 }
662
663 if (cgraph_state != CGRAPH_LTO_STREAMING)
664 {
665 hashed_node = symtab_get_node (node->decl);
666 if (!hashed_node)
667 {
668 error ("node not found in symtab decl hashtable");
669 error_found = true;
670 }
671 if (hashed_node != node
672 && (!is_a <cgraph_node> (node)
673 || !dyn_cast <cgraph_node> (node)->clone_of
674 || dyn_cast <cgraph_node> (node)->clone_of->decl
675 != node->decl))
676 {
677 error ("node differs from symtab decl hashtable");
678 error_found = true;
679 }
680 }
681 if (assembler_name_hash)
682 {
683 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
684 if (hashed_node && hashed_node->previous_sharing_asm_name)
685 {
686 error ("assembler name hash list corrupted");
687 error_found = true;
688 }
689 while (hashed_node)
690 {
691 if (hashed_node == node)
692 break;
693 hashed_node = hashed_node->next_sharing_asm_name;
694 }
695 if (!hashed_node
696 && !(is_a <varpool_node> (node)
697 || DECL_HARD_REGISTER (node->decl)))
698 {
699 error ("node not found in symtab assembler name hash");
700 error_found = true;
701 }
702 }
703 if (node->previous_sharing_asm_name
704 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
705 {
706 error ("double linked list of assembler names corrupted");
707 error_found = true;
708 }
709 if (node->analyzed && !node->definition)
710 {
711 error ("node is analyzed byt it is not a definition");
712 error_found = true;
713 }
714 if (node->cpp_implicit_alias && !node->alias)
715 {
716 error ("node is alias but not implicit alias");
717 error_found = true;
718 }
719 if (node->alias && !node->definition
720 && !node->weakref)
721 {
722 error ("node is alias but not definition");
723 error_found = true;
724 }
725 if (node->weakref && !node->alias)
726 {
727 error ("node is weakref but not an alias");
728 error_found = true;
729 }
730 if (node->same_comdat_group)
731 {
732 symtab_node *n = node->same_comdat_group;
733
734 if (!DECL_ONE_ONLY (n->decl))
735 {
736 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
737 error_found = true;
738 }
739 if (n->type != node->type)
740 {
741 error ("mixing different types of symbol in same comdat groups is not supported");
742 error_found = true;
743 }
744 if (n == node)
745 {
746 error ("node is alone in a comdat group");
747 error_found = true;
748 }
749 do
750 {
751 if (!n->same_comdat_group)
752 {
753 error ("same_comdat_group is not a circular list");
754 error_found = true;
755 break;
756 }
757 n = n->same_comdat_group;
758 }
759 while (n != node);
760 }
761 return error_found;
762 }
763
764 /* Verify consistency of NODE. */
765
766 DEBUG_FUNCTION void
767 verify_symtab_node (symtab_node *node)
768 {
769 if (seen_error ())
770 return;
771
772 timevar_push (TV_CGRAPH_VERIFY);
773 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
774 verify_cgraph_node (cnode);
775 else
776 if (verify_symtab_base (node))
777 {
778 dump_symtab_node (stderr, node);
779 internal_error ("verify_symtab_node failed");
780 }
781 timevar_pop (TV_CGRAPH_VERIFY);
782 }
783
784 /* Verify symbol table for internal consistency. */
785
786 DEBUG_FUNCTION void
787 verify_symtab (void)
788 {
789 symtab_node *node;
790 FOR_EACH_SYMBOL (node)
791 verify_symtab_node (node);
792 }
793
794 /* Return true when RESOLUTION indicate that linker will use
795 the symbol from non-LTO object files. */
796
797 bool
798 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
799 {
800 return (resolution == LDPR_PREVAILING_DEF
801 || resolution == LDPR_PREEMPTED_REG
802 || resolution == LDPR_RESOLVED_EXEC
803 || resolution == LDPR_RESOLVED_DYN);
804 }
805
806 /* Return true when NODE is known to be used from other (non-LTO) object file.
807 Known only when doing LTO via linker plugin. */
808
809 bool
810 symtab_used_from_object_file_p (symtab_node *node)
811 {
812 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
813 return false;
814 if (resolution_used_from_other_file_p (node->resolution))
815 return true;
816 return false;
817 }
818
819 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
820 but other code such as notice_global_symbol generates rtl. */
821
822 void
823 symtab_make_decl_local (tree decl)
824 {
825 rtx rtl, symbol;
826
827 if (TREE_CODE (decl) == VAR_DECL)
828 DECL_COMMON (decl) = 0;
829 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
830
831 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
832 {
833 DECL_SECTION_NAME (decl) = 0;
834 DECL_COMDAT (decl) = 0;
835 }
836 DECL_COMDAT_GROUP (decl) = 0;
837 DECL_WEAK (decl) = 0;
838 DECL_EXTERNAL (decl) = 0;
839 DECL_VISIBILITY_SPECIFIED (decl) = 0;
840 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
841 TREE_PUBLIC (decl) = 0;
842 if (!DECL_RTL_SET_P (decl))
843 return;
844
845 /* Update rtl flags. */
846 make_decl_rtl (decl);
847
848 rtl = DECL_RTL (decl);
849 if (!MEM_P (rtl))
850 return;
851
852 symbol = XEXP (rtl, 0);
853 if (GET_CODE (symbol) != SYMBOL_REF)
854 return;
855
856 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
857 }
858
859 /* Return availability of NODE. */
860
861 enum availability
862 symtab_node_availability (symtab_node *node)
863 {
864 if (is_a <cgraph_node> (node))
865 return cgraph_function_body_availability (cgraph (node));
866 else
867 return cgraph_variable_initializer_availability (varpool (node));
868 }
869
870 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
871 If NODE is not an alias, return NODE.
872 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
873
874 symtab_node *
875 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
876 {
877 bool weakref_p = false;
878
879 if (!node->alias)
880 {
881 if (availability)
882 *availability = symtab_node_availability (node);
883 return node;
884 }
885
886 /* To determine visibility of the target, we follow ELF semantic of aliases.
887 Here alias is an alternative assembler name of a given definition. Its
888 availability prevails the availability of its target (i.e. static alias of
889 weak definition is available.
890
891 Weakref is a different animal (and not part of ELF per se). It is just
892 alternative name of a given symbol used within one complation unit
893 and is translated prior hitting the object file. It inherits the
894 visibility of its target (i.e. weakref of non-overwritable definition
895 is non-overwritable, while weakref of weak definition is weak).
896
897 If we ever get into supporting targets with different semantics, a target
898 hook will be needed here. */
899
900 if (availability)
901 {
902 weakref_p = node->weakref;
903 if (!weakref_p)
904 *availability = symtab_node_availability (node);
905 else
906 *availability = AVAIL_LOCAL;
907 }
908 while (node)
909 {
910 if (node->alias && node->analyzed)
911 node = symtab_alias_target (node);
912 else
913 {
914 if (!availability)
915 ;
916 else if (node->analyzed)
917 {
918 if (weakref_p)
919 {
920 enum availability a = symtab_node_availability (node);
921 if (a < *availability)
922 *availability = a;
923 }
924 }
925 else
926 *availability = AVAIL_NOT_AVAILABLE;
927 return node;
928 }
929 if (node && availability && weakref_p)
930 {
931 enum availability a = symtab_node_availability (node);
932 if (a < *availability)
933 *availability = a;
934 weakref_p = node->weakref;
935 }
936 }
937 if (availability)
938 *availability = AVAIL_NOT_AVAILABLE;
939 return NULL;
940 }
941
942 /* C++ FE sometimes change linkage flags after producing same body aliases.
943
944 FIXME: C++ produce implicit aliases for virtual functions and vtables that
945 are obviously equivalent. The way it is doing so is however somewhat
946 kludgy and interferes with the visibility code. As a result we need to
947 copy the visibility from the target to get things right. */
948
949 void
950 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
951 {
952 if (is_a <cgraph_node> (node))
953 {
954 DECL_DECLARED_INLINE_P (node->decl)
955 = DECL_DECLARED_INLINE_P (target->decl);
956 DECL_DISREGARD_INLINE_LIMITS (node->decl)
957 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
958 }
959 /* FIXME: It is not really clear why those flags should not be copied for
960 functions, too. */
961 else
962 {
963 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
964 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
965 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
966 }
967 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
968 if (TREE_PUBLIC (node->decl))
969 {
970 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
971 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
972 DECL_COMDAT_GROUP (node->decl)
973 = DECL_COMDAT_GROUP (target->decl);
974 if (DECL_ONE_ONLY (target->decl)
975 && !node->same_comdat_group)
976 symtab_add_to_same_comdat_group (node, target);
977 }
978 node->externally_visible = target->externally_visible;
979 }
980
981 /* Add reference recording that NODE is alias of TARGET.
982 The function can fail in the case of aliasing cycles; in this case
983 it returns false. */
984
985 bool
986 symtab_resolve_alias (symtab_node *node, symtab_node *target)
987 {
988 symtab_node *n;
989
990 gcc_assert (!node->analyzed
991 && !vec_safe_length (node->ref_list.references));
992
993 /* Never let cycles to creep into the symbol table alias references;
994 those will make alias walkers to be infinite. */
995 for (n = target; n && n->alias;
996 n = n->analyzed ? symtab_alias_target (n) : NULL)
997 if (n == node)
998 {
999 if (is_a <cgraph_node> (node))
1000 error ("function %q+D part of alias cycle", node->decl);
1001 else if (is_a <varpool_node> (node))
1002 error ("variable %q+D part of alias cycle", node->decl);
1003 else
1004 gcc_unreachable ();
1005 node->alias = false;
1006 return false;
1007 }
1008
1009 /* "analyze" the node - i.e. mark the reference. */
1010 node->definition = true;
1011 node->alias = true;
1012 node->analyzed = true;
1013 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1014
1015 /* Alias targets become reudndant after alias is resolved into an reference.
1016 We do not want to keep it around or we would have to mind updating them
1017 when renaming symbols. */
1018 node->alias_target = NULL;
1019
1020 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1021 fixup_same_cpp_alias_visibility (node, target);
1022
1023 /* If alias has address taken, so does the target. */
1024 if (node->address_taken)
1025 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1026 return true;
1027 }
1028
1029 /* Call calback on NODE and aliases associated to NODE.
1030 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1031 skipped. */
1032
1033 bool
1034 symtab_for_node_and_aliases (symtab_node *node,
1035 bool (*callback) (symtab_node *, void *),
1036 void *data,
1037 bool include_overwritable)
1038 {
1039 int i;
1040 struct ipa_ref *ref;
1041
1042 if (callback (node, data))
1043 return true;
1044 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1045 if (ref->use == IPA_REF_ALIAS)
1046 {
1047 symtab_node *alias = ref->referring;
1048 if (include_overwritable
1049 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1050 if (symtab_for_node_and_aliases (alias, callback, data,
1051 include_overwritable))
1052 return true;
1053 }
1054 return false;
1055 }
1056
1057 /* Worker searching nonoverwritable alias. */
1058
1059 static bool
1060 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1061 {
1062 if (decl_binds_to_current_def_p (node->decl))
1063 {
1064 *(symtab_node **)data = node;
1065 return true;
1066 }
1067 return false;
1068 }
1069
1070 /* If NODE can not be overwriten by static or dynamic linker to point to different
1071 definition, return NODE. Otherwise look for alias with such property and if
1072 none exists, introduce new one. */
1073
1074 symtab_node *
1075 symtab_nonoverwritable_alias (symtab_node *node)
1076 {
1077 tree new_decl;
1078 symtab_node *new_node = NULL;
1079
1080 /* First try to look up existing alias or base object
1081 (if that is already non-overwritable). */
1082 node = symtab_alias_ultimate_target (node, NULL);
1083 gcc_assert (!node->alias && !node->weakref);
1084 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1085 (void *)&new_node, true);
1086 if (new_node)
1087 return new_node;
1088 #ifndef ASM_OUTPUT_DEF
1089 /* If aliases aren't supported by the assembler, fail. */
1090 return NULL;
1091 #endif
1092
1093 /* Otherwise create a new one. */
1094 new_decl = copy_node (node->decl);
1095 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1096 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1097 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1098 DECL_INITIAL (new_decl) = NULL;
1099 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1100 SET_DECL_RTL (new_decl, NULL);
1101
1102 /* Update the properties. */
1103 DECL_EXTERNAL (new_decl) = 0;
1104 if (DECL_ONE_ONLY (node->decl))
1105 DECL_SECTION_NAME (new_decl) = NULL;
1106 DECL_COMDAT_GROUP (new_decl) = 0;
1107 TREE_PUBLIC (new_decl) = 0;
1108 DECL_COMDAT (new_decl) = 0;
1109 DECL_WEAK (new_decl) = 0;
1110 DECL_VIRTUAL_P (new_decl) = 0;
1111 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1112 {
1113 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1114 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1115 new_node = cgraph_create_function_alias
1116 (new_decl, node->decl);
1117 }
1118 else
1119 new_node = varpool_create_variable_alias (new_decl,
1120 node->decl);
1121 symtab_resolve_alias (new_node, node);
1122 gcc_assert (decl_binds_to_current_def_p (new_decl));
1123 return new_node;
1124 }
1125
1126 /* Return true if A and B represents semantically equivalent symbols. */
1127
1128 bool
1129 symtab_semantically_equivalent_p (symtab_node *a,
1130 symtab_node *b)
1131 {
1132 enum availability avail;
1133 symtab_node *ba;
1134 symtab_node *bb;
1135
1136 /* Equivalent functions are equivalent. */
1137 if (a->decl == b->decl)
1138 return true;
1139
1140 /* If symbol is not overwritable by different implementation,
1141 walk to the base object it defines. */
1142 ba = symtab_alias_ultimate_target (a, &avail);
1143 if (avail >= AVAIL_AVAILABLE)
1144 {
1145 if (ba == b)
1146 return true;
1147 }
1148 else
1149 ba = a;
1150 bb = symtab_alias_ultimate_target (b, &avail);
1151 if (avail >= AVAIL_AVAILABLE)
1152 {
1153 if (a == bb)
1154 return true;
1155 }
1156 else
1157 bb = b;
1158 return bb == ba;
1159 }
1160 #include "gt-symtab.h"