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