[RFC] Use DW_CC_program to indicate Fortran main subroutine

Wu Zhou
2005-11-21T04:41:18Z

Hello GCC and GDB maintainers,

We ever discussed about how to indicate the main subroutine of fortran
and java code with DWARF info (GCC PR 23280, 1427 and 10220; GDB PR 822).
My point is that DW_CC_program might be the final solution. Here is what
DWARF standard said about this:

If the semantics of the language of the compilation unit containing the
subroutine entry distinguishes between ordinary subroutines and
subroutines that can serve as the main program, that is, subroutines
that cannot be called directly according to the ordinary calling
conventions, then the debugging information entry for such a subroutine
may have a calling convention attribute whose value is the constant
DW_CC_program.

The DW_CC_program value is intended to support Fortran main programs. It
is not intended as a way of finding the entry address for the program.

Following this, I am doing some experiment with both gfortran and gdb. And
I found it does works. The problems stated in GDB PR 822, GCC 10220 are
all gone. So I think this might be the proper way. And I post my patches
here for review. Any comments and suggestion are highly appreciated.

Below is the patch to gcc-4.0.2. I think this method might also work well
with gcj, but I am not sure.

2005-11-21 Wu Zhou <woodzltc@cn.ibm.com>

* dwarf2out.c (add_calling_convention_attribute): If the function
name is MAIN__, add a DW_AT_calling_convention attribute with the
value being DW_CC_program.

--- dwarf2out.c.orig 2005-11-21 17:37:34.000000000 +0800
+++ dwarf2out.c 2005-11-21 17:37:53.000000000 +0800
@@ -10755,6 +10755,13 @@ add_calling_convention_attribute (dw_die

value = targetm.dwarf_calling_convention (type);

+ /* DWARF standard suggests to use DW_CC_program value to indicate Fortran
+ main programs. Do this when we find the name is 'MAIN__'.
+ FIXME: Maybe it is better to do this in other way. But I don't figure
+ out the better way yet. */
+ if (!strncmp (get_AT_string (subr_die, DW_AT_name), "MAIN__", 6))
+ add_AT_unsigned (subr_die, DW_AT_calling_convention, DW_CC_program);
+
/* Only add the attribute if the backend requests it, and
is not DW_CC_normal. */
if (value && (value != DW_CC_normal))

This is the patch to gdb mainline source. I ever thought of adding these
code into read_subroutine_type, but it seems that is too late for setting
main name. So I am now adding them into read_partial_die.

Index: dwarf2read.c
===================================================================
RCS file: /cvs/src/src/gdb/dwarf2read.c,v
retrieving revision 1.184.2.2
diff -u -p -r1.184.2.2 dwarf2read.c
--- dwarf2read.c 4 Nov 2005 02:58:31 -0000 1.184.2.2
+++ dwarf2read.c 21 Nov 2005 04:11:35 -0000
@@ -5432,6 +5432,13 @@ read_partial_die (struct partial_die_inf
part_die->has_stmt_list = 1;
part_die->line_offset = DW_UNSND (&attr);
break;
+ case DW_AT_calling_convention:
+ /* DWARF standard suggests to use value DW_CC_program of attribute
+ DW_AT_calling_convention to indicate the Fortran main program.
+ The following code is to check this. */
+ if (DW_UNSND (&attr) == DW_CC_program)
+ set_main_name (part_die->name);
+ break;
default:
break;
}

Regards
- Wu Zhou