In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the contents of so files in Linux system". Interested friends may wish to have a look at them. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the contents of so files in Linux system"?
Introduction to 1.so files: also ELF format files, shared libraries (dynamic libraries), similar to DLL. Save resources, speed up, and simplify code upgrades. It's enough to know so much, pragmatism. Wait until you have an impression before you study the principle.
two。 How to generate and use a so dynamic library file? First write a C file: s.c
# include int count; void out_msg (const char * m) {/ / 2 seconds output information and count for (;;) {printf ("% s% d\ n", m, + + count); sleep (2);}} compile: get the output file libs.o gcc-fPIC-g-c s.c-o libs.o
-fPIC:-fPIC acts in the compilation phase, telling the compiler to generate location-independent code (Position-Independent Code), then the generated code has no absolute address, all using relative addresses, so the code can be loaded into any location in memory by the loader and can be executed correctly. This is exactly what the shared library requires. When the shared library is loaded, the location in memory is not fixed.
-g: causes gcc to generate debugging information, which can be generated using the "native format (native format)" of the operating system. GDB can take advantage of this information directly, and other debuggers can also use this debugging information.
-c: only compile operations are performed, no join operations are performed. -o: specify the name of the generated output file
Be careful! -cjingle Muso does not refer to .c files and .o files!
Get the output file libs.so gcc-g-shared-Wl,-soname,libs.so-o libs.so libs.o-lc
Libs.o in the above statement is the input file
-shared:
-Wl: note that the second letter is lowercase L, not I.
-soname:
The key function of soname is that it provides standards for compatibility:
When upgrading a library in the system, and the soname of the new library is the same as the soname of the old library, the program generated with the old library link will still work properly with the new library. This feature makes it easy to upgrade shared libraries to program and location errors under Linux.
In Linux, applications specify the version of the desired library by using soname, and library authors can declare which versions are compatible by retaining or changing the soname, which frees programmers from the problem of shared library version conflicts.
-lc:
-l is to add the name of a library directly. For example,-lc is the libc library-L is the path of the library. When searching, search under the-L directory first.
A header file: s.h
# ifndef _ MY_SO_HEADER_ # define _ MY_SO_HEADER_ void out_msg (const char * m); # endif, another C file to reference the function in this library: ts.c
# include # include "s.h" int main (int argc, char** argv) {printf ("TS Main\ n"); out_msg ("TS"); sleep (5); / / this sentence can be commented out and opened in section 4. Printf ("TS Quit\ n");} compile and link this file: get the output file ts gcc-g ts.c-o ts-L. -ls
Execute. / ts, hmmm: it worked. And almost got ts:error while loading shared libraries: libs.so: cannot open shared object file: No such file or directory system can not find our own definition of libs.so, so tell him to modify the variable LD_LIBRARY_PATH, for convenience, write a script: e (the file name is called e, too lazy to grow)
Export LD_LIBRARY_PATH=$ {pwd}: ${LD_LIBRARY_PATH}. / ts executes:. / e & there is a constant output of information on the screen. Of course, you can't see the TS Quit. There is an endless loop in front of it, and this sentence will be used later.
--
& put it after the startup parameter to set this process as a background process. By default, the process is the foreground process, so the Shell is occupied, and we cannot do other operations. For those processes that do not interact, we often want to start them in the background. We can add a'&'to the startup parameters to achieve this purpose.
--
3. Address space, and thread safety: what if:. / e & after you start execution, wait a little and then. / thread, what happens to the screen message? How does the global variable count change? The two processes will cross-output information, and their respective count will not interfere with each other, even though they reference the same so file. In other words, there is only the question of whether the code is thread-safe, not whether the code is process-safe.
I haven't taken a closer look at the next one, Khan.
4. Library initialization, parsing: dynamic library loading and unloading under windows will have initialization functions and uninstall functions to complete library initialization and resource recovery. Of course, linux can also be implemented. When the ELF file itself executes, it executes an init () function and a fini () function to do this. We just need to use our own function to allow the system to execute at this time. Modify our previous s.c file:
# include void my_init (void) _ attribute__ ((constructor)); / / tell gcc to throw this function to init section void my_fini (void) _ attribute__ ((destructor)); / / tell gcc to throw this function to fini section void out_msg (const char * m) {printf ("Ok!\ n");} int I / / it is still a counter void my_init (void) {printf ("Init...% d\ n", + + I);} void my_fini (void) {printf ("Fini...% d\ n", + + I);} remaking the libs.so,ts version without recompiling, the code maintenance and upgrade is much more convenient. Then execute:. / e & you can see the screen output: (incomplete information, just in the same order) Init Main OK Quit Fini can see that our own defined initialization functions and parsing functions are executed, and at the front and back. If sleep (5) in s.c is not commented out, there is a chance:. / e &. / e & execute twice in a row, then the initialization and parsing functions will be executed twice, even though the system loads libs.so only once. If kill drops the background process during sleep, the parsing function will not be executed.
5. Replace the system function with the function in our own library: create a new file b.c: we will replace the system function malloc and free (you can write your own memory leak detection tool)
# include void* malloc (int size) {printf ("My malloc\ n"); return NULL;} void free (void* ad) {printf ("My free\ n");} Old rule, compile and link to a so file: get libb.so gcc-fPIC-g-c b.c-o libb.o gcc-g-shared-o libb.so-lc modify s.c: regenerate libs.so
Void out_msg () {int* p; p = (int*) malloc (100); free (p); printf ("Stop Ok!\ n");} modify script file e:
The key to export LD_PRELOAD=$ {pwd} libb.so:$ {LD_PRELOAD} export LD_LIBRARY_PATH=$ {pwd}: ${LD_LIBRARY_PATH}. / ts is on LD_PRELOAD. The so specified in this path will be loaded before all so, and the symbol will overwrite the symbol in the so file that is loaded later. If the permissions of the executable file are not appropriate (SID), this variable is ignored. Execute:. / e & well, you can see our malloc,free work.
At this point, I believe you have a deeper understanding of "what are the contents of so files in Linux system?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.