LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
Visitor.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2* Copyright 2017 - 2024 Quarkslab
3* Copyright 2017 - 2021 K. Nakagawa
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*/
17#ifndef LIEF_VISITOR_H
18#define LIEF_VISITOR_H
19#include <set>
20#include <utility>
21#include <cstddef>
22
23#include "LIEF/visibility.h"
25
26
27namespace LIEF {
28// Forward declarations
29// ====================
30class Object;
37
38// PE
39// ===============================
41LIEF_PE_FORWARD(DelayImport)
42LIEF_PE_FORWARD(DelayImportEntry)
43LIEF_PE_FORWARD(DosHeader)
45LIEF_PE_FORWARD(OptionalHeader)
46LIEF_PE_FORWARD(RichHeader)
47LIEF_PE_FORWARD(RichEntry)
48LIEF_PE_FORWARD(DataDirectory)
51LIEF_PE_FORWARD(RelocationEntry)
52LIEF_PE_FORWARD(Export)
53LIEF_PE_FORWARD(ExportEntry)
56LIEF_PE_FORWARD(Debug)
57LIEF_PE_FORWARD(CodeView)
58LIEF_PE_FORWARD(CodeViewPDB)
59LIEF_PE_FORWARD(Import)
60LIEF_PE_FORWARD(ImportEntry)
61LIEF_PE_FORWARD(ResourceNode)
62LIEF_PE_FORWARD(ResourceData)
63LIEF_PE_FORWARD(ResourceDirectory)
64LIEF_PE_FORWARD(ResourcesManager)
65LIEF_PE_FORWARD(ResourceVersion)
66LIEF_PE_FORWARD(ResourceStringFileInfo)
67LIEF_PE_FORWARD(ResourceFixedFileInfo)
68LIEF_PE_FORWARD(ResourceVarFileInfo)
69LIEF_PE_FORWARD(LangCodeItem)
70LIEF_PE_FORWARD(ResourceIcon)
71LIEF_PE_FORWARD(ResourceDialog)
72LIEF_PE_FORWARD(ResourceDialogItem)
73LIEF_PE_FORWARD(ResourceStringTable)
74LIEF_PE_FORWARD(ResourceAccelerator)
75
76LIEF_PE_FORWARD(Signature)
78LIEF_PE_FORWARD(SignerInfo)
79LIEF_PE_FORWARD(ContentInfo)
80LIEF_PE_FORWARD(GenericContent)
81LIEF_PE_FORWARD(SpcIndirectData)
82LIEF_PE_FORWARD(Attribute)
83LIEF_PE_FORWARD(ContentType)
84LIEF_PE_FORWARD(GenericType)
85LIEF_PE_FORWARD(MsCounterSign)
86LIEF_PE_FORWARD(MsSpcNestedSignature)
87LIEF_PE_FORWARD(MsSpcStatementType)
88LIEF_PE_FORWARD(MsManifestBinaryID)
89LIEF_PE_FORWARD(PKCS9AtSequenceNumber)
90LIEF_PE_FORWARD(PKCS9CounterSignature)
91LIEF_PE_FORWARD(PKCS9MessageDigest)
92LIEF_PE_FORWARD(PKCS9SigningTime)
93LIEF_PE_FORWARD(SpcSpOpusInfo)
94LIEF_PE_FORWARD(SigningCertificateV2)
95LIEF_PE_FORWARD(SpcRelaxedPeMarkerCheck)
96
97LIEF_PE_FORWARD(CodeIntegrity)
98LIEF_PE_FORWARD(LoadConfiguration)
99LIEF_PE_FORWARD(LoadConfigurationV0)
100LIEF_PE_FORWARD(LoadConfigurationV1)
101LIEF_PE_FORWARD(LoadConfigurationV10)
102LIEF_PE_FORWARD(LoadConfigurationV11)
103LIEF_PE_FORWARD(LoadConfigurationV2)
104LIEF_PE_FORWARD(LoadConfigurationV3)
105LIEF_PE_FORWARD(LoadConfigurationV4)
106LIEF_PE_FORWARD(LoadConfigurationV5)
107LIEF_PE_FORWARD(LoadConfigurationV6)
108LIEF_PE_FORWARD(LoadConfigurationV7)
109LIEF_PE_FORWARD(LoadConfigurationV8)
110LIEF_PE_FORWARD(LoadConfigurationV9)
111LIEF_PE_FORWARD(Pogo)
112LIEF_PE_FORWARD(PogoEntry)
113LIEF_PE_FORWARD(Repro)
114
115// ELF
116// ==================================
120LIEF_ELF_FORWARD(Segment)
121LIEF_ELF_FORWARD(DynamicEntry)
122LIEF_ELF_FORWARD(DynamicEntryArray)
123LIEF_ELF_FORWARD(DynamicEntryLibrary)
124LIEF_ELF_FORWARD(DynamicSharedObject)
125LIEF_ELF_FORWARD(DynamicEntryRunPath)
126LIEF_ELF_FORWARD(DynamicEntryRpath)
127LIEF_ELF_FORWARD(DynamicEntryFlags)
130LIEF_ELF_FORWARD(SymbolVersion)
131LIEF_ELF_FORWARD(SymbolVersionRequirement)
132LIEF_ELF_FORWARD(SymbolVersionDefinition)
133LIEF_ELF_FORWARD(SymbolVersionAux)
134LIEF_ELF_FORWARD(SymbolVersionAuxRequirement)
136LIEF_ELF_FORWARD(AndroidIdent)
137LIEF_ELF_FORWARD(QNXStack)
138LIEF_ELF_FORWARD(NoteAbi)
139LIEF_ELF_FORWARD(NoteGnuProperty)
140LIEF_ELF_FORWARD(CorePrPsInfo)
141LIEF_ELF_FORWARD(CorePrStatus)
142LIEF_ELF_FORWARD(CoreAuxv)
143LIEF_ELF_FORWARD(CoreSigInfo)
144LIEF_ELF_FORWARD(CoreFile)
145LIEF_ELF_FORWARD(GnuHash)
146LIEF_ELF_FORWARD(SysvHash)
147
148
149// MACHO
150// ===============================
153LIEF_MACHO_FORWARD(LoadCommand)
154LIEF_MACHO_FORWARD(UUIDCommand)
155LIEF_MACHO_FORWARD(SymbolCommand)
156LIEF_MACHO_FORWARD(SegmentCommand)
158LIEF_MACHO_FORWARD(MainCommand)
159LIEF_MACHO_FORWARD(DynamicSymbolCommand)
160LIEF_MACHO_FORWARD(DylinkerCommand)
161LIEF_MACHO_FORWARD(DylibCommand)
162LIEF_MACHO_FORWARD(ThreadCommand)
163LIEF_MACHO_FORWARD(RPathCommand)
166LIEF_MACHO_FORWARD(RelocationObject)
167LIEF_MACHO_FORWARD(RelocationDyld)
168LIEF_MACHO_FORWARD(RelocationFixup)
169LIEF_MACHO_FORWARD(BindingInfo)
170LIEF_MACHO_FORWARD(DyldBindingInfo)
171LIEF_MACHO_FORWARD(DyldExportsTrie)
172LIEF_MACHO_FORWARD(ChainedBindingInfo)
173LIEF_MACHO_FORWARD(ExportInfo)
174LIEF_MACHO_FORWARD(FunctionStarts)
175LIEF_MACHO_FORWARD(CodeSignature)
176LIEF_MACHO_FORWARD(DataInCode)
177LIEF_MACHO_FORWARD(DataCodeEntry)
178LIEF_MACHO_FORWARD(SourceVersion)
179LIEF_MACHO_FORWARD(VersionMin)
180LIEF_MACHO_FORWARD(SegmentSplitInfo)
181LIEF_MACHO_FORWARD(SubFramework)
182LIEF_MACHO_FORWARD(Routine)
183LIEF_MACHO_FORWARD(DyldEnvironment)
184LIEF_MACHO_FORWARD(EncryptionInfo)
185LIEF_MACHO_FORWARD(BuildVersion)
186LIEF_MACHO_FORWARD(BuildToolVersion)
187LIEF_MACHO_FORWARD(FilesetCommand)
188LIEF_MACHO_FORWARD(TwoLevelHints)
189LIEF_MACHO_FORWARD(CodeSignatureDir)
190LIEF_MACHO_FORWARD(LinkerOptHint)
191LIEF_MACHO_FORWARD(UnknownCommand)
192
193// OAT
194// ===============================
197LIEF_OAT_FORWARD(DexFile)
198LIEF_OAT_FORWARD(Method)
199LIEF_OAT_FORWARD(Class)
200
201// DEX
202// ===============================
204LIEF_DEX_FORWARD(Field)
205LIEF_DEX_FORWARD(Method)
207LIEF_DEX_FORWARD(Class)
208LIEF_DEX_FORWARD(CodeInfo)
210LIEF_DEX_FORWARD(Prototype)
211LIEF_DEX_FORWARD(MapItem)
212LIEF_DEX_FORWARD(MapList)
213
214// VDEX
215// ===============================
218
219// ART
220// ===============================
223
224
226 public:
228 virtual ~Visitor();
229
230 virtual void operator()();
231
232 template<typename Arg1, typename... Args>
233 void operator()(Arg1&& arg1, Args&&... args);
234
235 virtual void visit(const Object&);
236
237 // Abstract Part
238 // =============
242
243
257
262 LIEF_ELF_VISITABLE(DynamicEntry)
263 LIEF_ELF_VISITABLE(DynamicEntryArray)
264 LIEF_ELF_VISITABLE(DynamicEntryLibrary)
265 LIEF_ELF_VISITABLE(DynamicSharedObject)
266 LIEF_ELF_VISITABLE(DynamicEntryRunPath)
267 LIEF_ELF_VISITABLE(DynamicEntryRpath)
268 LIEF_ELF_VISITABLE(DynamicEntryFlags)
271 LIEF_ELF_VISITABLE(SymbolVersion)
272 LIEF_ELF_VISITABLE(SymbolVersionRequirement)
273 LIEF_ELF_VISITABLE(SymbolVersionDefinition)
274 LIEF_ELF_VISITABLE(SymbolVersionAux)
275 LIEF_ELF_VISITABLE(SymbolVersionAuxRequirement)
277 LIEF_ELF_VISITABLE(AndroidIdent)
280 LIEF_ELF_VISITABLE(NoteGnuProperty)
281 LIEF_ELF_VISITABLE(CorePrPsInfo)
282 LIEF_ELF_VISITABLE(CorePrStatus)
288
289 // PE Part
290 // ======= LIEF_PE_VISITABLE(Binary)
293 LIEF_PE_VISITABLE(DosHeader)
296 LIEF_PE_VISITABLE(RichHeader)
299 LIEF_PE_VISITABLE(RichEntry)
305 LIEF_PE_VISITABLE(OptionalHeader)
308 LIEF_PE_VISITABLE(DataDirectory)
323 LIEF_PE_VISITABLE(RelocationEntry)
326 LIEF_PE_VISITABLE(Export)
329 LIEF_PE_VISITABLE(ExportEntry)
332 LIEF_PE_VISITABLE(Debug)
335 LIEF_PE_VISITABLE(CodeView)
338 LIEF_PE_VISITABLE(CodeViewPDB)
341 LIEF_PE_VISITABLE(Import)
344 LIEF_PE_VISITABLE(ImportEntry)
347 LIEF_PE_VISITABLE(DelayImport)
350 LIEF_PE_VISITABLE(DelayImportEntry)
353 LIEF_PE_VISITABLE(ResourceNode)
356 LIEF_PE_VISITABLE(ResourceData)
359 LIEF_PE_VISITABLE(ResourceDirectory)
362 LIEF_PE_VISITABLE(ResourcesManager)
365 LIEF_PE_VISITABLE(ResourceVersion)
368 LIEF_PE_VISITABLE(ResourceStringFileInfo)
371 LIEF_PE_VISITABLE(ResourceFixedFileInfo)
374 LIEF_PE_VISITABLE(ResourceVarFileInfo)
377 LIEF_PE_VISITABLE(ResourceStringTable)
380 LIEF_PE_VISITABLE(ResourceAccelerator)
383 LIEF_PE_VISITABLE(LangCodeItem)
386 LIEF_PE_VISITABLE(ResourceIcon)
389 LIEF_PE_VISITABLE(ResourceDialog)
392 LIEF_PE_VISITABLE(ResourceDialogItem)
395 LIEF_PE_VISITABLE(Signature)
401 LIEF_PE_VISITABLE(SignerInfo)
404 LIEF_PE_VISITABLE(ContentInfo)
407 LIEF_PE_VISITABLE(Attribute)
410 LIEF_PE_VISITABLE(ContentType)
413 LIEF_PE_VISITABLE(GenericContent)
416 LIEF_PE_VISITABLE(SpcIndirectData)
419 LIEF_PE_VISITABLE(GenericType)
422 LIEF_PE_VISITABLE(MsCounterSign)
425 LIEF_PE_VISITABLE(MsManifestBinaryID)
428 LIEF_PE_VISITABLE(MsSpcNestedSignature)
431 LIEF_PE_VISITABLE(MsSpcStatementType)
434 LIEF_PE_VISITABLE(PKCS9AtSequenceNumber)
437 LIEF_PE_VISITABLE(PKCS9CounterSignature)
440 LIEF_PE_VISITABLE(PKCS9MessageDigest)
443 LIEF_PE_VISITABLE(PKCS9SigningTime)
446 LIEF_PE_VISITABLE(SpcSpOpusInfo)
449 LIEF_PE_VISITABLE(SpcRelaxedPeMarkerCheck)
452 LIEF_PE_VISITABLE(SigningCertificateV2)
455 LIEF_PE_VISITABLE(LoadConfiguration)
458 LIEF_PE_VISITABLE(LoadConfigurationV0)
461 LIEF_PE_VISITABLE(LoadConfigurationV1)
464 LIEF_PE_VISITABLE(LoadConfigurationV2)
467 LIEF_PE_VISITABLE(LoadConfigurationV3)
470 LIEF_PE_VISITABLE(LoadConfigurationV4)
473 LIEF_PE_VISITABLE(LoadConfigurationV5)
476 LIEF_PE_VISITABLE(LoadConfigurationV6)
479 LIEF_PE_VISITABLE(LoadConfigurationV7)
482 LIEF_PE_VISITABLE(LoadConfigurationV8)
485 LIEF_PE_VISITABLE(LoadConfigurationV9)
488 LIEF_PE_VISITABLE(LoadConfigurationV10)
491 LIEF_PE_VISITABLE(LoadConfigurationV11)
494 LIEF_PE_VISITABLE(CodeIntegrity)
500 LIEF_PE_VISITABLE(PogoEntry)
503 LIEF_PE_VISITABLE(Repro)
506
507 // MachO part
508 // ========== LIEF_MACHO_VISITABLE(Binary)
514 LIEF_MACHO_VISITABLE(LoadCommand)
517 LIEF_MACHO_VISITABLE(UUIDCommand)
520 LIEF_MACHO_VISITABLE(SymbolCommand)
523 LIEF_MACHO_VISITABLE(SegmentCommand)
529 LIEF_MACHO_VISITABLE(MainCommand)
532 LIEF_MACHO_VISITABLE(DynamicSymbolCommand)
535 LIEF_MACHO_VISITABLE(DylinkerCommand)
538 LIEF_MACHO_VISITABLE(DylibCommand)
541 LIEF_MACHO_VISITABLE(ThreadCommand)
544 LIEF_MACHO_VISITABLE(RPathCommand)
553 LIEF_MACHO_VISITABLE(RelocationObject)
556 LIEF_MACHO_VISITABLE(RelocationDyld)
559 LIEF_MACHO_VISITABLE(RelocationFixup)
562 LIEF_MACHO_VISITABLE(BindingInfo)
565 LIEF_MACHO_VISITABLE(DyldBindingInfo)
568 LIEF_MACHO_VISITABLE(ChainedBindingInfo)
571 LIEF_MACHO_VISITABLE(DyldExportsTrie)
574 LIEF_MACHO_VISITABLE(ExportInfo)
577 LIEF_MACHO_VISITABLE(FunctionStarts)
580 LIEF_MACHO_VISITABLE(CodeSignature)
583 LIEF_MACHO_VISITABLE(DataInCode)
586 LIEF_MACHO_VISITABLE(DataCodeEntry)
589 LIEF_MACHO_VISITABLE(SourceVersion)
592 LIEF_MACHO_VISITABLE(VersionMin)
595 LIEF_MACHO_VISITABLE(SegmentSplitInfo)
598 LIEF_MACHO_VISITABLE(SubFramework)
601 LIEF_MACHO_VISITABLE(Routine)
604 LIEF_MACHO_VISITABLE(DyldEnvironment)
607 LIEF_MACHO_VISITABLE(EncryptionInfo)
610 LIEF_MACHO_VISITABLE(BuildVersion)
613 LIEF_MACHO_VISITABLE(BuildToolVersion)
616 LIEF_MACHO_VISITABLE(FilesetCommand)
619 LIEF_MACHO_VISITABLE(CodeSignatureDir)
622 LIEF_MACHO_VISITABLE(TwoLevelHints)
625 LIEF_MACHO_VISITABLE(LinkerOptHint)
628 LIEF_MACHO_VISITABLE(UnknownCommand)
631
632 // OAT part
633 // ========
640 LIEF_OAT_VISITABLE(DexFile)
643 LIEF_OAT_VISITABLE(Class)
646 LIEF_OAT_VISITABLE(Method)
649
650
651 // DEX part
652 // ========
656 LIEF_DEX_VISITABLE(Field)
659 LIEF_DEX_VISITABLE(Method)
665 LIEF_DEX_VISITABLE(Class)
668 LIEF_DEX_VISITABLE(CodeInfo)
674 LIEF_DEX_VISITABLE(Prototype)
677 LIEF_DEX_VISITABLE(MapList)
680 LIEF_DEX_VISITABLE(MapItem)
683
684 // VDEX part
685 // =========
692
693 // ART part
694 // =========
701
702 template<class T>
703 void dispatch(const T& obj);
704
705
706 private:
707 std::set<size_t> visited_;
708};
709
710
711
712template<typename Arg1, typename... Args>
713void Visitor::operator()(Arg1&& arg1, Args&&... args) {
714 dispatch(std::forward<Arg1>(arg1));
715 operator()(std::forward<Args>(args)... );
716}
717
718template<class T>
719void Visitor::dispatch(const T& obj) {
720 auto hash = reinterpret_cast<size_t>(&obj);
721 if (visited_.find(hash) != std::end(visited_)) {
722 // Already visited
723 return;
724 }
725
726 visited_.insert(hash);
727 visit(obj);
728}
729
730}
731#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:49
Class that represents a function in the binary.
Definition Abstract/Function.hpp:29
Definition Abstract/Header.hpp:40
Definition Object.hpp:25
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
Class which represents an abstracted section.
Definition Abstract/Section.hpp:29
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
Definition Visitor.hpp:225
virtual void operator()()
virtual ~Visitor()
virtual void visit(const Object &)
void dispatch(const T &obj)
Definition Visitor.hpp:719
LIEF namespace.
Definition Abstract/Binary.hpp:36
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:41
#define LIEF_ABSTRACT_FORWARD(OBJ)
Definition visitor_macros.hpp:126
#define LIEF_VDEX_FORWARD(OBJ)
Definition visitor_macros.hpp:98
#define LIEF_MACHO_VISITABLE(OBJ)
Definition visitor_macros.hpp:57
#define LIEF_ART_FORWARD(OBJ)
Definition visitor_macros.hpp:113
#define LIEF_ABSTRACT_VISITABLE(OBJ)
Definition visitor_macros.hpp:129
#define LIEF_DEX_VISITABLE(OBJ)
Definition visitor_macros.hpp:87
#define LIEF_OAT_FORWARD(OBJ)
Definition visitor_macros.hpp:68
#define LIEF_PE_VISITABLE(OBJ)
Definition visitor_macros.hpp:28
#define LIEF_VDEX_VISITABLE(OBJ)
Definition visitor_macros.hpp:102
#define LIEF_DEX_FORWARD(OBJ)
Definition visitor_macros.hpp:83
#define LIEF_MACHO_FORWARD(OBJ)
Definition visitor_macros.hpp:53
#define LIEF_ART_VISITABLE(OBJ)
Definition visitor_macros.hpp:117
#define LIEF_ELF_VISITABLE(OBJ)
Definition visitor_macros.hpp:43
#define LIEF_PE_FORWARD(OBJ)
Definition visitor_macros.hpp:23
#define LIEF_OAT_VISITABLE(OBJ)
Definition visitor_macros.hpp:72
#define LIEF_ELF_FORWARD(OBJ)
Definition visitor_macros.hpp:39