symtab.c (symtab_node::reset_section): New method.
[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
46 const char * const ld_plugin_symbol_resolution_names[]=
47 {
48 "",
49 "undef",
50 "prevailing_def",
51 "prevailing_def_ironly",
52 "preempted_reg",
53 "preempted_ir",
54 "resolved_ir",
55 "resolved_exec",
56 "resolved_dyn",
57 "prevailing_def_ironly_exp"
58 };
59
60
61 /* Hash table used to hold sectoons. */
62 static GTY((param_is (section_hash_entry))) htab_t section_hash;
63
64 /* Hash table used to convert assembler names into nodes. */
65 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
66
67 /* Linked list of symbol table nodes. */
68 symtab_node *symtab_nodes;
69
70 /* The order index of the next symtab node to be created. This is
71 used so that we can sort the cgraph nodes in order by when we saw
72 them, to support -fno-toplevel-reorder. */
73 int symtab_order;
74
75 /* Hash asmnames ignoring the user specified marks. */
76
77 static hashval_t
78 decl_assembler_name_hash (const_tree asmname)
79 {
80 if (IDENTIFIER_POINTER (asmname)[0] == '*')
81 {
82 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
83 size_t ulp_len = strlen (user_label_prefix);
84
85 if (ulp_len == 0)
86 ;
87 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
88 decl_str += ulp_len;
89
90 return htab_hash_string (decl_str);
91 }
92
93 return htab_hash_string (IDENTIFIER_POINTER (asmname));
94 }
95
96
97 /* Returns a hash code for P. */
98
99 static hashval_t
100 hash_node_by_assembler_name (const void *p)
101 {
102 const symtab_node *n = (const symtab_node *) p;
103 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
104 }
105
106 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
107
108 static bool
109 decl_assembler_name_equal (tree decl, const_tree asmname)
110 {
111 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
112 const char *decl_str;
113 const char *asmname_str;
114 bool test = false;
115
116 if (decl_asmname == asmname)
117 return true;
118
119 decl_str = IDENTIFIER_POINTER (decl_asmname);
120 asmname_str = IDENTIFIER_POINTER (asmname);
121
122
123 /* If the target assembler name was set by the user, things are trickier.
124 We have a leading '*' to begin with. After that, it's arguable what
125 is the correct thing to do with -fleading-underscore. Arguably, we've
126 historically been doing the wrong thing in assemble_alias by always
127 printing the leading underscore. Since we're not changing that, make
128 sure user_label_prefix follows the '*' before matching. */
129 if (decl_str[0] == '*')
130 {
131 size_t ulp_len = strlen (user_label_prefix);
132
133 decl_str ++;
134
135 if (ulp_len == 0)
136 test = true;
137 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
138 decl_str += ulp_len, test=true;
139 else
140 decl_str --;
141 }
142 if (asmname_str[0] == '*')
143 {
144 size_t ulp_len = strlen (user_label_prefix);
145
146 asmname_str ++;
147
148 if (ulp_len == 0)
149 test = true;
150 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
151 asmname_str += ulp_len, test=true;
152 else
153 asmname_str --;
154 }
155
156 if (!test)
157 return false;
158 return strcmp (decl_str, asmname_str) == 0;
159 }
160
161
162 /* Returns nonzero if P1 and P2 are equal. */
163
164 static int
165 eq_assembler_name (const void *p1, const void *p2)
166 {
167 const symtab_node *n1 = (const symtab_node *) p1;
168 const_tree name = (const_tree)p2;
169 return (decl_assembler_name_equal (n1->decl, name));
170 }
171
172 /* Insert NODE to assembler name hash. */
173
174 static void
175 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
176 {
177 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
178 return;
179 gcc_checking_assert (!node->previous_sharing_asm_name
180 && !node->next_sharing_asm_name);
181 if (assembler_name_hash)
182 {
183 void **aslot;
184 struct cgraph_node *cnode;
185 tree decl = node->decl;
186
187 tree name = DECL_ASSEMBLER_NAME (node->decl);
188
189 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
190 decl_assembler_name_hash (name),
191 INSERT);
192 gcc_assert (*aslot != node);
193 node->next_sharing_asm_name = (symtab_node *)*aslot;
194 if (*aslot != NULL)
195 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
196 *aslot = node;
197
198 /* Update also possible inline clones sharing a decl. */
199 cnode = dyn_cast <cgraph_node *> (node);
200 if (cnode && cnode->clones && with_clones)
201 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
202 if (cnode->decl == decl)
203 insert_to_assembler_name_hash (cnode, true);
204 }
205
206 }
207
208 /* Remove NODE from assembler name hash. */
209
210 static void
211 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
212 {
213 if (assembler_name_hash)
214 {
215 struct cgraph_node *cnode;
216 tree decl = node->decl;
217
218 if (node->next_sharing_asm_name)
219 node->next_sharing_asm_name->previous_sharing_asm_name
220 = node->previous_sharing_asm_name;
221 if (node->previous_sharing_asm_name)
222 {
223 node->previous_sharing_asm_name->next_sharing_asm_name
224 = node->next_sharing_asm_name;
225 }
226 else
227 {
228 tree name = DECL_ASSEMBLER_NAME (node->decl);
229 void **slot;
230 slot = htab_find_slot_with_hash (assembler_name_hash, name,
231 decl_assembler_name_hash (name),
232 NO_INSERT);
233 gcc_assert (*slot == node);
234 if (!node->next_sharing_asm_name)
235 htab_clear_slot (assembler_name_hash, slot);
236 else
237 *slot = node->next_sharing_asm_name;
238 }
239 node->next_sharing_asm_name = NULL;
240 node->previous_sharing_asm_name = NULL;
241
242 /* Update also possible inline clones sharing a decl. */
243 cnode = dyn_cast <cgraph_node *> (node);
244 if (cnode && cnode->clones && with_clones)
245 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
246 if (cnode->decl == decl)
247 unlink_from_assembler_name_hash (cnode, true);
248 }
249 }
250
251 /* Arrange node to be first in its entry of assembler_name_hash. */
252
253 void
254 symtab_prevail_in_asm_name_hash (symtab_node *node)
255 {
256 unlink_from_assembler_name_hash (node, false);
257 insert_to_assembler_name_hash (node, false);
258 }
259
260
261 /* Add node into symbol table. This function is not used directly, but via
262 cgraph/varpool node creation routines. */
263
264 void
265 symtab_register_node (symtab_node *node)
266 {
267 node->next = symtab_nodes;
268 node->previous = NULL;
269 if (symtab_nodes)
270 symtab_nodes->previous = node;
271 symtab_nodes = node;
272
273 if (!node->decl->decl_with_vis.symtab_node)
274 node->decl->decl_with_vis.symtab_node = node;
275
276 ipa_empty_ref_list (&node->ref_list);
277
278 node->order = symtab_order++;
279
280 /* Be sure to do this last; C++ FE might create new nodes via
281 DECL_ASSEMBLER_NAME langhook! */
282 insert_to_assembler_name_hash (node, false);
283 }
284
285 /* Remove NODE from same comdat group. */
286
287 void
288 symtab_remove_from_same_comdat_group (symtab_node *node)
289 {
290 if (node->same_comdat_group)
291 {
292 symtab_node *prev;
293 for (prev = node->same_comdat_group;
294 prev->same_comdat_group != node;
295 prev = prev->same_comdat_group)
296 ;
297 if (node->same_comdat_group == prev)
298 prev->same_comdat_group = NULL;
299 else
300 prev->same_comdat_group = node->same_comdat_group;
301 node->same_comdat_group = NULL;
302 }
303 }
304
305 /* Remove node from symbol table. This function is not used directly, but via
306 cgraph/varpool node removal routines. */
307
308 void
309 symtab_unregister_node (symtab_node *node)
310 {
311 ipa_remove_all_references (&node->ref_list);
312 ipa_remove_all_referring (&node->ref_list);
313
314 /* Remove reference to section. */
315 node->set_section_for_node (NULL);
316
317 symtab_remove_from_same_comdat_group (node);
318
319 if (node->previous)
320 node->previous->next = node->next;
321 else
322 symtab_nodes = node->next;
323 if (node->next)
324 node->next->previous = node->previous;
325 node->next = NULL;
326 node->previous = NULL;
327
328 /* During LTO symtab merging we temporarily corrupt decl to symtab node
329 hash. */
330 gcc_assert (node->decl->decl_with_vis.symtab_node || in_lto_p);
331 if (node->decl->decl_with_vis.symtab_node == node)
332 {
333 symtab_node *replacement_node = NULL;
334 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
335 replacement_node = cgraph_find_replacement_node (cnode);
336 node->decl->decl_with_vis.symtab_node = replacement_node;
337 }
338 if (!is_a <varpool_node *> (node) || !DECL_HARD_REGISTER (node->decl))
339 unlink_from_assembler_name_hash (node, false);
340 }
341
342
343 /* Remove symtab NODE from the symbol table. */
344
345 void
346 symtab_remove_node (symtab_node *node)
347 {
348 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
349 cgraph_remove_node (cnode);
350 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
351 varpool_remove_node (vnode);
352 }
353
354 /* Initalize asm name hash unless. */
355
356 void
357 symtab_initialize_asm_name_hash (void)
358 {
359 symtab_node *node;
360 if (!assembler_name_hash)
361 {
362 assembler_name_hash =
363 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
364 NULL);
365 FOR_EACH_SYMBOL (node)
366 insert_to_assembler_name_hash (node, false);
367 }
368 }
369
370 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
371 Return NULL if there's no such node. */
372
373 symtab_node *
374 symtab_node_for_asm (const_tree asmname)
375 {
376 symtab_node *node;
377 void **slot;
378
379 symtab_initialize_asm_name_hash ();
380 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
381 decl_assembler_name_hash (asmname),
382 NO_INSERT);
383
384 if (slot)
385 {
386 node = (symtab_node *) *slot;
387 return node;
388 }
389 return NULL;
390 }
391
392 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
393
394 void
395 change_decl_assembler_name (tree decl, tree name)
396 {
397 symtab_node *node = NULL;
398
399 /* We can have user ASM names on things, like global register variables, that
400 are not in the symbol table. */
401 if ((TREE_CODE (decl) == VAR_DECL
402 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
403 || TREE_CODE (decl) == FUNCTION_DECL)
404 node = symtab_get_node (decl);
405 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
406 {
407 SET_DECL_ASSEMBLER_NAME (decl, name);
408 if (node)
409 insert_to_assembler_name_hash (node, true);
410 }
411 else
412 {
413 if (name == DECL_ASSEMBLER_NAME (decl))
414 return;
415
416 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
417 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
418 : NULL);
419 if (node)
420 unlink_from_assembler_name_hash (node, true);
421 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
422 && DECL_RTL_SET_P (decl))
423 warning (0, "%D renamed after being referenced in assembly", decl);
424
425 SET_DECL_ASSEMBLER_NAME (decl, name);
426 if (alias)
427 {
428 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
429 TREE_CHAIN (name) = alias;
430 }
431 if (node)
432 insert_to_assembler_name_hash (node, true);
433 }
434 }
435
436 /* Add NEW_ to the same comdat group that OLD is in. */
437
438 void
439 symtab_add_to_same_comdat_group (symtab_node *new_node,
440 symtab_node *old_node)
441 {
442 gcc_assert (old_node->get_comdat_group ());
443 gcc_assert (!new_node->same_comdat_group);
444 gcc_assert (new_node != old_node);
445
446 new_node->set_comdat_group (old_node->get_comdat_group ());
447 new_node->same_comdat_group = old_node;
448 if (!old_node->same_comdat_group)
449 old_node->same_comdat_group = new_node;
450 else
451 {
452 symtab_node *n;
453 for (n = old_node->same_comdat_group;
454 n->same_comdat_group != old_node;
455 n = n->same_comdat_group)
456 ;
457 n->same_comdat_group = new_node;
458 }
459 }
460
461 /* Dissolve the same_comdat_group list in which NODE resides. */
462
463 void
464 symtab_dissolve_same_comdat_group_list (symtab_node *node)
465 {
466 symtab_node *n = node;
467 symtab_node *next;
468
469 if (!node->same_comdat_group)
470 return;
471 do
472 {
473 next = n->same_comdat_group;
474 n->same_comdat_group = NULL;
475 /* Clear comdat_group for comdat locals, since
476 make_decl_local doesn't. */
477 if (!TREE_PUBLIC (n->decl))
478 n->set_comdat_group (NULL);
479 n = next;
480 }
481 while (n != node);
482 }
483
484 /* Return printable assembler name of NODE.
485 This function is used only for debugging. When assembler name
486 is unknown go with identifier name. */
487
488 const char *
489 symtab_node::asm_name () const
490 {
491 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
492 return lang_hooks.decl_printable_name (decl, 2);
493 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
494 }
495
496 /* Return printable identifier name. */
497
498 const char *
499 symtab_node::name () const
500 {
501 return lang_hooks.decl_printable_name (decl, 2);
502 }
503
504 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
505
506 /* Dump base fields of symtab nodes. Not to be used directly. */
507
508 void
509 dump_symtab_base (FILE *f, symtab_node *node)
510 {
511 static const char * const visibility_types[] = {
512 "default", "protected", "hidden", "internal"
513 };
514
515 fprintf (f, "%s/%i (%s)",
516 node->asm_name (),
517 node->order,
518 node->name ());
519 dump_addr (f, " @", (void *)node);
520 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
521
522 if (node->definition)
523 fprintf (f, " definition");
524 if (node->analyzed)
525 fprintf (f, " analyzed");
526 if (node->alias)
527 fprintf (f, " alias");
528 if (node->weakref)
529 fprintf (f, " weakref");
530 if (node->cpp_implicit_alias)
531 fprintf (f, " cpp_implicit_alias");
532 if (node->alias_target)
533 fprintf (f, " target:%s",
534 DECL_P (node->alias_target)
535 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
536 (node->alias_target))
537 : IDENTIFIER_POINTER (node->alias_target));
538 if (node->body_removed)
539 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
540 fprintf (f, "\n Visibility:");
541 if (node->in_other_partition)
542 fprintf (f, " in_other_partition");
543 if (node->used_from_other_partition)
544 fprintf (f, " used_from_other_partition");
545 if (node->force_output)
546 fprintf (f, " force_output");
547 if (node->forced_by_abi)
548 fprintf (f, " forced_by_abi");
549 if (node->externally_visible)
550 fprintf (f, " externally_visible");
551 if (node->resolution != LDPR_UNKNOWN)
552 fprintf (f, " %s",
553 ld_plugin_symbol_resolution_names[(int)node->resolution]);
554 if (TREE_ASM_WRITTEN (node->decl))
555 fprintf (f, " asm_written");
556 if (DECL_EXTERNAL (node->decl))
557 fprintf (f, " external");
558 if (TREE_PUBLIC (node->decl))
559 fprintf (f, " public");
560 if (DECL_COMMON (node->decl))
561 fprintf (f, " common");
562 if (DECL_WEAK (node->decl))
563 fprintf (f, " weak");
564 if (DECL_DLLIMPORT_P (node->decl))
565 fprintf (f, " dll_import");
566 if (DECL_COMDAT (node->decl))
567 fprintf (f, " comdat");
568 if (node->get_comdat_group ())
569 fprintf (f, " comdat_group:%s",
570 IDENTIFIER_POINTER (node->get_comdat_group_id ()));
571 if (DECL_ONE_ONLY (node->decl))
572 fprintf (f, " one_only");
573 if (node->get_section ())
574 fprintf (f, " section:%s",
575 node->get_section ());
576 if (node->implicit_section)
577 fprintf (f," (implicit_section)");
578 if (DECL_VISIBILITY_SPECIFIED (node->decl))
579 fprintf (f, " visibility_specified");
580 if (DECL_VISIBILITY (node->decl))
581 fprintf (f, " visibility:%s",
582 visibility_types [DECL_VISIBILITY (node->decl)]);
583 if (DECL_VIRTUAL_P (node->decl))
584 fprintf (f, " virtual");
585 if (DECL_ARTIFICIAL (node->decl))
586 fprintf (f, " artificial");
587 if (TREE_CODE (node->decl) == FUNCTION_DECL)
588 {
589 if (DECL_STATIC_CONSTRUCTOR (node->decl))
590 fprintf (f, " constructor");
591 if (DECL_STATIC_DESTRUCTOR (node->decl))
592 fprintf (f, " destructor");
593 }
594 fprintf (f, "\n");
595
596 if (node->same_comdat_group)
597 fprintf (f, " Same comdat group as: %s/%i\n",
598 node->same_comdat_group->asm_name (),
599 node->same_comdat_group->order);
600 if (node->next_sharing_asm_name)
601 fprintf (f, " next sharing asm name: %i\n",
602 node->next_sharing_asm_name->order);
603 if (node->previous_sharing_asm_name)
604 fprintf (f, " previous sharing asm name: %i\n",
605 node->previous_sharing_asm_name->order);
606
607 if (node->address_taken)
608 fprintf (f, " Address is taken.\n");
609 if (node->aux)
610 {
611 fprintf (f, " Aux:");
612 dump_addr (f, " @", (void *)node->aux);
613 }
614
615 fprintf (f, " References: ");
616 ipa_dump_references (f, &node->ref_list);
617 fprintf (f, " Referring: ");
618 ipa_dump_referring (f, &node->ref_list);
619 if (node->lto_file_data)
620 fprintf (f, " Read from file: %s\n",
621 node->lto_file_data->file_name);
622 }
623
624 /* Dump symtab node. */
625
626 void
627 dump_symtab_node (FILE *f, symtab_node *node)
628 {
629 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
630 dump_cgraph_node (f, cnode);
631 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
632 dump_varpool_node (f, vnode);
633 }
634
635 /* Dump symbol table. */
636
637 void
638 dump_symtab (FILE *f)
639 {
640 symtab_node *node;
641 fprintf (f, "Symbol table:\n\n");
642 FOR_EACH_SYMBOL (node)
643 dump_symtab_node (f, node);
644 }
645
646 /* Dump symtab node NODE to stderr. */
647
648 DEBUG_FUNCTION void
649 debug_symtab_node (symtab_node *node)
650 {
651 dump_symtab_node (stderr, node);
652 }
653
654 /* Dump symbol table to stderr. */
655
656 DEBUG_FUNCTION void
657 debug_symtab (void)
658 {
659 dump_symtab (stderr);
660 }
661
662 /* Verify common part of symtab nodes. */
663
664 DEBUG_FUNCTION bool
665 verify_symtab_base (symtab_node *node)
666 {
667 bool error_found = false;
668 symtab_node *hashed_node;
669
670 if (is_a <cgraph_node *> (node))
671 {
672 if (TREE_CODE (node->decl) != FUNCTION_DECL)
673 {
674 error ("function symbol is not function");
675 error_found = true;
676 }
677 }
678 else if (is_a <varpool_node *> (node))
679 {
680 if (TREE_CODE (node->decl) != VAR_DECL)
681 {
682 error ("variable symbol is not variable");
683 error_found = true;
684 }
685 }
686 else
687 {
688 error ("node has unknown type");
689 error_found = true;
690 }
691
692 if (cgraph_state != CGRAPH_LTO_STREAMING)
693 {
694 hashed_node = symtab_get_node (node->decl);
695 if (!hashed_node)
696 {
697 error ("node not found node->decl->decl_with_vis.symtab_node");
698 error_found = true;
699 }
700 if (hashed_node != node
701 && (!is_a <cgraph_node *> (node)
702 || !dyn_cast <cgraph_node *> (node)->clone_of
703 || dyn_cast <cgraph_node *> (node)->clone_of->decl
704 != node->decl))
705 {
706 error ("node differs from node->decl->decl_with_vis.symtab_node");
707 error_found = true;
708 }
709 }
710 if (assembler_name_hash)
711 {
712 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
713 if (hashed_node && hashed_node->previous_sharing_asm_name)
714 {
715 error ("assembler name hash list corrupted");
716 error_found = true;
717 }
718 while (hashed_node)
719 {
720 if (hashed_node == node)
721 break;
722 hashed_node = hashed_node->next_sharing_asm_name;
723 }
724 if (!hashed_node
725 && !(is_a <varpool_node *> (node)
726 || DECL_HARD_REGISTER (node->decl)))
727 {
728 error ("node not found in symtab assembler name hash");
729 error_found = true;
730 }
731 }
732 if (node->previous_sharing_asm_name
733 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
734 {
735 error ("double linked list of assembler names corrupted");
736 error_found = true;
737 }
738 if (node->analyzed && !node->definition)
739 {
740 error ("node is analyzed byt it is not a definition");
741 error_found = true;
742 }
743 if (node->cpp_implicit_alias && !node->alias)
744 {
745 error ("node is alias but not implicit alias");
746 error_found = true;
747 }
748 if (node->alias && !node->definition
749 && !node->weakref)
750 {
751 error ("node is alias but not definition");
752 error_found = true;
753 }
754 if (node->weakref && !node->alias)
755 {
756 error ("node is weakref but not an alias");
757 error_found = true;
758 }
759 if (node->same_comdat_group)
760 {
761 symtab_node *n = node->same_comdat_group;
762
763 if (!n->get_comdat_group ())
764 {
765 error ("node is in same_comdat_group list but has no comdat_group");
766 error_found = true;
767 }
768 if (n->get_comdat_group () != node->get_comdat_group ())
769 {
770 error ("same_comdat_group list across different groups");
771 error_found = true;
772 }
773 if (!n->definition)
774 {
775 error ("Node has same_comdat_group but it is not a definition");
776 error_found = true;
777 }
778 if (n->type != node->type)
779 {
780 error ("mixing different types of symbol in same comdat groups is not supported");
781 error_found = true;
782 }
783 if (n == node)
784 {
785 error ("node is alone in a comdat group");
786 error_found = true;
787 }
788 do
789 {
790 if (!n->same_comdat_group)
791 {
792 error ("same_comdat_group is not a circular list");
793 error_found = true;
794 break;
795 }
796 n = n->same_comdat_group;
797 }
798 while (n != node);
799 if (symtab_comdat_local_p (node))
800 {
801 struct ipa_ref_list *refs = &node->ref_list;
802 struct ipa_ref *ref;
803
804 for (int i = 0; ipa_ref_list_referring_iterate (refs, i, ref); ++i)
805 {
806 if (!symtab_in_same_comdat_p (ref->referring, node))
807 {
808 error ("comdat-local symbol referred to by %s outside its "
809 "comdat",
810 identifier_to_locale (ref->referring->name()));
811 error_found = true;
812 }
813 }
814 }
815 }
816 if (node->implicit_section && !node->get_section ())
817 {
818 error ("implicit_section flag is set but section isn't");
819 error_found = true;
820 }
821 if (node->get_section () && node->get_comdat_group ()
822 && !node->implicit_section)
823 {
824 error ("Both section and comdat group is set");
825 error_found = true;
826 }
827 /* TODO: Add string table for sections, so we do not keep holding duplicated
828 strings. */
829 if (node->alias && node->definition
830 && node->get_section () != symtab_alias_target (node)->get_section ()
831 && (!node->get_section()
832 || !symtab_alias_target (node)->get_section ()
833 || strcmp (node->get_section(),
834 symtab_alias_target (node)->get_section ())))
835 {
836 error ("Alias and target's section differs");
837 dump_symtab_node (stderr, symtab_alias_target (node));
838 error_found = true;
839 }
840 if (node->alias && node->definition
841 && node->get_comdat_group () != symtab_alias_target (node)->get_comdat_group ())
842 {
843 error ("Alias and target's comdat groups differs");
844 dump_symtab_node (stderr, symtab_alias_target (node));
845 error_found = true;
846 }
847
848 return error_found;
849 }
850
851 /* Verify consistency of NODE. */
852
853 DEBUG_FUNCTION void
854 verify_symtab_node (symtab_node *node)
855 {
856 if (seen_error ())
857 return;
858
859 timevar_push (TV_CGRAPH_VERIFY);
860 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
861 verify_cgraph_node (cnode);
862 else
863 if (verify_symtab_base (node))
864 {
865 dump_symtab_node (stderr, node);
866 internal_error ("verify_symtab_node failed");
867 }
868 timevar_pop (TV_CGRAPH_VERIFY);
869 }
870
871 /* Verify symbol table for internal consistency. */
872
873 DEBUG_FUNCTION void
874 verify_symtab (void)
875 {
876 symtab_node *node;
877 pointer_map<symtab_node *> comdat_head_map;
878
879 FOR_EACH_SYMBOL (node)
880 {
881 verify_symtab_node (node);
882 if (node->get_comdat_group ())
883 {
884 symtab_node **entry, *s;
885 bool existed;
886
887 entry = comdat_head_map.insert (node->get_comdat_group (), &existed);
888 if (!existed)
889 *entry = node;
890 else
891 for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
892 if (!s || s == *entry)
893 {
894 error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
895 dump_symtab_node (stderr, *entry);
896 dump_symtab_node (stderr, s);
897 internal_error ("verify_symtab failed");
898 }
899 }
900 }
901 }
902
903 /* Return true when RESOLUTION indicate that linker will use
904 the symbol from non-LTO object files. */
905
906 bool
907 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
908 {
909 return (resolution == LDPR_PREVAILING_DEF
910 || resolution == LDPR_PREEMPTED_REG
911 || resolution == LDPR_RESOLVED_EXEC
912 || resolution == LDPR_RESOLVED_DYN);
913 }
914
915 /* Return true when NODE is known to be used from other (non-LTO) object file.
916 Known only when doing LTO via linker plugin. */
917
918 bool
919 symtab_used_from_object_file_p (symtab_node *node)
920 {
921 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
922 return false;
923 if (resolution_used_from_other_file_p (node->resolution))
924 return true;
925 return false;
926 }
927
928 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
929 but other code such as notice_global_symbol generates rtl. */
930
931 void
932 symtab_make_decl_local (tree decl)
933 {
934 rtx rtl, symbol;
935
936 /* Avoid clearing comdat_groups on comdat-local decls. */
937 if (TREE_PUBLIC (decl) == 0)
938 return;
939
940 if (TREE_CODE (decl) == VAR_DECL)
941 DECL_COMMON (decl) = 0;
942 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
943
944 DECL_COMDAT (decl) = 0;
945 DECL_WEAK (decl) = 0;
946 DECL_EXTERNAL (decl) = 0;
947 DECL_VISIBILITY_SPECIFIED (decl) = 0;
948 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
949 TREE_PUBLIC (decl) = 0;
950 if (!DECL_RTL_SET_P (decl))
951 return;
952
953 /* Update rtl flags. */
954 make_decl_rtl (decl);
955
956 rtl = DECL_RTL (decl);
957 if (!MEM_P (rtl))
958 return;
959
960 symbol = XEXP (rtl, 0);
961 if (GET_CODE (symbol) != SYMBOL_REF)
962 return;
963
964 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
965 }
966
967 /* Return availability of NODE. */
968
969 enum availability
970 symtab_node_availability (symtab_node *node)
971 {
972 if (is_a <cgraph_node *> (node))
973 return cgraph_function_body_availability (cgraph (node));
974 else
975 return cgraph_variable_initializer_availability (varpool (node));
976 }
977
978 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
979 If NODE is not an alias, return NODE.
980 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
981
982 symtab_node *
983 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
984 {
985 bool weakref_p = false;
986
987 if (!node->alias)
988 {
989 if (availability)
990 *availability = symtab_node_availability (node);
991 return node;
992 }
993
994 /* To determine visibility of the target, we follow ELF semantic of aliases.
995 Here alias is an alternative assembler name of a given definition. Its
996 availability prevails the availability of its target (i.e. static alias of
997 weak definition is available.
998
999 Weakref is a different animal (and not part of ELF per se). It is just
1000 alternative name of a given symbol used within one complation unit
1001 and is translated prior hitting the object file. It inherits the
1002 visibility of its target (i.e. weakref of non-overwritable definition
1003 is non-overwritable, while weakref of weak definition is weak).
1004
1005 If we ever get into supporting targets with different semantics, a target
1006 hook will be needed here. */
1007
1008 if (availability)
1009 {
1010 weakref_p = node->weakref;
1011 if (!weakref_p)
1012 *availability = symtab_node_availability (node);
1013 else
1014 *availability = AVAIL_LOCAL;
1015 }
1016 while (node)
1017 {
1018 if (node->alias && node->analyzed)
1019 node = symtab_alias_target (node);
1020 else
1021 {
1022 if (!availability)
1023 ;
1024 else if (node->analyzed)
1025 {
1026 if (weakref_p)
1027 {
1028 enum availability a = symtab_node_availability (node);
1029 if (a < *availability)
1030 *availability = a;
1031 }
1032 }
1033 else
1034 *availability = AVAIL_NOT_AVAILABLE;
1035 return node;
1036 }
1037 if (node && availability && weakref_p)
1038 {
1039 enum availability a = symtab_node_availability (node);
1040 if (a < *availability)
1041 *availability = a;
1042 weakref_p = node->weakref;
1043 }
1044 }
1045 if (availability)
1046 *availability = AVAIL_NOT_AVAILABLE;
1047 return NULL;
1048 }
1049
1050 /* C++ FE sometimes change linkage flags after producing same body aliases.
1051
1052 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1053 are obviously equivalent. The way it is doing so is however somewhat
1054 kludgy and interferes with the visibility code. As a result we need to
1055 copy the visibility from the target to get things right. */
1056
1057 void
1058 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1059 {
1060 if (is_a <cgraph_node *> (node))
1061 {
1062 DECL_DECLARED_INLINE_P (node->decl)
1063 = DECL_DECLARED_INLINE_P (target->decl);
1064 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1065 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1066 }
1067 /* FIXME: It is not really clear why those flags should not be copied for
1068 functions, too. */
1069 else
1070 {
1071 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1072 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1073 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1074 }
1075 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1076 if (TREE_PUBLIC (node->decl))
1077 {
1078 tree group;
1079
1080 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1081 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1082 group = target->get_comdat_group ();
1083 node->set_comdat_group (group);
1084 if (group
1085 && !node->same_comdat_group)
1086 symtab_add_to_same_comdat_group (node, target);
1087 }
1088 node->externally_visible = target->externally_visible;
1089 }
1090
1091 /* Hash sections by their names. */
1092
1093 static hashval_t
1094 hash_section_hash_entry (const void *p)
1095 {
1096 const section_hash_entry *n = (const section_hash_entry *) p;
1097 return htab_hash_string (n->name);
1098 }
1099
1100 /* Return true if section P1 name equals to P2. */
1101
1102 static int
1103 eq_sections (const void *p1, const void *p2)
1104 {
1105 const section_hash_entry *n1 = (const section_hash_entry *) p1;
1106 const char *name = (const char *)p2;
1107 return n1->name == name || !strcmp (n1->name, name);
1108 }
1109
1110 /* Set section, do not recurse into aliases.
1111 When one wants to change section of symbol and its aliases,
1112 use set_section */
1113
1114 void
1115 symtab_node::set_section_for_node (const char *section)
1116 {
1117 const char *current = get_section ();
1118 void **slot;
1119
1120 if (current == section
1121 || (current && section
1122 && !strcmp (current, section)))
1123 return;
1124
1125 if (current)
1126 {
1127 x_section->ref_count--;
1128 if (!x_section->ref_count)
1129 {
1130 slot = htab_find_slot_with_hash (section_hash, x_section->name,
1131 htab_hash_string (x_section->name),
1132 INSERT);
1133 ggc_free (x_section);
1134 htab_clear_slot (section_hash, slot);
1135 }
1136 x_section = NULL;
1137 }
1138 if (!section)
1139 {
1140 implicit_section = false;
1141 return;
1142 }
1143 if (!section_hash)
1144 section_hash = htab_create_ggc (10, hash_section_hash_entry,
1145 eq_sections, NULL);
1146 slot = htab_find_slot_with_hash (section_hash, section,
1147 htab_hash_string (section),
1148 INSERT);
1149 if (*slot)
1150 x_section = (section_hash_entry *)*slot;
1151 else
1152 {
1153 int len = strlen (section);
1154 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1155 x_section->name = ggc_vec_alloc<char> (len + 1);
1156 memcpy (x_section->name, section, len + 1);
1157 }
1158 x_section->ref_count++;
1159 }
1160
1161 /* Worker for set_section. */
1162
1163 static bool
1164 set_section_1 (struct symtab_node *n, void *s)
1165 {
1166 n->set_section_for_node ((char *)s);
1167 return false;
1168 }
1169
1170 /* Set section of symbol and its aliases. */
1171
1172 void
1173 symtab_node::set_section (const char *section)
1174 {
1175 gcc_assert (!this->alias);
1176 symtab_for_node_and_aliases (this, set_section_1, const_cast<char *>(section), true);
1177 }
1178
1179 /* Reset section of NODE. That is when NODE is being brought local
1180 we may want to clear section produced for comdat group and depending
1181 on function-sections produce now, local, unique section for it. */
1182
1183 void
1184 symtab_node::reset_section ()
1185 {
1186 if (!this->implicit_section)
1187 return;
1188 this->set_section (NULL);
1189 resolve_unique_section (this->decl, 0,
1190 is_a <cgraph_node *> (this)
1191 ? flag_function_sections : flag_data_sections);
1192 }
1193
1194 /* Worker for symtab_resolve_alias. */
1195
1196 static bool
1197 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
1198 {
1199 n->implicit_section = true;
1200 return false;
1201 }
1202
1203 /* Add reference recording that NODE is alias of TARGET.
1204 The function can fail in the case of aliasing cycles; in this case
1205 it returns false. */
1206
1207 bool
1208 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1209 {
1210 symtab_node *n;
1211
1212 gcc_assert (!node->analyzed
1213 && !vec_safe_length (node->ref_list.references));
1214
1215 /* Never let cycles to creep into the symbol table alias references;
1216 those will make alias walkers to be infinite. */
1217 for (n = target; n && n->alias;
1218 n = n->analyzed ? symtab_alias_target (n) : NULL)
1219 if (n == node)
1220 {
1221 if (is_a <cgraph_node *> (node))
1222 error ("function %q+D part of alias cycle", node->decl);
1223 else if (is_a <varpool_node *> (node))
1224 error ("variable %q+D part of alias cycle", node->decl);
1225 else
1226 gcc_unreachable ();
1227 node->alias = false;
1228 return false;
1229 }
1230
1231 /* "analyze" the node - i.e. mark the reference. */
1232 node->definition = true;
1233 node->alias = true;
1234 node->analyzed = true;
1235 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1236
1237 /* Add alias into the comdat group of its target unless it is already there. */
1238 if (node->same_comdat_group)
1239 symtab_remove_from_same_comdat_group (node);
1240 node->set_comdat_group (NULL);
1241 if (target->get_comdat_group ())
1242 symtab_add_to_same_comdat_group (node, target);
1243
1244 if ((node->get_section () != target->get_section ()
1245 || target->get_comdat_group ())
1246 && node->get_section () && !node->implicit_section)
1247 {
1248 error ("section of alias %q+D must match section of its target",
1249 node->decl);
1250 }
1251 symtab_for_node_and_aliases (node, set_section_1,
1252 const_cast<char *>(target->get_section ()), true);
1253 if (target->implicit_section)
1254 symtab_for_node_and_aliases (node,
1255 set_implicit_section, NULL, true);
1256
1257 /* Alias targets become redundant after alias is resolved into an reference.
1258 We do not want to keep it around or we would have to mind updating them
1259 when renaming symbols. */
1260 node->alias_target = NULL;
1261
1262 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1263 fixup_same_cpp_alias_visibility (node, target);
1264
1265 /* If alias has address taken, so does the target. */
1266 if (node->address_taken)
1267 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1268 return true;
1269 }
1270
1271 /* Call calback on NODE and aliases associated to NODE.
1272 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1273 skipped. */
1274
1275 bool
1276 symtab_for_node_and_aliases (symtab_node *node,
1277 bool (*callback) (symtab_node *, void *),
1278 void *data,
1279 bool include_overwritable)
1280 {
1281 int i;
1282 struct ipa_ref *ref;
1283
1284 if (callback (node, data))
1285 return true;
1286 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1287 if (ref->use == IPA_REF_ALIAS)
1288 {
1289 symtab_node *alias = ref->referring;
1290 if (include_overwritable
1291 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1292 if (symtab_for_node_and_aliases (alias, callback, data,
1293 include_overwritable))
1294 return true;
1295 }
1296 return false;
1297 }
1298
1299 /* Worker searching nonoverwritable alias. */
1300
1301 static bool
1302 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1303 {
1304 if (decl_binds_to_current_def_p (node->decl))
1305 {
1306 *(symtab_node **)data = node;
1307 return true;
1308 }
1309 return false;
1310 }
1311
1312 /* If NODE can not be overwriten by static or dynamic linker to point to different
1313 definition, return NODE. Otherwise look for alias with such property and if
1314 none exists, introduce new one. */
1315
1316 symtab_node *
1317 symtab_nonoverwritable_alias (symtab_node *node)
1318 {
1319 tree new_decl;
1320 symtab_node *new_node = NULL;
1321
1322 /* First try to look up existing alias or base object
1323 (if that is already non-overwritable). */
1324 node = symtab_alias_ultimate_target (node, NULL);
1325 gcc_assert (!node->alias && !node->weakref);
1326 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1327 (void *)&new_node, true);
1328 if (new_node)
1329 return new_node;
1330 #ifndef ASM_OUTPUT_DEF
1331 /* If aliases aren't supported by the assembler, fail. */
1332 return NULL;
1333 #endif
1334
1335 /* Otherwise create a new one. */
1336 new_decl = copy_node (node->decl);
1337 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1338 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1339 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1340 DECL_INITIAL (new_decl) = NULL;
1341 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1342 SET_DECL_RTL (new_decl, NULL);
1343
1344 /* Update the properties. */
1345 DECL_EXTERNAL (new_decl) = 0;
1346 TREE_PUBLIC (new_decl) = 0;
1347 DECL_COMDAT (new_decl) = 0;
1348 DECL_WEAK (new_decl) = 0;
1349
1350 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1351 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1352 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1353 {
1354 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1355 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1356 new_node = cgraph_create_function_alias
1357 (new_decl, node->decl);
1358 }
1359 else
1360 {
1361 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1362 DECL_INITIAL (new_decl) = error_mark_node;
1363 new_node = varpool_create_variable_alias (new_decl, node->decl);
1364 }
1365 symtab_resolve_alias (new_node, node);
1366 gcc_assert (decl_binds_to_current_def_p (new_decl)
1367 && targetm.binds_local_p (new_decl));
1368 return new_node;
1369 }
1370
1371 /* Return true if A and B represents semantically equivalent symbols. */
1372
1373 bool
1374 symtab_semantically_equivalent_p (symtab_node *a,
1375 symtab_node *b)
1376 {
1377 enum availability avail;
1378 symtab_node *ba;
1379 symtab_node *bb;
1380
1381 /* Equivalent functions are equivalent. */
1382 if (a->decl == b->decl)
1383 return true;
1384
1385 /* If symbol is not overwritable by different implementation,
1386 walk to the base object it defines. */
1387 ba = symtab_alias_ultimate_target (a, &avail);
1388 if (avail >= AVAIL_AVAILABLE)
1389 {
1390 if (ba == b)
1391 return true;
1392 }
1393 else
1394 ba = a;
1395 bb = symtab_alias_ultimate_target (b, &avail);
1396 if (avail >= AVAIL_AVAILABLE)
1397 {
1398 if (a == bb)
1399 return true;
1400 }
1401 else
1402 bb = b;
1403 return bb == ba;
1404 }
1405
1406 /* Classify symbol NODE for partitioning. */
1407
1408 enum symbol_partitioning_class
1409 symtab_get_symbol_partitioning_class (symtab_node *node)
1410 {
1411 /* Inline clones are always duplicated.
1412 This include external delcarations. */
1413 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1414
1415 if (DECL_ABSTRACT (node->decl))
1416 return SYMBOL_EXTERNAL;
1417
1418 if (cnode && cnode->global.inlined_to)
1419 return SYMBOL_DUPLICATE;
1420
1421 /* Weakref aliases are always duplicated. */
1422 if (node->weakref)
1423 return SYMBOL_DUPLICATE;
1424
1425 /* External declarations are external. */
1426 if (DECL_EXTERNAL (node->decl))
1427 return SYMBOL_EXTERNAL;
1428
1429 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
1430 {
1431 /* Constant pool references use local symbol names that can not
1432 be promoted global. We should never put into a constant pool
1433 objects that can not be duplicated across partitions. */
1434 if (DECL_IN_CONSTANT_POOL (node->decl))
1435 return SYMBOL_DUPLICATE;
1436 gcc_checking_assert (vnode->definition);
1437 }
1438 /* Functions that are cloned may stay in callgraph even if they are unused.
1439 Handle them as external; compute_ltrans_boundary take care to make
1440 proper things to happen (i.e. to make them appear in the boundary but
1441 with body streamed, so clone can me materialized). */
1442 else if (!cgraph (node)->definition)
1443 return SYMBOL_EXTERNAL;
1444
1445 /* Linker discardable symbols are duplicated to every use unless they are
1446 keyed. */
1447 if (DECL_ONE_ONLY (node->decl)
1448 && !node->force_output
1449 && !node->forced_by_abi
1450 && !symtab_used_from_object_file_p (node))
1451 return SYMBOL_DUPLICATE;
1452
1453 return SYMBOL_PARTITION;
1454 }
1455 #include "gt-symtab.h"