Linux has dynamic libraries and static libraries. Dynamic libraries use the .so extension, and static libraries have the .a extension. Both are widely used. This article focuses on dynamic library knowledge.
What about linked redundant libraries?
Basically every linux program will have at least one dynamic library, view a program using those dynamic library, use the ldd command to view
So much so, yes. Using sod to display so, not all sos need to be used. Here's an example:
Main.cpp
Compile the result with the default parameters
If I link some so, but the program does not use these so, what is the situation? Below I joined the link compression library, math library, thread library
Look, although not used, but there are links come in, then look at the program startup has not loaded them?
Look, there is loading! ! !
Therefore, it will definitely affect the speed of the process, so we won't compile the useless so in the end. What will happen here?
We all know that Linux is going to be a process (a process or a process) from a program (program or object). What steps must be taken? If this is done in detail, it is estimated that another article should be opened. Simply put it in three steps:
1, fork process, in the kernel to create process-related kernel items, load the process executable file;
2, find the dependent so, one by one to load the mapped virtual address
3, initialize the program variables.
It can be seen that the more DLL dependencies in the second step, the slower the process starts, and when the program is released, the links that are not used are also to be released together. Otherwise, the process will fail when it is started. So. So we can't link some meaningless so links like the above, wasting resources. But developers write makefiles are generally not so careful, drawing convenience, then what is a good approach. Keep watching and the following will give you a solution.
First use the ldd-u demo to see the so-so that does not need to link, see below, on the one hand, useless so all exposed it
Use the -Wl,--as-needed compile option
Oh, the solution is very simple and easy, this article mainly talk about so-dependent issues, the following will introduce some unknown secrets of so path.
The unknown secret of the library path
We know that the Linux link has two ways: display and implicit. The so-called display is that the program actively calls dlopen to open the relevant so; what needs to be added is that if the display link is used, none of the problems discussed in the previous article exist. First of all, dlopen's use of ldd is not visible. Secondly, using dlopen to open so does not load the map when the process starts, but only load the so when the process is running to call dlopen code, that is, if each process displays the link a.so; but if it is released When the program forgets to publish the a.so, the program can still start normally, even if the running logic does not trigger the call to the dlopen function code. The program will work even if there is no a.so.
Since there are so many advantages of display loading, why is it that few code farmers use it in actual production? The main reason is that it is not very convenient to use, and developers need to write a lot of code. Therefore, it is not used by most code farmers. Another important reason is that they can find errors in advance. When they are deployed, they can find which sos are missing, instead of waiting until the actual upper limit is run to find out what is missing.
Let's extend this section by taking the most frequently encountered problem in our work.
Write the simplest so, tmp.cpp
Compilation => link = "run, the main.cpp in the following instructions, see the previous article.
[stevenrao]$ g++ -fPIC -c tmp.cpp
[stevenrao]$ g++ -shared -o libtmp.so tmp.o
[stevenrao]$ mv libtmp.so /tmp/
[stevenrao]$ g++ -o demo -L/tmp -ltmp main.cpp
[stevenrao]$ ./demo
./demo: error while loading shared libraries: libtmp.so: cannot open shared object file: No such file or directory
[stevenrao]$ ldd demo
Linux-vdso.so.1 => (0x00007fff7fdc1000)
Libtmp.so => ​​not found
This mistake is the most common mistake. The dependent so cannot be found when running the program. Most people use the method is to modify the LD_LIBRARY_PATH environment variable
Export LD_LIBRARY_PATH=/tmp
[stevenrao]$ ./demo
Test
This is OK, but export is valid only for the current shell. When you open another shell, you have to set it again. The export LD_LIBRARY_PATH=/tmp statement can be written to ~/.bashrc so that it is valid for the current user. Writing to /etc/bashrc is valid for all users.
Using -L/tmp/ -ltmp in the previous link is a way to set relative paths, and there is an absolute path link method.
[stevenrao]$ g++ -o demo /tmp/libtmp.so main.cpp
[stevenrao]$ ./demo
Test
[stevenrao]$ ldd demo
Linux-vdso.so.1 => (0x00007fff083ff000)
/tmp/libtmp.so (0x00007f53ed30f000)
Although the absolute path applies to set the environment variable step, but the defect is also fatal, this SO must be placed in the absolute path, can not be placed elsewhere, which brings a lot of trouble to the deployment. Therefore, the use of the absolute path link so should be prohibited.
There are two kinds of search paths, one is the search path when linking, and the other is the search path during the run time. As mentioned earlier, -L/tmp/ is a search path that belongs to the link period, that is, when the compiled link provided by the ld program looks for a dynamic library path, and LD_LIBRARY_PATH belongs to both the link-period search path and the search path during the run-time period.
Here we need to introduce the chain-rpath link option, which is the search path that is used when specifying the runtime. The smart classmate immediately thought of the runtime search path, where it was recorded. Also like LD_LIBRARY_PATH, every deployment of a machine will need to match it. Oh, no.., because it has been hard-coded inside the executable. Take a look at the demo below
1. [stevenrao] $ g++ -o demo -L /tmp/ -ltmp main.cpp
2. [stevenrao] $ ./demo
3. ./demo: error while loading shared libraries: libtmp.so: cannot open shared object file: No such file or directory
4. [stevenrao] $ g++ -o demo -Wl,-rpath /tmp/ -L/tmp/ -ltmp main.cpp
5. [stevenrao] $ ./demo
6. test
7. [stevenrao] $ readelf -d demo
8.
9. Dynamic section at offset 0xc58 contains 26 entries:
10. Tag Type Name/Value
11. 0x0000000000000001 (NEEDED) Shared library: [libtmp.so]
12. 0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
13. 0x0000000000000001 (NEEDED) Shared library: [libm.so.6]
14. 0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
15. 0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
16. 0x000000000000000f (RPATH) Library rpath: [/tmp/]
17. 0x000000000000001d (RUNPATH) Library runpath: [/tmp/]
Look at it, compiled into the elf file, the path and the program deeply coupled together.
Innosilicon Machine:Innosilicon A10 ETHMaster (500Mh),Innosilicon A10 Pro ETH (500Mh),Innosilicon A10 Pro+ ETH (750Mh),Innosilicon A11 Pro ETH (1500Mh)
Innosilicon is a worldwide one-stop provider of high-speed mixed signal IPs and ASIC customization with leading market shares in Asian-Pacific market for 10 consecutive years. Its IP has enabled billions of SoC's to enter mass production, covering nodes from 180nm to 5nm across the world`s foundries including: GlobalFoundries, TSMC, Samsung, SMIC, UMC and others. Backed by its 14 years of technical expertise in developing cutting-edge IPs and ASIC products, Innosilicon has assisted our valued partners including AMD, Microchip and Microsoft to name but a few, in realizing their product goals.
Innosilicon team is fully devoted to providing the world's most advanced IP and ASIC technologies, and has achieved stellar results. In 2018, Innosilicon was the first in the world to reach mass production of the performance-leading GDDR6 interface in our cryptographic GPU product. In 2019, Innosilicon announced the availability of the HDMI v2.1 IP supporting 4K/8K displays as well as our 32Gbps SerDes PHY. In 2020, we launched the INNOLINK Chiplet which allows massive amounts of low-latency data to pass seamlessly between smaller chips as if they were all on the same bus. With a wide range of performance leading IP in multiple FinFET processes and 22nm planar processes all entering mass production, Innosilicon's remarkable innovation capabilities have been proven in fields such as: high-performance computing, high-bandwidth memory, encrypted computing, AI cloud computing, and low-power IoT.
Innosilicon Machine,A11 Pro 1500M Miner,Asic Miner A11 Pro 8G,A11 Pro 8G 1500Mh,ETC miner
Shenzhen YLHM Technology Co., Ltd. , https://www.hkcryptominer.com