From d8e3001e535fad178c621ff07418f81f25123dc4 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Thu, 22 Aug 2024 12:55:49 -0600 Subject: Allow multiple high level representations for display, add Pseudo Rust and a Pseudo Python example plugin --- lang/c/pseudoc.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lang/c/pseudoc.h (limited to 'lang/c/pseudoc.h') diff --git a/lang/c/pseudoc.h b/lang/c/pseudoc.h new file mode 100644 index 00000000..664e7695 --- /dev/null +++ b/lang/c/pseudoc.h @@ -0,0 +1,66 @@ +#pragma once + +#include "binaryninjaapi.h" + +class PseudoCFunction: public BinaryNinja::LanguageRepresentationFunction +{ + BinaryNinja::Ref m_highLevelIL; + + enum FieldDisplayType + { + FieldDisplayName, + FieldDisplayOffset, + FieldDisplayMemberOffset, + FieldDisplayNone + }; + + BinaryNinja::Ref GetFieldType(const BinaryNinja::HighLevelILInstruction& var, bool deref); + FieldDisplayType GetFieldDisplayType(BinaryNinja::Ref type, uint64_t offset, size_t memberIndex, bool deref); + + BNSymbolDisplayResult AppendPointerTextToken(const BinaryNinja::HighLevelILInstruction& instr, int64_t val, + std::vector& tokens, BinaryNinja::DisassemblySettings* settings, + BNSymbolDisplayType symbolDisplay, BNOperatorPrecedence precedence); + std::string GetSizeToken(size_t size, bool isSigned); + void AppendSizeToken(size_t size, bool isSigned, BinaryNinja::HighLevelILTokenEmitter& emitter); + void AppendSingleSizeToken(size_t size, BNInstructionTextTokenType type, BinaryNinja::HighLevelILTokenEmitter& emitter); + void AppendComparison(const std::string& comparison, const BinaryNinja::HighLevelILInstruction& instr, + BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, bool asFullAst, + BNOperatorPrecedence precedence, std::optional signedHint = std::nullopt); + void AppendTwoOperand(const std::string& operand, const BinaryNinja::HighLevelILInstruction& instr, + BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, bool asFullAst, + BNOperatorPrecedence precedence, std::optional signedHint = std::nullopt); + void AppendTwoOperandFunction(const std::string& function, const BinaryNinja::HighLevelILInstruction& instr, + BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings, bool sizeToken = true); + void AppendTwoOperandFunctionWithCarry(const std::string& function, const BinaryNinja::HighLevelILInstruction& instr, + BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings); + void AppendFieldTextTokens(const BinaryNinja::HighLevelILInstruction& var, uint64_t offset, size_t memberIndex, size_t size, + BinaryNinja::HighLevelILTokenEmitter& tokens, bool deref, bool displayDeref = true); + void GetExprTextInternal(const BinaryNinja::HighLevelILInstruction& instr, + BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings, bool asFullAst = true, + BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, bool statement = false, + std::optional signedHint = std::nullopt); + +protected: + void InitTokenEmitter(BinaryNinja::HighLevelILTokenEmitter& tokens) override; + void GetExprText(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens, + BinaryNinja::DisassemblySettings* settings, bool asFullAst = true, + BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, bool statement = false) override; + void BeginLines( + const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens) override; + void EndLines( + const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens) override; + +public: + PseudoCFunction(BinaryNinja::Architecture* arch, BinaryNinja::Function* owner, BinaryNinja::HighLevelILFunction* highLevelILFunction); + + std::string GetAnnotationStartString() const override; + std::string GetAnnotationEndString() const override; +}; + +class PseudoCFunctionType: public BinaryNinja::LanguageRepresentationFunctionType +{ +public: + PseudoCFunctionType(); + BinaryNinja::Ref Create(BinaryNinja::Architecture* arch, + BinaryNinja::Function* owner, BinaryNinja::HighLevelILFunction* highLevelILFunction) override; +}; -- cgit v1.3.1