Skip to content

Commit

Permalink
[IDA] Global structure variables sync is under development
Browse files Browse the repository at this point in the history
  • Loading branch information
a1ext committed Jun 12, 2018
1 parent a147573 commit 9a2be9c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
22 changes: 21 additions & 1 deletion labeless_ida/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ namespace compat {
inline bool is_stroff(flags_t F, int n) { return ::isStroff(F, n); }
inline bool is_stroff0(flags_t F) { return ::isStroff0(F); }
inline bool is_stroff1(flags_t F) { return ::isStroff1(F); }
inline bool is_off(flags_t F, int n) { return ::isOff(F, n); }
inline bool is_off0(flags_t F) { return ::isOff0(F); }
inline bool is_off1(flags_t F) { return ::isOff1(F); }
inline bool is_code(flags_t F) { return ::isCode(F); }
inline bool is_data(flags_t F) { return ::isData(F); }
inline bool is_tail(flags_t F) { return ::isTail(F); }
inline bool is_head(flags_t F) { return ::isHead(F); }
inline ea_t to_ea(sel_t reg_cs, ea_t reg_ip) { return ::toEA(reg_cs, reg_ip); }
inline bool is_enabled(ea_t ea) { return ::isEnabled(ea); }
inline const char* get_idb_path() { return ::database_idb; }
Expand Down Expand Up @@ -184,6 +189,7 @@ namespace compat {
inline bool is_qword(flags_t F) { return ::isQwrd(F); }
inline bool create_dword(ea_t ea, asize_t length) { return ::doDwrd(ea, length); }
inline bool create_qword(ea_t ea, asize_t length) { return ::doQwrd(ea, length); }
inline opinfo_t* get_opinfo(opinfo_t *buf, ea_t ea, int n, flags_t flags) { return ::get_opinfo(ea, n, flags, buf); }

#define PROCESSOR_T_NEWFILE (::processor_t::newfile)
#define PROCESSOR_T_OLDFILE (::processor_t::oldfile)
Expand Down Expand Up @@ -224,8 +230,13 @@ namespace compat {
using ::is_stroff;
using ::is_stroff0;
using ::is_stroff1;
using ::is_off;
using ::is_off0;
using ::is_off1;
using ::is_code;
using ::is_data;
using ::is_tail;
using ::is_head;
using ::to_ea;
inline bool get_member_name(qstring *out, tid_t mid) { return ::get_member_name(out, mid) > 0; }
inline bool is_enabled(ea_t ea) { return ::is_mapped(ea); }
Expand All @@ -239,7 +250,15 @@ namespace compat {
using ::get_cmt;
using ::auto_wait;
using ::is_call_insn;
using ::print_operand;
inline bool print_operand(qstring *out, ea_t ea, int n, int getn_flags = 0, printop_t *newtype = NULL) {
if (!::print_operand(out, ea, n, getn_flags, newtype))
return false;

if (!tag_remove(out))
return false;

return true;
}
inline bool do_unknown(ea_t ea, int flags) { return ::del_items(ea, flags); }
inline void do_unknown_range(ea_t ea, size_t size, int flags) {
::del_items(ea, flags, static_cast<::asize_t>(size));
Expand All @@ -256,6 +275,7 @@ namespace compat {
using ::is_qword;
using ::create_dword;
using ::create_qword;
using ::get_opinfo;

#define PROCESSOR_T_NEWFILE (::processor_t::ev_newfile)
#define PROCESSOR_T_OLDFILE (::processor_t::ev_oldfile)
Expand Down
97 changes: 96 additions & 1 deletion labeless_ida/labeless_ida.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,56 @@ void enumerateLocalVars(EA2CommentHash& ea2commentHash, bool allLocalVars)
addComment(ea2commentHash, ea, strucName.c_str());
}
}
#if 0
else if (compat::is_off0(flags) || compat::is_off1(flags))
{
// TODO
for (int opNum = 0; opNum < 2 /* UA_MAXOP */; ++opNum)
{
if (!compat::is_off(flags, opNum))
continue;

ea_t opTarget = INSN_T_OPNDS(&insn)[opNum].value;
if (!opTarget)
continue;

flags_t targetFlags = compat::get_flags(opTarget);
struc_t* pStruc = nullptr;
opinfo_t ti = {};

if (!compat::is_struct(targetFlags) && compat::is_tail(targetFlags))
{
opTarget = ::prev_not_tail(opTarget);
targetFlags = compat::get_flags(opTarget);
if (!compat::is_struct(targetFlags))
continue;

if (!::get_opinfo(&ti, targetFlags, 0, targetFlags))
continue;

pStruc = ::get_struc(ti.tid);
if (!pStruc)
continue;

const asize_t strucSize = get_struc_size(pStruc);
if (opTarget - INSN_T_OPNDS(&insn)[opNum].value >= strucSize)
continue;
}

// TODO
/*if (!pStruc)
{
if (!::get_opinfo(&ti, targetFlags, 0, targetFlags))
continue;

pStruc = ::get_struc(ti.tid);
if (!pStruc)
continue;

}*/
}
}
#endif // 0
/*if (isEnum0(flags) | isEnum1(flags))
{
for (int opNum = 0; opNum < 2 /* UA_MAXOP *\/; ++opNum)
Expand Down Expand Up @@ -493,6 +542,50 @@ void enumerateComments(EA2CommentHash& ea2commentHash)
}
}

void enumerateGlobalVars(EA2CommentHash& ea2comment)
{
const int segsn = get_segm_qty();

auto handleRef = [&ea2comment](::ea_t seg_ea, ::flags_t seg_ea_flags, const ::xrefblk_t& ref) {
const ::ea_t ref_ea = ref.from;
const ::flags_t ref_flags = compat::get_flags(ref_ea);
int opNum = -1;

if (compat::is_stroff0(ref_flags) || compat::is_off0(ref_flags))
opNum = 0;
else if (compat::is_stroff1(ref_flags) || compat::is_off1(ref_flags))
opNum = 1;
else
return;

qstring qoperand;
if (!compat::print_operand(&qoperand, ref_ea, opNum) || qoperand.empty())
return;

// low priority
if (!ea2comment.contains(ref_ea))
addComment(ea2comment, ref_ea, qoperand.c_str());
};

for (int segnum = 0; segnum < segsn; ++segnum)
{
segment_t* const segm = getnseg(segnum);
if (!segm || segm->type != SEG_DATA)
continue;

for (ea_t ea = START_RANGE_EA(segm), segEnd = END_RANGE_EA(segm); ea < segEnd; ++ea)
{
flags_t flags = compat::get_flags(ea);
if (!compat::is_head(flags) || !compat::is_struct(flags))
continue;

::xrefblk_t xref = {};
for (bool ok = xref.first_to(ea, XREF_DATA); ok; ok = xref.next_to())
handleRef(ea, flags, xref);
}
}
}

bool parseBackendId(const ::qstring& qid, std::string& result)
{
if (qid.empty())
Expand Down Expand Up @@ -656,6 +749,8 @@ void Labeless::onSyncronizeAllRequested()
enumerateComments(ea2comment);
}

enumerateGlobalVars(ea2comment);

CommentsSync::DataList commentPoints;

for (auto it = ea2comment.constBegin(), end = ea2comment.constEnd(); it != end; ++it)
Expand Down Expand Up @@ -1784,7 +1879,7 @@ bool Labeless::createSegment(const compat::IDARange& area, uchar perm, uchar typ
memset(&result, 0, sizeof(result));
static_cast<compat::IDARange&>(result) = area;

result.bitness = ::inf.is_64bit() ? 2 : 1;
result.bitness = ::inf.is_64bit() ? 2 : 1; // TODO: name them
result.sel = setup_selector(0);
result.perm = perm;
result.type = type;
Expand Down

0 comments on commit 9a2be9c

Please sign in to comment.