[PATCH] D107968: [llvm-readobj] Refactor ELFDumper::printAttributes()

Jozef Lawrynowicz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 16 08:12:58 PDT 2021


jozefl updated this revision to Diff 366636.
jozefl added a comment.
Herald added subscribers: frasercrmck, luismarques, apazos, sameer.abuasal, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, niosHD, sabuasal, johnrusso, rbar, asb, emaste.

Thanks @jhenderson, good point. Fixed in the updated diff.

Also, is it ok to just remove all user-visible warnings when attributes
aren't supported? If you try and dump attributes for for an unsupported
target, nothing will happen, but there is a test for RISCV that checks
for a warning when dumping from a big-endian encoded file.

In my updated diff, I removed these warnings, so we have some consistency.

Thanks,
Jozef


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D107968/new/

https://reviews.llvm.org/D107968

Files:
  llvm/test/tools/llvm-readobj/ELF/RISCV/validate-attr-section.test
  llvm/tools/llvm-readobj/ELFDumper.cpp


Index: llvm/tools/llvm-readobj/ELFDumper.cpp
===================================================================
--- llvm/tools/llvm-readobj/ELFDumper.cpp
+++ llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -339,7 +339,8 @@
     return DynRegionInfo(ObjF, *this, Obj.base() + Offset, Size, EntSize);
   }
 
-  void printAttributes();
+  void printAttributes(unsigned, std::unique_ptr<ELFAttributeParser>,
+                       support::endianness);
   void printMipsReginfo();
   void printMipsOptions();
 
@@ -2557,8 +2558,16 @@
 template <class ELFT> void ELFDumper<ELFT>::printArchSpecificInfo() {
   switch (Obj.getHeader().e_machine) {
   case EM_ARM:
+    if (Obj.isLE())
+      printAttributes(ELF::SHT_ARM_ATTRIBUTES,
+                      std::make_unique<ARMAttributeParser>(&W),
+                      support::little);
+    break;
   case EM_RISCV:
-    printAttributes();
+    if (Obj.isLE())
+      printAttributes(ELF::SHT_RISCV_ATTRIBUTES,
+                      std::make_unique<RISCVAttributeParser>(&W),
+                      support::little);
     break;
   case EM_MIPS: {
     printMipsABIFlags();
@@ -2581,20 +2590,15 @@
   }
 }
 
-template <class ELFT> void ELFDumper<ELFT>::printAttributes() {
-  if (!Obj.isLE()) {
-    W.startLine() << "Attributes not implemented.\n";
-    return;
-  }
-
-  const unsigned Machine = Obj.getHeader().e_machine;
-  assert((Machine == EM_ARM || Machine == EM_RISCV) &&
-         "Attributes not implemented.");
-
+template <class ELFT>
+void ELFDumper<ELFT>::printAttributes(
+    unsigned AttrShType, std::unique_ptr<ELFAttributeParser> AttrParser,
+    support::endianness Endianness) {
+  assert((AttrShType != ELF::SHT_NULL) && AttrParser &&
+         "Incomplete ELF attribute implementation");
   DictScope BA(W, "BuildAttributes");
   for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
-    if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES &&
-        Sec.sh_type != ELF::SHT_RISCV_ATTRIBUTES)
+    if (Sec.sh_type != AttrShType)
       continue;
 
     ArrayRef<uint8_t> Contents;
@@ -2613,13 +2617,7 @@
 
     W.printHex("FormatVersion", Contents[0]);
 
-    auto ParseAttrubutes = [&]() {
-      if (Machine == EM_ARM)
-        return ARMAttributeParser(&W).parse(Contents, support::little);
-      return RISCVAttributeParser(&W).parse(Contents, support::little);
-    };
-
-    if (Error E = ParseAttrubutes())
+    if (Error E = AttrParser->parse(Contents, Endianness))
       reportUniqueWarning("unable to dump attributes from the " +
                           describe(Sec) + ": " + toString(std::move(E)));
   }
Index: llvm/test/tools/llvm-readobj/ELF/RISCV/validate-attr-section.test
===================================================================
--- llvm/test/tools/llvm-readobj/ELF/RISCV/validate-attr-section.test
+++ llvm/test/tools/llvm-readobj/ELF/RISCV/validate-attr-section.test
@@ -3,7 +3,7 @@
 # RUN: yaml2obj %s -o %t.o
 # RUN: llvm-readobj -A %t.o | FileCheck %s
 
-# CHECK: Attributes not implemented.
+# CHECK-NOT: BuildAttributes {
 
 --- !ELF
 FileHeader:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107968.366636.patch
Type: text/x-patch
Size: 3049 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210816/18328080/attachment.bin>


More information about the llvm-commits mailing list