LIEF: Library to Instrument Executable Formats Version 0.15.1
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;
35LIEF_ABSTRACT_FORWARD(Relocation)
37
38// PE
39// ===============================
40LIEF_PE_FORWARD(Binary)
41LIEF_PE_FORWARD(DelayImport)
42LIEF_PE_FORWARD(DelayImportEntry)
43LIEF_PE_FORWARD(DosHeader)
44LIEF_PE_FORWARD(Header)
45LIEF_PE_FORWARD(OptionalHeader)
46LIEF_PE_FORWARD(RichHeader)
47LIEF_PE_FORWARD(RichEntry)
48LIEF_PE_FORWARD(DataDirectory)
49LIEF_PE_FORWARD(Section)
50LIEF_PE_FORWARD(Relocation)
51LIEF_PE_FORWARD(RelocationEntry)
52LIEF_PE_FORWARD(Export)
53LIEF_PE_FORWARD(ExportEntry)
55LIEF_PE_FORWARD(Symbol)
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// ==================================
117LIEF_ELF_FORWARD(Binary)
118LIEF_ELF_FORWARD(Header)
119LIEF_ELF_FORWARD(Section)
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)
128LIEF_ELF_FORWARD(Symbol)
129LIEF_ELF_FORWARD(Relocation)
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// ===============================
151LIEF_MACHO_FORWARD(Binary)
152LIEF_MACHO_FORWARD(Header)
153LIEF_MACHO_FORWARD(LoadCommand)
154LIEF_MACHO_FORWARD(UUIDCommand)
155LIEF_MACHO_FORWARD(SymbolCommand)
156LIEF_MACHO_FORWARD(SegmentCommand)
157LIEF_MACHO_FORWARD(Section)
158LIEF_MACHO_FORWARD(MainCommand)
159LIEF_MACHO_FORWARD(DynamicSymbolCommand)
160LIEF_MACHO_FORWARD(DylinkerCommand)
161LIEF_MACHO_FORWARD(DylibCommand)
162LIEF_MACHO_FORWARD(ThreadCommand)
163LIEF_MACHO_FORWARD(RPathCommand)
164LIEF_MACHO_FORWARD(Symbol)
165LIEF_MACHO_FORWARD(Relocation)
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(DyldEnvironment)
183LIEF_MACHO_FORWARD(EncryptionInfo)
184LIEF_MACHO_FORWARD(BuildVersion)
185LIEF_MACHO_FORWARD(BuildToolVersion)
186LIEF_MACHO_FORWARD(FilesetCommand)
187LIEF_MACHO_FORWARD(TwoLevelHints)
188LIEF_MACHO_FORWARD(CodeSignatureDir)
189LIEF_MACHO_FORWARD(LinkerOptHint)
190LIEF_MACHO_FORWARD(UnknownCommand)
191
192// OAT
193// ===============================
194LIEF_OAT_FORWARD(Binary)
195LIEF_OAT_FORWARD(Header)
196LIEF_OAT_FORWARD(DexFile)
197LIEF_OAT_FORWARD(Method)
198LIEF_OAT_FORWARD(Class)
199
200// DEX
201// ===============================
203LIEF_DEX_FORWARD(Field)
204LIEF_DEX_FORWARD(Method)
205LIEF_DEX_FORWARD(Header)
206LIEF_DEX_FORWARD(Class)
207LIEF_DEX_FORWARD(CodeInfo)
209LIEF_DEX_FORWARD(Prototype)
210LIEF_DEX_FORWARD(MapItem)
211LIEF_DEX_FORWARD(MapList)
212
213// VDEX
214// ===============================
216LIEF_VDEX_FORWARD(Header)
217
218// ART
219// ===============================
221LIEF_ART_FORWARD(Header)
222
223
225 public:
227 virtual ~Visitor();
228
229 virtual void operator()();
230
231 template<typename Arg1, typename... Args>
232 void operator()(Arg1&& arg1, Args&&... args);
233
234 virtual void visit(const Object&);
235
236 // Abstract Part
237 // =============
238
241
242
244
247
250
253
256
261 LIEF_ELF_VISITABLE(DynamicEntry)
262 LIEF_ELF_VISITABLE(DynamicEntryArray)
263 LIEF_ELF_VISITABLE(DynamicEntryLibrary)
264 LIEF_ELF_VISITABLE(DynamicSharedObject)
265 LIEF_ELF_VISITABLE(DynamicEntryRunPath)
266 LIEF_ELF_VISITABLE(DynamicEntryRpath)
267 LIEF_ELF_VISITABLE(DynamicEntryFlags)
270 LIEF_ELF_VISITABLE(SymbolVersion)
271 LIEF_ELF_VISITABLE(SymbolVersionRequirement)
272 LIEF_ELF_VISITABLE(SymbolVersionDefinition)
273 LIEF_ELF_VISITABLE(SymbolVersionAux)
274 LIEF_ELF_VISITABLE(SymbolVersionAuxRequirement)
276 LIEF_ELF_VISITABLE(AndroidIdent)
279 LIEF_ELF_VISITABLE(NoteGnuProperty)
280 LIEF_ELF_VISITABLE(CorePrPsInfo)
281 LIEF_ELF_VISITABLE(CorePrStatus)
287
288 // PE Part
289 // =======
292
295
298
301
304
306 LIEF_PE_VISITABLE(OptionalHeader)
307
309 LIEF_PE_VISITABLE(DataDirectory)
310
313
316
319
322
324 LIEF_PE_VISITABLE(RelocationEntry)
325
328
330 LIEF_PE_VISITABLE(ExportEntry)
331
334
337
339 LIEF_PE_VISITABLE(CodeViewPDB)
340
343
345 LIEF_PE_VISITABLE(ImportEntry)
346
348 LIEF_PE_VISITABLE(DelayImport)
349
351 LIEF_PE_VISITABLE(DelayImportEntry)
352
354 LIEF_PE_VISITABLE(ResourceNode)
355
357 LIEF_PE_VISITABLE(ResourceData)
358
360 LIEF_PE_VISITABLE(ResourceDirectory)
361
363 LIEF_PE_VISITABLE(ResourcesManager)
364
366 LIEF_PE_VISITABLE(ResourceVersion)
367
369 LIEF_PE_VISITABLE(ResourceStringFileInfo)
370
372 LIEF_PE_VISITABLE(ResourceFixedFileInfo)
373
375 LIEF_PE_VISITABLE(ResourceVarFileInfo)
376
378 LIEF_PE_VISITABLE(ResourceStringTable)
379
381 LIEF_PE_VISITABLE(ResourceAccelerator)
382
384 LIEF_PE_VISITABLE(LangCodeItem)
385
387 LIEF_PE_VISITABLE(ResourceIcon)
388
390 LIEF_PE_VISITABLE(ResourceDialog)
391
393 LIEF_PE_VISITABLE(ResourceDialogItem)
394
397
400
403
405 LIEF_PE_VISITABLE(ContentInfo)
406
409
411 LIEF_PE_VISITABLE(ContentType)
412
414 LIEF_PE_VISITABLE(GenericContent)
415
417 LIEF_PE_VISITABLE(SpcIndirectData)
418
420 LIEF_PE_VISITABLE(GenericType)
421
423 LIEF_PE_VISITABLE(MsCounterSign)
424
426 LIEF_PE_VISITABLE(MsManifestBinaryID)
427
429 LIEF_PE_VISITABLE(MsSpcNestedSignature)
430
432 LIEF_PE_VISITABLE(MsSpcStatementType)
433
435 LIEF_PE_VISITABLE(PKCS9AtSequenceNumber)
436
438 LIEF_PE_VISITABLE(PKCS9CounterSignature)
439
441 LIEF_PE_VISITABLE(PKCS9MessageDigest)
442
444 LIEF_PE_VISITABLE(PKCS9SigningTime)
445
447 LIEF_PE_VISITABLE(SpcSpOpusInfo)
448
450 LIEF_PE_VISITABLE(SpcRelaxedPeMarkerCheck)
451
453 LIEF_PE_VISITABLE(SigningCertificateV2)
454
456 LIEF_PE_VISITABLE(LoadConfiguration)
457
459 LIEF_PE_VISITABLE(LoadConfigurationV0)
460
462 LIEF_PE_VISITABLE(LoadConfigurationV1)
463
465 LIEF_PE_VISITABLE(LoadConfigurationV2)
466
468 LIEF_PE_VISITABLE(LoadConfigurationV3)
469
471 LIEF_PE_VISITABLE(LoadConfigurationV4)
472
474 LIEF_PE_VISITABLE(LoadConfigurationV5)
475
477 LIEF_PE_VISITABLE(LoadConfigurationV6)
478
480 LIEF_PE_VISITABLE(LoadConfigurationV7)
481
483 LIEF_PE_VISITABLE(LoadConfigurationV8)
484
486 LIEF_PE_VISITABLE(LoadConfigurationV9)
487
489 LIEF_PE_VISITABLE(LoadConfigurationV10)
490
492 LIEF_PE_VISITABLE(LoadConfigurationV11)
493
495 LIEF_PE_VISITABLE(CodeIntegrity)
496
499
502
505
506 // MachO part
507 // ==========
510
513
516
519
521 LIEF_MACHO_VISITABLE(SymbolCommand)
522
524 LIEF_MACHO_VISITABLE(SegmentCommand)
525
528
531
533 LIEF_MACHO_VISITABLE(DynamicSymbolCommand)
534
536 LIEF_MACHO_VISITABLE(DylinkerCommand)
537
540
542 LIEF_MACHO_VISITABLE(ThreadCommand)
543
546
549
552
554 LIEF_MACHO_VISITABLE(RelocationObject)
555
557 LIEF_MACHO_VISITABLE(RelocationDyld)
558
560 LIEF_MACHO_VISITABLE(RelocationFixup)
561
564
566 LIEF_MACHO_VISITABLE(DyldBindingInfo)
567
569 LIEF_MACHO_VISITABLE(ChainedBindingInfo)
570
572 LIEF_MACHO_VISITABLE(DyldExportsTrie)
573
576
578 LIEF_MACHO_VISITABLE(FunctionStarts)
579
581 LIEF_MACHO_VISITABLE(CodeSignature)
582
585
587 LIEF_MACHO_VISITABLE(DataCodeEntry)
588
590 LIEF_MACHO_VISITABLE(SourceVersion)
591
594
596 LIEF_MACHO_VISITABLE(SegmentSplitInfo)
597
600
602 LIEF_MACHO_VISITABLE(DyldEnvironment)
603
605 LIEF_MACHO_VISITABLE(EncryptionInfo)
606
609
611 LIEF_MACHO_VISITABLE(BuildToolVersion)
612
614 LIEF_MACHO_VISITABLE(FilesetCommand)
615
617 LIEF_MACHO_VISITABLE(CodeSignatureDir)
618
620 LIEF_MACHO_VISITABLE(TwoLevelHints)
621
623 LIEF_MACHO_VISITABLE(LinkerOptHint)
624
626 LIEF_MACHO_VISITABLE(UnknownCommand)
627
628 // OAT part
629 // ========
630
633
636
639
642
645
646
647 // DEX part
648 // ========
649
652
655
658
661
664
667
670
673
676
679
680 // VDEX part
681 // =========
682
685
688
689 // ART part
690 // =========
691
694
697
698 template<class T>
699 void dispatch(const T& obj);
700
701
702 private:
703 std::set<size_t> visited_;
704};
705
706
707
708template<typename Arg1, typename... Args>
709void Visitor::operator()(Arg1&& arg1, Args&&... args) {
710 dispatch(std::forward<Arg1>(arg1));
711 operator()(std::forward<Args>(args)... );
712}
713
714template<class T>
715void Visitor::dispatch(const T& obj) {
716 auto hash = reinterpret_cast<size_t>(&obj);
717 if (visited_.find(hash) != std::end(visited_)) {
718 // Already visited
719 return;
720 }
721
722 visited_.insert(hash);
723 visit(obj);
724}
725
726}
727#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:41
Class that represents a function in the binary.
Definition Abstract/Function.hpp:29
Definition Abstract/Header.hpp:29
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:224
virtual void operator()()
virtual ~Visitor()
virtual void visit(const Object &)
void dispatch(const T &obj)
Definition Visitor.hpp:715
LIEF namespace.
Definition Abstract/Binary.hpp:32
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