llvm.org GIT mirror llvm / edbf9d7
[llvm-objdump] Added -j flag to filter sections that are operated on. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243526 91177308-0d34-0410-b5e6-96231b3b80d8 Colin LeMahieu 5 years ago
4 changed file(s) with 95 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
0 // This test checks that --section works correctly
1 // RUN: llvm-objdump -h %p/Inputs/section-filter.obj -j=.text \
2 // RUN: -j=.bss | FileCheck %s
3
4 # CHECK: .text
5 # CHECK-NOT: .data
6 # CHECK: .bss
6969 cl::desc("Display assembler mnemonics for the machine instructions"));
7070 static cl::alias
7171 Disassembled("d", cl::desc("Alias for --disassemble"),
72 cl::aliasopt(Disassemble));
73
74 cl::opt
75 llvm::DisassembleAll("disassemble-all",
76 cl::desc("Display assembler mnemonics for the machine instructions"));
77 static cl::alias
78 DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
72 cl::aliasopt(Disassemble));
73
74 cl::opt
75 llvm::DisassembleAll("disassemble-all",
76 cl::desc("Display assembler mnemonics for the machine instructions"));
77 static cl::alias
78 DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
7979 cl::aliasopt(DisassembleAll));
8080
8181 cl::opt
134134 static cl::alias
135135 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
136136 cl::aliasopt(SectionHeaders));
137 cl::list
138 llvm::Sections("j", cl::desc("Operate on the specified sections only"));
137139
138140 cl::list
139141 llvm::MAttrs("mattr",
170172
171173 static StringRef ToolName;
172174 static int ReturnValue = EXIT_SUCCESS;
175
176 namespace {
177 typedef std::function FilterPredicate;
178
179 class SectionFilterIterator {
180 public:
181 SectionFilterIterator(FilterPredicate P,
182 llvm::object::section_iterator const &I,
183 llvm::object::section_iterator const &E)
184 : Predicate(P), Iterator(I), End(E) {
185 ScanPredicate();
186 }
187 llvm::object::SectionRef operator*() const { return *Iterator; }
188 SectionFilterIterator &operator++() {
189 ++Iterator;
190 ScanPredicate();
191 return *this;
192 }
193 bool operator!=(SectionFilterIterator const &Other) const {
194 return Iterator != Other.Iterator;
195 }
196
197 private:
198 void ScanPredicate() {
199 while (Iterator != End && Predicate(*Iterator)) {
200 ++Iterator;
201 }
202 }
203 FilterPredicate Predicate;
204 llvm::object::section_iterator Iterator;
205 llvm::object::section_iterator End;
206 };
207
208 class SectionFilter {
209 public:
210 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
211 : Predicate(P), Object(O) {}
212 SectionFilterIterator begin() {
213 return SectionFilterIterator(Predicate, Object.section_begin(),
214 Object.section_end());
215 }
216 SectionFilterIterator end() {
217 return SectionFilterIterator(Predicate, Object.section_end(),
218 Object.section_end());
219 }
220
221 private:
222 FilterPredicate Predicate;
223 llvm::object::ObjectFile const &Object;
224 };
225 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
226 if (Sections.empty()) {
227 return SectionFilter([](llvm::object::SectionRef const &) { return 0; }, O);
228 }
229 return SectionFilter([](llvm::object::SectionRef const &S) {
230 llvm::StringRef String;
231 std::error_code error = S.getName(String);
232 if (error) {
233 return error.value();
234 }
235 if (std::find(Sections.begin(), Sections.end(),
236 String) != Sections.end()) {
237 return 0;
238 }
239 return 1;
240 },
241 O);
242 }
243 }
173244
174245 bool llvm::error(std::error_code EC) {
175246 if (!EC)
477548
478549 // If we couldn't find a symbol that this relocation refers to, try
479550 // to find a section beginning instead.
480 for (const SectionRef &Section : O->sections()) {
551 for (const SectionRef &Section : ToolSectionFilter(*O)) {
481552 std::error_code ec;
482553
483554 StringRef Name;
812883 // in RelocSecs contain the relocations for section S.
813884 std::error_code EC;
814885 std::map> SectionRelocMap;
815 for (const SectionRef &Section : Obj->sections()) {
886 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
816887 section_iterator Sec2 = Section.getRelocatedSection();
817888 if (Sec2 != Obj->section_end())
818889 SectionRelocMap[*Sec2].push_back(Section);
842913 array_pod_sort(AllSymbols.begin(), AllSymbols.end());
843914 }
844915
845 for (const SectionRef &Section : Obj->sections()) {
916 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
846917 if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
847918 continue;
848919
10101081 if (!Obj->isRelocatableObject())
10111082 return;
10121083
1013 for (const SectionRef &Section : Obj->sections()) {
1084 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
10141085 if (Section.relocation_begin() == Section.relocation_end())
10151086 continue;
10161087 StringRef secname;
10381109 outs() << "Sections:\n"
10391110 "Idx Name Size Address Type\n";
10401111 unsigned i = 0;
1041 for (const SectionRef &Section : Obj->sections()) {
1112 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
10421113 StringRef Name;
10431114 if (error(Section.getName(Name)))
10441115 return;
10571128
10581129 void llvm::PrintSectionContents(const ObjectFile *Obj) {
10591130 std::error_code EC;
1060 for (const SectionRef &Section : Obj->sections()) {
1131 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
10611132 StringRef Name;
10621133 StringRef Contents;
10631134 if (error(Section.getName(Name)))
13351406 }
13361407
13371408 Optional ClangASTSection;
1338 for (auto Sec : Obj->sections()) {
1409 for (auto Sec : ToolSectionFilter(*Obj)) {
13391410 StringRef Name;
13401411 Sec.getName(Name);
13411412 if (Name == ClangASTSectionName) {
13701441
13711442 Optional FaultMapSection;
13721443
1373 for (auto Sec : Obj->sections()) {
1444 for (auto Sec : ToolSectionFilter(*Obj)) {
13741445 StringRef Name;
13751446 Sec.getName(Name);
13761447 if (Name == FaultMapSectionName) {
2424 extern cl::opt ArchName;
2525 extern cl::opt MCPU;
2626 extern cl::list MAttrs;
27 extern cl::list Sections;
2728 extern cl::list DumpSections;
28 extern cl::opt Disassemble;
29 extern cl::opt Disassemble;
2930 extern cl::opt DisassembleAll;
3031 extern cl::opt NoShowRawInsn;
3132 extern cl::opt PrivateHeaders;