xref: /llvm-project/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test (revision 535520c6636f9a2bd7cd8fcb221e598a372cae9e)
14070dffdSquic-akaryaki# RUN: yaml2obj --docnum=1 %s -o %t
24070dffdSquic-akaryaki
34070dffdSquic-akaryaki# RUN: not llvm-objcopy --gap-fill 1 %t 2>&1 | FileCheck %s --check-prefix=NOT-BINARY
44070dffdSquic-akaryaki# NOT-BINARY: error: '--gap-fill' is only supported for binary output
54070dffdSquic-akaryaki
64070dffdSquic-akaryaki# RUN: not llvm-objcopy -O binary --gap-fill= %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-FORMAT
74070dffdSquic-akaryaki# BAD-FORMAT: error: --gap-fill: bad number:
84070dffdSquic-akaryaki
94070dffdSquic-akaryaki# RUN: not llvm-objcopy -O binary --gap-fill=x %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT
104070dffdSquic-akaryaki# BAD-INPUT: error: --gap-fill: bad number: x
114070dffdSquic-akaryaki
124070dffdSquic-akaryaki# RUN: not llvm-objcopy -O binary --gap-fill=0x %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT2
134070dffdSquic-akaryaki# BAD-INPUT2: error: --gap-fill: bad number: 0x
144070dffdSquic-akaryaki
154070dffdSquic-akaryaki# RUN: not llvm-objcopy -O binary --gap-fill=0x1G %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT3
164070dffdSquic-akaryaki# BAD-INPUT3: error: --gap-fill: bad number: 0x1G
174070dffdSquic-akaryaki
184070dffdSquic-akaryaki# RUN: not llvm-objcopy -O binary --gap-fill=ff %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT4
194070dffdSquic-akaryaki# BAD-INPUT4: error: --gap-fill: bad number: ff
204070dffdSquic-akaryaki
214070dffdSquic-akaryaki# RUN: not llvm-objcopy -O binary --gap-fill=0x1122 %t %t-val16 2>&1 | FileCheck %s --check-prefix=TRUNCATED-ERR
224070dffdSquic-akaryaki# TRUNCATED-ERR: error: gap-fill value 0x1122 is out of range (0 to 0xff)
234070dffdSquic-akaryaki
244070dffdSquic-akaryaki## Test no gap fill with all allocatable output sections.
254070dffdSquic-akaryaki# RUN: llvm-objcopy -O binary %t %t-default
262439bc4aSquic-akaryaki# RUN: od -v -Ax -t x1 %t-default | FileCheck %s --check-prefix=DEFAULT --ignore-case --match-full-lines
272439bc4aSquic-akaryaki# DEFAULT:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba 00 a1 b2
282439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}10 c3 d4 00 00 00 00 00 00 00 00 00 00 00 00 00 00
292439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
302439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
312439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
322439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
332439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
342439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}70 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
352439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}80 00 00 89 ab cd ef
362439bc4aSquic-akaryaki# DEFAULT-NEXT: {{0*}}86
374070dffdSquic-akaryaki
384070dffdSquic-akaryaki## Test gap fill with all allocatable output sections.
394070dffdSquic-akaryaki# RUN: llvm-objcopy -O binary --gap-fill=0xe9 %t %t-filled
402439bc4aSquic-akaryaki# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=FULL --ignore-case --match-full-lines
412439bc4aSquic-akaryaki# FULL:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 a1 b2
422439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}10 c3 d4 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
432439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}20 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
442439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}30 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
452439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}40 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
462439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}50 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
472439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}60 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
482439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}70 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
492439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}80 e9 e9 89 ab cd ef
502439bc4aSquic-akaryaki# FULL-NEXT: {{0*}}86
514070dffdSquic-akaryaki
524070dffdSquic-akaryaki## Test gap fill with a decimal value.
534070dffdSquic-akaryaki# RUN: llvm-objcopy -O binary --gap-fill=99 %t %t-filled-decimal
542439bc4aSquic-akaryaki# RUN: od -v -Ax -t x1 %t-filled-decimal | FileCheck %s --check-prefix=DEC --ignore-case --match-full-lines
552439bc4aSquic-akaryaki# DEC:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba 63 a1 b2
562439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}10 c3 d4 63 63 63 63 63 63 63 63 63 63 63 63 63 63
572439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}20 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
582439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}30 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
592439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}40 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
602439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}50 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
612439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}60 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
622439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}70 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
632439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}80 63 63 89 ab cd ef
642439bc4aSquic-akaryaki# DEC-NEXT: {{0*}}86
654070dffdSquic-akaryaki
664070dffdSquic-akaryaki## Test gap fill with the last section removed, should be truncated.
674070dffdSquic-akaryaki# RUN: llvm-objcopy -O binary --gap-fill=0xe9 --remove-section=.foo %t %t-filled
682439bc4aSquic-akaryaki# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=REMOVE-LAST-SECTION --ignore-case --match-full-lines
692439bc4aSquic-akaryaki# REMOVE-LAST-SECTION: {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 a1 b2
702439bc4aSquic-akaryaki# REMOVE-LAST-SECTION-NEXT: {{0*}}10 c3 d4
712439bc4aSquic-akaryaki# REMOVE-LAST-SECTION-NEXT: {{0*}}12
724070dffdSquic-akaryaki
734070dffdSquic-akaryaki## Test gap fill with the middle section removed, should be filled.
744070dffdSquic-akaryaki# RUN: llvm-objcopy -O binary --gap-fill=0xe9 --remove-section=.gap2 %t %t-filled
752439bc4aSquic-akaryaki# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=REMOVE-MIDDLE-SECTION --ignore-case --match-full-lines
762439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION:      {{0*}}00 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 e9 e9
772439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}10 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
782439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}20 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
792439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}30 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
802439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}40 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
812439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}50 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
822439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}60 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
832439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}70 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
842439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}80 e9 e9 89 ab cd ef
852439bc4aSquic-akaryaki# REMOVE-MIDDLE-SECTION-NEXT: {{0*}}86
864070dffdSquic-akaryaki
874070dffdSquic-akaryaki--- !ELF
884070dffdSquic-akaryakiFileHeader:
894070dffdSquic-akaryaki  Class:           ELFCLASS64
904070dffdSquic-akaryaki  Data:            ELFDATA2LSB
914070dffdSquic-akaryaki  Type:            ET_EXEC
924070dffdSquic-akaryaki  Machine:         EM_X86_64
934070dffdSquic-akaryakiSections:
944070dffdSquic-akaryaki  - Name:            .space1
954070dffdSquic-akaryaki    Type:            Fill
964070dffdSquic-akaryaki    Pattern:         'ABCD'
974070dffdSquic-akaryaki    Size:            0x2
984070dffdSquic-akaryaki  - Name:            .nogap
994070dffdSquic-akaryaki    Type:            SHT_PROGBITS
1004070dffdSquic-akaryaki    Flags:           [ SHF_ALLOC ]
1014070dffdSquic-akaryaki    Address:         0x0102
1024070dffdSquic-akaryaki    Size:            0x6
1034070dffdSquic-akaryaki    Content:         'EEFF11223344'
1044070dffdSquic-akaryaki  - Name:            .gap1
1054070dffdSquic-akaryaki    Type:            SHT_PROGBITS
1064070dffdSquic-akaryaki    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
1074070dffdSquic-akaryaki    Address:         0x0108
1084070dffdSquic-akaryaki    Content:         'AABBCCDDFEDCBA'
109*535520c6Squic-akaryaki  - Name:            .zero_size
110*535520c6Squic-akaryaki    Type:            SHT_PROGBITS
111*535520c6Squic-akaryaki    Flags:           [ SHF_ALLOC ]
112*535520c6Squic-akaryaki    Address:         0x0110
113*535520c6Squic-akaryaki    Size:            0
1144070dffdSquic-akaryaki  - Name:            .space2
1154070dffdSquic-akaryaki    Type:            Fill
1164070dffdSquic-akaryaki    Pattern:         'DC'
1174070dffdSquic-akaryaki    Size:            1
1184070dffdSquic-akaryaki  - Name:            .gap2
1194070dffdSquic-akaryaki    Type:            SHT_PROGBITS
1204070dffdSquic-akaryaki    Flags:           [ SHF_ALLOC ]
1214070dffdSquic-akaryaki    Address:         0x0110
1224070dffdSquic-akaryaki    Content:         'A1B2C3D4'
1234070dffdSquic-akaryaki  - Name:            .space3
1244070dffdSquic-akaryaki    Type:            Fill
1254070dffdSquic-akaryaki    Pattern:         'FE'
1264070dffdSquic-akaryaki    Size:            0x1
1274070dffdSquic-akaryaki  - Name:            .nobit_tbss
1284070dffdSquic-akaryaki    Type:            SHT_NOBITS
1294070dffdSquic-akaryaki    Flags:           [ SHF_WRITE, SHF_ALLOC, SHF_TLS ]
1304070dffdSquic-akaryaki    Address:         0x0180
1314070dffdSquic-akaryaki    Size:            0x0018
1324070dffdSquic-akaryaki  - Name:            .space4
1334070dffdSquic-akaryaki    Type:            Fill
1344070dffdSquic-akaryaki    Pattern:         '01234567'
1354070dffdSquic-akaryaki    Size:            0x4
1364070dffdSquic-akaryaki  - Name:            .foo
1374070dffdSquic-akaryaki    Type:            SHT_PROGBITS
1384070dffdSquic-akaryaki    Flags:           [ SHF_WRITE, SHF_ALLOC ]
1394070dffdSquic-akaryaki    Address:         0x0184
1404070dffdSquic-akaryaki    Content:         '89ABCDEF'
1414070dffdSquic-akaryaki  - Name:            .nobit_bss
1424070dffdSquic-akaryaki    Type:            SHT_NOBITS
1434070dffdSquic-akaryaki    Flags:           [ SHF_WRITE, SHF_ALLOC ]
1444070dffdSquic-akaryaki    Address:         0x018A
1454070dffdSquic-akaryaki    Size:            0x0008
1464070dffdSquic-akaryaki  - Name:            .comment
1474070dffdSquic-akaryaki    Type:            SHT_PROGBITS
1484070dffdSquic-akaryaki    Flags:           [ SHF_MERGE, SHF_STRINGS ]
1494070dffdSquic-akaryaki    EntSize:         0x0001
1504070dffdSquic-akaryaki    Content:         4743433A
1514070dffdSquic-akaryaki
1524070dffdSquic-akaryaki## In this test, output sections are defined out of order with respect to their
1534070dffdSquic-akaryaki## load addresses. Verify that gaps are still correctly filled.
1544070dffdSquic-akaryaki
1554070dffdSquic-akaryaki# RUN: yaml2obj --docnum=2 %s -o %t.2
1564070dffdSquic-akaryaki# RUN: llvm-objcopy -O binary --gap-fill=0xe9 %t.2 %t.2.filled
1572439bc4aSquic-akaryaki# RUN: od -v -Ax -t x1 %t.2.filled | FileCheck --ignore-case --match-full-lines %s
1582439bc4aSquic-akaryaki# CHECK: {{0*}}00 aa bb cc dd e9 e9 e9 e9 11 22 33 44
1594070dffdSquic-akaryaki
1604070dffdSquic-akaryaki--- !ELF
1614070dffdSquic-akaryakiFileHeader:
1624070dffdSquic-akaryaki  Class:           ELFCLASS64
1634070dffdSquic-akaryaki  Data:            ELFDATA2LSB
1644070dffdSquic-akaryaki  Type:            ET_EXEC
1654070dffdSquic-akaryaki  Machine:         EM_X86_64
1664070dffdSquic-akaryakiSections:
1674070dffdSquic-akaryaki  - Name:            .bss
1684070dffdSquic-akaryaki    Type:            SHT_NOBITS
1694070dffdSquic-akaryaki    Flags:           [ SHF_ALLOC, SHF_WRITE ]
1704070dffdSquic-akaryaki    Address:         0x0104
1714070dffdSquic-akaryaki    Size:            4
1724070dffdSquic-akaryaki  - Name:            .section1
1734070dffdSquic-akaryaki    Type:            SHT_PROGBITS
1744070dffdSquic-akaryaki    Flags:           [ SHF_ALLOC, SHF_WRITE ]
1754070dffdSquic-akaryaki    Address:         0x0108
1764070dffdSquic-akaryaki    Content:         '11223344'
1774070dffdSquic-akaryaki  - Name:            .section3
1784070dffdSquic-akaryaki    Type:            SHT_PROGBITS
1794070dffdSquic-akaryaki    Flags:           [ SHF_ALLOC, SHF_WRITE ]
1804070dffdSquic-akaryaki    Address:         0x0100
1814070dffdSquic-akaryaki    Content:         'AABBCCDD'
182