> For the complete documentation index, see [llms.txt](https://blog.owent.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.owent.net/2018/1805.md).

# 可执行文件压缩

## 前言

最近看[Rust](https://rust-lang.org/)相关东西的时候看到一篇关于压缩可执行文件的文章。压缩可执行文件对嵌入式开发特别有用，但是延伸一下用来减少我们游戏行业里预编译的工具二进制包大小和Android/iOS的库也是蛮有用的。

原文见这里： <https://jamesmunns.com/blog/tinyrocket/>

## 基本流程

1. Release编译，移除调试符号文件，开启最小化size优化(-Oz)
2. 使用LLVM的全量LTO
3. 使用xargo重新编译标准库(std)和核心库(core)（这个C/C++不容易模仿，而且编译选项十分难搞）
4. 移除[jemalloc](https://github.com/jemalloc/jemalloc)（服务器程序还是留着比较好，内置的malloc实现一般碎片比较厉害。虽然C/C++默认也不是[jemalloc](https://github.com/jemalloc/jemalloc)，很多项目为了新能还是会用它）
5. 移除panic的详情信息（这个仅适用于[Rust](https://rust-lang.org/)）
6. strip（由GNU的[binutils](https://www.gnu.org/software/binutils/)提供），参考命令: `strip [二进制]`
7. [UPX](https://upx.github.io/)进一步压缩加壳

## 尝试改造优化

然后尝试使用上面的流程改造我们的 **gmtools-cli** 。原先我是直接开LTO+Release编译的，编出的文件大小为4.4MB（4520728字节）。

1. Release编译只要构建命令用 `cargo build --release` 就可以了，开size优化需要加个配置选项

   \`\`\`

   \[profile.release]

   opt-level = "z"

\[profile.dev] opt-level = "z"

```
2. LTO加几个配置选项就行了
```

\[profile.release] lto = true codegen-units = 1 incremental = false

\[profile.dev] lto = true codegen-units = 1 incremental = false

````
3. 重新编译标准库(std)和核心库(core)比较麻烦，而且我本地的环境报```could not find native static library `c`, perhaps an -L flag is missing?```。原文里自己编译这两个库反而体积变大了，我就先忽略这个了
4. 这个要改源码和配置文件
首先是 **Cargo.toml** 里要增加:
````

\[features] system-alloc = \[]

````
然后代码增加:
```rust
#![feature(global_allocator)]
#![feature(allocator_api)]
// When the `system-alloc` feature is used, use the System Allocator
#[cfg(feature = "system-alloc")]
mod allocator {
    use std::heap::System;

    #[global_allocator]
    pub static mut THE_ALLOC: System = System;
}

// When the `system-alloc` feature is not used, do nothing,
// retaining the default functionality (using jemalloc)
#[cfg(not(feature = "system-alloc"))]
mod allocator {
    #[allow(dead_code)]
    pub static THE_ALLOC: () = ();
}

#[allow(unused_imports)]
use allocator::THE_ALLOC;
````

最后构建命令加 `--features system-alloc`

1. 这个就是移除调试信息，把 `[profile.release]` 的配置 panic = "abort" 就可以了

   \`\`\`

   \[profile.release]

   panic = "abort"

\[profile.dev] panic = "abort"

````
6. 直接执行 ```strip 二进制``` 即可
7. 参考命令 ```upx --ultra-brute 二进制
````

最后执行完，成果很惊人。压缩完后的大小是274K(280264字节)。

来个更直观的对比。

| 对比项         | 压缩前                                                         | 压缩后                                                                |   |
| ----------- | ----------------------------------------------------------- | ------------------------------------------------------------------ | - |
| 编译选项        | release,opt-level=3,lto=true,codegen-units=3,panic="unwind" | release,opt-level="z",lto=true,codegen-units=1,panic="abort",strip |   |
| 原始编译结果      | 4.4MB（4520728字节）                                            | 2.1MB(2187784字节) -- 减少51.6%                                        |   |
| 仅执行strip    | 4.4MB（4520728字节）                                            | 844K(863312字节) -- 减少80.9%                                          |   |
| 执行strip和upx | 4.4MB（4520728字节）                                            | 274K(280264字节) -- 减少93.8%                                          |   |

## 其他C/C++的压缩

其实上面效果最大的是Release编译移除调试符号、strip和upx，这三项都可以直接用再C/C++项目里的。唯一不同的就是可以编译的时候保留调试符号，然后用 `objcopy` 来代替 `strip` 把调试符号导出来并且移除了。

## 关于UPX和WSL和Android

[UPX](https://upx.github.io/)的原理是压缩代码，然后加入一些初始化函数再运行时解压，以前被一些病毒拿来做加壳处理，所以可能有些杀毒软件会报。其实不用[UPX](https://upx.github.io/)只[strip](https://www.gnu.org/software/binutils/)也有不错的压缩率了。

在WSL环境下，现在的版本不支持[UPX](https://upx.github.io/)压缩后的可执行程序，会报 `exec format error` ，但是马上要发布的春季更新后就支持了。 这里有个Issue说这个问题的 <https://github.com/Microsoft/WSL/issues/330> 。

Android下用[UPX](https://upx.github.io/)看到说需要几个小patch（我没试，这里只是记录一下）:

* [UPX](https://upx.github.io/)需要二进制文件大于40K，如果不够大可以加个全局变量搞大这个.so。
* 在native代码中需要声明 `extern "C" {void _init(void){}}` 函数，用于在编译时生成 **\_init** 段。（[UPX](https://upx.github.io/)要求二进制文件必须存在init段，但是android的.so可能没有）
  * 或者也可以自定义初始化代码， `extern "C" {void my_init(void){}}` ，然后编译时在 `Android.mk` 里加入 `LOCAL_LDFLAGS += -Wl,-init=my_init` 。来指定自己的初始化加载函数

我的博客即将搬运同步至腾讯云+社区，邀请大家一同入驻：<https://cloud.tencent.com/developer/support-plan?invite\\_code=3n1gmsmrgq2ok>
