]>
Commit | Line | Data |
---|---|---|
2aae7680 | 1 | /* Symbol table. |
23a5b65a | 2 | Copyright (C) 2012-2014 Free Software Foundation, Inc. |
2aae7680 JH |
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" | |
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 | |
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 | }; | |
1ab24192 | 59 | |
1ab24192 | 60 | /* Hash table used to convert assembler names into nodes. */ |
5e20cdc9 | 61 | static GTY((param_is (symtab_node))) htab_t assembler_name_hash; |
2aae7680 JH |
62 | |
63 | /* Linked list of symbol table nodes. */ | |
5e20cdc9 | 64 | symtab_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. */ | |
69 | int symtab_order; | |
70 | ||
862d0b35 DN |
71 | /* Hash asmnames ignoring the user specified marks. */ |
72 | ||
73 | static hashval_t | |
74 | decl_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 | ||
95 | static hashval_t | |
96 | hash_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 | ||
104 | static bool | |
105 | decl_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 | ||
160 | static int | |
161 | eq_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 | ||
170 | static void | |
5e20cdc9 | 171 | insert_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 | ||
206 | static void | |
5e20cdc9 | 207 | unlink_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 | ||
249 | void | |
5e20cdc9 | 250 | symtab_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 | ||
260 | void | |
5e20cdc9 | 261 | symtab_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 | |
283 | void | |
7b3376a0 | 284 | symtab_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 | ||
304 | void | |
305 | symtab_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 | ||
338 | void | |
5e20cdc9 | 339 | symtab_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 |
349 | void |
350 | symtab_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 | 366 | symtab_node * |
b5493fb2 JH |
367 | symtab_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 | ||
387 | void | |
388 | change_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 | ||
431 | void | |
5e20cdc9 DM |
432 | symtab_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 | ||
456 | void | |
5e20cdc9 | 457 | symtab_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 | ||
481 | const char * | |
fec39fa6 | 482 | symtab_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 | ||
491 | const char * | |
fec39fa6 | 492 | symtab_node::name () const |
8f940ee6 | 493 | { |
fec39fa6 | 494 | return lang_hooks.decl_printable_name (decl, 2); |
8f940ee6 JH |
495 | } |
496 | ||
497 | static const char * const symtab_type_names[] = {"symbol", "function", "variable"}; | |
498 | ||
499 | /* Dump base fields of symtab nodes. Not to be used directly. */ | |
500 | ||
501 | void | |
5e20cdc9 | 502 | dump_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", |
aede2c10 | 563 | IDENTIFIER_POINTER (node->get_comdat_group ())); |
67348ccc | 564 | if (DECL_ONE_ONLY (node->decl)) |
8f940ee6 | 565 | fprintf (f, " one_only"); |
67348ccc | 566 | if (DECL_SECTION_NAME (node->decl)) |
8f940ee6 | 567 | fprintf (f, " section_name:%s", |
67348ccc DM |
568 | TREE_STRING_POINTER (DECL_SECTION_NAME (node->decl))); |
569 | if (DECL_VISIBILITY_SPECIFIED (node->decl)) | |
8f940ee6 | 570 | fprintf (f, " visibility_specified"); |
67348ccc | 571 | if (DECL_VISIBILITY (node->decl)) |
8f940ee6 | 572 | fprintf (f, " visibility:%s", |
67348ccc DM |
573 | visibility_types [DECL_VISIBILITY (node->decl)]); |
574 | if (DECL_VIRTUAL_P (node->decl)) | |
8f940ee6 | 575 | fprintf (f, " virtual"); |
67348ccc | 576 | if (DECL_ARTIFICIAL (node->decl)) |
8f940ee6 | 577 | fprintf (f, " artificial"); |
67348ccc | 578 | if (TREE_CODE (node->decl) == FUNCTION_DECL) |
838ff415 | 579 | { |
67348ccc | 580 | if (DECL_STATIC_CONSTRUCTOR (node->decl)) |
838ff415 | 581 | fprintf (f, " constructor"); |
67348ccc | 582 | if (DECL_STATIC_DESTRUCTOR (node->decl)) |
838ff415 JH |
583 | fprintf (f, " destructor"); |
584 | } | |
8f940ee6 JH |
585 | fprintf (f, "\n"); |
586 | ||
67348ccc | 587 | if (node->same_comdat_group) |
8f940ee6 | 588 | fprintf (f, " Same comdat group as: %s/%i\n", |
fec39fa6 | 589 | node->same_comdat_group->asm_name (), |
67348ccc DM |
590 | node->same_comdat_group->order); |
591 | if (node->next_sharing_asm_name) | |
8f940ee6 | 592 | fprintf (f, " next sharing asm name: %i\n", |
67348ccc DM |
593 | node->next_sharing_asm_name->order); |
594 | if (node->previous_sharing_asm_name) | |
8f940ee6 | 595 | fprintf (f, " previous sharing asm name: %i\n", |
67348ccc | 596 | node->previous_sharing_asm_name->order); |
8f940ee6 | 597 | |
67348ccc | 598 | if (node->address_taken) |
fe0bd630 | 599 | fprintf (f, " Address is taken.\n"); |
67348ccc | 600 | if (node->aux) |
66058468 JH |
601 | { |
602 | fprintf (f, " Aux:"); | |
67348ccc | 603 | dump_addr (f, " @", (void *)node->aux); |
66058468 | 604 | } |
8f940ee6 JH |
605 | |
606 | fprintf (f, " References: "); | |
67348ccc | 607 | ipa_dump_references (f, &node->ref_list); |
5932a4d4 | 608 | fprintf (f, " Referring: "); |
67348ccc DM |
609 | ipa_dump_referring (f, &node->ref_list); |
610 | if (node->lto_file_data) | |
b5493fb2 | 611 | fprintf (f, " Read from file: %s\n", |
67348ccc | 612 | node->lto_file_data->file_name); |
8f940ee6 JH |
613 | } |
614 | ||
615 | /* Dump symtab node. */ | |
616 | ||
617 | void | |
5e20cdc9 | 618 | dump_symtab_node (FILE *f, symtab_node *node) |
8f940ee6 | 619 | { |
7de90a6c | 620 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node)) |
5d59b5e1 | 621 | dump_cgraph_node (f, cnode); |
7de90a6c | 622 | else if (varpool_node *vnode = dyn_cast <varpool_node *> (node)) |
5d59b5e1 | 623 | dump_varpool_node (f, vnode); |
8f940ee6 JH |
624 | } |
625 | ||
626 | /* Dump symbol table. */ | |
627 | ||
628 | void | |
629 | dump_symtab (FILE *f) | |
630 | { | |
5e20cdc9 | 631 | symtab_node *node; |
8f940ee6 JH |
632 | fprintf (f, "Symbol table:\n\n"); |
633 | FOR_EACH_SYMBOL (node) | |
634 | dump_symtab_node (f, node); | |
635 | } | |
636 | ||
637 | /* Dump symtab node NODE to stderr. */ | |
638 | ||
639 | DEBUG_FUNCTION void | |
5e20cdc9 | 640 | debug_symtab_node (symtab_node *node) |
8f940ee6 JH |
641 | { |
642 | dump_symtab_node (stderr, node); | |
643 | } | |
644 | ||
645 | /* Dump symbol table to stderr. */ | |
646 | ||
647 | DEBUG_FUNCTION void | |
648 | debug_symtab (void) | |
649 | { | |
650 | dump_symtab (stderr); | |
651 | } | |
652 | ||
474ffc72 JH |
653 | /* Verify common part of symtab nodes. */ |
654 | ||
655 | DEBUG_FUNCTION bool | |
5e20cdc9 | 656 | verify_symtab_base (symtab_node *node) |
474ffc72 JH |
657 | { |
658 | bool error_found = false; | |
5e20cdc9 | 659 | symtab_node *hashed_node; |
474ffc72 | 660 | |
7de90a6c | 661 | if (is_a <cgraph_node *> (node)) |
474ffc72 | 662 | { |
67348ccc | 663 | if (TREE_CODE (node->decl) != FUNCTION_DECL) |
474ffc72 JH |
664 | { |
665 | error ("function symbol is not function"); | |
666 | error_found = true; | |
667 | } | |
668 | } | |
7de90a6c | 669 | else if (is_a <varpool_node *> (node)) |
474ffc72 | 670 | { |
67348ccc | 671 | if (TREE_CODE (node->decl) != VAR_DECL) |
474ffc72 JH |
672 | { |
673 | error ("variable symbol is not variable"); | |
674 | error_found = true; | |
675 | } | |
676 | } | |
677 | else | |
678 | { | |
679 | error ("node has unknown type"); | |
680 | error_found = true; | |
681 | } | |
682 | ||
ca0f62a8 | 683 | if (cgraph_state != CGRAPH_LTO_STREAMING) |
474ffc72 | 684 | { |
67348ccc | 685 | hashed_node = symtab_get_node (node->decl); |
ca0f62a8 JH |
686 | if (!hashed_node) |
687 | { | |
aede2c10 | 688 | error ("node not found node->decl->decl_with_vis.symtab_node"); |
ca0f62a8 JH |
689 | error_found = true; |
690 | } | |
e5b962d0 | 691 | if (hashed_node != node |
7de90a6c DM |
692 | && (!is_a <cgraph_node *> (node) |
693 | || !dyn_cast <cgraph_node *> (node)->clone_of | |
694 | || dyn_cast <cgraph_node *> (node)->clone_of->decl | |
67348ccc | 695 | != node->decl)) |
e5b962d0 | 696 | { |
aede2c10 | 697 | error ("node differs from node->decl->decl_with_vis.symtab_node"); |
e5b962d0 JH |
698 | error_found = true; |
699 | } | |
474ffc72 JH |
700 | } |
701 | if (assembler_name_hash) | |
702 | { | |
67348ccc DM |
703 | hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl)); |
704 | if (hashed_node && hashed_node->previous_sharing_asm_name) | |
474ffc72 JH |
705 | { |
706 | error ("assembler name hash list corrupted"); | |
707 | error_found = true; | |
708 | } | |
709 | while (hashed_node) | |
710 | { | |
711 | if (hashed_node == node) | |
712 | break; | |
67348ccc | 713 | hashed_node = hashed_node->next_sharing_asm_name; |
474ffc72 | 714 | } |
b5493fb2 | 715 | if (!hashed_node |
7de90a6c | 716 | && !(is_a <varpool_node *> (node) |
67348ccc | 717 | || DECL_HARD_REGISTER (node->decl))) |
474ffc72 JH |
718 | { |
719 | error ("node not found in symtab assembler name hash"); | |
720 | error_found = true; | |
721 | } | |
722 | } | |
67348ccc DM |
723 | if (node->previous_sharing_asm_name |
724 | && node->previous_sharing_asm_name->next_sharing_asm_name != node) | |
474ffc72 JH |
725 | { |
726 | error ("double linked list of assembler names corrupted"); | |
e70670cf JH |
727 | error_found = true; |
728 | } | |
67348ccc | 729 | if (node->analyzed && !node->definition) |
e70670cf JH |
730 | { |
731 | error ("node is analyzed byt it is not a definition"); | |
732 | error_found = true; | |
474ffc72 | 733 | } |
67348ccc | 734 | if (node->cpp_implicit_alias && !node->alias) |
40a7fe1e JH |
735 | { |
736 | error ("node is alias but not implicit alias"); | |
737 | error_found = true; | |
738 | } | |
67348ccc DM |
739 | if (node->alias && !node->definition |
740 | && !node->weakref) | |
40a7fe1e JH |
741 | { |
742 | error ("node is alias but not definition"); | |
743 | error_found = true; | |
744 | } | |
67348ccc | 745 | if (node->weakref && !node->alias) |
08346abd JH |
746 | { |
747 | error ("node is weakref but not an alias"); | |
748 | error_found = true; | |
749 | } | |
67348ccc | 750 | if (node->same_comdat_group) |
474ffc72 | 751 | { |
5e20cdc9 | 752 | symtab_node *n = node->same_comdat_group; |
474ffc72 | 753 | |
aede2c10 | 754 | if (!n->get_comdat_group ()) |
474ffc72 | 755 | { |
aede2c10 | 756 | error ("node is in same_comdat_group list but has no comdat_group"); |
474ffc72 JH |
757 | error_found = true; |
758 | } | |
aede2c10 | 759 | if (n->get_comdat_group () != node->get_comdat_group ()) |
7b3376a0 JH |
760 | { |
761 | error ("same_comdat_group list across different groups"); | |
762 | error_found = true; | |
763 | } | |
764 | if (!n->definition) | |
765 | { | |
766 | error ("Node has same_comdat_group but it is not a definition"); | |
767 | error_found = true; | |
768 | } | |
67348ccc | 769 | if (n->type != node->type) |
474ffc72 JH |
770 | { |
771 | error ("mixing different types of symbol in same comdat groups is not supported"); | |
772 | error_found = true; | |
773 | } | |
774 | if (n == node) | |
775 | { | |
776 | error ("node is alone in a comdat group"); | |
777 | error_found = true; | |
778 | } | |
779 | do | |
780 | { | |
67348ccc | 781 | if (!n->same_comdat_group) |
474ffc72 JH |
782 | { |
783 | error ("same_comdat_group is not a circular list"); | |
784 | error_found = true; | |
785 | break; | |
786 | } | |
67348ccc | 787 | n = n->same_comdat_group; |
474ffc72 JH |
788 | } |
789 | while (n != node); | |
1f26ac87 JM |
790 | if (symtab_comdat_local_p (node)) |
791 | { | |
792 | struct ipa_ref_list *refs = &node->ref_list; | |
793 | struct ipa_ref *ref; | |
794 | for (int i = 0; ipa_ref_list_referring_iterate (refs, i, ref); ++i) | |
795 | { | |
796 | if (!symtab_in_same_comdat_p (ref->referring, node)) | |
797 | { | |
798 | error ("comdat-local symbol referred to by %s outside its " | |
799 | "comdat", | |
800 | identifier_to_locale (ref->referring->name())); | |
801 | error_found = true; | |
802 | } | |
803 | } | |
804 | } | |
474ffc72 JH |
805 | } |
806 | return error_found; | |
807 | } | |
808 | ||
809 | /* Verify consistency of NODE. */ | |
810 | ||
811 | DEBUG_FUNCTION void | |
5e20cdc9 | 812 | verify_symtab_node (symtab_node *node) |
474ffc72 JH |
813 | { |
814 | if (seen_error ()) | |
815 | return; | |
816 | ||
817 | timevar_push (TV_CGRAPH_VERIFY); | |
7de90a6c | 818 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node)) |
5d59b5e1 | 819 | verify_cgraph_node (cnode); |
474ffc72 JH |
820 | else |
821 | if (verify_symtab_base (node)) | |
822 | { | |
823 | dump_symtab_node (stderr, node); | |
824 | internal_error ("verify_symtab_node failed"); | |
825 | } | |
826 | timevar_pop (TV_CGRAPH_VERIFY); | |
827 | } | |
828 | ||
829 | /* Verify symbol table for internal consistency. */ | |
830 | ||
831 | DEBUG_FUNCTION void | |
832 | verify_symtab (void) | |
833 | { | |
5e20cdc9 | 834 | symtab_node *node; |
474ffc72 JH |
835 | FOR_EACH_SYMBOL (node) |
836 | verify_symtab_node (node); | |
837 | } | |
838 | ||
65d630d4 JH |
839 | /* Return true when RESOLUTION indicate that linker will use |
840 | the symbol from non-LTO object files. */ | |
841 | ||
842 | bool | |
843 | resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution) | |
844 | { | |
845 | return (resolution == LDPR_PREVAILING_DEF | |
846 | || resolution == LDPR_PREEMPTED_REG | |
847 | || resolution == LDPR_RESOLVED_EXEC | |
848 | || resolution == LDPR_RESOLVED_DYN); | |
849 | } | |
850 | ||
851 | /* Return true when NODE is known to be used from other (non-LTO) object file. | |
852 | Known only when doing LTO via linker plugin. */ | |
853 | ||
854 | bool | |
5e20cdc9 | 855 | symtab_used_from_object_file_p (symtab_node *node) |
65d630d4 | 856 | { |
67348ccc | 857 | if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl)) |
65d630d4 | 858 | return false; |
67348ccc | 859 | if (resolution_used_from_other_file_p (node->resolution)) |
65d630d4 JH |
860 | return true; |
861 | return false; | |
862 | } | |
863 | ||
864 | /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early, | |
865 | but other code such as notice_global_symbol generates rtl. */ | |
40a7fe1e | 866 | |
65d630d4 JH |
867 | void |
868 | symtab_make_decl_local (tree decl) | |
869 | { | |
870 | rtx rtl, symbol; | |
871 | ||
aede2c10 | 872 | /* Avoid clearing comdat_groups on comdat-local decls. */ |
1f26ac87 JM |
873 | if (TREE_PUBLIC (decl) == 0) |
874 | return; | |
875 | ||
65d630d4 JH |
876 | if (TREE_CODE (decl) == VAR_DECL) |
877 | DECL_COMMON (decl) = 0; | |
878 | else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); | |
879 | ||
aede2c10 | 880 | if (DECL_COMDAT (decl)) |
65d630d4 | 881 | { |
65d630d4 JH |
882 | DECL_SECTION_NAME (decl) = 0; |
883 | DECL_COMDAT (decl) = 0; | |
884 | } | |
65d630d4 JH |
885 | DECL_WEAK (decl) = 0; |
886 | DECL_EXTERNAL (decl) = 0; | |
b5493fb2 JH |
887 | DECL_VISIBILITY_SPECIFIED (decl) = 0; |
888 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; | |
65d630d4 JH |
889 | TREE_PUBLIC (decl) = 0; |
890 | if (!DECL_RTL_SET_P (decl)) | |
891 | return; | |
892 | ||
893 | /* Update rtl flags. */ | |
894 | make_decl_rtl (decl); | |
895 | ||
896 | rtl = DECL_RTL (decl); | |
897 | if (!MEM_P (rtl)) | |
898 | return; | |
899 | ||
900 | symbol = XEXP (rtl, 0); | |
901 | if (GET_CODE (symbol) != SYMBOL_REF) | |
902 | return; | |
903 | ||
904 | SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl); | |
905 | } | |
e70670cf | 906 | |
40a7fe1e JH |
907 | /* Return availability of NODE. */ |
908 | ||
909 | enum availability | |
5e20cdc9 | 910 | symtab_node_availability (symtab_node *node) |
40a7fe1e | 911 | { |
7de90a6c | 912 | if (is_a <cgraph_node *> (node)) |
40a7fe1e JH |
913 | return cgraph_function_body_availability (cgraph (node)); |
914 | else | |
915 | return cgraph_variable_initializer_availability (varpool (node)); | |
916 | } | |
917 | ||
e70670cf JH |
918 | /* Given NODE, walk the alias chain to return the symbol NODE is alias of. |
919 | If NODE is not an alias, return NODE. | |
920 | When AVAILABILITY is non-NULL, get minimal availability in the chain. */ | |
921 | ||
5e20cdc9 DM |
922 | symtab_node * |
923 | symtab_alias_ultimate_target (symtab_node *node, enum availability *availability) | |
e70670cf | 924 | { |
aaae719d JH |
925 | bool weakref_p = false; |
926 | ||
67348ccc | 927 | if (!node->alias) |
aaae719d JH |
928 | { |
929 | if (availability) | |
930 | *availability = symtab_node_availability (node); | |
931 | return node; | |
932 | } | |
933 | ||
934 | /* To determine visibility of the target, we follow ELF semantic of aliases. | |
935 | Here alias is an alternative assembler name of a given definition. Its | |
1aa95df7 | 936 | availability prevails the availability of its target (i.e. static alias of |
aaae719d JH |
937 | weak definition is available. |
938 | ||
939 | Weakref is a different animal (and not part of ELF per se). It is just | |
940 | alternative name of a given symbol used within one complation unit | |
941 | and is translated prior hitting the object file. It inherits the | |
942 | visibility of its target (i.e. weakref of non-overwritable definition | |
943 | is non-overwritable, while weakref of weak definition is weak). | |
944 | ||
945 | If we ever get into supporting targets with different semantics, a target | |
946 | hook will be needed here. */ | |
947 | ||
e70670cf | 948 | if (availability) |
aaae719d | 949 | { |
67348ccc | 950 | weakref_p = node->weakref; |
aaae719d JH |
951 | if (!weakref_p) |
952 | *availability = symtab_node_availability (node); | |
953 | else | |
954 | *availability = AVAIL_LOCAL; | |
955 | } | |
e70670cf JH |
956 | while (node) |
957 | { | |
67348ccc | 958 | if (node->alias && node->analyzed) |
e70670cf JH |
959 | node = symtab_alias_target (node); |
960 | else | |
aaae719d JH |
961 | { |
962 | if (!availability) | |
963 | ; | |
67348ccc | 964 | else if (node->analyzed) |
aaae719d JH |
965 | { |
966 | if (weakref_p) | |
967 | { | |
968 | enum availability a = symtab_node_availability (node); | |
969 | if (a < *availability) | |
970 | *availability = a; | |
971 | } | |
972 | } | |
973 | else | |
974 | *availability = AVAIL_NOT_AVAILABLE; | |
975 | return node; | |
976 | } | |
977 | if (node && availability && weakref_p) | |
e70670cf | 978 | { |
40a7fe1e | 979 | enum availability a = symtab_node_availability (node); |
e70670cf JH |
980 | if (a < *availability) |
981 | *availability = a; | |
67348ccc | 982 | weakref_p = node->weakref; |
e70670cf JH |
983 | } |
984 | } | |
985 | if (availability) | |
986 | *availability = AVAIL_NOT_AVAILABLE; | |
987 | return NULL; | |
988 | } | |
40a7fe1e JH |
989 | |
990 | /* C++ FE sometimes change linkage flags after producing same body aliases. | |
991 | ||
992 | FIXME: C++ produce implicit aliases for virtual functions and vtables that | |
993 | are obviously equivalent. The way it is doing so is however somewhat | |
994 | kludgy and interferes with the visibility code. As a result we need to | |
995 | copy the visibility from the target to get things right. */ | |
996 | ||
997 | void | |
5e20cdc9 | 998 | fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target) |
40a7fe1e | 999 | { |
7de90a6c | 1000 | if (is_a <cgraph_node *> (node)) |
40a7fe1e | 1001 | { |
67348ccc DM |
1002 | DECL_DECLARED_INLINE_P (node->decl) |
1003 | = DECL_DECLARED_INLINE_P (target->decl); | |
1004 | DECL_DISREGARD_INLINE_LIMITS (node->decl) | |
1005 | = DECL_DISREGARD_INLINE_LIMITS (target->decl); | |
40a7fe1e JH |
1006 | } |
1007 | /* FIXME: It is not really clear why those flags should not be copied for | |
1008 | functions, too. */ | |
1009 | else | |
1010 | { | |
67348ccc DM |
1011 | DECL_WEAK (node->decl) = DECL_WEAK (target->decl); |
1012 | DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl); | |
1013 | DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl); | |
40a7fe1e | 1014 | } |
67348ccc DM |
1015 | DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl); |
1016 | if (TREE_PUBLIC (node->decl)) | |
40a7fe1e | 1017 | { |
aede2c10 JH |
1018 | tree group; |
1019 | ||
67348ccc DM |
1020 | DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl); |
1021 | DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl); | |
aede2c10 JH |
1022 | group = target->get_comdat_group (); |
1023 | node->set_comdat_group (group); | |
1024 | if (group | |
67348ccc DM |
1025 | && !node->same_comdat_group) |
1026 | symtab_add_to_same_comdat_group (node, target); | |
40a7fe1e | 1027 | } |
67348ccc | 1028 | node->externally_visible = target->externally_visible; |
40a7fe1e JH |
1029 | } |
1030 | ||
1031 | /* Add reference recording that NODE is alias of TARGET. | |
1032 | The function can fail in the case of aliasing cycles; in this case | |
1033 | it returns false. */ | |
1034 | ||
1035 | bool | |
5e20cdc9 | 1036 | symtab_resolve_alias (symtab_node *node, symtab_node *target) |
40a7fe1e | 1037 | { |
5e20cdc9 | 1038 | symtab_node *n; |
40a7fe1e | 1039 | |
67348ccc DM |
1040 | gcc_assert (!node->analyzed |
1041 | && !vec_safe_length (node->ref_list.references)); | |
40a7fe1e JH |
1042 | |
1043 | /* Never let cycles to creep into the symbol table alias references; | |
1044 | those will make alias walkers to be infinite. */ | |
67348ccc DM |
1045 | for (n = target; n && n->alias; |
1046 | n = n->analyzed ? symtab_alias_target (n) : NULL) | |
40a7fe1e JH |
1047 | if (n == node) |
1048 | { | |
7de90a6c | 1049 | if (is_a <cgraph_node *> (node)) |
67348ccc | 1050 | error ("function %q+D part of alias cycle", node->decl); |
7de90a6c | 1051 | else if (is_a <varpool_node *> (node)) |
67348ccc | 1052 | error ("variable %q+D part of alias cycle", node->decl); |
40a7fe1e JH |
1053 | else |
1054 | gcc_unreachable (); | |
67348ccc | 1055 | node->alias = false; |
40a7fe1e JH |
1056 | return false; |
1057 | } | |
1058 | ||
1059 | /* "analyze" the node - i.e. mark the reference. */ | |
67348ccc DM |
1060 | node->definition = true; |
1061 | node->alias = true; | |
1062 | node->analyzed = true; | |
40a7fe1e JH |
1063 | ipa_record_reference (node, target, IPA_REF_ALIAS, NULL); |
1064 | ||
1065 | /* Alias targets become reudndant after alias is resolved into an reference. | |
1066 | We do not want to keep it around or we would have to mind updating them | |
1067 | when renaming symbols. */ | |
67348ccc | 1068 | node->alias_target = NULL; |
40a7fe1e | 1069 | |
67348ccc | 1070 | if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION) |
40a7fe1e JH |
1071 | fixup_same_cpp_alias_visibility (node, target); |
1072 | ||
1073 | /* If alias has address taken, so does the target. */ | |
67348ccc DM |
1074 | if (node->address_taken) |
1075 | symtab_alias_ultimate_target (target, NULL)->address_taken = true; | |
40a7fe1e JH |
1076 | return true; |
1077 | } | |
af15184a JH |
1078 | |
1079 | /* Call calback on NODE and aliases associated to NODE. | |
1080 | When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are | |
1081 | skipped. */ | |
1082 | ||
1083 | bool | |
5e20cdc9 DM |
1084 | symtab_for_node_and_aliases (symtab_node *node, |
1085 | bool (*callback) (symtab_node *, void *), | |
af15184a JH |
1086 | void *data, |
1087 | bool include_overwritable) | |
1088 | { | |
1089 | int i; | |
1090 | struct ipa_ref *ref; | |
1091 | ||
1092 | if (callback (node, data)) | |
1093 | return true; | |
67348ccc | 1094 | for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++) |
af15184a JH |
1095 | if (ref->use == IPA_REF_ALIAS) |
1096 | { | |
5e20cdc9 | 1097 | symtab_node *alias = ref->referring; |
af15184a JH |
1098 | if (include_overwritable |
1099 | || symtab_node_availability (alias) > AVAIL_OVERWRITABLE) | |
1100 | if (symtab_for_node_and_aliases (alias, callback, data, | |
1101 | include_overwritable)) | |
1102 | return true; | |
1103 | } | |
1104 | return false; | |
1105 | } | |
1106 | ||
1107 | /* Worker searching nonoverwritable alias. */ | |
1108 | ||
1109 | static bool | |
5e20cdc9 | 1110 | symtab_nonoverwritable_alias_1 (symtab_node *node, void *data) |
af15184a | 1111 | { |
67348ccc | 1112 | if (decl_binds_to_current_def_p (node->decl)) |
af15184a | 1113 | { |
5e20cdc9 | 1114 | *(symtab_node **)data = node; |
af15184a JH |
1115 | return true; |
1116 | } | |
1117 | return false; | |
1118 | } | |
1119 | ||
1120 | /* If NODE can not be overwriten by static or dynamic linker to point to different | |
1121 | definition, return NODE. Otherwise look for alias with such property and if | |
1122 | none exists, introduce new one. */ | |
1123 | ||
5e20cdc9 DM |
1124 | symtab_node * |
1125 | symtab_nonoverwritable_alias (symtab_node *node) | |
af15184a JH |
1126 | { |
1127 | tree new_decl; | |
5e20cdc9 | 1128 | symtab_node *new_node = NULL; |
8a41354f JH |
1129 | |
1130 | /* First try to look up existing alias or base object | |
1131 | (if that is already non-overwritable). */ | |
1132 | node = symtab_alias_ultimate_target (node, NULL); | |
67348ccc | 1133 | gcc_assert (!node->alias && !node->weakref); |
af15184a JH |
1134 | symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1, |
1135 | (void *)&new_node, true); | |
1136 | if (new_node) | |
1137 | return new_node; | |
cdb87c08 JH |
1138 | #ifndef ASM_OUTPUT_DEF |
1139 | /* If aliases aren't supported by the assembler, fail. */ | |
1140 | return NULL; | |
1141 | #endif | |
af15184a | 1142 | |
8a41354f | 1143 | /* Otherwise create a new one. */ |
67348ccc DM |
1144 | new_decl = copy_node (node->decl); |
1145 | DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias"); | |
af15184a JH |
1146 | if (TREE_CODE (new_decl) == FUNCTION_DECL) |
1147 | DECL_STRUCT_FUNCTION (new_decl) = NULL; | |
1148 | DECL_INITIAL (new_decl) = NULL; | |
1149 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); | |
1150 | SET_DECL_RTL (new_decl, NULL); | |
1151 | ||
1152 | /* Update the properties. */ | |
1153 | DECL_EXTERNAL (new_decl) = 0; | |
af15184a JH |
1154 | TREE_PUBLIC (new_decl) = 0; |
1155 | DECL_COMDAT (new_decl) = 0; | |
1156 | DECL_WEAK (new_decl) = 0; | |
1157 | DECL_VIRTUAL_P (new_decl) = 0; | |
1158 | if (TREE_CODE (new_decl) == FUNCTION_DECL) | |
1159 | { | |
1160 | DECL_STATIC_CONSTRUCTOR (new_decl) = 0; | |
1161 | DECL_STATIC_DESTRUCTOR (new_decl) = 0; | |
67348ccc DM |
1162 | new_node = cgraph_create_function_alias |
1163 | (new_decl, node->decl); | |
af15184a JH |
1164 | } |
1165 | else | |
aede2c10 | 1166 | new_node = varpool_create_variable_alias (new_decl, node->decl); |
af15184a | 1167 | symtab_resolve_alias (new_node, node); |
fc11f321 | 1168 | gcc_assert (decl_binds_to_current_def_p (new_decl)); |
af15184a JH |
1169 | return new_node; |
1170 | } | |
fc11f321 JH |
1171 | |
1172 | /* Return true if A and B represents semantically equivalent symbols. */ | |
1173 | ||
1174 | bool | |
5e20cdc9 DM |
1175 | symtab_semantically_equivalent_p (symtab_node *a, |
1176 | symtab_node *b) | |
fc11f321 JH |
1177 | { |
1178 | enum availability avail; | |
5e20cdc9 DM |
1179 | symtab_node *ba; |
1180 | symtab_node *bb; | |
fc11f321 JH |
1181 | |
1182 | /* Equivalent functions are equivalent. */ | |
67348ccc | 1183 | if (a->decl == b->decl) |
fc11f321 JH |
1184 | return true; |
1185 | ||
1186 | /* If symbol is not overwritable by different implementation, | |
1187 | walk to the base object it defines. */ | |
1188 | ba = symtab_alias_ultimate_target (a, &avail); | |
1189 | if (avail >= AVAIL_AVAILABLE) | |
1190 | { | |
1191 | if (ba == b) | |
1192 | return true; | |
1193 | } | |
1194 | else | |
1195 | ba = a; | |
1196 | bb = symtab_alias_ultimate_target (b, &avail); | |
1197 | if (avail >= AVAIL_AVAILABLE) | |
1198 | { | |
1199 | if (a == bb) | |
1200 | return true; | |
1201 | } | |
1202 | else | |
1203 | bb = b; | |
1204 | return bb == ba; | |
1205 | } | |
ddb3e20a JH |
1206 | |
1207 | /* Classify symbol NODE for partitioning. */ | |
1208 | ||
1209 | enum symbol_partitioning_class | |
1210 | symtab_get_symbol_partitioning_class (symtab_node *node) | |
1211 | { | |
1212 | /* Inline clones are always duplicated. | |
1213 | This include external delcarations. */ | |
7de90a6c | 1214 | cgraph_node *cnode = dyn_cast <cgraph_node *> (node); |
ddb3e20a JH |
1215 | |
1216 | if (DECL_ABSTRACT (node->decl)) | |
1217 | return SYMBOL_EXTERNAL; | |
1218 | ||
1219 | if (cnode && cnode->global.inlined_to) | |
1220 | return SYMBOL_DUPLICATE; | |
1221 | ||
1222 | /* Weakref aliases are always duplicated. */ | |
1223 | if (node->weakref) | |
1224 | return SYMBOL_DUPLICATE; | |
1225 | ||
1226 | /* External declarations are external. */ | |
1227 | if (DECL_EXTERNAL (node->decl)) | |
1228 | return SYMBOL_EXTERNAL; | |
1229 | ||
7de90a6c | 1230 | if (varpool_node *vnode = dyn_cast <varpool_node *> (node)) |
ddb3e20a JH |
1231 | { |
1232 | /* Constant pool references use local symbol names that can not | |
1233 | be promoted global. We should never put into a constant pool | |
1234 | objects that can not be duplicated across partitions. */ | |
1235 | if (DECL_IN_CONSTANT_POOL (node->decl)) | |
1236 | return SYMBOL_DUPLICATE; | |
1237 | gcc_checking_assert (vnode->definition); | |
1238 | } | |
1239 | /* Functions that are cloned may stay in callgraph even if they are unused. | |
1240 | Handle them as external; compute_ltrans_boundary take care to make | |
1241 | proper things to happen (i.e. to make them appear in the boundary but | |
1242 | with body streamed, so clone can me materialized). */ | |
1243 | else if (!cgraph (node)->definition) | |
1244 | return SYMBOL_EXTERNAL; | |
1245 | ||
1246 | /* Linker discardable symbols are duplicated to every use unless they are | |
cf288ed3 | 1247 | keyed. */ |
ddb3e20a JH |
1248 | if (DECL_ONE_ONLY (node->decl) |
1249 | && !node->force_output | |
1250 | && !node->forced_by_abi | |
1251 | && !symtab_used_from_object_file_p (node)) | |
1252 | return SYMBOL_DUPLICATE; | |
1253 | ||
1254 | return SYMBOL_PARTITION; | |
1255 | } | |
1ab24192 | 1256 | #include "gt-symtab.h" |