Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -136,6 +136,11 @@ SectionHeadersShorter("h", cl::desc("Alias for --section-headers"), cl::aliasopt(SectionHeaders)); +static cl::opt +Section("section", cl::desc("Display information only for section")); +static cl::alias +Sectionj("j", cl::desc("Alias for --section"), cl::aliasopt(Section)); + cl::list llvm::MAttrs("mattr", cl::CommaSeparated, @@ -172,6 +177,81 @@ static StringRef ToolName; static int ReturnValue = EXIT_SUCCESS; +namespace { + typedef std::function 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 (Section.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 (String == Section) + { + return 0; + } + return 1; + }, + O); + } +} + bool llvm::error(std::error_code EC) { if (!EC) return false; @@ -478,7 +558,7 @@ // 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 +893,7 @@ // in RelocSecs contain the relocations for section S. std::error_code EC; std::map> 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 +923,7 @@ 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 +1091,7 @@ 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 +1119,7 @@ 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 +1138,7 @@ 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 +1416,7 @@ } Optional ClangASTSection; - for (auto Sec : Obj->sections()) { + for (auto Sec : ToolSectionFilter(*Obj)) { StringRef Name; Sec.getName(Name); if (Name == ClangASTSectionName) { @@ -1371,7 +1451,7 @@ Optional FaultMapSection; - for (auto Sec : Obj->sections()) { + for (auto Sec : ToolSectionFilter(*Obj)) { StringRef Name; Sec.getName(Name); if (Name == FaultMapSectionName) {