在Linux上可执行文件中嵌入数据时的符号名称

时间:2023-02-12 04:54:39

I need to embed some data into an executable or SO file on Linux. I've found I can do it with ls option --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.

我需要将一些数据嵌入到Linux上的可执行文件中。我发现我可以使用ls选项——格式二进制文件,但是,我看到的所有示例都假设数据文件在当前目录中。如果不是,那么结果的符号名称就会变得复杂,因为它试图包含文件的完整路径。

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

有没有一种方法可以显式地为符号提供一个名称,例如,说这个数据的符号名称应该是MyData ?

Thanks

谢谢

1 个解决方案

#1


2  

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.

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

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

但是我们再来讨论自定义符号名。您可以使用小的内联汇编程序魔术(incbin指令)来实现它。准备汇编文件:

    .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

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

并与您的C代码链接,安全使用:

extern char MyData[];
extern unsigned MyData_size;

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

另外(如上面列出的链接器方法)您可以使用简单的形式:

    .incbin "MyData.bin"

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

并指定-Ilongpath/to/my/binary/ as GCC选项。

#1


2  

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.

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

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

但是我们再来讨论自定义符号名。您可以使用小的内联汇编程序魔术(incbin指令)来实现它。准备汇编文件:

    .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

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

并与您的C代码链接,安全使用:

extern char MyData[];
extern unsigned MyData_size;

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

另外(如上面列出的链接器方法)您可以使用简单的形式:

    .incbin "MyData.bin"

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

并指定-Ilongpath/to/my/binary/ as GCC选项。