For developers who design embedded Linux systems, there is a problem that must be taken into account, and that is the space of the memory.
We know that embedded Linux systems use memory that is not a floppy disk, hard drive, Zip disk, CD-ROM, DVD these well-known large-capacity conventional memory, it uses such as Rom,compactflash,m-systems diskonchip, Sony's MEMORYSTICK,IBM Microdrive and so on are very small, similar to the BIOS on the motherboard, the storage capacity of the memory is minimal. So how to save space as much as possible is significant.
Embedded systems are placed in the memory of the kernel, file system, software, as well as the development of their own programs. This article starts from the program, with a very simple C program as an example, through three steps to let it lose weight.
HELLO.C:
#include
int main ()
{
printf ("Hello,world");
return 0;
}
Let's start with the normal compilation method to see what the size of the generated program is
#gcc –o Hello hello.c
#ls –l Hello
-rwxr-xr-x 1 root root 11542 Nov 20:07 Hello
From the results you can see that the normal compiled program size is 11542Byte
Now let's start with three steps to lose weight and see how it works.
Step one: Use gcc code to optimize parameters
Code optimization refers to the compiler to analyze the source code, find the part which has not yet reached the optimal, and then regroup it to improve the execution performance of the program. GCC provides a very powerful code optimization function, which controls the generation of optimized code by compiling option-on, where n is an integer that represents the optimization level. For different versions of GCC, the range of values for n and its corresponding optimizations may not be exactly the same, and the typical range is from 0 to 2 or 3.
Compile-time Use option-O can tell GCC to reduce the length and execution time of the code at the same time, the effect is equivalent to-o1. The types of optimizations that can be performed at this level, while depending on the target processor, generally include both thread jump (threadjump) and deferred fallback (Deferred stackpops) optimizations. Option-o2 tells GCC that in addition to completing all the-o1 level optimizations, there are some additional adjustments, such as processor instruction scheduling. The option-o3, in addition to completing all the-o2-level optimizations, includes looping and other optimizations related to processor characteristics. In general, the higher the number of optimizations, the greater the level of optimization, which also means that the program is running faster. Many Linux programmers like to use the-O2 option because it has an ideal balance between optimizing length, compile time, and code size.
#gcc –o2–o Hello hello.c
#ls –l Hello
-rwxr-xr-x 1 root root 11534 Nov 20:09 Hello
Optimized the size of the program is 11534Byte, compared to the results of normal compilation 11542Byte does not seem much smaller, but do not worry, this is the first step. We went on.
Step two: Use the Strip command
We know that binary programs contain a lot of symbolic information (symboltable), and part of it is used to provide the necessary help for GDB to debug. You can find these symbolic information by 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 Dynsym
[5]. Dynstr Strtab
[6]. Gnu.version Versym
[7]. Gnu.version_r Verneed
[8]. Rel.dyn rel
[9]. REL.PLT rel
[Ten]. Init progbits
[one]. PLT progbits
[A]. Text progbits
[Fini progbits
[A]. Rodata progbits
[m]. Eh_frame progbits
[A]. Data progbits
[+]. Dynamic dynamic
[A]. Ctors progbits
[Dtors]. progbits
[m]. JCR progbits
[Got]. Progbits
[Nobits]. BSS
[Comment]. progbits
[Debug_aranges]. progbits
[Debug_pubnames]. progbits
[+]. Debug_info progbits
[Debug_abbrev]. progbits
[Debug_line]. progbits
[Debug_frame]. progbits
[A]. Debug_str progbits
[+]. Shstrtab strtab
[Symtab]. Symtab
[Strtab]. Strtab
Similar to. debug_xxxx is used for GDB debugging. Removing them will not affect program execution but also reduce the size of the program. Here we take them off by the strip command.
#strip Hello
#ls –l Hello
-rwxr-xr-x 1 root root 2776 Nov 20:11 Hello
The program immediately becomes 2776Byte, the effect is good. Let's redouble our efforts and take the final step.
Step three: Use the objcopy command
The last step Strip command can only take off the general symboltable, some information is still not taken away, and this information for the final implementation of the program has no effect. such as:. Comment; note. Abi-tag;. Gnu.version can be completely removed. So there's still room for simplification, and we can use the objcopy command to extract them out.
#objcopy –R. comment–r. Note. Abi-tag–r. Gnu.version Hellohello1
#ls –l Hello1
-rwxr-xr-x 1 root 2316 Nov 20:23 hello1
To this step, the program's weight loss is completed, we can see the program from the normal compiled 11542Byte suddenly gradually less to 2316Byte, the effect is very obvious.
Summary
The reduction of program capacity is undoubtedly of great significance to the design of embedded Linux system, which saves us a lot of space so that we can use this part of space to perfect our system, such as enlarging the kernel and so on, after all, this is our ultimate goal.
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.