@@ -70,13 +70,13 @@ llvm::Disassemble("disassemble",
70
70
cl::desc (" Display assembler mnemonics for the machine instructions" ));
71
71
static cl::alias
72
72
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" ),
80
80
cl::aliasopt(DisassembleAll));
81
81
82
82
cl::opt<bool >
@@ -135,6 +135,8 @@ SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
135
135
static cl::alias
136
136
SectionHeadersShorter (" h" , cl::desc(" Alias for --section-headers" ),
137
137
cl::aliasopt(SectionHeaders));
138
+ cl::list<std::string>
139
+ llvm::Sections (" j" , cl::desc(" Operate on the specified sections only" ));
138
140
139
141
cl::list<std::string>
140
142
llvm::MAttrs (" mattr" ,
@@ -172,6 +174,75 @@ cl::opt<bool> PrintFaultMaps("fault-map-section",
172
174
static StringRef ToolName;
173
175
static int ReturnValue = EXIT_SUCCESS;
174
176
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
+
175
246
bool llvm::error (std::error_code EC) {
176
247
if (!EC)
177
248
return false ;
@@ -478,7 +549,7 @@ static void printRelocationTargetName(const MachOObjectFile *O,
478
549
479
550
// If we couldn't find a symbol that this relocation refers to, try
480
551
// to find a section beginning instead.
481
- for (const SectionRef &Section : O-> sections ( )) {
552
+ for (const SectionRef &Section : ToolSectionFilter (*O )) {
482
553
std::error_code ec;
483
554
484
555
StringRef Name;
@@ -813,7 +884,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
813
884
// in RelocSecs contain the relocations for section S.
814
885
std::error_code EC;
815
886
std::map<SectionRef, SmallVector<SectionRef, 1 >> SectionRelocMap;
816
- for (const SectionRef &Section : Obj-> sections ( )) {
887
+ for (const SectionRef &Section : ToolSectionFilter (*Obj )) {
817
888
section_iterator Sec2 = Section.getRelocatedSection ();
818
889
if (Sec2 != Obj->section_end ())
819
890
SectionRelocMap[*Sec2].push_back (Section);
@@ -843,7 +914,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
843
914
array_pod_sort (AllSymbols.begin (), AllSymbols.end ());
844
915
}
845
916
846
- for (const SectionRef &Section : Obj-> sections ( )) {
917
+ for (const SectionRef &Section : ToolSectionFilter (*Obj )) {
847
918
if (!DisassembleAll && (!Section.isText () || Section.isVirtual ()))
848
919
continue ;
849
920
@@ -1011,7 +1082,7 @@ void llvm::PrintRelocations(const ObjectFile *Obj) {
1011
1082
if (!Obj->isRelocatableObject ())
1012
1083
return ;
1013
1084
1014
- for (const SectionRef &Section : Obj-> sections ( )) {
1085
+ for (const SectionRef &Section : ToolSectionFilter (*Obj )) {
1015
1086
if (Section.relocation_begin () == Section.relocation_end ())
1016
1087
continue ;
1017
1088
StringRef secname;
@@ -1039,7 +1110,7 @@ void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
1039
1110
outs () << " Sections:\n "
1040
1111
" Idx Name Size Address Type\n " ;
1041
1112
unsigned i = 0 ;
1042
- for (const SectionRef &Section : Obj-> sections ( )) {
1113
+ for (const SectionRef &Section : ToolSectionFilter (*Obj )) {
1043
1114
StringRef Name;
1044
1115
if (error (Section.getName (Name)))
1045
1116
return ;
@@ -1058,7 +1129,7 @@ void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
1058
1129
1059
1130
void llvm::PrintSectionContents (const ObjectFile *Obj) {
1060
1131
std::error_code EC;
1061
- for (const SectionRef &Section : Obj-> sections ( )) {
1132
+ for (const SectionRef &Section : ToolSectionFilter (*Obj )) {
1062
1133
StringRef Name;
1063
1134
StringRef Contents;
1064
1135
if (error (Section.getName (Name)))
@@ -1336,7 +1407,7 @@ void llvm::printRawClangAST(const ObjectFile *Obj) {
1336
1407
}
1337
1408
1338
1409
Optional<object::SectionRef> ClangASTSection;
1339
- for (auto Sec : Obj-> sections ( )) {
1410
+ for (auto Sec : ToolSectionFilter (*Obj )) {
1340
1411
StringRef Name;
1341
1412
Sec.getName (Name);
1342
1413
if (Name == ClangASTSectionName) {
@@ -1371,7 +1442,7 @@ static void printFaultMaps(const ObjectFile *Obj) {
1371
1442
1372
1443
Optional<object::SectionRef> FaultMapSection;
1373
1444
1374
- for (auto Sec : Obj-> sections ( )) {
1445
+ for (auto Sec : ToolSectionFilter (*Obj )) {
1375
1446
StringRef Name;
1376
1447
Sec.getName (Name);
1377
1448
if (Name == FaultMapSectionName) {
0 commit comments