Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Linux stubs are used for standard libraries. Why are stubs required?

Tags:

c

linux

In Linux, why are stubs required for standard libraries?

like image 655
sunmoon Avatar asked Dec 08 '10 08:12

sunmoon


1 Answers

stubs are required to ensure proper linking of an executable across various linux releases without building the object files.

For example: Let a be the executable we are building:

gcc -o a test.o test1.o test2.o -lz

In the above case executable a has a dependency on the libz.so (-lz is to link with libz.so). The linker resolves libz.so using the LD_LIBRARY_PATH.

Now let us see the problem:

In RHEL4(Linux Zseries):
objdump -T /usr/lib64/libz.so.1 | grep stack_chk 

In RHEL5(Linux ZSeries);
objdump -T /usr/lib64/libz.so.1 | grep stack_chk

0000000000000000 DF UND 0000000000000031 GLIBC_2.4 __stack_chk_fail 

In RHEL5, we see an undefined symbol in the libz.so. Unless we pass libc to the linker after lz in the above command, this cannot be resolved.

Stubs: If we generate the stub for libz.so, packing all the symbols of libz.so in to a stub library and link with the stub library instead of the real library, we don't see any errors:

Modified link line:

gcc -o a test.o test1.o test2.o -L/home/lib/stubs/ -lz

In the /home/lib/stubs directory, we have a stub library for libz.so by name libzstub.so.

Linker gives higher priority to the path given in the link command than LD_LIBRARY_PATH.

Now even if we link in the RHEL5 release, the linker resolves the symbols for the libz.so from the /home/lib/stubs directory.

Here the configuration details of the boxes i have used.

Loader takes care of loading the coresponding function at runtime.

RHEL5:

libcmpiutil-0.4-2.el5
glibc-utils-2.5-42
libc-client-2004g-2.2.1
libcap-1.10-26
libcap-1.10-26
libchewing-devel-0.3.0-8.el5
libchewing-0.3.0-8.el5
libcxgb3-1.2.3-1.el5
libcap-devel-1.10-26
glibc-common-2.5-42
libcxgb3-static-1.2.3-1.el5
libcroco-devel-0.6.1-2.1
compat-glibc-headers-2.3.4-2.26
libcroco-0.6.1-2.1
compat-libcom_err-1.0-7
libcmpiutil-devel-0.4-2.el5
compat-glibc-2.3.4-2.26
glibc-headers-2.5-42
glibc-devel-2.5-42
libcap-devel-1.10-26
libc-client-2004g-2.2.1
libcmpiutil-0.4-2.el5
libcroco-0.6.1-2.1
libc-client-devel-2004g-2.2.1
glibc-2.5-42
libchewing-devel-0.3.0-8.el5
libcroco-devel-0.6.1-2.1
compat-libcom_err-1.0-7
libc-client-devel-2004g-2.2.1
libchewing-0.3.0-8.el5
libcxgb3-1.2.3-1.el5
libcmpiutil-devel-0.4-2.el5
glibc-2.5-42
glibc-devel-2.5-42
compat-glibc-2.3.4-2.26

RHEL4:

rpm -qa | grep libc
glibc-2.3.4-2.41
libcxgb3-1.1.4-1.el4
libc-client-2002e-14
libcroco-0.6.0-4
libcap-devel-1.10-20
glibc-kernheaders-2.4-9.1.103.EL
compat-libcom_err-1.0-5
glibc-devel-2.3.4-2.41
compat-glibc-2.3.2-95.30
compat-libcom_err-1.0-5
glibc-common-2.3.4-2.41
libcroco-devel-0.6.0-4
libcxgb3-1.1.4-1.el4
libc-client-2002e-14
glibc-utils-2.3.4-2.41
libcap-1.10-20
glibc-headers-2.3.4-2.41
glibc-profile-2.3.4-2.41
libcxgb3-static-1.1.4-1.el4
glibc-devel-2.3.4-2.41
compat-glibc-2.3.2-95.30
like image 194
sunmoon Avatar answered Nov 08 '22 05:11

sunmoon