2019-03-04 21:45:17 +00:00
|
|
|
#include "LinkTypeDialog.h"
|
|
|
|
#include "ui_LinkTypeDialog.h"
|
|
|
|
|
|
|
|
LinkTypeDialog::LinkTypeDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::LinkTypeDialog)
|
|
|
|
{
|
2019-04-15 11:14:46 +00:00
|
|
|
addrValid = false;
|
|
|
|
|
2019-03-04 21:45:17 +00:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
setWindowTitle(tr("Link type to address"));
|
|
|
|
|
|
|
|
// Populate the structureTypeComboBox
|
|
|
|
ui->structureTypeComboBox->addItem(tr("(No Type)"));
|
|
|
|
for (const TypeDescription &thisType : Core()->getAllStructs()) {
|
|
|
|
ui->structureTypeComboBox->addItem(thisType.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkTypeDialog::~LinkTypeDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkTypeDialog::setDefaultType(const QString &type)
|
|
|
|
{
|
|
|
|
int index = ui->structureTypeComboBox->findText(type);
|
|
|
|
if (index != -1) {
|
|
|
|
// index is valid so set is as the default
|
|
|
|
ui->structureTypeComboBox->setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 11:14:46 +00:00
|
|
|
bool LinkTypeDialog::setDefaultAddress(const QString &address)
|
2019-03-04 21:45:17 +00:00
|
|
|
{
|
2019-04-15 11:14:46 +00:00
|
|
|
// setting the text here will trigger on_exprLineEdit_textChanged, which will update addrValid
|
2019-03-04 21:45:17 +00:00
|
|
|
ui->exprLineEdit->setText(address);
|
|
|
|
|
2019-04-15 11:14:46 +00:00
|
|
|
if (!addrValid) {
|
|
|
|
return false;
|
2019-03-04 21:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if the current address is already linked to a type and set it as default
|
|
|
|
QString type = findLinkedType(Core()->math(ui->addressLineEdit->text()));
|
|
|
|
if (!type.isEmpty()) {
|
|
|
|
setDefaultType(type);
|
|
|
|
}
|
2019-04-15 11:14:46 +00:00
|
|
|
return true;
|
2019-03-04 21:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LinkTypeDialog::done(int r)
|
|
|
|
{
|
|
|
|
if (r == QDialog::Accepted) {
|
|
|
|
QString address = ui->addressLineEdit->text();
|
|
|
|
if (Core()->isAddressMapped(Core()->math(address))) {
|
|
|
|
// Address is valid so link the type to the address
|
|
|
|
QString type = ui->structureTypeComboBox->currentText();
|
|
|
|
if (type == tr("(No Type)")) {
|
|
|
|
// Delete link
|
|
|
|
Core()->cmdRaw("tl- " + address);
|
|
|
|
} else {
|
|
|
|
// Create link
|
|
|
|
Core()->cmdRaw(QString("tl %1 = %2").arg(type).arg(address));
|
|
|
|
}
|
|
|
|
QDialog::done(r);
|
|
|
|
|
|
|
|
// Seek to the specified address
|
2019-07-19 19:21:12 +00:00
|
|
|
Core()->seekAndShow(address);
|
2019-03-04 21:45:17 +00:00
|
|
|
|
|
|
|
// Refresh the views
|
|
|
|
emit Core()->refreshCodeViews();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address is invalid so display error message
|
|
|
|
QMessageBox::warning(this, tr("Error"), tr("The given address is invalid"));
|
|
|
|
} else {
|
|
|
|
QDialog::done(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LinkTypeDialog::findLinkedType(RVA address)
|
|
|
|
{
|
|
|
|
if (address == RVA_INVALID) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ret = Core()->cmdRaw(QString("tls %1").arg(address));
|
|
|
|
if (ret.isEmpty()) {
|
|
|
|
// return empty string since the current address is not linked to a type
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the given type from returned data
|
|
|
|
// TODO: Implement "tlsj" in radare2 or some other function to directly get linked type
|
2019-03-23 10:54:34 +00:00
|
|
|
QString s = ret.section(QLatin1Char('\n'), 0, 0);
|
2019-03-04 21:45:17 +00:00
|
|
|
return s.mid(1, s.size() - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkTypeDialog::on_exprLineEdit_textChanged(const QString &text)
|
|
|
|
{
|
|
|
|
RVA addr = Core()->math(text);
|
|
|
|
if (Core()->isAddressMapped(addr)) {
|
2019-04-15 11:14:46 +00:00
|
|
|
ui->addressLineEdit->setText(RAddressString(addr));
|
|
|
|
addrValid = true;
|
2019-03-04 21:45:17 +00:00
|
|
|
} else {
|
|
|
|
ui->addressLineEdit->setText(tr("Invalid Address"));
|
2019-04-15 11:14:46 +00:00
|
|
|
addrValid = false;
|
2019-03-04 21:45:17 +00:00
|
|
|
}
|
|
|
|
}
|