This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: extern declared variable present in bss section of dynamic executable
- From: Alan Modra <amodra at gmail dot com>
- To: kamlesh kumar <kamleshbhalui at gmail dot com>
- Cc: binutils at sourceware dot org
- Date: Sat, 27 Jul 2019 10:09:03 +0930
- Subject: Re: extern declared variable present in bss section of dynamic executable
- References: <CABKRkgjDS98q6bWQnyzBVPXtoDWadmbxTRYf441VHh_pH-GTAg@mail.gmail.com>
On Fri, Jul 26, 2019 at 06:57:48PM +0530, kamlesh kumar wrote:
> Hi All,
> consider below test case,
> $cat shared.c
> int var=10;
> $gcc -shared -fpic shared.c -o libshared.so
>
> $cat test.c
> #include <stdio.h>
> extern int var;
> int main(){
> var++;
> printf("%d",var);
> return 0;
> }
> $gcc test.c -L. -lshared -Wl,-rpath=`pwd`
> $./a.out
> 11
>
> It prints correct ouput.
> But below observation looks littile strange,Digging into linker but like to
> know your thought as well.
> $ nm ./a.out | grep var
> 0000000000402030 B var
> $ nm libshared.so | grep var
> 0000000000002018 D var
>
> Global variable var is present in data section that is ok.
> But
> why global variable var is present in bss section of dynamic
> executable,while it should be undefined?
Variables defined in shared libraries are copied into an executable's
.bss section when code in the executable is non-PIC and thus would
require dynamic text relocations to access the variable directly in
the shared library.
See also R_*_COPY relocation in your processor ABI supplement.
--
Alan Modra
Australia Development Lab, IBM