From af39658da74af4c340d74f842a09a146dba1a6fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Wed, 7 Jun 2017 12:56:55 +0200 Subject: [PATCH] XRefs from JSON Commands --- src/dialogs/xrefsdialog.cpp | 10 +-- src/dialogs/xrefsdialog.h | 3 +- src/qrcore.cpp | 122 +++++++++++++++++++++----------- src/qrcore.h | 15 +++- src/widgets/functionswidget.cpp | 38 ++-------- src/widgets/memorywidget.cpp | 38 ++-------- 6 files changed, 109 insertions(+), 117 deletions(-) diff --git a/src/dialogs/xrefsdialog.cpp b/src/dialogs/xrefsdialog.cpp index 8815ab57..00442ecc 100644 --- a/src/dialogs/xrefsdialog.cpp +++ b/src/dialogs/xrefsdialog.cpp @@ -27,15 +27,15 @@ XrefsDialog::~XrefsDialog() delete ui; } -void XrefsDialog::fillRefs(QList refs, QList xrefs) +void XrefsDialog::fillRefs(QList refs, QList xrefs) { ui->fromTreeWidget->clear(); for (int i = 0; i < refs.size(); ++i) { //this->add_debug_output(refs.at(i).at(0) + " " + refs.at(i).at(1)); QTreeWidgetItem *tempItem = new QTreeWidgetItem(); - tempItem->setText(0, refs.at(i).at(0)); - tempItem->setText(1, refs.at(i).at(1)); + tempItem->setText(0, RAddressString(refs.at(i).to)); + tempItem->setText(1, refs.at(i).opcode); //tempItem->setToolTip( 0, this->main->core->cmd("pdi 10 @ " + refs.at(i).at(0)) ); //tempItem->setToolTip( 1, this->main->core->cmd("pdi 10 @ " + refs.at(i).at(0)) ); ui->fromTreeWidget->insertTopLevelItem(0, tempItem); @@ -52,8 +52,8 @@ void XrefsDialog::fillRefs(QList refs, QList xrefs) { //this->add_debug_output(xrefs.at(i).at(0) + " " + xrefs.at(i).at(1)); QTreeWidgetItem *tempItem = new QTreeWidgetItem(); - tempItem->setText(0, xrefs.at(i).at(0)); - tempItem->setText(1, xrefs.at(i).at(1)); + tempItem->setText(0, RAddressString(xrefs.at(i).from)); + tempItem->setText(1, xrefs.at(i).opcode); //tempItem->setToolTip( 0, this->main->core->cmd("pdi 10 @ " + xrefs.at(i).at(0)) ); //tempItem->setToolTip( 1, this->main->core->cmd("pdi 10 @ " + xrefs.at(i).at(0)) ); ui->toTreeWidget->insertTopLevelItem(0, tempItem); diff --git a/src/dialogs/xrefsdialog.h b/src/dialogs/xrefsdialog.h index 74747ca5..6d54e15e 100644 --- a/src/dialogs/xrefsdialog.h +++ b/src/dialogs/xrefsdialog.h @@ -2,6 +2,7 @@ #define XREFSDIALOG_H #include "highlighter.h" +#include "qrcore.h" #include #include @@ -21,7 +22,7 @@ public: explicit XrefsDialog(MainWindow *main, QWidget *parent = 0); ~XrefsDialog(); - void fillRefs(QList refs, QList xrefs); + void fillRefs(QList refs, QList xrefs); void updateLabels(QString name); private slots: diff --git a/src/qrcore.cpp b/src/qrcore.cpp index 4bb0f988..2ab64d3b 100644 --- a/src/qrcore.cpp +++ b/src/qrcore.cpp @@ -66,47 +66,6 @@ QRCore::QRCore(QObject *parent) : this->db = sdb_new(NULL, NULL, 0); // WTF NOES } -QList QRCore::getFunctionXrefs(ut64 addr) -{ - CORE_LOCK(); - QList ret = QList(); - RList *list = r_anal_xrefs_get(core_->anal, addr); - RAnalRef *ref; - RListIter *it; - QRListForeach(list, it, RAnalRef, ref) - { - ret << QString("%1,0x%2,0x%3").arg( - QString(ref->type), - QString::number(ref->addr, 16), - QString::number(ref->at, 16)); - } - return ret; -} - -QList QRCore::getFunctionRefs(ut64 addr, char type) -{ - CORE_LOCK(); - QList ret = QList(); - //RAnalFunction *fcn = r_anal_get_fcn_at(core_->anal, addr, addr); - RAnalFunction *fcn = r_anal_get_fcn_in(core_->anal, addr, 0); - if (!fcn) - { - eprintf("qcore->getFunctionRefs: No function found\n"); - return ret; - } - //eprintf(fcn->name); - RAnalRef *ref; - RListIter *it; - QRListForeach(fcn->refs, it, RAnalRef, ref) - { - if (type == ref->type || type == 0) - ret << QString("%1,0x%2,0x%3").arg( - QString(ref->type), - QString::number(ref->addr, 16), - QString::number(ref->at, 16)); - } - return ret; -} int QRCore::getCycloComplex(ut64 addr) { @@ -1125,3 +1084,84 @@ QList QRCore::getAllSections() } return ret; } + + +QList QRCore::getFunctionXrefs(ut64 addr) +{ + CORE_LOCK(); + QList ret = QList(); + RList *list = r_anal_xrefs_get(core_->anal, addr); + RAnalRef *ref; + RListIter *it; + QRListForeach(list, it, RAnalRef, ref) + { + ret << QString("%1,0x%2,0x%3").arg( + QString(ref->type), + QString::number(ref->addr, 16), + QString::number(ref->at, 16)); + } + return ret; +} + +QList QRCore::getFunctionRefs(ut64 addr, char type) +{ + CORE_LOCK(); + QList ret = QList(); + //RAnalFunction *fcn = r_anal_get_fcn_at(core_->anal, addr, addr); + RAnalFunction *fcn = r_anal_get_fcn_in(core_->anal, addr, 0); + if (!fcn) + { + eprintf("qcore->getFunctionRefs: No function found\n"); + return ret; + } + //eprintf(fcn->name); + RAnalRef *ref; + RListIter *it; + QRListForeach(fcn->refs, it, RAnalRef, ref) + { + if (type == ref->type || type == 0) + ret << QString("%1,0x%2,0x%3").arg( + QString(ref->type), + QString::number(ref->addr, 16), + QString::number(ref->at, 16)); + } + return ret; +} + +QList QRCore::getXRefs(RVA addr, bool to, const QString &filterType) +{ + QList ret = QList(); + + QJsonArray xrefsArray; + + if (to) + xrefsArray = cmdj("axtj@" + QString::number(addr)).array(); + else + xrefsArray = cmdj("axfj@" + QString::number(addr)).array(); + + for (QJsonValue value : xrefsArray) + { + QJsonObject xrefObject = value.toObject(); + + XRefDescription xref; + xref.type = xrefObject["type"].toString(); + + if (!filterType.isNull() && filterType != xref.type) + continue; + + xref.from = xrefObject["from"].toVariant().toULongLong(); + + if (to && !xrefObject.contains("to")) + xref.to = addr; + else + xref.to = xrefObject["to"].toVariant().toULongLong(); + + xref.opcode = xrefObject["opcode"].toString(); + + printf("xref %s %s\n", to ? "to" : "from", xref.opcode.toLocal8Bit().constData()); + + ret << xref; + } + + return ret; +} \ No newline at end of file diff --git a/src/qrcore.h b/src/qrcore.h index 8668cbc0..8bc73a49 100644 --- a/src/qrcore.h +++ b/src/qrcore.h @@ -134,6 +134,14 @@ struct SectionDescription QString flags; }; +struct XRefDescription +{ + RVA from; + RVA to; + QString type; + QString opcode; +}; + Q_DECLARE_METATYPE(FunctionDescription) Q_DECLARE_METATYPE(ImportDescription) Q_DECLARE_METATYPE(ExportDescription) @@ -155,8 +163,6 @@ public: ~QRCore(); RVA getOffset() const { return core_->offset; } - QList getFunctionXrefs(ut64 addr); - QList getFunctionRefs(ut64 addr, char type); int getCycloComplex(ut64 addr); int getFcnSize(ut64 addr); int fcnCyclomaticComplexity(ut64 addr); @@ -227,6 +233,11 @@ public: QList getAllFlags(QString flagspace = NULL); QList getAllSections(); + + QList getFunctionXrefs(ut64 addr); + QList getFunctionRefs(ut64 addr, char type); + QList getXRefs(RVA addr, bool to, const QString &filterType = QString::null); + RCoreLocked core() const; /* fields */ diff --git a/src/widgets/functionswidget.cpp b/src/widgets/functionswidget.cpp index 6f16cf4a..77f6626d 100644 --- a/src/widgets/functionswidget.cpp +++ b/src/widgets/functionswidget.cpp @@ -518,44 +518,14 @@ void FunctionsWidget::on_action_References_triggered() x->setWindowTitle("X-Refs for function " + QString::fromUtf8(fcn->name)); // Get Refs and Xrefs - QList ret_refs; - QList ret_xrefs; // refs = calls q hace esa funcion - QList refs = this->main->core->getFunctionRefs(fcn->addr, 'C'); - if (refs.size() > 0) - { - for (int i = 0; i < refs.size(); ++i) - { - //this->main->add_debug_output(refs.at(i)); - QStringList retlist = refs.at(i).split(","); - QStringList temp; - QString addr = retlist.at(2); - temp << addr; - QString op = this->main->core->cmd("pi 1 @ " + addr); - temp << op.simplified(); - ret_refs << temp; - } - } + QList refs = main->core->getXRefs(fcn->addr, false, "C"); // xrefs = calls a esa funcion - //qDebug() << this->main->core->getFunctionXrefs(offset.toLong(&ok, 16)); - QList xrefs = this->main->core->getFunctionXrefs(fcn->addr); - if (xrefs.size() > 0) - { - for (int i = 0; i < xrefs.size(); ++i) - { - //this->main->add_debug_output(xrefs.at(i)); - QStringList retlist = xrefs.at(i).split(","); - QStringList temp; - QString addr = retlist.at(1); - temp << addr; - QString op = this->main->core->cmd("pi 1 @ " + addr); - temp << op.simplified(); - ret_xrefs << temp; - } - } - x->fillRefs(ret_refs, ret_xrefs); + QList xrefs = main->core->getXRefs(fcn->addr, true); + + x->fillRefs(refs, xrefs); x->exec(); } diff --git a/src/widgets/memorywidget.cpp b/src/widgets/memorywidget.cpp index 773f4251..c10db4df 100644 --- a/src/widgets/memorywidget.cpp +++ b/src/widgets/memorywidget.cpp @@ -1880,44 +1880,14 @@ void MemoryWidget::on_actionXRefs_triggered() x->updateLabels(QString(fcn->name)); // Get Refs and Xrefs - QList ret_refs; - QList ret_xrefs; // refs = calls q hace esa funcion - QList refs = this->main->core->getFunctionRefs(fcn->addr, 'C'); - if (refs.size() > 0) - { - for (int i = 0; i < refs.size(); ++i) - { - //this->main->add_debug_output(refs.at(i)); - QStringList retlist = refs.at(i).split(","); - QStringList temp; - QString addr = retlist.at(2); - temp << addr; - QString op = this->main->core->cmd("pi 1 @ " + addr); - temp << op.simplified(); - ret_refs << temp; - } - } + QList refs = main->core->getXRefs(fcn->addr, false, "C"); // xrefs = calls a esa funcion - //qDebug() << this->main->core->getFunctionXrefs(offset.toLong(&ok, 16)); - QList xrefs = this->main->core->getFunctionXrefs(fcn->addr); - if (xrefs.size() > 0) - { - for (int i = 0; i < xrefs.size(); ++i) - { - //this->main->add_debug_output(xrefs.at(i)); - QStringList retlist = xrefs.at(i).split(","); - QStringList temp; - QString addr = retlist.at(1); - temp << addr; - QString op = this->main->core->cmd("pi 1 @ " + addr); - temp << op.simplified(); - ret_xrefs << temp; - } - } - x->fillRefs(ret_refs, ret_xrefs); + QList xrefs = main->core->getXRefs(fcn->addr, true); + + x->fillRefs(refs, xrefs); x->exec(); } }