embedding binary objects in c

栏目: IT技术 · 发布时间: 4年前

内容简介:You have a blob of some data which you would like to embed into your C program. Perhaps a splash screen, or a special font, firmware for your scsi card, or whatever. The usual approach which I think most people are familiar with is to run something likeBut

You have a blob of some data which you would like to embed into your C program. Perhaps a splash screen, or a special font, firmware for your scsi card, or whatever. The usual approach which I think most people are familiar with is to run something like xxd -i to generate a source file with a large array of hex constants. Or write your own little script for that purpose.

But what if we could just link binaries into our program directly? We can. Mostly. I had no idea this was possible until after reading this list posting which explains the technique. It requires an intermediate object file, but it’s much faster to generate and compile then a textual equivalent file.

I thought this was pretty cool and needed to try it out. I’m going to write a little quine like program that prints its own source.

quine.c:

#include <stdio.h>

int
main(int argc, char **argv)
{
        extern const char _binary_quine_c_start, _binary_quine_c_end;
        const char *start = &_binary_quine_c_start;
        const char *end = &_binary_quine_c_end;

        fwrite(start, end - start, 1, stdout);
}

Now we need to prepare the object. This is where I ran into just a touch of trouble. Over here, ld is ld.lld , the llvm linker and it produces an error message at first.

> ld -r -b binary quine.c -o myself.o
ld: error: target emulation unknown: -m or at least one .o file required

The ld man page is most unhelpful, telling me -m can set an emulation, but that is the only occurrence of the word emulation in the entire page and there is nothing more to be learned. Running ld.bfd just works. However, digging into the source at llvm/tools/lld/ELF/Driver.cpp one can find a list of supported emulations, thankfully not too far from the top of the file.

And now we’re on our way.

> ld -r -b binary quine.c -o myself.o -m elf_amd64
> cc -c quine.c
> cc quine.o myself.o

Running the result and we get the expected output.

> ./a.out                                                                             
#include <stdio.h>

int
main(int argc, char **argv)
{
        extern const char _binary_quine_c_start, _binary_quine_c_end;
        const char *start = &_binary_quine_c_start;
        const char *end = &_binary_quine_c_end;

        fwrite(start, end - start, 1, stdout);
}

Tada.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

产品经理手册(原书第4版)(白金版)

产品经理手册(原书第4版)(白金版)

[美] 琳达·哥乔斯(Linda Gorchels) / 祝亚雄、冯华丽、金骆彬 / 机械工业出版社 / 2017-8 / 65.00

产品经理的职责起点是新产品开发,贯穿产品生命周期的全过程。本书按上下游产品管理进行组织。 在上游的新产品开发流程中,作者阐述了如何从市场、产品、行业、公司的角度规划企划方案,并获得老板、销售部、运营部的资源支持,推进新产品的项目流程,实现所有目标,制定和实施新产品发布。 下游产品的管理核心在于生命周期的管理,营销更是生命周期管理的重中之重。产品经理如何让产品满足客户需求,让客户获得对产......一起来看看 《产品经理手册(原书第4版)(白金版)》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具