--- /dev/null
+## Check that program headers are output correctly\r
+\r
+# RUN: yaml2obj --docnum=1 -DBITS=32 -DMACHINE=EM_386 %s -o %t32.elf\r
+# RUN: llvm-objdump --private-headers %t32.elf | FileCheck %s --check-prefixes=ELF32 \r
+\r
+# ELF32: Program Header:\r
+# ELF32-NEXT: PHDR off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags -w-\r
+# ELF32-NEXT: PHDR off 0x00000317 vaddr 0x00002000 paddr 0x00002000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000007 memsz 0x00000007 flags --x\r
+# ELF32-NEXT: UNKNOWN off 0x00000317 vaddr 0x00002000 paddr 0x00002000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000007 memsz 0x00000007 flags --x\r
+# ELF32-NEXT: DYNAMIC off 0x00000324 vaddr 0x00006000 paddr 0x00006000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000010 memsz 0x00000010 flags rwx\r
+# ELF32-NEXT: INTERP off 0x0000031e vaddr 0x00003000 paddr 0x00003000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000004 memsz 0x00000004 flags rw-\r
+# ELF32-NEXT: NOTE off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000001 memsz 0x00000001 flags ---\r
+# ELF32-NEXT: TLS off 0x00000322 vaddr 0x00004000 paddr 0x00004000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000001 memsz 0x00000001 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT:EH_FRAME off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: STACK off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: RELRO off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT:PROPERTY off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: OPENBSD_RANDOMIZE off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: OPENBSD_WXNEEDED off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: OPENBSD_BOOTDATA off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-NEXT: UNKNOWN off 0x00000314 vaddr 0x00001000 paddr 0x00001000 align 2**0\r
+# ELF32-NEXT: filesz 0x00000003 memsz 0x00000003 flags ---\r
+# ELF32-EMPTY:\r
+\r
+# RUN: yaml2obj --docnum=1 -DBITS=64 -DMACHINE=EM_X86_64 %s -o %t64.elf\r
+# RUN: llvm-objdump --private-headers %t64.elf | FileCheck %s --check-prefixes=ELF64\r
+\r
+# ELF64: Program Header:\r
+# ELF64-NEXT: PHDR off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags -w-\r
+# ELF64-NEXT: PHDR off 0x000000000000054b vaddr 0x0000000000002000 paddr 0x0000000000002000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000007 memsz 0x0000000000000007 flags --x\r
+# ELF64-NEXT: UNKNOWN off 0x000000000000054b vaddr 0x0000000000002000 paddr 0x0000000000002000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000007 memsz 0x0000000000000007 flags --x\r
+# ELF64-NEXT: DYNAMIC off 0x0000000000000558 vaddr 0x0000000000006000 paddr 0x0000000000006000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000020 memsz 0x0000000000000020 flags rwx\r
+# ELF64-NEXT: INTERP off 0x0000000000000552 vaddr 0x0000000000003000 paddr 0x0000000000003000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000004 memsz 0x0000000000000004 flags rw-\r
+# ELF64-NEXT: NOTE off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000001 memsz 0x0000000000000001 flags ---\r
+# ELF64-NEXT: TLS off 0x0000000000000556 vaddr 0x0000000000004000 paddr 0x0000000000004000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000001 memsz 0x0000000000000001 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT:EH_FRAME off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: STACK off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: RELRO off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: PROPERTY off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: OPENBSD_RANDOMIZE off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: OPENBSD_WXNEEDED off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: OPENBSD_BOOTDATA off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-NEXT: UNKNOWN off 0x0000000000000548 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**0\r
+# ELF64-NEXT: filesz 0x0000000000000003 memsz 0x0000000000000003 flags ---\r
+# ELF64-EMPTY:\r
+\r
+--- !ELF\r
+FileHeader:\r
+ Class: ELFCLASS[[BITS]]\r
+ Data: ELFDATA2LSB\r
+ Type: ET_EXEC\r
+ Machine: [[MACHINE]]\r
+Sections:\r
+ - Name: .foo.begin\r
+ Type: SHT_PROGBITS\r
+ Flags: [ SHF_ALLOC ]\r
+ Address: 0x1000\r
+ Size: 0x1\r
+ - Name: .foo.end\r
+ Type: SHT_PROGBITS\r
+ Flags: [ SHF_ALLOC ]\r
+ Size: 0x2\r
+ - Name: .bar.begin\r
+ Type: SHT_PROGBITS\r
+ Flags: [ SHF_ALLOC ]\r
+ Address: 0x2000\r
+ Size: 0x3\r
+ - Name: .bar.end\r
+ Type: SHT_PROGBITS\r
+ Flags: [ SHF_ALLOC ]\r
+ Size: 0x4\r
+ - Name: .interp\r
+ Type: SHT_PROGBITS\r
+ Flags: [ SHF_ALLOC ]\r
+ Address: 0x3000\r
+ Content: "41424300" ## "ABC"\r
+ - Name: .tls\r
+ Type: SHT_PROGBITS\r
+ Flags: [ SHF_ALLOC, SHF_TLS ]\r
+ Address: 0x4000\r
+ Size: 0x1\r
+ - Name: .unused\r
+ Type: SHT_PROGBITS\r
+ Flags: [ SHF_ALLOC, SHF_TLS ]\r
+ Address: 0x5000\r
+ Size: 0x1\r
+ - Name: .dynamic\r
+ Type: SHT_DYNAMIC\r
+ Flags: [ SHF_ALLOC ]\r
+ Address: 0x6000\r
+ Entries:\r
+ - Tag: DT_NEEDED\r
+ Value: 0x1\r
+ - Tag: DT_NULL\r
+ Value: 0x0\r
+ProgramHeaders:\r
+## Case 1: an arbitrary segment with sections.\r
+ - Type: PT_PHDR\r
+ Flags: [ PF_W ]\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 2: another segment with different sections.\r
+ - Type: PT_PHDR\r
+ Flags: [ PF_X ]\r
+ VAddr: 0x2000\r
+ FirstSec: .bar.begin\r
+ LastSec: .bar.end\r
+## Case 3: the PT_NULL segment.\r
+ - Type: PT_NULL\r
+ Flags: [ PF_X ]\r
+ VAddr: 0x2000\r
+ FirstSec: .bar.begin\r
+ LastSec: .bar.end\r
+## Case 4: the PT_DYNAMIC segment.\r
+ - Type: PT_DYNAMIC\r
+ Flags: [ PF_R, PF_W, PF_X ]\r
+ VAddr: 0x6000\r
+ FirstSec: .dynamic\r
+ LastSec: .dynamic\r
+## Case 5: the PT_INTERP segment.\r
+ - Type: PT_INTERP\r
+ Flags: [ PF_R, PF_W ]\r
+ VAddr: 0x3000\r
+ FirstSec: .interp\r
+ LastSec: .interp\r
+## Case 6: the PT_NOTE segment.\r
+ - Type: PT_NOTE\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 7: the PT_SHLIB segment.\r
+ - Type: PT_SHLIB\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.begin\r
+## Case 8: the PT_TLS segment.\r
+ - Type: PT_TLS\r
+ VAddr: 0x4000\r
+ FirstSec: .tls\r
+ LastSec: .tls\r
+## Case 9: the PT_LOOS segment.\r
+ - Type: 0x60000000 ## PT_LOOS\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 10: the PT_GNU_EH_FRAME segment.\r
+ - Type: PT_GNU_EH_FRAME\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 11: the PT_SUNW_UNWIND segment.\r
+ - Type: 0x6464e550 ## PT_SUNW_UNWIND\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 12: the PT_GNU_STACK segment.\r
+ - Type: PT_GNU_STACK\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 13: the PT_GNU_RELRO segment.\r
+ - Type: PT_GNU_RELRO\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 14: the PT_GNU_PROPERTY segment.\r
+ - Type: PT_GNU_PROPERTY\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 15: the PT_OPENBSD_RANDOMIZE segment.\r
+ - Type: 0x65a3dbe6 ## PT_OPENBSD_RANDOMIZE\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 16: the PT_OPENBSD_WXNEEDED segment.\r
+ - Type: 0x65a3dbe7 ## PT_OPENBSD_WXNEEDED\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 17: the PT_OPENBSD_BOOTDATA segment.\r
+ - Type: 0x65a41be6 ## PT_OPENBSD_BOOTDATA\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 18: the PT_HIOS segment.\r
+ - Type: 0x6fffffff ## PT_HIOS\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 19: the PT_LOPROC/PT_ARM_ARCHEXT/PT_MIPS_REGINFO segment.\r
+ - Type: 0x70000000 ## PT_LOPROC/PT_ARM_ARCHEXT/PT_MIPS_REGINFO\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 20: the PT_ARM_EXIDX/PT_MIPS_RTPROC segment.\r
+ - Type: 0x70000001 ## PT_ARM_EXIDX, PT_MIPS_RTPROC\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 20: the PT_MIPS_OPTIONS segment.\r
+ - Type: 0x70000002 ## PT_MIPS_OPTIONS\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 21: the PT_MIPS_ABIFLAGS segment.\r
+ - Type: 0x70000003 ## PT_MIPS_ABIFLAGS\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+## Case 22: the PT_HIPROC segment.\r
+ - Type: 0x7fffffff ## PT_HIPROC\r
+ VAddr: 0x1000\r
+ FirstSec: .foo.begin\r
+ LastSec: .foo.end\r
+\r
+## Check we report an error / warning when we are unable to read program headers.\r
+## Case A: the e_phentsize field is invalid.\r
+# RUN: yaml2obj --docnum=2 -DPHENTSIZE=1 %s -o %t.phdr.err\r
+# RUN: not llvm-objdump --private-headers %t.phdr.err 2>&1 | \\r
+# RUN: FileCheck %s -DFILE=%t.phdr.err --check-prefix=PHENTSIZE\r
+\r
+# PHENTSIZE: Program Header:\r
+# PHENTSIZE-NEXT: warning: '[[FILE]]': unable to read program headers: invalid e_phentsize: 1\r
+# PHENTSIZE-NEXT: error: '[[FILE]]': invalid e_phentsize: 1\r
+\r
+--- !ELF\r
+FileHeader:\r
+ Class: ELFCLASS64\r
+ Data: ELFDATA2LSB\r
+ Type: ET_EXEC\r
+ EPhEntSize: [[PHENTSIZE=<none>]]\r
+ EPhOff: [[PHOFF=<none>]]\r
+Sections:\r
+ - Name: .foo\r
+ Type: SHT_PROGBITS\r
+ProgramHeaders:\r
+ - Type: PT_PHDR\r
+ FirstSec: .foo\r
+ LastSec: .foo\r
+\r
+## Case B: the value of the e_phoff field is invalid.\r
+\r
+## Check that we do not report a warning when the program header table ends right before the end of the file.\r
+## 0x160 + size of headers (56) == file size.\r
+# RUN: yaml2obj --docnum=2 -DPHOFF=0x160 %s -o %t.phdr.no.err2\r
+# RUN: llvm-objdump %t.phdr.no.err2 --private-headers 2>&1 | FileCheck %s --implicit-check-not=warning:\r
+\r
+## Check we report a warning / error when e_phoff goes 1 byte past the end of the file.\r
+# RUN: yaml2obj --docnum=2 -DPHOFF=0x161 %s -o %t.phdr.err2\r
+# RUN: not llvm-objdump --private-headers %t.phdr.err2 2>&1 | \\r
+# RUN: FileCheck %s -DFILE=%t.phdr.err2 --check-prefix=PHOFF -DOFF=0x161\r
+\r
+# PHOFF: Program Header:\r
+# PHOFF-NEXT: warning: '[[FILE]]': unable to read program headers: program headers are longer than binary of size 408: e_phoff = [[OFF]], e_phnum = 1, e_phentsize = 56\r
+# PHOFF-NEXT: error: '[[FILE]]': program headers are longer than binary of size 408: e_phoff = [[OFF]], e_phnum = 1, e_phentsize = 56\r
+\r
+\r
+## Check we report a warning / error when the value of e_phoff is so large that\r
+## e_phoff + e_phnum * e_phentsize > UINT64_MAX.\r
+# RUN: yaml2obj --docnum=2 -DPHOFF=0xffffffffffffffff %s -o %t.phdr.err3\r
+# RUN: not llvm-objdump --private-headers %t.phdr.err3 2>&1 | \\r
+# RUN: FileCheck %s -DFILE=%t.phdr.err3 --check-prefix=PHOFF -DOFF=0xffffffffffffffff\r