From 10f03596cb1eff3cf5e961cf5355c38f450f33d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sun, 19 Nov 2023 13:00:19 +0100 Subject: [PATCH] Use flags for generating missing function names (#335) When there is no rizin function, but a flag and we need to generate some function name there, we can use the flag name instead of the default behavior, which is to call it "func_". --- src/RizinPrintC.cpp | 29 ++++ src/RizinPrintC.h | 1 + test/db/extras/ghidra | 375 +++++++++++++++++++++++++----------------- 3 files changed, 250 insertions(+), 155 deletions(-) diff --git a/src/RizinPrintC.cpp b/src/RizinPrintC.cpp index 74e98ec0..d8cde082 100644 --- a/src/RizinPrintC.cpp +++ b/src/RizinPrintC.cpp @@ -2,10 +2,15 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #include "RizinPrintC.h" +#include "RizinArchitecture.h" #include #include +#include + +#include "RizinUtils.h" + using namespace ghidra; // Constructing this registers the capability @@ -42,3 +47,27 @@ void RizinPrintC::pushUnnamedLocation(const Address &addr, const Varnode *vn, co PrintC::pushUnnamedLocation(addr,vn, op); } } + +std::string RizinPrintC::genericFunctionName(const ghidra::Address &addr) +{ + auto arch = dynamic_cast(glb); + if (arch) { + RzCoreLock core(arch->getCore()); + const RzList *flags = rz_flag_get_list(core->flags, addr.getOffset()); + if(flags) + { + RzListIter *iter; + void *pos; + rz_list_foreach(flags, iter, pos) + { + auto flag = reinterpret_cast(pos); + if(flag->space && flag->space->name && !strcmp(flag->space->name, RZ_FLAGS_FS_SECTIONS)) + continue; + if(core->flags->realnames && flag->realname) + return flag->realname; + return flag->name; + } + } + } + return PrintC::genericFunctionName(addr); +} diff --git a/src/RizinPrintC.h b/src/RizinPrintC.h index 6f280a9a..afaf607f 100644 --- a/src/RizinPrintC.h +++ b/src/RizinPrintC.h @@ -10,6 +10,7 @@ class RizinPrintC : public ghidra::PrintC { protected: void pushUnnamedLocation(const ghidra::Address &addr, const ghidra::Varnode *vn,const ghidra::PcodeOp *op) override; + std::string genericFunctionName(const ghidra::Address &addr) override; public: explicit RizinPrintC(ghidra::Architecture *g, const std::string &nm = "c-language"); diff --git a/test/db/extras/ghidra b/test/db/extras/ghidra index a7e52ce2..bb3c1877 100644 --- a/test/db/extras/ghidra +++ b/test/db/extras/ghidra @@ -1129,7 +1129,7 @@ NAME=constant variable annotation FILE=bins/dectest32 EXPECT=<