?? square.idc
字號:
/**
文件名: square.idc
用途 : 通過分析程序上下文來追蹤數(shù)據(jù)的來歷.
說明 : 針對HP-UX PA指令。
編寫 : watercloud
日期 : 2003-2-27
**/
//#include <idc.idc>
/**
函數(shù): success createFunction(long)
說明: 如果指定地址處沒有定義函數(shù)則試圖在該處定義一個函數(shù)。錯誤返回0
**/
static createFunction(_addr)
{
auto addr,funcBegin,funcEnd;
addr=_addr;
if(GetFunctionName(addr)=="" )
{
while(SegName(addr)=="$CODE$" && GetFunctionName(addr)=="" && !(GetMnem(addr) == "stw" && GetOpnd(addr,0)=="%rp") )
{
addr=addr-4;
}
//Message("funcBegin :%x Name:%s\n",addr,GetFunctionName(addr));
if(SegName(addr) == "$CODE$" && GetFunctionName(addr)=="")
{
//Message("funcBegin :%x\n",addr);
funcBegin = addr;
funcEnd=FindFuncEnd(funcBegin);
if(funcEnd !=BADADDR)
{
return MakeFunction(funcBegin,funcEnd);
}
}
}
return 0;
}
/**
函數(shù): long getFuncBeginEA(long)
返回: 指定地址所在函數(shù)的函數(shù)起始地址,出錯返回 BADADDR 。
**/
static getFuncBeginEA(_addr)
{
auto addr_begin,name_and_off,point_offset,sOff,xOff;
name_and_off = GetFuncOffset(_addr);
if(name_and_off == "")
return BADADDR;
point_offset = strstr(name_and_off,"+");
if(point_offset == -1)
return BADADDR;
sOff = substr(name_and_off,point_offset+1,-1);
xOff = xtol(sOff);
return _addr - xOff;
}
/**
函數(shù): string getRegister(string opn)
返回: 寄存器名稱,出錯返回 ""
說明: 從如下:im+var_im(%s,%r) /im+var_im(%r) / im(%sr,%r) / im(%r) /%r 格式字符串中取得寄存器%r。
**/
static getRegister(opn)
{
auto str,index1,index2;
index1 = strstr(opn,"%");
if(index1 == -1)
return "";
str = substr(opn,index1+1,-1);
index2 = strstr(str,"%");
if(index2 != -1)
return substr(str,index2,strstr(str,")"));
else
return substr(opn,index1,strstr(opn,")"));
}
/**
宏:getRegDP() : 取得程序初始化時寄存器%dp的值,錯誤返回 BADADDR
**/
#define getRegDP() SegEnd(getSegAddr("$GLOBAL$"))
/**
函數(shù):long getSegAddr(string _name)
說明:返回指定名稱段的起始地址,錯誤返回 BADADDR
**/
static getSegAddr(_name)
{
auto tmpAddr;
tmpAddr = FirstSeg();
while( tmpAddr != BADADDR)
{
if( SegName(tmpAddr) == _name )
return SegStart(tmpAddr);
tmpAddr = NextSeg(tmpAddr);
}
return BADADDR;
}
/**
函數(shù):string getString(long _addr)
用途:從指定地址處試圖得到一個字符串,如果需要在該地址創(chuàng)建一個String。
**/
static getString(_addr)
{
auto tmpStr, chr,addr,flag;
tmpStr= "";
addr = _addr;
chr = Byte(addr);
while( chr != 0 && chr != 0xFF )
{
tmpStr = form("%s%c", tmpStr, chr);
addr ++;
chr = Byte(addr);
}
if( addr - _addr >1 )
{
if( hasName(GetFlags(_addr) ) == 0 && SegName(_addr) != "$CODE$" )
{
MakeStr(_addr,addr);
}
}
return(tmpStr);
}
/**
函數(shù):long getValue(long _addr,long _n)
說明:取得指定地址處指令的第_n個操作數(shù)的值
返回:情形:ldo 0x44,%r 時返回0x44 / copy name,%r時返回名字name對應(yīng)的真實值 / -0x140 + var_40(%sr0,%sp)返回 -0x100
錯誤返回"ERR"。
**/
static getValue(_addr,_n)
{
auto value,tmp,type,subType,flag;
flag=GetFlags(_addr);
type=GetOpType(_addr,_n);
tmp=GetOpnd(_addr,_n);
if(type == 5) //立即數(shù)
{
value = LocByName(tmp);
if(value == BADADDR)
return xtol(tmp);
else return value;
}
else if(type == 7) //操作數(shù)為名稱
{
tmp = LocByName(tmp);
if(tmp == BADADDR)
return "ERR";
else
return tmp;
}
else if(type == 4) //基址+段尋址方式
{
if(_n == 0)
subType = isStkvar0(flag); //取第一個操作數(shù)類型
else if(_n == 1)
subType = isStkvar1(flag);//取第二個操作數(shù)類型
else
return "ERR"; //系統(tǒng)只提供了處理兩個操作數(shù)的能力,當(dāng)處理更多的操作數(shù)出錯。
if(subType == 0) //0x50C(%sr0,%r1)
{
return xtol(tmp);
}
else if(subType == 1) //arg_18(%sr0,%sp) 或-0x180+var_20(%sr0,%sp)形勢
{
OpHex(_addr,_n); //將其轉(zhuǎn)換為:0x999(%r)形勢
tmp = GetOpnd(_addr,_n);
OpStkvar(_addr,_n);
return xtol(tmp);
}
}
return "ERR";
}
/*
宏定義,用于指定函數(shù)getDataResEx的中止?fàn)顟B(tài)。
ERR: 出錯,不能處理; STA: 遇到棧變量; RET: 遇到函數(shù)返回值;IMP: 遇到還未實現(xiàn)的指令。
ARG: 依賴函數(shù)被調(diào)用時傳入的參數(shù)。 NOF: 沒有找到需要的寄存器。
D_MSG宏用于處理打印調(diào)試信息。
*/
#define F_ERR(X,V) return form("ERR %x %x",X,V)
#define F_STA(X,V) return form("STA %x %x",X,V)
#define F_RET(X,V) return form("RET %x %x",X,V)
#define F_IMP(X,V) return form("IMP %x %x",X,V)
#define F_ARG(R,V) return form("ARG %s %x",R,V)
#define F_NOF(R,V) return form("NOF %s %x",R,V)
#define D_MSG(F,X,O,R) if(F>=5) Message(" %x : %s -> %s\n",X,O,R)
#define getDRStr0(S) substr(S,0,3)
#define getDRType(S) substr(S,0,3)
#define getDRStr1(S) substr(S,4,4+strstr(substr(S,4,-1)," ") )
#define getDRReg(S) substr(S,4,4+strstr(substr(S,4,-1)," ") )
#define getDRStr0_1(S) substr(S,0,4+strstr(substr(S,4,-1)," "))
static isDRValue(_string)
{
auto f0;
f0=getDRStr0(_string);
return !(f0=="ERR" || f0=="STA" || f0=="IMP" || f0=="RET" || f0=="NOF" || f0=="ARG");
}
#define PREVENT_UN_LOCK_CLICK 1000
/**
函數(shù): string getDataResEx(long,string,long)
說明: 取得數(shù)據(jù)來源 :函數(shù)參數(shù)、數(shù)據(jù)區(qū)、BSS區(qū)、立即數(shù)
**/
static getDataResEx(_addr,_reg,_debugFlag)
{
auto addr,type,subType,reg,tmp,tmpReg,tmpStr,tmpValue,tmpAddr,opcode;
auto REG,VALUE,DEBUG_FLAG,REGDP,FUNC_BEGIN,FUNC_END,CLICK,FUNC_HEAD_REPEAT,FUNC_HEAD_REPEAT_FLAG;
FUNC_HEAD_REPEAT=30; //如果出現(xiàn)地址跳轉(zhuǎn)錯誤,并試圖在函數(shù)開始處找到該地址。
DEBUG_FLAG=_debugFlag;
addr = _addr+4;
REG = _reg;
VALUE = 0;
REGDP = getRegDP();
if(REGDP == BADADDR)
{
if(DEBUG_FLAG >=3)
Message("ERROR: getDataResEx -> getRegDP() FALSE!\n");
F_ERR(0,addr);
}
tmpStr = GetFunctionName(_addr);
if(tmpStr == "") //如果該處還沒有函數(shù),則試圖定義一個函數(shù)
{
if(createFunction(_addr) == 0)
{
if(DEBUG_FLAG >=3)
Message("ERROR: getDataResEx -> createFunction(%x) == FALSE\n",_addr);
F_ERR(1,addr);
}
if(DEBUG_FLAG >=10)
Message("Message : getDataResEx -> GetFunctionName(%x) = \"%s\"\n",_addr,tmpStr);
}
FUNC_BEGIN = getFuncBeginEA(_addr);
FUNC_END = FindFuncEnd(FUNC_BEGIN);
if(FUNC_BEGIN == BADADDR || FUNC_END == BADADDR || FUNC_END <= FUNC_BEGIN)
{
if(DEBUG_FLAG >=3)
Message("ERROR: getDataResEx -> getFuncBeginEA() || FindFuncEnd FALSE!\n");
F_ERR(2,-1);
}
if(DEBUG_FLAG >= 10)
Message("Trace into getDataResEx : %x %s\n",_addr,_reg);
CLICK = 0; //防止死循環(huán)
FUNC_HEAD_REPEAT_FLAG = 0;
while(addr > FUNC_BEGIN && addr < FUNC_END && CLICK < PREVENT_UN_LOCK_CLICK )
{
// addr=RfirstB(addr);
addr=addr-4;
CLICK ++; //防止死循環(huán)
opcode = GetMnem(addr);
//如果最終無法找到且查找對象為常見數(shù)據(jù)段使用的寄存器那么從函數(shù)頭部一定范圍內(nèi)再給一次搜索機會。
if(FUNC_HEAD_REPEAT_FLAG==0 && (addr <= FUNC_BEGIN || addr >=FUNC_END || opcode == "" ) )
{
if(REG=="%r1"||REG=="%r3"||REG=="%r4"||REG=="%r5"||REG=="%r6"||REG=="%r7"||REG=="%r8"||REG=="%r9")
{
addr = FUNC_BEGIN + FUNC_HEAD_REPEAT * 4;
FUNC_HEAD_REPEAT_FLAG = 1;
continue;
}
}
if(opcode == "")
F_ERR(addr,VALUE);
if(DEBUG_FLAG >=10)
Message("%x: %s %s %s %s\n",addr,opcode,GetOpnd(addr,0),GetOpnd(addr,1),GetOpnd(addr,2) );
if(opcode == "addil" && GetOpnd(addr,2) == REG ) //addil 0x4567,%r,%r
{
D_MSG(DEBUG_FLAG,addr,"addil",REG);
tmp = GetOpnd(addr,1);
if(tmp == "%dp")
return VALUE + getValue(addr,0) + REGDP;
if(tmp == "%r0")
return VALUE + getValue(addr,0);
if(tmp == "%sp")
F_STA(addr,VALUE);
if(tmp == "%r28")
F_RET(addr,VALUE);
REG = GetOpnd(addr,1);
VALUE = VALUE + getValue(addr,0);
}
else if(opcode == "copy" && GetOpnd(addr,1) == REG ) // copy 0,%r / copy %r,%r
{
D_MSG(DEBUG_FLAG,addr,"copy",REG);
type=GetOpType(addr,0);
if(type == 1)
{
tmp=GetOpnd(addr,0);
if(tmp == "%dp")
return VALUE + REGDP;
if(tmp == "%r0")
return VALUE ;
if(tmp == "%sp")
F_STA(addr,VALUE);
if(tmp == "%r28")
F_RET(addr,VALUE);
REG = GetOpnd(addr,0);
}
else if(type == 5 || type == 7)
{
return VALUE + getValue(addr,0);
}
else
F_ERR(addr,VALUE);
}
else if(opcode == "ldil" && GetOpnd(addr,1) == REG ) //ldil 0x55,%r
{
D_MSG(DEBUG_FLAG,addr,"ldil",REG);
return VALUE+getValue(addr,0);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -