[v2] gdb: dwarf2read: implement handling of variables in Fortran common block
Commit Message
This patch allows the gdb DWARF reader to handle variables located in Fortran common blocks. This fixes 20 unexpected failures in the gdb.fortran testsuite with host, target and build triplet x86_64-apple-darwin13.4.0 with gfortran 4.9.2.
Re-posted as requested with patch file attached. Revised from original post to prevent double relocation.
2015-04-12 George Helffrich <ghfbsd@gmail.com>
* dwarf2read.c (read_common_block): Relocate symbols in Fortran common
blocks properly, and avoid double relocation.
(read_new_symbol_full): Don't discard zero-offset symbols in Fortran
common blocks.
(_initialize_dwarf2_read): Add debug diagnostic print for common
symbols.
* psymtab.c (print_partial_symbols): Recognize and print
COMMON_BLOCK_DOMAIN.
* symmisc.c (print_symbol): Recognize and dump LOC_COMMON_BLOCK.
———
———
--- dwarf2read.c.orig 2015-02-21 02:11:44.000000000 +0900
+++ dwarf2read.c 2016-04-06 18:50:34.000000000 +0900
@@ -84,6 +84,9 @@ static unsigned int dwarf2_read_debug =
/* When non-zero, dump DIEs after they are read in. */
static unsigned int dwarf2_die_debug = 0;
+/* When non-zero, report definition of common block variables. */
+static unsigned int dwarf2_common_debug = 0;
+
/* When non-zero, cross-check physname against demangler. */
static int check_physname = 0;
@@ -13877,6 +13880,57 @@ static void
read_common_block (struct die_info *die, struct dwarf2_cu *cu)
{
struct attribute *attr;
+ CORE_ADDR baseaddr;
+ struct program_space *psp;
+ struct minimal_symbol *msym = NULL;
+ struct objfile *objfile = cu->objfile;
+ struct gdbarch *gdbarch = get_objfile_arch (objfile);
+ const char *name;
+
+ attr = dwarf2_attr (die, DW_AT_name, cu);
+ name = dwarf2_compute_name (NULL, die, cu, 1);
+ if (dwarf2_common_debug)
+ {
+ fprintf_unfiltered (gdb_stdlog,
+ "Read common blk %s (%s)\n",
+ DW_STRING(attr), name);
+ }
+ ALL_PSPACES (psp)
+ ALL_PSPACE_OBJFILES (psp, objfile)
+ if (objfile->flags & OBJF_MAINLINE) goto found;
+found:
+ if (objfile->flags & OBJF_MAINLINE)
+ ALL_OBJFILE_MSYMBOLS (objfile, msym)
+ {
+ struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msym);
+
+ if (0 == strcmp(MSYMBOL_LINKAGE_NAME (msym), name))
+ {
+ if (dwarf2_common_debug)
+ {
+ fprintf_unfiltered (gdb_stdlog, " Found %s at ", name);
+ fputs_filtered (paddress (gdbarch,
+ MSYMBOL_VALUE_ADDRESS (objfile, msym)),
+ gdb_stdlog);
+ }
+ if (section && dwarf2_common_debug)
+ {
+ if (section->the_bfd_section != NULL)
+ fprintf_filtered (gdb_stdlog, " section %s",
+ bfd_section_name (objfile->obfd,
+ section->the_bfd_section));
+ else
+ fprintf_filtered (gdb_stdlog, " spurious section %ld",
+ (long) (section - objfile->sections));
+ }
+ if (dwarf2_common_debug)
+ fprintf_unfiltered (gdb_stdlog, "\n");
+
+ /* Found base of common, now use it to relocate symbols in it */
+ baseaddr = MSYMBOL_VALUE_ADDRESS (objfile, msym);
+ break;
+ }
+ }
attr = dwarf2_attr (die, DW_AT_location, cu);
if (attr)
@@ -13906,6 +13960,7 @@ read_common_block (struct die_info *die,
size_t n_entries = 0, size;
struct common_block *common_block;
struct symbol *sym;
+ int needs_reloc = 0;
for (child_die = die->child;
child_die && child_die->tag;
@@ -13929,6 +13984,9 @@ read_common_block (struct die_info *die,
{
struct attribute *member_loc;
+ if (common_block->n_entries == 0)
+ needs_reloc = SYMBOL_VALUE_ADDRESS (sym) == 0;
+
common_block->contents[common_block->n_entries++] = sym;
member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
@@ -13958,6 +14016,34 @@ read_common_block (struct die_info *die,
else
dwarf2_complex_location_expr_complaint ();
}
+
+ if (dwarf2_common_debug)
+ {
+ struct attribute *name = dwarf2_attr (child_die, DW_AT_name, cu);
+ fprintf_unfiltered (gdb_stdlog,
+ " Define common blk var %s at ",
+ DW_STRING(name));
+ fputs_filtered (paddress (gdbarch,
+ SYMBOL_VALUE_ADDRESS (sym)),
+ gdb_stdlog);
+ if (needs_reloc)
+ {
+ fputs_filtered (" -> ", gdb_stdlog);
+ if (msym)
+ fputs_filtered (paddress (gdbarch,
+ SYMBOL_VALUE_ADDRESS (sym)
+ + baseaddr),
+ gdb_stdlog);
+ else
+ fputs_filtered ("(unknown)", gdb_stdlog);
+ }
+ fputs_filtered ("\n", gdb_stdlog);
+ }
+
+ /* Relocate symbol in common block */
+ if (msym && needs_reloc)
+ SYMBOL_VALUE_ADDRESS (sym) += baseaddr;
+
}
}
@@ -18165,6 +18251,8 @@ new_symbol_full (struct die_info *die, s
attr = dwarf2_attr (die, DW_AT_location, cu);
if (attr)
{
+ unsigned int fortran_common_symbol = 0;
+
var_decode_location (attr, sym, cu);
attr2 = dwarf2_attr (die, DW_AT_external, cu);
@@ -18172,16 +18260,23 @@ new_symbol_full (struct die_info *die, s
scope by DW_TAG_common_block. */
if (cu->language == language_fortran && die->parent
&& die->parent->tag == DW_TAG_common_block)
- attr2 = NULL;
+ {
+ attr2 = NULL;
+ fortran_common_symbol = 1;
+ }
if (SYMBOL_CLASS (sym) == LOC_STATIC
&& SYMBOL_VALUE_ADDRESS (sym) == 0
+ && !fortran_common_symbol
&& !dwarf2_per_objfile->has_section_at_zero)
{
/* When a static variable is eliminated by the linker,
the corresponding debug information is not stripped
out, but the variable address is set to null;
- do not add such variables into symbol table. */
+ do not add such variables into symbol table.
+
+ An exception is Fortran, for which the first symbol
+ in a common block will have address/offset 0. */
}
else if (attr2 && (DW_UNSND (attr2) != 0))
{
@@ -23195,6 +23290,16 @@ The value is the maximum depth to print.
NULL,
&setdebuglist, &showdebuglist);
+ add_setshow_zuinteger_cmd ("dwarf2-common",
+ no_class, &dwarf2_common_debug, _("\
+Set debugging of dwarf2 common block handling."), _("\
+Show debugging of dwarf2 common block handling."), _("\
+When enabled (non-zero), common block variables are printed as they are\n\
+are relocated to their position in the common section."),
+ NULL,
+ NULL,
+ &setdebuglist, &showdebuglist);
+
add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
Set cross-checking of \"physname\" code against demangler."), _("\
Show cross-checking of \"physname\" code against demangler."), _("\
--- symmisc.c.orig 2015-02-21 02:11:44.000000000 +0900
+++ symmisc.c 2016-03-27 18:46:17.000000000 +0900
@@ -626,6 +626,15 @@ print_symbol (void *args)
fprintf_filtered (outfile, "optimized out");
break;
+ case LOC_COMMON_BLOCK:
+ fprintf_filtered (outfile, "common block storage");
+
+ if (section)
+ fprintf_filtered (outfile, " section %s",
+ bfd_section_name (section->the_bfd_section->owner,
+ section->the_bfd_section));
+ break;
+
default:
fprintf_filtered (outfile, "botched symbol class %x",
SYMBOL_CLASS (symbol));
--- psymtab.c.orig 2015-02-21 02:11:44.000000000 +0900
+++ psymtab.c 2016-03-27 18:13:25.000000000 +0900
@@ -915,6 +915,9 @@ print_partial_symbols (struct gdbarch *g
case LABEL_DOMAIN:
fputs_filtered ("label domain, ", outfile);
break;
+ case COMMON_BLOCK_DOMAIN:
+ fputs_filtered ("common block domain, ", outfile);
+ break;
default:
fputs_filtered ("<invalid domain>, ", outfile);
break;
G Helffrich
ghfbsd@gmail.com
Comments
On 12/04/2016 02:31, G Helffrich wrote:
> This patch allows the gdb DWARF reader to handle variables located in Fortran common blocks. This fixes 20 unexpected failures in the gdb.fortran testsuite with host, target and build triplet x86_64-apple-darwin13.4.0 with gfortran 4.9.2.
> Re-posted as requested with patch file attached. Revised from original post to prevent double relocation.
> 2015-04-12 George Helffrich <ghfbsd@gmail.com>
>
> * dwarf2read.c (read_common_block): Relocate symbols in Fortran common
> blocks properly, and avoid double relocation.
> (read_new_symbol_full): Don't discard zero-offset symbols in Fortran
> common blocks.
> (_initialize_dwarf2_read): Add debug diagnostic print for common
> symbols.
> * psymtab.c (print_partial_symbols): Recognize and print
> COMMON_BLOCK_DOMAIN.
> * symmisc.c (print_symbol): Recognize and dump LOC_COMMON_BLOCK.
>
>
Hi George, Luis,
I manually patched last week and got 16 fails in the common block testcase.
Just adding a base-address to the variables lead to failing tests.
From what I have seen, all the child's of the common block have their
own fix (DW_OP_addr) location.
At least in my environment when I check the DWARF of the compiled testcase.
Here an excerpt of my DWARF
<2><4a>: Abbrev Number: 3 (DW_TAG_common_block)
<4b> DW_AT_name : (indirect string, offset: 0x0): fo_o
<4f> DW_AT_decl_file : 1
<50> DW_AT_decl_line : 31
<51> DW_AT_linkage_name: (indirect string, offset: 0x5): fo_o_
<55> DW_AT_location : 9 byte block: 3 60 10 60 0 0 0 0 0
(DW_OP_addr: 601060)
<5f> DW_AT_sibling : <0xa1>
<3><63>: Abbrev Number: 4 (DW_TAG_variable)
<64> DW_AT_name : ix
<67> DW_AT_decl_file : 1
<68> DW_AT_decl_line : 23
<69> DW_AT_type : <0x116>
<6d> DW_AT_external : 1
<6d> DW_AT_location : 9 byte block: 3 60 10 60 0 0 0 0 0
(DW_OP_addr: 601060)
<3><77>: Abbrev Number: 4 (DW_TAG_variable)
<78> DW_AT_name : iy2
<7c> DW_AT_decl_file : 1
<7d> DW_AT_decl_line : 24
<7e> DW_AT_type : <0x11d>
<82> DW_AT_external : 1
<82> DW_AT_location : 9 byte block: 3 64 10 60 0 0 0 0 0
(DW_OP_addr: 601064)
May George can sent the important part of the DWARF
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
Bernhard & Luis -
Here is the dump of the analogous parts of the DWARF for my environment for this testcase:
0x00000155: TAG_common_block [3] *
AT_name( "fo_o" )
AT_decl_file( "gdb.fortran/common-block.f90" )
AT_decl_line( 31 )
AT_MIPS_linkage_name( "fo_o_" )
AT_location( [0x0000000000000000] )
AT_sibling( {0x000001b2} )
0x00000171: TAG_variable [4]
AT_name( "ix" )
AT_decl_file( "gdb.fortran/common-block.f90" )
AT_decl_line( 23 )
AT_type( {0x00000230} ( integer(kind=4) ) )
AT_external( 0x01 )
AT_location( [0x0000000000000000] )
0x00000186: TAG_variable [4]
AT_name( "iy2" )
AT_decl_file( "gdb.fortran/common-block.f90" )
AT_decl_line( 24 )
AT_type( {0x00000243} ( real(kind=4) ) )
AT_external( 0x01 )
AT_location( [0x0000000000000004] )
On 12 Apr 2016, at 16:00, Heckel, Bernhard <bernhard.heckel@intel.com> wrote:
> On 12/04/2016 02:31, G Helffrich wrote:
>> This patch allows the gdb DWARF reader to handle variables located in Fortran common blocks. This fixes 20 unexpected failures in the gdb.fortran testsuite with host, target and build triplet x86_64-apple-darwin13.4.0 with gfortran 4.9.2.
>> Re-posted as requested with patch file attached. Revised from original post to prevent double relocation.
>> 2015-04-12 George Helffrich <ghfbsd@gmail.com>
>>
>> * dwarf2read.c (read_common_block): Relocate symbols in Fortran common
>> blocks properly, and avoid double relocation.
>> (read_new_symbol_full): Don't discard zero-offset symbols in Fortran
>> common blocks.
>> (_initialize_dwarf2_read): Add debug diagnostic print for common
>> symbols.
>> * psymtab.c (print_partial_symbols): Recognize and print
>> COMMON_BLOCK_DOMAIN.
>> * symmisc.c (print_symbol): Recognize and dump LOC_COMMON_BLOCK.
>>
>>
> Hi George, Luis,
>
> I manually patched last week and got 16 fails in the common block testcase.
> Just adding a base-address to the variables lead to failing tests.
> From what I have seen, all the child's of the common block have their own fix (DW_OP_addr) location.
> At least in my environment when I check the DWARF of the compiled testcase.
>
> Here an excerpt of my DWARF
> <2><4a>: Abbrev Number: 3 (DW_TAG_common_block)
> <4b> DW_AT_name : (indirect string, offset: 0x0): fo_o
> <4f> DW_AT_decl_file : 1
> <50> DW_AT_decl_line : 31
> <51> DW_AT_linkage_name: (indirect string, offset: 0x5): fo_o_
> <55> DW_AT_location : 9 byte block: 3 60 10 60 0 0 0 0 0 (DW_OP_addr: 601060)
> <5f> DW_AT_sibling : <0xa1>
> <3><63>: Abbrev Number: 4 (DW_TAG_variable)
> <64> DW_AT_name : ix
> <67> DW_AT_decl_file : 1
> <68> DW_AT_decl_line : 23
> <69> DW_AT_type : <0x116>
> <6d> DW_AT_external : 1
> <6d> DW_AT_location : 9 byte block: 3 60 10 60 0 0 0 0 0 (DW_OP_addr: 601060)
> <3><77>: Abbrev Number: 4 (DW_TAG_variable)
> <78> DW_AT_name : iy2
> <7c> DW_AT_decl_file : 1
> <7d> DW_AT_decl_line : 24
> <7e> DW_AT_type : <0x11d>
> <82> DW_AT_external : 1
> <82> DW_AT_location : 9 byte block: 3 64 10 60 0 0 0 0 0 (DW_OP_addr: 601064)
>
> May George can sent the important part of the DWARF
>
>
> Intel Deutschland GmbH
> Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
> Tel: +49 89 99 8853-0, www.intel.de
> Managing Directors: Christin Eisenschmid, Christian Lamprechter
> Chairperson of the Supervisory Board: Nicole Lau
> Registered Office: Munich
> Commercial Register: Amtsgericht Muenchen HRB 186928
>
G Helffrich
ghfbsd@gmail.com
@@ -84,6 +84,9 @@ static unsigned int dwarf2_read_debug =
/* When non-zero, dump DIEs after they are read in. */
static unsigned int dwarf2_die_debug = 0;
+/* When non-zero, report definition of common block variables. */
+static unsigned int dwarf2_common_debug = 0;
+
/* When non-zero, cross-check physname against demangler. */
static int check_physname = 0;
@@ -13877,6 +13880,57 @@ static void
read_common_block (struct die_info *die, struct dwarf2_cu *cu)
{
struct attribute *attr;
+ CORE_ADDR baseaddr;
+ struct program_space *psp;
+ struct minimal_symbol *msym = NULL;
+ struct objfile *objfile = cu->objfile;
+ struct gdbarch *gdbarch = get_objfile_arch (objfile);
+ const char *name;
+
+ attr = dwarf2_attr (die, DW_AT_name, cu);
+ name = dwarf2_compute_name (NULL, die, cu, 1);
+ if (dwarf2_common_debug)
+ {
+ fprintf_unfiltered (gdb_stdlog,
+ "Read common blk %s (%s)\n",
+ DW_STRING(attr), name);
+ }
+ ALL_PSPACES (psp)
+ ALL_PSPACE_OBJFILES (psp, objfile)
+ if (objfile->flags & OBJF_MAINLINE) goto found;
+found:
+ if (objfile->flags & OBJF_MAINLINE)
+ ALL_OBJFILE_MSYMBOLS (objfile, msym)
+ {
+ struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msym);
+
+ if (0 == strcmp(MSYMBOL_LINKAGE_NAME (msym), name))
+ {
+ if (dwarf2_common_debug)
+ {
+ fprintf_unfiltered (gdb_stdlog, " Found %s at ", name);
+ fputs_filtered (paddress (gdbarch,
+ MSYMBOL_VALUE_ADDRESS (objfile, msym)),
+ gdb_stdlog);
+ }
+ if (section && dwarf2_common_debug)
+ {
+ if (section->the_bfd_section != NULL)
+ fprintf_filtered (gdb_stdlog, " section %s",
+ bfd_section_name (objfile->obfd,
+ section->the_bfd_section));
+ else
+ fprintf_filtered (gdb_stdlog, " spurious section %ld",
+ (long) (section - objfile->sections));
+ }
+ if (dwarf2_common_debug)
+ fprintf_unfiltered (gdb_stdlog, "\n");
+
+ /* Found base of common, now use it to relocate symbols in it */
+ baseaddr = MSYMBOL_VALUE_ADDRESS (objfile, msym);
+ break;
+ }
+ }
attr = dwarf2_attr (die, DW_AT_location, cu);
if (attr)
@@ -13906,6 +13960,7 @@ read_common_block (struct die_info *die,
size_t n_entries = 0, size;
struct common_block *common_block;
struct symbol *sym;
+ int needs_reloc = 0;
for (child_die = die->child;
child_die && child_die->tag;
@@ -13929,6 +13984,9 @@ read_common_block (struct die_info *die,
{
struct attribute *member_loc;
+ if (common_block->n_entries == 0)
+ needs_reloc = SYMBOL_VALUE_ADDRESS (sym) == 0;
+
common_block->contents[common_block->n_entries++] = sym;
member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
@@ -13958,6 +14016,34 @@ read_common_block (struct die_info *die,
else
dwarf2_complex_location_expr_complaint ();
}
+
+ if (dwarf2_common_debug)
+ {
+ struct attribute *name = dwarf2_attr (child_die, DW_AT_name, cu);
+ fprintf_unfiltered (gdb_stdlog,
+ " Define common blk var %s at ",
+ DW_STRING(name));
+ fputs_filtered (paddress (gdbarch,
+ SYMBOL_VALUE_ADDRESS (sym)),
+ gdb_stdlog);
+ if (needs_reloc)
+ {
+ fputs_filtered (" -> ", gdb_stdlog);
+ if (msym)
+ fputs_filtered (paddress (gdbarch,
+ SYMBOL_VALUE_ADDRESS (sym)
+ + baseaddr),
+ gdb_stdlog);
+ else
+ fputs_filtered ("(unknown)", gdb_stdlog);
+ }
+ fputs_filtered ("\n", gdb_stdlog);
+ }
+
+ /* Relocate symbol in common block */
+ if (msym && needs_reloc)
+ SYMBOL_VALUE_ADDRESS (sym) += baseaddr;
+
}
}
@@ -18165,6 +18251,8 @@ new_symbol_full (struct die_info *die, s
attr = dwarf2_attr (die, DW_AT_location, cu);
if (attr)
{
+ unsigned int fortran_common_symbol = 0;
+
var_decode_location (attr, sym, cu);
attr2 = dwarf2_attr (die, DW_AT_external, cu);
@@ -18172,16 +18260,23 @@ new_symbol_full (struct die_info *die, s
scope by DW_TAG_common_block. */
if (cu->language == language_fortran && die->parent
&& die->parent->tag == DW_TAG_common_block)
- attr2 = NULL;
+ {
+ attr2 = NULL;
+ fortran_common_symbol = 1;
+ }
if (SYMBOL_CLASS (sym) == LOC_STATIC
&& SYMBOL_VALUE_ADDRESS (sym) == 0
+ && !fortran_common_symbol
&& !dwarf2_per_objfile->has_section_at_zero)
{
/* When a static variable is eliminated by the linker,
the corresponding debug information is not stripped
out, but the variable address is set to null;
- do not add such variables into symbol table. */
+ do not add such variables into symbol table.
+
+ An exception is Fortran, for which the first symbol
+ in a common block will have address/offset 0. */
}
else if (attr2 && (DW_UNSND (attr2) != 0))
{
@@ -23195,6 +23290,16 @@ The value is the maximum depth to print.
NULL,
&setdebuglist, &showdebuglist);
+ add_setshow_zuinteger_cmd ("dwarf2-common",
+ no_class, &dwarf2_common_debug, _("\
+Set debugging of dwarf2 common block handling."), _("\
+Show debugging of dwarf2 common block handling."), _("\
+When enabled (non-zero), common block variables are printed as they are\n\
+are relocated to their position in the common section."),
+ NULL,
+ NULL,
+ &setdebuglist, &showdebuglist);
+
add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
Set cross-checking of \"physname\" code against demangler."), _("\
Show cross-checking of \"physname\" code against demangler."), _("\
@@ -626,6 +626,15 @@ print_symbol (void *args)
fprintf_filtered (outfile, "optimized out");
break;
+ case LOC_COMMON_BLOCK:
+ fprintf_filtered (outfile, "common block storage");
+
+ if (section)
+ fprintf_filtered (outfile, " section %s",
+ bfd_section_name (section->the_bfd_section->owner,
+ section->the_bfd_section));
+ break;
+
default:
fprintf_filtered (outfile, "botched symbol class %x",
SYMBOL_CLASS (symbol));
@@ -915,6 +915,9 @@ print_partial_symbols (struct gdbarch *g
case LABEL_DOMAIN:
fputs_filtered ("label domain, ", outfile);
break;
+ case COMMON_BLOCK_DOMAIN:
+ fputs_filtered ("common block domain, ", outfile);
+ break;
default:
fputs_filtered ("<invalid domain>, ", outfile);
break;