summaryrefslogtreecommitdiff
path: root/plugins/rtti/rtti.h
blob: b46a33a14a7019e10a4678a4dd3a5a840c5c34cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

#include "binaryninjaapi.h"

constexpr const char *VIEW_METADATA_RTTI = "rtti";
constexpr int RTTI_CONFIDENCE = 100;

namespace BinaryNinja::RTTI {
	std::optional<std::string> DemangleNameMS(BinaryView* view, bool allowMangled, const std::string &mangledName);

	std::optional<std::string> DemangleNameGNU3(BinaryView* view, bool allowMangled, const std::string &mangledName);

	std::optional<std::string> DemangleNameItanium(BinaryView* view, bool allowMangled, const std::string &mangledName);

	std::optional<std::string> DemangleNameLLVM(bool allowMangled, const std::string &mangledName);

	struct VirtualFunctionInfo
	{
		uint64_t funcAddr;

		Ref<Metadata> SerializedMetadata() const;

		static VirtualFunctionInfo DeserializedMetadata(const Ref<Metadata> &metadata);
	};

	struct VirtualFunctionTableInfo
	{
		uint64_t address;
		std::vector<VirtualFunctionInfo> virtualFunctions;

		Ref<Metadata> SerializedMetadata(bool serializeFunctions = true) const;

		static VirtualFunctionTableInfo DeserializedMetadata(const Ref<Metadata> &metadata);
	};

	enum class RTTIProcessorType
	{
		Microsoft = 0,
		Itanium = 1,
	};

	struct BaseClassInfo
	{
		std::string className;
		// TODO: This has to be optional, as we might need to resolve this at a later stage.
		// TODO: The offset also might literally not exist.
		uint64_t offset;
		std::optional<VirtualFunctionTableInfo> vft;

		Ref<Metadata> SerializedMetadata() const;

		static BaseClassInfo DeserializedMetadata(const Ref<Metadata> &metadata);
	};

	// TODO: This needs to have some flags. Virtual, pure iirc.
	struct ClassInfo
	{
		RTTIProcessorType processor;
		std::string className;

		std::optional<VirtualFunctionTableInfo> vft;
		std::vector<BaseClassInfo> baseClasses;

		Ref<Metadata> SerializedMetadata() const;

		static ClassInfo DeserializedMetadata(const Ref<Metadata> &metadata);
	};

	class RTTIProcessor
	{
	protected:
		Ref<BinaryView> m_view;
		Ref<Logger> m_logger;

		std::map<uint64_t, ClassInfo> m_classInfo;
		std::map<uint64_t, ClassInfo> m_unhandledClassInfo;

		virtual std::optional<ClassInfo> ProcessRTTI(uint64_t objectAddr) = 0;

		virtual std::optional<VirtualFunctionTableInfo> ProcessVFT(uint64_t vftAddr, ClassInfo &classInfo, std::optional<BaseClassInfo> baseClassInfo) = 0;
	public:
		virtual ~RTTIProcessor() = default;

		void DeserializedMetadata(RTTIProcessorType type, const Ref<Metadata> &metadata);

		Ref<Metadata> SerializedMetadata();

		virtual void ProcessRTTI() = 0;

		virtual void ProcessVFT() = 0;
	};
}