Re: How to run code before global constructors?
From: David Schwartz (davids_at_webmaster.com)
Date: 07/24/03
- Next message: Sybren Stuvel: "Re: kdevelop c/c++ mix"
- Previous message: David Schwartz: "Re: How to run code before global constructors?"
- In reply to: Paul Pluzhnikov: "Re: How to run code before global constructors?"
- Next in thread: Ulrich.Teichert_at_gmx.de: "Re: How to run code before global constructors?"
- Reply: Ulrich.Teichert_at_gmx.de: "Re: How to run code before global constructors?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Sybren Stuvel: "Re: kdevelop c/c++ mix"
- Previous message: David Schwartz: "Re: How to run code before global constructors?"
- In reply to: Paul Pluzhnikov: "Re: How to run code before global constructors?"
- Next in thread: Ulrich.Teichert_at_gmx.de: "Re: How to run code before global constructors?"
- Reply: Ulrich.Teichert_at_gmx.de: "Re: How to run code before global constructors?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|