221 changes: 100 additions & 121 deletions llvm/test/tools/llvm-readobj/ELF/program-headers.test
Original file line number Diff line number Diff line change
Expand Up @@ -445,146 +445,125 @@ Sections:
Size: 0x1
ProgramHeaders:
## Case 1: an arbitrary segment with sections.
- Type: PT_PHDR
Flags: [ PF_W ]
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: PT_PHDR
Flags: [ PF_W ]
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 2: another segment with different sections.
- Type: PT_PHDR
Flags: [ PF_X ]
VAddr: 0x2000
Sections:
- Section: .bar.begin
- Section: .bar.end
- Type: PT_PHDR
Flags: [ PF_X ]
VAddr: 0x2000
FirstSec: .bar.begin
LastSec: .bar.end
## Case 3: the PT_NULL segment.
- Type: PT_NULL
Flags: [ PF_X ]
VAddr: 0x2000
Sections:
- Section: .bar.begin
- Section: .bar.end
- Type: PT_NULL
Flags: [ PF_X ]
VAddr: 0x2000
FirstSec: .bar.begin
LastSec: .bar.end
## Case 4: the PT_DYNAMIC segment.
- Type: PT_DYNAMIC
Flags: [ PF_R, PF_W, PF_X ]
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: PT_DYNAMIC
Flags: [ PF_R, PF_W, PF_X ]
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 5: the PT_INTERP segment.
- Type: PT_INTERP
Flags: [ PF_R, PF_W ]
VAddr: 0x3000
Sections:
- Section: .interp
- Type: PT_INTERP
Flags: [ PF_R, PF_W ]
VAddr: 0x3000
FirstSec: .interp
LastSec: .interp
## Case 6: the PT_NOTE segment.
- Type: PT_NOTE
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: PT_NOTE
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 7: the PT_SHLIB segment.
- Type: PT_SHLIB
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Type: PT_SHLIB
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.begin
## Case 8: the PT_TLS segment.
- Type: PT_TLS
VAddr: 0x4000
Sections:
- Section: .tls
- Type: PT_TLS
VAddr: 0x4000
FirstSec: .tls
LastSec: .tls
## Case 9: the PT_LOOS segment.
- Type: 0x60000000 ## PT_LOOS
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x60000000 ## PT_LOOS
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 10: the PT_GNU_EH_FRAME segment.
- Type: PT_GNU_EH_FRAME
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: PT_GNU_EH_FRAME
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 11: the PT_SUNW_UNWIND segment.
- Type: 0x6464e550 ## PT_SUNW_UNWIND
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x6464e550 ## PT_SUNW_UNWIND
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 12: the PT_GNU_STACK segment.
- Type: PT_GNU_STACK
Sections:
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: PT_GNU_STACK
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 13: the PT_GNU_RELRO segment.
- Type: PT_GNU_RELRO
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: PT_GNU_RELRO
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 14: the PT_GNU_PROPERTY segment.
- Type: PT_GNU_PROPERTY
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: PT_GNU_PROPERTY
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 15: the PT_OPENBSD_RANDOMIZE segment.
- Type: 0x65a3dbe6 ## PT_OPENBSD_RANDOMIZE
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x65a3dbe6 ## PT_OPENBSD_RANDOMIZE
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 16: the PT_OPENBSD_WXNEEDED segment.
- Type: 0x65a3dbe7 ## PT_OPENBSD_WXNEEDED
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x65a3dbe7 ## PT_OPENBSD_WXNEEDED
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 17: the PT_OPENBSD_BOOTDATA segment.
- Type: 0x65a41be6 ## PT_OPENBSD_BOOTDATA
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x65a41be6 ## PT_OPENBSD_BOOTDATA
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 18: the PT_HIOS segment.
- Type: 0x6fffffff ## PT_HIOS
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x6fffffff ## PT_HIOS
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 19: the PT_LOPROC/PT_ARM_ARCHEXT/PT_MIPS_REGINFO segment.
- Type: 0x70000000 ## PT_LOPROC/PT_ARM_ARCHEXT/PT_MIPS_REGINFO
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x70000000 ## PT_LOPROC/PT_ARM_ARCHEXT/PT_MIPS_REGINFO
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 20: the PT_ARM_EXIDX/PT_MIPS_RTPROC segment.
- Type: 0x70000001 ## PT_ARM_EXIDX, PT_MIPS_RTPROC
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x70000001 ## PT_ARM_EXIDX, PT_MIPS_RTPROC
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 20: the PT_MIPS_OPTIONS segment.
- Type: 0x70000002 ## PT_MIPS_OPTIONS
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x70000002 ## PT_MIPS_OPTIONS
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 21: the PT_MIPS_ABIFLAGS segment.
- Type: 0x70000003 ## PT_MIPS_ABIFLAGS
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x70000003 ## PT_MIPS_ABIFLAGS
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end
## Case 22: the PT_HIPROC segment.
- Type: 0x7fffffff ## PT_HIPROC
VAddr: 0x1000
Sections:
- Section: .foo.begin
- Section: .foo.end
- Type: 0x7fffffff ## PT_HIPROC
VAddr: 0x1000
FirstSec: .foo.begin
LastSec: .foo.end

## Check how we dump ARM specific program headers.
# RUN: yaml2obj --docnum=1 -DBITS=64 -DMACHINE=EM_ARM %s -o %tarm.elf
Expand Down Expand Up @@ -702,9 +681,9 @@ Sections:
- Name: .foo
Type: SHT_PROGBITS
ProgramHeaders:
- Type: PT_PHDR
Sections:
- Section: .foo
- Type: PT_PHDR
FirstSec: .foo
LastSec: .foo

## Case B: the value of the e_phoff field is invalid.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ DynamicSymbols:
- Name: force_dynsym
Binding: STB_GLOBAL
ProgramHeaders:
- Type: PT_LOAD
VAddr: 0x1000
Sections:
- Section: .dynamic
- Section: .rela.dyn
- Type: PT_DYNAMIC
VAddr: 0x1000
Sections:
- Section: .dynamic
- Type: PT_LOAD
VAddr: 0x1000
FirstSec: .dynamic
LastSec: .rela.dyn
- Type: PT_DYNAMIC
VAddr: 0x1000
FirstSec: .dynamic
LastSec: .dynamic
17 changes: 8 additions & 9 deletions llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,11 @@ DynamicSymbols:
Section: .text
Binding: STB_GLOBAL
ProgramHeaders:
- Type: PT_LOAD
VAddr: 0x1000
Sections:
- Section: .dynamic
- Section: .rela.dyn
- Type: PT_DYNAMIC
VAddr: 0x1000
Sections:
- Section: .dynamic
- Type: PT_LOAD
VAddr: 0x1000
FirstSec: .dynamic
LastSec: .rela.dyn
- Type: PT_DYNAMIC
VAddr: 0x1000
FirstSec: .dynamic
LastSec: .dynamic
19 changes: 9 additions & 10 deletions llvm/test/tools/llvm-readobj/ELF/unwind.test
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,16 @@ ProgramHeaders:
Flags: [ PF_X, PF_R ]
VAddr: 0x00400000
PAddr: 0x00400000
Sections:
- Section: .text
- Type: PT_GNU_EH_FRAME
Flags: [ PF_X, PF_R ]
VAddr: 0x004013C0
PAddr: 0x004013C0
FirstSec: .text
LastSec: .text
- Type: PT_GNU_EH_FRAME
Flags: [ PF_X, PF_R ]
VAddr: 0x004013C0
PAddr: 0x004013C0
MemSize: [[MEMSIZE]]
FileSize: [[FILESIZE]]
Sections:
- Section: .eh_frame_hdr
FirstSec: .eh_frame_hdr
LastSec: .eh_frame_hdr

## Document we report a error when the memory size of the PT_GNU_EH_FRAME does not match its file size.
## TODO: we want to report a warning and continue dumping instead.
Expand Down Expand Up @@ -326,11 +326,10 @@ FileHeader:
Data: ELFDATA2LSB
Type: ET_EXEC
ProgramHeaders:
- Type: PT_GNU_EH_FRAME
- Type: PT_GNU_EH_FRAME
MemSize: [[SIZE]]
FileSize: [[SIZE]]
Offset: [[OFFSET]]
Sections: []

## Case B: test we report an error when the file size of the PT_GNU_EH_FRAME
## is invalid (goes past the end of the file).
Expand Down
35 changes: 17 additions & 18 deletions llvm/test/tools/llvm-xray/ARM/extract-instrmap.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@ FileHeader:
Flags: [ EF_ARM_SOFT_FLOAT, EF_ARM_EABI_VER5 ]
Entry: 0x00000000000012B0
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Sections:
- Section: .rel.dyn
Align: 0x0000000000001000
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
Sections:
- Section: .text
VAddr: 0x00000000000012B0
Align: 0x0000000000001000
- Type: PT_LOAD
Flags: [ PF_W, PF_R ]
Sections:
- Section: xray_instr_map
- Section: xray_fn_idx
VAddr: 0x00000000000033CC
Align: 0x0000000000001000
- Type: PT_LOAD
Flags: [ PF_R ]
FirstSec: .rel.dyn
LastSec: .rel.dyn
Align: 0x0000000000001000
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
FirstSec: .text
LastSec: .text
VAddr: 0x00000000000012B0
Align: 0x0000000000001000
- Type: PT_LOAD
Flags: [ PF_W, PF_R ]
FirstSec: xray_instr_map
LastSec: xray_fn_idx
VAddr: 0x00000000000033CC
Align: 0x0000000000001000
Sections:
- Name: .rel.dyn
Type: SHT_REL
Expand Down
529 changes: 255 additions & 274 deletions llvm/test/tools/obj2yaml/ELF/program-headers.yaml

Large diffs are not rendered by default.

27 changes: 13 additions & 14 deletions llvm/test/tools/yaml2obj/ELF/custom-fill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,14 @@ Sections:
Pattern: ""
Size: 0x45
ProgramHeaders:
- Type: PT_LOAD
VAddr: 0x100
Sections:
- Section: fill1
- Section: .bar
- Section: fill2
- Type: PT_GNU_RELRO
VAddr: 0x105
Sections:
- Section: fill2
- Type: PT_LOAD
VAddr: 0x100
FirstSec: fill1
LastSec: fill2
- Type: PT_GNU_RELRO
VAddr: 0x105
FirstSec: fill2
LastSec: fill2

## Check that the "Pattern" field is not mandatory.
# RUN: yaml2obj --docnum=4 2>&1 -o %t4 %s
Expand Down Expand Up @@ -269,7 +267,8 @@ Sections:
## an unknown section or fill and have at least one Fill defined.

# RUN: not yaml2obj --docnum=10 2>&1 %s | FileCheck %s --check-prefix=UNKNOWN-ERR
# UNKNOWN-ERR: error: unknown section or fill referenced: 'fill' by program header
# UNKNOWN-ERR: error: unknown section or fill referenced: 'fill' by the 'FirstSec' key of the program header with index 0
# UNKNOWN-ERR: error: unknown section or fill referenced: 'fill' by the 'LastSec' key of the program header with index 0

--- !ELF
FileHeader:
Expand All @@ -281,9 +280,9 @@ Sections:
Pattern: ""
Size: 0
ProgramHeaders:
- Type: PT_LOAD
Sections:
- Section: fill
- Type: PT_LOAD
FirstSec: fill
LastSec: fill

## Show that we can use the "Offset" key to set an arbitrary offset for a Fill.

Expand Down
22 changes: 11 additions & 11 deletions llvm/test/tools/yaml2obj/ELF/dynamic-section-i386.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Sections:
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
VAddr: 0x0000
Align: 8
Sections:
- Section: .dynamic
- Type: PT_DYNAMIC
Flags: [ PF_X, PF_R ]
VAddr: 0x0008
Sections:
- Section: .dynamic
- Type: PT_LOAD
Flags: [ PF_R ]
VAddr: 0x0000
Align: 8
FirstSec: .dynamic
LastSec: .dynamic
- Type: PT_DYNAMIC
Flags: [ PF_X, PF_R ]
VAddr: 0x0008
FirstSec: .dynamic
LastSec: .dynamic
12 changes: 4 additions & 8 deletions llvm/test/tools/yaml2obj/ELF/header-sh-fields.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ FileHeader:
Data: ELFDATA2LSB
Type: ET_REL
ProgramHeaders:
- Type: PT_LOAD
Sections: []
- Type: PT_LOAD
Sections: []
- Type: PT_LOAD
- Type: PT_LOAD

## Check we can override all default values using the same values
## and that this does not change the output.
Expand All @@ -43,10 +41,8 @@ FileHeader:
EPhEntSize: [[PHENTSIZE=56]]
EPhNum: [[PHNUM=2]]
ProgramHeaders:
- Type: PT_LOAD
Sections: []
- Type: PT_LOAD
Sections: []
- Type: PT_LOAD
- Type: PT_LOAD

## Override different fields to check the output produced.

Expand Down
31 changes: 15 additions & 16 deletions llvm/test/tools/yaml2obj/ELF/program-header-address.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,27 @@ Sections:
ProgramHeaders:
## Show what virtual and physical address we set by default for the case where
## a program header has no sections.
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
Sections: []
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
## Show what virtual and physical address we set by default for the case
## where a program header includes a section with a virtual address that
## is explicitly set.
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
Sections:
- Section: .foo
FirstSec: .foo
LastSec: .foo
## Now we have a program header that has a virtual address different to
## the address of the section included. Show that the default physical address
## is equal to the program header's virtual address.
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
VAddr: 0xAAAA1000
Sections:
- Section: .foo
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
VAddr: 0xAAAA1000
FirstSec: .foo
LastSec: .foo
## Show that we are able to set different virtual and physical addresses.
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
VAddr: 0xAAAA1000
PAddr: 0xBBBB2000
Sections:
- Section: .foo
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
VAddr: 0xAAAA1000
PAddr: 0xBBBB2000
FirstSec: .foo
LastSec: .foo
12 changes: 5 additions & 7 deletions llvm/test/tools/yaml2obj/ELF/program-header-align.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ Sections:
ProgramHeaders:
- Type: PT_TLS
Align: 16
Sections:
- Section: .tdata
- Section: .tbss
FirstSec: .tdata
LastSec: .tbss

## If Align is not specified, p_align is inferred from the maximum alignment
## of contained sections.
Expand All @@ -48,7 +47,6 @@ Sections:
Type: SHT_PROGBITS
AddressAlign: 16
ProgramHeaders:
- Type: PT_LOAD
Sections:
- Section: .text
- Section: .text.hot
- Type: PT_LOAD
FirstSec: .text
LastSec: .text.hot
28 changes: 12 additions & 16 deletions llvm/test/tools/yaml2obj/ELF/program-header-nobits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@ ProgramHeaders:
## We have 2 segments, the first is predefined and the second can be customized.
## We want to have more than one segment to show we check all of them when
## trying to find a non-nobits section after a nobits one.
- Type: PT_LOAD
Flags: [ PF_R ]
Sections:
- Section: .data.before
- Section: .nobits.1
- Type: PT_LOAD
Flags: [ PF_R ]
Sections:
- Section: [[SEC1]]
- Section: [[SEC2]]
- Type: PT_LOAD
Flags: [ PF_R ]
FirstSec: .data.before
LastSec: .nobits.1
- Type: PT_LOAD
Flags: [ PF_R ]
FirstSec: [[SEC1]]
LastSec: [[SEC2]]

## Case D. We have a segment with SHT_NOBITS sections on its borders and one
## non-nobits in the middle. Check we allocate the file space only for
Expand Down Expand Up @@ -132,9 +130,7 @@ Sections:
Flags: [ SHF_ALLOC ]
Size: 0x100
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Sections:
- Section: .nobits.1
- Section: .data
- Section: .nobits.2
- Type: PT_LOAD
Flags: [ PF_R ]
FirstSec: .nobits.1
LastSec: .nobits.2
53 changes: 25 additions & 28 deletions llvm/test/tools/yaml2obj/ELF/program-header-size-offset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,25 @@ ProgramHeaders:
# Program header with only file size set.
- Type: 0x6abcdef0
FileSize: 6
Sections:
- Section: .rodata
FirstSec: .rodata
LastSec: .rodata
# Program header with only mem size set.
- Type: 0x6abcdef0
MemSize: 6
Sections:
- Section: .rodata
MemSize: 6
FirstSec: .rodata
LastSec: .rodata
# Program header with only offset set.
- Type: 0x6abcdef0
Offset: 0x1fff
Sections:
- Section: .rodata
FirstSec: .rodata
LastSec: .rodata
# Program header with sections, valid properties.
- Type: 0x6abcdef0
Offset: 0xffe
FileSize: 7
MemSize: 9
Sections:
- Section: .text
FirstSec: .text
LastSec: .text
# Program header with invalid properties.
- Type: 0x6abcdef0
Offset: 0x3000
Expand All @@ -100,10 +100,8 @@ ProgramHeaders:
# Program header with 2 SHT_NOBITS sections.
- Type: 0x6abcdef0
Offset: 0x2004
Sections:
- Section: .data
- Section: .nobits1
- Section: .nobits2
FirstSec: .data
LastSec: .nobits2

## Test the "Offset" property.

Expand Down Expand Up @@ -137,13 +135,12 @@ Sections:
Flags: [ SHF_ALLOC ]
Size: 0x1
ProgramHeaders:
- Type: PT_LOAD
Sections:
- Section: .foo
- Section: .bar
- Type: PT_LOAD
Sections:
- Section: .bar
- Type: PT_LOAD
FirstSec: .foo
LastSec: .bar
- Type: PT_LOAD
FirstSec: .bar
LastSec: .bar

## Check we can set the "Offset" value explicitly to be less than or equal to
## the offset of a section in the segment.
Expand Down Expand Up @@ -172,10 +169,10 @@ Sections:
Flags: [ SHF_ALLOC ]
Size: 0x1
ProgramHeaders:
- Type: PT_LOAD
Offset: [[OFFSET]]
Sections:
- Section: .foo
- Type: PT_LOAD
Offset: [[OFFSET]]
FirstSec: .foo
LastSec: .foo

## Check we report an error when the "Offset" value is larger than the offset of a section in the segment.
# RUN: not yaml2obj --docnum=3 -DOFFSET=0x79 %s -o /dev/null 2>&1 | \
Expand Down Expand Up @@ -207,7 +204,7 @@ Sections:
## 0xFFFFFF00, but no error is reported.
ShOffset: 0xFFFFFFFF
ProgramHeaders:
- Type: PT_LOAD
Offset: 0xFFFFFF00
Sections:
- Section: .foo
- Type: PT_LOAD
Offset: 0xFFFFFF00
FirstSec: .foo
LastSec: .foo
113 changes: 93 additions & 20 deletions llvm/test/tools/yaml2obj/ELF/program-header.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ Sections:
Content: "00000000"
AddressAlign: 0x0000000000001000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
VAddr: 0xAAAA1000
PAddr: 0xFFFF1000
Sections:
- Section: .text
- Section: .init
- Type: PT_LOAD
Flags: [ PF_R ]
VAddr: 0xAAAA2000
PAddr: 0xFFFF2000
Sections:
- Section: .data
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
VAddr: 0xAAAA1000
PAddr: 0xFFFF1000
FirstSec: .text
LastSec: .init
- Type: PT_LOAD
Flags: [ PF_R ]
VAddr: 0xAAAA2000
PAddr: 0xFFFF2000
FirstSec: .data
LastSec: .data
- Type: PT_GNU_EH_FRAME
- Type: PT_GNU_STACK
- Type: PT_GNU_RELRO
Expand Down Expand Up @@ -82,18 +81,92 @@ ProgramHeaders:
#CHECK-NEXT:]

## Check we do not allow referencing sections that do not exist.
# RUN: not yaml2obj --docnum=2 %s -o %t 2>&1 | FileCheck %s --check-prefix=ERR
# RUN: not yaml2obj -DFIRST=".unknown1" -DLAST=".unknown2" --docnum=2 %s 2>&1 | \
# RUN: FileCheck %s --check-prefix=UNKNOWN-ERR

# ERR: error: unknown section or fill referenced: '.foo' by program header
# ERR: error: unknown section or fill referenced: '.bar' by program header
# UNKNOWN-ERR: error: unknown section or fill referenced: '.unknown1' by the 'FirstSec' key of the program header with index 0
# UNKNOWN-ERR: error: unknown section or fill referenced: '.unknown2' by the 'LastSec' key of the program header with index 0

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Sections:
- Name: .foo
Type: SHT_PROGBITS
Offset: 0x100
Size: 0x10
- Name: .bar
Type: SHT_PROGBITS
Offset: 0x200
Size: 0x20
- Name: .zed
Type: SHT_PROGBITS
Offset: 0x300
Size: 0x30
- Name: .fill1
Type: Fill
Offset: 0x400
Size: 0x40
- Name: .fill2
Type: Fill
Offset: 0x500
Size: 0x50
ProgramHeaders:
- Type: PT_LOAD
Sections:
- Section: .foo
- Section: .bar
- Type: PT_LOAD
FirstSec: [[FIRST=<none>]]
LastSec: [[LAST=<none>]]

## Check we report an error when the index of the section specified by the "FirstSec" key
## is greater than the index of the section specified by the "LastSec" key.

# RUN: not yaml2obj -DFIRST=".bar" -DLAST=".foo" --docnum=2 %s 2>&1 | \
# RUN: FileCheck %s --check-prefix=ORDER-ERR

# ORDER-ERR: error: program header with index 0: the section index of .bar is greater than the index of .foo

## Check that we can't use the "LastSec" key without the "FirstSec" key.

# RUN: not yaml2obj -DLAST=".foo" --docnum=2 %s 2>&1 | \
# RUN: FileCheck %s --check-prefix=ONLY-LAST-ERR

# ONLY-LAST-ERR: error: the "LastSec" key can't be used without the "FirstSec" key

## Check that we can't use the "FirstSec" key without the "LastSec" key.

# RUN: not yaml2obj -DFIRST=".foo" --docnum=2 %s 2>&1 | \
# RUN: FileCheck %s --check-prefix=ONLY-FIRST-ERR

# ONLY-FIRST-ERR: error: the "FirstSec" key can't be used without the "LastSec" key

## Check we create an empty segment when neither "FirstSec" nor "LastSec" are specified.

# RUN: yaml2obj --docnum=2 %s -o %t3
# RUN: llvm-readelf --program-headers %t3 | FileCheck %s --check-prefix=EMPTY-SEGMENT

# EMPTY-SEGMENT: Type Offset VirtAddr PhysAddr FileSiz MemSiz
# EMPTY-SEGMENT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000
# EMPTY-SEGMENT-EMPTY:

## Check that we include all sections between FirstSec and LastSec in the segment when both keys are used.

# RUN: yaml2obj --docnum=2 -DFIRST=".foo" -DLAST=".zed" %s -o %t4
# RUN: llvm-readelf --program-headers %t4 | FileCheck %s --check-prefix=BOTH

# BOTH: Type Offset VirtAddr PhysAddr FileSiz MemSiz
# BOTH-NEXT: LOAD 0x000100 0x0000000000000000 0x0000000000000000 0x000230 0x000230

## Check that we include fills that are between FirstSec and LastSec in the segment when both keys are used.

# RUN: yaml2obj --docnum=2 -DFIRST=".foo" -DLAST=".fill1" %s -o %t5a
# RUN: llvm-readelf --program-headers %t5a | FileCheck %s --check-prefix=FILL1

# FILL1: Type Offset VirtAddr PhysAddr FileSiz MemSiz
# FILL1: LOAD 0x000100 0x0000000000000000 0x0000000000000000 0x000340 0x000340

# RUN: yaml2obj --docnum=2 -DFIRST=".foo" -DLAST=".fill2" %s -o %t5b
# RUN: llvm-readelf --program-headers %t5b | FileCheck %s --check-prefix=FILL2

# FILL2: Type Offset VirtAddr PhysAddr FileSiz MemSiz
# FILL2: LOAD 0x000100 0x0000000000000000 0x0000000000000000 0x000450 0x000450
7 changes: 5 additions & 2 deletions llvm/tools/obj2yaml/elf2yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,11 @@ ELFDumper<ELFT>::dumpProgramHeaders(
// obj2yaml does not create Fill chunks.
for (const std::unique_ptr<ELFYAML::Chunk> &C : Chunks) {
ELFYAML::Section &S = cast<ELFYAML::Section>(*C.get());
if (isInSegment<ELFT>(S, Sections[S.OriginalSecNdx], Phdr))
PH.Sections.push_back({S.Name});
if (isInSegment<ELFT>(S, Sections[S.OriginalSecNdx], Phdr)) {
if (!PH.FirstSec)
PH.FirstSec = S.Name;
PH.LastSec = S.Name;
}
}

Ret.push_back(PH);
Expand Down