llvm/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test

# RUN: yaml2obj --docnum=1 %s -o %t

# RUN: not llvm-objcopy --gap-fill 1 %t 2>&1 | FileCheck %s --check-prefix=NOT-BINARY
# NOT-BINARY: error: '--gap-fill' is only supported for binary output

# RUN: not llvm-objcopy -O binary --gap-fill= %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-FORMAT
# BAD-FORMAT: error: --gap-fill: bad number:

# RUN: not llvm-objcopy -O binary --gap-fill=x %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT
# BAD-INPUT: error: --gap-fill: bad number: x

# RUN: not llvm-objcopy -O binary --gap-fill=0x %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT2
# BAD-INPUT2: error: --gap-fill: bad number: 0x

# RUN: not llvm-objcopy -O binary --gap-fill=0x1G %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT3
# BAD-INPUT3: error: --gap-fill: bad number: 0x1G

# RUN: not llvm-objcopy -O binary --gap-fill=ff %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT4
# BAD-INPUT4: error: --gap-fill: bad number: ff

# RUN: not llvm-objcopy -O binary --gap-fill=0x1122 %t %t-val16 2>&1 | FileCheck %s --check-prefix=TRUNCATED-ERR
# TRUNCATED-ERR: error: gap-fill value 0x1122 is out of range (0 to 0xff)

## Test no gap fill with all allocatable output sections.
# RUN: llvm-objcopy -O binary %t %t-default
# RUN: od -v -Ax -t x1 %t-default | FileCheck %s --check-prefix=DEFAULT --ignore-case --match-full-lines
# DEFAULT:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba 00 a1 b2
# DEFAULT-NEXT: {{0*}}10 c3 d4 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: {{0*}}20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: {{0*}}30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: {{0*}}40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: {{0*}}50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: {{0*}}60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: {{0*}}70 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: {{0*}}80 00 00 89 ab cd ef
# DEFAULT-NEXT: {{0*}}86

## Test gap fill with all allocatable output sections.
# RUN: llvm-objcopy -O binary --gap-fill=0xe9 %t %t-filled
# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=FULL --ignore-case --match-full-lines
# FULL:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 a1 b2
# FULL-NEXT: {{0*}}10 c3 d4 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: {{0*}}20 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: {{0*}}30 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: {{0*}}40 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: {{0*}}50 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: {{0*}}60 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: {{0*}}70 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: {{0*}}80 e9 e9 89 ab cd ef
# FULL-NEXT: {{0*}}86

## Test gap fill with a decimal value.
# RUN: llvm-objcopy -O binary --gap-fill=99 %t %t-filled-decimal
# RUN: od -v -Ax -t x1 %t-filled-decimal | FileCheck %s --check-prefix=DEC --ignore-case --match-full-lines
# DEC:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba 63 a1 b2
# DEC-NEXT: {{0*}}10 c3 d4 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: {{0*}}20 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: {{0*}}30 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: {{0*}}40 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: {{0*}}50 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: {{0*}}60 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: {{0*}}70 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: {{0*}}80 63 63 89 ab cd ef
# DEC-NEXT: {{0*}}86

## Test gap fill with the last section removed, should be truncated.
# RUN: llvm-objcopy -O binary --gap-fill=0xe9 --remove-section=.foo %t %t-filled
# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=REMOVE-LAST-SECTION --ignore-case --match-full-lines
# REMOVE-LAST-SECTION: {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 a1 b2
# REMOVE-LAST-SECTION-NEXT: {{0*}}10 c3 d4
# REMOVE-LAST-SECTION-NEXT: {{0*}}12

## Test gap fill with the middle section removed, should be filled.
# RUN: llvm-objcopy -O binary --gap-fill=0xe9 --remove-section=.gap2 %t %t-filled
# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=REMOVE-MIDDLE-SECTION --ignore-case --match-full-lines
# REMOVE-MIDDLE-SECTION:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}10 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}20 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}30 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}40 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}50 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}60 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}70 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}80 e9 e9 89 ab cd ef
# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}86

--- !ELF
FileHeader:
  Class:           ELFCLASS64
  Data:            ELFDATA2LSB
  Type:            ET_EXEC
  Machine:         EM_X86_64
Sections:
  - Name:            .space1
    Type:            Fill
    Pattern:         'ABCD'
    Size:            0x2
  - Name:            .nogap
    Type:            SHT_PROGBITS
    Flags:           [ SHF_ALLOC ]
    Address:         0x0102
    Size:            0x6
    Content:         'EEFF11223344'
  - Name:            .gap1
    Type:            SHT_PROGBITS
    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
    Address:         0x0108
    Content:         'AABBCCDDFEDCBA'
  - Name:            .zero_size
    Type:            SHT_PROGBITS
    Flags:           [ SHF_ALLOC ]
    Address:         0x0110
    Size:            0
  - Name:            .space2
    Type:            Fill
    Pattern:         'DC'
    Size:            1
  - Name:            .gap2
    Type:            SHT_PROGBITS
    Flags:           [ SHF_ALLOC ]
    Address:         0x0110
    Content:         'A1B2C3D4'
  - Name:            .space3
    Type:            Fill
    Pattern:         'FE'
    Size:            0x1
  - Name:            .nobit_tbss
    Type:            SHT_NOBITS
    Flags:           [ SHF_WRITE, SHF_ALLOC, SHF_TLS ]
    Address:         0x0180
    Size:            0x0018
  - Name:            .space4
    Type:            Fill
    Pattern:         '01234567'
    Size:            0x4
  - Name:            .foo
    Type:            SHT_PROGBITS
    Flags:           [ SHF_WRITE, SHF_ALLOC ]
    Address:         0x0184
    Content:         '89ABCDEF'
  - Name:            .nobit_bss
    Type:            SHT_NOBITS
    Flags:           [ SHF_WRITE, SHF_ALLOC ]
    Address:         0x018A
    Size:            0x0008
  - Name:            .comment
    Type:            SHT_PROGBITS
    Flags:           [ SHF_MERGE, SHF_STRINGS ]
    EntSize:         0x0001
    Content:         4743433A

## In this test, output sections are defined out of order with respect to their
## load addresses. Verify that gaps are still correctly filled.

# RUN: yaml2obj --docnum=2 %s -o %t.2
# RUN: llvm-objcopy -O binary --gap-fill=0xe9 %t.2 %t.2.filled
# RUN: od -v -Ax -t x1 %t.2.filled | FileCheck --ignore-case --match-full-lines %s
# CHECK: {{0*}}00 aa bb cc dd e9 e9 e9 e9 11 22 33 44

--- !ELF
FileHeader:
  Class:           ELFCLASS64
  Data:            ELFDATA2LSB
  Type:            ET_EXEC
  Machine:         EM_X86_64
Sections:
  - Name:            .bss
    Type:            SHT_NOBITS
    Flags:           [ SHF_ALLOC, SHF_WRITE ]
    Address:         0x0104
    Size:            4
  - Name:            .section1
    Type:            SHT_PROGBITS
    Flags:           [ SHF_ALLOC, SHF_WRITE ]
    Address:         0x0108
    Content:         '11223344'
  - Name:            .section3
    Type:            SHT_PROGBITS
    Flags:           [ SHF_ALLOC, SHF_WRITE ]
    Address:         0x0100
    Content:         'AABBCCDD'