PATCH(2.95): attribute((constructor)) is NOOP when compiled by C++

Alexander N. Kabaev kabaev@mail.ru
Thu Oct 19 04:48:00 GMT 2000


The following test-case works as expected when compiled as C code
but fails to call static initialization function declared with
attribute((constructor)) when processed as C++:

test7.c:

include <stdio.h>

void f() __attribute((__constructor__));
int i;
void f()
{
    i = 1;
}

int main(int argc, char *argv[])
{
    printf("%d\n", i);
    return 0;
}

Running the program compiled by C++ and then C frontends yields the 
following results:

% g++ -o test7 test7.c
% ./test7
0
% gcc -o test7 test7.c
% ./test7             
1

The following patch against 2.95 branch fixes the problem by bringing
C++ frontend in sync with similar code in C frontend. 


Index: cp/decl.c
===================================================================
RCS file: /home/ncvs/src/contrib/gcc.295/cp/decl.c,v
retrieving revision 1.4
diff -c -r1.4 decl.c
*** cp/decl.c	1999/10/16 07:35:28	1.4
--- cp/decl.c	2000/10/19 02:37:39
***************
*** 14515,14523 ****
      }
  
    if (DECL_STATIC_CONSTRUCTOR (fndecl))
!     static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
    if (DECL_STATIC_DESTRUCTOR (fndecl))
!     static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
  
    if (! nested)
      {
--- 14515,14537 ----
      }
  
    if (DECL_STATIC_CONSTRUCTOR (fndecl))
!     {
! #ifndef ASM_OUTPUT_CONSTRUCTOR
!       if (! flag_gnu_linker)
! 	static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
!       else
! #endif
!       assemble_constructor (IDENTIFIER_POINTER (DECL_NAME (fndecl)));
!     }
    if (DECL_STATIC_DESTRUCTOR (fndecl))
!     {
! #ifndef ASM_OUTPUT_DESTRUCTOR
!       if (! flag_gnu_linker)
! 	static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
!       else
! #endif
!       assemble_destructor (IDENTIFIER_POINTER (DECL_NAME (fndecl)));
!     }
  
    if (! nested)
      {


More information about the Gcc-patches mailing list