3

I've embedded a text file in a C program using the following method: http://www.linuxjournal.com/content/embedding-file-executable-aka-hello-world-version-5967

a.out:prog.c file.text
    objcopy --input binary --output elf64-x86-64 --binary-architecture i386 file.text file.o
    gcc prog.c file.o

objcopy requires to specify the target with the "--output" option.

How can I set "--output" in Makefile so objcopy will use the user's architecture ?

Thanks.

thiton
  • 35,651
  • 4
  • 70
  • 100
Pierre
  • 34,472
  • 31
  • 113
  • 192
  • The question is, of course equivalent to asking *"How do I learn the correct architecture specification from the command line?"*, but the answer is not clear to me. – dmckee --- ex-moderator kitten Oct 13 '11 at 17:26
  • I tried to use 'uname' but it doesn't give me the solution. – Pierre Oct 13 '11 at 18:32
  • Yeah. I tried that too. And I looked at `file` and `ar` and `nm`. The only thought I had was a custom `magic` file for `file`, but I don't know enough to begin assembling such a thing. – dmckee --- ex-moderator kitten Oct 13 '11 at 19:01
  • It would help if you told us what OS you have. Different systems have different syntax for asking about architecture. – Beta Oct 14 '11 at 12:47
  • Since you speak of a "user" here: Do ***please*** not introduce this objcopy hack into production or release it into the wild. It is a beautiful hack, but just meant as a hack: Showing what technology can do. In *any* practical use, just load the file at runtime or use simple sed/awk/perl/whatever magic to convert it into a C file. – thiton Oct 15 '11 at 18:59

1 Answers1

3

Firstly: You are not trying to emulate the -b capability of the GCC ld, are you? In more verbose terms: The GCC ld can actually load a number of binary formats, see the documentation. If that's what you want to achieve, something like:

 gcc prog.c -Wl,-b -Wl,binary file.o

might save you the whole objcopy call.

While I'm not able to find documentation on the issue, the output of objdump -i seems to be sorted by preference, so

 `objdump -i | head -n 2 | tail -n 1`

should expand to the usual target architecture. Stating again: I have no documentation on this behaviour, so better don't rely blindly on it.

thiton
  • 35,651
  • 4
  • 70
  • 100
  • Thanks for this quick BFD target one-liner! – Dmitry Mikushin Aug 04 '19 at 23:39
  • When I try this in a test program, `gcc` moans at me ```$ gcc use-binblob.c -Wl,-b -Wl,binary binblob -lc /usr/bin/ld: .../libc.so:(.data+0x0): multiple definition of _binary__lib_x86_64_linux_gnu_libc_so_start; .../libc.so:(.data+0x0): first defined here ... /usr/bin/ld: .../Scrt1.o: in function _start: (.text+0x16): undefined reference to __libc_csu_fini ... /usr/bin/ld: use-binblob.c:(.text+0x6f): undefined reference to putchar /usr/bin/ld: a.out: hidden symbol __TMC_END__ isn't defined /usr/bin/ld: final link failed: bad value collect2: error: ld returned 1 exit status ``` – FrankH. Jul 15 '21 at 07:31
  • (various errors related to being unable to link in libc; it seems that the `-Wl,b` "transfers" to the entire output object. I suspect this can be solved with linker mapfiles (to make sure it only is applied to the binary blob file and nothing else); I haven't figured out how yet though, – FrankH. Jul 15 '21 at 07:37