Skip to content

Commit

Permalink
Update Ghidra
Browse files Browse the repository at this point in the history
  • Loading branch information
thestr4ng3r committed Feb 13, 2023
1 parent 4bb1894 commit 30cd6b0
Show file tree
Hide file tree
Showing 21 changed files with 649 additions and 427 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ set(CORE_SOURCE
src/RizinPrintC.cpp
src/RzCoreMutex.h
src/RzCoreMutex.cpp
src/rz_ghidra.h)
src/PrettyXmlEncode.h
src/PrettyXmlEncode.cpp
src/rz_ghidra.h
src/rz_ghidra_internal.h)

if(BUILD_SLEIGH_PLUGIN)
set(ASM_SOURCE
Expand Down
2 changes: 2 additions & 0 deletions ghidra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Base of everything
set(SOURCE_BASE_CXX
# xml.cc // generated by yacc task
marshal.cc
space.cc
float.cc
address.cc
Expand Down Expand Up @@ -46,6 +47,7 @@ set(SOURCE_DECOMPILER_CXX
funcdata.cc
funcdata_block.cc
funcdata_varnode.cc
unionresolve.cc
funcdata_op.cc
pcodeinject.cc
heritage.cc
Expand Down
2 changes: 1 addition & 1 deletion ghidra/ghidra
Submodule ghidra updated 7367 files
57 changes: 36 additions & 21 deletions src/CodeXMLParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,32 +139,47 @@ void AnnotateCommentOffset(ANNOTATOR_PARAMS)
void AnnotateColor(ANNOTATOR_PARAMS)
{
pugi::xml_attribute attr = node.attribute("color");
if (attr.empty())
if(attr.empty())
return;

std::string color = attr.as_string();
if (color == "")
int color = attr.as_int(-1);
if(color < 0)
return;

RSyntaxHighlightType type;
if (color == "keyword")
type = RZ_SYNTAX_HIGHLIGHT_TYPE_KEYWORD;
else if (color == "comment")
type = RZ_SYNTAX_HIGHLIGHT_TYPE_COMMENT;
else if (color == "type")
type = RZ_SYNTAX_HIGHLIGHT_TYPE_DATATYPE;
else if (color == "funcname")
type = RZ_SYNTAX_HIGHLIGHT_TYPE_FUNCTION_NAME;
else if (color == "param")
type = RZ_SYNTAX_HIGHLIGHT_TYPE_FUNCTION_PARAMETER;
else if (color == "var")
type = RZ_SYNTAX_HIGHLIGHT_TYPE_LOCAL_VARIABLE;
else if (color == "const")
type = RZ_SYNTAX_HIGHLIGHT_TYPE_CONSTANT_VARIABLE;
else if (color == "global")
type = RZ_SYNTAX_HIGHLIGHT_TYPE_GLOBAL_VARIABLE;
else
return;
switch(color)
{
case Emit::syntax_highlight::keyword_color:
type = RZ_SYNTAX_HIGHLIGHT_TYPE_KEYWORD;
break;
case Emit::syntax_highlight::comment_color:
type = RZ_SYNTAX_HIGHLIGHT_TYPE_COMMENT;
break;
case Emit::syntax_highlight::type_color:
type = RZ_SYNTAX_HIGHLIGHT_TYPE_DATATYPE;
break;
case Emit::syntax_highlight::funcname_color:
type = RZ_SYNTAX_HIGHLIGHT_TYPE_FUNCTION_NAME;
break;
case Emit::syntax_highlight::var_color:
type = RZ_SYNTAX_HIGHLIGHT_TYPE_LOCAL_VARIABLE;
break;
case Emit::syntax_highlight::const_color:
type = RZ_SYNTAX_HIGHLIGHT_TYPE_CONSTANT_VARIABLE;
break;
case Emit::syntax_highlight::param_color:
type = RZ_SYNTAX_HIGHLIGHT_TYPE_FUNCTION_PARAMETER;
break;
case Emit::syntax_highlight::global_color:
type = RZ_SYNTAX_HIGHLIGHT_TYPE_GLOBAL_VARIABLE;
break;
case Emit::syntax_highlight::no_color:
case Emit::syntax_highlight::error_color:
case Emit::syntax_highlight::special_color:
default:
return;
}

RzCodeAnnotation annotation = {};
annotation.type = RZ_CODE_ANNOTATION_TYPE_SYNTAX_HIGHLIGHT;
annotation.syntax_highlight.type = type;
Expand Down
37 changes: 37 additions & 0 deletions src/PrettyXmlEncode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2023 Florian Märkl <info@florianmaerkl.de>
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "PrettyXmlEncode.h"

void PrettyXmlEncode::indent()
{
for(int i = 0; i < depth; i++)
outStream << " ";
}

void PrettyXmlEncode::openElement(const ElementId &elemId)
{
if(elementTagIsOpen)
outStream << ">\n";
else
elementTagIsOpen = true;
indent();
depth++;
outStream << '<' << elemId.getName();
}

void PrettyXmlEncode::closeElement(const ElementId &elemId)
{
depth--;
if(elementTagIsOpen)
{
outStream << "/>\n";
elementTagIsOpen = false;
}
else
{
indent();
outStream << "</" << elemId.getName() << ">\n";
}
}

21 changes: 21 additions & 0 deletions src/PrettyXmlEncode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2023 Florian Märkl <info@florianmaerkl.de>
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef PRETTY_XML_ENCODE_H
#define PRETTY_XML_ENCODE_H

#include <marshal.hh>

class PrettyXmlEncode: public XmlEncode
{
private:
int depth = 0;
void indent();

public:
PrettyXmlEncode(std::ostream &s) : XmlEncode(s) {}
void openElement(const ElementId &elemId) override;
void closeElement(const ElementId &elemId) override;
};

#endif
2 changes: 1 addition & 1 deletion src/RizinArchitecture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::string FilenameFromCore(RzCore *core)
}

RizinArchitecture::RizinArchitecture(RzCore *core, const std::string &sleigh_id)
: SleighArchitecture(FilenameFromCore(core), sleigh_id.empty() ? SleighIdFromCore(core) : sleigh_id, &cout),
: SleighArchitecture(FilenameFromCore(core), sleigh_id.empty() ? SleighIdFromCore(core) : sleigh_id, &std::cout),
coreMutex(core)
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/RizinCommentDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class RizinCommentDatabase : public CommentDatabase
CommentSet::const_iterator beginComment(const Address &fad) const override;
CommentSet::const_iterator endComment(const Address &fad) const override;

void saveXml(ostream &s) const override { cache.saveXml(s); }
void restoreXml(const Element *el, const AddrSpaceManager *trans) override { throw LowlevelError("commentdb::restoreXml unimplemented"); }
void encode(Encoder &encoder) const override { cache.encode(encoder); }
void decode(Decoder &decoder) override { throw LowlevelError("CommentDatabaseGhidra::decode unimplemented"); }
};

#endif //RZ_GHIDRA_RizinCOMMENTDATABASE_H
16 changes: 13 additions & 3 deletions src/RizinScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ FunctionSymbol *RizinScope::registerFunction(RzAnalysisFunction *fcn) const
params.registerTrial(addr, type->getSize());
int4 i = params.whichTrial(addr, type->getSize());
params.getTrial(i).markActive();
params.getTrial(i).markUsed();
return true;
});

Expand Down Expand Up @@ -298,12 +299,20 @@ FunctionSymbol *RizinScope::registerFunction(RzAnalysisFunction *fcn) const
return true;
}

paramIndex = params.whichTrial(addr, type->getSize());
if(paramIndex < 0)
int4 paramTrialIndex = params.whichTrial(addr, type->getSize());
if(paramTrialIndex < 0)
{
arch->addWarning("Failed to determine arg index of " + to_string(var->name));
return true;
}

paramIndex = 0;
for(int4 i = 0; i < paramTrialIndex; i++)
{
if(!params.getTrial(i).isUsed())
continue;
paramIndex++;
}
}

varRanges.insertRange(addr.getSpace(), addr.getOffset(), last);
Expand Down Expand Up @@ -406,7 +415,8 @@ FunctionSymbol *RizinScope::registerFunction(RzAnalysisFunction *fcn) const

child(&doc, "rangelist");

auto sym = cache->addMapSym(&doc);
XmlDecode dec(arch, &doc);
auto sym = cache->addMapSym(dec);
return dynamic_cast<FunctionSymbol *>(sym);
}

Expand Down
4 changes: 2 additions & 2 deletions src/RizinScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class RizinScope : public Scope
void renameSymbol(Symbol *sym,const string &newname) override { throw LowlevelError("renameSymbol unimplemented"); }
void retypeSymbol(Symbol *sym,Datatype *ct) override { throw LowlevelError("retypeSymbol unimplemented"); }
string makeNameUnique(const string &nm) const override { throw LowlevelError("makeNameUnique unimplemented"); }
void saveXml(ostream &s) const override { cache->saveXml(s); }
void restoreXml(const Element *el) override { throw LowlevelError("restoreXml unimplemented"); }
void encode(Encoder &encoder) const override { cache->encode(encoder); }
void decode(Decoder &decoder) override { throw LowlevelError("decode unimplemented"); }
void printEntries(ostream &s) const override { throw LowlevelError("printEntries unimplemented"); }
int4 getCategorySize(int4 cat) const override { throw LowlevelError("getCategorySize unimplemented"); }
Symbol *getCategorySymbol(int4 cat,int4 ind) const override { throw LowlevelError("getCategorySymbol unimplemented"); }
Expand Down
8 changes: 5 additions & 3 deletions src/RizinTypeFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ Datatype *RizinTypeFactory::addRizinStruct(RzBaseType *type, StackTypes &stack_t
// if(elements > 0)
// memberType = getTypeArray(elements, memberType);

fields.push_back({
TypeField tf = {
(int4)offset, // id = offset by default
(int4)offset, // Currently, this is 0 most of the time: member->offset,
std::string(member->name),
member_type
});
};
fields.push_back(tf);

// TODO: right now, we track member offset ourselves
// which means all structs are assumed to be packed.
Expand Down Expand Up @@ -110,7 +112,7 @@ Datatype *RizinTypeFactory::addRizinTypedef(RzBaseType *type, StackTypes &stack_
Datatype *resolved = fromRzTypeInternal(type->type, nullptr, &stack_types, true, false); // use prototype=true to avoid recursion
if(!resolved)
return nullptr;
Datatype *typedefd = getTypedef(resolved, type->name, 0);
Datatype *typedefd = getTypedef(resolved, type->name, 0, 0);
fromRzTypeInternal(type->type, nullptr, &stack_types, false, false); // fully create the type after querying with prototype=true before
return typedefd;
}
Expand Down
Loading

0 comments on commit 30cd6b0

Please sign in to comment.