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

Jozef Lawrynowicz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 17 02:12:56 PDT 2021


jozefl updated this revision to Diff 366841.
jozefl marked an inline comment as done.
jozefl added a comment.

Ah yes that makes sense. My mistake was assuming there were other targets with
unimplemented ELF attribute support when in fact, MSP430 is the only one.

Updated the diff with the warnings added back.

I don't see any reference to endinanness in the build attributes section of the
ARM ABI[1], so at the moment it is only coincidental that both ARM and RISCV
don't have attribute support for big-endian machines. This means we need the
duplicated warnings when big-endian mode is used.

Thanks,
Jozef

[1] https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aaelf32/aaelf32.rst#build-attributes


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,20 @@
 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);
+    else
+      W.startLine() << "Attributes not implemented for big-endian modes.\n";
+    break;
   case EM_RISCV:
-    printAttributes();
+    if (Obj.isLE())
+      printAttributes(ELF::SHT_RISCV_ATTRIBUTES,
+                      std::make_unique<RISCVAttributeParser>(&W),
+                      support::little);
+    else
+      W.startLine() << "Attributes not implemented for big-endian modes.\n";
     break;
   case EM_MIPS: {
     printMipsABIFlags();
@@ -2581,20 +2594,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 +2621,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: Attributes not implemented for big-endian modes.
 
 --- !ELF
 FileHeader:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107968.366841.patch
Type: text/x-patch
Size: 3252 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210817/9f7f3a47/attachment.bin>


More information about the llvm-commits mailing list