Linux 程式減肥(strip & objcopy)(转载)
转自:http://calamaryshop.blogspot.com/2011/11/linux-strip-objcopy.html
對於設計嵌入式Linux系統的研發人員來說,記憶體的空間是非常斤斤計較的。
嵌入式Linux系統所用的記憶體不是軟碟、硬碟、ZIP 盤、CD-ROM、DVD這些大容量記憶裝置,而是使用的是Rom,Flash,
CompactFlash,
SD...等體積極小,與主板上的BIOS大小相近,存儲容量很小的記憶體。所以怎樣盡可能的節省空間就顯的很重要。嵌入式系統的記憶體中放置的是
Linux核心,文件系統,軟體,以及自己開發的程式。
以一個簡單的C程式來做例子,讓它減肥。
HelloWorld.c:
#include
int main()
{
printf("Hello, World1.\n");
printf("Hello, World2.\n");
printf("Hello, World3.\n");
return 0;
}
先用正常的編譯方法編譯,看看生成的程式的大小是多少
~#gcc –o HelloWorld HelloWorld.c
~# ls HelloWorld -al
-rwxr-xr-x 1 root root 7144 2011-11-25 11:23 HelloWorld
從結果可以看到正常編譯後的程式大小是 7144Byte
~# readelf -S HelloWorld
There are 30 section headers, starting at offset 0x1128:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .interp PROGBITS 08048134 000134 000013 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 08048148 000148 000020 00 A 0 0 4
[ 3] .note.gnu.build-i NOTE 08048168 000168 000024 00 A 0 0 4
[ 4] .hash HASH 0804818c 00018c 000028 04 A 6 0 4
[ 5] .gnu.hash GNU_HASH 080481b4 0001b4 000020 04 A 6 0 4
[ 6] .dynsym DYNSYM 080481d4 0001d4 000050 10 A 7 1 4
[ 7] .dynstr STRTAB 08048224 000224 00004a 00 A 0 0 1
[ 8] .gnu.version VERSYM 0804826e 00026e 00000a 02 A 6 0 2
[ 9] .gnu.version_r VERNEED 08048278 000278 000020 00 A 7 1 4
[10] .rel.dyn REL 08048298 000298 000008 08 A 6 0 4
[11] .rel.plt REL 080482a0 0002a0 000018 08 A 6 13 4
[12] .init PROGBITS 080482b8 0002b8 000030 00 AX 0 0 4
[13] .plt PROGBITS 080482e8 0002e8 000040 04 AX 0 0 4
[14] .text PROGBITS 08048330 000330 00018c 00 AX 0 0 16
[15] .fini PROGBITS 080484bc 0004bc 00001c 00 AX 0 0 4
[16] .rodata PROGBITS 080484d8 0004d8 000035 00 A 0 0 4
[17] .eh_frame PROGBITS 08048510 000510 000004 00 A 0 0 4
[18] .ctors PROGBITS 08049f0c 000f0c 000008 00 WA 0 0 4
[19] .dtors PROGBITS 08049f14 000f14 000008 00 WA 0 0 4
[20] .jcr PROGBITS 08049f1c 000f1c 000004 00 WA 0 0 4
[21] .dynamic DYNAMIC 08049f20 000f20 0000d0 08 WA 7 0 4
[22] .got PROGBITS 08049ff0 000ff0 000004 04 WA 0 0 4
[23] .got.plt PROGBITS 08049ff4 000ff4 000018 04 WA 0 0 4
[24] .data PROGBITS 0804a00c 00100c 000008 00 WA 0 0 4
[25] .bss NOBITS 0804a014 001014 000008 00 WA 0 0 4
[26] .comment PROGBITS 00000000 001014 000023 01 MS 0 0 1
[27] .shstrtab STRTAB 00000000 001037 0000ee 00 0 0 1
[28] .symtab SYMTAB 00000000 0015d8 000410 10 29 45 4
[29] .strtab STRTAB 00000000 0019e8 000200 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
以減小程式的 size。這裏我們用 strip 命令拿掉它們。
~# cp HelloWorld HelloWorld_org
~# strip HelloWorld
~# ll
total 28
drwxr-xr-x 2 root root 4096 2011-11-25 12:01 ./
drwxr-xr-x 19 root root 4096 2011-11-25 11:20 ../
-rwxr-xr-x 1 root root 5496 2011-11-25 12:01 HelloWorld*
-rw-r--r-- 1 root root 156 2011-11-25 11:56 HelloWorld.c
-rwxr-xr-x 1 root root 7144 2011-11-25 12:01 HelloWorld_org*
程式立刻變成 5496Byte了,效果不錯。
2. 用 objcopy 命令
還有什麼是可以拿掉的呢?上一步的 strip 命令只能拿掉一般 symbol
table,有些資訊還是沒拿掉,而這些資訊對於程式的最終執行是沒有什麼影響的。如:.comment; .note.ABI-tag;
.gnu.version 就是完全可以去掉的。所以說程式還有簡化的餘地,我們可以使用 objcopy 命令把它們抽取掉。
~# objcopy -R .comment -R .note.ABI-tag -R .gnu.version HelloWorld HelloWorld_obj
~# ll
total 52
drwxr-xr-x 2 root root 4096 2011-11-25 13:17 ./
drwxr-xr-x 19 root root 4096 2011-11-25 11:20 ../
-rwxr-xr-x 1 root root 5496 2011-11-25 13:15 HelloWorld*
-rw-r--r-- 1 root root 156 2011-11-25 11:56 HelloWorld.c
-rwxr-xr-x 1 root root 5304 2011-11-25 13:17 HelloWorld_obj*
-rwxr-xr-x 1 root root 7144 2011-11-25 12:01 HelloWorld_org*
-rwxr-xr-x 1 root root 5496 2011-11-25 12:02 HelloWorld_strip*
查看一下 HelloWorld_obj為5304Bytes,又少了一點。
比較一下原始bin,strip後的bin,objcopy後的bin:
-rwxr-xr-x 1 root root 7144 2011-11-25 12:01 HelloWorld_org*
-rwxr-xr-x 1 root root 5496 2011-11-25 12:02 HelloWorld_strip*
-rwxr-xr-x 1 root root 5304 2011-11-25 13:17 HelloWorld_obj*
程式容量的減小對嵌入式 Linux系統的設計很有意義,節省了大量空間,使得我們可以利用這部分空間來完善我們的系統,如可再加大Linux核心等等。
Linux 程式減肥(strip & objcopy)(转载)的更多相关文章
- linux进程地址空间详解(转载)
linux进程地址空间详解(转载) 在前面的<对一个程序在内存中的分析 >中很好的描述了程序在内存中的布局,这里对这个结果做些总结和实验验证.下面以Linux为例(实验结果显示window ...
- Linux下高cpu解决方案(转载)
Linux下高cpu解决方案(转载 1.用top命令查看哪个进程占用CPU高gateway网关进程14094占用CPU高达891%,这个数值是进程内各个线程占用CPU的累加值. PID USER ...
- 祝贺 Linux 25 岁:25 个关于 Linux 的惊人真相!【转载】
作者:Javen Fang链接:https://zhuanlan.zhihu.com/p/22222383来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 25 年前的这 ...
- Linux下不同服务器间数据传输--转载
因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...
- Linux文件操作学习总结【转载】
本文转载自: http://blog.csdn.net/xiaoweibeibei/article/details/6556951 文件类型:普通文件(文本文件,二进制文件).目录文件.链接文件.设备 ...
- Linux常用Shell脚本珍藏【转载】
我们在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个linux运维必须学会的一门功课,这里收藏linux运维常用的脚本.如何学好脚本,最关键的 ...
- Linux top和负载的解释(转载)
转载自://www.blogjava.net/freeman1984/archive/2011/12/08/365853.html op命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程 ...
- Linux学习笔记—Linux磁盘与文件系统管理(转载)
认识EXT2文件系统 文件的系统特性 Linux的正规文件系统为Ext2 文件数据除了文件实际内容外,还包括其他属性(文件权限.文件属性). 文件系统将这两部分数据分别存放在不同的块,权限和属性放在i ...
- CentOS 6.3下源码安装LAMP(Linux+Apache+Mysql+Php)环境【转载】
本文转载自 园友David_Tang的博客,如有侵权请联系本人及时删除,原文地址: http://www.cnblogs.com/mchina/archive/2012/11/28/2778779.h ...
随机推荐
- svn服务的安装和使用
更新linux软件库 cat /etc/redhat-release wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.c ...
- Why use async requests instead of using a larger threadpool?(转载)
问: During the Techdays here in the Netherlands Steve Sanderson gave a presentation about C#5, ASP.NE ...
- [2018-08-25]模板引擎Razor Engine 用法示例
好久没写博客了,回宁波后最近几个月一直忙些线下的事情. 敲代码方面脱产有阵子了,生疏了,回头一看,这行业果然更新飞快. 最近线下的事情基本忙完,准备开始干回老本行,最重要的一件事就是升级abplus库 ...
- SQL语法之初级增删改查
SQL语法之初级增删改查 1.增 1.1插入单行 INSERT INTO [表名](列名) VALUES(列值) 语法如下: INSERT INTO bsp_Nproductclass(guid,pi ...
- python基础-pickle与shelve
pickle Example 写入文件 import pickle integers = [1, 2, 3, 4, 5] with open('pickle-example.p', 'wb') as ...
- web性能压力测试工具http_load/webbench/ad
http_load 下载地址:http://www.acme.com/software/http_load/http_load-12mar2006.tar.gz 程序非常小,解压后也不到100K 居家 ...
- struct tm 和 time_t 时间和日期的使用方法(转
关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元) .概念 在C/C++中,对字符串的操作有很多值得注意的问题,同样,C ...
- 机器视觉 Histogram of oriented gradients
Histogram of oriented gradients 简称 HoG, 是计算机视觉和图像处理领域一种非常重要的特征,被广泛地应用于物体检测,人脸检测,人脸表情检测等. HoG 最早是在200 ...
- 从结果推断过程----->使用System.out和Root Device
刚才解决了一个App中更新的逻辑问题.出问题之后发现,有很多处调用了更新,后来都不知道是哪里改写了SharedPreferences. 然后一直在挨个寻找每一处更新的地方,花了很多时间. 最后直接使用 ...
- 示例的libevent的程序
著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:auxten 链接:http://zhuanlan.zhihu.com/auxten/20315482 来源:知乎 /* ...