]> granicus.if.org Git - llvm/commitdiff
[llvm-objcopy] Default --output-target to --input-target when unspecified
authorFangrui Song <maskray@google.com>
Sat, 14 Sep 2019 01:36:16 +0000 (01:36 +0000)
committerFangrui Song <maskray@google.com>
Sat, 14 Sep 2019 01:36:16 +0000 (01:36 +0000)
Fixes PR42171.

In GNU objcopy, if -O (--output-target) is not specified, the value is
copied from -I (--input-target).

```
objcopy -I binary -B i386:x86-64 a.txt b       # b is copied from a.txt
llvm-objcopy -I binary -B i386:x86-64 a.txt b  # b is an x86-64 object file
```

This patch changes our behavior to match GNU. With this change, we can
delete code related to -B handling (D67215).

Reviewed By: jakehehrlich

Differential Revision: https://reviews.llvm.org/D67144

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@371913 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-objcopy/ELF/binary-input-and-output.test
test/tools/llvm-objcopy/ELF/binary-input-error.test
test/tools/llvm-objcopy/ELF/binary-input.test
test/tools/llvm-objcopy/ELF/binary-output-target.test [moved from test/tools/llvm-objcopy/ELF/binary-input-arch.test with 72% similarity]
test/tools/llvm-objcopy/ELF/new-symbol-visibility.test
tools/llvm-objcopy/CopyConfig.cpp

index 0fc2a4bc3c17c6db63e017e900f12b4bc9cc295e..68331f23986b39cbb5b625f54637c4484a388c09 100644 (file)
@@ -3,13 +3,21 @@
 # Preserve input to verify it is not modified
 # RUN: cp %t.txt %t-copy.txt
 
-# -I binary -O binary preserves payload through in-memory representation
-# RUN: llvm-objcopy -I binary -B i386:x86-64 -O binary %t.txt %t.2.txt
+## -I binary -O binary preserves payload through in-memory representation.
+# RUN: llvm-objcopy -I binary -O binary %t.txt %t.2.txt
 # RUN: cmp %t.txt %t.2.txt
 # RUN: cmp %t.txt %t-copy.txt
 
-# -I binary -O binary preserves payload through an intermediate object file
-# RUN: llvm-objcopy -I binary -B i386:x86-64 %t.txt %t.o
-# RUN: llvm-objcopy -O binary %t.o %t.3.txt
+## If -O is not specified, it defaults to -I, i.e. "binary".
+# RUN: llvm-objcopy -I binary %t.txt %t.3.txt
 # RUN: cmp %t.txt %t.3.txt
-# RUN: cmp %t.txt %t-copy.txt
+
+## -I binary -O elf* creates an ELF. -O binary transforms it back to the
+## original file.
+# RUN: llvm-objcopy -I binary -O elf64-x86-64 %t.txt %t.o
+# RUN: llvm-objcopy -O binary %t.o %t.4.txt
+# RUN: cmp %t.txt %t.4.txt
+
+## -B is ignored.
+# RUN: llvm-objcopy -I binary -B i386:x86-64 -O binary %t.txt %t.5.txt
+# RUN: cmp %t.txt %t.5.txt
index 08cc22c750e9e627e5617a5897c058bc428990c0..d0dd50a4194080bdcd97c7c6fba4a965d12948fc 100644 (file)
@@ -1,10 +1,6 @@
 # RUN: echo abcd > %t.txt
 
-# RUN: not llvm-objcopy -I binary %t.txt %t.o 2>&1 \
-# RUN:   | FileCheck %s --check-prefix=MISSING-BINARY-ARCH
-
 # RUN: not llvm-objcopy -I binary -B xyz %t.txt %t.o 2>&1 \
 # RUN:   | FileCheck %s --check-prefix=BAD-BINARY-ARCH
 
-# MISSING-BINARY-ARCH: specified binary input without specifiying an architecture
 # BAD-BINARY-ARCH: invalid architecture: 'xyz'
index d7a708dcea537d3b49520e05b77d06b98e28716b..8eef7f772f9ceaa7abbde8fc13eada54a8b58727 100644 (file)
@@ -1,7 +1,7 @@
 # RUN: echo -n abcd > %t.x-txt
 # Preserve input to verify it is not modified
 # RUN: cp %t.x-txt %t-copy.txt
-# RUN: llvm-objcopy -I binary -B i386:x86-64 %t.x-txt %t.o
+# RUN: llvm-objcopy -I binary -O elf64-x86-64 %t.x-txt %t.o
 # RUN: llvm-readobj --sections --symbols %t.o | FileCheck %s
 # RUN: cmp %t.x-txt %t-copy.txt
 
similarity index 72%
rename from test/tools/llvm-objcopy/ELF/binary-input-arch.test
rename to test/tools/llvm-objcopy/ELF/binary-output-target.test
index 2b63e48866cf8b043a258c72143de953461cf6f8..1d683cc9ff50258612ed24406d3fe4d5cb23eabc 100644 (file)
@@ -1,38 +1,35 @@
 # RUN: echo abcd > %t.txt
 
-# RUN: llvm-objcopy -I binary -aarch64 %t.txt %t.aarch64.o
+# RUN: llvm-objcopy -I binary -O elf64-littleaarch64 %t.txt %t.aarch64.o
 # RUN: llvm-readobj --file-headers %t.aarch64.o | FileCheck %s --check-prefixes=CHECK,LE,AARCH64,64
 
-# RUN: llvm-objcopy -I binary -arm %t.txt %t.arm.o
+# RUN: llvm-objcopy -I binary -O elf32-littlearm %t.txt %t.arm.o
 # RUN: llvm-readobj --file-headers %t.arm.o | FileCheck %s --check-prefixes=CHECK,LE,ARM,32
 
-# RUN: llvm-objcopy -I binary -i386 %t.txt %t.i386.o
+# RUN: llvm-objcopy -I binary -O elf32-i386 %t.txt %t.i386.o
 # RUN: llvm-readobj --file-headers %t.i386.o | FileCheck %s --check-prefixes=CHECK,LE,I386,32
 
-# RUN: llvm-objcopy -I binary -B i386:x86-64 %t.txt %t.i386_x86-64.o
-# RUN: llvm-readobj --file-headers %t.i386_x86-64.o | FileCheck %s --check-prefixes=CHECK,LE,X86-64,64
+# RUN: llvm-objcopy -I binary -O elf64-x86-64 %t.txt %t.x86-64.o
+# RUN: llvm-readobj --file-headers %t.x86-64.o | FileCheck %s --check-prefixes=CHECK,LE,X86-64,64
 
-# RUN: llvm-objcopy -I binary -mips %t.txt %t.mips.o
+# RUN: llvm-objcopy -I binary -O elf32-bigmips %t.txt %t.mips.o
 # RUN: llvm-readobj --file-headers %t.mips.o | FileCheck %s --check-prefixes=CHECK,BE,MIPS,32
 
-# RUN: llvm-objcopy -I binary -B powerpc:common64 %t.txt %t.powerpc_common64.o
-# RUN: llvm-readobj --file-headers %t.powerpc_common64.o | FileCheck %s --check-prefixes=CHECK,LE,PPC,64
+# RUN: llvm-objcopy -I binary -O elf64-powerpcle %t.txt %t.ppc64le.o
+# RUN: llvm-readobj --file-headers %t.ppc64le.o | FileCheck %s --check-prefixes=CHECK,LE,PPC64,64
 
-# RUN: llvm-objcopy -I binary -B riscv:rv32 %t.txt %t.rv32.o
+# RUN: llvm-objcopy -I binary -O elf32-littleriscv %t.txt %t.rv32.o
 # RUN: llvm-readobj --file-headers %t.rv32.o | FileCheck %s --check-prefixes=CHECK,LE,RISCV32,32
 
-# RUN: llvm-objcopy -I binary -B riscv:rv64 %t.txt %t.rv64.o
+# RUN: llvm-objcopy -I binary -O elf64-littleriscv %t.txt %t.rv64.o
 # RUN: llvm-readobj --file-headers %t.rv64.o | FileCheck %s --check-prefixes=CHECK,LE,RISCV64,64
 
-# RUN: llvm-objcopy -I binary -sparc %t.txt %t.sparc.o
+# RUN: llvm-objcopy -I binary -O elf32-sparc %t.txt %t.sparc.o
 # RUN: llvm-readobj --file-headers %t.sparc.o | FileCheck %s --check-prefixes=CHECK,BE,SPARC,32
 
-# RUN: llvm-objcopy -I binary -sparcel %t.txt %t.sparcel.o
+# RUN: llvm-objcopy -I binary -O elf32-sparcel %t.txt %t.sparcel.o
 # RUN: llvm-readobj --file-headers %t.sparcel.o | FileCheck %s --check-prefixes=CHECK,LE,SPARCEL,32
 
-# RUN: llvm-objcopy -I binary -B x86-64 %t.txt %t.x86-64.o
-# RUN: llvm-readobj --file-headers %t.x86-64.o | FileCheck %s --check-prefixes=CHECK,LE,X86-64,64
-
 # CHECK: Format:
 # 32-SAME:      ELF32-
 # 64-SAME:      ELF64-
@@ -42,7 +39,8 @@
 # MIPS-SAME:    mips{{$}}
 # RISCV32-SAME: riscv{{$}}
 # RISCV64-SAME: riscv{{$}}
-# PPC-SAME:     ppc64
+# PPC-SAME:     ppc{{$}}
+# PPC64le-SAME: ppc64
 # SPARC-SAME:   sparc
 # SPARCEL-SAME: sparc
 # X86-64-SAME:  x86-64
@@ -50,8 +48,9 @@
 # AARCH64-NEXT: Arch: aarch64
 # ARM-NEXT:     Arch: arm
 # I386-NEXT:    Arch: i386
-# MIPS-NEXT:    Arch: mips{{$$}}
-# PPC-NEXT:     Arch: powerpc64le
+# MIPS-NEXT:    Arch: mips{{$}}
+# PPC-NEXT:     Arch: powerpc{{$}}
+# PPC64-NEXT:   Arch: powerpc64le
 # RISCV32-NEXT: Arch: riscv32
 # RISCV64-NEXT: Arch: riscv64
 # SPARC-NEXT:   Arch: sparc{{$}}
 # CHECK-NEXT:       Unused: (00 00 00 00 00 00 00)
 # CHECK-NEXT:     }
 # CHECK-NEXT:     Type: Relocatable (0x1)
+
+## In GNU objcopy, e_machine is EM_NONE unless -B is specifid.
+## It is not very clear why it behaves this way.
+## We fill in the field regardless of -B.
 # AARCH64-NEXT:   Machine: EM_AARCH64 (0xB7)
 # ARM-NEXT:       Machine: EM_ARM (0x28)
 # I386-NEXT:      Machine: EM_386 (0x3)
 # MIPS-NEXT:      Machine: EM_MIPS (0x8)
-# PPC-NEXT:       Machine: EM_PPC64 (0x15)
+# PPC-NEXT:       Machine: EM_PPC (0x14)
+# PPC64-NEXT:     Machine: EM_PPC64 (0x15)
 # RISCV32-NEXT:   Machine: EM_RISCV (0xF3)
 # RISCV64-NEXT:   Machine: EM_RISCV (0xF3)
 # SPARC-NEXT:     Machine: EM_SPARC (0x2)
 # SPARCEL-NEXT:   Machine: EM_SPARC (0x2)
 # X86-64-NEXT:    Machine: EM_X86_64 (0x3E)
+
 # CHECK-NEXT:     Version: 1
 # CHECK-NEXT:     Entry: 0x0
 # CHECK-NEXT:     ProgramHeaderOffset:
index 143a18c0a7e57afabc4e3c292cb3b1ce4942fc08..c0112050be4e121b19a4c75269619afe7ddade9e 100644 (file)
@@ -1,22 +1,22 @@
 ## Ensure that the visibility of the start, end and size symbols created when
 ## using binary input can be specified with the --new-symbol-visibility switch.
 
-# RUN: llvm-objcopy -I binary -B i386:x86-64 %s %t.unspecified
+# RUN: llvm-objcopy -I binary -O elf64-x86-64 %s %t.unspecified
 # RUN: llvm-readelf -s %t.unspecified | FileCheck %s --check-prefix=BINARY -DVISIBILITY=DEFAULT
-# RUN: llvm-objcopy --new-symbol-visibility default -I binary -B i386:x86-64 %s %t.default
+# RUN: llvm-objcopy --new-symbol-visibility default -I binary -O elf64-x86-64 %s %t.default
 # RUN: llvm-readelf -s %t.default | FileCheck %s --check-prefix=BINARY -DVISIBILITY=DEFAULT
-# RUN: llvm-objcopy --new-symbol-visibility hidden -I binary -B i386:x86-64 %s %t.hidden
+# RUN: llvm-objcopy --new-symbol-visibility hidden -I binary -O elf64-x86-64 %s %t.hidden
 # RUN: llvm-readelf -s %t.hidden | FileCheck %s --check-prefix=BINARY -DVISIBILITY=HIDDEN
-# RUN: llvm-objcopy --new-symbol-visibility protected -I binary -B i386:x86-64 %s %t.protected
+# RUN: llvm-objcopy --new-symbol-visibility protected -I binary -O elf64-x86-64 %s %t.protected
 # RUN: llvm-readelf -s %t.protected | FileCheck %s --check-prefix=BINARY -DVISIBILITY=PROTECTED
-# RUN: llvm-objcopy --new-symbol-visibility internal -I binary -B i386:x86-64 %s %t.internal
+# RUN: llvm-objcopy --new-symbol-visibility internal -I binary -O elf64-x86-64 %s %t.internal
 # RUN: llvm-readelf -s %t.internal | FileCheck %s --check-prefix=BINARY -DVISIBILITY=INTERNAL
 
 # BINARY:      [[VISIBILITY]] {{.*}} _binary{{.*}}_start
 # BINARY-NEXT: [[VISIBILITY]] {{.*}} _binary{{.*}}_end
 # BINARY-NEXT: [[VISIBILITY]] {{.*}} _binary{{.*}}_size
 
-# RUN: not llvm-objcopy --new-symbol-visibility fluff -I binary -B i386:x86-64 %s %t.error 2>&1 \
-# RUN:   | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-objcopy --new-symbol-visibility fluff -I binary -O elf64-x86-64 \
+# RUN:   %s %t.error 2>&1 | FileCheck %s --check-prefix=ERR
 
 # ERR: error: 'fluff' is not a valid symbol visibility
index d235f23e13dc0626df3d8d501a7fc64ca2d39746..73e9221a476b49a260e681171f1e9b2bb49baff8 100644 (file)
@@ -491,14 +491,12 @@ Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
                            .Default(FileFormat::Unspecified);
   if (Config.InputFormat == FileFormat::Binary) {
     auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
-    if (BinaryArch.empty())
-      return createStringError(
-          errc::invalid_argument,
-          "specified binary input without specifiying an architecture");
-    Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch);
-    if (!MI)
-      return MI.takeError();
-    Config.BinaryArch = *MI;
+    if (!BinaryArch.empty()) {
+      Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch);
+      if (!MI)
+        return MI.takeError();
+      Config.BinaryArch = *MI;
+    }
   }
 
   if (opt::Arg *A = InputArgs.getLastArg(OBJCOPY_new_symbol_visibility)) {
@@ -520,12 +518,17 @@ Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
                             .Case("binary", FileFormat::Binary)
                             .Case("ihex", FileFormat::IHex)
                             .Default(FileFormat::Unspecified);
-  if (Config.OutputFormat == FileFormat::Unspecified && !OutputFormat.empty()) {
-    Expected<TargetInfo> Target = getOutputTargetInfoByTargetName(OutputFormat);
-    if (!Target)
-      return Target.takeError();
-    Config.OutputFormat = Target->Format;
-    Config.OutputArch = Target->Machine;
+  if (Config.OutputFormat == FileFormat::Unspecified) {
+    if (OutputFormat.empty()) {
+      Config.OutputFormat = Config.InputFormat;
+    } else {
+      Expected<TargetInfo> Target =
+          getOutputTargetInfoByTargetName(OutputFormat);
+      if (!Target)
+        return Target.takeError();
+      Config.OutputFormat = Target->Format;
+      Config.OutputArch = Target->Machine;
+    }
   }
 
   if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections,