[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: dlopen from dlopend library question

From: Ralf Wildenhues
Subject: Re: dlopen from dlopend library question
Date: Mon, 8 Aug 2005 11:10:08 +0200
User-agent: Mutt/1.4.1i

Hi Gleb,

* Gleb Natapov wrote on Sun, Aug 07, 2005 at 11:31:54AM CEST:
> On Tue, Aug 02, 2005 at 03:44:05PM +0200, Ralf Wildenhues wrote:
> > * Gleb Natapov wrote on Mon, Aug 01, 2005 at 04:58:03PM CEST:
> > > After investigation of the problem I found that tries to load
> > > from library constructor. Even if is dlopen with 
> > > RTLD_GLOBAL flag set, can't resolve symbols from
> > 
> > Just so we don't misunderstand each other: what exactly do you mean by
> > library constructor?  Are you overwriting DT_INIT?  (If so, you are a far
> > way from portable shared libraries.)
> I am talking about function marked __attribute__((constructor)).

Yes, that is what I meant.

> Portability is not an issue for me.

OK.  You should look into it more.

> > > Program 'prog' calls dlopen(, RTLD_GLOBAL), in constructor
> > > calls dlopen( and this call fails with unresolved symbols
> > > from
> > > 
> > > If we add function init_a() to and move dlopen( from 
> > > constructor to the function and we call this function from 'prog' after
> > > dlopen (, RTLD_GLOBAL) then everything works as expected.
> > 
> > Best would be if you showed short reproducible code.  Not that I know
> > whether I could help you then.
> >
> Attached. To compile run following:
> $ gcc liba.c -shared -o
> $ gcc plugin.c -shared -o
> $ gcc prog.c -ldl -o prog

Some notes:
- In general, you _must_ use -fPIC to compile the code that ends up in
  shared objects.  Above will fail blatantly on x86_64, for example.

- You should make use of dlerror, for example like this, in
  the failure case:
  fprintf (stderr, "Load of failed: %s\n", dlerror());

- Quoting 'info gcc Link\ Options':
|  (1) On some systems, `gcc -shared' needs to build supplementary stub
| code for constructors to work.  On multi-libbed systems, `gcc -shared'
| must select the correct support libraries to link against.  Failing to
| supply the correct flags may lead to subtle defects.  Supplying them in
| cases where they are not necessary is innocuous.

So, for example, using
  gcc plugin.c -shared -o -L. -la

and then using

  LD_LIRBARY_PAH=. ./prog

will succeed.

> Notice that second dlopen of succeeds.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]