Re: -z preinitarray

From: Jonathan Adams (jwadams_at_gmail.com)
Date: 04/30/05


Date: Fri, 29 Apr 2005 15:45:33 -0700

In article <42728e0e@news.meer.net>, "Dan Koren" <dankoren@yahoo.com>
wrote:

> OK, thanks.
>
> Does this imply, in effect, that there is no way for an
> interposer shared library (e.g. a memory allocator) to
> arrange to always initialize itself first, before any
> other library or code in the main executable, and
> regardless of how the main executable may have
> been built?

It generally can't, anyway; relying on init sections to set everything
up is a bad idea. It's much better to set everything up in static data,
and arrange for any additional initialization to happen at the first
call to malloc/calloc/realloc/etc. With a bit of care, this can be done
without impacting the fast path.

The problem is that init section ordering is not guaranteed, especially
when cyclic dependencies happen. The classic problem is LD_BIND_NOW=1
plus C++; libCrun.so.1, libc.so.1, and libmymalloc.so form a cyclic
dependency group, the linker calls libCrun's initializer first, the C++
runtime initialization calls malloc(), but libmymalloc.so's init section
has not fired. Chaos ensues.

Summary: init sections are evil and unreliable; avoid them if you
possibly can.

Cheers,
- jonathan



Relevant Pages

  • Re: -z preinitarray
    ... > able to reach the initialization code, ... > nothing done in the init section is required for proper initialization. ... > It has to depend on libc, ... BTW the entire allocator is ...
    (comp.unix.solaris)
  • Re: -z preinitarray
    ... then put the check for initialization in the slow ... Previously, I did most of my initialization in the init section; ... It has to depend on libc, ... caused the dependency loop; libC depends on libumem depends on libc ...
    (comp.unix.solaris)
  • Re: Default initialization and save
    ... My_default_init_type has default initialization, ... The explicit init of y overrides its ... The default init of xa get's applied on both allocations, ... subroutine nothing ...
    (comp.lang.fortran)
  • Re: I think C# is forcing us to write more (redundant) code
    ... > If the COR guarantees that, then C# compiler is generating redundant code. ... > Reflector does not give the 'init' flag. ... Are you sure you did read all other replies, this is exactly what we are ... directive that forces the JIT to generate initialization code too. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Should I use BEGIN, CHECK, or INIT?
    ... > sub import { ... Either that, or the initialization is prepared to be run more than once, ... I had always "known" that INIT runs before the first executable statement ...
    (comp.lang.perl.misc)