summaryrefslogtreecommitdiff
path: root/plugins/rtti/rtti.h
blob: 00b105ebaae355dbc9ba7fd85b795524e756dfe2 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once

#include "binaryninjaapi.h"

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

namespace BinaryNinja::RTTI {
	Ref<Symbol> GetRealSymbol(BinaryView *view, uint64_t relocAddr, uint64_t symAddr);

	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:
		enum class FunctionDiscoverState
		{
			Failed = 0,
			AlreadyExists = 1,
			Discovered = 2,
			Extern = 3
		};

		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;

		[[nodiscard]] bool IsLikelyFunction(uint64_t addr) const;

		[[nodiscard]] FunctionDiscoverState DiscoverVirtualFunction(uint64_t vftEntryAddr, uint64_t& vFuncAddr);
	public:
		virtual ~RTTIProcessor() = default;

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

		Ref<Metadata> SerializedMetadata();

		virtual void ProcessRTTI() = 0;

		virtual void ProcessVFT() = 0;
	};
}