[llvm] c990455 - [llvm-readelf/obj] - Refine the implementation of printMipsReginfo().

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 9 01:19:32 PDT 2020


Author: Georgii Rymar
Date: 2020-08-09T11:10:12+03:00
New Revision: c990455db91e52bd6b8ac9b68b06ef19f7239722

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

LOG: [llvm-readelf/obj] - Refine the implementation of printMipsReginfo().

It adds the proper warnings reporting and updates the mips-reginfo.test to
remove using of the precompiled binary.

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

Added: 
    

Modified: 
    llvm/test/tools/llvm-readobj/ELF/mips-reginfo.test
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    llvm/test/tools/llvm-readobj/ELF/Inputs/reginfo.obj.elf-mipsel


################################################################################
diff  --git a/llvm/test/tools/llvm-readobj/ELF/Inputs/reginfo.obj.elf-mipsel b/llvm/test/tools/llvm-readobj/ELF/Inputs/reginfo.obj.elf-mipsel
deleted file mode 100644
index 5cd09809f8bb..000000000000
Binary files a/llvm/test/tools/llvm-readobj/ELF/Inputs/reginfo.obj.elf-mipsel and /dev/null 
diff er

diff  --git a/llvm/test/tools/llvm-readobj/ELF/mips-reginfo.test b/llvm/test/tools/llvm-readobj/ELF/mips-reginfo.test
index 9f200e9d9525..6e8310fb07f8 100644
--- a/llvm/test/tools/llvm-readobj/ELF/mips-reginfo.test
+++ b/llvm/test/tools/llvm-readobj/ELF/mips-reginfo.test
@@ -1,18 +1,37 @@
 ## Check that we are able to dump the SHT_MIPS_REGINFO section using -A properly.
 
-# RUN: llvm-readobj -A %p/Inputs/reginfo.obj.elf-mipsel | FileCheck %s
+# RUN: yaml2obj --docnum=1 %s -o %t1
+# RUN: llvm-readobj -A %t1 | FileCheck %s --check-prefix=OPTIONS
+# RUN: llvm-readelf -A %t1 | FileCheck %s --check-prefix=OPTIONS
 
-# CHECK:      MIPS RegInfo {
-# CHECK-NEXT:   GP: 0x7FEF
-# CHECK-NEXT:   General Mask: 0xB00001F6
-# CHECK-NEXT:   Co-Proc Mask0: 0x0
-# CHECK-NEXT:   Co-Proc Mask1: 0x0
-# CHECK-NEXT:   Co-Proc Mask2: 0x0
-# CHECK-NEXT:   Co-Proc Mask3: 0x0
-# CHECK-NEXT: }
+# OPTIONS:      MIPS RegInfo {
+# OPTIONS-NEXT:   GP:            0x807060504030201
+# OPTIONS-NEXT:   General Mask:  0xD0C0B0A
+# OPTIONS-NEXT:   Co-Proc Mask0: 0x88776655
+# OPTIONS-NEXT:   Co-Proc Mask1: 0xCCBBAA99
+# OPTIONS-NEXT:   Co-Proc Mask2: 0x1EFFEEDD
+# OPTIONS-NEXT:   Co-Proc Mask3: 0x5E4E3E2E
+# OPTIONS-NEXT: }
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_MIPS
+Sections:
+  - Name: .reginfo
+    Type: SHT_MIPS_REGINFO
+    ContentArray: [ 0xA, 0xB, 0xC, 0xD,     ## Bit-mask of used general registers.
+                    0x11, 0x22, 0x33, 0x44, ## Unused padding field.
+                    0x55, 0x66, 0x77, 0x88, ## Bit-mask of used co-processor registers (0).
+                    0x99, 0xAA, 0xBB, 0xCC, ## Bit-mask of used co-processor registers (1).
+                    0xDD, 0xEE, 0xFF, 0x1E, ## Bit-mask of used co-processor registers (2).
+                    0x2E, 0x3E, 0x4E, 0x5E, ## Bit-mask of used co-processor registers (3).
+                    0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 ] ## GP register value.
 
 ## Check that we try to dump the .reginfo section when we are able to locate it by name.
-# RUN: yaml2obj --docnum=1 -DNAME=0xffff %s -o %t.err1
+# RUN: yaml2obj --docnum=2 -DNAME=0xffff %s -o %t.err1
 # RUN: llvm-readelf -A %t.err1 2>&1 | \
 # RUN:   FileCheck %s -DFILE=%t.err1 --check-prefix=NAME-ERR-FOUND --implicit-check-not=warning:
 # RUN: llvm-readobj -A %t.err1 2>&1 | \
@@ -20,7 +39,7 @@
 
 # NAME-ERR-FOUND:      warning: '[[FILE]]': unable to read the name of SHT_PROGBITS section with index 1: a section [index 1] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table
 # NAME-ERR-FOUND-NEXT: warning: '[[FILE]]': unable to read the name of SHT_PROGBITS section with index 3: a section [index 3] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table
-# NAME-ERR-FOUND:      The .reginfo section has a wrong size.
+# NAME-ERR-FOUND:      warning: '[[FILE]]': the .reginfo section has an invalid size (0x0)
 
 --- !ELF
 FileHeader:
@@ -38,7 +57,7 @@ Sections:
     ShName: [[NAME=<none>]]
 
 ## Check we report a warning when we are unable to find the .reginfo section due to an error.
-# RUN: yaml2obj --docnum=1 -DREGINFONAME=0xffff %s -o %t.err2
+# RUN: yaml2obj --docnum=2 -DREGINFONAME=0xffff %s -o %t.err2
 # RUN: llvm-readelf -A %t.err2 2>&1 | \
 # RUN:   FileCheck %s -DFILE=%t.err2 --check-prefix=NAME-ERR-NOTFOUND --implicit-check-not=warning:
 # RUN: llvm-readobj -A %t.err2 2>&1 | \

diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index aca33858c6cf..e5a591d31514 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -3328,23 +3328,32 @@ static void printMipsReginfoData(ScopedPrinter &W,
 }
 
 template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
-  const ELFFile<ELFT> *Obj = ObjF->getELFFile();
-  const Elf_Shdr *RegInfo = findSectionByName(".reginfo");
-  if (!RegInfo) {
+  const Elf_Shdr *RegInfoSec = findSectionByName(".reginfo");
+  if (!RegInfoSec) {
     W.startLine() << "There is no .reginfo section in the file.\n";
     return;
   }
 
-  ArrayRef<uint8_t> Sec = unwrapOrError(ObjF->getFileName(),
-                                        Obj->getSectionContents(RegInfo));
-  if (Sec.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
-    W.startLine() << "The .reginfo section has a wrong size.\n";
+  const ELFFile<ELFT> *Obj = ObjF->getELFFile();
+  Expected<ArrayRef<uint8_t>> ContentsOrErr =
+      Obj->getSectionContents(RegInfoSec);
+  if (!ContentsOrErr) {
+    this->reportUniqueWarning(createError(
+        "unable to read the content of the .reginfo section (" +
+        describe(*RegInfoSec) + "): " + toString(ContentsOrErr.takeError())));
+    return;
+  }
+
+  if (ContentsOrErr->size() < sizeof(Elf_Mips_RegInfo<ELFT>)) {
+    this->reportUniqueWarning(
+        createError("the .reginfo section has an invalid size (0x" +
+                    Twine::utohexstr(ContentsOrErr->size()) + ")"));
     return;
   }
 
   DictScope GS(W, "MIPS RegInfo");
-  auto *Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(Sec.data());
-  printMipsReginfoData(W, *Reginfo);
+  printMipsReginfoData(W, *reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(
+                              ContentsOrErr->data()));
 }
 
 template <class ELFT>


        


More information about the llvm-commits mailing list