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 - 2025 R. Thomas
2* Copyright 2017 - 2025 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(DynamicEntryRunPath)
110LIEF_ELF_FORWARD(DynamicEntryRpath)
111LIEF_ELF_FORWARD(DynamicEntryFlags)
114LIEF_ELF_FORWARD(SymbolVersion)
115LIEF_ELF_FORWARD(SymbolVersionRequirement)
116LIEF_ELF_FORWARD(SymbolVersionDefinition)
117LIEF_ELF_FORWARD(SymbolVersionAux)
118LIEF_ELF_FORWARD(SymbolVersionAuxRequirement)
120LIEF_ELF_FORWARD(AndroidIdent)
121LIEF_ELF_FORWARD(QNXStack)
122LIEF_ELF_FORWARD(NoteAbi)
123LIEF_ELF_FORWARD(NoteGnuProperty)
124LIEF_ELF_FORWARD(CorePrPsInfo)
125LIEF_ELF_FORWARD(CorePrStatus)
126LIEF_ELF_FORWARD(CoreAuxv)
127LIEF_ELF_FORWARD(CoreSigInfo)
128LIEF_ELF_FORWARD(CoreFile)
129LIEF_ELF_FORWARD(GnuHash)
130LIEF_ELF_FORWARD(SysvHash)
131
132
133// MACHO
134// ===============================
177
178// OAT
179// ===============================
182LIEF_OAT_FORWARD(DexFile)
183LIEF_OAT_FORWARD(Method)
184LIEF_OAT_FORWARD(Class)
185
186// DEX
187// ===============================
189LIEF_DEX_FORWARD(Field)
190LIEF_DEX_FORWARD(Method)
192LIEF_DEX_FORWARD(Class)
193LIEF_DEX_FORWARD(CodeInfo)
195LIEF_DEX_FORWARD(Prototype)
196LIEF_DEX_FORWARD(MapItem)
197LIEF_DEX_FORWARD(MapList)
198
199// VDEX
200// ===============================
203
204// ART
205// ===============================
208
209
211 public:
213 virtual ~Visitor();
214
215 virtual void operator()();
216
217 template<typename Arg1, typename... Args>
218 void operator()(Arg1&& arg1, Args&&... args);
219
220 virtual void visit(const Object&);
221
222 // Abstract Part
223 // =============
224
227
228
230
233
236
239
242
247 LIEF_ELF_VISITABLE(DynamicEntry)
248 LIEF_ELF_VISITABLE(DynamicEntryArray)
249 LIEF_ELF_VISITABLE(DynamicEntryLibrary)
250 LIEF_ELF_VISITABLE(DynamicSharedObject)
251 LIEF_ELF_VISITABLE(DynamicEntryRunPath)
252 LIEF_ELF_VISITABLE(DynamicEntryRpath)
253 LIEF_ELF_VISITABLE(DynamicEntryFlags)
256 LIEF_ELF_VISITABLE(SymbolVersion)
257 LIEF_ELF_VISITABLE(SymbolVersionRequirement)
258 LIEF_ELF_VISITABLE(SymbolVersionDefinition)
259 LIEF_ELF_VISITABLE(SymbolVersionAux)
260 LIEF_ELF_VISITABLE(SymbolVersionAuxRequirement)
262 LIEF_ELF_VISITABLE(AndroidIdent)
265 LIEF_ELF_VISITABLE(NoteGnuProperty)
266 LIEF_ELF_VISITABLE(CorePrPsInfo)
267 LIEF_ELF_VISITABLE(CorePrStatus)
273
274 // PE Part
275 // =======
278
281
284
287
290
292 LIEF_PE_VISITABLE(OptionalHeader)
293
295 LIEF_PE_VISITABLE(DataDirectory)
296
299
302
305
307 LIEF_PE_VISITABLE(RelocationEntry)
308
311
313 LIEF_PE_VISITABLE(ExportEntry)
314
317
320
322 LIEF_PE_VISITABLE(CodeViewPDB)
323
326
328 LIEF_PE_VISITABLE(ImportEntry)
329
331 LIEF_PE_VISITABLE(DelayImport)
332
334 LIEF_PE_VISITABLE(DelayImportEntry)
335
337 LIEF_PE_VISITABLE(ResourceNode)
338
340 LIEF_PE_VISITABLE(ResourceData)
341
343 LIEF_PE_VISITABLE(ResourceDirectory)
344
346 LIEF_PE_VISITABLE(ResourcesManager)
347
349 LIEF_PE_VISITABLE(ResourceVersion)
350
352 LIEF_PE_VISITABLE(ResourceStringFileInfo)
353
355 LIEF_PE_VISITABLE(ResourceVarFileInfo)
356
358 LIEF_PE_VISITABLE(ResourceStringTable)
359
361 LIEF_PE_VISITABLE(ResourceAccelerator)
362
364 LIEF_PE_VISITABLE(ResourceIcon)
365
367 LIEF_PE_VISITABLE(ResourceDialogExtended)
368
370 LIEF_PE_VISITABLE(ResourceDialogRegular)
371
374
377
380
382 LIEF_PE_VISITABLE(ContentInfo)
383
386
388 LIEF_PE_VISITABLE(ContentType)
389
391 LIEF_PE_VISITABLE(GenericContent)
392
394 LIEF_PE_VISITABLE(SpcIndirectData)
395
397 LIEF_PE_VISITABLE(GenericType)
398
400 LIEF_PE_VISITABLE(MsCounterSign)
401
403 LIEF_PE_VISITABLE(MsManifestBinaryID)
404
406 LIEF_PE_VISITABLE(MsSpcNestedSignature)
407
409 LIEF_PE_VISITABLE(MsSpcStatementType)
410
412 LIEF_PE_VISITABLE(PKCS9AtSequenceNumber)
413
415 LIEF_PE_VISITABLE(PKCS9CounterSignature)
416
418 LIEF_PE_VISITABLE(PKCS9MessageDigest)
419
421 LIEF_PE_VISITABLE(PKCS9SigningTime)
422
424 LIEF_PE_VISITABLE(SpcSpOpusInfo)
425
427 LIEF_PE_VISITABLE(SpcRelaxedPeMarkerCheck)
428
430 LIEF_PE_VISITABLE(SigningCertificateV2)
431
433 LIEF_PE_VISITABLE(LoadConfiguration)
434
436 LIEF_PE_VISITABLE(CodeIntegrity)
437
440
443
446
447 // MachO part
448 // ==========
451
454
457
460
462 LIEF_MACHO_VISITABLE(SymbolCommand)
463
465 LIEF_MACHO_VISITABLE(SegmentCommand)
466
469
472
475
477 LIEF_MACHO_VISITABLE(DynamicSymbolCommand)
478
480 LIEF_MACHO_VISITABLE(DylinkerCommand)
481
484
486 LIEF_MACHO_VISITABLE(ThreadCommand)
487
490
493
496
498 LIEF_MACHO_VISITABLE(RelocationObject)
499
501 LIEF_MACHO_VISITABLE(RelocationDyld)
502
504 LIEF_MACHO_VISITABLE(RelocationFixup)
505
508
510 LIEF_MACHO_VISITABLE(DyldBindingInfo)
511
513 LIEF_MACHO_VISITABLE(ChainedBindingInfo)
514
516 LIEF_MACHO_VISITABLE(DyldExportsTrie)
517
520
522 LIEF_MACHO_VISITABLE(FunctionStarts)
523
525 LIEF_MACHO_VISITABLE(CodeSignature)
526
529
531 LIEF_MACHO_VISITABLE(DataCodeEntry)
532
534 LIEF_MACHO_VISITABLE(SourceVersion)
535
538
540 LIEF_MACHO_VISITABLE(SegmentSplitInfo)
541
544
547
549 LIEF_MACHO_VISITABLE(DyldEnvironment)
550
552 LIEF_MACHO_VISITABLE(EncryptionInfo)
553
556
558 LIEF_MACHO_VISITABLE(BuildToolVersion)
559
561 LIEF_MACHO_VISITABLE(FilesetCommand)
562
564 LIEF_MACHO_VISITABLE(CodeSignatureDir)
565
567 LIEF_MACHO_VISITABLE(TwoLevelHints)
568
570 LIEF_MACHO_VISITABLE(LinkerOptHint)
571
573 LIEF_MACHO_VISITABLE(UnknownCommand)
574
575 // OAT part
576 // ========
577
580
583
586
589
592
593
594 // DEX part
595 // ========
596
599
602
605
608
611
614
617
620
623
626
627 // VDEX part
628 // =========
629
632
635
636 // ART part
637 // =========
638
641
644
645 template<class T>
646 void dispatch(const T& obj);
647
648
649 private:
650 std::set<size_t> visited_;
651};
652
653
654
655template<typename Arg1, typename... Args>
656void Visitor::operator()(Arg1&& arg1, Args&&... args) {
657 dispatch(std::forward<Arg1>(arg1));
658 operator()(std::forward<Args>(args)... );
659}
660
661template<class T>
662void Visitor::dispatch(const T& obj) {
663 auto hash = reinterpret_cast<size_t>(&obj);
664 if (visited_.find(hash) != std::end(visited_)) {
665 // Already visited
666 return;
667 }
668
669 visited_.insert(hash);
670 visit(obj);
671}
672
673}
674#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:53
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:662
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