diff options
author | FreeArtMan <dos21h@gmail.com> | 2022-08-12 14:51:12 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2022-08-12 14:51:12 +0100 |
commit | 4fe12ecb448214f5c449ad5b821df5d450ade76f (patch) | |
tree | b0343194a768339159d710b83d36299af440c487 /md/notes/undefined_c/titles.md | |
parent | 476d1f5deaf8a2982256d0d978a3ce99ae68723e (diff) | |
download | md-content-4fe12ecb448214f5c449ad5b821df5d450ade76f.tar.gz md-content-4fe12ecb448214f5c449ad5b821df5d450ade76f.zip |
Add shared
Diffstat (limited to 'md/notes/undefined_c/titles.md')
-rw-r--r-- | md/notes/undefined_c/titles.md | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/md/notes/undefined_c/titles.md b/md/notes/undefined_c/titles.md index 158ed20..7753e65 100644 --- a/md/notes/undefined_c/titles.md +++ b/md/notes/undefined_c/titles.md @@ -553,6 +553,88 @@ https://panthema.net/2013/0124-GCC-Output-Assembler-Code/ https://blogs.oracle.com/linux/post/making-code-more-secure-with-gcc-part-1 +### Shared library + +Shared library is common way how to reuse big chunks of code. + +```c +#include <stdio.h> +int fun1() { + return 1; +} + +int fun2() { + printf("Function name fun2\n"); +} + +int fun3(int a, int b) { + return a+b; +} +``` + +``` +$ gcc -c lib_share.c +$ gcc -shared -o lib_share.so libshare.o +$ ldd lib_share.so + linux-vdso.so.1 (0x00007ffdb994d000) + libc.so.6 => /usr/lib/libc.so.6 (0x00007f0c39400000) + /usr/lib64/ld-linux-x86-64.so.2 (0x00007f0c39835000) +``` + +Now lets link to our binary +```c +#include <stdio.h> + +//functions that are implemented in shared lib +int fun1(); +int fun2(); +int fun3(int a, int b); + +int main() { + fun1(); + fun2(); + fun3(); +} +``` + +``` +$ gcc -L. -lshare use_share.c -o use_share +./use_share +./use_share: error while loading shared libraries: libshare.so: cannot open shared object file: No such file or directory +ldd ./use_share + linux-vdso.so.1 (0x00007ffedcad5000) + libshare.so => not found + libc.so.6 => /usr/lib/libc.so.6 (0x00007f7b99a00000) + /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f7b99c90000) +``` + +Library is not in search path +``` +$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd` +$ ./use_share +$ ldd use_share + linux-vdso.so.1 (0x00007fffc415c000) + libshare.so => /your/path/libshare.so (0x00007f48b03c6000) + libc.so.6 => /usr/lib/libc.so.6 (0x00007f48b0000000) + /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f48b03d2000) +``` + +Other way is to set custom library search location. Lets set it to search in current directory. +And no need to modify LD_LIBRARY_PATH + +``` +$ gcc use_share.c -o use_share -L. -lshare -Wl,-rpath=./ +$ ldd ./use_share + linux-vdso.so.1 (0x00007fff5c964000) + libshare.so => ./libshare.so (0x00007f791000f000) + libc.so.6 => /usr/lib/libc.so.6 (0x00007f790fc00000) + /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f791001b000) +``` + +So now executable runs libshare from local directory. Ofc there is possible to install shared library into systems /usr/lib + +### Static library + ### Static binary Static binary don't use any shared libraries, and its possible to built it once and distribute on other platforms @@ -593,8 +675,19 @@ $ ldd static_elf Statically compiled file should work on most platforms. + + ### Dynamic binary +Dynamic binary is required to load dynamically linked libraries, they must be in known location. + +Replacing dynamic libraries with your own + + + +Point to custom location of dynamic library + + ### stdin,stdout,stderr @@ -605,6 +698,7 @@ Statically compiled file should work on most platforms. + ## Basic usage ### File manipulation with libc |