在Linux的可执行文件中嵌入数据时,如何控制符号名称? [英] How to control the symbol names when embedding data in an executable in Linux?

查看:89
本文介绍了在Linux的可执行文件中嵌入数据时,如何控制符号名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一些数据嵌入Linux上的可执行文件或SO文件中.我发现可以使用ld --format binary进行操作,但是,我看到的所有示例都假定数据文件位于当前目录中.如果不是,那么生成的符号名称将变得很复杂,因为它试图包含文件的完整路径.

I need to embed some data into an executable or SO file on Linux. I've found I can do it with ld --format binary, however, all examples I've seen assume the data file is in the current directory. If it is not, then the resulting symbol name gets complicated, as it tries to include the full path to the file.

是否有一种方法可以为ex明确提供符号名称.说这个数据的符号名称应该是MyData吗?

Is there a way to provide a name for the symbol explicitly, for ex. Say symbol name for this data should be MyData ?

谢谢

推荐答案

您绝对不能在--format=binary方法中指定链接器生成的二进制符号名称.但是,使用-L选项,您可以指定二进制文件的路径,链接器将在任何路径中看到它,而无需在文件名中指定路径,而使符号名简短而美观.

You definitely can not specify linker-generated binary symbol name in --format=binary approach. But with -L option you may specify path to binary and linker will see it in any path without specifying path in filename, leaving symbol name short and pretty.

但是让我们更多地讨论自定义符号名称.您可以用很少的内联汇编程序做到这一点( incbin指令).准备汇编文件,例如:

But lets talk about custom symbol names more. You can do it with little inline assembler magic (incbin directive). Prepare assembler file like:

    .section .rodata
    .global MyData
    .type   MyData, @object
    .align  4
MyData:
    .incbin "longpath/to/my/binary/MyData.bin"
    .global MyData_size
    .type   MyData_size, @object
    .align  4
MyData_size:
    .int    MyData_size - MyData

并安全地使用以下代码将其与您的C代码链接在一起:

And link it together with you C code, safely using:

extern char MyData[];
extern unsigned MyData_size;

(与上面列出的链接器方法一样),您可以使用简单的形式:

Also (as with linker approach, listed above) you may use simple form:

    .incbin "MyData.bin"

并指定-Ilongpath/to/my/binary/作为GCC选项.

And specify -Ilongpath/to/my/binary/ as GCC option.

这篇关于在Linux的可执行文件中嵌入数据时,如何控制符号名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆