打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux

You can create an object file with informative fields like a version number and link that file such that they are included in the resulting ELF binary.

Ident

For example, as part of you build process, you can generate - say - info.c that contains one or more #ident directives:

#ident "Build: 1.2.3 (Halloween)"#ident "Environment: example.org"

Compile it:

$ gcc -c info.c

Check if the information is included:

$ readelf -p .comment info.oString dump of section '.comment':  [     1]  Build: 1.2.3 (Halloween)  [    1a]  Environment: example.org  [    33]  GCC: (GNU) 7.2.1 20170915 (Red Hat 7.2.1-2)

Alternatively, you can use objdump -s --section .comment info.o. Note that GCC also writes its own comment, by default.

Check the information after linking an ELF executable:

$ gcc -o main main.o info.o$ readelf -p .comment main String dump of section '.comment':  [     0]  GCC: (GNU) 7.2.1 20170915 (Red Hat 7.2.1-2)  [    2c]  Build: 1.2.3 (Halloween)  [    45]  Environment: example.org

Comment Section

Using #ident in a C translation unit is basically equivalent to creating a .comment section in an assembler file. Example:

$ cat info.s.section .comment.string "Build: 1.2.3 (Halloween)".string "Environment: example.org"$ gcc -c info.sString dump of section '.comment':  [     0]  Build: 1.2.3 (Halloween)  [    19]  Environment: example.org

Using an uncommon section name works, as well (e.g. .section .blahblah). But .comment is used and understood by other tools. GNU as also understands the .ident directive, and this is what GCC translates #ident to.

With Symbols

For data that you also want to access from the ELF executable itself you need to create symbols.

Objcopy

Say you want to include some magic bytes stored in a data file:

$ cat magic.bin 2342

Convert into a object file with GNU objcopy:

$ objcopy -I binary -O elf64-x86-64 -B i386     --rename-section .data=.rodata,alloc,load,readonly,data,contents     magic.bin magic.o

Check for the symbols:

$ nm  magic.o  0000000000000005 R _binary_magic_bin_end0000000000000005 A _binary_magic_bin_size0000000000000000 R _binary_magic_bin_start

Example usage:

#include <stdio.h>#include <string.h>#include <inttypes.h>extern const char _binary_magic_bin_start[];extern const char _binary_magic_bin_end[];extern const unsigned char _binary_magic_bin_size;static const size_t magic_bin_size = (uintptr_t) &_binary_magic_bin_size;int main(){  char s[23];  memcpy(s, _binary_magic_bin_start,      _binary_magic_bin_end - _binary_magic_bin_start);  s[magic_bin_size] = 0;  puts(s);  return 0;}

Link everything together:

$ gcc -g -o main_magic main_magic.c magic.o

GNU ld

GNU ld is also able to turn data files into object files using an objcopy compatible naming scheme:

$ ld -r -b binary magic.bin -o magic-ld.o

Unlike objcopy, it places the symbols into the .data instead of the .rodata section, though (cf. objdump -h magic.o).

incbin

In case GNU objcopy isn't available, one can use the GNU as .incbin directive to create the object file (assemble with gcc -c incbin.s):

    .section .rodata    .global _binary_magic_bin_start    .type _binary_magic_bin_start, @object_binary_magic_bin_start:    .incbin "magic.bin"    .size _binary_magic_bin_start, . - _binary_magic_bin_start    .global _binary_magic_bin_size    .type _binary_magic_bin_size, @object    .set _binary_magic_bin_size, . - _binary_magic_bin_start    .global _binary_magic_bin_end    .type _binary_magic_bin_end, @object    .set _binary_magic_bin_end, _binary_magic_bin_start + _binary_magic_bin_size    ; an alternate  way to include the size        .global _binary_magic_bin_len    .type _binary_magic_bin_len, @object    .size _binary_magic_bin_len, 8_binary_magic_bin_len:    .quad _binary_magic_bin_size

xxd

A more portable alternative that doesn't require GNU objcopy nor GNU as is to create an intermediate C file and compile and link that. For example with xxd:

$ xxd -i magic.bin | sed 's/\(unsigned\)/const \1/' > magic.c$ gcc -c magic.c$ nm magic.o0000000000000000 R magic_bin0000000000000008 R magic_bin_len$ cat magic.cconst unsigned char magic_bin[] = {  0x32, 0x33, 0x34, 0x32, 0x0a};const unsigned int magic_bin_len = 5;
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux下如何反汇编arm raw binary文件
S-Records介绍+objcopy命令
u-boot2012.04.01编译过程分析
GBA开发入门:做一个名叫Hello World的游戏
Apache ActiveMQ ?
编写简单的Makefile文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服