创建仅包含const数据的linux二进制文件(可执行文件)

问题描述 投票:-1回答:1

在使用任何计算机脚本/语言的Linux上,有没有一种方法可以创建/生成二进制文件(可执行文件),它仅具有const数据而没有任何函数/ main()?例如,我们具有以下结构(C格式),二进制文件仅包含以下const数据:

const struct {
    uint32_t mag;
    uint16_t ver;
    uint16_t par_no;
    uint32_t level;
} foo = {1, 2, 3, 0};
linux scripting binary executable language
1个回答
0
投票

是,很容易:

$ cat foo.c
#include <stdint.h>
const struct {
    uint32_t mag;
    uint16_t ver;
    uint16_t par_no;
    uint32_t level;
} foo = {1, 2, 3, 0};

$ gcc foo.c -c -o foo.o

$ ld foo.o -o foo
ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000

(警告不是致命的。

生成的可执行文件具有12个字节的.rodata段,没有.text段。

© www.soinside.com 2019 - 2024. All rights reserved.