Create a function library in Linux (1)

Source: Internet
Author: User
1. Introduction

How can we create our ownProgramFunction library? A "program function library" is simply a file that contains some compiledCodeAnd data, the compiled code and data can be used by other programs after the event.The program function library makes the entire program more modular, easier to re-compile, and easier to upgrade. The function library can be divided into three types: static libraries, shared libraries, and dynamically loaded
Libraries ).
The static function library is added to the target program before the program is executed. The shared function library is loaded into the program at the startup of the program, which can be shared by different programs; the function library can be dynamically loaded at any time when the program is running. In fact, the dynamic function library is not another library function format. The difference is how the dynamic loading function library is used by programmers. The following is an example. This document mainly refers to the program library howto, the author is luster (hwang@ustc.edu), any non-commercial purpose re-release this document is allowed, but please keep the author information and this copyright statement. This document is first published at www.linuxaid.com.cn. 2. Static function library The static function library is actually a simple set of common target files. Generally, ". A" is used as the file suffix.You can use the AR program to generate static library files.Ar
Is the abbreviation of archiver. Currently, the static function library is not used as much as before, mainly because the shared function library has many advantages over it. Gradually, everyone enjoys using the shared function library. However, in some places, the static function library is still in use. First, it is compatible with some previous programs, and second, it is relatively simple to describe. Static library functions allow programmers to link the program without re-compiling the code, saving time for re-compiling the code. However, in the face of today's fast computers, recompilation of General programs does not take much time, so this advantage is not as obvious as it was before. Static function libraries are very useful for developers. For example, if you want to use the functions you provide to othersSource codeTo keep it confidential, you can provide a static library file to others. Theoretically, the code generated using static library functions in ELF format can be better than using shared function libraries (or dynamic functions ).
Library. To create a static function library file or add a new target code to an existing static function library file, run the following command:
Ar RCS my_library.a file1.o file2.o
In this exampleTarget code: file1.o and
File2.o is added to my_library.a. If my_library.a does not exist, a new file is created.. When using the AR command to create a static library function, there are other optional parameters that can be used in Ar help. I will not go into details here. Once you create a static function library, you can use it. You can use it as part of your compilation and connection process to generate your executable code. If you use GCC to compile and generate executable code, you can use the "-l" parameter to specify this library function. You can also use LD to use its "-l" and "-l" Parameter options.. For more information, see info: GCC. 3. Shared function library Functions in the shared function library are loaded when an executable program is started. If a shared function library is properly installed, all programs can automatically load the functions in the latest function library when re-running. For Linux systems, there are more functions that can be implemented: O upgrade the function library but allow the program to use the function library of the old version. O when executing a specific program, it can overwrite the specified function in a specific library or library. O you can modify these function libraries when they are used. 3.1. 1 Conventions If the shared function library you want to write supports all the useful features, you must follow a series of conventions during the writing process. You must understand the differences between different library names, such as its "soname" and "real
The difference between names and how they interact. You also need to know where you should put these library functions in your file system. Let's take a look at these issues. 3.1.1 . Name of the Shared Library Each shared function library has a special name called "soname ". The soname name must be prefixed with "lib", followed by the name of the function library, followed by ". So", and finally the version number.However, there is a special case, that is, very underlying C-library functions are not named after Lib. Each shared function library has a real name ("Real Name"), which is a file containing the real library function code. The real name has a major version number and a release version number. The last release version is optional. The main version number and release version number let you know what version of the library function you have installed. In addition, another name is the name of the function library required by the compiler during compilation. This name is a simple soname name without any version number information. The key to managing the shared function library is to differentiate these names. ★ When an executable program needs to list these shared library functions in its own program, it only needs to use soname; ★ In turn, when you want to create a new shared function library, you need to specify a specific file name, which contains detailed version information. ★ When you install a new version of the function library, you only need to copy these function library files to a specific directory and run the ldconfig utility. Ldconfig checks the existing library file, creates a soname symbol to link to the real function library, and sets the/etc/lD. So. cache buffer file. We will discuss this later. Ldconfig does not set the name of the link. The usual practice is to create the name of The Link during installation. Generally, this symbolic link simply points to the latest soname.
Or the latest library file. It is best to point this symbolic link to soname, because after you upgrade your library function, you can automatically use the new function library. Let's take an example:
/Usr/lib/libreadline. so.3
Is a complete soname, ldconfig can set a symbol to link to another real library file, such
/Usr/lib/libreadline. so.3.0. You must also have a link name, such as/usr/lib/libreadline. So.
Is a symbolic link pointing to/usr/lib/libreadline. so.3. 3.1.2 . Location of the library file in the file system The shared function library file must be placed in some specific directories. In this way, the application can correctly use these function libraries by setting system environment variables. Most of the source code development programs follow
For some GNU standards, we can refer to the info help file to get a trusted description. The location of info information is: info:
Standards # directory_variables. The GNU standard recommends that all library files be stored in the/usr/local/lib directory, and that executable programs be stored in the/usr/local/bin directory.. These are some habits that can be changed. Hierarchical FHS (filesystem hierarchy
Standard) (http://www.pathname.com/fhs) specifies that a large part of the library file in a release package should be installed under the/usr/lib directory, however, if some libraries are to be loaded when the system is started, put them in the/lib directory, and put those libraries that are not part of the system itself under/usr/local/lib. There is no essential conflict between the two paths above. The standard proposed by GNU is mainly for developer development source code, while the FHS proposal is for the release version path. For specific location information, see the configuration information in/etc/lD. So. conf. 3.2. How to use these function libraries In GNU glibc-based systems, including all Linux systems, starting an elf binary executable file will automatically start and run a program

Loader. For Linux, the loader name is/lib/ld-linux.so.X (X is the version number ). After the loader is started
Load all other shared function libraries used by this program. In which directories do I find the shared function library? These definitionsBy default, it is stored in the/etc/lD. So. conf file., We can modify this file to add some of our own special path requirements. The/etc/ld of most release packages of the RedHat series. so. the/usr/local/lib directory is not included in the conf file. If this directory is not available, we can modify/etc/lD. so. conf, manually add this entry. If you want to override some functions in a library, replace them with your own functions, and keep other functions in the library, you can go to/etc/lD. so. add the library you want to replace (. ), these preloading library functions will have the right to give priority to loading. When the program starts, searching for all directories is obviously inefficient, so Linux uses a high-speed buffer method. Ldconfig read by default
/Etc/lD. So. conf related information, and then set the appropriate symbolic link, then write a cache to the/etc/lD. So. cache file, and this
/Etc/lD. So. cache can be effectively used by other programs. This method can greatly improve the speed of accessing the function library. This requires that each time you add a Dynamically Loaded function library, you need to run ldconfig to update the cache. If you want to delete a function library or modify the path of a function library, you must re-run ldconfig to update this
Cache. Usually some package managers need to run ldconfig when installing a new function library. In addition, FreeBSD uses different cache files. FreeBSD elf cache is/var/run/ld-elf.so.hints, and A. Out cache is responsible for/var/run/lD. So. hints. They are also updated through ldconfig. 3.3. Environment Variables Various environment variables control some key processes. For example, you can temporarily specify a different function library for one execution of a specific program.In Linux
LD_LIBRARY_PATH can be used to specify the search path of the function library, and this path is usually located before the standard search path.
This is very useful, especially when debugging a new function library or using a non-standard function library in special cases. The environment variable ld_preload lists the library files to be preferentially loaded in all shared function libraries.
/Etc/lD. So. preload is similar. These are implemented by the loader/lib/ld-linux.so. It is worth mentioning that,
LD_LIBRARY_PATH can work normally in most UNIX-Linke systems, but not all systems can be used, such as HP-UX systems
Shlib_path, while libpath is used in Aix. LD_LIBRARY_PATH is frequently used during development and debugging, but it should not be modified by an ordinary user during installation.For more information, see

Http://www.visi.com /~ Barr/ldpath.html. Here is a document specifically designed to explain why LD_LIBRARY_PATH is not used.

Variable. In fact, there are more environment variables that affect the program's call-in process. Their names usually start with LD _ or rtld. Most of the documents used for these environment variables are incomplete. They are usually confusing. If you really want to figure out their usage, you 'd better read the source code of Loader (that is, part of GCC ). Allowing users to control the dynamic link function library involves the setuid/Setgid function, if special functions are required. Therefore, GNU
Loader usually limits or ignores Setuid and setgid These variables are used. If loader judges whether the program is used by judging the environment variables of the program
Setuid or setgid. If the UID and EUID are different, or the GID and EGID are different, loader assumes that the program has used setuid or
Setgid, and then greatly restrict the permission to control the old link. If you read the GNU
Glibc library function source code, you can clearly see this, especially we can look at the elf/rtld. C and sysdeps/generic/dl-sysdep.c these two files. This means that if you make the UID and GID are equal to EUID and EGID respectively, and then call a program, these variables will be fully effective. 3.4.Create a shared function library Now we are going to learn how to create a shared function library. In fact, it is very easy to create a shared function library.First, create an object file, Set This file uses the GCC-FPIC
Add the parameter commands to the shared function library.
PIC indicates the position independent code ). The following is a standard format:

Gcc-shared-wl,-soname, your_soname-O library_name file_list library_list
The following is another example. It creates two object files (A. O and B. O), and then creates a shared function library that contains a. o and B. O. In this example, the "-G" and "-wall" parameters are not required.
Gcc-FPIC-g-C-wall a. cGcc-FPIC-g-C-wall B. CGcc-shared-wl,-soname, liblusterstuff. so.1-O liblusterstuff. so.1.0.1 A. o B. O-lC
Note the following: You do not need to use the-fomit-frame-pointer compilation parameter unless you have. Although this parameter is used, the function library can still be used, but this makes the debugging program almost useless and cannot be traced for debugging. · Use-FPIC to generate code instead of-FPIC. · In some cases, use gcc
To generate the object file, use the option parameter "-wl,-export-dynamic. Generally, the symbol table of the dynamic function library contains the symbols of these dynamic objects.
This option adds all the symbols to the dynamic symbol table when creating an elf file. Refer to the help of LD for more details. 3.5. install and use the shared function library Once you have a shared function library, you also need to install it. In fact, the simple method is Copy your library file to the specified standard directory (for example,/usr/lib) and run ldconfig. If you do not have the permission to do this, for example, you cannot modify
The/usr/lib directory enables you to use these function libraries by modifying your environment variables. First, you need to create these shared libraries. Then, you need to set some symbolic links, especially the symbolic links from soname to the real library file. The simple method is to run ldconfig:
Ldconfig-N directory_with_shared_libraries
Then you can set your LD_LIBRARY_PATH environment variable, which is a set of paths separated by commas. This variable can be used to specify the search path of the shared function library. For example, you can use Bash to start a program my_program:
LD_LIBRARY_PATH =.: $ LD_LIBRARY_PATH my_program
If you need to overload some functions, you need to create an object file that contains the functions to be overloaded, and then set the ld_preload environment variable.Usually you can
You can easily upgrade your function library. If an API changes, the program that creates the library will change the soname. However, if a function upgrades a function library and maintains the original soname, You can forcibly copy the function library of the old version to a certain position, then rename the file (for example, use the original name and add it later. orig suffix), and then create a small "wrapper" script to set this library function and related things. For example:
#! /Bin/sh export LD_LIBRARY_PATH =/usr/local/my_lib: $ LD_LIBRARY_PATH Exec/Usr/bin/my_program.orig $ *
You can run LDD to view the shared function library used by a program. For example, you can see the function library used by the LS utility:
LDD/bin/lsLibtermcap. so.2 =>/lib/libtermcap. so.2 (0x4001c000)Libc. so.6 =>/lib/libc. so.6 (0x40020000)/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)
Generally, I can see a list of soname, including the path. In all cases, you can see at least two databases:
·/Lib/ld-linux.so.N (N is 1 or larger, generally at least 2 ).This is the library that forces loading all other shared libraries.· Libc. So. n (n should be greater than or equal to 6 ). This is the C language function library.
It is worth mentioning that do not run programs that do not trust you
LDD command. In the manual of LDD, LDD is clearly written by setting some special environment variables (for example
In the elf object, set ld_trace_loaded_objects), and then run the program. In this way, a program in a certain place may execute some unexpected code by using LDD, which may lead to insecure risks. 3.6. Incompatible function libraries If a new version of the function library is incompatible with the binary library of the old version, the soname needs to be changed. There are a total of four basic reasons for C language compatibility: O. The text of a function has changed, so that it may not be consistent with the initial definition. O. The output data items have changed. O. Some output functions are deleted. O. Some output function interfaces have changed. If you can avoid this, you can maintain the compatibility of your function library in binary code, or you can make your application binary interface (ABI: Application binary Interface).

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.