Bug 24968 - make objcopy use output ELF arch if -B not given
Summary: make objcopy use output ELF arch if -B not given
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.33
: P2 normal
Target Milestone: 2.34
Assignee: Alan Modra
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-09-06 04:57 UTC by Fangrui Song
Modified: 2019-11-19 09:23 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed: 2019-11-19 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2019-09-06 04:57:40 UTC
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;
Comment 1 Sourceware Commits 2019-11-19 08:58:33 UTC
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.
Comment 2 Alan Modra 2019-11-19 09:23:32 UTC
Done