Linux programs lose weight three steps away

Source: Internet
Author: User

For designing embeddedFor developers of Linux systems, there is a problem that must be taken into account, which is the memory space.
We know that embeddedThe storage used by the Linux system is not a floppy disk,Zip disk,CD-ROM,DVD of these well-known large-capacity conventional memory, which is used for exampleRom, CompactFlash,M-systems'sDiskonchip,Sony'sMemoryStick,IBM'sMicrodrive such as small size, with the motherboard on theThe BIOS is similar in size and has very small memory capacity. So how to save as much space as possible is very important.
Embedded system storage is nothing more than the kernel, file system, software, as well as the development of their own programs. This article begins with a program, with a very simpleC program to use as an example, three steps to make it lose weight.
HELLO.C:
#include
int main ()
{
printf ("Hello,world");
return 0;
}
Let's start with a normal compile 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 is11542Byte, now start our three-step weight loss and see how it works.
Step one: with gcc the Code optimization parameters
Code optimization means that the compiler analyzes the source code, finds the part that is not yet optimal, and then re-combines it to improve the execution performance of the program.The code optimization features provided by GCC are very powerful, and they are compiled with options-on to control the generation of optimized code, whereN is an integer representing the level of optimization. For different versions ofGCC speaking,The value range of n and its corresponding optimization effect may not be exactly the same, the typical range is from0 changes to2 or3.
Compile-Time Use options-O can tellGCC also reduces the length and execution time of the code, and its effect is equivalent to-o1. The types of optimizations that can be made at this level, although dependent on the target processor, generally include thread jumps (Thread jump) and deferred fallback (Deferred Stack Pops) two optimizations. Options-o2 toldGCC, in addition to completing all-o1 level of optimization, but also some additional adjustment work, such as processor instruction scheduling. Options-o3 In addition to the completion of all-o2-level optimizations include loop unwinding and other optimizations related to processor characteristics. Generally speaking, the higher the number, the higher the level of optimization, and it means that the program will run faster. ManyLinux programmers like to useThe-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
The size of the optimized program is11534Byte, than the result of normal compilation11542Byte doesn't seem like much, but don't worry, that's the first step. We went on to go down.
Step two: Use strip Command
We know that the binary program contains a lot of symbolic information(symbol table), part of which is used toGDB provides the necessary help in addition to the error. can be done byReadelf–s see these symbolic information.
#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
[All]. PLT Progbits
[]. Text progbits
[]. Fini progbits
[Rodata]. progbits
[]. Eh_frame progbits
[+]. Data progbits
[+]. Dynamic dynamic
[Ctors]. progbits
[+]. Dtors progbits
[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
[Debug_str]. progbits
[to]. Shstrtab Strtab
[Symtab]. Symtab
[Strtab]. Strtab
Similar to. The debug_xxxx is used toGDB is in addition to the wrong. Removing them will not affect the execution of the program and can reduce the program'sSize Here we passStrip command to take them off.
#strip Hello
#ls –l Hello
-rwxr-xr-x 1 root root 2776 Nov 20:11 Hello
The program immediately becomes2776Byte, the effect is good. Let's keep it up., take the final step.
Step Three: Use objcopy Command
Of the previous stepstrip command can only take away the general symbol table, some information is still not taken away, and this information for the final execution of the program is not any impact. such as :.comment;. Note. Abi-tag: Gnu.version is completely possible to remove. So there is room for simplification, and we can use the objcopy command to extract them out.  
#objcopy –R. comment–r. Note. Abi-tag–r. gnu.version Hello hello1 
#ls –l hello1 
-rwxr-xr-x 1 root root 2316 Nov 20:23 hello1 
     to this step, the program of weight loss is complete, we can see the program by the normal compilation of 11542byte  suddenly gradually less to 2316byte, the effect is very obvious.  
summary  
    
The reduction of the program capacity is undoubtedly of great significance to the design of the embedded linux system, which saves us a lot of space so that we can use this space to perfect our system  

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.