Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 3 Oct 2000 11:12:06 -0700 (PDT)
From:      jdp@polstra.com
To:        hackers@freebsd.org
Cc:        mirko.viviani@rccr.cremona.it
Subject:   Re: dlopen() & objc initializer...
Message-ID:  <200010031812.e93IC6l16826@vashon.polstra.com>
In-Reply-To: <200010031324.PAA21704@rccr1.rccr.cremona.it>
References:  <200010031324.PAA21704@rccr1.rccr.cremona.it>

next in thread | previous in thread | raw e-mail | index | archive | help
In article <200010031324.PAA21704@rccr1.rccr.cremona.it>,
 <mirko.viviani@rccr.cremona.it> wrote:

> I have an ObjC shared object compiled in this way:
> 
> gcc -shared -rdynamic -o Bundle BreakTest.o SetTestCase.o -lSenFoundation -lSenTestingKit
> 
> When I load this object with dlopen() the __objc_exec_class() initializer
> of the libraries's classes are called first than the objc initializers of the
> bundle shared objects (BreakTest.o and SetTestCase.o).
> Why ?

Because by specifying the needed libraries (-lSenFoundation
-lSenTestingKit) you are in effect stating that the other shared
objects depend on those libraries.  The dynamic linker initializes
the libraries before it initializes the shared objects which depend
on them.  That is almost always the correct thing to do.  Suppose for
example that BreakTest.o contains a constructor which calls a function
in libSenFoundation.  The library had better be initialized already
when that happens.

This is also the ordering used in Solaris, by the way.  From their
ld.so.1(1):

        o  It calls any initialization functions provided by  the
           shared  object  dependencies.  By  default  these  are
           called in  the  reverse  order  of  the  topologically
           sorted dependencies. Should cyclic dependencies exist,
           the initialization  functions  are  called  using  the
           sorted  order  with  the  cycle removed. ldd(1) can be
           used to display the  initialization  order  of  shared
           object dependencies. See also LD_BREADTH.

I have tried a lot of different strategies for initializing shared
libraries, and the current one breaks fewer programs than any of the
others.

> Is it possible to tell the dynamic loader to call the
> BreakTest/SetTestCase objc initializer before the libs initializers
> ?

No.

> When I open an handle with dlopen() is possible to get the executable
> names/paths of the libs linked to it ?

Not directly.  But you might be able to achieve what you want by
using dladdr(3).

> Is there in elf something like the __CTOR_LIST__ ?

No, nothing that is available to application programs.

> With this example is possible to know in which libs a particular symbol
> resides ?

You might be able to look up the symbol using dlsym with RTLD_DEFAULT.
Then you could pass its address to dladdr() to find out which shared
library contained the definition.  See the notes in the BUGS section
of dladdr(3), though.

Also, you might need to specify RTLD_GLOBAL when loading the
libraries, if you use dlopen() for that.

By the way, when asking questions it is a good idea to say which
version of FreeBSD you are using.

John
-- 
  John Polstra                                               jdp@polstra.com
  John D. Polstra & Co., Inc.                        Seattle, Washington USA
  "Disappointment is a good sign of basic intelligence."  -- Chögyam Trungpa



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?200010031812.e93IC6l16826>