Re: How to run code before global constructors?

From: David Schwartz (davids_at_webmaster.com)
Date: 07/24/03


Date: Thu, 24 Jul 2003 12:39:49 -0700


"Paul Pluzhnikov" <ppluzhnikov@earthlink.net> wrote in message
news:usmowouwa.fsfYB8X@earthlink.net...

> "David Schwartz" <davids@webmaster.com> writes:

> > I have a program that needs to install a signal handler before any
> > global constructors are called. (The global constructors do things that
may
> > cause signals to be received that need to be properly handled.)

> Perhaps these constructors need to ensure that signals are handled
> properly, then?

    Not easy. The constructors call low-level code that is heavily
micro-optimized. An extra test for initialization would destroy the whole
point of the signal handler. The code is designed to fault if it's not
initialized and the signal handler handles the fault by doing the
initialization.

    I can't do the initialization directly anyway because there's any number
of places that might need to be initialized and there's no way to find all
of them before global constructors finish running. So I've rigged it so that
uninitialized sections fault and the fault handler does the initialization.

    An analogy that might help is if you imagine a program that has two
run-time modes. In one run mode, a piece of inline low-level assembly code
called from many places has to increment a variable and in the other run
mode it has to decrement it. So instead of an increment or decrement
instruction, you put an illegal opcode. In the SIGILL handler, you check
which run mode you're in and replace the illegal opcode with the correct
one.

    This works perfectly until a global constructor calls something that
uses this inline code.

> You could provide e.g. strrchr() implementation in your a.elf, and it
> will be called during libc.so initialization (very early in the process'
> lifetime).

    That's not a bad idea, actually.

> Beware: libc has not finished initializing itself yet, so be *very*
> restricted in using libc at that point. I think calling sigaction()
> is ok though.

    If I had to, I could make the system call directly. This only has to
work on Linux right now, but ultimately would be nice to have on FreeBSD as
well.

    It now does work on Linux, using the gmon startup hook.

    DS



Relevant Pages

  • Re: Different menu bars in a MDI
    ... If you check the menu item using other than an ON_UPDATE_COMMAND_UI handler, ... This menu item is available in both menu bars, and should either be checked in either or be unchecked in either. ... Should I put it in the initialization of the View, the Document, or perhaps the Frame? ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • RE: Stopping Event Handlers on Startup
    ... So you write the handler ... Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ... Then after you are done with initialization and you now want the event to ...
    (microsoft.public.dotnet.languages.vb)
  • Re: eVC4 basics: Combo Box
    ... during the dialog initialization, say in your ... OnInitDialog() handler: ... not want "Select An Item" to be amongst the list of items that the Customer ...
    (microsoft.public.windowsce.embedded.vc)
  • page fault error?
    ... page fault while in kernel mode ... TCP keepalive=YES ... Initial rc.i386 initialization: ...
    (freebsd-questions)
  • page fault while in kernel mode?
    ... page fault while in kernel mode ... TCP keepalive=YES ... Initial rc.i386 initialization: ...
    (freebsd-questions)