summaryrefslogtreecommitdiff
path: root/architecture.cpp
diff options
context:
space:
mode:
authorRusty Wagner <rusty.wagner@gmail.com>2024-08-22 12:55:49 -0600
committerRusty Wagner <rusty.wagner@gmail.com>2024-10-21 13:56:55 -0400
commitd8e3001e535fad178c621ff07418f81f25123dc4 (patch)
tree54c03cef2f0bdfd9a3b6df4334c81becb63e4993 /architecture.cpp
parent0e281a30d73c0f31ef9442fef0346779164231ad (diff)
Allow multiple high level representations for display, add Pseudo Rust and a Pseudo Python example plugin
Diffstat (limited to 'architecture.cpp')
-rw-r--r--architecture.cpp142
1 files changed, 133 insertions, 9 deletions
diff --git a/architecture.cpp b/architecture.cpp
index aa229b4e..a86d7256 100644
--- a/architecture.cpp
+++ b/architecture.cpp
@@ -60,6 +60,17 @@ InstructionTextToken::InstructionTextToken() :
}
+InstructionTextToken::InstructionTextToken(uint8_t confidence, BNInstructionTextTokenType t, const string& txt) :
+ type(t), text(txt), value(0), width(WidthIsByteCount), size(0), operand(BN_INVALID_OPERAND),
+ context(NoTokenContext), confidence(confidence), address(0), exprIndex(BN_INVALID_EXPR)
+{
+ if (width == WidthIsByteCount)
+ {
+ width = text.size();
+ }
+}
+
+
InstructionTextToken::InstructionTextToken(BNInstructionTextTokenType t, const std::string& txt, uint64_t val, size_t s,
size_t o, uint8_t c, const vector<string>& n, uint64_t w) :
type(t),
@@ -107,7 +118,7 @@ InstructionTextToken InstructionTextToken::WithConfidence(uint8_t conf)
}
-static void ConvertInstructionTextToken(const InstructionTextToken& token, BNInstructionTextToken* result)
+void InstructionTextToken::ConvertInstructionTextToken(const InstructionTextToken& token, BNInstructionTextToken* result)
{
result->type = token.type;
result->text = BNAllocString(token.text.c_str());
@@ -144,15 +155,19 @@ BNInstructionTextToken* InstructionTextToken::CreateInstructionTextTokenList(con
}
+void InstructionTextToken::FreeInstructionTextToken(BNInstructionTextToken* token)
+{
+ BNFreeString(token->text);
+ for (size_t j = 0; j < token->namesCount; j++)
+ BNFreeString(token->typeNames[j]);
+ delete[] token->typeNames;
+}
+
+
void InstructionTextToken::FreeInstructionTextTokenList(BNInstructionTextToken* tokens, size_t count)
{
for (size_t i = 0; i < count; i++)
- {
- BNFreeString(tokens[i].text);
- for (size_t j = 0; j < tokens[i].namesCount; j++)
- BNFreeString(tokens[i].typeNames[j]);
- delete[] tokens[i].typeNames;
- }
+ FreeInstructionTextToken(&tokens[i]);
delete[] tokens;
}
@@ -2285,7 +2300,8 @@ void ArchitectureHook::Register(BNCustomArchitecture* callbacks)
string DisassemblyTextRenderer::GetDisplayStringForInteger(
Ref<BinaryView> binaryView, BNIntegerDisplayType type, uint64_t value, size_t inputWidth, bool isSigned)
{
- char* str = BNGetDisplayStringForInteger(binaryView->GetObject(), type, value, inputWidth, isSigned);
+ char* str = BNGetDisplayStringForInteger(binaryView ? binaryView->GetObject() : nullptr,
+ type, value, inputWidth, isSigned);
string s(str);
BNFreeString(str);
return s;
@@ -2527,6 +2543,24 @@ bool DisassemblyTextRenderer::AddSymbolToken(
}
+BNSymbolDisplayResult DisassemblyTextRenderer::AddSymbolTokenStatic(
+ std::vector<InstructionTextToken>& tokens, uint64_t addr, size_t size, size_t operand,
+ BinaryView* data, size_t maxSymbolWidth, Function* func, uint8_t confidence,
+ BNSymbolDisplayType symbolDisplay, BNOperatorPrecedence precedence, uint64_t instrAddr, uint64_t exprIndex)
+{
+ BNInstructionTextToken* result = nullptr;
+ size_t count = 0;
+ BNSymbolDisplayResult display = BNGetDisassemblyTextRendererSymbolTokensStatic(
+ addr, size, operand, data ? data->GetObject() : nullptr,
+ maxSymbolWidth, func ? func->GetObject() : nullptr, confidence, symbolDisplay, precedence,
+ instrAddr, exprIndex, &result, &count);
+ vector<InstructionTextToken> newTokens =
+ InstructionTextToken::ConvertAndFreeInstructionTextTokenList(result, count);
+ tokens.insert(tokens.end(), newTokens.begin(), newTokens.end());
+ return display;
+}
+
+
void DisassemblyTextRenderer::AddStackVariableReferenceTokens(
vector<InstructionTextToken>& tokens, const StackVariableReference& ref)
{
@@ -2560,7 +2594,7 @@ void DisassemblyTextRenderer::AddIntegerToken(
vector<InstructionTextToken>& tokens, const InstructionTextToken& token, Architecture* arch, uint64_t addr)
{
BNInstructionTextToken inToken;
- ConvertInstructionTextToken(token, &inToken);
+ InstructionTextToken::ConvertInstructionTextToken(token, &inToken);
size_t count = 0;
BNInstructionTextToken* result =
@@ -2606,3 +2640,93 @@ void DisassemblyTextRenderer::WrapComment(DisassemblyTextLine& line, vector<Disa
BNFreeDisassemblyTextLines(result, count);
BNFreeInstructionText(inLine.tokens, inLine.count);
}
+
+
+string DisassemblyTextRenderer::GetStringLiteralPrefix(BNStringType type)
+{
+ char* prefix = BNGetStringLiteralPrefix(type);
+ string result = prefix;
+ BNFreeString(prefix);
+ return result;
+}
+
+
+FunctionViewType::FunctionViewType(BNFunctionGraphType viewType) : type(viewType)
+{
+ if (type == HighLevelLanguageRepresentationFunctionGraph)
+ name = "Pseudo C";
+}
+
+
+FunctionViewType::FunctionViewType(const BNFunctionViewType& viewType) : type(viewType.type)
+{
+ if (type == HighLevelLanguageRepresentationFunctionGraph)
+ {
+ if (viewType.name)
+ name = viewType.name;
+ else
+ name = "Pseudo C";
+ }
+}
+
+
+BNFunctionViewType FunctionViewType::ToAPIObject() const
+{
+ BNFunctionViewType result;
+ result.type = type;
+ result.name = nullptr;
+ if (type == HighLevelLanguageRepresentationFunctionGraph)
+ result.name = name.c_str();
+ return result;
+}
+
+
+BNFunctionGraphType FunctionViewType::GetBackingILType() const
+{
+ if (type == HighLevelLanguageRepresentationFunctionGraph)
+ return HighLevelILFunctionGraph;
+ return type;
+}
+
+
+bool FunctionViewType::IsValidForView(BinaryView* view) const
+{
+ if (type != HighLevelLanguageRepresentationFunctionGraph)
+ return true;
+ Ref<LanguageRepresentationFunctionType> type = LanguageRepresentationFunctionType::GetByName(name);
+ if (!type)
+ return false;
+ return type->IsValid(view);
+}
+
+
+bool FunctionViewType::operator==(const FunctionViewType& other) const
+{
+ if (type != other.type)
+ return false;
+ if (type != HighLevelLanguageRepresentationFunctionGraph)
+ return true;
+ return name == other.name;
+}
+
+
+bool FunctionViewType::operator!=(const FunctionViewType& other) const
+{
+ if (type != other.type)
+ return true;
+ if (type != HighLevelLanguageRepresentationFunctionGraph)
+ return false;
+ return name != other.name;
+}
+
+
+bool FunctionViewType::operator<(const FunctionViewType& other) const
+{
+ if (type < other.type)
+ return true;
+ if (type > other.type)
+ return false;
+ if (type != HighLevelLanguageRepresentationFunctionGraph)
+ return false;
+ return name < other.name;
+}