LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
Visitor.hpp
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"
24#include "LIEF/visitor_macros.hpp"
25
26
27namespace LIEF {
28// Forward declarations
29// ====================
30class Object;
31LIEF_ABSTRACT_FORWARD(Binary)
32LIEF_ABSTRACT_FORWARD(Header)
33LIEF_ABSTRACT_FORWARD(Section)
34LIEF_ABSTRACT_FORWARD(Symbol)
35LIEF_ABSTRACT_FORWARD(Relocation)
36LIEF_ABSTRACT_FORWARD(Function)
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)
54LIEF_PE_FORWARD(TLS)
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)
77LIEF_PE_FORWARD(x509)
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)
85//LIEF_PE_FORWARD(MsCounterSign)
86LIEF_PE_FORWARD(MsSpcNestedSignature)
87LIEF_PE_FORWARD(MsSpcStatementType)
88LIEF_PE_FORWARD(PKCS9AtSequenceNumber)
89LIEF_PE_FORWARD(PKCS9CounterSignature)
90LIEF_PE_FORWARD(PKCS9MessageDigest)
91LIEF_PE_FORWARD(PKCS9SigningTime)
92LIEF_PE_FORWARD(SpcSpOpusInfo)
93
94LIEF_PE_FORWARD(CodeIntegrity)
95LIEF_PE_FORWARD(LoadConfiguration)
96LIEF_PE_FORWARD(LoadConfigurationV0)
97LIEF_PE_FORWARD(LoadConfigurationV1)
98LIEF_PE_FORWARD(LoadConfigurationV10)
99LIEF_PE_FORWARD(LoadConfigurationV11)
100LIEF_PE_FORWARD(LoadConfigurationV2)
101LIEF_PE_FORWARD(LoadConfigurationV3)
102LIEF_PE_FORWARD(LoadConfigurationV4)
103LIEF_PE_FORWARD(LoadConfigurationV5)
104LIEF_PE_FORWARD(LoadConfigurationV6)
105LIEF_PE_FORWARD(LoadConfigurationV7)
106LIEF_PE_FORWARD(LoadConfigurationV8)
107LIEF_PE_FORWARD(LoadConfigurationV9)
108LIEF_PE_FORWARD(Pogo)
109LIEF_PE_FORWARD(PogoEntry)
110LIEF_PE_FORWARD(Repro)
111
112// ELF
113// ==================================
114LIEF_ELF_FORWARD(Binary)
115LIEF_ELF_FORWARD(Header)
116LIEF_ELF_FORWARD(Section)
117LIEF_ELF_FORWARD(Segment)
118LIEF_ELF_FORWARD(DynamicEntry)
119LIEF_ELF_FORWARD(DynamicEntryArray)
120LIEF_ELF_FORWARD(DynamicEntryLibrary)
121LIEF_ELF_FORWARD(DynamicSharedObject)
122LIEF_ELF_FORWARD(DynamicEntryRunPath)
123LIEF_ELF_FORWARD(DynamicEntryRpath)
124LIEF_ELF_FORWARD(DynamicEntryFlags)
125LIEF_ELF_FORWARD(Symbol)
126LIEF_ELF_FORWARD(Relocation)
127LIEF_ELF_FORWARD(SymbolVersion)
128LIEF_ELF_FORWARD(SymbolVersionRequirement)
129LIEF_ELF_FORWARD(SymbolVersionDefinition)
130LIEF_ELF_FORWARD(SymbolVersionAux)
131LIEF_ELF_FORWARD(SymbolVersionAuxRequirement)
132LIEF_ELF_FORWARD(Note)
133LIEF_ELF_FORWARD(AndroidIdent)
134LIEF_ELF_FORWARD(NoteAbi)
135LIEF_ELF_FORWARD(NoteGnuProperty)
136LIEF_ELF_FORWARD(CorePrPsInfo)
137LIEF_ELF_FORWARD(CorePrStatus)
138LIEF_ELF_FORWARD(CoreAuxv)
139LIEF_ELF_FORWARD(CoreSigInfo)
140LIEF_ELF_FORWARD(CoreFile)
141LIEF_ELF_FORWARD(GnuHash)
142LIEF_ELF_FORWARD(SysvHash)
143
144
145// MACHO
146// ===============================
147LIEF_MACHO_FORWARD(Binary)
148LIEF_MACHO_FORWARD(Header)
149LIEF_MACHO_FORWARD(LoadCommand)
150LIEF_MACHO_FORWARD(UUIDCommand)
151LIEF_MACHO_FORWARD(SymbolCommand)
152LIEF_MACHO_FORWARD(SegmentCommand)
153LIEF_MACHO_FORWARD(Section)
154LIEF_MACHO_FORWARD(MainCommand)
155LIEF_MACHO_FORWARD(DynamicSymbolCommand)
156LIEF_MACHO_FORWARD(DylinkerCommand)
157LIEF_MACHO_FORWARD(DylibCommand)
158LIEF_MACHO_FORWARD(ThreadCommand)
159LIEF_MACHO_FORWARD(RPathCommand)
160LIEF_MACHO_FORWARD(Symbol)
161LIEF_MACHO_FORWARD(Relocation)
162LIEF_MACHO_FORWARD(RelocationObject)
163LIEF_MACHO_FORWARD(RelocationDyld)
164LIEF_MACHO_FORWARD(RelocationFixup)
165LIEF_MACHO_FORWARD(BindingInfo)
166LIEF_MACHO_FORWARD(DyldBindingInfo)
167LIEF_MACHO_FORWARD(DyldExportsTrie)
168LIEF_MACHO_FORWARD(ChainedBindingInfo)
169LIEF_MACHO_FORWARD(ExportInfo)
170LIEF_MACHO_FORWARD(FunctionStarts)
171LIEF_MACHO_FORWARD(CodeSignature)
172LIEF_MACHO_FORWARD(DataInCode)
173LIEF_MACHO_FORWARD(DataCodeEntry)
174LIEF_MACHO_FORWARD(SourceVersion)
175LIEF_MACHO_FORWARD(VersionMin)
176LIEF_MACHO_FORWARD(SegmentSplitInfo)
177LIEF_MACHO_FORWARD(SubFramework)
178LIEF_MACHO_FORWARD(DyldEnvironment)
179LIEF_MACHO_FORWARD(EncryptionInfo)
180LIEF_MACHO_FORWARD(BuildVersion)
181LIEF_MACHO_FORWARD(BuildToolVersion)
182LIEF_MACHO_FORWARD(FilesetCommand)
183LIEF_MACHO_FORWARD(TwoLevelHints)
184LIEF_MACHO_FORWARD(CodeSignatureDir)
185LIEF_MACHO_FORWARD(LinkerOptHint)
186
187// OAT
188// ===============================
189LIEF_OAT_FORWARD(Binary)
190LIEF_OAT_FORWARD(Header)
191LIEF_OAT_FORWARD(DexFile)
192LIEF_OAT_FORWARD(Method)
193LIEF_OAT_FORWARD(Class)
194
195// DEX
196// ===============================
197LIEF_DEX_FORWARD(File)
198LIEF_DEX_FORWARD(Field)
199LIEF_DEX_FORWARD(Method)
200LIEF_DEX_FORWARD(Header)
201LIEF_DEX_FORWARD(Class)
202LIEF_DEX_FORWARD(CodeInfo)
203LIEF_DEX_FORWARD(Type)
204LIEF_DEX_FORWARD(Prototype)
205LIEF_DEX_FORWARD(MapItem)
206LIEF_DEX_FORWARD(MapList)
207
208// VDEX
209// ===============================
210LIEF_VDEX_FORWARD(File)
211LIEF_VDEX_FORWARD(Header)
212
213// ART
214// ===============================
215LIEF_ART_FORWARD(File)
216LIEF_ART_FORWARD(Header)
217
218
219class LIEF_API Visitor {
220 public:
221 Visitor();
222 virtual ~Visitor();
223
224 virtual void operator()();
225
226 template<typename Arg1, typename... Args>
227 void operator()(Arg1&& arg1, Args&&... args);
228
229 virtual void visit(const Object&);
230
231 // Abstract Part
232 // =============
233
235 LIEF_ABSTRACT_VISITABLE(Binary)
236
237
238 LIEF_ABSTRACT_VISITABLE(Header)
239
241 LIEF_ABSTRACT_VISITABLE(Section)
242
244 LIEF_ABSTRACT_VISITABLE(Symbol)
245
247 LIEF_ABSTRACT_VISITABLE(Relocation)
248
250 LIEF_ABSTRACT_VISITABLE(Function)
251
252 LIEF_ELF_VISITABLE(Binary)
253 LIEF_ELF_VISITABLE(Header)
254 LIEF_ELF_VISITABLE(Section)
255 LIEF_ELF_VISITABLE(Segment)
256 LIEF_ELF_VISITABLE(DynamicEntry)
257 LIEF_ELF_VISITABLE(DynamicEntryArray)
258 LIEF_ELF_VISITABLE(DynamicEntryLibrary)
259 LIEF_ELF_VISITABLE(DynamicSharedObject)
260 LIEF_ELF_VISITABLE(DynamicEntryRunPath)
261 LIEF_ELF_VISITABLE(DynamicEntryRpath)
262 LIEF_ELF_VISITABLE(DynamicEntryFlags)
263 LIEF_ELF_VISITABLE(Symbol)
264 LIEF_ELF_VISITABLE(Relocation)
265 LIEF_ELF_VISITABLE(SymbolVersion)
266 LIEF_ELF_VISITABLE(SymbolVersionRequirement)
267 LIEF_ELF_VISITABLE(SymbolVersionDefinition)
268 LIEF_ELF_VISITABLE(SymbolVersionAux)
269 LIEF_ELF_VISITABLE(SymbolVersionAuxRequirement)
270 LIEF_ELF_VISITABLE(Note)
271 LIEF_ELF_VISITABLE(AndroidIdent)
272 LIEF_ELF_VISITABLE(NoteAbi)
273 LIEF_ELF_VISITABLE(NoteGnuProperty)
274 LIEF_ELF_VISITABLE(CorePrPsInfo)
275 LIEF_ELF_VISITABLE(CorePrStatus)
276 LIEF_ELF_VISITABLE(CoreAuxv)
277 LIEF_ELF_VISITABLE(CoreSigInfo)
278 LIEF_ELF_VISITABLE(CoreFile)
279 LIEF_ELF_VISITABLE(GnuHash)
280 LIEF_ELF_VISITABLE(SysvHash)
281
282 // PE Part
283 // =======
285 LIEF_PE_VISITABLE(Binary)
286
288 LIEF_PE_VISITABLE(DosHeader)
289
291 LIEF_PE_VISITABLE(RichHeader)
292
294 LIEF_PE_VISITABLE(RichEntry)
295
297 LIEF_PE_VISITABLE(Header)
298
300 LIEF_PE_VISITABLE(OptionalHeader)
301
303 LIEF_PE_VISITABLE(DataDirectory)
304
306 LIEF_PE_VISITABLE(TLS)
307
309 LIEF_PE_VISITABLE(Symbol)
310
312 LIEF_PE_VISITABLE(Section)
313
315 LIEF_PE_VISITABLE(Relocation)
316
318 LIEF_PE_VISITABLE(RelocationEntry)
319
321 LIEF_PE_VISITABLE(Export)
322
324 LIEF_PE_VISITABLE(ExportEntry)
325
327 LIEF_PE_VISITABLE(Debug)
328
330 LIEF_PE_VISITABLE(CodeView)
331
333 LIEF_PE_VISITABLE(CodeViewPDB)
334
336 LIEF_PE_VISITABLE(Import)
337
339 LIEF_PE_VISITABLE(ImportEntry)
340
342 LIEF_PE_VISITABLE(DelayImport)
343
345 LIEF_PE_VISITABLE(DelayImportEntry)
346
348 LIEF_PE_VISITABLE(ResourceNode)
349
351 LIEF_PE_VISITABLE(ResourceData)
352
354 LIEF_PE_VISITABLE(ResourceDirectory)
355
357 LIEF_PE_VISITABLE(ResourcesManager)
358
360 LIEF_PE_VISITABLE(ResourceVersion)
361
363 LIEF_PE_VISITABLE(ResourceStringFileInfo)
364
366 LIEF_PE_VISITABLE(ResourceFixedFileInfo)
367
369 LIEF_PE_VISITABLE(ResourceVarFileInfo)
370
372 LIEF_PE_VISITABLE(ResourceStringTable)
373
375 LIEF_PE_VISITABLE(ResourceAccelerator)
376
378 LIEF_PE_VISITABLE(LangCodeItem)
379
381 LIEF_PE_VISITABLE(ResourceIcon)
382
384 LIEF_PE_VISITABLE(ResourceDialog)
385
387 LIEF_PE_VISITABLE(ResourceDialogItem)
388
390 LIEF_PE_VISITABLE(Signature)
391
393 LIEF_PE_VISITABLE(x509)
394
396 LIEF_PE_VISITABLE(SignerInfo)
397
399 LIEF_PE_VISITABLE(ContentInfo)
400
402 LIEF_PE_VISITABLE(Attribute)
403
405 LIEF_PE_VISITABLE(ContentType)
406
408 LIEF_PE_VISITABLE(GenericContent)
409
411 LIEF_PE_VISITABLE(SpcIndirectData)
412
414 LIEF_PE_VISITABLE(GenericType)
415
417 //LIEF_PE_VISITABLE(MsCounterSign)
418
420 LIEF_PE_VISITABLE(MsSpcNestedSignature)
421
423 LIEF_PE_VISITABLE(MsSpcStatementType)
424
426 LIEF_PE_VISITABLE(PKCS9AtSequenceNumber)
427
429 LIEF_PE_VISITABLE(PKCS9CounterSignature)
430
432 LIEF_PE_VISITABLE(PKCS9MessageDigest)
433
435 LIEF_PE_VISITABLE(PKCS9SigningTime)
436
438 LIEF_PE_VISITABLE(SpcSpOpusInfo)
439
441 LIEF_PE_VISITABLE(LoadConfiguration)
442
444 LIEF_PE_VISITABLE(LoadConfigurationV0)
445
447 LIEF_PE_VISITABLE(LoadConfigurationV1)
448
450 LIEF_PE_VISITABLE(LoadConfigurationV2)
451
453 LIEF_PE_VISITABLE(LoadConfigurationV3)
454
456 LIEF_PE_VISITABLE(LoadConfigurationV4)
457
459 LIEF_PE_VISITABLE(LoadConfigurationV5)
460
462 LIEF_PE_VISITABLE(LoadConfigurationV6)
463
465 LIEF_PE_VISITABLE(LoadConfigurationV7)
466
468 LIEF_PE_VISITABLE(LoadConfigurationV8)
469
471 LIEF_PE_VISITABLE(LoadConfigurationV9)
472
474 LIEF_PE_VISITABLE(LoadConfigurationV10)
475
477 LIEF_PE_VISITABLE(LoadConfigurationV11)
478
480 LIEF_PE_VISITABLE(CodeIntegrity)
481
483 LIEF_PE_VISITABLE(Pogo)
484
486 LIEF_PE_VISITABLE(PogoEntry)
487
489 LIEF_PE_VISITABLE(Repro)
490
491 // MachO part
492 // ==========
494 LIEF_MACHO_VISITABLE(Binary)
495
497 LIEF_MACHO_VISITABLE(Header)
498
500 LIEF_MACHO_VISITABLE(LoadCommand)
501
503 LIEF_MACHO_VISITABLE(UUIDCommand)
504
506 LIEF_MACHO_VISITABLE(SymbolCommand)
507
509 LIEF_MACHO_VISITABLE(SegmentCommand)
510
512 LIEF_MACHO_VISITABLE(Section)
513
515 LIEF_MACHO_VISITABLE(MainCommand)
516
518 LIEF_MACHO_VISITABLE(DynamicSymbolCommand)
519
521 LIEF_MACHO_VISITABLE(DylinkerCommand)
522
524 LIEF_MACHO_VISITABLE(DylibCommand)
525
527 LIEF_MACHO_VISITABLE(ThreadCommand)
528
530 LIEF_MACHO_VISITABLE(RPathCommand)
531
533 LIEF_MACHO_VISITABLE(Symbol)
534
536 LIEF_MACHO_VISITABLE(Relocation)
537
539 LIEF_MACHO_VISITABLE(RelocationObject)
540
542 LIEF_MACHO_VISITABLE(RelocationDyld)
543
545 LIEF_MACHO_VISITABLE(RelocationFixup)
546
548 LIEF_MACHO_VISITABLE(BindingInfo)
549
551 LIEF_MACHO_VISITABLE(DyldBindingInfo)
552
554 LIEF_MACHO_VISITABLE(ChainedBindingInfo)
555
557 LIEF_MACHO_VISITABLE(DyldExportsTrie)
558
560 LIEF_MACHO_VISITABLE(ExportInfo)
561
563 LIEF_MACHO_VISITABLE(FunctionStarts)
564
566 LIEF_MACHO_VISITABLE(CodeSignature)
567
569 LIEF_MACHO_VISITABLE(DataInCode)
570
572 LIEF_MACHO_VISITABLE(DataCodeEntry)
573
575 LIEF_MACHO_VISITABLE(SourceVersion)
576
578 LIEF_MACHO_VISITABLE(VersionMin)
579
581 LIEF_MACHO_VISITABLE(SegmentSplitInfo)
582
584 LIEF_MACHO_VISITABLE(SubFramework)
585
587 LIEF_MACHO_VISITABLE(DyldEnvironment)
588
590 LIEF_MACHO_VISITABLE(EncryptionInfo)
591
593 LIEF_MACHO_VISITABLE(BuildVersion)
594
596 LIEF_MACHO_VISITABLE(BuildToolVersion)
597
599 LIEF_MACHO_VISITABLE(FilesetCommand)
600
602 LIEF_MACHO_VISITABLE(CodeSignatureDir)
603
605 LIEF_MACHO_VISITABLE(TwoLevelHints)
606
608 LIEF_MACHO_VISITABLE(LinkerOptHint)
609
610 // OAT part
611 // ========
612
614 LIEF_OAT_VISITABLE(Binary)
615
617 LIEF_OAT_VISITABLE(Header)
618
620 LIEF_OAT_VISITABLE(DexFile)
621
623 LIEF_OAT_VISITABLE(Class)
624
626 LIEF_OAT_VISITABLE(Method)
627
628
629 // DEX part
630 // ========
631
633 LIEF_DEX_VISITABLE(File)
634
636 LIEF_DEX_VISITABLE(Field)
637
639 LIEF_DEX_VISITABLE(Method)
640
642 LIEF_DEX_VISITABLE(Header)
643
645 LIEF_DEX_VISITABLE(Class)
646
648 LIEF_DEX_VISITABLE(CodeInfo)
649
651 LIEF_DEX_VISITABLE(Type)
652
654 LIEF_DEX_VISITABLE(Prototype)
655
657 LIEF_DEX_VISITABLE(MapList)
658
660 LIEF_DEX_VISITABLE(MapItem)
661
662 // VDEX part
663 // =========
664
666 LIEF_VDEX_VISITABLE(File)
667
669 LIEF_VDEX_VISITABLE(Header)
670
671 // ART part
672 // =========
673
675 LIEF_ART_VISITABLE(File)
676
678 LIEF_ART_VISITABLE(Header)
679
680 template<class T>
681 void dispatch(const T& obj);
682
683
684 private:
685 std::set<size_t> visited_;
686};
687
688
689
690template<typename Arg1, typename... Args>
691void Visitor::operator()(Arg1&& arg1, Args&&... args) {
692 dispatch(std::forward<Arg1>(arg1));
693 operator()(std::forward<Args>(args)... );
694}
695
696template<class T>
697void Visitor::dispatch(const T& obj) {
698 auto hash = reinterpret_cast<size_t>(&obj);
699 if (visited_.find(hash) != std::end(visited_)) {
700 // Already visited
701 return;
702 }
703
704 visited_.insert(hash);
705 visit(obj);
706}
707
708}
709#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:39
Class that represents a function in the binary.
Definition 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:30
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
Definition Visitor.hpp:219
LIEF namespace.
Definition Abstract/Binary.hpp:32