Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit edbf9d7

Browse files
author
Colin LeMahieu
committed
[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
1 parent d63325d commit edbf9d7

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed
441 Bytes
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This test checks that --section works correctly
2+
// RUN: llvm-objdump -h %p/Inputs/section-filter.obj -j=.text \
3+
// RUN: -j=.bss | FileCheck %s
4+
5+
# CHECK: .text
6+
# CHECK-NOT: .data
7+
# CHECK: .bss

tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ llvm::Disassemble("disassemble",
7070
cl::desc("Display assembler mnemonics for the machine instructions"));
7171
static cl::alias
7272
Disassembled("d", cl::desc("Alias for --disassemble"),
73-
cl::aliasopt(Disassemble));
74-
75-
cl::opt<bool>
76-
llvm::DisassembleAll("disassemble-all",
77-
cl::desc("Display assembler mnemonics for the machine instructions"));
78-
static cl::alias
79-
DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
73+
cl::aliasopt(Disassemble));
74+
75+
cl::opt<bool>
76+
llvm::DisassembleAll("disassemble-all",
77+
cl::desc("Display assembler mnemonics for the machine instructions"));
78+
static cl::alias
79+
DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
8080
cl::aliasopt(DisassembleAll));
8181

8282
cl::opt<bool>
@@ -135,6 +135,8 @@ SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
135135
static cl::alias
136136
SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
137137
cl::aliasopt(SectionHeaders));
138+
cl::list<std::string>
139+
llvm::Sections("j", cl::desc("Operate on the specified sections only"));
138140

139141
cl::list<std::string>
140142
llvm::MAttrs("mattr",
@@ -172,6 +174,75 @@ cl::opt<bool> PrintFaultMaps("fault-map-section",
172174
static StringRef ToolName;
173175
static int ReturnValue = EXIT_SUCCESS;
174176

177+
namespace {
178+
typedef std::function<int(llvm::object::SectionRef const &)> FilterPredicate;
179+
180+
class SectionFilterIterator {
181+
public:
182+
SectionFilterIterator(FilterPredicate P,
183+
llvm::object::section_iterator const &I,
184+
llvm::object::section_iterator const &E)
185+
: Predicate(P), Iterator(I), End(E) {
186+
ScanPredicate();
187+
}
188+
llvm::object::SectionRef operator*() const { return *Iterator; }
189+
SectionFilterIterator &operator++() {
190+
++Iterator;
191+
ScanPredicate();
192+
return *this;
193+
}
194+
bool operator!=(SectionFilterIterator const &Other) const {
195+
return Iterator != Other.Iterator;
196+
}
197+
198+
private:
199+
void ScanPredicate() {
200+
while (Iterator != End && Predicate(*Iterator)) {
201+
++Iterator;
202+
}
203+
}
204+
FilterPredicate Predicate;
205+
llvm::object::section_iterator Iterator;
206+
llvm::object::section_iterator End;
207+
};
208+
209+
class SectionFilter {
210+
public:
211+
SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
212+
: Predicate(P), Object(O) {}
213+
SectionFilterIterator begin() {
214+
return SectionFilterIterator(Predicate, Object.section_begin(),
215+
Object.section_end());
216+
}
217+
SectionFilterIterator end() {
218+
return SectionFilterIterator(Predicate, Object.section_end(),
219+
Object.section_end());
220+
}
221+
222+
private:
223+
FilterPredicate Predicate;
224+
llvm::object::ObjectFile const &Object;
225+
};
226+
SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
227+
if (Sections.empty()) {
228+
return SectionFilter([](llvm::object::SectionRef const &) { return 0; }, O);
229+
}
230+
return SectionFilter([](llvm::object::SectionRef const &S) {
231+
llvm::StringRef String;
232+
std::error_code error = S.getName(String);
233+
if (error) {
234+
return error.value();
235+
}
236+
if (std::find(Sections.begin(), Sections.end(),
237+
String) != Sections.end()) {
238+
return 0;
239+
}
240+
return 1;
241+
},
242+
O);
243+
}
244+
}
245+
175246
bool llvm::error(std::error_code EC) {
176247
if (!EC)
177248
return false;
@@ -478,7 +549,7 @@ static void printRelocationTargetName(const MachOObjectFile *O,
478549

479550
// If we couldn't find a symbol that this relocation refers to, try
480551
// to find a section beginning instead.
481-
for (const SectionRef &Section : O->sections()) {
552+
for (const SectionRef &Section : ToolSectionFilter(*O)) {
482553
std::error_code ec;
483554

484555
StringRef Name;
@@ -813,7 +884,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
813884
// in RelocSecs contain the relocations for section S.
814885
std::error_code EC;
815886
std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
816-
for (const SectionRef &Section : Obj->sections()) {
887+
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
817888
section_iterator Sec2 = Section.getRelocatedSection();
818889
if (Sec2 != Obj->section_end())
819890
SectionRelocMap[*Sec2].push_back(Section);
@@ -843,7 +914,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
843914
array_pod_sort(AllSymbols.begin(), AllSymbols.end());
844915
}
845916

846-
for (const SectionRef &Section : Obj->sections()) {
917+
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
847918
if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
848919
continue;
849920

@@ -1011,7 +1082,7 @@ void llvm::PrintRelocations(const ObjectFile *Obj) {
10111082
if (!Obj->isRelocatableObject())
10121083
return;
10131084

1014-
for (const SectionRef &Section : Obj->sections()) {
1085+
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
10151086
if (Section.relocation_begin() == Section.relocation_end())
10161087
continue;
10171088
StringRef secname;
@@ -1039,7 +1110,7 @@ void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
10391110
outs() << "Sections:\n"
10401111
"Idx Name Size Address Type\n";
10411112
unsigned i = 0;
1042-
for (const SectionRef &Section : Obj->sections()) {
1113+
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
10431114
StringRef Name;
10441115
if (error(Section.getName(Name)))
10451116
return;
@@ -1058,7 +1129,7 @@ void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
10581129

10591130
void llvm::PrintSectionContents(const ObjectFile *Obj) {
10601131
std::error_code EC;
1061-
for (const SectionRef &Section : Obj->sections()) {
1132+
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
10621133
StringRef Name;
10631134
StringRef Contents;
10641135
if (error(Section.getName(Name)))
@@ -1336,7 +1407,7 @@ void llvm::printRawClangAST(const ObjectFile *Obj) {
13361407
}
13371408

13381409
Optional<object::SectionRef> ClangASTSection;
1339-
for (auto Sec : Obj->sections()) {
1410+
for (auto Sec : ToolSectionFilter(*Obj)) {
13401411
StringRef Name;
13411412
Sec.getName(Name);
13421413
if (Name == ClangASTSectionName) {
@@ -1371,7 +1442,7 @@ static void printFaultMaps(const ObjectFile *Obj) {
13711442

13721443
Optional<object::SectionRef> FaultMapSection;
13731444

1374-
for (auto Sec : Obj->sections()) {
1445+
for (auto Sec : ToolSectionFilter(*Obj)) {
13751446
StringRef Name;
13761447
Sec.getName(Name);
13771448
if (Name == FaultMapSectionName) {

tools/llvm-objdump/llvm-objdump.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ extern cl::opt<std::string> TripleName;
2525
extern cl::opt<std::string> ArchName;
2626
extern cl::opt<std::string> MCPU;
2727
extern cl::list<std::string> MAttrs;
28+
extern cl::list<std::string> Sections;
2829
extern cl::list<std::string> DumpSections;
29-
extern cl::opt<bool> Disassemble;
30+
extern cl::opt<bool> Disassemble;
3031
extern cl::opt<bool> DisassembleAll;
3132
extern cl::opt<bool> NoShowRawInsn;
3233
extern cl::opt<bool> PrivateHeaders;

0 commit comments

Comments
 (0)