[llvm] r258845 - [llvm-readobj] Add -elf-section-groups option

Hemant Kulkarni via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 26 11:46:40 PST 2016


Author: khemant
Date: Tue Jan 26 13:46:39 2016
New Revision: 258845

URL: http://llvm.org/viewvc/llvm-project?rev=258845&view=rev
Log:
[llvm-readobj] Add -elf-section-groups option

Adds a way to inspect SHT_GROUP sections in ELF objects.
Displays signature, member sections of these sections.

Differential revision: http://reviews.llvm.org/D16555

Added:
    llvm/trunk/test/tools/llvm-readobj/Inputs/elf-groups.x86_64
    llvm/trunk/test/tools/llvm-readobj/elf-groups.test
Modified:
    llvm/trunk/docs/CommandGuide/llvm-readobj.rst
    llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
    llvm/trunk/tools/llvm-readobj/ObjDumper.h
    llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp

Modified: llvm/trunk/docs/CommandGuide/llvm-readobj.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/CommandGuide/llvm-readobj.rst?rev=258845&r1=258844&r2=258845&view=diff
==============================================================================
--- llvm/trunk/docs/CommandGuide/llvm-readobj.rst (original)
+++ llvm/trunk/docs/CommandGuide/llvm-readobj.rst Tue Jan 26 13:46:39 2016
@@ -80,6 +80,10 @@ input. Otherwise, it will read from the
 
  Display the ELF program headers (only for ELF object files).
 
+.. option:: -elf-section-groups, -g
+
+ Display section groups (only for ELF object files).
+
 EXIT STATUS
 -----------
 

Added: llvm/trunk/test/tools/llvm-readobj/Inputs/elf-groups.x86_64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/Inputs/elf-groups.x86_64?rev=258845&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-readobj/Inputs/elf-groups.x86_64 (added) and llvm/trunk/test/tools/llvm-readobj/Inputs/elf-groups.x86_64 Tue Jan 26 13:46:39 2016 differ

Added: llvm/trunk/test/tools/llvm-readobj/elf-groups.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/elf-groups.test?rev=258845&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/elf-groups.test (added)
+++ llvm/trunk/test/tools/llvm-readobj/elf-groups.test Tue Jan 26 13:46:39 2016
@@ -0,0 +1,39 @@
+# Source file compiled:
+##include <iostream>
+# #include <typeinfo>
+#template <class A>
+#void foo(A variable) {
+#  std::cout << "foo is " << typeid(A).name() << "\n";
+#}
+#int main () {
+#  char x = 'x';
+#  int y = 1;
+#  foo(x);
+#  foo(y);
+#  return 0;
+#}
+
+RUN: llvm-readobj -g %p/Inputs/elf-groups.x86_64 \
+RUN:   | FileCheck %s
+CHECK:         Groups {
+CHECK-NEXT:      Group {
+CHECK-NEXT:        Name: .group (92)
+CHECK-NEXT:        Index: 1
+CHECK-NEXT:        Type: COMDAT (0x1)
+CHECK-NEXT:        Signature: _Z3fooIcEvT_
+CHECK-NEXT:        Section(s) in group [
+CHECK-NEXT:          .text._Z3fooIcEvT_ (10)
+CHECK-NEXT:          .rela.text._Z3fooIcEvT_ (11)
+CHECK-NEXT:        ]
+CHECK-NEXT:      }
+CHECK-NEXT:      Group {
+CHECK-NEXT:        Name: .group (92)
+CHECK-NEXT:        Index: 2
+CHECK-NEXT:        Type: COMDAT (0x1)
+CHECK-NEXT:        Signature: _Z3fooIiEvT_
+CHECK-NEXT:        Section(s) in group [
+CHECK-NEXT:          .text._Z3fooIiEvT_ (12)
+CHECK-NEXT:          .rela.text._Z3fooIiEvT_ (13)
+CHECK-NEXT:        ]
+CHECK-NEXT:      }
+

Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=258845&r1=258844&r2=258845&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Tue Jan 26 13:46:39 2016
@@ -59,6 +59,7 @@ public:
   void printGnuHashTable() override;
   void printLoadName() override;
   void printVersionInfo() override;
+  void printGroupSections() override;
 
   void printAttributes() override;
   void printMipsPLTGOT() override;
@@ -792,6 +793,13 @@ static const char *getElfSectionType(uns
   }
 }
 
+static const char *getGroupType(uint32_t Flag) {
+  if (Flag & ELF::GRP_COMDAT)
+    return "COMDAT";
+  else
+    return "(unknown)";
+}
+
 static const EnumEntry<unsigned> ElfSectionFlags[] = {
   LLVM_READOBJ_ENUM_ENT(ELF, SHF_WRITE           ),
   LLVM_READOBJ_ENUM_ENT(ELF, SHF_ALLOC           ),
@@ -2250,3 +2258,41 @@ template <class ELFT> void ELFDumper<ELF
               llvm::outs(),
               StackMapV1Parser<ELFT::TargetEndianness>(*StackMapContentsArray));
 }
+template <class ELFT> void ELFDumper<ELFT>::printGroupSections() {
+  DictScope Lists(W, "Groups");
+  uint32_t SectionIndex = 0;
+  bool HasGroups = false;
+  for (const Elf_Shdr &Sec : Obj->sections()) {
+    if (Sec.sh_type == ELF::SHT_GROUP) {
+      HasGroups = true;
+      ErrorOr<const Elf_Shdr *> Symtab =
+          errorOrDefault(Obj->getSection(Sec.sh_link));
+      ErrorOr<StringRef> StrTableOrErr = Obj->getStringTableForSymtab(**Symtab);
+      error(StrTableOrErr.getError());
+      StringRef StrTable = *StrTableOrErr;
+      const Elf_Sym *Sym =
+          Obj->template getEntry<Elf_Sym>(*Symtab, Sec.sh_info);
+      auto Data = errorOrDefault(
+          Obj->template getSectionContentsAsArray<Elf32_Word>(&Sec));
+      DictScope D(W, "Group");
+      StringRef Name = errorOrDefault(Obj->getSectionName(&Sec));
+      W.printNumber("Name", Name, Sec.sh_name);
+      W.printNumber("Index", SectionIndex);
+      W.printHex("Type", getGroupType(Data[0]), Data[0]);
+      W.startLine() << "Signature: " << StrTable.data() + Sym->st_name << "\n";
+      {
+        ListScope L(W, "Section(s) in group");
+        int Member = 1;
+        while (Member < Data.size()) {
+          auto Sec = errorOrDefault(Obj->getSection(Data[Member]));
+          const StringRef Name = errorOrDefault(Obj->getSectionName(Sec));
+          W.startLine() << Name << " (" << std::to_string(Data[Member++])
+                        << ")\n";
+        }
+      }
+    }
+    ++SectionIndex;
+  }
+  if (!HasGroups)
+    W.startLine() << "There are no group sections in the file.\n";
+}

Modified: llvm/trunk/tools/llvm-readobj/ObjDumper.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ObjDumper.h?rev=258845&r1=258844&r2=258845&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ObjDumper.h (original)
+++ llvm/trunk/tools/llvm-readobj/ObjDumper.h Tue Jan 26 13:46:39 2016
@@ -42,6 +42,7 @@ public:
   virtual void printGnuHashTable() { }
   virtual void printLoadName() {}
   virtual void printVersionInfo() {}
+  virtual void printGroupSections() {}
 
   // Only implemented for ARM ELF at this time.
   virtual void printAttributes() { }

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=258845&r1=258844&r2=258845&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp Tue Jan 26 13:46:39 2016
@@ -227,6 +227,12 @@ namespace opts {
                   cl::desc("Display ELF version sections (if present)"));
   cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"),
                              cl::aliasopt(VersionInfo));
+
+  cl::opt<bool> SectionGroups("elf-section-groups",
+                              cl::desc("Display ELF section group contents"));
+  cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
+                               cl::aliasopt(SectionGroups));
+
 } // namespace opts
 
 namespace llvm {
@@ -334,16 +340,20 @@ static void dumpObject(const ObjectFile
     Dumper->printGnuHashTable();
   if (opts::VersionInfo)
     Dumper->printVersionInfo();
-  if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
-    if (opts::ARMAttributes)
-      Dumper->printAttributes();
-  if (isMipsArch(Obj->getArch()) && Obj->isELF()) {
-    if (opts::MipsPLTGOT)
-      Dumper->printMipsPLTGOT();
-    if (opts::MipsABIFlags)
-      Dumper->printMipsABIFlags();
-    if (opts::MipsReginfo)
-      Dumper->printMipsReginfo();
+  if (Obj->isELF()) {
+    if (Obj->getArch() == llvm::Triple::arm)
+      if (opts::ARMAttributes)
+        Dumper->printAttributes();
+    if (isMipsArch(Obj->getArch())) {
+      if (opts::MipsPLTGOT)
+        Dumper->printMipsPLTGOT();
+      if (opts::MipsABIFlags)
+        Dumper->printMipsABIFlags();
+      if (opts::MipsReginfo)
+        Dumper->printMipsReginfo();
+    }
+    if (opts::SectionGroups)
+      Dumper->printGroupSections();
   }
   if (Obj->isCOFF()) {
     if (opts::COFFImports)




More information about the llvm-commits mailing list