From owner-freebsd-hackers Sun Apr 18 13:21:21 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from wall.polstra.com (rtrwan160.accessone.com [206.213.115.74]) by hub.freebsd.org (Postfix) with ESMTP id 5A44414D13 for ; Sun, 18 Apr 1999 13:21:10 -0700 (PDT) (envelope-from jdp@polstra.com) Received: from vashon.polstra.com (vashon.polstra.com [206.213.73.13]) by wall.polstra.com (8.9.3/8.9.1) with ESMTP id NAA22478; Sun, 18 Apr 1999 13:18:45 -0700 (PDT) (envelope-from jdp@polstra.com) Received: (from jdp@localhost) by vashon.polstra.com (8.9.3/8.9.1) id NAA20628; Sun, 18 Apr 1999 13:18:45 -0700 (PDT) (envelope-from jdp@polstra.com) Message-ID: X-Mailer: XFMail 1.3 [p0] on FreeBSD X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit MIME-Version: 1.0 In-Reply-To: <199904181724.MAA30234@spawn.nectar.com> Date: Sun, 18 Apr 1999 13:18:45 -0700 (PDT) Organization: Polstra & Co., Inc. From: John Polstra To: Jacques Vidrine Subject: Re: __attribute__ ((constructor)) functions & shared libs Cc: hackers@FreeBSD.ORG Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Jacques Vidrine wrote: > On 18 April 1999 at 9:39, John Polstra 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