【发布时间】:2016-02-14 06:33:18
【问题描述】:
我正在尝试将 C 程序的大小降低到 main.c 看起来像:
#include<unistd.h>
#include<sys/syscall.h>
void _start() {
const char msg [] = "Hello World!";
syscall(SYS_write, 0, msg, sizeof(msg)-1);
syscall(SYS_exit, 0);
}
我正在编译它
gcc -nostdlib -s -O3 -o main main.c /usr/lib/path/to/libc.a
然后我strip它。但是如果我在剥离它之前对其进行了 objdump,我会看到
main:文件格式elf64-x86-64
SYMBOL TABLE:
0000000000400158 l d .note.gnu.build-id 0000000000000000 .note.gnu.build-id
0000000000400180 l d .text 0000000000000000 .text
0000000000400214 l d .eh_frame_hdr 0000000000000000 .eh_frame_hdr
0000000000400238 l d .eh_frame 0000000000000000 .eh_frame
0000000000601000 l d .tbss 0000000000000000 .tbss
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000000 l df *ABS* 0000000000000000 main.c
0000000000000000 l df *ABS* 0000000000000000
00000000004001d0 g F .text 0000000000000026 syscall
0000000000000000 g .tbss 0000000000000004 errno
0000000000400203 g .text 0000000000000000 __syscall_error_1
0000000000400180 g F .text 0000000000000048 _start
0000000000000000 g .tbss 0000000000000004 __libc_errno
0000000000400200 g F .text 0000000000000013 __syscall_error
0000000000601000 g .eh_frame 0000000000000000 __bss_start
0000000000601000 g .eh_frame 0000000000000000 _edata
0000000000000000 *UND* 0000000000000000 _GLOBAL_OFFSET_TABLE_
0000000000601000 g .eh_frame 0000000000000000 _end
似乎我可以删除一些东西来手动减小可执行文件的大小? 注意:我知道这不是我实际上会做的事情,但我只是想删除任何现有的样板。
我会从可执行文件main 中删除什么来减小它的大小?我该怎么做?
【问题讨论】:
-
我不明白:你是想减小可执行代码的大小还是可执行文件的大小? $ ls -l 测试或 $ 大小测试;检查一个。
-
我猜,你对
-Os不满意…… -
然后有编译成32位代码的选项。
-
@OldestSoftwareGuy 我正在尝试尽可能减小可执行文件的整体大小。我知道实际编辑和取出可执行文件的各个部分并不好,但我想知道无论我如何从命令行中做到这一点
-
你可以通过
-fno-asynchronous-unwind-tables -Qn获得几个字节
标签: linux size executable elf