public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/40309]  New: gfortran does not support static c/d-tors.
@ 2009-05-30 14:53 dave dot korn dot cygwin at gmail dot com
  2009-05-30 15:01 ` [Bug fortran/40309] " dave dot korn dot cygwin at gmail dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: dave dot korn dot cygwin at gmail dot com @ 2009-05-30 14:53 UTC (permalink / raw)
  To: gcc-bugs

[ ref: http://gcc.gnu.org/onlinedocs/gccint/Initialization.html ]
[ ref: http://gcc.gnu.org/ml/fortran/2009-05/threads.html#00440 ]

gfortran does not set main_identifier_node to anything.  This causes
gimple_expand_cfg() in cfgexpand.c to fail to emit a call to the libgcc static
init function __main, because the MAIN_NAME_P check in this conditional:

  /* If this function is `main', emit a call to `__main'
     to run global initializers, etc.  */
  if (DECL_NAME (current_function_decl)
      && MAIN_NAME_P (DECL_NAME (current_function_decl))
      && DECL_FILE_SCOPE_P (current_function_decl))
    expand_main_function ();

fails.  On cygwin, this means that gfortran's init() function is never set up,
the units and option variables don't get initialised, and "hello world" fails
when it tries to output the first char, with a runtime library error.

The fix is to correctly initialise it somewhere in the fortran compiler's early
 startup, using a line as simple as:

  main_identifier_node = get_identifier ("main");

although I don't know for sure what mangled version of the name "main" to use
for fortran, or where would be the best place in the fortran frontend to put
this code.


-- 
           Summary: gfortran does not support static c/d-tors.
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dave dot korn dot cygwin at gmail dot com
 GCC build triplet: i686-pc-cygwin
  GCC host triplet: i686-pc-cygwin
GCC target triplet: i686-pc-cygwin


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
@ 2009-05-30 15:01 ` dave dot korn dot cygwin at gmail dot com
  2009-05-30 15:11 ` pinskia at gcc dot gnu dot org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: dave dot korn dot cygwin at gmail dot com @ 2009-05-30 15:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from dave dot korn dot cygwin at gmail dot com  2009-05-30 15:01 -------

It's not entirely straightforward, it seems.  An earlier attempt appears to
have fizzled out:

http://gcc.gnu.org/ml/fortran/2007-09/threads.html#00289

I do not yet understand Andrew Pinski's objection: 

" This is wrong as it causes the middle-end to also emitt a call to
__main inside MAIN__.  Now you will get two calls to __main which
calls the global constructors now twice. "

as I don't understand what the "also" refers to.  Right now there are precisely
no calls to __main at all.  Perhaps there used to be an actual C-linkage main
function in libgfortran.  Yes, wait a minute, this is what libgfortranbegin
used to get us, is it not?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
  2009-05-30 15:01 ` [Bug fortran/40309] " dave dot korn dot cygwin at gmail dot com
@ 2009-05-30 15:11 ` pinskia at gcc dot gnu dot org
  2009-05-30 15:19 ` dave dot korn dot cygwin at gmail dot com
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-05-30 15:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2009-05-30 15:11 -------
(In reply to comment #1)
> It's not entirely straightforward, it seems.  An earlier attempt appears to
> have fizzled out:

That no longer applies because gfortran produces main now.  MAIN__ is not going
to be used but main will be.

Thanks,
Andrew Pinski


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
  2009-05-30 15:01 ` [Bug fortran/40309] " dave dot korn dot cygwin at gmail dot com
  2009-05-30 15:11 ` pinskia at gcc dot gnu dot org
@ 2009-05-30 15:19 ` dave dot korn dot cygwin at gmail dot com
  2009-05-30 15:34 ` dave dot korn dot cygwin at gmail dot com
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: dave dot korn dot cygwin at gmail dot com @ 2009-05-30 15:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from dave dot korn dot cygwin at gmail dot com  2009-05-30 15:19 -------

About to test this:

$ svn diff -x -p fortran/trans-decl.c
Index: fortran/trans-decl.c
===================================================================
--- fortran/trans-decl.c        (revision 147949)
+++ fortran/trans-decl.c        (working copy)
@@ -3859,7 +3859,8 @@ create_main_function (tree fndecl)
   tmp =  build_function_type_list (integer_type_node, integer_type_node,
                                   build_pointer_type (pchar_type_node),
                                   NULL_TREE);
-  ftn_main = build_decl (FUNCTION_DECL, get_identifier ("main"), tmp);
+  main_identifier_node
+    = ftn_main = build_decl (FUNCTION_DECL, get_identifier ("main"), tmp);
   DECL_EXTERNAL (ftn_main) = 0;
   TREE_PUBLIC (ftn_main) = 1;
   TREE_STATIC (ftn_main) = 1;


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
                   ` (2 preceding siblings ...)
  2009-05-30 15:19 ` dave dot korn dot cygwin at gmail dot com
@ 2009-05-30 15:34 ` dave dot korn dot cygwin at gmail dot com
  2009-05-30 15:38 ` dave dot korn dot cygwin at gmail dot com
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: dave dot korn dot cygwin at gmail dot com @ 2009-05-30 15:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from dave dot korn dot cygwin at gmail dot com  2009-05-30 15:34 -------
(In reply to comment #3)
> About to test this:

  That was wrong.  This is what I should have said:

$ svn diff fortran/trans-decl.c
Index: fortran/trans-decl.c
===================================================================
--- fortran/trans-decl.c        (revision 147949)
+++ fortran/trans-decl.c        (working copy)
@@ -3859,7 +3859,8 @@
   tmp =  build_function_type_list (integer_type_node, integer_type_node,
                                   build_pointer_type (pchar_type_node),
                                   NULL_TREE);
-  ftn_main = build_decl (FUNCTION_DECL, get_identifier ("main"), tmp);
+  main_identifier_node = get_identifier ("main");
+  ftn_main = build_decl (FUNCTION_DECL, main_identifier_node, tmp);
   DECL_EXTERNAL (ftn_main) = 0;
   TREE_PUBLIC (ftn_main) = 1;
   TREE_STATIC (ftn_main) = 1;


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
                   ` (3 preceding siblings ...)
  2009-05-30 15:34 ` dave dot korn dot cygwin at gmail dot com
@ 2009-05-30 15:38 ` dave dot korn dot cygwin at gmail dot com
  2009-05-30 16:13 ` dave dot korn dot cygwin at gmail dot com
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: dave dot korn dot cygwin at gmail dot com @ 2009-05-30 15:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from dave dot korn dot cygwin at gmail dot com  2009-05-30 15:38 -------
The patch in comment 4 works.

DKAdmin@ubik /tmp/fortran
$ ./hello-fixed.exe
 Hello World!

DKAdmin@ubik /tmp/fortran
$ gdb ./hello-fixed.exe
GNU gdb 6.8.0.20080328-cvs (cygwin-special)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-cygwin"...
(gdb) disass main
Dump of assembler code for function main:
0x0040114d <main+0>:    push   %ebp
0x0040114e <main+1>:    mov    %esp,%ebp
0x00401150 <main+3>:    and    $0xfffffff0,%esp
0x00401153 <main+6>:    sub    $0x10,%esp
0x00401156 <main+9>:    call   0x401210 <__main>
0x0040115b <main+14>:   mov    0xc(%ebp),%eax
0x0040115e <main+17>:   mov    %eax,0x4(%esp)
0x00401162 <main+21>:   mov    0x8(%ebp),%eax
0x00401165 <main+24>:   mov    %eax,(%esp)
0x00401168 <main+27>:   call   0x402320 <*__gfortran_set_args>
0x0040116d <main+32>:   movl   $0x417b00,0x4(%esp)
0x00401175 <main+40>:   movl   $0x8,(%esp)
0x0040117c <main+47>:   call   0x4023b0 <*__gfortran_set_options>
0x00401181 <main+52>:   call   0x4010e0 <MAIN__>
0x00401186 <main+57>:   mov    $0x0,%eax
0x0040118b <main+62>:   leave
0x0040118c <main+63>:   ret
0x0040118d <main+64>:   nop
0x0040118e <main+65>:   nop
0x0040118f <main+66>:   nop
End of assembler dump.
(gdb)

I'll try a testsuite run now.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
                   ` (4 preceding siblings ...)
  2009-05-30 15:38 ` dave dot korn dot cygwin at gmail dot com
@ 2009-05-30 16:13 ` dave dot korn dot cygwin at gmail dot com
  2009-05-30 17:09 ` dave dot korn dot cygwin at gmail dot com
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: dave dot korn dot cygwin at gmail dot com @ 2009-05-30 16:13 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from dave dot korn dot cygwin at gmail dot com  2009-05-30 16:12 -------
Groan.

PASS: gfortran.dg/Wall.f90 (test for excess errors)
spawn [open ...]
Internal Error: insert(): Duplicate key found!
FAIL: gfortran.dg/Wall.f90 execution test

In other words, exactly what Andrew warned would happen.

Breakpoint 1, __main () at /gnu/winsup/src/winsup/cygwin/dcrt0.cc:993
993       do_global_ctors (user_data->ctors, false);
(gdb) bt
#0  __main () at /gnu/winsup/src/winsup/cygwin/dcrt0.cc:993
#1  0x00401166 in main ()
(gdb) c
Continuing.

Breakpoint 1, __main () at /gnu/winsup/src/winsup/cygwin/dcrt0.cc:993
993       do_global_ctors (user_data->ctors, false);
(gdb) bt
#0  __main () at /gnu/winsup/src/winsup/cygwin/dcrt0.cc:993
#1  0x004010ee in MAIN__ ()
#2  0x00401191 in main ()
(gdb)

For some reason though, there isn't any call to __main in MAIN__ in my original
"hello world" testcase, only in main.  I don't yet understand that.  It may be
to do with the usage of main_program_symbol() in parse.c, which is called from
two places:

    case ST_PROGRAM:
      if (seen_program)
        goto duplicate_main;
      seen_program = 1;
      prog_locus = gfc_current_locus;

      push_state (&s, COMP_PROGRAM, gfc_new_block);
      main_program_symbol(gfc_current_ns, gfc_new_block->name);
      accept_statement (st);


    /* Anything else starts a nameless main program block.  */
    default:
      if (seen_program)
        goto duplicate_main;
      seen_program = 1;
      prog_locus = gfc_current_locus;

      push_state (&s, COMP_PROGRAM, gfc_new_block);
      main_program_symbol (gfc_current_ns, "MAIN__");


  I'm not sure yet what happens when we take the first one of these clauses and
create a main function that is not called "MAIN__", because there is code in
gfc_sym_mangled_function_id in trans-decl.c that assumes any main function has
the same name:

      /* Main program is mangled into MAIN__.  */
      if (sym->attr.is_main_program)
        return get_identifier ("MAIN__");

Possibly this is an inconsistency, and if there's a named main block we create
a main function but then look up what will effectively be an unrelated name.  I
need advice from someone who knows the fortran compiler internals better than
me. 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
                   ` (5 preceding siblings ...)
  2009-05-30 16:13 ` dave dot korn dot cygwin at gmail dot com
@ 2009-05-30 17:09 ` dave dot korn dot cygwin at gmail dot com
  2009-05-30 21:15 ` burnus at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: dave dot korn dot cygwin at gmail dot com @ 2009-05-30 17:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from dave dot korn dot cygwin at gmail dot com  2009-05-30 17:09 -------
http://gcc.gnu.org/ml/fortran/2009-05/msg00452.html

We have two functions that both match MAIN_NAME_P, because they share the same
DECL_NAME.  This happens when there is a "PROGRAM main" directive in the
fortran source.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
                   ` (6 preceding siblings ...)
  2009-05-30 17:09 ` dave dot korn dot cygwin at gmail dot com
@ 2009-05-30 21:15 ` burnus at gcc dot gnu dot org
  2009-06-01  7:01 ` burnus at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: burnus at gcc dot gnu dot org @ 2009-05-30 21:15 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from burnus at gcc dot gnu dot org  2009-05-30 21:14 -------
Latest patch - fixing the main_identifier_node (__main() call) problem as in
comment 4 plus fixes the "PROGRAM main" problem.

http://gcc.gnu.org/ml/fortran/2009-05/msg00458.html


-- 

burnus at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |burnus at gcc dot gnu dot
                   |                            |org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2009-05-30 21:14:46
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
                   ` (7 preceding siblings ...)
  2009-05-30 21:15 ` burnus at gcc dot gnu dot org
@ 2009-06-01  7:01 ` burnus at gcc dot gnu dot org
  2009-06-01  7:34 ` burnus at gcc dot gnu dot org
  2009-06-01  8:05 ` dave dot korn dot cygwin at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: burnus at gcc dot gnu dot org @ 2009-06-01  7:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from burnus at gcc dot gnu dot org  2009-06-01 07:00 -------
Subject: Bug 40309

Author: burnus
Date: Mon Jun  1 07:00:35 2009
New Revision: 148035

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148035
Log:
2009-06-01  Tobias Burnus  <burnus@net-b.de>

       PR fortran/40309
       * trans-decl.c (gfc_sym_identifier): Use "MAIN__" for PROGRAM "main".
       (create_main_function): Set main_identifier_node.


Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
                   ` (8 preceding siblings ...)
  2009-06-01  7:01 ` burnus at gcc dot gnu dot org
@ 2009-06-01  7:34 ` burnus at gcc dot gnu dot org
  2009-06-01  8:05 ` dave dot korn dot cygwin at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: burnus at gcc dot gnu dot org @ 2009-06-01  7:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from burnus at gcc dot gnu dot org  2009-06-01 07:34 -------
FIXED on the trunk (4.5).


-- 

burnus at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug fortran/40309] gfortran does not support static c/d-tors.
  2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
                   ` (9 preceding siblings ...)
  2009-06-01  7:34 ` burnus at gcc dot gnu dot org
@ 2009-06-01  8:05 ` dave dot korn dot cygwin at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: dave dot korn dot cygwin at gmail dot com @ 2009-06-01  8:05 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #11 from dave dot korn dot cygwin at gmail dot com  2009-06-01 08:05 -------
I checked the fix and it works.  Verified.


-- 

dave dot korn dot cygwin at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |VERIFIED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40309


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2009-06-01  8:05 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-30 14:53 [Bug fortran/40309] New: gfortran does not support static c/d-tors dave dot korn dot cygwin at gmail dot com
2009-05-30 15:01 ` [Bug fortran/40309] " dave dot korn dot cygwin at gmail dot com
2009-05-30 15:11 ` pinskia at gcc dot gnu dot org
2009-05-30 15:19 ` dave dot korn dot cygwin at gmail dot com
2009-05-30 15:34 ` dave dot korn dot cygwin at gmail dot com
2009-05-30 15:38 ` dave dot korn dot cygwin at gmail dot com
2009-05-30 16:13 ` dave dot korn dot cygwin at gmail dot com
2009-05-30 17:09 ` dave dot korn dot cygwin at gmail dot com
2009-05-30 21:15 ` burnus at gcc dot gnu dot org
2009-06-01  7:01 ` burnus at gcc dot gnu dot org
2009-06-01  7:34 ` burnus at gcc dot gnu dot org
2009-06-01  8:05 ` dave dot korn dot cygwin at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).