Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 18 Apr 1999 13:18:45 -0700 (PDT)
From:      John Polstra <jdp@polstra.com>
To:        Jacques Vidrine <n@nectar.com>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: __attribute__ ((constructor)) functions & shared libs
Message-ID:  <XFMail.990418131845.jdp@polstra.com>
In-Reply-To: <199904181724.MAA30234@spawn.nectar.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Jacques Vidrine wrote:
> On 18 April 1999 at 9:39, John Polstra <jdp@polstra.com> wrote:
>  
>> If it's ELF, the dynamic linker runs before crt1 gets control.  The
>> kernel invokes the dynamic linker directly.  After it has finished
>> its job, the dynamic linker transfers control to the main program's
>> entry point.
> 
> Where can I find this code (which invokes the dynamic linker)?  

In "src/sys/kern/imgact_elf.c".  The dynamic linker is referred to
there as the "interpreter".  The kernel finds its pathname in the
executable.  The kernel loads both the executable and the interpreter,
and then it passes control to the interpreter.  The interpreter is
responsible for getting control to the entry point of the executable,
eventually.  You'll find the jump instruction that does that in
"src/libexec/rtld-elf/i386/rtld_start.S".

> A backtrace from the debugger showed _rtld -> _init -> do_ctors ->
> libfoo_init, but I'm not sure where to find _rtld() and _init().

_rtld() is in "src/libexec/rtld-elf/rtld.c".

_init() is a little bit more complicated.  It is in several pieces,
all of which can be found in "src/lib/csu/i386-elf/".  The function
label comes from "crti.S".  The body consists of whatever is in the
".init" section. "crtbegin.c" puts a call to do_ctors() there, to
invoke all the global constructors.  Other components (libraries, etc)
can contribute additional code if they want to.  (None of ours do.)
Finally, the return from subroutine instruction comes from "crtn.S".
To see how the pieces fit together, link something using "cc -v" and
examine the "ld" command that is generated.

>> You are not officially "allowed" to assume that anything is already
>> initialized in those constructor/destructor functions.  Not even
>> printf is really guaranteed to work.  But perhaps we can make some
>> simple changes so that most things will work despite that.
> 
> I hope so.  This code does some fairly heavy duty stuff during
> initialization, including XOpenDisplay!  It apparently works on some
> versions of OSF/1, HP/UX, AIX, SCO, IRIX, Solaris, SunOS, and Linux.
> I have a hunch it would work on a FreeBSD a.out system, though I 
> haven't tested this.

I'm sure I can make it work for ELF too.  I know how to do it.  I'll
send you a patch to try when it's ready, but I have to do some other
things first for a few hours.

John
---
  John Polstra                                               jdp@polstra.com
  John D. Polstra & Co., Inc.                        Seattle, Washington USA
  "Self-interest is the aphrodisiac of belief."           -- James V. DeLong



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.990418131845.jdp>