As described in this answer, GNU objcopy can be used to create object files from arbitrary file content. This method can be used to embed resources into programs. However, the symbol name is generated by input file name.
For example, if you run:
objcopy --input binary --output elf64-x86-64 --binary-architecture i386:x86-64 myfile.txt myfile.o
The generated object file would contain _binary_myfile_txt_start, _binary_myfile_txt_end and _binary_myfile_txt_size, which simply replaces non-alphanumeric characters with _.
However, when I process files foo.bar and foo_bar, or even multiple foo.bar on different directories, the symbols would be same. So is there a way to manually specify the symbol in output object file to avoid conflict?
I find that I can run
objcopyto rename the symbol via--redefine-symoption. The only caveat is I have to runobjcopytwice for each input file, once for creating the object file and once for rename symbols.The
--redefine-symoption can be specified multiple times, so the rename only needs to runobjcopyonce for all threexxx_startx_endandxxx_sizesymbols. The input file and output file toobjcopycan be same, so you don't need to decide where to place another temp file.