2019-07-30
Keywords: dynamic compilation, static compilation, readelf
introduction
Linux executable program can be divided into two types:
1, dynamic compiled;
2, static compiled.
So, what is the difference between the two?
Dynamic compilation
It refers to dynamic compiler program source code at compile time, if necessary reference an external interface, such as external libraries or external executable program, save only links corresponding to the library. The subsequent application programming interface library or go looking for the system specified library path required at run time.
This mode at compile time only package of its own source code, compile their advantage is the product compact, and there will be no wasted memory space while loading the external interface. The disadvantage is to run more dependent on the integrity of the environment. A system in the absence of any external program needed programming interface will cause the program not to run.
Statically compiled
And the type of relatively dynamic compilation, it means that in addition to its own source code compiler package outside, if you want to refer to the external library interface, but also come packaged together at compile time. I use dynamic compilation type is I just need you to remember where you are on it, and I want to use static compilation is you, you have to come to my side, always stay with me.
Therefore, the static translation of the program, its volume will be relatively large, but it does not rely on the running database environment. It also has a drawback that may cause memory waste. Because inside each executable programs depend on libraries to hold a copy of the will need to use these libraries are packaged together to load into memory. If there are multiple static compiled program will contain the same dependencies, then the situation will cause the library has multiple copies of the same memory footprint.
How to tell the compiler type
We can distinguish the Linux readelf command to an executable program is compiled dynamic or static compiler-based.
readelf -l xxx
If the static compiler-based program, you can see something like the following information
Elf file type is EXEC (Executable file) Entry point 0x8b28 There are 6 program headers, starting at offset 52 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align EXIDX 0x077610 0x0007f610 0x0007f610 0x00768 0x00768 R 0x4 LOAD 0x000000 0x00008000 0x00008000 0x77d7c 0x77d7c R E 0x8000 LOAD 0x077d7c 0x00087d7c 0x00087d7c 0x00c58 0x020dc RW 0x8000 NOTE 0x0000f4 0x000080f4 0x000080f4 0x00020 0x00020 R 0x4 TLS 0x077d7c 0x00087d7c 0x00087d7c 0x00010 0x00028 R 0x4 GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4 Section to Segment mapping: Segment Sections... 00 .ARM.exidx 01 .note.ABI-tag .init .text __libc_freeres_fn __libc_thread_freeres_fn .fini .rodata __libc_subfreeres __libc_atexit __libc_thread_subfreeres .ARM.extab .ARM.exidx .eh_frame 02 .tdata .init_array .fini_array .jcr .data.rel.ro .got .data .bss __libc_freeres_ptrs 03 .note.ABI-tag 04 .tdata .tbss 05
Nothing very special place.
If it is dynamically compiled type, you can see something like the following information
Elf file type is EXEC (Executable file) Entry point 0xe5d0 There are 8 program headers, starting at offset 52 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align EXIDX 0x00bd50 0x00013d50 0x00013d50 0x00008 0x00008 R 0x4 PHDR 0x000034 0x00008034 0x00008034 0x00100 0x00100 R E 0x4 INTERP 0x000134 0x00008134 0x00008134 0x00013 0x00013 R 0x1 [Requesting program interpreter: /lib/ld-linux.so.3] LOAD 0x000000 0x00008000 0x00008000 0x0bd5c 0x0bd5c R E 0x8000 LOAD 0x00c000 0x0001c000 0x0001c000 0x00670 0x006b4 RW 0x8000 DYNAMIC 0x00c01c 0x0001c01c 0x0001c01c 0x00130 0x00130 RW 0x4 NOTE 0x000148 0x00008148 0x00008148 0x00020 0x00020 R 0x4 GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4 Section to Segment mapping: Segment Sections... 00 .ARM.exidx 01 02 .interp 03 .interp .note.ABI-tag .hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .ARM.exidx .eh_frame 04 .init_array .fini_array .jcr .dynamic .got .data .bss 05 .dynamic 06 .note.ABI-tag 07
You can see above in bold red field. Dynamic compiler-based program is run readelf -l command, it will be more of a INTERP segment. This field is described in the loading conditions of this procedure . Here, you want to read and load the program, you must have a presence /lib/ld-linux.so.3 library.
If the system does not have this environment /lib/ld-linux.so.3, in the implementation of this program will be reported No such file or directory error.
Dynamic compiled rely inquiry
If an executable is statically compiled, it is very simple ah, directly run just fine.
But if it is dynamically compiled, sometimes the program may have to rely on the strength of some outside Kufa. Fortunately, we can also view it by readelf commands which depend on the library.
readelf -d xxx
For dynamic compiler-based program, similar to the following types of information will appear
Dynamic section at offset 0xc01c contains 33 entries: Tag Type Name/Value 0x00000001 (NEEDED) Shared library: [libqte.so.3] 0x00000001 (NEEDED) Shared library: [libts-0.0.so.0] 0x00000001 (NEEDED) Shared library: [librt.so.1] 0x00000001 (NEEDED) Shared library: [libpthread.so.0] 0x00000001 (NEEDED) Shared library: [libdl.so.2] 0x00000001 (NEEDED) Shared library: [libstdc++.so.6] 0x00000001 (NEEDED) Shared library: [libm.so.6] 0x00000001 (NEEDED) Shared library: [libgcc_s.so.1] 0x00000001 (NEEDED) Shared library: [libc.so.6] 0x0000000f (RPATH) Library rpath: [/usr/qte338-target2/lib] 0x0000000c (INIT) 0xd684 0x0000000d (FINI) 0x1304c 0x00000019 (INIT_ARRAY) 0x1c000
All the above information is marked with external libraries are running this program to be dependent NEEDED's. We only need to lower these libraries in the system can find the path to run this program the normal.
In Linux, there is a system variable is recorded inventory put path. It is
LD_LIBRARY_PATH
For example, Android, its value may be
LD_LIBRARY_PATH=/vendor/lib:/system/lib
Linux system in general need to find external libraries, are the first to find under / lib and / usr / lib directory, if not found, under LD_LIBRARY_PATH go forth to find the path.