[llvm] r365762 - [llvm-readobj/llvm-readelf] - Report a warning instead of a error when dumping a broken dynamic section.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 05:26:48 PDT 2019


Author: grimar
Date: Thu Jul 11 05:26:48 2019
New Revision: 365762

URL: http://llvm.org/viewvc/llvm-project?rev=365762&view=rev
Log:
[llvm-readobj/llvm-readelf] - Report a warning instead of a error when dumping a broken dynamic section.

It does not make sence to stop dumping the object if the broken
dynamic section was found. In this patch I changed the behavior from
"report an error" to "report a warning". This matches GNU.

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

Modified:
    llvm/trunk/test/Object/invalid.test
    llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test
    llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
    llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp

Modified: llvm/trunk/test/Object/invalid.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/invalid.test?rev=365762&r1=365761&r2=365762&view=diff
==============================================================================
--- llvm/trunk/test/Object/invalid.test (original)
+++ llvm/trunk/test/Object/invalid.test Thu Jul 11 05:26:48 2019
@@ -126,12 +126,12 @@ Sections:
 Symbols:
   - Name: foo
 
-## Check that llvm-readobj reports an error if .dynsym has an invalid sh_entsize.
+## Check that llvm-readobj reports a warning if .dynsym has an invalid sh_entsize.
 
 # RUN: yaml2obj %s --docnum=7 -o %t7
-# RUN: not llvm-readobj --dyn-symbols %t7 2>&1 | FileCheck --check-prefix=INVALID-DYNSYM-SIZE %s
+# RUN: llvm-readobj --dyn-symbols %t7 2>&1 | FileCheck --check-prefix=INVALID-DYNSYM-SIZE %s
 
-# INVALID-DYNSYM-SIZE: error: Invalid entity size
+# INVALID-DYNSYM-SIZE: warning: invalid section size (48) or entity size (32)
 
 --- !ELF
 FileHeader:
@@ -458,22 +458,22 @@ ProgramHeaders:
     Sections:
       - Section: .dynamic
 
-## Check that llvm-readobj reports an error when a dynamic relocation section
+## Check that llvm-readobj reports a warning when a dynamic relocation section
 ## has sh_entsize field with size != sizeof(Elf_Rela).
 
-# RUN: not llvm-readobj --dyn-relocations \
+# RUN: llvm-readobj --dyn-relocations \
 # RUN:   %p/Inputs/corrupt-invalid-relocation-size.elf.x86-64 2>&1 \
 # RUN:    | FileCheck --check-prefix=RELOC-BROKEN-ENTSIZE %s
 
-# RELOC-BROKEN-ENTSIZE: error: Invalid entity size
+# RELOC-BROKEN-ENTSIZE: warning: invalid section size (24) or entity size (25)
 
-## Check that llvm-readobj reports an error when .dynamic section has an invalid
+## Check that llvm-readobj reports a warning when .dynamic section has an invalid
 ## size, which isn't a multiple of the dynamic entry size. 
 
 # RUN: yaml2obj %s --docnum=22 -o %t22
-# RUN: not llvm-readobj --dyn-relocations %t22 2>&1 | FileCheck --check-prefix=DYN-TABLE-SIZE %s
+# RUN: llvm-readobj --dyn-relocations %t22 2>&1 | FileCheck --check-prefix=DYN-TABLE-SIZE %s
 
-# DYN-TABLE-SIZE: error: Invalid entity size
+# DYN-TABLE-SIZE: warning: invalid section size (1) or entity size (16)
 
 --- !ELF
 FileHeader:

Modified: llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test?rev=365762&r1=365761&r2=365762&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test (original)
+++ llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test Thu Jul 11 05:26:48 2019
@@ -1,9 +1,32 @@
-# Test handling of a dynamic section size which is not a multiple of its entry size.
-# RUN: yaml2obj %s --docnum=1 -o %t.bad-size
-# RUN: not llvm-readobj --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE
-# RUN: not llvm-readelf --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE
+## Test handling of a dynamic section size which is not a multiple of its entry size.
+## Test the full output to demonstrate how we print the warnings.
 
-# ERR-SIZE: error: Invalid entity size
+# RUN: yaml2obj %s --docnum=1 -o %t.bad-size
+# RUN: llvm-readobj --all %t.bad-size 2>&1 \
+# RUN:   | FileCheck %s --implicit-check-not=warning --check-prefix WARN
+# RUN: llvm-readelf --all %t.bad-size 2>&1 \
+# RUN:   | FileCheck %s --implicit-check-not=warning --check-prefix WARN-GNU
+
+# WARN-NOT: warning
+# WARN: warning: invalid section size (4) or entity size (16)
+# WARN-EMPTY:
+# WARN-NEXT: File:
+# WARN:      Symbols [
+# WARN:      ]
+# WARN-EMPTY:
+## A warning is printed at the place where a normal dynamic table should be. 
+# WARN-NEXT: warning: invalid section size (4) or entity size (16)
+# WARN-NEXT: ProgramHeaders [
+
+# WARN-GNU-NOT:  warning
+# WARN-GNU:      warning: invalid section size (4) or entity size (16)
+# WARN-GNU-NEXT: ELF Header:
+# WARN-GNU:      Symbol table '.symtab' contains 1 entries:
+# WARN-GNU:        0:
+# WARN-GNU-EMPTY:
+## A warning is printed at the place where a normal dynamic table should be. 
+# WARN-GNU:      warning: invalid section size (4) or entity size (16)
+# WARN-GNU-EMPTY:
 
 --- !ELF
 FileHeader:
@@ -26,7 +49,7 @@ ProgramHeaders:
     Sections:
       - Section: .dynamic
 
-# Test handling of a .dynamic section with an invalid entsize (i.e. not 2 * sizeof(Elf_Dyn)).
+## Test handling of a .dynamic section with an invalid entsize (i.e. not 2 * sizeof(Elf_Dyn)).
 # RUN: yaml2obj %s --docnum=2 -o %t.bad-entsize
 # RUN: llvm-readobj --dynamic-table %t.bad-entsize | FileCheck %s --check-prefix BAD-ENTSIZE-LLVM
 # RUN: llvm-readelf --dynamic-table %t.bad-entsize | FileCheck %s --check-prefix BAD-ENTSIZE-GNU
@@ -68,7 +91,7 @@ ProgramHeaders:
     Sections:
       - Section: .dynamic
 
-# Test handling of string references pointing past the end of the dynamic string table.
+## Test handling of string references pointing past the end of the dynamic string table.
 # RUN: yaml2obj %s --docnum=3 -o %t.bad-string
 # RUN: llvm-readobj --dynamic-table %t.bad-string | FileCheck %s --check-prefix BAD-STRING-LLVM
 # RUN: llvm-readelf --dynamic-table %t.bad-string | FileCheck %s --check-prefix BAD-STRING-GNU
@@ -136,7 +159,7 @@ ProgramHeaders:
     Sections:
       - Section: .dynamic
 
-# Test handling of DT_STRTAB pointing outside the file's address space.
+## Test handling of DT_STRTAB pointing outside the file's address space.
 # RUN: yaml2obj %s --docnum=4 -o %t.bad-strtab
 
 # RUN: llvm-readobj --dynamic-table %t.bad-strtab 2>&1 >/dev/null | FileCheck %s --check-prefix BAD-STRTAB-ERR
@@ -181,7 +204,7 @@ ProgramHeaders:
     Sections:
       - Section: .dynamic
 
-# Test handling of other d_ptr tags pointing outside the file's address space.
+## Test handling of other d_ptr tags pointing outside the file's address space.
 # RUN: yaml2obj %s --docnum=5 -o %t.bad-rela
 # RUN: llvm-readobj --dynamic-table %t.bad-rela 2>&1 | FileCheck %s --check-prefixes=CHECK,BAD-RELA
 # RUN: llvm-readelf --dynamic-table %t.bad-rela 2>&1 | FileCheck %s --check-prefixes=CHECK,BAD-RELA-GNU

Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=365762&r1=365761&r2=365762&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Thu Jul 11 05:26:48 2019
@@ -134,8 +134,12 @@ struct DynRegionInfo {
     const Type *Start = reinterpret_cast<const Type *>(Addr);
     if (!Start)
       return {Start, Start};
-    if (EntSize != sizeof(Type) || Size % EntSize)
-      reportError("Invalid entity size");
+    if (EntSize != sizeof(Type) || Size % EntSize) {
+      // TODO: Add a section index to this warning.
+      reportWarning("invalid section size (" + Twine(Size) +
+                    ") or entity size (" + Twine(EntSize) + ")");
+      return {Start, Start};
+    }
     return {Start, Start + (Size / EntSize)};
   }
 };

Modified: llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp?rev=365762&r1=365761&r2=365762&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp Thu Jul 11 05:26:48 2019
@@ -377,6 +377,7 @@ LLVM_ATTRIBUTE_NORETURN void reportError
 }
 
 void reportWarning(Twine Msg) {
+  fouts().flush();
   errs() << "\n";
   WithColor::warning(errs()) << Msg << "\n";
 }




More information about the llvm-commits mailing list