LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
Visitor.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2* Copyright 2017 - 2026 Quarkslab
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16#ifndef LIEF_VISITOR_H
17#define LIEF_VISITOR_H
18#include <set>
19#include <utility>
20#include <cstddef>
21
22#include "LIEF/visibility.h"
24
25
26namespace LIEF {
27// Forward declarations
28// ====================
29class Object;
36
37// PE
38// ===============================
40LIEF_PE_FORWARD(DelayImport)
41LIEF_PE_FORWARD(DelayImportEntry)
42LIEF_PE_FORWARD(DosHeader)
44LIEF_PE_FORWARD(OptionalHeader)
45LIEF_PE_FORWARD(RichHeader)
46LIEF_PE_FORWARD(RichEntry)
47LIEF_PE_FORWARD(DataDirectory)
50LIEF_PE_FORWARD(RelocationEntry)
51LIEF_PE_FORWARD(Export)
52LIEF_PE_FORWARD(ExportEntry)
54LIEF_PE_FORWARD(Debug)
55LIEF_PE_FORWARD(CodeView)
56LIEF_PE_FORWARD(CodeViewPDB)
57LIEF_PE_FORWARD(Import)
58LIEF_PE_FORWARD(ImportEntry)
59LIEF_PE_FORWARD(ResourceNode)
60LIEF_PE_FORWARD(ResourceData)
61LIEF_PE_FORWARD(ResourceDirectory)
62LIEF_PE_FORWARD(ResourcesManager)
63LIEF_PE_FORWARD(ResourceVersion)
64LIEF_PE_FORWARD(ResourceStringFileInfo)
65LIEF_PE_FORWARD(ResourceVarFileInfo)
66LIEF_PE_FORWARD(ResourceIcon)
67LIEF_PE_FORWARD(ResourceDialogExtended)
68LIEF_PE_FORWARD(ResourceDialogRegular)
69LIEF_PE_FORWARD(ResourceStringTable)
70LIEF_PE_FORWARD(ResourceAccelerator)
71
72LIEF_PE_FORWARD(Signature)
74LIEF_PE_FORWARD(SignerInfo)
75LIEF_PE_FORWARD(ContentInfo)
76LIEF_PE_FORWARD(GenericContent)
77LIEF_PE_FORWARD(SpcIndirectData)
78LIEF_PE_FORWARD(Attribute)
79LIEF_PE_FORWARD(ContentType)
80LIEF_PE_FORWARD(GenericType)
81LIEF_PE_FORWARD(MsCounterSign)
82LIEF_PE_FORWARD(MsSpcNestedSignature)
83LIEF_PE_FORWARD(MsSpcStatementType)
84LIEF_PE_FORWARD(MsManifestBinaryID)
85LIEF_PE_FORWARD(PKCS9AtSequenceNumber)
86LIEF_PE_FORWARD(PKCS9CounterSignature)
87LIEF_PE_FORWARD(PKCS9MessageDigest)
88LIEF_PE_FORWARD(PKCS9SigningTime)
89LIEF_PE_FORWARD(SpcSpOpusInfo)
90LIEF_PE_FORWARD(SigningCertificateV2)
91LIEF_PE_FORWARD(SpcRelaxedPeMarkerCheck)
92
93LIEF_PE_FORWARD(CodeIntegrity)
94LIEF_PE_FORWARD(LoadConfiguration)
96LIEF_PE_FORWARD(PogoEntry)
97LIEF_PE_FORWARD(Repro)
98
99// ELF
100// ==================================
104LIEF_ELF_FORWARD(Segment)
105LIEF_ELF_FORWARD(DynamicEntry)
106LIEF_ELF_FORWARD(DynamicEntryArray)
107LIEF_ELF_FORWARD(DynamicEntryLibrary)
108LIEF_ELF_FORWARD(DynamicSharedObject)
109LIEF_ELF_FORWARD(DynamicEntryAuxiliary)
110LIEF_ELF_FORWARD(DynamicEntryFilter)
111LIEF_ELF_FORWARD(DynamicEntryRunPath)
112LIEF_ELF_FORWARD(DynamicEntryRpath)
113LIEF_ELF_FORWARD(DynamicEntryFlags)
116LIEF_ELF_FORWARD(SymbolVersion)
117LIEF_ELF_FORWARD(SymbolVersionRequirement)
118LIEF_ELF_FORWARD(SymbolVersionDefinition)
119LIEF_ELF_FORWARD(SymbolVersionAux)
120LIEF_ELF_FORWARD(SymbolVersionAuxRequirement)
122LIEF_ELF_FORWARD(AndroidIdent)
123LIEF_ELF_FORWARD(QNXStack)
124LIEF_ELF_FORWARD(NoteAbi)
125LIEF_ELF_FORWARD(NoteGnuProperty)
126LIEF_ELF_FORWARD(CorePrPsInfo)
127LIEF_ELF_FORWARD(CorePrStatus)
128LIEF_ELF_FORWARD(CoreAuxv)
129LIEF_ELF_FORWARD(CoreSigInfo)
130LIEF_ELF_FORWARD(CoreFile)
131LIEF_ELF_FORWARD(GnuHash)
132LIEF_ELF_FORWARD(SysvHash)
133
134
135// MACHO
136// ===============================
179
180// OAT
181// ===============================
184LIEF_OAT_FORWARD(DexFile)
185LIEF_OAT_FORWARD(Method)
186LIEF_OAT_FORWARD(Class)
187
188// DEX
189// ===============================
191LIEF_DEX_FORWARD(Field)
192LIEF_DEX_FORWARD(Method)
194LIEF_DEX_FORWARD(Class)
195LIEF_DEX_FORWARD(CodeInfo)
197LIEF_DEX_FORWARD(Prototype)
198LIEF_DEX_FORWARD(MapItem)
199LIEF_DEX_FORWARD(MapList)
200
201// VDEX
202// ===============================
205
206// ART
207// ===============================
210
211
213 public:
215 virtual ~Visitor();
216
217 virtual void operator()();
218
219 template<typename Arg1, typename... Args>
220 void operator()(Arg1&& arg1, Args&&... args);
221
222 virtual void visit(const Object&);
223
224 // Abstract Part
225 // =============
226
229
230
232
235
238
241
244
249 LIEF_ELF_VISITABLE(DynamicEntry)
250 LIEF_ELF_VISITABLE(DynamicEntryArray)
251 LIEF_ELF_VISITABLE(DynamicEntryLibrary)
252 LIEF_ELF_VISITABLE(DynamicSharedObject)
253 LIEF_ELF_VISITABLE(DynamicEntryAuxiliary)
254 LIEF_ELF_VISITABLE(DynamicEntryFilter)
255 LIEF_ELF_VISITABLE(DynamicEntryRunPath)
256 LIEF_ELF_VISITABLE(DynamicEntryRpath)
257 LIEF_ELF_VISITABLE(DynamicEntryFlags)
260 LIEF_ELF_VISITABLE(SymbolVersion)
261 LIEF_ELF_VISITABLE(SymbolVersionRequirement)
262 LIEF_ELF_VISITABLE(SymbolVersionDefinition)
263 LIEF_ELF_VISITABLE(SymbolVersionAux)
264 LIEF_ELF_VISITABLE(SymbolVersionAuxRequirement)
266 LIEF_ELF_VISITABLE(AndroidIdent)
269 LIEF_ELF_VISITABLE(NoteGnuProperty)
270 LIEF_ELF_VISITABLE(CorePrPsInfo)
271 LIEF_ELF_VISITABLE(CorePrStatus)
277
278 // PE Part
279 // =======
282
285
288
291
294
296 LIEF_PE_VISITABLE(OptionalHeader)
297
299 LIEF_PE_VISITABLE(DataDirectory)
300
303
306
309
311 LIEF_PE_VISITABLE(RelocationEntry)
312
315
317 LIEF_PE_VISITABLE(ExportEntry)
318
321
324
326 LIEF_PE_VISITABLE(CodeViewPDB)
327
330
332 LIEF_PE_VISITABLE(ImportEntry)
333
335 LIEF_PE_VISITABLE(DelayImport)
336
338 LIEF_PE_VISITABLE(DelayImportEntry)
339
341 LIEF_PE_VISITABLE(ResourceNode)
342
344 LIEF_PE_VISITABLE(ResourceData)
345
347 LIEF_PE_VISITABLE(ResourceDirectory)
348
350 LIEF_PE_VISITABLE(ResourcesManager)
351
353 LIEF_PE_VISITABLE(ResourceVersion)
354
356 LIEF_PE_VISITABLE(ResourceStringFileInfo)
357
359 LIEF_PE_VISITABLE(ResourceVarFileInfo)
360
362 LIEF_PE_VISITABLE(ResourceStringTable)
363
365 LIEF_PE_VISITABLE(ResourceAccelerator)
366
368 LIEF_PE_VISITABLE(ResourceIcon)
369
371 LIEF_PE_VISITABLE(ResourceDialogExtended)
372
374 LIEF_PE_VISITABLE(ResourceDialogRegular)
375
378
381
384
386 LIEF_PE_VISITABLE(ContentInfo)
387
390
392 LIEF_PE_VISITABLE(ContentType)
393
395 LIEF_PE_VISITABLE(GenericContent)
396
398 LIEF_PE_VISITABLE(SpcIndirectData)
399
401 LIEF_PE_VISITABLE(GenericType)
402
404 LIEF_PE_VISITABLE(MsCounterSign)
405
407 LIEF_PE_VISITABLE(MsManifestBinaryID)
408
410 LIEF_PE_VISITABLE(MsSpcNestedSignature)
411
413 LIEF_PE_VISITABLE(MsSpcStatementType)
414
416 LIEF_PE_VISITABLE(PKCS9AtSequenceNumber)
417
419 LIEF_PE_VISITABLE(PKCS9CounterSignature)
420
422 LIEF_PE_VISITABLE(PKCS9MessageDigest)
423
425 LIEF_PE_VISITABLE(PKCS9SigningTime)
426
428 LIEF_PE_VISITABLE(SpcSpOpusInfo)
429
431 LIEF_PE_VISITABLE(SpcRelaxedPeMarkerCheck)
432
434 LIEF_PE_VISITABLE(SigningCertificateV2)
435
437 LIEF_PE_VISITABLE(LoadConfiguration)
438
440 LIEF_PE_VISITABLE(CodeIntegrity)
441
444
447
450
451 // MachO part
452 // ==========
455
458
461
464
466 LIEF_MACHO_VISITABLE(SymbolCommand)
467
469 LIEF_MACHO_VISITABLE(SegmentCommand)
470
473
476
479
481 LIEF_MACHO_VISITABLE(DynamicSymbolCommand)
482
484 LIEF_MACHO_VISITABLE(DylinkerCommand)
485
488
490 LIEF_MACHO_VISITABLE(ThreadCommand)
491
494
497
500
502 LIEF_MACHO_VISITABLE(RelocationObject)
503
505 LIEF_MACHO_VISITABLE(RelocationDyld)
506
508 LIEF_MACHO_VISITABLE(RelocationFixup)
509
512
514 LIEF_MACHO_VISITABLE(DyldBindingInfo)
515
517 LIEF_MACHO_VISITABLE(ChainedBindingInfo)
518
520 LIEF_MACHO_VISITABLE(DyldExportsTrie)
521
524
526 LIEF_MACHO_VISITABLE(FunctionStarts)
527
529 LIEF_MACHO_VISITABLE(CodeSignature)
530
533
535 LIEF_MACHO_VISITABLE(DataCodeEntry)
536
538 LIEF_MACHO_VISITABLE(SourceVersion)
539
542
544 LIEF_MACHO_VISITABLE(SegmentSplitInfo)
545
548
551
553 LIEF_MACHO_VISITABLE(DyldEnvironment)
554
556 LIEF_MACHO_VISITABLE(EncryptionInfo)
557
560
562 LIEF_MACHO_VISITABLE(BuildToolVersion)
563
565 LIEF_MACHO_VISITABLE(FilesetCommand)
566
568 LIEF_MACHO_VISITABLE(CodeSignatureDir)
569
571 LIEF_MACHO_VISITABLE(TwoLevelHints)
572
574 LIEF_MACHO_VISITABLE(LinkerOptHint)
575
577 LIEF_MACHO_VISITABLE(UnknownCommand)
578
579 // OAT part
580 // ========
581
584
587
590
593
596
597
598 // DEX part
599 // ========
600
603
606
609
612
615
618
621
624
627
630
631 // VDEX part
632 // =========
633
636
639
640 // ART part
641 // =========
642
645
648
649 template<class T>
650 void dispatch(const T& obj);
651
652
653 private:
654 std::set<size_t> visited_;
655};
656
657
658
659template<typename Arg1, typename... Args>
660void Visitor::operator()(Arg1&& arg1, Args&&... args) {
661 dispatch(std::forward<Arg1>(arg1));
662 operator()(std::forward<Args>(args)... );
663}
664
665template<class T>
666void Visitor::dispatch(const T& obj) {
667 auto hash = reinterpret_cast<size_t>(&obj);
668 if (visited_.find(hash) != std::end(visited_)) {
669 // Already visited
670 return;
671 }
672
673 visited_.insert(hash);
674 visit(obj);
675}
676
677}
678#endif
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:59
Class that represents a function in the binary.
Definition Abstract/Function.hpp:29
Definition Abstract/Header.hpp:40
Class which represents a MachO binary.
Definition MachO/Binary.hpp:88
Class that provides an interface over a binding operation.
Definition BindingInfo.hpp:39
Class that represents a tool's version that was involved in the build of the binary.
Definition BuildToolVersion.hpp:34
Definition BuildVersion.hpp:34
This class represents a symbol binding operation associated with the LC_DYLD_CHAINED_FIXUPS command.
Definition ChainedBindingInfo.hpp:48
Definition CodeSignatureDir.hpp:36
Definition CodeSignature.hpp:37
Interface over an entry in the DataInCode command.
Definition DataCodeEntry.hpp:33
Interface of the LC_DATA_IN_CODE command This command is used to list slices of code sections that co...
Definition DataInCode.hpp:42
This class represents a symbol binding operation associated with the LC_DYLD_INFO bytecode.
Definition DyldBindingInfo.hpp:34
Class that represents a LC_DYLD_ENVIRONMENT command which is used by the Mach-O linker/loader to init...
Definition DyldEnvironment.hpp:34
Class that represents the LC_DYLD_EXPORTS_TRIE command.
Definition DyldExportsTrie.hpp:40
Class which represents a library dependency.
Definition DylibCommand.hpp:34
Class that represents the Mach-O linker, also named loader. Most of the time, DylinkerCommand::name()...
Definition DylinkerCommand.hpp:34
Class that represents the LC_DYSYMTAB command.
Definition DynamicSymbolCommand.hpp:40
Class that represents the LC_ENCRYPTION_INFO / LC_ENCRYPTION_INFO_64 commands.
Definition EncryptionInfo.hpp:35
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:38
Class associated with the LC_FILESET_ENTRY commands.
Definition FilesetCommand.hpp:35
Class which represents the LC_FUNCTION_STARTS command.
Definition FunctionStarts.hpp:39
Class that represents the Mach-O header.
Definition MachO/Header.hpp:38
Class which represents the LC_LINKER_OPTIMIZATION_HINT command.
Definition LinkerOptHint.hpp:37
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:37
Class that represent the LC_MAIN command. This kind of command can be used to determine the entrypoin...
Definition MainCommand.hpp:33
Class that represent the LC_NOTE command.
Definition NoteCommand.hpp:35
Class that represents the LC_RPATH command.
Definition RPathCommand.hpp:36
Class that represents a relocation found in the DyldInfo structure.
Definition RelocationDyld.hpp:33
Class that represents a rebase relocation found in the LC_DYLD_CHAINED_FIXUPS command.
Definition RelocationFixup.hpp:52
Class that represents a relocation presents in the MachO object file (.o). Usually,...
Definition RelocationObject.hpp:38
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
Class that represents the LC_ROUTINE/LC_ROUTINE64 commands. Accodring to the Mach-O loader....
Definition Routine.hpp:38
Class that represents a Mach-O section.
Definition MachO/Section.hpp:46
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:50
Class that represents the LoadCommand::TYPE::SEGMENT_SPLIT_INFO command.
Definition SegmentSplitInfo.hpp:35
Class that represents the MachO LoadCommand::TYPE::SOURCE_VERSION This command is used to provide the...
Definition SourceVersion.hpp:35
Class that represents the SubFramework command. Accodring to the Mach-O loader.h documentation:
Definition SubFramework.hpp:46
Class that represents the LC_SYMTAB command.
Definition SymbolCommand.hpp:35
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
Class that represents the LC_THREAD / LC_UNIXTHREAD commands and that can be used to get the binary e...
Definition ThreadCommand.hpp:41
Class which represents the LC_TWOLEVEL_HINTS command.
Definition TwoLevelHints.hpp:39
Class that represents the UUID command.
Definition UUIDCommand.hpp:35
Generic class when the command is not recognized by LIEF.
Definition UnknownCommand.hpp:32
Class that wraps the LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, ... commands.
Definition VersionMin.hpp:33
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
virtual void operator()()
virtual ~Visitor()
virtual void visit(const Object &)
void dispatch(const T &obj)
Definition Visitor.hpp:666
LIEF namespace.
Definition Abstract/Binary.hpp:40
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