For developers who design embedded Linux systems, there is a problem that must be taken into account, that is, the storage space.
We know that the memory used in the embedded Linux system is not the well-known large-capacity general memory, such as hard disk, hard disk, zipdisk, CD-Rom, and DVD. It uses Rom, compactflash, the diskonchip of M-systems, memorystick of Sony, and microdrive of IBM are positive and small, which are similar to the BIOS size on the motherboard and have a small storage capacity. Therefore, it is important to save space as much as possible.
The memory of the embedded system is nothing more than the kernel, file system, software, and self-developed programs. This article starts with a program and uses a very simple C program as an example to let it lose weight in three steps.
Hello. C:
# Include
Int main ()
{
Printf ("Hello, world ");
Return 0;
}
We first use the normal compilation method to compile and check the size of the generated program.
# Gcc-O hello. c
# Ls-l Hello
-Rwxr-XR-x 1 Root 11542 Nov 13 hello
The result shows that the size of the program after normal compilation is 11542 bytes.
Now let's take three steps to lose weight and see how it works.
Step 1: Use the GCC code to optimize parameters
Code optimization means that the compiler analyzes the source code, finds out the part that has not yet reached the optimal value, and re-combines it to improve the execution performance of the program. The code optimization function provided by GCC is very powerful. It controls the generation of optimization code through the compilation option-on, where n is an integer representing the optimization level. For GCC of different versions, the value range of N and its corresponding optimization effects may be different. The typical range is from 0 to 2 or 3.
The option-O can be used during compilation to tell GCC to reduce the code length and execution time at the same time. The effect is equivalent to-O1. The types of optimizations that can be performed at this level depend on the target processor, but they generally include thread jump and deferred stack pops. Option-O2 tells GCC not only to complete all-O1-level optimization, but also to make some extra adjustments, such as processor command scheduling. In addition to completing all-O2-level optimization, option-O3 also includes loop expansion and other optimizations related to the processor features. Generally, the larger the number, the higher the optimization level, and the faster the program runs. Many Linux programmers like to use the-O2 option because it achieves an ideal balance between the optimized length, Compilation Time, and code size.
# Gcc-O2-O hello. c
# Ls-l Hello
-Rwxr-XR-x 1 Root 11534 Nov 13 hello
The size of the optimized program is 11534 bytes, which is not much smaller than the normal compilation result 11542byte, but don't worry. This is the first step. Let's proceed.
Step 2: Use the Strip command
We know that a binary program contains a large amount of symbolic table, and some of them are used to provide necessary help for GDB debugging. You can view the symbolic information through readelf-s.
# Readelf-s hello
Section headers:
[Nr] Name type
[0] Null
[1]. interp progbits
[2]. Note. Abi-tag note
[3]. Hash hash
[4]. dynsym
[5]. dynstr strtab
[6]. GNU. Version versym
[7]. GNU. version_r verneed
[8]. Rel. Dyn REL
[9]. Rel. PLT REL
[10]. init progbits
[11]. PLT progbits
[12]. Text progbits
[13]. Fini progbits
[14]. rodata progbits
[15]. eh_frame progbits
[16]. Data progbits
[17]. Dynamic dynamic
[18]. ctors progbits
[19]. dtors progbits
[20]. JCR progbits
[21]. Got progbits
[22]. BSS nobits
[23]. Comment progbits
[24]. debug_aranges progbits
[25]. debug_pubnames progbits
[26]. debug_info progbits
[27]. debug_abbrev progbits
[28]. debug_line progbits
[29]. debug_frame progbits
[30]. debug_str progbits
[31]. shstrtab strtab
[32]. symtab
[33]. strtab
Similar to. debug_xxxx, it is used to debug GDB. Removing them will not only affect program execution, but also reduce the program size. Here we use the Strip command to remove them.
# Strip hello
# Ls-l Hello
-Rwxr-XR-x 1 Root 2776 Nov 13 hello
The program immediately changes to 2776byte. The effect is good. Let's make persistent efforts and proceed to the last step.
Step 3: Use the objcopy command
The strip command in the previous step can only remove the general symbol table, but some information is still not removed, which has no effect on the final execution of the program. For example:. comment;. Note. Abi-tag;. GNU. version can be completely removed. So there is still room for simplifying the program. We can use the objcopy command to extract them.
# Objcopy-R. comment-R. Note. Abi-tag-R. GNU. Version Hello hello1
# Ls-l hello1
-Rwxr-XR-x 1 Root 2316 Nov 13 hello1
In this step, the program's weight loss is complete. We can see that the program is gradually reduced from the normal compiled 11542 bytes to 2316 bytes, and the effect is very obvious.
Summary
The reduction of program capacity is undoubtedly of great significance to the design of the embedded Linux system. It saves us a lot of space, so that we can use this space to improve our system, for example, increasing the kernel. After all, this is our ultimate goal.
Author Profile
Name: leikai
Work Unit: Technical Promotion Board (Suzhou) R & D center
Contact address: R & D center of mayun luoli Technology Co., Ltd., Suzhou New District, 215000
E-mail: tigerleihm@yahoo.com.cn
"The author of this article is an engineer at the R & D center of leikai Technology Board (Suzhou. He is currently working at the R & D center of Suzhou Technology Promotion Board (Suzhou) in China. You can contact him through the tigerleihm@yahoo.com.cn ."