问题
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.
Is there a way to provide a name for the symbol explicitly, for ex. Say symbol name for this data should be MyData ?
Thanks
回答1:
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.
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
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"
And specify -Ilongpath/to/my/binary/
as GCC option.
来源:https://stackoverflow.com/questions/19169039/how-to-control-the-symbol-names-when-embedding-data-in-an-executable-in-linux