How to specify symbol name when creating objects from raw binary files using objcopy?

66 views Asked by At

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?

2

There are 2 answers

0
jiandingzhe On BEST ANSWER

I find that I can run objcopy to rename the symbol via --redefine-sym option. The only caveat is I have to run objcopy twice for each input file, once for creating the object file and once for rename symbols.

The --redefine-sym option can be specified multiple times, so the rename only needs to run objcopy once for all three xxx_start x_end and xxx_size symbols. The input file and output file to objcopy can be same, so you don't need to decide where to place another temp file.

0
Employed Russian On

However, when I process files foo.bar and foo_bar, or even multiple foo.bar on different directories,

It seems exceedingly unlikely that you will want to embed both foo.bar and foo_bar from a single directory. If you do, you can always make a copy (or a symlink) under a different name, and embed that.

The multiple directories also doesn't appear to be a real issue: if you use directory path, the name ends up as _binary_relative_dir_foo_bar_start or _binary__absolute_dir_foo_bar_start.

So is there a way to manually specify the symbol in output object file to avoid conflict?

No, the name is hard-coded in binutils/bfd/binary.c here.