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