Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Added separate calling convention detection pass
Browse files Browse the repository at this point in the history
  • Loading branch information
yegord committed Jun 14, 2015
1 parent 6cd5753 commit 8dcd9e2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/nc/arch/x86/X86MasterAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,39 @@ void X86MasterAnalyzer::createProgram(core::Context &context) const {
}
}

void X86MasterAnalyzer::detectCallingConvention(core::Context &context, const core::ir::calling::CalleeId &calleeId) const {
auto architecture = context.image()->architecture();
void X86MasterAnalyzer::detectCallingConventions(core::Context &context) const {
context.logToken().info(tr("Detecting calling conventions."));

auto setConvention = [&](const char *name) {
context.conventions()->setConvention(calleeId, architecture->getCallingConvention(QLatin1String(name)));
};
auto architecture = context.image()->architecture();

if (architecture->bitness() == 32) {
if (auto addr = calleeId.entryAddress()) {
if (auto symbol = context.image()->getSymbol(*addr)) {
int index = symbol->name().lastIndexOf(QChar('@'));
if (index != -1) {
if (auto argumentsSize = stringToInt<ByteSize>(symbol->name().mid(index + 1))) {
setConvention("stdcall32");
context.conventions()->setStackArgumentsSize(calleeId, *argumentsSize);
return;
}
}
auto stdcall32 = architecture->getCallingConvention(QLatin1String("stdcall32"));

foreach (auto symbol, context.image()->symbols()) {
if (!symbol->value()) {
continue;
}
auto index = symbol->name().lastIndexOf(QChar('@'));
if (index == -1) {
continue;
}
auto argumentsSize = stringToInt<ByteSize>(symbol->name().mid(index + 1));
if (!argumentsSize) {
continue;
}
core::ir::calling::CalleeId calleeId(core::ir::calling::EntryAddress(*symbol->value()));
context.conventions()->setConvention(calleeId, stdcall32);
context.conventions()->setStackArgumentsSize(calleeId, *argumentsSize);
}
}
}

void X86MasterAnalyzer::detectCallingConvention(core::Context &context, const core::ir::calling::CalleeId &calleeId) const {
auto architecture = context.image()->architecture();

auto setConvention = [&](const char *name) {
context.conventions()->setConvention(calleeId, architecture->getCallingConvention(QLatin1String(name)));
};

switch (architecture->bitness()) {
case 16:
Expand Down
1 change: 1 addition & 0 deletions src/nc/arch/x86/X86MasterAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace x86 {
class X86MasterAnalyzer: public core::MasterAnalyzer {
public:
void createProgram(core::Context &context) const override;
void detectCallingConventions(core::Context &context) const override;
void detectCallingConvention(core::Context &context, const core::ir::calling::CalleeId &calleeId) const override;
};

Expand Down
7 changes: 7 additions & 0 deletions src/nc/core/MasterAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ void MasterAnalyzer::createHooks(Context &context) const {
});
}

void MasterAnalyzer::detectCallingConventions(Context &) const {
return;
}

void MasterAnalyzer::detectCallingConvention(Context &context, const ir::calling::CalleeId &calleeId) const {
if (!context.image()->architecture()->conventions().empty()) {
context.conventions()->setConvention(calleeId, context.image()->architecture()->conventions().front());
Expand Down Expand Up @@ -230,6 +234,9 @@ void MasterAnalyzer::decompile(Context &context) const {
createHooks(context);
context.cancellationToken().poll();

detectCallingConventions(context);
context.cancellationToken().poll();

dataflowAnalysis(context);
context.cancellationToken().poll();

Expand Down
2 changes: 2 additions & 0 deletions src/nc/core/MasterAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class MasterAnalyzer {
*/
virtual void createHooks(Context &context) const;

virtual void detectCallingConventions(Context &context) const;

/**
* Detects and sets the calling convention of a function.
*
Expand Down

0 comments on commit 8dcd9e2

Please sign in to comment.