-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathidc_perl.xs
214 lines (185 loc) · 5 KB
/
idc_perl.xs
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
/* vim:set ts=4 sw=4 ai si:
*
* (C) 2008 Willem Hengeveld itsme@xs4all.nl
*
* IdcPerl - a perl scripting plugin for the Interactive Disassembler by hex-rays.
* see http://www.xs4all.nl/~itsme/projects/idcperl
*
* this file contains perl XSUBS calling the ida api ( as implemented in ida.wll )
* implementing all functionality as available normally for idc scripts
*
*/
// define this, so we get the flags (FF_) constants from bytes.hpp
#define BYTES_SOURCE
// ida includes
#include <pro.h> // for basic types.
#include <ida.hpp> // for ida constants and types.
#include <netnode.hpp> // for RootNode
#include <expr.hpp> // for IDCFuncs
#ifdef _MSC_VER
#pragma warning(disable:4700)
#pragma warning(disable:4100)
#pragma warning(disable:4189)
#endif
// for all functions to implement the idc functionality
#include <auto.hpp>
#include <bytes.hpp>
#include <funcs.hpp>
#include <idp.hpp>
#include <kernwin.hpp>
#include <lines.hpp>
#include <nalt.hpp>
#include <name.hpp>
#include <offset.hpp>
#include <search.hpp>
#include <segment.hpp>
#include <srarea.hpp>
#include <struct.hpp>
#include <ua.hpp>
#include <xref.hpp>
#include <frame.hpp>
#include <ieee.h>
// perl includes
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#undef do_open
#undef do_close
#ifdef DYNAMIC_PERL
#include "redefperlasdll.h"
#include "perldllprocs.h"
#endif
#include "cv_sv2idc.h"
#ifdef TRACE_XS
#define tracexs msg
#else
#define tracexs while(0)
#endif
#ifdef _PERL_THREADS
#include <boost/thread/mutex.hpp>
boost::mutex g_idcmtx;
#endif
#include <map>
#include "idchotkey.h"
#include <boost/shared_ptr.hpp>
typedef boost::shared_ptr<hotkey> hotkey_ptr;
typedef std::map<std::string,hotkey_ptr> hotkey_map;
xrefblk_t lastxr;
extfun_t *find_builtin_idc_func(const char *name)
{
extfun_t *f= IDCFuncs.f;
for (int i=0 ; i<IDCFuncs.qnty ; i++, f++)
{
if (strcmp(f->name, name)==0)
return f;
}
return NULL;
}
#define MY_CXT_KEY "IDC::_guts"
typedef struct {
hotkey_map *hotkeys;
} my_cxt_t;
START_MY_CXT
MODULE = IDC PACKAGE = IDC PREFIX = idaidc_
PROTOTYPES: DISABLE
BOOT:
{
MY_CXT_INIT;
MY_CXT.hotkeys= new hotkey_map;
tracexs("idc_perl.xs boot, %p, %p\n", &MY_CXT, &lastxr);
}
void idaidc_END()
PREINIT:
dMY_CXT;
CODE:
tracexs("idaidc_END\n");
delete MY_CXT.hotkeys;
MY_CXT.hotkeys= NULL;
# this creates this construction:
#
# evaluated/compiled idc:
# static idcthunk_XXXX() { perlthunk_XXXX(); }
# asm code:
# perlthunk_XXXX = patched version of idcperlthunk, calling perlfunc
long idaidc_AddHotkey(const char* hotkeyname, const char* idcfunc)
PREINIT:
dMY_CXT;
CODE:
tracexs("AddHotkey");
hotkey_map::iterator oldhk= MY_CXT.hotkeys->find(hotkeyname);
if (oldhk!=MY_CXT.hotkeys->end()) {
MY_CXT.hotkeys->erase(oldhk);
}
hotkey_ptr hk= hotkey_ptr(new hotkey());
if (!hk->add(hotkeyname, idcfunc)) {
RETVAL= -1;
}
else {
MY_CXT.hotkeys->insert(hotkey_map::value_type(hotkeyname,hk));
RETVAL= 0;
}
OUTPUT:
RETVAL
bool idaidc_DelHotkey(const char* hotkeyname)
PREINIT:
dMY_CXT;
CODE:
tracexs("DelHotkey");
// kernwin.hpp: del_idc_hotkey(hotkey)
hotkey_map::iterator oldhk= MY_CXT.hotkeys->find(hotkeyname);
if (oldhk!=MY_CXT.hotkeys->end()) {
MY_CXT.hotkeys->erase(oldhk);
}
OUTPUT:
RETVAL
void idaidc_exec_idcfunc(...)
PPCODE:
if (items < 1)
croak("Usage: exec_idcfunc(name, args)");
const char*name= (const char *)SvPV_nolen(ST(0));
extfun_t *fn= find_builtin_idc_func(name);
if (fn==NULL)
croak("unknown idc function: %s", name);
int fnitems= strlen(fn->args);
// validate arg count
if ((fnitems==0 && items!=1)
|| (fn->args[fnitems-1]!=VT_WILD && fnitems!=items-1))
croak("expected %d args for %s, got %ld\n", fnitems, name, items-1);
if (fn->args[fnitems-1]==VT_WILD && fnitems>items) {
croak("expected at least %d args for %s, got %ld\n", fnitems-1, name, items-1);
}
tracexs("idcinternal: %s, %ld params\n", name, items-1);
idc_value_t *argv= new idc_value_t[items-1];
idc_value_t result;
for (int i=0 ; i<items-1 ; i++)
{
// first deterimine argument type
if (i<fnitems && fn->args[i]!=VT_WILD)
argv[i].vtype= fn->args[i];
else {
// variable arguments: base on perl SV type
argv[i].vtype= sv2idctype(ST(i+1));
}
// then convert perl SV to idc_value_t
sv2idcval(ST(i+1), &argv[i]);
}
result.num= items; // nr of arguments
error_t err=0;
#ifdef _PERL_THREADS
{
boost::mutex::scoped_lock lock(g_idcmtx);
#endif
err= fn->fp(argv, &result);
#ifdef _PERL_THREADS
}
#endif
delete[] argv;
if (err) {
msg("error in %s: %d\n", name, err);
XSRETURN_UNDEF;
}
ST(0)= newSVidc(&result);
sv_2mortal(ST(0));
XSRETURN(1);
// note: need to research if the 'unreachable code' after XSRETURN has any consequences.
// the 'PUTBACK' is now 'unreachable'