objcopy -I binary -O elf64-x86-64 a.txt b # e_machine is EM_NONE objcopy -I binary -B i386:x86-64 -O elf64-x86-64 a.txt c # e_machine is EM_X86_64 cmp -l b c # one byte difference: 19 0 76 objcopy -I ihex -O elf64-x86-64 a.hex b.o # e_machine is EM_NONE objcopy -I ihex -B i386:x86-64 -O elf64-x86-64 a.hex b.o # e_machine is EM_X86_64 cmp -l b c # one byte difference: 19 0 76 aarch64/binutils/objcopy -I binary -O elf64-littleaarch64 a.txt b; file b a.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not stripped This means when -I binary is used, both -O and -B have to be specified. The e_machine information "i386:x86-64" can be inferred from elf64-x86-64. So is it possible to ignore -B and let `objcopy -I binary -O elf*` create an object file with e_machine set? This will not break existing use cases. There is a precedent that ignores -B, elftoolchain elfcopy case 'B': /* ignored */ break; ... case 'I': case 's': set_input_target(ecp, optarg); break; ... case 'O': set_output_target(ecp, optarg); break;
The master branch has been updated by Alan Modra <amodra@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6765ee1825d92d25d9b3f6260de3f8a2a27e4998 commit 6765ee1825d92d25d9b3f6260de3f8a2a27e4998 Author: Alan Modra <amodra@gmail.com> Date: Tue Nov 19 19:15:20 2019 +1030 PR24968, make objcopy use output ELF arch if -B not given This should make objcopy -B redundant for the common case of producing ELF output where the -O target defaults to the desired arch:mach. PR 24968 * objcopy.c (copy_object): For ELF output and non-ELF input without arch, take arch from output file if not given by -B. Don't bfd_get_arch_info when we already have iarch.
Done