How to obtain the BFD architecture specification for the current platform?
Asked Answered
H

1

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.

Heaume answered 13/10, 2011 at 17:13 Comment(5)
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.Dobbs
I tried to use 'uname' but it doesn't give me the solution.Heaume
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.Dobbs
It would help if you told us what OS you have. Different systems have different syntax for asking about architecture.Bodega
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.Flycatcher
F
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.

Flycatcher answered 15/10, 2011 at 18:26 Comment(3)
Thanks for this quick BFD target one-liner!Gaberones
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 Redeem
(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,Redeem

© 2022 - 2024 — McMap. All rights reserved.