[llvm] r243526 - [llvm-objdump] Added -j flag to filter sections that are operated on.

Colin LeMahieu colinl at codeaurora.org
Wed Jul 29 08:45:39 PDT 2015


Author: colinl
Date: Wed Jul 29 10:45:39 2015
New Revision: 243526

URL: http://llvm.org/viewvc/llvm-project?rev=243526&view=rev
Log:
[llvm-objdump] Added -j flag to filter sections that are operated on.

Added:
    llvm/trunk/test/tools/llvm-objdump/Inputs/section-filter.obj   (with props)
    llvm/trunk/test/tools/llvm-objdump/section-filter.test
Modified:
    llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
    llvm/trunk/tools/llvm-objdump/llvm-objdump.h

Added: llvm/trunk/test/tools/llvm-objdump/Inputs/section-filter.obj
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/Inputs/section-filter.obj?rev=243526&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/llvm-objdump/Inputs/section-filter.obj
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: llvm/trunk/test/tools/llvm-objdump/section-filter.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/section-filter.test?rev=243526&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/section-filter.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/section-filter.test Wed Jul 29 10:45:39 2015
@@ -0,0 +1,7 @@
+// This test checks that --section works correctly
+// RUN: llvm-objdump -h %p/Inputs/section-filter.obj -j=.text \
+// RUN: -j=.bss | FileCheck %s
+
+# CHECK: .text
+# CHECK-NOT: .data
+# CHECK: .bss
\ No newline at end of file

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=243526&r1=243525&r2=243526&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Wed Jul 29 10:45:39 2015
@@ -70,13 +70,13 @@ llvm::Disassemble("disassemble",
   cl::desc("Display assembler mnemonics for the machine instructions"));
 static cl::alias
 Disassembled("d", cl::desc("Alias for --disassemble"),
-             cl::aliasopt(Disassemble));
-
-cl::opt<bool>
-llvm::DisassembleAll("disassemble-all",
-  cl::desc("Display assembler mnemonics for the machine instructions"));
-static cl::alias
-DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
+             cl::aliasopt(Disassemble));
+
+cl::opt<bool>
+llvm::DisassembleAll("disassemble-all",
+  cl::desc("Display assembler mnemonics for the machine instructions"));
+static cl::alias
+DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
              cl::aliasopt(DisassembleAll));
 
 cl::opt<bool>
@@ -135,6 +135,8 @@ SectionHeadersShort("headers", cl::desc(
 static cl::alias
 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
                       cl::aliasopt(SectionHeaders));
+cl::list<std::string>
+llvm::Sections("j", cl::desc("Operate on the specified sections only"));
 
 cl::list<std::string>
 llvm::MAttrs("mattr",
@@ -172,6 +174,75 @@ cl::opt<bool> PrintFaultMaps("fault-map-
 static StringRef ToolName;
 static int ReturnValue = EXIT_SUCCESS;
 
+namespace {
+typedef std::function<int(llvm::object::SectionRef const &)> FilterPredicate;
+
+class SectionFilterIterator {
+public:
+  SectionFilterIterator(FilterPredicate P,
+                        llvm::object::section_iterator const &I,
+                        llvm::object::section_iterator const &E)
+      : Predicate(P), Iterator(I), End(E) {
+    ScanPredicate();
+  }
+  llvm::object::SectionRef operator*() const { return *Iterator; }
+  SectionFilterIterator &operator++() {
+    ++Iterator;
+    ScanPredicate();
+    return *this;
+  }
+  bool operator!=(SectionFilterIterator const &Other) const {
+    return Iterator != Other.Iterator;
+  }
+
+private:
+  void ScanPredicate() {
+    while (Iterator != End && Predicate(*Iterator)) {
+      ++Iterator;
+    }
+  }
+  FilterPredicate Predicate;
+  llvm::object::section_iterator Iterator;
+  llvm::object::section_iterator End;
+};
+
+class SectionFilter {
+public:
+  SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
+      : Predicate(P), Object(O) {}
+  SectionFilterIterator begin() {
+    return SectionFilterIterator(Predicate, Object.section_begin(),
+                                 Object.section_end());
+  }
+  SectionFilterIterator end() {
+    return SectionFilterIterator(Predicate, Object.section_end(),
+                                 Object.section_end());
+  }
+
+private:
+  FilterPredicate Predicate;
+  llvm::object::ObjectFile const &Object;
+};
+SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
+  if (Sections.empty()) {
+    return SectionFilter([](llvm::object::SectionRef const &) { return 0; }, O);
+  }
+  return SectionFilter([](llvm::object::SectionRef const &S) {
+                         llvm::StringRef String;
+                         std::error_code error = S.getName(String);
+                         if (error) {
+                           return error.value();
+                         }
+                         if (std::find(Sections.begin(), Sections.end(),
+                                       String) != Sections.end()) {
+                           return 0;
+                         }
+                         return 1;
+                       },
+                       O);
+}
+}
+
 bool llvm::error(std::error_code EC) {
   if (!EC)
     return false;
@@ -478,7 +549,7 @@ static void printRelocationTargetName(co
 
     // If we couldn't find a symbol that this relocation refers to, try
     // to find a section beginning instead.
-    for (const SectionRef &Section : O->sections()) {
+    for (const SectionRef &Section : ToolSectionFilter(*O)) {
       std::error_code ec;
 
       StringRef Name;
@@ -813,7 +884,7 @@ static void DisassembleObject(const Obje
   // in RelocSecs contain the relocations for section S.
   std::error_code EC;
   std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
-  for (const SectionRef &Section : Obj->sections()) {
+  for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
     section_iterator Sec2 = Section.getRelocatedSection();
     if (Sec2 != Obj->section_end())
       SectionRelocMap[*Sec2].push_back(Section);
@@ -843,7 +914,7 @@ static void DisassembleObject(const Obje
     array_pod_sort(AllSymbols.begin(), AllSymbols.end());
   }
 
-  for (const SectionRef &Section : Obj->sections()) {
+  for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
     if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
       continue;
 
@@ -1011,7 +1082,7 @@ void llvm::PrintRelocations(const Object
   if (!Obj->isRelocatableObject())
     return;
 
-  for (const SectionRef &Section : Obj->sections()) {
+  for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
     if (Section.relocation_begin() == Section.relocation_end())
       continue;
     StringRef secname;
@@ -1039,7 +1110,7 @@ void llvm::PrintSectionHeaders(const Obj
   outs() << "Sections:\n"
             "Idx Name          Size      Address          Type\n";
   unsigned i = 0;
-  for (const SectionRef &Section : Obj->sections()) {
+  for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
     StringRef Name;
     if (error(Section.getName(Name)))
       return;
@@ -1058,7 +1129,7 @@ void llvm::PrintSectionHeaders(const Obj
 
 void llvm::PrintSectionContents(const ObjectFile *Obj) {
   std::error_code EC;
-  for (const SectionRef &Section : Obj->sections()) {
+  for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
     StringRef Name;
     StringRef Contents;
     if (error(Section.getName(Name)))
@@ -1336,7 +1407,7 @@ void llvm::printRawClangAST(const Object
   }
 
   Optional<object::SectionRef> ClangASTSection;
-  for (auto Sec : Obj->sections()) {
+  for (auto Sec : ToolSectionFilter(*Obj)) {
     StringRef Name;
     Sec.getName(Name);
     if (Name == ClangASTSectionName) {
@@ -1371,7 +1442,7 @@ static void printFaultMaps(const ObjectF
 
   Optional<object::SectionRef> FaultMapSection;
 
-  for (auto Sec : Obj->sections()) {
+  for (auto Sec : ToolSectionFilter(*Obj)) {
     StringRef Name;
     Sec.getName(Name);
     if (Name == FaultMapSectionName) {

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.h?rev=243526&r1=243525&r2=243526&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.h (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.h Wed Jul 29 10:45:39 2015
@@ -25,8 +25,9 @@ extern cl::opt<std::string> TripleName;
 extern cl::opt<std::string> ArchName;
 extern cl::opt<std::string> MCPU;
 extern cl::list<std::string> MAttrs;
+extern cl::list<std::string> Sections;
 extern cl::list<std::string> DumpSections;
-extern cl::opt<bool> Disassemble;
+extern cl::opt<bool> Disassemble;
 extern cl::opt<bool> DisassembleAll;
 extern cl::opt<bool> NoShowRawInsn;
 extern cl::opt<bool> PrivateHeaders;





More information about the llvm-commits mailing list