Skip to content

Commit 345a03b

Browse files
JestrTulipmtrofin
authored andcommitted
[nfc] Factoring out utility that can be used for other object-level tools
Related rfc can be found at https://discourse.llvm.org/t/rfc-llvm-cm-cost-model-evaluation-for-object-files-machine-code/71502. We want to reuse the instruction iterator for this tool. Reviewed By: mtrofin, kazu, jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D152869
1 parent 0fd4175 commit 345a03b

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

llvm/include/llvm/Object/ObjectFile.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class WasmObjectFile;
4646

4747
using section_iterator = content_iterator<SectionRef>;
4848

49+
typedef std::function<bool(const SectionRef &)> SectionFilterPredicate;
4950
/// This is a value type class that represents a single relocation in the list
5051
/// of relocations in the object file.
5152
class RelocationRef {
@@ -399,6 +400,57 @@ class ObjectFile : public SymbolicFile {
399400
createWasmObjectFile(MemoryBufferRef Object);
400401
};
401402

403+
/// A filtered iterator for SectionRefs that skips sections based on some given
404+
/// predicate.
405+
class SectionFilterIterator {
406+
public:
407+
SectionFilterIterator(SectionFilterPredicate Pred,
408+
const section_iterator &Begin,
409+
const section_iterator &End)
410+
: Predicate(std::move(Pred)), Iterator(Begin), End(End) {
411+
scanPredicate();
412+
}
413+
const SectionRef &operator*() const { return *Iterator; }
414+
SectionFilterIterator &operator++() {
415+
++Iterator;
416+
scanPredicate();
417+
return *this;
418+
}
419+
bool operator!=(const SectionFilterIterator &Other) const {
420+
return Iterator != Other.Iterator;
421+
}
422+
423+
private:
424+
void scanPredicate() {
425+
while (Iterator != End && !Predicate(*Iterator)) {
426+
++Iterator;
427+
}
428+
}
429+
SectionFilterPredicate Predicate;
430+
section_iterator Iterator;
431+
section_iterator End;
432+
};
433+
434+
/// Creates an iterator range of SectionFilterIterators for a given Object and
435+
/// predicate.
436+
class SectionFilter {
437+
public:
438+
SectionFilter(SectionFilterPredicate Pred, const ObjectFile &Obj)
439+
: Predicate(std::move(Pred)), Object(Obj) {}
440+
SectionFilterIterator begin() {
441+
return SectionFilterIterator(Predicate, Object.section_begin(),
442+
Object.section_end());
443+
}
444+
SectionFilterIterator end() {
445+
return SectionFilterIterator(Predicate, Object.section_end(),
446+
Object.section_end());
447+
}
448+
449+
private:
450+
SectionFilterPredicate Predicate;
451+
const ObjectFile &Object;
452+
};
453+
402454
// Inline function definitions.
403455
inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
404456
: BasicSymbolRef(SymbolP, Owner) {}

llvm/tools/llvm-objdump/llvm-objdump.h

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
1515
#include "llvm/MC/MCSubtargetInfo.h"
1616
#include "llvm/Object/Archive.h"
17+
#include "llvm/Object/ObjectFile.h"
1718
#include "llvm/Support/Compiler.h"
1819
#include "llvm/Support/DataTypes.h"
1920
#include "llvm/Support/FormattedStream.h"
@@ -64,59 +65,6 @@ extern bool UnwindInfo;
6465

6566
extern StringSet<> FoundSectionSet;
6667

67-
typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
68-
69-
/// A filtered iterator for SectionRefs that skips sections based on some given
70-
/// predicate.
71-
class SectionFilterIterator {
72-
public:
73-
SectionFilterIterator(FilterPredicate P,
74-
llvm::object::section_iterator const &I,
75-
llvm::object::section_iterator const &E)
76-
: Predicate(std::move(P)), Iterator(I), End(E) {
77-
ScanPredicate();
78-
}
79-
const llvm::object::SectionRef &operator*() const { return *Iterator; }
80-
SectionFilterIterator &operator++() {
81-
++Iterator;
82-
ScanPredicate();
83-
return *this;
84-
}
85-
bool operator!=(SectionFilterIterator const &Other) const {
86-
return Iterator != Other.Iterator;
87-
}
88-
89-
private:
90-
void ScanPredicate() {
91-
while (Iterator != End && !Predicate(*Iterator)) {
92-
++Iterator;
93-
}
94-
}
95-
FilterPredicate Predicate;
96-
llvm::object::section_iterator Iterator;
97-
llvm::object::section_iterator End;
98-
};
99-
100-
/// Creates an iterator range of SectionFilterIterators for a given Object and
101-
/// predicate.
102-
class SectionFilter {
103-
public:
104-
SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
105-
: Predicate(std::move(P)), Object(O) {}
106-
SectionFilterIterator begin() {
107-
return SectionFilterIterator(Predicate, Object.section_begin(),
108-
Object.section_end());
109-
}
110-
SectionFilterIterator end() {
111-
return SectionFilterIterator(Predicate, Object.section_end(),
112-
Object.section_end());
113-
}
114-
115-
private:
116-
FilterPredicate Predicate;
117-
llvm::object::ObjectFile const &Object;
118-
};
119-
12068
// Various helper functions.
12169

12270
/// Creates a SectionFilter with a standard predicate that conditionally skips
@@ -125,8 +73,8 @@ class SectionFilter {
12573
/// Idx is an optional output parameter that keeps track of which section index
12674
/// this is. This may be different than the actual section number, as some
12775
/// sections may be filtered (e.g. symbol tables).
128-
SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O,
129-
uint64_t *Idx = nullptr);
76+
object::SectionFilter ToolSectionFilter(const llvm::object::ObjectFile &O,
77+
uint64_t *Idx = nullptr);
13078

13179
bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
13280
void printRelocations(const object::ObjectFile *O);

0 commit comments

Comments
 (0)