This is the mail archive of the glibc-linux@ricardo.ecn.wfu.edu mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Shared library -shared vs. -fpic


On Mon, 22 May 2000, Philip J. Mucci wrote:

> Date: Mon, 22 May 2000 11:06:03 +0200
> From: Philip J. Mucci <mucci@cs.utk.edu>
> Reply-To: glibc-linux@ricardo.ecn.wfu.edu
> To: glibc-linux@ricardo.ecn.wfu.edu
> Subject: Shared library -shared vs. -fpic
> 
> Hi all,
> 
> I thought I'd report my findings. 
> 
> Indeed -fpic and -fPIC are not necessary for shared libraries to be used
> either by ld.so or libdl.so. Why? I have no idea... As long as you do
> -shared with gcc, you're golden.

PIC stands for Position-Independent Code. Code isn't position-independent (or
``relocatable'') cannot be loaded at an arbitrary address; it requires some
references to be patched at load time. Shared libraries need to be relocatable
because it's not known beforehand what address they will be loaded at, and
because the same library image may be loaded at different addresses in
different processes.

Just because you don't specify -fPIC doesn't mean that the compiler won't emit
position-independent code; the option prevents it from emitting
position-dependent code in situations where it otherwise would.

> However, interesting enough, when I dlopen() to load a shared library
> that I built and that library references _start,
> dlopen() reports that _start is undefined. _init is defined so I used
> that instead. But, as it turns out, these symbols get changed when the
> library is dlopen(). For some reason, I cannot get to _init or _start of
> the original code.

You are probably resolving to the _init symbol in the library.
Did you read the dlopen man page about RTLD_GLOBAL?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]