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

Jozef Lawrynowicz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 12 08:39:41 PDT 2021


jozefl created this revision.
Herald added subscribers: s.egerton, rupprecht, simoncook.
Herald added a reviewer: jhenderson.
jozefl requested review of this revision.
Herald added subscribers: llvm-commits, MaskRay.
Herald added a project: LLVM.

The current implementation of printAttributes makes it fiddly to extend
attribute support for new targets.

By refactoring the code so all target specific variables are
initialized in a switch/case statement, it becomes simpler to extend
attribute support for new targets.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107968

Files:
  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
@@ -2587,14 +2587,27 @@
     return;
   }
 
-  const unsigned Machine = Obj.getHeader().e_machine;
-  assert((Machine == EM_ARM || Machine == EM_RISCV) &&
-         "Attributes not implemented.");
+  auto AttrShType = ELF::SHT_NULL;
+  std::unique_ptr<ELFAttributeParser> AttrParser;
+  switch (Obj.getHeader().e_machine) {
+  case EM_ARM:
+    AttrShType = ELF::SHT_ARM_ATTRIBUTES;
+    AttrParser = std::make_unique<ARMAttributeParser>(&W);
+    break;
+  case EM_RISCV:
+    AttrShType = ELF::SHT_RISCV_ATTRIBUTES;
+    AttrParser = std::make_unique<RISCVAttributeParser>(&W);
+    break;
+  default:
+    llvm_unreachable("ELF attributes not implemented for target");
+    return;
+  }
+  assert((AttrShType != ELF::SHT_NULL && AttrParser != nullptr) &&
+         "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 +2626,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, support::little))
       reportUniqueWarning("unable to dump attributes from the " +
                           describe(Sec) + ": " + toString(std::move(E)));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107968.366004.patch
Type: text/x-patch
Size: 1844 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210812/7995003d/attachment-0001.bin>


More information about the llvm-commits mailing list