Skip to content

Commit

Permalink
[yaml2obj] - Verify that sections are sorted by their file offsets wh…
Browse files Browse the repository at this point in the history
…en creating segments.

This validates that sections listed for a segment in the YAML
declaration are ordered by their file offsets.

It might help to simplify the file size computation, but also
is useful by itself as helps to avoid issues in test cases and
to maintain their readability.

Differential revision: https://reviews.llvm.org/D78361
  • Loading branch information
Georgii Rymar committed Apr 21, 2020
1 parent c3a2929 commit 3471ae9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/ObjectYAML/ELFEmitter.cpp
Expand Up @@ -753,6 +753,11 @@ void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
for (auto &YamlPhdr : Doc.ProgramHeaders) {
Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
if (!llvm::is_sorted(Fragments, [](const Fragment &A, const Fragment &B) {
return A.Offset < B.Offset;
}))
reportError("sections in the program header with index " +
Twine(PhdrIdx) + " are not sorted by their file offset");

if (YamlPhdr.Offset) {
PHeader.p_offset = *YamlPhdr.Offset;
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml
Expand Up @@ -395,8 +395,6 @@ ProgramHeaders:
- Section: .note.ABI-tag
- Section: .note.gnu.build-id
- Section: .gnu.hash
- Section: .dynsym
- Section: .dynstr
- Section: .gnu.version
- Section: .gnu.version_r
- Section: .rela.dyn
Expand All @@ -409,6 +407,8 @@ ProgramHeaders:
- Section: .rodata
- Section: .eh_frame_hdr
- Section: .eh_frame
- Section: .dynsym
- Section: .dynstr
Symbols:
- Name: .interp
Type: STT_SECTION
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-readobj/ELF/demangle.test
Expand Up @@ -213,8 +213,8 @@ ProgramHeaders:
Flags: [ PF_R, PF_X ]
VAddr: 0x0
Sections:
- Section: .dynsym
- Section: .dynstr
- Section: .dynsym
- Section: .rela.dyn
- Section: .dynamic
- Section: .text.foo
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
Expand Up @@ -298,8 +298,8 @@ ProgramHeaders:
Flags: [ PF_R, PF_X ]
VAddr: 0x0
Sections:
- Section: .dynsym
- Section: .dynstr
- Section: .dynsym
- Section: .dynamic
- Section: .text.foo
- Type: PT_DYNAMIC
Expand Down
Expand Up @@ -65,8 +65,8 @@ ProgramHeaders:
- Type: PT_LOAD
VAddr: 0x1000
Sections:
- Section: .rela.dyn
- Section: .dynamic
- Section: .rela.dyn
- Type: PT_DYNAMIC
VAddr: 0x1000
Sections:
Expand Down
Expand Up @@ -101,8 +101,8 @@ ProgramHeaders:
- Type: PT_LOAD
VAddr: 0x1000
Sections:
- Section: .rela.dyn
- Section: .dynamic
- Section: .rela.dyn
- Type: PT_DYNAMIC
VAddr: 0x1000
Sections:
Expand Down
57 changes: 57 additions & 0 deletions llvm/test/tools/obj2yaml/program-headers.yaml
Expand Up @@ -517,3 +517,60 @@ Sections:
## Use an arbitrary size that is different to the size of
## the previous section.
Size: 0x20

## Check that we require sections in a program header
## declaration to be sorted by their offsets.
# RUN: not yaml2obj --docnum=6 %s -o %t6 2>&1 | \
# RUN: FileCheck %s --check-prefix=UNSORTED --implicit-check-not="error:"

# UNSORTED: error: sections in the program header with index 1 are not sorted by their file offset
# UNSORTED-NEXT: error: sections in the program header with index 3 are not sorted by their file offset

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
ProgramHeaders:
## Case 1: the .bar section is placed after the .foo section in the file.
## Check we report an error about the violation of the order.
- Type: PT_LOAD
Flags: [ PF_R ]
Sections:
- Section: .bar
- Section: .foo
VAddr: 0x1000
## There is nothing wrong with this segment. We have it to show that
## we report correct program header indices in error messages.
- Type: PT_LOAD
Flags: [ PF_R ]
Sections:
- Section: .foo
- Section: .bar
VAddr: 0x1000
## Case 2: the .bar section is placed before the .zed section in the file,
## but the sh_offset of .zed is less than the sh_offset of
## the .bar section because of the "ShOffset" property.
## Document we report an error for such a case.
- Type: PT_LOAD
Flags: [ PF_R ]
Sections:
- Section: .bar
- Section: .zed
VAddr: 0x1001
Sections:
- Name: .foo
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Size: 0x1
Address: 0x1000
- Name: .bar
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Size: 0x1
- Name: .zed
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Size: 0x1
ShOffset: 0x0

0 comments on commit 3471ae9

Please sign in to comment.