编辑ELF可执行文件以减小其大小 [英] Editing an ELF executable to reduce it's size

查看:0
本文介绍了编辑ELF可执行文件以减小其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个C程序缩小到1KB大小。我快完成了,但是我被困在编辑我的ELF可执行文件上了。我的程序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它。但如果我在剥离它之前对其进行了对象转储,我会看到

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中删除什么来减小其大小?我该怎么做呢?

附注:我已经阅读了thisthis文章。不需要将它们联系起来。我特意选择留在C

推荐答案

简单的东西

您可以使用:

删除相当多的无用部分
  • -fno-asynchronous-unwind-tables -Qn
  • 使用自定义链接器脚本-rlinker_script

我得到了一个992字节的工作二进制文件(在条带之后)。

链接器脚本

我们来看看(剥离前)部分:

[Nr] Name              Type             Address           Offset
     Size              EntSize          Flags  Link  Info  Align
[ 0]                   NULL             0000000000000000  00000000
     0000000000000000  0000000000000000           0     0     0
[ 1] .note.gnu.build-i NOTE             0000000000400120  00000120
     0000000000000024  0000000000000000   A       0     0     4
[ 2] .text             PROGBITS         0000000000400150  00000150
     0000000000000090  0000000000000000  AX       0     0     16
[ 3] .eh_frame         PROGBITS         00000000004001e0  000001e0
     0000000000000048  0000000000000000   A       0     0     8
[ 4] .tbss             NOBITS           0000000000601000  00000228
     0000000000000004  0000000000000000 WAT       0     0     4
[ 5] .shstrtab         STRTAB           0000000000000000  000003e7
     0000000000000044  0000000000000000           0     0     1
[ 6] .symtab           SYMTAB           0000000000000000  00000228
     0000000000000168  0000000000000018           7     6     8
[ 7] .strtab           STRTAB           0000000000000000  00000390
     0000000000000057  0000000000000000

程序头5中的所有内容都被剥离了,但我们给出了两个相对无用的部分,它们没有被剥离:.note.gnu.build-id.eh_frame。已在编译器中禁用.eh_frame,但某些.eh_frame是由静态libc提供的。

我们可以使用自定义链接器脚本(gcc -T linker_script)完全删除.eh_frame.note.gnu.build-id节。

首先,我们获取默认链接器脚本:

gcc test.c -Wl,--verbose

我们删除这些行:

.eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) }
.eh_frame       : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) }
.note.gnu.build-id : { *(.note.gnu.build-id) }

并修改此行:

/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*)  *(.note.gnu.build-id) *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) *(.eh_frame) *(.eh_frame.*)  }

我使用它可以获得664个字节。

其他选项

缩小尺寸的其他解决方案:

  • 优化大小(-Os);

  • 32位编译(-m32)。

所有这些操作之后,我得到了一个760字节的二进制文件,没有自定义链接器脚本,488个字节的修改后的链接器脚本。

清除errno

可以删除的"无用"内容(如errno处理和TLS)非常少。

[Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
[ 0]                   NULL            00000000 000000 000000 00      0   0  0
[ 1] .text             PROGBITS        080480a0 0000a0 00008e 00  AX  0   0 16
[ 2] .tbss             NOBITS          08049130 000130 000004 00 WAT  0   0  4
[ 3] .shstrtab         STRTAB          00000000 000257 000027 00      0   0  1
[ 4] .symtab           SYMTAB          00000000 000130 0000d0 10      5   4  4
[ 5] .strtab           STRTAB          00000000 000200 000057 00      0   0  1

(所有从第3节开始的内容都被剥离。)

通过编写我们自己的syscall代码,我们可以消除errno处理。我们将删除:

  • 4字节.symtab

  • errno相关说明。

但执行此操作需要使用(内联)程序集。

这篇关于编辑ELF可执行文件以减小其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆