How to control the symbol names when embedding data in an executable in Linux?

久未见 提交于 2020-01-03 18:34:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!