[llvm] 3471ae9 - [yaml2obj] - Verify that sections are sorted by their file offsets when creating segments.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 21 05:50:57 PDT 2020


Author: Georgii Rymar
Date: 2020-04-21T15:50:42+03:00
New Revision: 3471ae9dadab14a0eafeb855dfd071052813c15a

URL: https://github.com/llvm/llvm-project/commit/3471ae9dadab14a0eafeb855dfd071052813c15a
DIFF: https://github.com/llvm/llvm-project/commit/3471ae9dadab14a0eafeb855dfd071052813c15a.diff

LOG: [yaml2obj] - Verify that sections are sorted by their file offsets when 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

Added: 
    

Modified: 
    llvm/lib/ObjectYAML/ELFEmitter.cpp
    llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml
    llvm/test/tools/llvm-readobj/ELF/demangle.test
    llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
    llvm/test/tools/llvm-readobj/ELF/reloc-negative-addend-no-sym.test
    llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
    llvm/test/tools/obj2yaml/program-headers.yaml

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 71e21b90344d..3a673cf67e87 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -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;

diff  --git a/llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml b/llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml
index 0fb56a5b2e01..bf964d9bf348 100644
--- a/llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml
+++ b/llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml
@@ -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
@@ -409,6 +407,8 @@ ProgramHeaders:
       - Section: .rodata
       - Section: .eh_frame_hdr
       - Section: .eh_frame
+      - Section: .dynsym
+      - Section: .dynstr
 Symbols:
   - Name:            .interp
     Type:            STT_SECTION

diff  --git a/llvm/test/tools/llvm-readobj/ELF/demangle.test b/llvm/test/tools/llvm-readobj/ELF/demangle.test
index 46ea48f147c9..3a9b11a88efd 100644
--- a/llvm/test/tools/llvm-readobj/ELF/demangle.test
+++ b/llvm/test/tools/llvm-readobj/ELF/demangle.test
@@ -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

diff  --git a/llvm/test/tools/llvm-readobj/ELF/hash-symbols.test b/llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
index 40e680e3b751..dd0d9e398174 100644
--- a/llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
+++ b/llvm/test/tools/llvm-readobj/ELF/hash-symbols.test
@@ -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

diff  --git a/llvm/test/tools/llvm-readobj/ELF/reloc-negative-addend-no-sym.test b/llvm/test/tools/llvm-readobj/ELF/reloc-negative-addend-no-sym.test
index 3a318d52175f..58bc1f35c243 100644
--- a/llvm/test/tools/llvm-readobj/ELF/reloc-negative-addend-no-sym.test
+++ b/llvm/test/tools/llvm-readobj/ELF/reloc-negative-addend-no-sym.test
@@ -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:

diff  --git a/llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test b/llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
index c6481badc14c..2a401f86c506 100644
--- a/llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
+++ b/llvm/test/tools/llvm-readobj/ELF/reloc-zero-name-or-value.test
@@ -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:

diff  --git a/llvm/test/tools/obj2yaml/program-headers.yaml b/llvm/test/tools/obj2yaml/program-headers.yaml
index 22b86865a90e..5d56487f8c0c 100644
--- a/llvm/test/tools/obj2yaml/program-headers.yaml
+++ b/llvm/test/tools/obj2yaml/program-headers.yaml
@@ -517,3 +517,60 @@ Sections:
 ## Use an arbitrary size that is 
diff erent 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


        


More information about the llvm-commits mailing list