用IDA反汇编可执行文件时,经常遇到未命名代码片段。对于少量这样的代码片段可以用IDA的Search->not function来定位(快捷键通常为Alt-U),再用Edit->Functoins->Create function...(快捷键为P)创建新函数。如果出现大量这样的代码片段,反复用Alt-U,P操作就很繁琐。于是就想到利用用idc脚本来实现这个功能。查阅了IDA的Help(https://www.hex-rays.com/products/ida/support/idadoc/index.shtml),Search->not function这个功能的名称为JumpNotFunction,但在Help->Index of IDC functions中找不到相对应的函数(快捷键P有对应的MakeFunction)。
用Google搜索 "How to implement JumpNotFunction using idc script in IDA environment" (如何在IDA环境下用idc脚本实现JumpNotFunction),找不到答案。于是决定自己动手解决问题。经过一番学习,找到了主要的可以使用的相关函数:FindCode、GetFunctionName、FindFuncEnd、isCode、GetFlags,摘录如下。
1.
// ea - address to start from
// flag is combination of the following bits:
#define SEARCH_DOWN 0x01 // search forward
#define SEARCH_NEXT 0x02 // search next occurence
#define SEARCH_CASE 0x04 // search case-sensitive
// (only for bin&txt search)
#define SEARCH_REGEX 0x08 // enable regular expressions
#define SEARCH_NOBRK 0x10 // don't test ctrl-break
#define SEARCH_NOSHOW 0x20 // don't display the search progress
// return BADADDR - not found
long FindCode(long ea,long flag);
2.
// ea - any address belonging to the function
// returns: null string - function doesn't exist otherwise returns function name
string GetFunctionName(long ea);
3.
// ea - starting address of a new function
// returns: if a function already exists, then return its end address.
// if a function end cannot be determined, then return BADADDR
// otherwise return the end address of the new function
long FindFuncEnd(long ea);
5.
// ea - linear address
// returns: 32-bit value of internal flags. See start of IDC.IDC file for explanations.
long GetFlags(long ea); // get internal flags for ea