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