1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
Copyright (C) 2006 - 2015 Evan Teran
evan.teran@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DialogAssembler.h"
#include "IDebugger.h"
#include "edb.h"
#include "string_hash.h"
#include <QDebug>
#include <QDir>
#include <QDomDocument>
#include <QFile>
#include <QFileInfo>
#include <QLineEdit>
#include <QMessageBox>
#include <QProcess>
#include <QRegExp>
#include <QSettings>
#include <QTemporaryFile>
#include <QTextDocument>
#include <QXmlQuery>
#ifdef Q_OS_UNIX
#include <sys/types.h>
#include <unistd.h>
#endif
namespace AssemblerPlugin {
namespace {
/**
* @brief escape_html
* @param str
* @return
*/
QString escape_html(const QString &str) {
return str.toHtmlEscaped();
}
/**
* @brief assembler_description
* @return
*/
QDomDocument assembler_description() {
const QString assembler = QSettings().value("Assembler/helper", "yasm").toString();
QFile file(":/debugger/Assembler/xml/assemblers.xml");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QXmlQuery query;
QString assembler_xml;
query.setFocus(&file);
query.setQuery(QString("assemblers/assembler[@name='%1']").arg(escape_html(assembler)));
if (query.isValid()) {
query.evaluateTo(&assembler_xml);
}
QDomDocument xml;
xml.setContent(assembler_xml);
return xml;
}
return {};
}
/**
* @brief fixupSyntax
* @param insn
* @return
*/
QString fixup_syntax(QString insn) {
const QDomElement asmRoot = assembler_description().documentElement();
if (asmRoot.isNull()) {
return insn;
}
const QDomElement opSizes = asmRoot.firstChildElement("operand_sizes");
if (opSizes.isNull()) {
return insn;
}
static const QString sizes[] = {
"byte",
"word",
"dword",
"qword",
"tbyte",
"xmmword",
"ymmword",
"zmmword"};
for (const QString &size : sizes) {
const QString replacement = opSizes.attribute(size);
if (!replacement.isEmpty()) {
insn.replace(QRegExp("\\b" + size + "\\b"), replacement);
}
}
return insn;
}
}
/**
* @brief DialogAssembler::DialogAssembler
* @param parent
* @param f
*/
DialogAssembler::DialogAssembler(QWidget *parent, Qt::WindowFlags f)
: QDialog(parent, f) {
ui.setupUi(this);
// Disable click focus: we don't want to unnecessarily defocus instruction entry without need
ui.fillWithNOPs->setFocusPolicy(Qt::TabFocus);
ui.keepSize->setFocusPolicy(Qt::TabFocus);
}
/**
* @brief DialogAssembler::setAddress
* @param address
*/
void DialogAssembler::setAddress(edb::address_t address) {
address_ = address;
ui.address->setText(edb::v1::format_pointer(address_));
uint8_t buffer[edb::Instruction::MaxSize];
if (const int size = edb::v1::get_instruction_bytes(address, buffer)) {
edb::Instruction inst(buffer, buffer + size, address);
if (inst) {
ui.assembly->setEditText(fixup_syntax(edb::v1::formatter().toString(inst).c_str()).simplified());
instructionSize_ = inst.byteSize();
}
}
}
/**
* @brief DialogAssembler::on_buttonBox_accepted
*/
void DialogAssembler::on_buttonBox_accepted() {
if (IDebugger *core = edb::v1::debugger_core) {
const QString nasm_syntax = ui.assembly->currentText().trimmed();
const QDomElement asm_root = assembler_description().documentElement();
if (!asm_root.isNull()) {
QDomElement asm_executable = asm_root.firstChildElement("executable");
QDomElement asm_template = asm_root.firstChildElement("template");
#if defined(EDB_ARM32)
const auto mode = core->cpuMode();
while (mode == IDebugger::CpuMode::ARM32 && asm_template.attribute("mode") != "arm" ||
mode == IDebugger::CpuMode::Thumb && asm_template.attribute("mode") != "thumb") {
asm_template = asm_template.nextSiblingElement("template");
if (asm_template.isNull()) {
QMessageBox::critical(
this,
tr("Error running assembler"),
tr("Failed to locate source file template for current CPU mode"));
return;
}
}
#endif
const QString asm_name = asm_root.attribute("name");
const QString asm_cmd = asm_executable.attribute("command_line");
const QString asm_ext = asm_executable.attribute("extension");
Q_UNUSED(asm_name)
QString asm_code = asm_template.text();
QStringList command_line = edb::v1::parse_command_line(asm_cmd);
if (command_line.isEmpty()) {
QMessageBox::warning(this, tr("Couldn't Find Assembler"), tr("Failed to locate your assembler."));
return;
}
QTemporaryFile source_file(QString("%1/edb_asm_temp_%2_XXXXXX.%3").arg(QDir::tempPath()).arg(QCoreApplication::applicationPid()).arg(asm_ext));
if (!source_file.open()) {
QMessageBox::critical(this, tr("Error Creating File"), tr("Failed to create temporary source file."));
return;
}
QTemporaryFile output_file(QString("%1/edb_asm_temp_%2_XXXXXX.bin").arg(QDir::tempPath()).arg(QCoreApplication::applicationPid()));
if (!output_file.open()) {
QMessageBox::critical(this, tr("Error Creating File"), tr("Failed to create temporary object file."));
return;
}
const QString bitsStr = std::to_string(core->pointerSize() * 8).c_str();
const QString addrStr = edb::v1::format_pointer(address_);
static const char *bitsTag = "%BITS%";
static const char *addrTag = "%ADDRESS%";
static const char *insnTag = "%INSTRUCTION%";
asm_code.replace(bitsTag, bitsStr);
asm_code.replace(addrTag, addrStr);
asm_code.replace(insnTag, nasm_syntax);
source_file.write(asm_code.toLatin1());
source_file.close();
QProcess process;
QString program(command_line[0]);
command_line.pop_front();
QStringList arguments = command_line;
for (QString &arg : arguments) {
arg.replace("%OUT%", output_file.fileName());
arg.replace("%IN%", source_file.fileName());
arg.replace(bitsTag, bitsStr);
arg.replace(addrTag, addrStr);
arg.replace(insnTag, nasm_syntax);
}
qDebug() << "RUNNING ASM TOOL: " << program << arguments;
process.start(program, arguments);
if (process.waitForFinished()) {
const int exit_code = process.exitCode();
if (exit_code != 0) {
QMessageBox::warning(this, tr("Error In Code"), process.readAllStandardError());
} else {
QByteArray bytes = output_file.readAll();
const size_t replacement_size = bytes.size();
if (replacement_size != 0 && replacement_size <= instructionSize_) {
if (ui.fillWithNOPs->isChecked()) {
if (!edb::v1::modify_bytes(address_, instructionSize_, bytes, core->nopFillByte())) {
return;
}
} else {
if (!edb::v1::modify_bytes(address_, instructionSize_, bytes, 0x00)) {
return;
}
}
} else if (replacement_size == 0) {
const QString stdError = process.readAllStandardError();
QMessageBox::warning(this, tr("Error In Code"), tr("Got zero bytes from the assembler") + (stdError.isEmpty() ? "" : tr(", here's what it has to say:\n\n") + stdError));
return;
} else {
if (ui.keepSize->isChecked()) {
QMessageBox::warning(this, tr("Error In Code"), tr("New instruction is too big to fit."));
return;
} else {
if (!edb::v1::modify_bytes(address_, replacement_size, bytes, 0x00)) {
return;
}
}
}
const edb::address_t new_address = address_ + replacement_size;
setAddress(new_address);
edb::v1::set_cpu_selected_address(new_address);
}
ui.assembly->setFocus(Qt::OtherFocusReason);
ui.assembly->lineEdit()->selectAll();
} else if (process.error() == QProcess::FailedToStart) {
QMessageBox::warning(this, tr("Couldn't Launch Assembler"), tr("Failed to start your assembler."));
return;
}
}
}
}
/**
* @brief DialogAssembler::showEvent
* @param event
*/
void DialogAssembler::showEvent(QShowEvent *event) {
Q_UNUSED(event)
QSettings settings;
const QString assembler = settings.value("Assembler/helper", "yasm").toString();
ui.label->setText(tr("Assembler: %1").arg(assembler));
ui.assembly->setFocus(Qt::OtherFocusReason);
}
}
|