🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gdb.bug
Article #451

g++ generates superfluous global constructor

#451
From: Urs Hoelzle
Date: Wed, 07 Oct 1992 21:03
59 lines
2259 bytes
The following code segment produces a global initializer when compiled
with g++, even though it's just a simple constant initialization:

    typedef void (*fntype)();
    extern void fn();

    void* f = &fn;

Code produced by g++ (2.2.2, Sparc):

gcc2_compiled.:
.text
        .align 4
        .global __GLOBAL_$I$x_c
        .proc   020
__GLOBAL_$I$x_c:
        !#PROLOGUE# 0
        save %sp,-112,%sp
        !#PROLOGUE# 1
        sethi %hi(_fn__Fv),%g2
        or %g2,%lo(_fn__Fv),%g2
        sethi %hi(_f),%g3
        st %g2,[%g3+%lo(_f)]
        ret
        restore
        .global _f
        .common _f,8,"bss"

Code produced by gcc (i.e C mode instead of C++ mode):

gcc2_compiled.:
        .global _f
.data
        .align 4
_f:
        .word _fn__Fv

Interestingly, g++ generates the correct code (i.e, none :-) if the & is
dropped (i.e.  void* f = fn;).  But unfortunately the & can't be dropped
when initializing f with a member function.

The problem is that in my application I have a large array of structs
looking roughly like

      struct s { fntype f; int flag1, flag2, flag3, flag4, flag5; }

and g++ generates many kilobytes of code to initialize each of the
values at startup (even the flags!!).  The other problem is that if
you put such code in a dynamic link library to be called by C, you'll
be screwed because the initializer isn't run.  There shouldn't be any
global initializers if there aren't any global objects.

Any fix or workaround?

-Urs

----------------------------------------------------------------------------
Urs Hoelzle                                              urs@cs.stanford.EDU
Computer Systems Laboratory, CIS 57, Stanford University, Stanford, CA 94305
Technical Details
Message-ID: <hoelzle.718491784@Xenon.Stanford.EDU>
Path: arc1-ca.pugleaf.net!sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!xenon.stanford.edu!hoelzle

Show Full Headers
Complete Article Headers
Newsgroups: gnu.gdb.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!xenon.stanford.edu!hoelzle
From: hoelzle@xenon.stanford.edu (Urs Hoelzle)
Subject: g++ generates superfluous global constructor
Message-ID: <hoelzle.718491784@Xenon.Stanford.EDU>
Sender: gnulists@ai.mit.edu
Organization: CS Department, Stanford University, California, USA
Distribution: gnu
Date: Wed, 7 Oct 1992 21:03:04 GMT
Approved: bug-gdb@prep.ai.mit.edu
Lines: 60