脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - 减少 golang 二进制文件大小操作

减少 golang 二进制文件大小操作

2021-03-04 00:46疯疯癫癫 Golang

这篇文章主要介绍了减少 golang 二进制文件大小操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

环境:

?
1
2
3
4
5
$ go version
go version go1.11.2 linux/amd64
 
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609

一. Go VS C 二进制

 

hello.go

?
1
2
3
4
5
package main
import "fmt"
func main() {
 fmt.Println("hello world")
}

hello.c

?
1
2
3
4
5
#include <stdio.h>
int main() {
 printf("hello world\n");
 return 0;
}
?
1
2
3
$ go build -o hello hello.go
$ go build -ldflags "-s -w" -o hello2 hello.go
$ gcc hello.c
?
1
2
3
4
$ ls -l
-rwxrwxr-x 1 zengxl zengxl 1902849 11月 27 15:40 hello
-rwxrwxr-x 1 zengxl zengxl 1353824 11月 27 15:43 hello2
-rwxrwxr-x 1 zengxl zengxl 8600 11月 27 15:44 a.out

golang 连接的参数:

?
1
2
3
4
5
$ go tool link -h
 
usage: link [options] main.o
-s disable symbol table  # 去掉符号表
-w disable DWARF generation # 去掉调试信息

ELF

先来看下 C 的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ readelf -h a.out
ELF 头:
 Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
 类别:        ELF64
 数据:        2 补码,小端序 (little endian)
 版本:        1 (current)
 OS/ABI:       UNIX - System V
 ABI 版本:       0
 类型:        EXEC (可执行文件)
 系统架构:       Advanced Micro Devices X86-64
 版本:        0x1
 入口点地址:    0x400430
 程序头起点:   64 (bytes into file)
 Start of section headers:   6616 (bytes into file)
 标志:    0x0
 本头的大小:  64 (字节)
 程序头大小:  56 (字节)
 Number of program headers:   9
 节头大小:   64 (字节)
 节头数量:   31
 字符串表索引节头: 28
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ readelf -d a.out
 
Dynamic section at offset 0xe28 contains 24 entries:
 标记  类型       名称/值
 0x0000000000000001 (NEEDED)    共享库:[libc.so.6]
 0x000000000000000c (INIT)    0x4003c8
 0x000000000000000d (FINI)    0x4005b4
 0x0000000000000019 (INIT_ARRAY)   0x600e10
 0x000000000000001b (INIT_ARRAYSZ)  8 (bytes)
 0x000000000000001a (FINI_ARRAY)   0x600e18
 0x000000000000001c (FINI_ARRAYSZ)  8 (bytes)
 0x000000006ffffef5 (GNU_HASH)   0x400298
 0x0000000000000005 (STRTAB)    0x400318
 0x0000000000000006 (SYMTAB)    0x4002b8
 0x000000000000000a (STRSZ)    61 (bytes)
 0x000000000000000b (SYMENT)    24 (bytes)
 0x0000000000000015 (DEBUG)    0x0
 0x0000000000000003 (PLTGOT)    0x601000
 0x0000000000000002 (PLTRELSZ)   48 (bytes)
 0x0000000000000014 (PLTREL)    RELA
 0x0000000000000017 (JMPREL)    0x400398
 0x0000000000000007 (RELA)    0x400380
 0x0000000000000008 (RELASZ)    24 (bytes)
 0x0000000000000009 (RELAENT)   24 (bytes)
 0x000000006ffffffe (VERNEED)   0x400360
 0x000000006fffffff (VERNEEDNUM)   1
 0x000000006ffffff0 (VERSYM)    0x400356
 0x0000000000000000 (NULL)    0x0

再来看下 go 的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ readelf -h hello
ELF 头:
 Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
 类别:        ELF64
 数据:        2 补码,小端序 (little endian)
 版本:        1 (current)
 OS/ABI:       UNIX - System V
 ABI 版本:       0
 类型:        EXEC (可执行文件)
 系统架构:       Advanced Micro Devices X86-64
 版本:        0x1
 入口点地址:    0x451fa0
 程序头起点:   64 (bytes into file)
 Start of section headers:   456 (bytes into file)
 标志:    0x0
 本头的大小:  64 (字节)
 程序头大小:  56 (字节)
 Number of program headers:   7
 节头大小:   64 (字节)
 节头数量:   13
 字符串表索引节头: 3

$ readelf -d hello

There is no dynamic section in this file.

The linker in the gc toolchain creates statically-linked binaries by default. All Go binaries therefore include the Go runtime, along with the run-time type information necessary to support dynamic type checks, reflection, and even panic-time stack traces.

A simple C “hello, world” program compiled and linked statically using gcc on Linux is around 750 kB, including an implementation of printf. An equivalent Go program using fmt.Printf weighs a couple of megabytes, but that includes more powerful run-time support and type and debugging information.

所以,为什么 go 二进制比 C 大很多就比较明显了。

golang 静态编译,不依赖动态库。

二. 如何减小 go 二进制文件大小

 

2.1. -ldflags

上面已经提到了过了。

$ go build -ldflags "-s -w" xxx.go

2.2. UPX

https://github.com/upx/upx

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Commands:
 -1  compress faster     -9 compress better
 -d  decompress      -l list compressed file
 -t  test compressed file    -V display version number
 -h  give more help     -L display software license
Options:
 -q  be quiet       -v be verbose
 -oFILE write output to 'FILE'
 -f  force compression of suspicious files
 -k  keep backup files
file.. executables to (de)compress
 
Compression tuning options:
 --brute    try all available compression methods & filters [slow]
 --ultra-brute  try even more compression variants [very slow]

$ upx --brute binaryfile

IDA 逆向分析简单看下:

https://www.hex-rays.com/products/ida/support/download.shtml

下面是支持 Go 的 IDA helper

https://github.com/sibears/IDAGolangHelper

原始的 go 二进制文件:

可以看到 go 的一些函数名。

减少 golang 二进制文件大小操作

去掉符号表和调试信息的 go 二进制文件:

已经看不到函数名信息,只有类似 sub_47BF70 这样。

减少 golang 二进制文件大小操作

经过 upx 压缩的 go 二进制文件:

信息已经比较少了,入口点也发生了变化。

减少 golang 二进制文件大小操作

2.3. 压缩结果对比

?
1
2
3
$ go build -o hello hello.go
$ go build -ldflags "-s -w" -o hello-strip hello.go
$ upx --brute hello
?
1
2
3
4
$ ll -h
-rwxr-xr-x 1 aland aland 1.9M Dec 6 13:06 hello
-rwxr-xr-x 1 aland aland 809K Dec 6 13:07 hello-upx
-rwxr-xr-x 1 aland aland 1.3M Dec 6 13:06 hello-strip

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/fengfengdiandia/article/details/84582076

延伸 · 阅读

精彩推荐
  • Golanggo日志系统logrus显示文件和行号的操作

    go日志系统logrus显示文件和行号的操作

    这篇文章主要介绍了go日志系统logrus显示文件和行号的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    SmallQinYan12302021-02-02
  • Golanggolang 通过ssh代理连接mysql的操作

    golang 通过ssh代理连接mysql的操作

    这篇文章主要介绍了golang 通过ssh代理连接mysql的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    a165861639710342021-03-08
  • Golanggolang的httpserver优雅重启方法详解

    golang的httpserver优雅重启方法详解

    这篇文章主要给大家介绍了关于golang的httpserver优雅重启的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    helight2992020-05-14
  • GolangGolang中Bit数组的实现方式

    Golang中Bit数组的实现方式

    这篇文章主要介绍了Golang中Bit数组的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    天易独尊11682021-06-09
  • Golanggolang如何使用struct的tag属性的详细介绍

    golang如何使用struct的tag属性的详细介绍

    这篇文章主要介绍了golang如何使用struct的tag属性的详细介绍,从例子说起,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看...

    Go语言中文网11352020-05-21
  • Golanggo语言制作端口扫描器

    go语言制作端口扫描器

    本文给大家分享的是使用go语言编写的TCP端口扫描器,可以选择IP范围,扫描的端口,以及多线程,有需要的小伙伴可以参考下。 ...

    脚本之家3642020-04-25
  • GolangGolang通脉之数据类型详情

    Golang通脉之数据类型详情

    这篇文章主要介绍了Golang通脉之数据类型,在编程语言中标识符就是定义的具有某种意义的词,比如变量名、常量名、函数名等等,Go语言中标识符允许由...

    4272021-11-24
  • Golanggolang json.Marshal 特殊html字符被转义的解决方法

    golang json.Marshal 特殊html字符被转义的解决方法

    今天小编就为大家分享一篇golang json.Marshal 特殊html字符被转义的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    李浩的life12792020-05-27