Weight-loss program around anywhere [author: Leikai topic I modified a bit]

xiaoxiao2021-03-06  20

Go to the private house for bird brother Linux

You can find related connections in this blog for the PDF format for the development of the embedded Linux system, there is a question to be considered, that is

The space of the memory.

We know that the memory used by the embedded Linux system is not a flop disc, a hard disk, a zip disk, a CD-ROM, DVD.

Some "well-known large-capacity conventional memory, it is, for example, ROM, CompactFlash, M-Systems

Diskonchip, Sony's Memorystick, IBM Microdrive and other volumes, with BIOS on the motherboard

Poor size, memory capacity, small memory. So how to save space as much as possible.

The memory of the memory of the embedded system is not unreliable to the kernel, file system, software, and the program they develop.

This article starts from the program, with a very simple C program? Example, through three steps to lose weight.

Hello.c:

#include

int main ()

{

Printf ("Hello, World");

Return 0;

}

Let's compile with a normal compilation method to see how much the generated program is

#gcc - o Hello Hello.c

#ls - l Hello

-RWXR-XR-x 1 root root 11542 NOV 13 20:07 Hello

From the results, you can see the normal compiled program size is 11542byte.

Now we start our three steps to lose weight, see how the effect is.

Step 1: Optimize parameters with GCC code

The code optimization refers to the compiler through the analysis source code, find out the part that has not yet reached the best, and then heavy

The new combination is to improve the performance performance of the program. The code optimization function provided by GCC is very powerful, it

The compilation option -on controls the generation of optimized code, where N is an integer representing the optimization level. No

In terms of the same version of GCC, N value range and its corresponding optimization effect may not be the same, more typical

The range is from 0 to 2 or 3.

When compiling, use -O -O can tell GCC to reduce the length and execution time of the code, its effect is equivalent to

-O1. Although the type of optimization on this level depends on the target processor, it is generally included.

Thread Jump and Deferred Stack POPS. Option -o2

In addition to the optimization of all-O1 levels, v. GCC, while paying some additional adjustments,

Protection command scheduling, etc. Option -o3 In addition to completing all-O2 levels, it also includes ring circles and

Some other optimization work related to processor features. Generally, the higher the number of optimized levels, the higher the level, while

It means that the faster running speed is run. Many Linux programmas prefer to use -O2 options, because it is

Optimizing the length, the compilation time, and the code size have achieved a relatively ideal balance point.

#gcc - o2 - o hello hello.c

#ls - l Hello

-RWXR-XR-x 1 root root 11534 NOV 13 20:09 Hello

The size of the optimized program is 11534byte, which does not seem to be small than the result of normal compilation.

No need to worry, this is the first step. We will follow down.

Step 2: Command with strip

We know that the binary program contains a large number of symbol tables, is some of them?

GDB offnsions provide necessary help. These symbols can be viewed via 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 Stutab

[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] .e_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] .ssymtab SymTab

[33] .strtab Strtab

Similar to .debug_xxxx is used for GDB to be deactivated. Removing them not only affects the implementation of the program

To reduce the Size of the program. Here we take them from the strip command.

#Strip Hello

#ls - l Hello

-RWXR-XR-x 1 root root 2776 NOV 13 20:11 Hello

The program turned immediately into 2776byte, the effect is good. Let us make persistent efforts to carry out the final step.

Step 3: Use the objcopy command

The last strip command can only take off the general Symbol Table, some information still didn't take it, and these information

Is there anything about the final implementation of the program? Such as: .comment ;.note.abi-tag; .net.Version

It can be completely removed. So the stylus also has a simplified room, we can use the Objcopy command to put them

Extract it.

#objcopy - r .comment - r.Note.abi-tag - r .gnu.version Hello Hello1

#ls - l hello1

-rwxr-xr-x 1 root root 2316 NOV 13 20:23 Hello1

To this step, the prime weight loss is complete, we can see the items that have been compiled by the 11542byte

As more than 2316byte, the effect is very obvious.

summary

The reduction in program capacity is undoubtedly important for the design of the embedded Linux system. We save big

Space, so that we can use this part of space to improve our system, such as increasing the kernel, etc., after all, this

It is our ultimate goal.

About the Author

Name: Lekai

Work unit: promotion motherboard (Suzhou) R & D center

Address: Luo Li Technology Co., Ltd. R & D Center, Suzhou New District, Zip Code 215000

E-mail:

Tigerleihm@yahoo.com.cn

"The author of this article is the engineer of the Raytan R & D, the R & D center. He is currently in Suzhou, Suzhou, China

(Suzhou) R & D center work. able to pass

转载请注明原文地址:https://www.9cbs.com/read-43788.html

New Post(0)