LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
Visitor.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_VISITOR_H
16#define LIEF_VISITOR_H
17#include <cstddef>
18#include <set>
19#include <utility>
20
21#include "LIEF/visibility.h"
23
24
25namespace LIEF {
26// Forward declarations
27// ====================
28class Object;
35
36// PE
37// ===============================
39LIEF_PE_FORWARD(DelayImport)
40LIEF_PE_FORWARD(DelayImportEntry)
41LIEF_PE_FORWARD(DosHeader)
43LIEF_PE_FORWARD(OptionalHeader)
44LIEF_PE_FORWARD(RichHeader)
45LIEF_PE_FORWARD(RichEntry)
46LIEF_PE_FORWARD(DataDirectory)
49LIEF_PE_FORWARD(RelocationEntry)
50LIEF_PE_FORWARD(Export)
51LIEF_PE_FORWARD(ExportEntry)
53LIEF_PE_FORWARD(Debug)
54LIEF_PE_FORWARD(CodeView)
55LIEF_PE_FORWARD(CodeViewPDB)
56LIEF_PE_FORWARD(Import)
57LIEF_PE_FORWARD(ImportEntry)
58LIEF_PE_FORWARD(ResourceNode)
59LIEF_PE_FORWARD(ResourceData)
60LIEF_PE_FORWARD(ResourceDirectory)
61LIEF_PE_FORWARD(ResourcesManager)
62LIEF_PE_FORWARD(ResourceVersion)
63LIEF_PE_FORWARD(ResourceStringFileInfo)
64LIEF_PE_FORWARD(ResourceVarFileInfo)
65LIEF_PE_FORWARD(ResourceIcon)
66LIEF_PE_FORWARD(ResourceDialogExtended)
67LIEF_PE_FORWARD(ResourceDialogRegular)
68LIEF_PE_FORWARD(ResourceStringTable)
69LIEF_PE_FORWARD(ResourceAccelerator)
70
71LIEF_PE_FORWARD(Signature)
73LIEF_PE_FORWARD(SignerInfo)
74LIEF_PE_FORWARD(ContentInfo)
75LIEF_PE_FORWARD(GenericContent)
76LIEF_PE_FORWARD(SpcIndirectData)
77LIEF_PE_FORWARD(Attribute)
78LIEF_PE_FORWARD(ContentType)
79LIEF_PE_FORWARD(GenericType)
80LIEF_PE_FORWARD(MsCounterSign)
81LIEF_PE_FORWARD(MsSpcNestedSignature)
82LIEF_PE_FORWARD(MsSpcStatementType)
83LIEF_PE_FORWARD(MsManifestBinaryID)
84LIEF_PE_FORWARD(PKCS9AtSequenceNumber)
85LIEF_PE_FORWARD(PKCS9CounterSignature)
86LIEF_PE_FORWARD(PKCS9MessageDigest)
87LIEF_PE_FORWARD(PKCS9SigningTime)
88LIEF_PE_FORWARD(SpcSpOpusInfo)
89LIEF_PE_FORWARD(SigningCertificateV2)
90LIEF_PE_FORWARD(SpcRelaxedPeMarkerCheck)
91
92LIEF_PE_FORWARD(CodeIntegrity)
93LIEF_PE_FORWARD(LoadConfiguration)
95LIEF_PE_FORWARD(PogoEntry)
96LIEF_PE_FORWARD(Repro)
97
98// ELF
99// ==================================
103LIEF_ELF_FORWARD(Segment)
104LIEF_ELF_FORWARD(DynamicEntry)
105LIEF_ELF_FORWARD(DynamicEntryArray)
106LIEF_ELF_FORWARD(DynamicEntryLibrary)
107LIEF_ELF_FORWARD(DynamicSharedObject)
108LIEF_ELF_FORWARD(DynamicEntryAuxiliary)
109LIEF_ELF_FORWARD(DynamicEntryFilter)
110LIEF_ELF_FORWARD(DynamicEntryRunPath)
111LIEF_ELF_FORWARD(DynamicEntryRpath)
112LIEF_ELF_FORWARD(DynamicEntryFlags)
115LIEF_ELF_FORWARD(SymbolVersion)
116LIEF_ELF_FORWARD(SymbolVersionRequirement)
117LIEF_ELF_FORWARD(SymbolVersionDefinition)
118LIEF_ELF_FORWARD(SymbolVersionAux)
119LIEF_ELF_FORWARD(SymbolVersionAuxRequirement)
121LIEF_ELF_FORWARD(AndroidIdent)
122LIEF_ELF_FORWARD(QNXStack)
123LIEF_ELF_FORWARD(NoteAbi)
124LIEF_ELF_FORWARD(NoteGnuProperty)
125LIEF_ELF_FORWARD(CorePrPsInfo)
126LIEF_ELF_FORWARD(CorePrStatus)
127LIEF_ELF_FORWARD(CoreAuxv)
128LIEF_ELF_FORWARD(CoreSigInfo)
129LIEF_ELF_FORWARD(CoreFile)
130LIEF_ELF_FORWARD(GnuHash)
131LIEF_ELF_FORWARD(SysvHash)
132
133
134// MACHO
135// ===============================
178
179// OAT
180// ===============================
183LIEF_OAT_FORWARD(DexFile)
184LIEF_OAT_FORWARD(Method)
185LIEF_OAT_FORWARD(Class)
186
187// DEX
188// ===============================
190LIEF_DEX_FORWARD(Field)
191LIEF_DEX_FORWARD(Method)
193LIEF_DEX_FORWARD(Class)
194LIEF_DEX_FORWARD(CodeInfo)
196LIEF_DEX_FORWARD(Prototype)
197LIEF_DEX_FORWARD(MapItem)
198LIEF_DEX_FORWARD(MapList)
199
200// VDEX
201// ===============================
204
205// ART
206// ===============================
209
210
212 public:
214 virtual ~Visitor();
215
216 virtual void operator()();
217
218 template<typename Arg1, typename... Args>
219 void operator()(Arg1&& arg1, Args&&... args);
220
221 virtual void visit(const Object&);
222
223 // Abstract Part
224 // =============
225
228
229
231
234
237
240
243
248 LIEF_ELF_VISITABLE(DynamicEntry)
249 LIEF_ELF_VISITABLE(DynamicEntryArray)
250 LIEF_ELF_VISITABLE(DynamicEntryLibrary)
251 LIEF_ELF_VISITABLE(DynamicSharedObject)
252 LIEF_ELF_VISITABLE(DynamicEntryAuxiliary)
253 LIEF_ELF_VISITABLE(DynamicEntryFilter)
254 LIEF_ELF_VISITABLE(DynamicEntryRunPath)
255 LIEF_ELF_VISITABLE(DynamicEntryRpath)
256 LIEF_ELF_VISITABLE(DynamicEntryFlags)
259 LIEF_ELF_VISITABLE(SymbolVersion)
260 LIEF_ELF_VISITABLE(SymbolVersionRequirement)
261 LIEF_ELF_VISITABLE(SymbolVersionDefinition)
262 LIEF_ELF_VISITABLE(SymbolVersionAux)
263 LIEF_ELF_VISITABLE(SymbolVersionAuxRequirement)
265 LIEF_ELF_VISITABLE(AndroidIdent)
268 LIEF_ELF_VISITABLE(NoteGnuProperty)
269 LIEF_ELF_VISITABLE(CorePrPsInfo)
270 LIEF_ELF_VISITABLE(CorePrStatus)
276
277 // PE Part
278 // =======
281
284
287
290
293
295 LIEF_PE_VISITABLE(OptionalHeader)
296
298 LIEF_PE_VISITABLE(DataDirectory)
299
302
305
308
310 LIEF_PE_VISITABLE(RelocationEntry)
311
314
316 LIEF_PE_VISITABLE(ExportEntry)
317
320
323
325 LIEF_PE_VISITABLE(CodeViewPDB)
326
329
331 LIEF_PE_VISITABLE(ImportEntry)
332
334 LIEF_PE_VISITABLE(DelayImport)
335
337 LIEF_PE_VISITABLE(DelayImportEntry)
338
340 LIEF_PE_VISITABLE(ResourceNode)
341
343 LIEF_PE_VISITABLE(ResourceData)
344
346 LIEF_PE_VISITABLE(ResourceDirectory)
347
349 LIEF_PE_VISITABLE(ResourcesManager)
350
352 LIEF_PE_VISITABLE(ResourceVersion)
353
355 LIEF_PE_VISITABLE(ResourceStringFileInfo)
356
358 LIEF_PE_VISITABLE(ResourceVarFileInfo)
359
361 LIEF_PE_VISITABLE(ResourceStringTable)
362
364 LIEF_PE_VISITABLE(ResourceAccelerator)
365
367 LIEF_PE_VISITABLE(ResourceIcon)
368
370 LIEF_PE_VISITABLE(ResourceDialogExtended)
371
373 LIEF_PE_VISITABLE(ResourceDialogRegular)
374
377
380
383
385 LIEF_PE_VISITABLE(ContentInfo)
386
389
391 LIEF_PE_VISITABLE(ContentType)
392
394 LIEF_PE_VISITABLE(GenericContent)
395
397 LIEF_PE_VISITABLE(SpcIndirectData)
398
400 LIEF_PE_VISITABLE(GenericType)
401
403 LIEF_PE_VISITABLE(MsCounterSign)
404
406 LIEF_PE_VISITABLE(MsManifestBinaryID)
407
409 LIEF_PE_VISITABLE(MsSpcNestedSignature)
410
412 LIEF_PE_VISITABLE(MsSpcStatementType)
413
415 LIEF_PE_VISITABLE(PKCS9AtSequenceNumber)
416
418 LIEF_PE_VISITABLE(PKCS9CounterSignature)
419
421 LIEF_PE_VISITABLE(PKCS9MessageDigest)
422
424 LIEF_PE_VISITABLE(PKCS9SigningTime)
425
427 LIEF_PE_VISITABLE(SpcSpOpusInfo)
428
430 LIEF_PE_VISITABLE(SpcRelaxedPeMarkerCheck)
431
433 LIEF_PE_VISITABLE(SigningCertificateV2)
434
436 LIEF_PE_VISITABLE(LoadConfiguration)
437
439 LIEF_PE_VISITABLE(CodeIntegrity)
440
443
446
449
450 // MachO part
451 // ==========
454
457
460
463
465 LIEF_MACHO_VISITABLE(SymbolCommand)
466
468 LIEF_MACHO_VISITABLE(SegmentCommand)
469
472
475
478
480 LIEF_MACHO_VISITABLE(DynamicSymbolCommand)
481
483 LIEF_MACHO_VISITABLE(DylinkerCommand)
484
487
489 LIEF_MACHO_VISITABLE(ThreadCommand)
490
493
496
499
501 LIEF_MACHO_VISITABLE(RelocationObject)
502
504 LIEF_MACHO_VISITABLE(RelocationDyld)
505
507 LIEF_MACHO_VISITABLE(RelocationFixup)
508
511
513 LIEF_MACHO_VISITABLE(DyldBindingInfo)
514
516 LIEF_MACHO_VISITABLE(ChainedBindingInfo)
517
519 LIEF_MACHO_VISITABLE(DyldExportsTrie)
520
523
525 LIEF_MACHO_VISITABLE(FunctionStarts)
526
528 LIEF_MACHO_VISITABLE(CodeSignature)
529
532
534 LIEF_MACHO_VISITABLE(DataCodeEntry)
535
537 LIEF_MACHO_VISITABLE(SourceVersion)
538
541
543 LIEF_MACHO_VISITABLE(SegmentSplitInfo)
544
547
550
552 LIEF_MACHO_VISITABLE(DyldEnvironment)
553
555 LIEF_MACHO_VISITABLE(EncryptionInfo)
556
559
561 LIEF_MACHO_VISITABLE(BuildToolVersion)
562
564 LIEF_MACHO_VISITABLE(FilesetCommand)
565
567 LIEF_MACHO_VISITABLE(CodeSignatureDir)
568
570 LIEF_MACHO_VISITABLE(TwoLevelHints)
571
573 LIEF_MACHO_VISITABLE(LinkerOptHint)
574
576 LIEF_MACHO_VISITABLE(UnknownCommand)
577
578 // OAT part
579 // ========
580
583
586
589
592
595
596
597 // DEX part
598 // ========
599
602
605
608
611
614
617
620
623
626
629
630 // VDEX part
631 // =========
632
635
638
639 // ART part
640 // =========
641
644
647
648 template<class T>
649 void dispatch(const T& obj);
650
651
652 private:
653 std::set<size_t> visited_;
654};
655
656
657template<typename Arg1, typename... Args>
658void Visitor::operator()(Arg1&& arg1, Args&&... args) {
659 dispatch(std::forward<Arg1>(arg1));
660 operator()(std::forward<Args>(args)...);
661}
662
663template<class T>
664void Visitor::dispatch(const T& obj) {
665 auto hash = reinterpret_cast<size_t>(&obj);
666 if (visited_.find(hash) != std::end(visited_)) {
667 // Already visited
668 return;
669 }
670
671 visited_.insert(hash);
672 visit(obj);
673}
674
675}
676#endif
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:60
Class that represents a function in the binary.
Definition Abstract/Function.hpp:29
Definition Abstract/Header.hpp:39
Class which represents a MachO binary.
Definition MachO/Binary.hpp:93
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:33
Definition BuildVersion.hpp:34
This class represents a symbol binding operation associated with the LC_DYLD_CHAINED_FIXUPS command.
Definition ChainedBindingInfo.hpp:47
Definition CodeSignatureDir.hpp:37
Definition CodeSignature.hpp:38
Interface over an entry in the DataInCode command.
Definition DataCodeEntry.hpp:32
Interface of the LC_DATA_IN_CODE command This command is used to list slices of code sections that co...
Definition DataInCode.hpp:43
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:35
Class that represents the LC_DYLD_EXPORTS_TRIE command.
Definition DyldExportsTrie.hpp:42
Class which represents a library dependency.
Definition DylibCommand.hpp:35
Class that represents the Mach-O linker, also named loader. Most of the time, DylinkerCommand::name()...
Definition DylinkerCommand.hpp:35
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:37
Class associated with the LC_FILESET_ENTRY commands.
Definition FilesetCommand.hpp:36
Class which represents the LC_FUNCTION_STARTS command.
Definition FunctionStarts.hpp:40
Class that represents the Mach-O header.
Definition MachO/Header.hpp:37
Class which represents the LC_LINKER_OPTIMIZATION_HINT command.
Definition LinkerOptHint.hpp:38
Base class for the Mach-O load commands.
Definition LoadCommand.hpp:37
Class that represents the LC_MAIN command. This kind of command can be used to determine the entrypoi...
Definition MainCommand.hpp:33
Class that represents the LC_NOTE command.
Definition NoteCommand.hpp:38
Class that represents the LC_RPATH command.
Definition RPathCommand.hpp:38
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:54
Class that represents a relocation present in the MachO object file (.o). Usually,...
Definition RelocationObject.hpp:38
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:39
Class that represents the LC_ROUTINE/LC_ROUTINE64 commands. According to the Mach-O loader....
Definition Routine.hpp:38
Class that represents a Mach-O section.
Definition MachO/Section.hpp:48
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:53
Class that represents the LoadCommand::TYPE::SEGMENT_SPLIT_INFO command.
Definition SegmentSplitInfo.hpp:36
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. According to the Mach-O loader.h documentation:
Definition SubFramework.hpp:48
Class that represents the LC_SYMTAB command.
Definition SymbolCommand.hpp:36
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:46
Class that represents the LC_THREAD / LC_UNIXTHREAD commands and that can be used to get the binary e...
Definition ThreadCommand.hpp:43
Class which represents the LC_TWOLEVEL_HINTS command.
Definition TwoLevelHints.hpp:40
Class that represents the UUID command.
Definition UUIDCommand.hpp:35
Generic class when the command is not recognized by LIEF.
Definition UnknownCommand.hpp:33
Class that wraps the LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, ... commands.
Definition VersionMin.hpp:34
Definition Object.hpp:24
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:26
Class which represents an abstracted section.
Definition Abstract/Section.hpp:30
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:29
virtual void operator()()
virtual ~Visitor()
virtual void visit(const Object &)
void dispatch(const T &obj)
Definition Visitor.hpp:664
LIEF namespace.
Definition Abstract/Binary.hpp:41
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:44
#define LIEF_ABSTRACT_FORWARD(OBJ)
Definition visitor_macros.hpp:125
#define LIEF_VDEX_FORWARD(OBJ)
Definition visitor_macros.hpp:97
#define LIEF_MACHO_VISITABLE(OBJ)
Definition visitor_macros.hpp:56
#define LIEF_ART_FORWARD(OBJ)
Definition visitor_macros.hpp:112
#define LIEF_ABSTRACT_VISITABLE(OBJ)
Definition visitor_macros.hpp:127
#define LIEF_DEX_VISITABLE(OBJ)
Definition visitor_macros.hpp:86
#define LIEF_OAT_FORWARD(OBJ)
Definition visitor_macros.hpp:67
#define LIEF_PE_VISITABLE(OBJ)
Definition visitor_macros.hpp:27
#define LIEF_VDEX_VISITABLE(OBJ)
Definition visitor_macros.hpp:101
#define LIEF_DEX_FORWARD(OBJ)
Definition visitor_macros.hpp:82
#define LIEF_MACHO_FORWARD(OBJ)
Definition visitor_macros.hpp:52
#define LIEF_ART_VISITABLE(OBJ)
Definition visitor_macros.hpp:116
#define LIEF_ELF_VISITABLE(OBJ)
Definition visitor_macros.hpp:42
#define LIEF_PE_FORWARD(OBJ)
Definition visitor_macros.hpp:22
#define LIEF_OAT_VISITABLE(OBJ)
Definition visitor_macros.hpp:71
#define LIEF_ELF_FORWARD(OBJ)
Definition visitor_macros.hpp:38