This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: Global variables and shared libraries
- From: Godmar Back <godmar at gmail dot com>
- To: "libc-help at sourceware dot org" <libc-help at sourceware dot org>
- Date: Wed, 28 Nov 2012 09:57:45 -0500
- Subject: Re: Global variables and shared libraries
- References: <20121128054115.GC3115@vt.edu>
You're compiling your code without -fPIC. Consequently, the variable
'foo' is treated as a global variable, which is addressed using 32-bit
relative to %rip. It must thus be in the lower address range as per
the x86_64 'small' model (so that's it no more than 4GB away from the
%rip). That's why it's added to the bss section of the executable, and
that's why a R_X86_64_COPY relocation entry appears in the libfoo.so
file.
See info gcc and look for -mcmodel=small
On Wed, Nov 28, 2012 at 12:41 AM, Bharath Ramesh <bramesh@vt.edu> wrote:
> I want to export a global variable that has been defined in a
> shared library to the binary that links with it. The most common
> approach would be to declare the global variable as an extern
> variable in the application code. This approach would make the
> actual reference to the global variable be present in the data
> segment of the binary. My use case would require that the
> reference not be present in the data segment of the binary. I am
> wondering if there is anyway I can export a global variable from
> a shared library so that it doesnt end up in the binaries data
> segment after linking. I was thinking some what in the way errno
> is linked, but I believe it ends up as a function call eventually
> so that appropriate address can be dereferenced, please do
> correct me if I am wrong. I do not want to access the variable
> using get/set calls either. If this is not the appropriate forum
> for this question, I would greatly appreciate it if you could
> point me in the direction of a more suitable forum as well.
>
> --
> Bharath