extern declared variable present in bss section of dynamic executable

Alan Modra amodra@gmail.com
Sat Jul 27 00:39:00 GMT 2019


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



More information about the Binutils mailing list