[llvm] 84a1bc7 - [test/Object][llvm-objdump] - llvm-objdump: don't abort() when the e_phoff field is invalid and refine testing.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 04:57:57 PDT 2020


Author: Georgii Rymar
Date: 2020-07-14T14:45:18+03:00
New Revision: 84a1bc7f2c0c7bd5f18a4ecaf91e27644aa94190

URL: https://github.com/llvm/llvm-project/commit/84a1bc7f2c0c7bd5f18a4ecaf91e27644aa94190
DIFF: https://github.com/llvm/llvm-project/commit/84a1bc7f2c0c7bd5f18a4ecaf91e27644aa94190.diff

LOG: [test/Object][llvm-objdump] - llvm-objdump: don't abort() when the e_phoff field is invalid and refine testing.

llvm-objdump currently calls report_fatal_error() when the e_phoff field is invalid.

This is tested by elf-invalid-phdr.test which has the following issues:
1) It uses a precompiled object.
2) it could be a part of invalid.test.
3) It tests the Object lib, but we have no separate test for llvm-objdump.

This patch addresses issues mentioned.

Differential revision: https://reviews.llvm.org/D83559

Added: 
    llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test

Modified: 
    llvm/test/Object/invalid.test
    llvm/tools/llvm-objdump/ELFDump.cpp

Removed: 
    llvm/test/Object/Inputs/invalid-phdr.elf
    llvm/test/Object/elf-invalid-phdr.test


################################################################################
diff  --git a/llvm/test/Object/Inputs/invalid-phdr.elf b/llvm/test/Object/Inputs/invalid-phdr.elf
deleted file mode 100644
index 8a5cc53cc94b..000000000000
Binary files a/llvm/test/Object/Inputs/invalid-phdr.elf and /dev/null 
diff er

diff  --git a/llvm/test/Object/elf-invalid-phdr.test b/llvm/test/Object/elf-invalid-phdr.test
deleted file mode 100644
index 1b47f8d66cc4..000000000000
--- a/llvm/test/Object/elf-invalid-phdr.test
+++ /dev/null
@@ -1,26 +0,0 @@
-# invalid-phdr.elf is generated by creating a simple elf file with yaml2obj:
-# !ELF
-# FileHeader:
-#   Class:           ELFCLASS64
-#   Data:            ELFDATA2LSB
-#   Type:            ET_EXEC
-#   Machine:         EM_X86_64
-# Sections:
-#   - Name:            .text
-#     Type:            SHT_PROGBITS
-#     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
-#     AddressAlign:    0x0000000000001000
-#     Content:         "00000000"
-# ProgramHeaders:
-#   - Type: PT_LOAD
-#     Flags: [ PF_X, PF_R ]
-#     VAddr: 0xAAAA1000
-#     PAddr: 0xFFFF1000
-#     Sections:
-#       - Section: .text
-#
-# Then editing the e_phoff in with a hexeditor to set it to 0xffffff
-RUN: not --crash llvm-objdump --private-headers %p/Inputs/invalid-phdr.elf 2>&1 \
-RUN:         | FileCheck %s
-
-CHECK: LLVM ERROR: program headers are longer than binary of size 4162: e_phoff = 0xffffff, e_phnum = 1, e_phentsize = 56

diff  --git a/llvm/test/Object/invalid.test b/llvm/test/Object/invalid.test
index 475143102872..499200bbc8a7 100644
--- a/llvm/test/Object/invalid.test
+++ b/llvm/test/Object/invalid.test
@@ -650,3 +650,17 @@ FileHeader:
 Sections:
   - Type: SHT_NULL
     Link: 0xff
+
+## Check the case when the e_phoff field is invalid.
+# RUN: yaml2obj --docnum=31 %s -o %t31
+# RUN: not llvm-objdump --private-headers %t31 2>&1 | FileCheck -DFILE=%t31 %s --check-prefix=INVALID-PHOFF
+
+# INVALID-PHOFF: error: '[[FILE]]': program headers are longer than binary of size 280: e_phoff = 0xffffff, e_phnum = 0, e_phentsize = 0
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+  EPhOff:  0xffffff

diff  --git a/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test b/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test
new file mode 100644
index 000000000000..94de7eacc3ec
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test
@@ -0,0 +1,16 @@
+## Test how we handle the case when the e_phoff field is invalid.
+# RUN: yaml2obj %s -o %t
+# RUN: not llvm-objdump --private-headers %t 2>&1 | \
+# RUN:   FileCheck -DFILE=%t %s --check-prefix=INVALID-PHOFF
+
+# INVALID-PHOFF:      Program Header:
+# INVALID-PHOFF-NEXT: warning: '[[FILE]]': unable to read program headers: program headers are longer than binary of size 280: e_phoff = 0xffffff, e_phnum = 0, e_phentsize = 0
+# INVALID-PHOFF-NEXT: error: '[[FILE]]': program headers are longer than binary of size 280: e_phoff = 0xffffff, e_phnum = 0, e_phentsize = 0
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+  EPhOff:  0xffffff

diff  --git a/llvm/tools/llvm-objdump/ELFDump.cpp b/llvm/tools/llvm-objdump/ELFDump.cpp
index d9b1bad52183..602bc6388252 100644
--- a/llvm/tools/llvm-objdump/ELFDump.cpp
+++ b/llvm/tools/llvm-objdump/ELFDump.cpp
@@ -198,11 +198,17 @@ static void printDynamicSection(const ELFFile<ELFT> *Elf, StringRef Filename) {
   }
 }
 
-template <class ELFT> static void printProgramHeaders(const ELFFile<ELFT> *o) {
+template <class ELFT>
+static void printProgramHeaders(const ELFFile<ELFT> *Obj, StringRef FileName) {
   outs() << "Program Header:\n";
-  auto ProgramHeaderOrError = o->program_headers();
-  if (!ProgramHeaderOrError)
-    report_fatal_error(toString(ProgramHeaderOrError.takeError()));
+  auto ProgramHeaderOrError = Obj->program_headers();
+  if (!ProgramHeaderOrError) {
+    reportWarning("unable to read program headers: " +
+                      toString(ProgramHeaderOrError.takeError()),
+                  FileName);
+    return;
+  }
+
   for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) {
     switch (Phdr.p_type) {
     case ELF::PT_DYNAMIC:
@@ -346,13 +352,13 @@ static void printSymbolVersionInfo(const ELFFile<ELFT> *Elf,
 
 void objdump::printELFFileHeader(const object::ObjectFile *Obj) {
   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
-    printProgramHeaders(ELFObj->getELFFile());
+    printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
-    printProgramHeaders(ELFObj->getELFFile());
+    printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
-    printProgramHeaders(ELFObj->getELFFile());
+    printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
   else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
-    printProgramHeaders(ELFObj->getELFFile());
+    printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
 }
 
 void objdump::printELFDynamicSection(const object::ObjectFile *Obj) {


        


More information about the llvm-commits mailing list