From 8d8ffd86d73d2dfc8b2a631261d39571f6f9a97c Mon Sep 17 00:00:00 2001 From: Cary Coutant Date: Fri, 25 Apr 2014 23:05:14 +0000 Subject: [PATCH] Fix a few ICEs and other problems with -fdebug-types-sections. (1) If a function contains a local typedef of an anonymous structure, GCC will generate a typedef DIE in the function, where the typedef DIE points to a structure_type DIE at the top level. That structure_type DIE, if it's a non-POD, can contain ctor and dtor definitions. This causes an assertion in should_move_die_to_comdat to fail, as we have up to now assumed that this could never happen. (2) With --std=c++11, a template parameter can refer to a local type defined within a function. Because that local type doesn't qualify for its own type unit, we copy it as an "unworthy" type into the type unit that refers to it, but we copy too much, leading to a comdat type unit that contains a DIE with subprogram definitions rather than declarations. These DIEs may have DW_AT_low_pc/high_pc or DW_AT_ranges attributes, and consequently can refer to range list entries that don't get emitted because they're not marked when the compile unit is scanned, eventually causing an undefined symbol at link time. (3) When a class template instantiation is moved into a separate type unit, it can bring along a lot of other referenced types into the type unit, especially if the template is derived from another (large) type that does not have an actually have a type definition in a type unit of its own. When there are many instantiations of the same template, we get a lot of duplication, and in the worst case (a template with several parameters, instantiated multiple times along each dimension), GCC can end up taking a long time and exhausting available memory. This combinatorial explosion is being caused by copy_decls_walk, where it finds a type DIE that is referenced by the type unit, but is not itself a type unit, and copies a declaration for that type into the type unit in order to resolve the reference within the type unit. In the process, copy_decls_walk also copies all of the children of that DIE. In the case of a base class with member function templates, every one of the instantiated member functions is copied into every type unit that references the base class. gcc/ PR debug/60929 * dwarf2out.c (should_move_die_to_comdat): A type definition can contain a subprogram definition, but don't move it to a comdat unit. (clone_as_declaration): Copy DW_AT_abstract_origin attribute. (generate_skeleton_bottom_up): Remove DW_AT_object_pointer attribute from original DIE. (clone_tree_hash): Rename to... (clone_tree_partial): ...this; change callers. Copy DW_TAG_subprogram DIEs as declarations. (copy_decls_walk): Don't copy children of a declaration into a type unit. gcc/testsuite/ PR debug/60929 * g++.dg/debug/dwarf2/dwarf4-nested.C: New test case. * g++.dg/debug/dwarf2/dwarf4-typedef.C: Add -fdebug-types-section flag. From-SVN: r209812 --- gcc/ChangeLog | 15 +++++ gcc/dwarf2out.c | 56 +++++++++++++------ gcc/testsuite/ChangeLog | 7 +++ .../g++.dg/debug/dwarf2/dwarf4-nested.C | 55 ++++++++++++++++++ .../g++.dg/debug/dwarf2/dwarf4-typedef.C | 2 +- 5 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 gcc/testsuite/g++.dg/debug/dwarf2/dwarf4-nested.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c0cb9939f80..ccb79643242 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,18 @@ +2014-04-25 Cary Coutant + + PR debug/60929 + * dwarf2out.c (should_move_die_to_comdat): A type definition + can contain a subprogram definition, but don't move it to a + comdat unit. + (clone_as_declaration): Copy DW_AT_abstract_origin attribute. + (generate_skeleton_bottom_up): Remove DW_AT_object_pointer attribute + from original DIE. + (clone_tree_hash): Rename to... + (clone_tree_partial): ...this; change callers. Copy + DW_TAG_subprogram DIEs as declarations. + (copy_decls_walk): Don't copy children of a declaration into a + type unit. + 2014-04-25 H.J. Lu PR target/60969 diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c index 8caf940c69e..12723265e87 100644 --- a/gcc/dwarf2out.c +++ b/gcc/dwarf2out.c @@ -6830,14 +6830,13 @@ should_move_die_to_comdat (dw_die_ref die) case DW_TAG_structure_type: case DW_TAG_enumeration_type: case DW_TAG_union_type: - /* Don't move declarations, inlined instances, or types nested in a - subprogram. */ + /* Don't move declarations, inlined instances, types nested in a + subprogram, or types that contain subprogram definitions. */ if (is_declaration_die (die) || get_AT (die, DW_AT_abstract_origin) - || is_nested_in_subprogram (die)) + || is_nested_in_subprogram (die) + || contains_subprogram_definition (die)) return 0; - /* A type definition should never contain a subprogram definition. */ - gcc_assert (!contains_subprogram_definition (die)); return 1; case DW_TAG_array_type: case DW_TAG_interface_type: @@ -6926,6 +6925,7 @@ clone_as_declaration (dw_die_ref die) switch (a->dw_attr) { + case DW_AT_abstract_origin: case DW_AT_artificial: case DW_AT_containing_type: case DW_AT_external: @@ -7158,6 +7158,12 @@ generate_skeleton_bottom_up (skeleton_chain_node *parent) dw_die_ref clone = clone_die (c); move_all_children (c, clone); + /* If the original has a DW_AT_object_pointer attribute, + it would now point to a child DIE just moved to the + cloned tree, so we need to remove that attribute from + the original. */ + remove_AT (c, DW_AT_object_pointer); + replace_child (c, clone, prev); generate_skeleton_ancestor_tree (parent); add_child_die (parent->new_die, c); @@ -7299,28 +7305,38 @@ break_out_comdat_types (dw_die_ref die) } while (next != NULL); } -/* Like clone_tree, but additionally enter all the children into - the hash table decl_table. */ +/* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations. + Enter all the cloned children into the hash table decl_table. */ static dw_die_ref -clone_tree_hash (dw_die_ref die, decl_hash_type decl_table) +clone_tree_partial (dw_die_ref die, decl_hash_type decl_table) { dw_die_ref c; - dw_die_ref clone = clone_die (die); + dw_die_ref clone; struct decl_table_entry *entry; - decl_table_entry **slot = decl_table.find_slot_with_hash (die, - htab_hash_pointer (die), INSERT); + decl_table_entry **slot; + + if (die->die_tag == DW_TAG_subprogram) + clone = clone_as_declaration (die); + else + clone = clone_die (die); + + slot = decl_table.find_slot_with_hash (die, + htab_hash_pointer (die), INSERT); + /* Assert that DIE isn't in the hash table yet. If it would be there before, the ancestors would be necessarily there as well, therefore - clone_tree_hash wouldn't be called. */ + clone_tree_partial wouldn't be called. */ gcc_assert (*slot == HTAB_EMPTY_ENTRY); + entry = XCNEW (struct decl_table_entry); entry->orig = die; entry->copy = clone; *slot = entry; - FOR_EACH_CHILD (die, c, - add_child_die (clone, clone_tree_hash (c, decl_table))); + if (die->die_tag != DW_TAG_subprogram) + FOR_EACH_CHILD (die, c, + add_child_die (clone, clone_tree_partial (c, decl_table))); return clone; } @@ -7371,9 +7387,15 @@ copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table) entry->copy = copy; *slot = entry; - FOR_EACH_CHILD (targ, c, - add_child_die (copy, - clone_tree_hash (c, decl_table))); + /* If TARG is not a declaration DIE, we need to copy its + children. */ + if (!is_declaration_die (targ)) + { + FOR_EACH_CHILD ( + targ, c, + add_child_die (copy, + clone_tree_partial (c, decl_table))); + } /* Make sure the cloned tree is marked as part of the type unit. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 88681c8d698..92a33c1102c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2014-04-25 Cary Coutant + + PR debug/60929 + * g++.dg/debug/dwarf2/dwarf4-nested.C: New test case. + * g++.dg/debug/dwarf2/dwarf4-typedef.C: Add + -fdebug-types-section flag. + 2014-04-25 Jiong Wang * gcc.target/arm/tail-long-call.c: New test. diff --git a/gcc/testsuite/g++.dg/debug/dwarf2/dwarf4-nested.C b/gcc/testsuite/g++.dg/debug/dwarf2/dwarf4-nested.C new file mode 100644 index 00000000000..160694c3c9f --- /dev/null +++ b/gcc/testsuite/g++.dg/debug/dwarf2/dwarf4-nested.C @@ -0,0 +1,55 @@ +// { dg-do compile } +// { dg-options "--std=c++11 -dA -gdwarf-4 -fdebug-types-section -fno-merge-debug-strings" } + +// Check that -fdebug-types-sections does not copy a full referenced type +// into a type unit. + +// Checks that at least one type unit is generated. +// +// { dg-final { scan-assembler "DIE \\(\[^\n\]*\\) DW_TAG_type_unit" } } +// +// Check that func is declared exactly once in the debug info (in the +// compile unit). +// +// { dg-final { scan-assembler-times "\\.ascii \"func\\\\0\"\[^\n\]*DW_AT_name" 1 } } +// +// Check to make sure that no type unit contains a DIE with DW_AT_low_pc +// or DW_AT_ranges. These patterns assume that the compile unit is always +// emitted after all type units. +// +// { dg-final { scan-assembler-not "\\.quad\[^\n\]*DW_AT_low_pc.*DIE \\(\[^\n\]*\\) DW_TAG_compile_unit" } } +// { dg-final { scan-assembler-not "\\.quad\[^\n\]*DW_AT_ranges.*DIE \\(\[^\n\]*\\) DW_TAG_compile_unit" } } + +struct A { + A(); + virtual ~A(); + virtual void foo(); + private: + int data; +}; + +struct B { + B(); + virtual ~B(); +}; + +extern B* table[]; + +struct D { + template + T* get(int i) + { + B*& cell = table[i]; + if (cell == 0) + cell = new T(); + return static_cast(cell); + } +}; + +void func(D* d) +{ + struct C : B { + A a; + }; + d->get(0)->a.foo(); +} diff --git a/gcc/testsuite/g++.dg/debug/dwarf2/dwarf4-typedef.C b/gcc/testsuite/g++.dg/debug/dwarf2/dwarf4-typedef.C index c5520fa72b0..89a6bb44e10 100644 --- a/gcc/testsuite/g++.dg/debug/dwarf2/dwarf4-typedef.C +++ b/gcc/testsuite/g++.dg/debug/dwarf2/dwarf4-typedef.C @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-gdwarf-4" } */ +/* { dg-options "-gdwarf-4 -fdebug-types-section" } */ /* Regression test for an ICE in output_die when using -gdwarf-4. */ -- 2.30.2