Dynamic library loading on the Development Board

Source: Internet
Author: User

Http://blog.163.com/seven_7_one/blog/static/162606412201102882946483/

Readers who are not familiar with the actual application of dynamic libraries may have encountered errors such as "error while loading shared libraries". This is typical because the required dynamic library is not in the dynamic linker lD. so.
Specifically, the dynamic linker lD. So searches for the desired dynamic shared library in the following order:
1. The path specified by dt_rpath in the dynamic segment of the elf executable file. This is actually set through a method that is not very common, but more practical: when compiling the target code, you can add the Link parameter "-wl, -rpath "specifies the dynamic library search path;

2. The dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH;
3. The dynamic library path cached in/etc/lD. So. cache (if lD. So. cache is supported ). This can be changed by modifying the dynamic library search path specified in the configuration file/etc/lD. So. conf;

4. Default dynamic library search path/LIB;
5. The default dynamic library search path is/usr/lib.
In the practical application of embedded Linux systems, 1 and 2 are often used. Some relatively simple embedded systems use 4 or 5 paths to regulate dynamic libraries. 3. It is rarely used in embedded systems because many systems do not support lD. So. cache.

The steps 4 and 5 are very simple. You only need to put the required libraries in/lib or/usr/lib to solve the problem that the library cannot be found. However, for larger systems, not easy to manage.
According to the actual situation, the Development Board does not provide the ldconfig command and does not want to compile the kernel or busybox any more. Therefore, it is useless even if you add the path to lD. So. conf. 2, 4, 5 is simple.
First, write a simple dynamic sharing library. The source code pirnt. C is as follows:
1 # include <stdio. h>
2
3 void print_foo ()
4 {
5 printf ("fooooooooo \ n ");
6} compile it into a shared library:
# GCC print. C-shared-O libprint. So
# File libprint. So
Libprint. So: Elf 32-bit LSB shared object, Intel 80386, Version 1 (sysv), not stripped

The main. C code of calling the shared library is as follows: 1 # include <stdio. h>
2
3 extern void print_foo ();
4
5 Int main ()
6 {
7 print_foo ();
8
9 return 0;
10}
The compiled running result is as follows: # GCC main. C-L./-lprint-O pfoo
#./Pfoo
./Pfoo: Error while loading shared libraries: libprint. So: cannot open shared object file: no such file or directory

This is a typical error where the dynamic library cannot be found. Generally, we can specify the search path of the dynamic library by setting the environment variable LD_LIBRARY_PATH (that is, method 2 above). For example, we can run the following statement correctly: # export LD_LIBRARY_PATH = ./

#./Pfoo
Fooooooooo
However, this method has an obvious drawback: Once LD_LIBRARY_PATH is set, it is within the effective range of this environment variable, all other elf executable programs will also search for dynamic libraries in this order, which will inevitably result in a waste of searching.

We can also use another solution to solve this problem, that is, using the "-wl,-rpath" parameter to specify the search path (that is, the above method 1) during compilation ), as follows:
# Unset LD_LIBRARY_PATH
# Echo $ LD_LIBRARY_PATH
# GCC main. C-L./-lprint-O pfoo_r-wl,-rpath = ./
#./Pfoo
./Pfoo: Error while loading shared libraries: libprint. So: cannot open shared object file: no such file or directory

#./Pfoo_r
Fooooooooo
First, we unset LD_LIBRARY_PATH, and we can see that it is no longer valid (of course, this is not a necessary step to use the parameter "-wl,-rpath, here, it is only to show that it no longer works), and the "pfoo" program will also encounter an error where the library cannot be found, and we add the compilation parameter "-wl,-rpath ,. /", The pfoo_r program will run normally.

In fact, we can use the readelf tool to view the differences between the two files:
# Readelf-D pfoo
Dynamic segment at offset 0x514 contains 21 entries:
Tag type name/value
0x00000001 (needed) Shared Library: [libprint. So]
0x00000001 (needed) Shared Library: [libc. so.6]
0x0000000c (init) 0x8048344
0x0000000d (fini) 0x80484e0
0x00000004 (hash) 0x8048128
0x00000005 (strtab) 0x8048240
0x00000006 (symtab) 0x8048170
0x0000000a (strsz) 178 (bytes)
0x0000000b (syment) 16 (bytes)
0x00000015 (Debug) 0x0
0x00000003 (pltgot) 0x80495f8
0x00000002 (pltrelsz) 16 (bytes)
0x00000014 (pltrel) REL
0x00000017 (jmprel) 0x8048334
0x00000011 (rel) 0x804832c
0x00000012 (relsz) 8 (bytes)
0x00000013 (relent) 8 (bytes)
0x6ffffffe (verneed) 0x804830c
0x6fffffff (verneednum) 1
0x6ffffff0 (versym) 0x80482f2
0x00000000 (null) 0x0
[Root @ localhost ldpath] # readelf-D pfoo_r
Dynamic segment at offset 0x518 contains 22 entries:
Tag type name/value
0x00000001 (needed) Shared Library: [libprint. So]
0x00000001 (needed) Shared Library: [libc. so.6]
0x0000000f (rpath) Library rpath: [./]
0x0000000c (init) 0x8048348
0x0000000d (fini) 0x80484e4
0x00000004 (hash) 0x8048128
0x00000005 (strtab) 0x8048240
0x00000006 (symtab) 0x8048170
0x0000000a (strsz) 181 (bytes)
0x0000000b (syment) 16 (bytes)
0x00000015 (Debug) 0x0
0x00000003 (pltgot) 0x8049604
0x00000002 (pltrelsz) 16 (bytes)
0x00000014 (pltrel) REL
0x00000017 (jmprel) 0x8048338
0x00000011 (rel) 0x8048330
0x00000012 (relsz) 8 (bytes)
0x00000013 (relent) 8 (bytes)
0x6ffffffe (verneed) 0x8048310
0x6fffffff (verneednum) 1
0x6ffffff0 (versym) 0x80482f6
0x00000000 (null) 0x0
"Readelf-d" can be used to view the dynamic section of the ELF File ). Comparing the results of pfoo and pfoo_r, we can find that the rpath item exists in pfoo_r, specifying "library rpath: [./]". In this way, we can use a very small price (only increase the space overhead that can be ignored) to specify the optimal search path for each ELF File to improve performance. This is a recommended method. Of course, if the operation depends on a specific software system, simply putting all the databases in/lib is not a simple and easy optimization solution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.