1 /* LTO symbol table.
2    Copyright (C) 2009-2016 Free Software Foundation, Inc.
3    Contributed by CodeSourcery, Inc.
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 "target.h"
25 #include "function.h"
26 #include "basic-block.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cgraph.h"
30 #include "lto-streamer.h"
31 #include "ipa-utils.h"
32 #include "builtins.h"
33 #include "alias.h"
34 #include "lto-symtab.h"
35 
36 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
37    all edges and removing the old node.  */
38 
39 static void
lto_cgraph_replace_node(struct cgraph_node * node,struct cgraph_node * prevailing_node)40 lto_cgraph_replace_node (struct cgraph_node *node,
41 			 struct cgraph_node *prevailing_node)
42 {
43   struct cgraph_edge *e, *next;
44   bool compatible_p;
45 
46   if (symtab->dump_file)
47     {
48       fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
49  	       " for symbol %s\n",
50 	       node->name (), node->order,
51 	       prevailing_node->name (),
52 	       prevailing_node->order,
53 	       IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
54 		 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
55     }
56 
57   /* Merge node flags.  */
58   if (node->force_output)
59     prevailing_node->mark_force_output ();
60   if (node->forced_by_abi)
61     prevailing_node->forced_by_abi = true;
62   if (node->address_taken)
63     {
64       gcc_assert (!prevailing_node->global.inlined_to);
65       prevailing_node->mark_address_taken ();
66     }
67   if (node->definition && prevailing_node->definition
68       && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
69     prevailing_node->merged_comdat = true;
70 
71   /* Redirect all incoming edges.  */
72   compatible_p
73     = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
74 			  TREE_TYPE (TREE_TYPE (node->decl)));
75   for (e = node->callers; e; e = next)
76     {
77       next = e->next_caller;
78       e->redirect_callee (prevailing_node);
79       /* If there is a mismatch between the supposed callee return type and
80 	 the real one do not attempt to inline this function.
81 	 ???  We really need a way to match function signatures for ABI
82 	 compatibility and perform related promotions at inlining time.  */
83       if (!compatible_p)
84 	e->call_stmt_cannot_inline_p = 1;
85     }
86   /* Redirect incomming references.  */
87   prevailing_node->clone_referring (node);
88 
89   /* Fix instrumentation references.  */
90   if (node->instrumented_version)
91     {
92       gcc_assert (node->instrumentation_clone
93 		  == prevailing_node->instrumentation_clone);
94       node->instrumented_version->instrumented_version = prevailing_node;
95       if (!prevailing_node->instrumented_version)
96 	prevailing_node->instrumented_version = node->instrumented_version;
97       /* Need to reset node->instrumented_version to NULL,
98 	 otherwise node removal code would reset
99 	 node->instrumented_version->instrumented_version.  */
100       node->instrumented_version = NULL;
101     }
102 
103   lto_free_function_in_decl_state_for_node (node);
104 
105   if (node->decl != prevailing_node->decl)
106     node->release_body ();
107 
108   /* Finally remove the replaced node.  */
109   node->remove ();
110 }
111 
112 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
113    all edges and removing the old node.  */
114 
115 static void
lto_varpool_replace_node(varpool_node * vnode,varpool_node * prevailing_node)116 lto_varpool_replace_node (varpool_node *vnode,
117 			  varpool_node *prevailing_node)
118 {
119   gcc_assert (!vnode->definition || prevailing_node->definition);
120   gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
121 
122   prevailing_node->clone_referring (vnode);
123   if (vnode->force_output)
124     prevailing_node->force_output = true;
125   if (vnode->forced_by_abi)
126     prevailing_node->forced_by_abi = true;
127 
128   /* Be sure we can garbage collect the initializer.  */
129   if (DECL_INITIAL (vnode->decl)
130       && vnode->decl != prevailing_node->decl)
131     DECL_INITIAL (vnode->decl) = error_mark_node;
132 
133   /* Check and report ODR violations on virtual tables.  */
134   if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
135     compare_virtual_tables (prevailing_node, vnode);
136 
137   if (vnode->tls_model != prevailing_node->tls_model)
138     {
139       bool error = false;
140 
141       /* Non-TLS and TLS never mix together.  Also emulated model is not
142 	 compatible with anything else.  */
143       if (prevailing_node->tls_model == TLS_MODEL_NONE
144 	  || prevailing_node->tls_model == TLS_MODEL_EMULATED
145 	  || vnode->tls_model == TLS_MODEL_NONE
146 	  || vnode->tls_model == TLS_MODEL_EMULATED)
147 	error = true;
148       /* Linked is silently supporting transitions
149 	 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
150 	 Do the same transitions and error out on others.  */
151       else if ((prevailing_node->tls_model == TLS_MODEL_REAL
152 		|| prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
153 	       && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
154 		   || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
155 	prevailing_node->tls_model = vnode->tls_model;
156       else if ((vnode->tls_model == TLS_MODEL_REAL
157 		|| vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
158 	       && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
159 		   || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
160 	;
161       else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
162 	       && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
163 	prevailing_node->tls_model = vnode->tls_model;
164       else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
165 	       && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
166 	;
167       else
168 	error = true;
169       if (error)
170 	{
171 	  error_at (DECL_SOURCE_LOCATION (vnode->decl),
172 		    "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
173 	  inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
174 		  "previously defined here as %s",
175 		  tls_model_names [prevailing_node->tls_model]);
176 	}
177     }
178   /* Finally remove the replaced node.  */
179   vnode->remove ();
180 }
181 
182 /* Return non-zero if we want to output waring about T1 and T2.
183    Return value is a bitmask of reasons of violation:
184    Bit 0 indicates that types are not compatible.
185    Bit 1 indicates that types are not compatible because of C++ ODR rule.
186    If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays.
187    Bit 2 indicates that types are not ODR compatible
188 
189    The interoperability rules are language specific.  At present we do only
190    full checking for C++ ODR rule and for other languages we do basic check
191    that data structures are of same size and TBAA compatible.  Our TBAA
192    implementation should be coarse enough so all valid type transitions
193    across different languages are allowed.
194 
195    In partiucular we thus allow almost arbitrary type changes with
196    -fno-strict-aliasing which may be tough of as a feature rather than bug
197    as it allows to implement dodgy tricks in the language runtimes.
198 
199    Naturally this code can be strenghtened significantly if we could track
200    down the language of origin.  */
201 
202 static int
warn_type_compatibility_p(tree prevailing_type,tree type,bool common_or_extern)203 warn_type_compatibility_p (tree prevailing_type, tree type,
204 			   bool common_or_extern)
205 {
206   int lev = 0;
207   bool odr_p = odr_or_derived_type_p (prevailing_type)
208 	       && odr_or_derived_type_p (type);
209 
210   if (prevailing_type == type)
211     return 0;
212 
213   /* C++ provide a robust way to check for type compatibility via the ODR
214      rule.  */
215   if (odr_p && !odr_types_equivalent_p (prevailing_type, type))
216     lev |= 2;
217 
218   /* Function types needs special care, because types_compatible_p never
219      thinks prototype is compatible to non-prototype.  */
220   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
221     {
222       if (TREE_CODE (type) != TREE_CODE (prevailing_type))
223 	lev |= 1;
224       lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
225 				        TREE_TYPE (type), false);
226       if (TREE_CODE (type) == METHOD_TYPE
227 	  && TREE_CODE (prevailing_type) == METHOD_TYPE)
228 	lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
229 					  TYPE_METHOD_BASETYPE (type), false);
230       if (prototype_p (prevailing_type) && prototype_p (type)
231 	  && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
232 	{
233 	  tree parm1, parm2;
234 	  for (parm1 = TYPE_ARG_TYPES (prevailing_type),
235 	       parm2 = TYPE_ARG_TYPES (type);
236 	       parm1 && parm2;
237 	       parm1 = TREE_CHAIN (parm1),
238 	       parm2 = TREE_CHAIN (parm2))
239 	    lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
240 					      TREE_VALUE (parm2), false);
241 	  if (parm1 || parm2)
242 	    lev |= odr_p ? 3 : 1;
243 	}
244       if (comp_type_attributes (prevailing_type, type) == 0)
245 	lev |= 1;
246       return lev;
247     }
248 
249   /* Get complete type.  */
250   prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
251   type = TYPE_MAIN_VARIANT (type);
252 
253   /* We can not use types_compatible_p because we permit some changes
254      across types.  For example unsigned size_t and "signed size_t" may be
255      compatible when merging C and Fortran types.  */
256   if (COMPLETE_TYPE_P (prevailing_type)
257       && COMPLETE_TYPE_P (type)
258       /* While global declarations are never variadic, we can recurse here
259 	 for function parameter types.  */
260       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
261       && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
262       && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type)))
263     {
264        /* As a special case do not warn about merging
265 	  int a[];
266 	  and
267 	  int a[]={1,2,3};
268 	  here the first declaration is COMMON or EXTERN
269 	  and sizeof(a) == sizeof (int).  */
270        if (!common_or_extern
271 	   || TREE_CODE (type) != ARRAY_TYPE
272 	   || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))
273        lev |= 1;
274     }
275 
276   /* Verify TBAA compatibility.  Take care of alias set 0 and the fact that
277      we make ptr_type_node to TBAA compatible with every other type.  */
278   if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
279     {
280       alias_set_type set1 = get_alias_set (type);
281       alias_set_type set2 = get_alias_set (prevailing_type);
282 
283       if (set1 && set2 && set1 != set2)
284 	{
285           tree t1 = type, t2 = prevailing_type;
286 
287 	  /* Alias sets of arrays with aliased components are the same as alias
288 	     sets of the inner types.  */
289 	  while (TREE_CODE (t1) == ARRAY_TYPE
290 		 && !TYPE_NONALIASED_COMPONENT (t1)
291 		 && TREE_CODE (t2) == ARRAY_TYPE
292 		 && !TYPE_NONALIASED_COMPONENT (t2))
293 	    {
294 	      t1 = TREE_TYPE (t1);
295 	      t2 = TREE_TYPE (t2);
296 	    }
297           if ((!POINTER_TYPE_P (t1) || !POINTER_TYPE_P (t2))
298 	      || (set1 != TYPE_ALIAS_SET (ptr_type_node)
299 		  && set2 != TYPE_ALIAS_SET (ptr_type_node)))
300              lev |= 5;
301 	}
302     }
303 
304   return lev;
305 }
306 
307 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
308    Return false if the symbols are not fully compatible and a diagnostic
309    should be emitted.  */
310 
311 static bool
lto_symtab_merge(symtab_node * prevailing,symtab_node * entry)312 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
313 {
314   tree prevailing_decl = prevailing->decl;
315   tree decl = entry->decl;
316 
317   if (prevailing_decl == decl)
318     return true;
319 
320   if (TREE_CODE (decl) != TREE_CODE (prevailing_decl))
321     return false;
322 
323   /* Merge decl state in both directions, we may still end up using
324      the new decl.  */
325   TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
326   TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
327 
328   /* The linker may ask us to combine two incompatible symbols.
329      Detect this case and notify the caller of required diagnostics.  */
330 
331   if (TREE_CODE (decl) == FUNCTION_DECL)
332     {
333       /* Merge decl state in both directions, we may still end up using
334 	 the new decl.  */
335       DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
336       DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
337 
338       if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
339 			             TREE_TYPE (decl),
340 				     DECL_COMMON (decl)
341 				     || DECL_EXTERNAL (decl)))
342 	return false;
343 
344       return true;
345     }
346 
347   if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
348 				 TREE_TYPE (decl),
349 				 DECL_COMMON (decl) || DECL_EXTERNAL (decl)))
350     return false;
351 
352   /* There is no point in comparing too many details of the decls here.
353      The type compatibility checks or the completing of types has properly
354      dealt with most issues.  */
355 
356   /* The following should all not invoke fatal errors as in non-LTO
357      mode the linker wouldn't complain either.  Just emit warnings.  */
358 
359   /* Report a warning if user-specified alignments do not match.  */
360   if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
361       && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
362     return false;
363 
364   if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
365       && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl)))
366       {
367 	if (!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
368 	  return false;
369 
370 	tree type = TREE_TYPE (decl);
371 
372 	/* For record type, check for array at the end of the structure.  */
373 	if (TREE_CODE (type) == RECORD_TYPE)
374 	  {
375 	    tree field = TYPE_FIELDS (type);
376 	    while (DECL_CHAIN (field) != NULL_TREE)
377 	      field = DECL_CHAIN (field);
378 
379 	    return TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE;
380 	  }
381       /* As a special case do not warn about merging
382 	 int a[];
383 	 and
384 	 int a[]={1,2,3};
385 	 here the first declaration is COMMON
386 	 and sizeof(a) == sizeof (int).  */
387 	else if (TREE_CODE (type) == ARRAY_TYPE)
388 	  return (TYPE_SIZE (decl) == TYPE_SIZE (TREE_TYPE (type)));
389       }
390 
391   return true;
392 }
393 
394 /* Return true if the symtab entry E can be replaced by another symtab
395    entry.  */
396 
397 static bool
lto_symtab_resolve_replaceable_p(symtab_node * e)398 lto_symtab_resolve_replaceable_p (symtab_node *e)
399 {
400   if (DECL_EXTERNAL (e->decl)
401       || DECL_COMDAT (e->decl)
402       || DECL_ONE_ONLY (e->decl)
403       || DECL_WEAK (e->decl))
404     return true;
405 
406   if (TREE_CODE (e->decl) == VAR_DECL)
407     return (DECL_COMMON (e->decl)
408 	    || (!flag_no_common && !DECL_INITIAL (e->decl)));
409 
410   return false;
411 }
412 
413 /* Return true, if the symbol E should be resolved by lto-symtab.
414    Those are all external symbols and all real symbols that are not static (we
415    handle renaming of static later in partitioning).  */
416 
417 static bool
lto_symtab_symbol_p(symtab_node * e)418 lto_symtab_symbol_p (symtab_node *e)
419 {
420   if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
421     return false;
422   return e->real_symbol_p ();
423 }
424 
425 /* Return true if the symtab entry E can be the prevailing one.  */
426 
427 static bool
lto_symtab_resolve_can_prevail_p(symtab_node * e)428 lto_symtab_resolve_can_prevail_p (symtab_node *e)
429 {
430   if (!lto_symtab_symbol_p (e))
431     return false;
432 
433   /* The C++ frontend ends up neither setting TREE_STATIC nor
434      DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
435      So do not reject !TREE_STATIC here but only DECL_EXTERNAL.  */
436   if (DECL_EXTERNAL (e->decl))
437     return false;
438 
439   return e->definition;
440 }
441 
442 /* Resolve the symbol with the candidates in the chain *SLOT and store
443    their resolutions.  */
444 
445 static symtab_node *
lto_symtab_resolve_symbols(symtab_node * first)446 lto_symtab_resolve_symbols (symtab_node *first)
447 {
448   symtab_node *e;
449   symtab_node *prevailing = NULL;
450 
451   /* Always set e->node so that edges are updated to reflect decl merging. */
452   for (e = first; e; e = e->next_sharing_asm_name)
453     if (lto_symtab_symbol_p (e)
454 	&& (e->resolution == LDPR_PREVAILING_DEF_IRONLY
455 	    || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
456 	    || e->resolution == LDPR_PREVAILING_DEF))
457       {
458 	prevailing = e;
459 	break;
460       }
461 
462   /* If the chain is already resolved there is nothing else to do.  */
463   if (prevailing)
464     {
465       /* Assert it's the only one.  */
466       for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
467 	if (lto_symtab_symbol_p (e)
468 	    && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
469 		|| e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
470 		|| e->resolution == LDPR_PREVAILING_DEF))
471 	  fatal_error (input_location, "multiple prevailing defs for %qE",
472 		       DECL_NAME (prevailing->decl));
473       return prevailing;
474     }
475 
476   /* Find the single non-replaceable prevailing symbol and
477      diagnose ODR violations.  */
478   for (e = first; e; e = e->next_sharing_asm_name)
479     {
480       if (!lto_symtab_resolve_can_prevail_p (e))
481 	continue;
482 
483       /* If we have a non-replaceable definition it prevails.  */
484       if (!lto_symtab_resolve_replaceable_p (e))
485 	{
486 	  if (prevailing)
487 	    {
488 	      error_at (DECL_SOURCE_LOCATION (e->decl),
489 			"%qD has already been defined", e->decl);
490 	      inform (DECL_SOURCE_LOCATION (prevailing->decl),
491 		      "previously defined here");
492 	    }
493 	  prevailing = e;
494 	}
495     }
496   if (prevailing)
497     return prevailing;
498 
499   /* Do a second round choosing one from the replaceable prevailing decls.  */
500   for (e = first; e; e = e->next_sharing_asm_name)
501     {
502       if (!lto_symtab_resolve_can_prevail_p (e))
503 	continue;
504 
505       /* Choose the first function that can prevail as prevailing.  */
506       if (TREE_CODE (e->decl) == FUNCTION_DECL)
507 	{
508 	  prevailing = e;
509 	  break;
510 	}
511 
512       /* From variables that can prevail choose the largest one.  */
513       if (!prevailing
514 	  || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
515 			      DECL_SIZE (e->decl))
516 	  /* When variables are equivalent try to chose one that has useful
517 	     DECL_INITIAL.  This makes sense for keyed vtables that are
518 	     DECL_EXTERNAL but initialized.  In units that do not need them
519 	     we replace the initializer by error_mark_node to conserve
520 	     memory.
521 
522 	     We know that the vtable is keyed outside the LTO unit - otherwise
523 	     the keyed instance would prevail.  We still can preserve useful
524 	     info in the initializer.  */
525 	  || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
526 	      && (DECL_INITIAL (e->decl)
527 		  && DECL_INITIAL (e->decl) != error_mark_node)
528 	      && (!DECL_INITIAL (prevailing->decl)
529 		  || DECL_INITIAL (prevailing->decl) == error_mark_node)))
530 	prevailing = e;
531     }
532 
533   return prevailing;
534 }
535 
536 /* Decide if it is OK to merge DECL into PREVAILING.
537    Because we wrap most of uses of declarations in MEM_REF, we can tolerate
538    some differences but other code may inspect directly the DECL.  */
539 
540 static bool
lto_symtab_merge_p(tree prevailing,tree decl)541 lto_symtab_merge_p (tree prevailing, tree decl)
542 {
543   if (TREE_CODE (prevailing) != TREE_CODE (decl))
544     {
545       if (symtab->dump_file)
546 	fprintf (symtab->dump_file, "Not merging decls; "
547 		 "TREE_CODE mismatch\n");
548       return false;
549     }
550   gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
551 
552   if (TREE_CODE (prevailing) == FUNCTION_DECL)
553     {
554       if (DECL_BUILT_IN (prevailing) != DECL_BUILT_IN (decl))
555 	{
556           if (symtab->dump_file)
557 	    fprintf (symtab->dump_file, "Not merging decls; "
558 		     "DECL_BUILT_IN mismatch\n");
559 	  return false;
560 	}
561       if (DECL_BUILT_IN (prevailing)
562 	  && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
563 	      || DECL_FUNCTION_CODE (prevailing) != DECL_FUNCTION_CODE (decl)))
564 	{
565           if (symtab->dump_file)
566 	    fprintf (symtab->dump_file, "Not merging decls; "
567 		     "DECL_BUILT_IN_CLASS or CODE mismatch\n");
568 	  return false;
569 	}
570     }
571 
572   /* FIXME: after MPX is removed, use flags_from_decl_or_type
573      function instead.  PR lto/85248.  */
574   if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
575     {
576       tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
577       tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
578       if ((prev_attr == NULL) != (attr == NULL)
579 	  || (prev_attr
580 	      && TREE_VALUE (TREE_VALUE (prev_attr))
581 		 != TREE_VALUE (TREE_VALUE (attr))))
582 	{
583           if (symtab->dump_file)
584 	    fprintf (symtab->dump_file, "Not merging decls; "
585 		     "error attribute mismatch\n");
586 	  return false;
587 	}
588 
589       prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
590       attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
591       if ((prev_attr == NULL) != (attr == NULL)
592 	  || (prev_attr
593 	      && TREE_VALUE (TREE_VALUE (prev_attr))
594 		 != TREE_VALUE (TREE_VALUE (attr))))
595 	{
596           if (symtab->dump_file)
597 	    fprintf (symtab->dump_file, "Not merging decls; "
598 		     "warning attribute mismatch\n");
599 	  return false;
600 	}
601 
602       prev_attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (prevailing));
603       attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (decl));
604       if ((prev_attr == NULL) != (attr == NULL))
605 	{
606           if (symtab->dump_file)
607 	    fprintf (symtab->dump_file, "Not merging decls; "
608 		     "noreturn attribute mismatch\n");
609 	  return false;
610 	}
611     }
612   return true;
613 }
614 
615 /* Merge all decls in the symbol table chain to the prevailing decl and
616    issue diagnostics about type mismatches.  If DIAGNOSED_P is true
617    do not issue further diagnostics.*/
618 
619 static void
lto_symtab_merge_decls_2(symtab_node * first,bool diagnosed_p)620 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
621 {
622   symtab_node *prevailing;
623   symtab_node *e;
624   vec<tree> mismatches = vNULL;
625   unsigned i;
626   tree decl;
627   bool tbaa_p = false;
628 
629   /* Nothing to do for a single entry.  */
630   prevailing = first;
631   if (!prevailing->next_sharing_asm_name)
632     return;
633 
634   /* Try to merge each entry with the prevailing one.  */
635   symtab_node *last_prevailing = prevailing, *next;
636   for (e = prevailing->next_sharing_asm_name; e; e = next)
637     {
638       next = e->next_sharing_asm_name;
639 
640       /* Skip non-LTO symbols and symbols whose declaration we already
641 	 visited.  */
642       if (lto_symtab_prevailing_decl (e->decl) != e->decl
643 	  || !lto_symtab_symbol_p (e)
644           || e->decl == prevailing->decl)
645 	continue;
646 
647       if (!lto_symtab_merge (prevailing, e)
648 	  && !diagnosed_p
649 	  && !DECL_ARTIFICIAL (e->decl))
650 	mismatches.safe_push (e->decl);
651 
652       symtab_node *this_prevailing;
653       for (this_prevailing = prevailing; ;
654 	   this_prevailing = this_prevailing->next_sharing_asm_name)
655 	{
656 	  if (this_prevailing->decl != e->decl
657 	      && lto_symtab_merge_p (this_prevailing->decl, e->decl))
658 	    break;
659 	  if (this_prevailing == last_prevailing)
660 	    {
661 	      this_prevailing = NULL;
662 	      break;
663 	    }
664 	}
665 
666       if (this_prevailing)
667 	lto_symtab_prevail_decl (this_prevailing->decl, e->decl);
668       /* Maintain LRU list: relink the new prevaililng symbol
669 	 just after previaling node in the chain and update last_prevailing.
670 	 Since the number of possible declarations of a given symbol is
671 	 small, this should be faster than building a hash.  */
672       else if (e == prevailing->next_sharing_asm_name)
673 	last_prevailing = e;
674       else
675 	{
676 	  if (e->next_sharing_asm_name)
677 	    e->next_sharing_asm_name->previous_sharing_asm_name
678 	      = e->previous_sharing_asm_name;
679 	  e->previous_sharing_asm_name->next_sharing_asm_name
680 	    = e->next_sharing_asm_name;
681 	  e->previous_sharing_asm_name = prevailing;
682 	  e->next_sharing_asm_name = prevailing->next_sharing_asm_name;
683 	  prevailing->next_sharing_asm_name->previous_sharing_asm_name = e;
684 	  prevailing->next_sharing_asm_name = e;
685 	  if (last_prevailing == prevailing)
686 	    last_prevailing = e;
687 	}
688     }
689   if (mismatches.is_empty ())
690     return;
691 
692   /* Diagnose all mismatched re-declarations.  */
693   FOR_EACH_VEC_ELT (mismatches, i, decl)
694     {
695       int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
696 					     TREE_TYPE (decl),
697 					     DECL_COMDAT (decl));
698       if (level)
699 	{
700 	  bool diag = false;
701 	  if (level & 2)
702 	    diag = warning_at (DECL_SOURCE_LOCATION (decl),
703 			       OPT_Wodr,
704 			       "%qD violates the C++ One Definition Rule ",
705 			       decl);
706 	  if (!diag && (level & 1))
707 	    diag = warning_at (DECL_SOURCE_LOCATION (decl),
708 			       OPT_Wlto_type_mismatch,
709 			       "type of %qD does not match original "
710 			       "declaration", decl);
711 	  if (diag)
712 	    {
713 	      warn_types_mismatch (TREE_TYPE (prevailing->decl),
714 				   TREE_TYPE (decl),
715 				   DECL_SOURCE_LOCATION (prevailing->decl),
716 				   DECL_SOURCE_LOCATION (decl));
717 	      if ((level & 4)
718 		  && !TREE_READONLY (prevailing->decl))
719 		tbaa_p = true;
720 	    }
721 	  diagnosed_p |= diag;
722 	}
723       else if ((DECL_USER_ALIGN (prevailing->decl)
724 	        && DECL_USER_ALIGN (decl))
725 	       && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
726 	{
727 	  diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
728 				     OPT_Wlto_type_mismatch,
729 				     "alignment of %qD is bigger than "
730 				     "original declaration", decl);
731 	}
732       else
733 	diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
734 				   OPT_Wlto_type_mismatch,
735 				   "size of %qD differ from the size of "
736 				   "original declaration", decl);
737     }
738   if (diagnosed_p)
739     inform (DECL_SOURCE_LOCATION (prevailing->decl),
740 	    "%qD was previously declared here", prevailing->decl);
741   if (tbaa_p)
742     inform (DECL_SOURCE_LOCATION (prevailing->decl),
743 	    "code may be misoptimized unless "
744 	    "-fno-strict-aliasing is used");
745 
746   mismatches.release ();
747 }
748 
749 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
750 
751 static void
lto_symtab_merge_decls_1(symtab_node * first)752 lto_symtab_merge_decls_1 (symtab_node *first)
753 {
754   symtab_node *e;
755   symtab_node *prevailing;
756   bool diagnosed_p = false;
757 
758   if (symtab->dump_file)
759     {
760       fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
761 	       first->asm_name ());
762       for (e = first; e; e = e->next_sharing_asm_name)
763 	if (TREE_PUBLIC (e->decl))
764 	  e->dump (symtab->dump_file);
765     }
766 
767   /* Compute the symbol resolutions.  This is a no-op when using the
768      linker plugin and resolution was decided by the linker.  */
769   prevailing = lto_symtab_resolve_symbols (first);
770 
771   /* If there's not a prevailing symbol yet it's an external reference.
772      Happens a lot during ltrans.  Choose the first symbol with a
773      cgraph or a varpool node.  */
774   if (!prevailing)
775     {
776       for (prevailing = first;
777 	   prevailing; prevailing = prevailing->next_sharing_asm_name)
778 	if (lto_symtab_symbol_p (prevailing))
779 	  break;
780       if (!prevailing)
781 	return;
782       /* For variables chose with a priority variant with vnode
783 	 attached (i.e. from unit where external declaration of
784 	 variable is actually used).
785 	 When there are multiple variants, chose one with size.
786 	 This is needed for C++ typeinfos, for example in
787 	 lto/20081204-1 there are typeifos in both units, just
788 	 one of them do have size.  */
789       if (TREE_CODE (prevailing->decl) == VAR_DECL)
790 	{
791 	  for (e = prevailing->next_sharing_asm_name;
792 	       e; e = e->next_sharing_asm_name)
793 	    if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
794 		&& COMPLETE_TYPE_P (TREE_TYPE (e->decl))
795 		&& lto_symtab_symbol_p (e))
796 	      prevailing = e;
797 	}
798       /* For functions prefer the non-builtin if one is available.  */
799       else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
800 	{
801 	  for (e = first; e; e = e->next_sharing_asm_name)
802 	    if (TREE_CODE (e->decl) == FUNCTION_DECL
803 		&& !DECL_BUILT_IN (e->decl)
804 		&& lto_symtab_symbol_p (e))
805 	      {
806 		prevailing = e;
807 		break;
808 	      }
809 	}
810     }
811 
812   symtab->symtab_prevail_in_asm_name_hash (prevailing);
813 
814   /* Diagnose mismatched objects.  */
815   for (e = prevailing->next_sharing_asm_name;
816        e; e = e->next_sharing_asm_name)
817     {
818       if (TREE_CODE (prevailing->decl)
819 	  == TREE_CODE (e->decl))
820 	continue;
821       if (!lto_symtab_symbol_p (e))
822 	continue;
823 
824       switch (TREE_CODE (prevailing->decl))
825 	{
826 	case VAR_DECL:
827 	  gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
828 	  error_at (DECL_SOURCE_LOCATION (e->decl),
829 		    "variable %qD redeclared as function",
830 		    prevailing->decl);
831 	  break;
832 
833 	case FUNCTION_DECL:
834 	  gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
835 	  error_at (DECL_SOURCE_LOCATION (e->decl),
836 		    "function %qD redeclared as variable",
837 		    prevailing->decl);
838 	  break;
839 
840 	default:
841 	  gcc_unreachable ();
842 	}
843 
844       diagnosed_p = true;
845     }
846   if (diagnosed_p)
847       inform (DECL_SOURCE_LOCATION (prevailing->decl),
848 	      "previously declared here");
849 
850   /* Merge the chain to the single prevailing decl and diagnose
851      mismatches.  */
852   lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
853 
854   if (symtab->dump_file)
855     {
856       fprintf (symtab->dump_file, "After resolution:\n");
857       for (e = prevailing; e; e = e->next_sharing_asm_name)
858 	e->dump (symtab->dump_file);
859     }
860 }
861 
862 /* Resolve and merge all symbol table chains to a prevailing decl.  */
863 
864 void
lto_symtab_merge_decls(void)865 lto_symtab_merge_decls (void)
866 {
867   symtab_node *node;
868 
869   /* Populate assembler name hash.   */
870   symtab->symtab_initialize_asm_name_hash ();
871 
872   FOR_EACH_SYMBOL (node)
873     if (!node->previous_sharing_asm_name
874 	&& node->next_sharing_asm_name)
875       lto_symtab_merge_decls_1 (node);
876 }
877 
878 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
879 
880 static void
lto_symtab_merge_symbols_1(symtab_node * prevailing)881 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
882 {
883   symtab_node *e;
884   symtab_node *next;
885 
886   prevailing->decl->decl_with_vis.symtab_node = prevailing;
887 
888   /* Replace the cgraph node of each entry with the prevailing one.  */
889   for (e = prevailing->next_sharing_asm_name; e;
890        e = next)
891     {
892       next = e->next_sharing_asm_name;
893 
894       if (!lto_symtab_symbol_p (e))
895 	continue;
896       cgraph_node *ce = dyn_cast <cgraph_node *> (e);
897       symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
898 
899       /* No matter how we are going to deal with resolution, we will ultimately
900 	 use prevailing definition.  */
901       if (ce)
902           ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
903 			      dyn_cast<cgraph_node *> (e));
904 
905       /* If we decided to replace the node by TO, do it.  */
906       if (e != to)
907 	{
908 	  if (ce)
909 	    lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
910 	  else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
911 	    lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
912 	}
913       /* Watch out for duplicated symbols for a given declaration.  */
914       else if (!e->transparent_alias
915 	       || !e->definition || e->get_alias_target () != to)
916 	{
917 	  /* We got a new declaration we do not want to merge.  In this case
918 	     get rid of the existing definition and create a transparent
919 	     alias.  */
920 	  if (ce)
921 	    {
922 	      lto_free_function_in_decl_state_for_node (ce);
923 	      if (!ce->weakref)
924 	        ce->release_body ();
925 	      ce->reset ();
926 	      symtab->call_cgraph_removal_hooks (ce);
927 	    }
928 	  else
929 	    {
930 	      DECL_INITIAL (e->decl) = error_mark_node;
931 	      if (e->lto_file_data)
932 		{
933 		  lto_free_function_in_decl_state_for_node (e);
934 		  e->lto_file_data = NULL;
935 		}
936 	      symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
937 	    }
938 	  e->remove_all_references ();
939 	  e->analyzed = e->body_removed = false;
940 	  e->resolve_alias (prevailing, true);
941 	  gcc_assert (e != prevailing);
942 	}
943     }
944 
945   return;
946 }
947 
948 /* Merge cgraph nodes according to the symbol merging done by
949    lto_symtab_merge_decls.  */
950 
951 void
lto_symtab_merge_symbols(void)952 lto_symtab_merge_symbols (void)
953 {
954   symtab_node *node;
955 
956   if (!flag_ltrans)
957     {
958       symtab->symtab_initialize_asm_name_hash ();
959 
960       /* Do the actual merging.
961          At this point we invalidate hash translating decls into symtab nodes
962 	 because after removing one of duplicate decls the hash is not correcly
963 	 updated to the ohter dupliate.  */
964       FOR_EACH_SYMBOL (node)
965 	if (lto_symtab_symbol_p (node)
966 	    && node->next_sharing_asm_name
967 	    && !node->previous_sharing_asm_name)
968 	  lto_symtab_merge_symbols_1 (node);
969 
970       /* Resolve weakref aliases whose target are now in the compilation unit.
971 	 also re-populate the hash translating decls into symtab nodes*/
972       FOR_EACH_SYMBOL (node)
973 	{
974 	  cgraph_node *cnode, *cnode2;
975 	  varpool_node *vnode;
976 	  symtab_node *node2;
977 
978 	  if (!node->analyzed && node->alias_target)
979 	    {
980 	      symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
981 	      gcc_assert (node->weakref);
982 	      if (tgt)
983 		node->resolve_alias (tgt, true);
984 	    }
985 
986 	  if (!(cnode = dyn_cast <cgraph_node *> (node))
987 	      || !cnode->clone_of
988 	      || cnode->clone_of->decl != cnode->decl)
989 	    {
990 	      /* Builtins are not merged via decl merging.  It is however
991 		 possible that tree merging unified the declaration.  We
992 		 do not want duplicate entries in symbol table.  */
993 	      if (cnode && DECL_BUILT_IN (node->decl)
994 		  && (cnode2 = cgraph_node::get (node->decl))
995 		  && cnode2 != cnode)
996 		lto_cgraph_replace_node (cnode2, cnode);
997 
998 	      /* The user defined assembler variables are also not unified by their
999 		 symbol name (since it is irrelevant), but we need to unify symbol
1000 		 nodes if tree merging occured.  */
1001 	      if ((vnode = dyn_cast <varpool_node *> (node))
1002 		  && DECL_HARD_REGISTER (vnode->decl)
1003 		  && (node2 = symtab_node::get (vnode->decl))
1004 		  && node2 != node)
1005 		lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
1006 					  vnode);
1007 
1008 
1009 	      /* Abstract functions may have duplicated cgraph nodes attached;
1010 		 remove them.  */
1011 	      else if (cnode && DECL_ABSTRACT_P (cnode->decl)
1012 		       && (cnode2 = cgraph_node::get (node->decl))
1013 		       && cnode2 != cnode)
1014 		cnode2->remove ();
1015 
1016 	      node->decl->decl_with_vis.symtab_node = node;
1017 	    }
1018 	}
1019     }
1020 }
1021 
1022 /* Virtual tables may matter for code generation even if they are not
1023    directly refernced by the code because they may be used for devirtualizaiton.
1024    For this reason it is important to merge even virtual tables that have no
1025    associated symbol table entries.  Without doing so we lose optimization
1026    oppurtunities by losing track of the vtable constructor.
1027    FIXME: we probably ought to introduce explicit symbol table entries for
1028    those before streaming.  */
1029 
1030 tree
lto_symtab_prevailing_virtual_decl(tree decl)1031 lto_symtab_prevailing_virtual_decl (tree decl)
1032 {
1033   if (DECL_ABSTRACT_P (decl))
1034     return decl;
1035   gcc_checking_assert (!type_in_anonymous_namespace_p (DECL_CONTEXT (decl))
1036 		       && DECL_ASSEMBLER_NAME_SET_P (decl));
1037 
1038   symtab_node *n = symtab_node::get_for_asmname
1039 		     (DECL_ASSEMBLER_NAME (decl));
1040   while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
1041 	       || !DECL_VIRTUAL_P (n->decl)))
1042     n = n->next_sharing_asm_name;
1043   if (n)
1044     {
1045       /* Merge decl state in both directions, we may still end up using
1046 	 the other decl.  */
1047       TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
1048       TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
1049 
1050       if (TREE_CODE (decl) == FUNCTION_DECL)
1051 	{
1052 	  /* Merge decl state in both directions, we may still end up using
1053 	     the other decl.  */
1054 	  DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
1055 	  DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
1056 	}
1057       lto_symtab_prevail_decl (n->decl, decl);
1058       decl = n->decl;
1059     }
1060   else
1061     symtab_node::get_create (decl);
1062 
1063   return decl;
1064 }
1065