summaryrefslogtreecommitdiff
path: root/demangler/gnu3/demangle_gnu3.h
blob: 2e46e05d503fee88b98855e6dd4b92bc32a4e565 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2016-2026 Vector 35 Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <stdexcept>
#include <exception>

// XXX: Compiled directly into the core for performance reasons
// Will still work fine compiled independently, just at about a
// 50-100% performance penalty due to FFI overhead
#ifdef BINARYNINJACORE_LIBRARY
#include "qualifiedname.h"
#include "type.h"
#include "architecture.h"
#include "binaryview.h"
#include "demangle.h"
#define BN BinaryNinjaCore
#define _STD_STRING BinaryNinjaCore::string
#define _STD_VECTOR BinaryNinjaCore::vector
#else
#include "binaryninjaapi.h"
#define BN BinaryNinja
#define _STD_STRING std::string
#define _STD_VECTOR std::vector
#endif

#include "demangled_type_node.h"

class DemangleException: public std::exception
{
	_STD_STRING m_message;
public:
	DemangleException(_STD_STRING msg="Attempt to read beyond bounds or missing expected character"): m_message(msg){}
	virtual const char* what() const noexcept { return m_message.c_str(); }
};

class DemangleGNU3Reader
{
public:
	DemangleGNU3Reader(const _STD_STRING& data);
	void Reset(const _STD_STRING& data);
	_STD_STRING PeekString(size_t count=1);
	bool NextIsOneOf(const _STD_STRING& list);
	_STD_STRING GetRaw();
	_STD_STRING ReadString(size_t count=1);
	_STD_STRING ReadUntil(char sentinal);

	size_t Length() const { return m_data.length() - m_offset; }

	char Peek()
	{
		if (1 > Length())
			return '\0';
		return (char)m_data[m_offset];
	}

	char Read()
	{
		if (1 > Length())
			throw DemangleException();
		return m_data[m_offset++];
	}

	void Consume(size_t count=1)
	{
		if (count > Length())
			throw DemangleException();
		m_offset += count;
	}

	void UnRead(size_t count=1)
	{
		if (count <= m_offset)
			m_offset -= count;
	}

private:
	_STD_STRING m_data;
	size_t m_offset;
};


class DemangleGNU3
{
	using ParamList = _STD_VECTOR<DemangledTypeNode::Param>;

	BN::QualifiedName m_varName;
	DemangleGNU3Reader m_reader;
	BN::Architecture* m_arch;
	_STD_VECTOR<DemangledTypeNode> m_substitute;
	_STD_VECTOR<DemangledTypeNode> m_templateSubstitute;
	_STD_VECTOR<_STD_VECTOR<DemangledTypeNode>> m_functionSubstitute;
	_STD_STRING m_lastName;
	BNNameType m_nameType;
	bool m_localType;
	bool m_hasReturnType;
	bool m_isParameter;
	bool m_shouldDeleteReader;
	bool m_topLevel;
	bool m_isOperatorOverload;
	enum SymbolType { Function, FunctionWithReturn, Data, VTable, Rtti, Name};
	BN::QualifiedName DemangleBaseUnresolvedName();
	DemangledTypeNode DemangleUnresolvedType();
	_STD_STRING DemangleUnarySuffixExpression(const _STD_STRING& op);
	_STD_STRING DemangleUnaryPrefixExpression(const _STD_STRING& op);
	_STD_STRING DemangleBinaryExpression(const _STD_STRING& op);
	_STD_STRING DemangleUnaryPrefixType(const _STD_STRING& op);
	_STD_STRING DemangleTypeString();
	_STD_STRING DemangleExpressionList();
	DemangledTypeNode DemangleUnqualifiedName();
	_STD_STRING DemangleSourceName();
	_STD_STRING DemangleNumberAsString();
	_STD_STRING DemangleInitializer();
	_STD_STRING DemangleExpression();
	_STD_STRING DemanglePrimaryExpression();
	DemangledTypeNode DemangleName();
	DemangledTypeNode DemangleLocalName();

	void DemangleCVQualifiers(bool& cnst, bool& vltl, bool& rstrct);
	DemangledTypeNode DemangleSubstitution();
	const DemangledTypeNode& DemangleTemplateSubstitution();
	void DemangleTemplateArgs(_STD_VECTOR<_STD_STRING>& args);
	DemangledTypeNode DemangleFunction(bool cnst, bool vltl);
	DemangledTypeNode DemangleType();
	int64_t DemangleNumber();
	DemangledTypeNode DemangleNestedName();
	void PushTemplateType(const DemangledTypeNode& type);
	const DemangledTypeNode& GetTemplateType(size_t ref);
	void PushType(const DemangledTypeNode& type);
	const DemangledTypeNode& GetType(size_t ref);

	DemangledTypeNode CreateUnknownType(const BN::QualifiedName& s);
	DemangledTypeNode CreateUnknownType(const _STD_STRING& s);
	static void ExtendTypeName(DemangledTypeNode& type, const _STD_STRING& extend);

public:
	DemangleGNU3(BN::Architecture* arch, const _STD_STRING& mangledName);
	void Reset(BN::Architecture* arch, const _STD_STRING& mangledName);
	DemangledTypeNode DemangleSymbol(BN::QualifiedName& varName);
	BN::QualifiedName GetVarName() const { return m_varName; }
	void PrintTables();
};


class DemangleGNU3Static
{
public:
	static bool IsGNU3MangledString(const _STD_STRING& name);
	static bool DemangleGlobalHeader(_STD_STRING& name, _STD_STRING& header);

	static bool DemangleStringGNU3(BN::Architecture* arch, const _STD_STRING& name, BN::Ref<BN::Type>& outType, BN::QualifiedName& outVarName);
};