Skip to content

Commit

Permalink
[llvm-objcopy] Add support for -I binary -B <arch>.
Browse files Browse the repository at this point in the history
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.

This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).

Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar

Reviewed By: jhenderson

Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits

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

llvm-svn: 340070
  • Loading branch information
rupprecht committed Aug 17, 2018
1 parent da5864c commit cf67633
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 64 deletions.
15 changes: 15 additions & 0 deletions llvm/test/tools/llvm-objcopy/binary-input-and-output.test
@@ -0,0 +1,15 @@
# RUN: echo abcd > %t.txt

# 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
# 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
# RUN: cmp %t.txt %t.3.txt
# RUN: cmp %t.txt %t-copy.txt
75 changes: 75 additions & 0 deletions llvm/test/tools/llvm-objcopy/binary-input-arch.test
@@ -0,0 +1,75 @@
# RUN: echo abcd > %t.txt

# RUN: llvm-objcopy -I binary -B aarch64 %t.txt %t.aarch64.o
# RUN: llvm-readobj -file-headers %t.aarch64.o | FileCheck %s --check-prefixes=CHECK,AARCH64,64

# RUN: llvm-objcopy -I binary -B arm %t.txt %t.arm.o
# RUN: llvm-readobj -file-headers %t.arm.o | FileCheck %s --check-prefixes=CHECK,ARM,32

# RUN: llvm-objcopy -I binary -B i386 %t.txt %t.i386.o
# RUN: llvm-readobj -file-headers %t.i386.o | FileCheck %s --check-prefixes=CHECK,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,X86-64,64

# 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,PPC,64

# RUN: llvm-objcopy -I binary -B sparc %t.txt %t.sparc.o
# RUN: llvm-readobj -file-headers %t.sparc.o | FileCheck %s --check-prefixes=CHECK,SPARC,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,X86-64,64

# CHECK: Format:
# AARCH64-SAME: ELF64-aarch64-little
# ARM-SAME: ELF32-arm-little
# I386-SAME: ELF32-i386
# PPC-SAME: ELF64-ppc64
# SPARC-SAME: ELF32-sparc
# X86-64-SAME: ELF64-x86-64

# AARCH64-NEXT: Arch: aarch64
# ARM-NEXT: Arch: arm
# I386-NEXT: Arch: i386
# PPC-NEXT: Arch: powerpc64le
# SPARC-NEXT: Arch: sparcel
# X86-64-NEXT: Arch: x86_64

# 32-NEXT: AddressSize: 32bit
# 64-NEXT: AddressSize: 64bit

# CHECK: ElfHeader {
# CHECK-NEXT: Ident {
# CHECK-NEXT: Magic: (7F 45 4C 46)
# 32-NEXT: Class: 32-bit (0x1)
# 64-NEXT: Class: 64-bit (0x2)
# CHECK-NEXT: DataEncoding: LittleEndian (0x1)
# CHECK-NEXT: FileVersion: 1
# CHECK-NEXT: OS/ABI: SystemV (0x0)
# CHECK-NEXT: ABIVersion: 0
# CHECK-NEXT: Unused: (00 00 00 00 00 00 00)
# CHECK-NEXT: }
# CHECK-NEXT: Type: Relocatable (0x1)
# AARCH64-NEXT: Machine: EM_AARCH64 (0xB7)
# ARM-NEXT: Machine: EM_ARM (0x28)
# I386-NEXT: Machine: EM_386 (0x3)
# PPC-NEXT: Machine: EM_PPC64 (0x15)
# SPARC-NEXT: Machine: EM_SPARC (0x2)
# X86-64-NEXT: Machine: EM_X86_64 (0x3E)
# CHECK-NEXT: Version: 1
# CHECK-NEXT: Entry: 0x0
# CHECK-NEXT: ProgramHeaderOffset:
# CHECK-NEXT: SectionHeaderOffset:
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# 32-NEXT: HeaderSize: 52
# 64-NEXT: HeaderSize: 64
# 32-NEXT: ProgramHeaderEntrySize: 32
# 64-NEXT: ProgramHeaderEntrySize: 56
# CHECK-NEXT: ProgramHeaderCount: 0
# 32-NEXT: SectionHeaderEntrySize: 40
# 64-NEXT: SectionHeaderEntrySize: 64
# CHECK-NEXT: SectionHeaderCount: 4
# CHECK-NEXT: StringTableSectionIndex:
# CHECK-NEXT: }
10 changes: 10 additions & 0 deletions llvm/test/tools/llvm-objcopy/binary-input-error.test
@@ -0,0 +1,10 @@
# 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'.
112 changes: 112 additions & 0 deletions llvm/test/tools/llvm-objcopy/binary-input.test
@@ -0,0 +1,112 @@
# 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-readobj -sections -symbols %t.o | FileCheck %s
# RUN: cmp %t.x-txt %t-copy.txt

# CHECK: Sections [
# CHECK-NEXT: Section {
# CHECK-NEXT: Index: 0
# CHECK-NEXT: Name: (0)
# CHECK-NEXT: Type: SHT_NULL (0x0)
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# CHECK-NEXT: Address: 0x0
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size:
# CHECK-NEXT: Link: 0
# CHECK-NEXT: Info: 0
# CHECK-NEXT: AddressAlignment: 0
# CHECK-NEXT: EntrySize: 0
# CHECK-NEXT: }
# CHECK-NEXT: Section {
# CHECK-NEXT: Index: 1
# CHECK-NEXT: Name: .strtab
# CHECK-NEXT: Type: SHT_STRTAB (0x3)
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# CHECK-NEXT: Address: 0x0
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size:
# CHECK-NEXT: Link: 0
# CHECK-NEXT: Info: 0
# CHECK-NEXT: AddressAlignment: 1
# CHECK-NEXT: EntrySize: 0
# CHECK-NEXT: }
# CHECK-NEXT: Section {
# CHECK-NEXT: Index: 2
# CHECK-NEXT: Name: .symtab
# CHECK-NEXT: Type: SHT_SYMTAB (0x2)
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# CHECK-NEXT: Address: 0x0
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size:
# CHECK-NEXT: Link: 1
# CHECK-NEXT: Info: 1
# CHECK-NEXT: AddressAlignment: 1
# CHECK-NEXT: EntrySize: 24
# CHECK-NEXT: }
# CHECK-NEXT: Section {
# CHECK-NEXT: Index: 3
# CHECK-NEXT: Name: .data
# CHECK-NEXT: Type: SHT_PROGBITS (0x1)
# CHECK-NEXT: Flags [ (0x3)
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: SHF_WRITE (0x1)
# CHECK-NEXT: ]
# CHECK-NEXT: Address: 0x0
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size: 4
# CHECK-NEXT: Link: 0
# CHECK-NEXT: Info: 0
# CHECK-NEXT: AddressAlignment: 1
# CHECK-NEXT: EntrySize: 0
# CHECK-NEXT: }
# CHECK-NEXT: ]

# Note: the symbol names are derived from the full path (with non-alnum values
# replaced with "_"), e.g. "/tmp/a-b.c" should yield
# _binary__tmp_a_b_c_{start,end,size}.
# Just check for _binary_{{[_a-zA-Z0-9]*}}_x_txt_{start,end,size} to avoid
# making assumptions about how this test is run.

# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name:
# CHECK-NEXT: Value: 0x0
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Binding: Local (0x0)
# CHECK-NEXT: Type: None (0x0)
# CHECK-NEXT: Other: 0
# CHECK-NEXT: Section: Undefined (0x0)
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _binary_{{[_a-zA-Z0-9]*}}_x_txt_start
# CHECK-NEXT: Value: 0x0
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Binding: Global (0x1)
# CHECK-NEXT: Type: None (0x0)
# CHECK-NEXT: Other: 0
# CHECK-NEXT: Section: .data
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _binary_{{[_a-zA-Z0-9]*}}_x_txt_end
# CHECK-NEXT: Value: 0x4
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Binding: Global (0x1)
# CHECK-NEXT: Type: None (0x0)
# CHECK-NEXT: Other: 0
# CHECK-NEXT: Section: .data
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: _binary_{{[_a-zA-Z0-9]*}}_x_txt_size
# CHECK-NEXT: Value: 0x4
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Binding: Global (0x1)
# CHECK-NEXT: Type: None (0x0)
# CHECK-NEXT: Other: 0
# CHECK-NEXT: Section: Absolute
# CHECK-NEXT: }
# CHECK-NEXT: ]

0 comments on commit cf67633

Please sign in to comment.