LinuxC ELF优化

本文详细介绍了使用GCC编译器的不同选项对C程序进行编译,包括默认构建、调试目的和优化目的的设置。通过`gcc`命令的不同参数,如`-g0`、`-g`、`-O0`、`-O1`、`-O2`、`-O3`以及`strip`工具,展示了如何生成和分析二进制文件的特性,如是否包含调试信息、是否被剥离符号等。这些编译选项对于理解和优化程序性能至关重要。
摘要由CSDN通过智能技术生成

1. default build

$gcc helloworld.c  -o default.elf
$file default.elf
default.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=fe1fe362cb4e81ba401e5904b56e981941117fe7, for GNU/Linux 3.2.0, not stripped

2. debug purpose

在这里插入图片描述

	# g0 == default
	$gcc -g0 helloworld.c -o g0_hello.elf
	$file g0_hello.elf 
	g0_hello.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=fe1fe362cb4e81ba401e5904b56e981941117fe7, for GNU/Linux 3.2.0, not stripped
	$gcc -g1 helloworld.c -o g1_hello.elf
	$file g1_hello.elf
	g1_hello.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=8c1d21a2f78c63b06fc4636f6a8d0502292bda40, for GNU/Linux 3.2.0, with debug_info, not stripped
	$gcc -g helloworld.c -o g_hello.elf
	$file g_hello.elf
	g_hello.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=661921e05d61b8108316526de2cbe30551f5fec6, for GNU/Linux 3.2.0, with debug_info, not stripped
	$gcc -g3 helloworld.c -o g3_hello.elf
	$file g3_hello.elf
	g3_hello.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=4995b637d6284e89d19a699d40d6947ad5ffc6e6, for GNU/Linux 3.2.0, with debug_info, not stripped

3. strip purpose

	$gcc -s helloworld.c  -o s_hello.elf
	s_hello.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), 
	$file s_hello.elf
	dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=1d22b7327febb246050c6237d295e0d78d04f339, for GNU/Linux 3.2.0, stripped
	$nm s_hello.elf
	nm: s_hello.elf: no symbols
	$gcc helloworld.c -o hello.elf
	$file hello.elf 
	hello.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=fe1fe362cb4e81ba401e5904b56e981941117fe7, for GNU/Linux 3.2.0, not stripped
	
	$strip --strip-unneeded  hello.elf 
	$file hello.elf 
	hello.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=fe1fe362cb4e81ba401e5904b56e981941117fe7, for GNU/Linux 3.2.0, stripped
	
	$strip --strip-all  hello.elf 
	$file hello.elf 
	hello.elf: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=fe1fe362cb4e81ba401e5904b56e981941117fe7, for GNU/Linux 3.2.0, stripped
	$nm hello.elf 
	nm: hello.elf: no symbols

4. efficiency purpose

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值