Q: How to create an object file with a big binary char[] array on Linux? I need tool which is equivalent to:
$ echo 'char bar[] = {65, 66, 67};' >foo.c
$ gcc -c foo.c

I have a multi-megabyte binary file to be put to the bar array, and I need it without creating an .c file: I'd like the .o file be created directly from the binary file. Another option can be creating .s or .S files, but I'd like to avoid that as well. Is there a tool in binutils etc. which can do the job?
An update: gcc segfaults for a 9 MB binary file. as works, but it's slow and the the temporary .s file is too large.

A: You can use objcopy --add-section to create a section with contents found in a file.  I think you'll need to use a linker script to add a symbol pointing at the start of the new section.

A: For a long time the easiest solution was creating an .s or an .S file.
binutils doesn't contain anything useful.
There is a 2-step trick which is fast and uses little memory:
Step 1. Create a and compile a .c file which contains the symbol of the right size, but it has a short signature instead of the real, long data. It should look like:
const char hi[1234567] = "SIGNATURE";
const char *hi_end = hi + sizeof(hi) / sizeof(char);

Step 2. Find the bytes SIGNATURE in the .o file generated by the compiler, and replace it (and the following '\0's) with the data from the real binary data file.
The Perl script cobjgen automates both steps. See this blog post for a more detailed analysis and usage instructions.
