parser.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /****************************************************************************
  2. * dcc project procedure list builder
  3. * (C) Cristina Cifuentes, Mike van Emmerik, Jeff Ledermann
  4. ****************************************************************************/
  5. #include "dcc.h"
  6. #include "project.h"
  7. #include "CallGraph.h"
  8. #include "msvc_fixes.h"
  9. #include <inttypes.h>
  10. #include <string.h>
  11. #include <stdlib.h> /* For exit() */
  12. #include <sstream>
  13. #include <stdio.h>
  14. #include <algorithm>
  15. #include <deque>
  16. #include <QMap>
  17. #include <QtCore/QDebug>
  18. using namespace std;
  19. //static void FollowCtrl (Function * pProc, CALL_GRAPH * pcallGraph, STATE * pstate);
  20. static void setBits(int16_t type, uint32_t start, uint32_t len);
  21. static void process_MOV(LLInst &ll, STATE * pstate);
  22. static SYM * lookupAddr (LLOperand *pm, STATE * pstate, int size, uint16_t duFlag);
  23. void interactDis(Function * initProc, int ic);
  24. extern uint32_t SynthLab;
  25. /* Returns the size of the string pointed by sym and delimited by delim.
  26. * Size includes delimiter. */
  27. int strSize (const uint8_t *sym, char delim)
  28. {
  29. PROG &prog(Project::get()->prog);
  30. int till_end = sym-prog.image();
  31. const uint8_t *end_ptr=std::find(sym,sym+(prog.cbImage-(till_end)),delim);
  32. return end_ptr-sym+1;
  33. }
  34. ICODE * Function::translate_DIV(LLInst *ll, ICODE &_Icode)
  35. {
  36. /* MOV rTMP, reg */
  37. ICODE eIcode = ICODE();
  38. eIcode.type = LOW_LEVEL;
  39. eIcode.ll()->set(iMOV,0,rTMP);
  40. if (ll->testFlags(B) )
  41. {
  42. eIcode.ll()->setFlags( B );
  43. eIcode.ll()->replaceSrc(rAX);
  44. }
  45. else /* implicit dx:ax */
  46. {
  47. eIcode.ll()->setFlags( IM_SRC );
  48. eIcode.setRegDU( rDX, eUSE);
  49. }
  50. eIcode.setRegDU( rAX, eUSE);
  51. eIcode.setRegDU( rTMP, eDEF);
  52. eIcode.ll()->setFlags( SYNTHETIC );
  53. /* eIcode.ll()->label = SynthLab++; */
  54. eIcode.ll()->label = _Icode.ll()->label;
  55. Icode.addIcode(&eIcode);
  56. /* iDIV, iIDIV */
  57. Icode.addIcode(&_Icode);
  58. /* iMOD */
  59. eIcode = ICODE();
  60. eIcode.type = LOW_LEVEL;
  61. eIcode.ll()->set(iMOD,ll->getFlag() | SYNTHETIC | IM_TMP_DST);
  62. eIcode.ll()->replaceSrc(_Icode.ll()->src());
  63. eIcode.du = _Icode.du;
  64. eIcode.ll()->label = SynthLab++;
  65. return Icode.addIcode(&eIcode);
  66. }
  67. ICODE *Function::translate_XCHG(LLInst *ll,ICODE &_Icode)
  68. {
  69. /* MOV rTMP, regDst */
  70. ICODE eIcode;
  71. eIcode.type = LOW_LEVEL;
  72. eIcode.ll()->set(iMOV,SYNTHETIC,rTMP,ll->m_dst);
  73. eIcode.setRegDU( rTMP, eDEF);
  74. if(eIcode.ll()->src().getReg2())
  75. {
  76. eReg srcreg=eIcode.ll()->src().getReg2();
  77. eIcode.setRegDU( srcreg, eUSE);
  78. if((srcreg>=rAL) and (srcreg<=rBH))
  79. eIcode.ll()->setFlags( B );
  80. }
  81. eIcode.ll()->label = ll->label;
  82. Icode.addIcode(&eIcode);
  83. /* MOV regDst, regSrc */
  84. ll->set(iMOV,SYNTHETIC|ll->getFlag());
  85. Icode.addIcode(&_Icode);
  86. ll->setOpcode(iXCHG); /* for next case */
  87. /* MOV regSrc, rTMP */
  88. eIcode = ICODE();
  89. eIcode.type = LOW_LEVEL;
  90. eIcode.ll()->set(iMOV,SYNTHETIC);
  91. eIcode.ll()->replaceDst(ll->src());
  92. if(eIcode.ll()->m_dst.regi)
  93. {
  94. if((eIcode.ll()->m_dst.regi>=rAL) and (eIcode.ll()->m_dst.regi<=rBH))
  95. eIcode.ll()->setFlags( B );
  96. eIcode.setRegDU( eIcode.ll()->m_dst.regi, eDEF);
  97. }
  98. eIcode.ll()->replaceSrc(rTMP);
  99. eIcode.setRegDU( rTMP, eUSE);
  100. eIcode.ll()->label = SynthLab++;
  101. return Icode.addIcode(&eIcode);
  102. }
  103. /** FollowCtrl - Given an initial procedure, state information and symbol table
  104. * builds a list of procedures reachable from the initial procedure
  105. * using a depth first search. */
  106. void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
  107. {
  108. PROG &prog(Project::get()->prog);
  109. ICODE _Icode, *pIcode; /* This gets copied to pProc->Icode[] later */
  110. SYM * psym;
  111. uint32_t offset;
  112. eErrorId err;
  113. bool done = false;
  114. SYMTAB &global_symbol_table(Project::get()->symtab);
  115. if (name.find("chkstk") != string::npos)
  116. {
  117. // Danger! Dcc will likely fall over in this code.
  118. // So we act as though we have done with this proc
  119. // pProc->flg &= ~TERMINATES; // Not sure about this
  120. done = true;
  121. // And mark it as a library function, so structure() won't choke on it
  122. flg |= PROC_ISLIB;
  123. return;
  124. }
  125. if (option.VeryVerbose)
  126. {
  127. printf("Parsing proc %s at %X\n", name.c_str(), pstate->IP);
  128. }
  129. while (not done )
  130. {
  131. err = scan(pstate->IP, _Icode);
  132. if(err)
  133. break;
  134. LLInst *ll = _Icode.ll();
  135. pstate->IP += (uint32_t)ll->numBytes;
  136. setBits(BM_CODE, ll->label, (uint32_t)ll->numBytes);
  137. process_operands(_Icode,pstate);
  138. /* Keep track of interesting instruction flags in procedure */
  139. flg |= (ll->getFlag() & (NOT_HLL | FLOAT_OP));
  140. /* Check if this instruction has already been parsed */
  141. iICODE labLoc = Icode.labelSrch(ll->label);
  142. if (Icode.end()!=labLoc)
  143. { /* Synthetic jump */
  144. _Icode.type = LOW_LEVEL;
  145. ll->set(iJMP,I | SYNTHETIC | NO_OPS);
  146. ll->replaceSrc(LLOperand::CreateImm2(labLoc->ll()->GetLlLabel()));
  147. ll->label = SynthLab++;
  148. }
  149. /* Copy Icode to Proc */
  150. if ((ll->getOpcode() == iDIV) or (ll->getOpcode() == iIDIV))
  151. pIcode = translate_DIV(ll, _Icode);
  152. else if (_Icode.ll()->getOpcode() == iXCHG)
  153. pIcode = translate_XCHG(ll, _Icode);
  154. else
  155. pIcode = Icode.addIcode(&_Icode);
  156. switch (ll->getOpcode()) {
  157. /*** Conditional jumps ***/
  158. case iLOOP: case iLOOPE: case iLOOPNE:
  159. case iJB: case iJBE: case iJAE: case iJA:
  160. case iJL: case iJLE: case iJGE: case iJG:
  161. case iJE: case iJNE: case iJS: case iJNS:
  162. case iJO: case iJNO: case iJP: case iJNP:
  163. case iJCXZ:
  164. {
  165. STATE StCopy;
  166. int ip = Icode.size()-1; /* Index of this jump */
  167. ICODE &prev(*(++Icode.rbegin())); /* Previous icode */
  168. bool fBranch = false;
  169. pstate->JCond.regi = 0;
  170. /* This sets up range check for indexed JMPs hopefully
  171. * Handles JA/JAE for fall through and JB/JBE on branch
  172. */
  173. if (ip > 0 and prev.ll()->getOpcode() == iCMP and (prev.ll()->testFlags(I)))
  174. {
  175. pstate->JCond.immed = (int16_t)prev.ll()->src().getImm2();
  176. if (ll->match(iJA) or ll->match(iJBE) )
  177. pstate->JCond.immed++;
  178. if (ll->getOpcode() == iJAE or ll->getOpcode() == iJA)
  179. pstate->JCond.regi = prev.ll()->m_dst.regi;
  180. fBranch = (bool) (ll->getOpcode() == iJB or ll->getOpcode() == iJBE);
  181. }
  182. StCopy = *pstate;
  183. /* Straight line code */
  184. this->FollowCtrl (pcallGraph, &StCopy); // recurrent ?
  185. if (fBranch) /* Do branching code */
  186. {
  187. pstate->JCond.regi = prev.ll()->m_dst.regi;
  188. }
  189. /* Next icode. Note: not the same as GetLastIcode() because of the call
  190. to FollowCtrl() */
  191. pIcode = Icode.GetIcode(ip);
  192. } /* Fall through to do the jump path */
  193. /*** Jumps ***/
  194. case iJMP:
  195. case iJMPF: /* Returns true if we've run into a loop */
  196. done = process_JMP (*pIcode, pstate, pcallGraph);
  197. break;
  198. /*** Calls ***/
  199. case iCALL:
  200. case iCALLF:
  201. done = process_CALL (*pIcode, pcallGraph, pstate);
  202. pstate->kill(rBX);
  203. pstate->kill(rCX);
  204. break;
  205. /*** Returns ***/
  206. case iRET:
  207. case iRETF:
  208. this->flg |= (ll->getOpcode() == iRET)? PROC_NEAR:PROC_FAR;
  209. /* Fall through */
  210. case iIRET:
  211. this->flg &= ~TERMINATES;
  212. done = true;
  213. break;
  214. case iINT:
  215. if (ll->src().getImm2() == 0x21 and pstate->f[rAH])
  216. {
  217. int funcNum = pstate->r[rAH];
  218. int operand;
  219. int size;
  220. /* Save function number */
  221. Icode.back().ll()->m_dst.off = (int16_t)funcNum;
  222. //Icode.GetIcode(Icode.GetNumIcodes() - 1)->
  223. /* Program termination: int21h, fn 00h, 31h, 4Ch */
  224. done = (bool)(funcNum == 0x00 or funcNum == 0x31 or
  225. funcNum == 0x4C);
  226. /* String functions: int21h, fn 09h */
  227. if (pstate->f[rDX]) /* offset goes into DX */
  228. if (funcNum == 0x09)
  229. {
  230. operand = ((uint32_t)(uint16_t)pstate->r[rDS]<<4) +
  231. (uint32_t)(uint16_t)pstate->r[rDX];
  232. size = prog.fCOM ?
  233. strSize (&prog.image()[operand], '$') :
  234. strSize (&prog.image()[operand], '$'); // + 0x100
  235. global_symbol_table.updateSymType (operand, TypeContainer(TYPE_STR, size));
  236. }
  237. }
  238. else if ((ll->src().getImm2() == 0x2F) and (pstate->f[rAH]))
  239. {
  240. Icode.back().ll()->m_dst.off = pstate->r[rAH];
  241. }
  242. else /* Program termination: int20h, int27h */
  243. done = (ll->src().getImm2() == 0x20 or ll->src().getImm2() == 0x27);
  244. if (done)
  245. pIcode->ll()->setFlags(TERMINATES);
  246. break;
  247. case iMOV:
  248. process_MOV(*pIcode->ll(), pstate);
  249. break;
  250. /* case iXCHG:
  251. process_MOV (pIcode, pstate);
  252. break; **** HERE ***/
  253. case iSHL:
  254. if (pstate->JCond.regi == ll->m_dst.regi)
  255. {
  256. if ((ll->testFlags(I)) and ll->src().getImm2() == 1)
  257. pstate->JCond.immed *= 2;
  258. else
  259. pstate->JCond.regi = 0;
  260. }
  261. break;
  262. case iLEA:
  263. if (ll->src().getReg2()== rUNDEF) /* direct mem offset */
  264. pstate->setState( ll->m_dst.getReg2(), ll->src().off);
  265. break;
  266. case iLDS: case iLES:
  267. if ((psym = lookupAddr(&ll->src(), pstate, 4, eDuVal::USE))
  268. /* and (Icode.ll()->flg & SEG_IMMED) */ )
  269. {
  270. offset = LH(&prog.image()[psym->label]);
  271. pstate->setState( (ll->getOpcode() == iLDS)? rDS: rES,
  272. LH(&prog.image()[psym->label + 2]));
  273. pstate->setState( ll->m_dst.regi, (int16_t)offset);
  274. psym->type = TYPE_PTR;
  275. }
  276. break;
  277. }
  278. }
  279. if (err) {
  280. this->flg &= ~TERMINATES;
  281. if (err == INVALID_386OP or err == INVALID_OPCODE)
  282. {
  283. fatalError(err, prog.image()[_Icode.ll()->label], _Icode.ll()->label);
  284. this->flg |= PROC_BADINST;
  285. }
  286. else if (err == IP_OUT_OF_RANGE)
  287. fatalError (err, _Icode.ll()->label);
  288. else
  289. reportError(err, _Icode.ll()->label);
  290. }
  291. }
  292. /* Firstly look for a leading range check of the form:-
  293. * CMP {BX | SI | DI}, immed
  294. * JA | JAE | JB | JBE
  295. * This is stored in the current state as if we had just
  296. * followed a JBE branch (i.e. [reg] lies between 0 - immed).
  297. */
  298. void Function::extractJumpTableRange(ICODE& pIcode, STATE *pstate, JumpTable &table)
  299. {
  300. static uint8_t i2r[4] = {rSI, rDI, rBP, rBX};
  301. if (pstate->JCond.regi == i2r[pIcode.ll()->src().getReg2()-INDEX_SI])
  302. table.finish = table.start + pstate->JCond.immed;
  303. else
  304. table.finish = table.start + 2;
  305. }
  306. /* process_JMP - Handles JMPs, returns true if we should end recursion */
  307. bool Function::followAllTableEntries(JumpTable &table, uint32_t cs, ICODE& pIcode, CALL_GRAPH* pcallGraph, STATE *pstate)
  308. {
  309. PROG &prog(Project::get()->prog);
  310. STATE StCopy;
  311. setBits(BM_DATA, table.start, table.size()*table.entrySize());
  312. pIcode.ll()->setFlags(SWITCH);
  313. pIcode.ll()->caseTbl2.resize( table.size() );
  314. assert(pIcode.ll()->caseTbl2.size()<512);
  315. uint32_t k=0;
  316. for (size_t i = table.start; i < table.finish; i += 2)
  317. {
  318. StCopy = *pstate;
  319. StCopy.IP = cs + LH(&prog.image()[i]);
  320. iICODE last_current_insn = (++Icode.rbegin()).base();
  321. FollowCtrl (pcallGraph, &StCopy);
  322. ++last_current_insn; // incremented here because FollowCtrl might have adde more instructions after the Jmp
  323. last_current_insn->ll()->caseEntry = k++;
  324. last_current_insn->ll()->setFlags(CASE);
  325. pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
  326. }
  327. return true;
  328. }
  329. bool Function::decodeIndirectJMP(ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
  330. {
  331. PROG &prog(Project::get()->prog);
  332. // mov cx,NUM_CASES
  333. // mov bx,JUMP_TABLE
  334. // LAB1:
  335. // mov ax, [bx]
  336. // cmp ax,VAL
  337. // jz LAB2
  338. // add bx,2
  339. // loop LAB1
  340. // jmp DEFAULT_CASE
  341. // LAB2
  342. // jmp word ptr [bx+2*NUM_CASES]
  343. static const llIcode match_seq[] = {iMOV,iMOV,iMOV,iCMP,iJE,iADD,iLOOP,iJMP,iJMP};
  344. if(Icode.size()<8)
  345. return false;
  346. if(&Icode.back()!=&pIcode) // not the last insn in the list skip it
  347. return false;
  348. if(pIcode.ll()->src().regi != INDEX_BX) {
  349. return false;
  350. }
  351. // find address-wise predecessors of the icode
  352. std::deque<ICODE *> matched;
  353. QMap<uint32_t,ICODE *> addrmap;
  354. for(ICODE & ic : Icode) {
  355. addrmap[ic.ll()->GetLlLabel()] = &ic;
  356. }
  357. auto iter = addrmap.find(pIcode.ll()->GetLlLabel());
  358. while(matched.size()<9) {
  359. matched.push_front(*iter);
  360. --iter;
  361. if(iter==addrmap.end())
  362. return false;
  363. }
  364. // pattern starts at the last jmp
  365. ICODE *load_num_cases = matched[0];
  366. ICODE *load_jump_table_addr = matched[1];
  367. ICODE *read_case_entry_insn = matched[2];
  368. ICODE *cmp_case_val_insn = matched[3];
  369. ICODE *exit_loop_insn = matched[4];
  370. ICODE *add_bx_insn = matched[5];
  371. ICODE *loop_insn = matched[6];
  372. ICODE *default_jmp = matched[7];
  373. ICODE *last_jmp = matched[8];
  374. for(int i=0; i<8; ++i) {
  375. if(matched[i+1]->ll()->GetLlLabel()!=matched[i]->ll()->GetLlLabel()+matched[i]->ll()->numBytes) {
  376. qDebug() << "Matching jump pattern impossible - undecoded instructions in pattern area ";
  377. return false;
  378. }
  379. }
  380. for(int i=0; i<9; ++i) {
  381. if(matched[i]->ll()->getOpcode()!=match_seq[i]) {
  382. return false;
  383. }
  384. }
  385. // verify that bx+offset == 2* case count
  386. uint32_t num_cases = load_num_cases->ll()->src().getImm2();
  387. if(last_jmp->ll()->src().off != 2*num_cases)
  388. return false;
  389. const LLOperand &op = last_jmp->ll()->src();
  390. if(op.regi != INDEX_BX)
  391. return false;
  392. if(not load_jump_table_addr->ll()->src().isImmediate())
  393. return false;
  394. uint32_t cs = (uint32_t)(uint16_t)pstate->r[rCS] << 4;
  395. uint32_t table_addr = cs + load_jump_table_addr->ll()->src().getImm2();
  396. uint32_t default_label = cs + default_jmp->ll()->src().getImm2();
  397. setBits(BM_DATA, table_addr, num_cases*2 + num_cases*2); // num_cases of short values + num cases short ptrs
  398. pIcode.ll()->setFlags(SWITCH);
  399. for(int i=0; i<num_cases; ++i) {
  400. STATE StCopy = *pstate;
  401. uint32_t jump_target_location = table_addr + num_cases*2 + i*2;
  402. StCopy.IP = cs + *(uint16_t *)(prog.image()+jump_target_location);
  403. iICODE last_current_insn = (++Icode.rbegin()).base();
  404. FollowCtrl (pcallGraph, &StCopy);
  405. ++last_current_insn;
  406. last_current_insn->ll()->caseEntry = i;
  407. last_current_insn->ll()->setFlags(CASE);
  408. pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
  409. }
  410. return true;
  411. }
  412. bool Function::decodeIndirectJMP2(ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
  413. {
  414. PROG &prog(Project::get()->prog);
  415. // mov cx,NUM_CASES
  416. // mov bx,JUMP_TABLE
  417. // LAB1:
  418. // mov ax, [bx]
  419. // cmp ax, LOW_WORD_OF_VAL
  420. // jnz LAB2
  421. // mov ax, [bx + 2 * NUM_CASES]
  422. // cmp ax, HIGH_WORD_OF_VAL
  423. // jz LAB3
  424. // LAB2
  425. // add bx,2
  426. // loop LAB1
  427. // jmp DEFAULT_CASE
  428. // LAB3
  429. // jmp word ptr [bx+2*NUM_CASES]
  430. static const llIcode match_seq[] = {iMOV,iMOV,iMOV,iCMP,iJNE,iMOV,iCMP,iJE,iADD,iLOOP,iJMP,iJMP};
  431. if(Icode.size()<12)
  432. return false;
  433. if(&Icode.back() != &pIcode) // not the last insn in the list skip it
  434. return false;
  435. if(pIcode.ll()->src().regi != INDEX_BX) {
  436. return false;
  437. }
  438. // find address-wise predecessors of the icode
  439. std::deque<ICODE *> matched;
  440. QMap<uint32_t,ICODE *> addrmap;
  441. for(ICODE & ic : Icode) {
  442. addrmap[ic.ll()->GetLlLabel()] = &ic;
  443. }
  444. auto iter = addrmap.find(pIcode.ll()->GetLlLabel());
  445. while(matched.size()<12) {
  446. matched.push_front(*iter);
  447. --iter;
  448. if(iter==addrmap.end())
  449. return false;
  450. }
  451. // pattern starts at the last jmp
  452. ICODE *load_num_cases = matched[0];
  453. ICODE *load_jump_table_addr = matched[1];
  454. ICODE *default_jmp = matched[10];
  455. ICODE *last_jmp = matched[11];
  456. for(int i=0; i<11; ++i) {
  457. if(matched[i+1]->ll()->GetLlLabel()!=matched[i]->ll()->GetLlLabel()+matched[i]->ll()->numBytes) {
  458. qDebug() << "Matching jump pattern impossible - undecoded instructions in pattern area ";
  459. return false;
  460. }
  461. }
  462. for(int i=0; i<12; ++i) {
  463. if(matched[i]->ll()->getOpcode()!=match_seq[i]) {
  464. return false;
  465. }
  466. }
  467. // verify that bx+offset == 2* case count
  468. uint32_t num_cases = load_num_cases->ll()->src().getImm2();
  469. if(last_jmp->ll()->src().off != 4*num_cases)
  470. return false;
  471. const LLOperand &op = last_jmp->ll()->src();
  472. if(op.regi != INDEX_BX)
  473. return false;
  474. if(not load_jump_table_addr->ll()->src().isImmediate())
  475. return false;
  476. uint32_t cs = (uint32_t)(uint16_t)pstate->r[rCS] << 4;
  477. uint32_t table_addr = cs + load_jump_table_addr->ll()->src().getImm2();
  478. int default_label = cs + default_jmp->ll()->src().getImm2();
  479. setBits(BM_DATA, table_addr, num_cases*4 + num_cases*2); // num_cases of long values + num cases short ptrs
  480. pIcode.ll()->setFlags(SWITCH);
  481. for(int i=0; i<num_cases; ++i) {
  482. STATE StCopy = *pstate;
  483. uint32_t jump_target_location = table_addr + num_cases*4 + i*2;
  484. StCopy.IP = cs + *(uint16_t *)(prog.image()+jump_target_location);
  485. iICODE last_current_insn = (++Icode.rbegin()).base();
  486. FollowCtrl (pcallGraph, &StCopy);
  487. ++last_current_insn;
  488. last_current_insn->ll()->caseEntry = i;
  489. last_current_insn->ll()->setFlags(CASE);
  490. pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
  491. }
  492. return true;
  493. }
  494. bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
  495. {
  496. PROG &prog(Project::get()->prog);
  497. static uint8_t i2r[4] = {rSI, rDI, rBP, rBX};
  498. ICODE _Icode;
  499. uint32_t cs, offTable, endTable;
  500. uint32_t i, k, seg, target;
  501. if (pIcode.ll()->testFlags(I))
  502. {
  503. if (pIcode.ll()->getOpcode() == iJMPF)
  504. pstate->setState( rCS, LH(prog.image() + pIcode.ll()->label + 3));
  505. pstate->IP = pIcode.ll()->src().getImm2();
  506. int64_t i = pIcode.ll()->src().getImm2();
  507. if (i < 0)
  508. {
  509. exit(1);
  510. }
  511. /* Return true if jump target is already parsed */
  512. return Icode.alreadyDecoded(i);
  513. }
  514. /* We've got an indirect JMP - look for switch() stmt. idiom of the form
  515. * JMP uint16_t ptr word_offset[rBX | rSI | rDI] */
  516. seg = (pIcode.ll()->src().seg)? pIcode.ll()->src().seg: rDS;
  517. /* Ensure we have a uint16_t offset & valid seg */
  518. if (pIcode.ll()->match(iJMP) and (pIcode.ll()->testFlags(WORD_OFF)) and
  519. pstate->f[seg] and
  520. (pIcode.ll()->src().regi == INDEX_SI or
  521. pIcode.ll()->src().regi == INDEX_DI or /* Idx reg. BX, SI, DI */
  522. pIcode.ll()->src().regi == INDEX_BX))
  523. {
  524. offTable = ((uint32_t)(uint16_t)pstate->r[seg] << 4) + pIcode.ll()->src().off;
  525. /* Firstly look for a leading range check of the form:-
  526. * CMP {BX | SI | DI}, immed
  527. * JA | JAE | JB | JBE
  528. * This is stored in the current state as if we had just
  529. * followed a JBE branch (i.e. [reg] lies between 0 - immed).
  530. */
  531. if (pstate->JCond.regi == i2r[pIcode.ll()->src().regi-(INDEX_BX_SI+4)])
  532. endTable = offTable + pstate->JCond.immed;
  533. else
  534. endTable = (uint32_t)prog.cbImage;
  535. /* Search for first uint8_t flagged after start of table */
  536. for (i = offTable; i <= endTable; i++)
  537. if (BITMAP(i, BM_CODE | BM_DATA))
  538. break;
  539. endTable = i & ~1; /* Max. possible table size */
  540. /* Now do some heuristic pruning. Look for ptrs. into the table
  541. * and for addresses that don't appear to point to valid code.
  542. */
  543. cs = (uint32_t)(uint16_t)pstate->r[rCS] << 4;
  544. for (i = offTable; i < endTable; i += 2)
  545. {
  546. target = cs + LH(&prog.image()[i]);
  547. if (target < endTable and target >= offTable)
  548. endTable = target;
  549. else if (target >= (uint32_t)prog.cbImage)
  550. endTable = i;
  551. }
  552. for (i = offTable; i < endTable; i += 2)
  553. {
  554. target = cs + LH(&prog.image()[i]);
  555. /* Be wary of 00 00 as code - it's probably data */
  556. if (not (prog.image()[target] or prog.image()[target+1]) or
  557. scan(target, _Icode))
  558. endTable = i;
  559. }
  560. /* Now for each entry in the table take a copy of the current
  561. * state and recursively call FollowCtrl(). */
  562. if (offTable < endTable)
  563. {
  564. assert(((endTable - offTable) / 2)<512);
  565. STATE StCopy;
  566. //int ip;
  567. //uint32_t *psw;
  568. setBits(BM_DATA, offTable, endTable - offTable);
  569. pIcode.ll()->setFlags(SWITCH);
  570. //pIcode.ll()->caseTbl2.numEntries = (endTable - offTable) / 2;
  571. for (i = offTable, k = 0; i < endTable; i += 2)
  572. {
  573. StCopy = *pstate;
  574. StCopy.IP = cs + LH(&prog.image()[i]);
  575. iICODE last_current_insn = (++Icode.rbegin()).base();
  576. //ip = Icode.size();
  577. FollowCtrl (pcallGraph, &StCopy);
  578. ++last_current_insn;
  579. last_current_insn->ll()->caseEntry = k++;
  580. last_current_insn->ll()->setFlags(CASE);
  581. pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
  582. }
  583. return true;
  584. }
  585. }
  586. if(decodeIndirectJMP(pIcode,pstate,pcallGraph)) {
  587. return true;
  588. }
  589. if(decodeIndirectJMP2(pIcode,pstate,pcallGraph)) {
  590. return true;
  591. }
  592. /* Can't do anything with this jump */
  593. flg |= PROC_IJMP;
  594. flg &= ~TERMINATES;
  595. interactDis(this, this->Icode.size()-1);
  596. return true;
  597. }
  598. /* Process procedure call.
  599. * Note: We assume that CALL's will return unless there is good evidence to
  600. * the contrary - thus we return false unless all paths in the called
  601. * procedure end in DOS exits. This is reasonable since C procedures
  602. * will always include the epilogue after the call anyway and it's to
  603. * be assumed that if an assembler program contains a CALL that the
  604. * programmer expected it to come back - otherwise surely a JMP would
  605. * have been used. */
  606. bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *pstate)
  607. {
  608. PROG &prog(Project::get()->prog);
  609. ICODE &last_insn(Icode.back());
  610. STATE localState; /* Local copy of the machine state */
  611. uint32_t off;
  612. /* For Indirect Calls, find the function address */
  613. bool indirect = false;
  614. //pIcode.ll()->immed.proc.proc=fakeproc;
  615. if ( not pIcode.ll()->testFlags(I) )
  616. {
  617. /* Not immediate, i.e. indirect call */
  618. if (pIcode.ll()->m_dst.regi and (not option.Calls))
  619. {
  620. /* We have not set the brave option to attempt to follow
  621. the execution path through register indirect calls.
  622. So we just exit this function, and ignore the call.
  623. We probably should not have parsed this deep, anyway. */
  624. return false;
  625. }
  626. /* Offset into program image is seg:off of read input */
  627. /* Note: this assumes that the pointer itself is at
  628. es:0 where es:0 is the start of the image. This is
  629. usually wrong! Consider also CALL [BP+0E] in which the
  630. segment for the pointer is in SS! - Mike */
  631. if(pIcode.ll()->m_dst.isReg())
  632. {
  633. if( not pstate->isKnown(pIcode.ll()->m_dst.regi)
  634. or
  635. not pstate->isKnown(pIcode.ll()->m_dst.seg)
  636. )
  637. {
  638. fprintf(stderr,"Indirect call with unknown register values\n");
  639. return false;
  640. }
  641. off = pstate->r[pIcode.ll()->m_dst.seg];
  642. off <<=4;
  643. off += pstate->r[pIcode.ll()->m_dst.regi];
  644. }
  645. else
  646. {
  647. off = (uint32_t)(uint16_t)pIcode.ll()->m_dst.off +
  648. ((uint32_t)(uint16_t)pIcode.ll()->m_dst.segValue << 4);
  649. }
  650. /* Address of function is given by 4 (CALLF) or 2 (CALL) bytes at
  651. * previous offset into the program image */
  652. uint32_t tgtAddr=0;
  653. if (pIcode.ll()->getOpcode() == iCALLF)
  654. tgtAddr= LH(&prog.image()[off]) + ((uint32_t)(LH(&prog.image()[off+2])) << 4);
  655. else
  656. tgtAddr= LH(&prog.image()[off]) + ((uint32_t)(uint16_t)state.r[rCS] << 4);
  657. pIcode.ll()->replaceSrc(LLOperand::CreateImm2( tgtAddr ) );
  658. pIcode.ll()->setFlags(I);
  659. indirect = true;
  660. }
  661. /* Process CALL. Function address is located in pIcode.ll()->immed.op */
  662. if (pIcode.ll()->testFlags(I))
  663. {
  664. /* Search procedure list for one with appropriate entry point */
  665. ilFunction iter = Project::get()->findByEntry(pIcode.ll()->src().getImm2());
  666. /* Create a new procedure node and save copy of the state */
  667. if ( not Project::get()->valid(iter) )
  668. {
  669. iter = Project::get()->createFunction(0,"");
  670. Function &x(*iter);
  671. x.procEntry = pIcode.ll()->src().getImm2();
  672. LibCheck(x);
  673. if (x.flg & PROC_ISLIB)
  674. {
  675. /* A library function. No need to do any more to it */
  676. pcallGraph->insertCallGraph (this, iter);
  677. //iter = (++pProcList.rbegin()).base();
  678. last_insn.ll()->src().proc.proc = &x;
  679. return false;
  680. }
  681. if (indirect)
  682. x.flg |= PROC_ICALL;
  683. if (x.name.empty()) /* Don't overwrite existing name */
  684. {
  685. ostringstream os;
  686. os<<"proc_"<< ++prog.cProcs;
  687. x.name = os.str();
  688. }
  689. x.depth = x.depth + 1;
  690. x.flg |= TERMINATES;
  691. /* Save machine state in localState, load up IP and CS.*/
  692. localState = *pstate;
  693. pstate->IP = pIcode.ll()->src().getImm2();
  694. if (pIcode.ll()->getOpcode() == iCALLF)
  695. pstate->setState( rCS, LH(prog.image() + pIcode.ll()->label + 3));
  696. x.state = *pstate;
  697. /* Insert new procedure in call graph */
  698. pcallGraph->insertCallGraph (this, iter);
  699. /* Process new procedure */
  700. x.FollowCtrl (pcallGraph, pstate);
  701. /* Restore segment registers & IP from localState */
  702. pstate->IP = localState.IP;
  703. pstate->setState( rCS, localState.r[rCS]);
  704. pstate->setState( rDS, localState.r[rDS]);
  705. pstate->setState( rES, localState.r[rES]);
  706. pstate->setState( rSS, localState.r[rSS]);
  707. }
  708. else
  709. Project::get()->callGraph->insertCallGraph (this, iter);
  710. last_insn.ll()->src().proc.proc = &(*iter); // ^ target proc
  711. /* return ((p->flg & TERMINATES) != 0); */
  712. }
  713. return false; // Cristina, please check!!
  714. }
  715. /* process_MOV - Handles state changes due to simple assignments */
  716. static void process_MOV(LLInst & ll, STATE * pstate)
  717. {
  718. PROG &prog(Project::get()->prog);
  719. SYM * psym, *psym2; /* Pointer to symbol in global symbol table */
  720. uint8_t dstReg = ll.m_dst.regi;
  721. uint8_t srcReg = ll.src().regi;
  722. if (dstReg > 0 and dstReg < INDEX_BX_SI)
  723. {
  724. if (ll.testFlags(I))
  725. pstate->setState( dstReg, (int16_t)ll.src().getImm2());
  726. else if (srcReg == 0) /* direct memory offset */
  727. {
  728. psym = lookupAddr(&ll.src(), pstate, 2, eDuVal::USE);
  729. if (psym and ((psym->flg & SEG_IMMED) or psym->duVal.val))
  730. pstate->setState( dstReg, LH(&prog.image()[psym->label]));
  731. }
  732. else if (srcReg < INDEX_BX_SI and pstate->f[srcReg]) /* reg */
  733. {
  734. pstate->setState( dstReg, pstate->r[srcReg]);
  735. /* Follow moves of the possible index register */
  736. if (pstate->JCond.regi == srcReg)
  737. pstate->JCond.regi = dstReg;
  738. }
  739. }
  740. else if (dstReg == 0) { /* direct memory offset */
  741. int size=2;
  742. if((ll.src().regi>=rAL)and(ll.src().regi<=rBH))
  743. size=1;
  744. psym = lookupAddr (&ll.m_dst, pstate, size, eDEF);
  745. if (psym and not (psym->duVal.val)) /* no initial value yet */
  746. {
  747. if (ll.testFlags(I)) /* immediate */
  748. {
  749. //prog.image()[psym->label] = (uint8_t)ll.src().getImm2();
  750. pstate->setMemoryByte(psym->label,(uint8_t)ll.src().getImm2());
  751. if(psym->size>1)
  752. {
  753. pstate->setMemoryByte(psym->label+1,uint8_t(ll.src().getImm2()>>8));
  754. //prog.image()[psym->label+1] = (uint8_t)(ll.src().getImm2()>>8);
  755. }
  756. psym->duVal.val = 1;
  757. }
  758. else if (srcReg == 0) /* direct mem offset */
  759. {
  760. psym2 = lookupAddr (&ll.src(), pstate, 2, eDuVal::USE);
  761. if (psym2 and ((psym->flg & SEG_IMMED) or (psym->duVal.val)))
  762. {
  763. //prog.image()[psym->label] = (uint8_t)prog.image()[psym2->label];
  764. pstate->setMemoryByte(psym->label,(uint8_t)prog.image()[psym2->label]);
  765. if(psym->size>1)
  766. {
  767. pstate->setMemoryByte(psym->label+1,(uint8_t)prog.image()[psym2->label+1]);
  768. //prog.image()[psym->label+1] = prog.image()[psym2->label+1];//(uint8_t)(prog.image()[psym2->label+1] >> 8);
  769. }
  770. psym->duVal.setFlags(eDuVal::DEF);
  771. psym2->duVal.setFlags(eDuVal::USE);
  772. }
  773. }
  774. else if (srcReg < INDEX_BX_SI and pstate->f[srcReg]) /* reg */
  775. {
  776. //prog.image()[psym->label] = (uint8_t)pstate->r[srcReg];
  777. pstate->setMemoryByte(psym->label,(uint8_t)pstate->r[srcReg]);
  778. if(psym->size>1)
  779. {
  780. pstate->setMemoryByte(psym->label,(uint8_t)pstate->r[srcReg]>>8);
  781. //prog.image()[psym->label+1] = (uint8_t)(pstate->r[srcReg] >> 8);
  782. }
  783. psym->duVal.setFlags(eDuVal::DEF);
  784. }
  785. }
  786. }
  787. }
  788. /* Updates the offset entry to the stack frame table (arguments),
  789. * and returns a pointer to such entry. */
  790. void STKFRAME::updateFrameOff ( int16_t off, int _size, uint16_t duFlag)
  791. {
  792. /* Check for symbol in stack frame table */
  793. auto iter=findByLabel(off);
  794. if(iter!=end())
  795. {
  796. if (iter->size < _size)
  797. {
  798. iter->size = _size;
  799. }
  800. }
  801. else
  802. {
  803. char nm[16];
  804. STKSYM new_sym;
  805. sprintf (nm, "arg%" PRIu64, uint64_t(size()));
  806. new_sym.name = nm;
  807. new_sym.label= off;
  808. new_sym.size = _size;
  809. new_sym.type = TypeContainer::defaultTypeForSize(_size);
  810. if (duFlag == eDuVal::USE) /* must already have init value */
  811. {
  812. new_sym.duVal.use=1;
  813. //new_sym.duVal.val=1;
  814. }
  815. else
  816. {
  817. new_sym.duVal.setFlags(duFlag);
  818. }
  819. push_back(new_sym);
  820. this->numArgs++;
  821. }
  822. /* Save maximum argument offset */
  823. if ((uint32_t)this->maxOff < (off + (uint32_t)_size))
  824. this->maxOff = off + (int16_t)_size;
  825. }
  826. /* lookupAddr - Looks up a data reference in the symbol table and stores it
  827. * if necessary.
  828. * Returns a pointer to the symbol in the
  829. * symbol table, or Null if it's not a direct memory offset. */
  830. static SYM * lookupAddr (LLOperand *pm, STATE *pstate, int size, uint16_t duFlag)
  831. {
  832. PROG &prog(Project::get()->prog);
  833. int i;
  834. SYM * psym=nullptr;
  835. uint32_t operand;
  836. bool created_new=false;
  837. if (pm->regi != rUNDEF)
  838. return nullptr; // register or indexed
  839. /* Global var */
  840. if (pm->segValue) /* there is a value in the seg field */
  841. {
  842. operand = opAdr (pm->segValue, pm->off);
  843. psym = Project::get()->symtab.updateGlobSym (operand, size, duFlag,created_new);
  844. }
  845. else if (pstate->f[pm->seg]) /* new value */
  846. {
  847. pm->segValue = pstate->r[pm->seg];
  848. operand = opAdr(pm->segValue, pm->off);
  849. psym = Project::get()->symtab.updateGlobSym (operand, size, duFlag,created_new);
  850. /* Flag new memory locations that are segment values */
  851. if (created_new)
  852. {
  853. if (size == 4)
  854. operand += 2; /* High uint16_t */
  855. for (i = 0; i < prog.cReloc; i++)
  856. if (prog.relocTable[i] == operand) {
  857. psym->flg = SEG_IMMED;
  858. break;
  859. }
  860. }
  861. }
  862. /* Check for out of bounds */
  863. if (psym and (psym->label < (uint32_t)prog.cbImage))
  864. return psym;
  865. return nullptr;
  866. }
  867. /* setState - Assigns a value to a reg. */
  868. void STATE::setState(uint16_t reg, int16_t value)
  869. {
  870. value &= 0xFFFF;
  871. r[reg] = value;
  872. f[reg] = true;
  873. switch (reg) {
  874. case rAX: case rCX: case rDX: case rBX:
  875. r[reg + rAL - rAX] = value & 0xFF;
  876. f[reg + rAL - rAX] = true;
  877. r[reg + rAH - rAX] = (value >> 8) & 0xFF;
  878. f[reg + rAH - rAX] = true;
  879. break;
  880. case rAL: case rCL: case rDL: case rBL:
  881. if (f[reg - rAL + rAH]) {
  882. r[reg - rAL + rAX] =(r[reg - rAL + rAH] << 8) + (value & 0xFF);
  883. f[reg - rAL + rAX] = true;
  884. }
  885. break;
  886. case rAH: case rCH: case rDH: case rBH:
  887. if (f[reg - rAH + rAL])
  888. {
  889. r[reg - rAH + rAX] = r[reg - rAH + rAL] + ((value & 0xFF) << 8);
  890. f[reg - rAH + rAX] = true;
  891. }
  892. break;
  893. }
  894. }
  895. /* labelSrchRepl - Searches Icode for instruction with label = target, and
  896. replaces *pIndex with an icode index */
  897. /* setBits - Sets memory bitmap bits for BM_CODE or BM_DATA (additively) */
  898. static void setBits(int16_t type, uint32_t start, uint32_t len)
  899. {
  900. PROG &prog(Project::get()->prog);
  901. uint32_t i;
  902. if (start < (uint32_t)prog.cbImage)
  903. {
  904. if (start + len > (uint32_t)prog.cbImage)
  905. len = (uint32_t)(prog.cbImage - start);
  906. for (i = start + len - 1; i >= start; i--)
  907. {
  908. prog.map[i >> 2] |= type << ((i & 3) << 1);
  909. if (i == 0) break; // Fixes inf loop!
  910. }
  911. }
  912. }
  913. /* Checks which registers were used and updates the du.u flag.
  914. * Places local variables on the local symbol table.
  915. * Arguments: d : SRC or DST icode operand
  916. * pIcode: ptr to icode instruction
  917. * pProc : ptr to current procedure structure
  918. * pstate: ptr to current procedure state
  919. * size : size of the operand
  920. * ix : current index into icode array */
  921. static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int size)
  922. {
  923. const LLOperand * pm = pIcode.ll()->get(d) ;
  924. SYM * psym;
  925. if ( Machine_X86::isMemOff(pm->regi) )
  926. {
  927. if (pm->regi == INDEX_BP) /* indexed on bp */
  928. {
  929. if (pm->off >= 2)
  930. pProc->args.updateFrameOff ( pm->off, size, eDuVal::USE);
  931. else if (pm->off < 0)
  932. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
  933. }
  934. else if (pm->regi == INDEX_BP_SI or pm->regi == INDEX_BP_DI)
  935. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off,
  936. (uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
  937. else if ((pm->regi >= INDEX_SI) and (pm->regi <= INDEX_BX))
  938. {
  939. if ((pm->seg == rDS) and (pm->regi == INDEX_BX)) /* bx */
  940. {
  941. if (pm->off > 0) /* global indexed variable */
  942. pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,TYPE_WORD_SIGN);
  943. }
  944. pIcode.du.use.addReg(pm->regi);
  945. }
  946. else
  947. {
  948. psym = lookupAddr(const_cast<LLOperand *>(pm), pstate, size, eDuVal::USE);
  949. if( nullptr != psym )
  950. {
  951. setBits (BM_DATA, psym->label, (uint32_t)size);
  952. pIcode.ll()->setFlags(SYM_USE);
  953. pIcode.ll()->caseEntry = distance(&Project::get()->symtab[0],psym); //WARNING: was setting case count
  954. }
  955. }
  956. }
  957. /* Use of register */
  958. else if ((d == DST) or ((d == SRC) and (not pIcode.ll()->testFlags(I))))
  959. pIcode.du.use.addReg(pm->regi);
  960. }
  961. /* Checks which registers were defined (ie. got a new value) and updates the
  962. * du.d flag.
  963. * Places local variables in the local symbol table. */
  964. static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int size)
  965. {
  966. LLOperand *pm = pIcode.ll()->get(d);
  967. if (pm->regi==0)
  968. {
  969. SYM * psym;
  970. psym = lookupAddr(pm, pstate, size, eDEF);
  971. if (nullptr!=psym)
  972. {
  973. setBits(BM_DATA, psym->label, (uint32_t)size);
  974. pIcode.ll()->setFlags(SYM_DEF);
  975. pIcode.ll()->caseEntry = distance(&Project::get()->symtab[0],psym); // WARNING: was setting Case count
  976. }
  977. }
  978. else if (pm->regi >= INDEX_BX_SI)
  979. {
  980. if (pm->regi == INDEX_BP) /* indexed on bp */
  981. {
  982. if (pm->off >= 2)
  983. pProc->args.updateFrameOff ( pm->off, size, eDEF);
  984. else if (pm->off < 0)
  985. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
  986. }
  987. else if (pm->regi == INDEX_BP_SI or pm->regi == INDEX_BP_DI)
  988. {
  989. pProc->localId.newByteWordStk(TYPE_WORD_SIGN, pm->off,
  990. (uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
  991. }
  992. else if ((pm->regi >= INDEX_SI) and (pm->regi <= INDEX_BX))
  993. {
  994. if ((pm->seg == rDS) and (pm->regi == INDEX_BX)) /* bx */
  995. {
  996. if (pm->off > 0) /* global var */
  997. pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,TYPE_WORD_SIGN);
  998. }
  999. pIcode.du.use.addReg(pm->regi);
  1000. }
  1001. }
  1002. /* Definition of register */
  1003. else if ((d == DST) or ((d == SRC) and (not pIcode.ll()->testFlags(I))))
  1004. {
  1005. assert(not pIcode.ll()->match(iPUSH));
  1006. pIcode.du1.addDef(pm->regi);
  1007. pIcode.du.def.addReg(pm->regi);
  1008. }
  1009. }
  1010. /* use_def - operand is both use and def'd.
  1011. * Note: the destination will always be a register, stack variable, or global
  1012. * variable. */
  1013. static void use_def(opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int cb)
  1014. {
  1015. const LLOperand * pm = pIcode.ll()->get(d);
  1016. use (d, pIcode, pProc, pstate, cb);
  1017. if (pm->regi < INDEX_BX_SI) /* register */
  1018. {
  1019. assert(not pIcode.ll()->match(iPUSH));
  1020. pIcode.du1.addDef(pm->regi);
  1021. pIcode.du.def.addReg(pm->regi);
  1022. }
  1023. }
  1024. /* Set DU vector, local variables and arguments, and DATA bits in the
  1025. * bitmap */
  1026. extern LLOperand convertOperand(const x86_op_t &from);
  1027. void Function::process_operands(ICODE & pIcode, STATE * pstate)
  1028. {
  1029. LLInst &ll_ins(*pIcode.ll());
  1030. int sseg = (ll_ins.src().seg)? ll_ins.src().seg: rDS;
  1031. int cb = pIcode.ll()->testFlags(B) ? 1: 2;
  1032. //x86_op_t *im= pIcode.insn.x86_get_imm();
  1033. bool Imm = (pIcode.ll()->testFlags(I));
  1034. switch (pIcode.ll()->getOpcode()) {
  1035. case iAND: case iOR: case iXOR:
  1036. case iSAR: case iSHL: case iSHR:
  1037. case iRCL: case iRCR: case iROL: case iROR:
  1038. case iADD: case iADC: case iSUB: case iSBB:
  1039. if (not Imm) {
  1040. use(SRC, pIcode, this, pstate, cb);
  1041. }
  1042. case iINC: case iDEC: case iNEG: case iNOT:
  1043. case iAAA: case iAAD: case iAAM: case iAAS:
  1044. case iDAA: case iDAS:
  1045. use_def(DST, pIcode, this, pstate, cb);
  1046. break;
  1047. case iXCHG:
  1048. /* This instruction is replaced by 3 instructions, only need
  1049. * to define the src operand and use the destination operand
  1050. * in the mean time. */
  1051. use(SRC, pIcode, this, pstate, cb);
  1052. def(DST, pIcode, this, pstate, cb);
  1053. break;
  1054. case iTEST: case iCMP:
  1055. if (not Imm)
  1056. use(SRC, pIcode, this, pstate, cb);
  1057. use(DST, pIcode, this, pstate, cb);
  1058. break;
  1059. case iDIV: case iIDIV:
  1060. use(SRC, pIcode, this, pstate, cb);
  1061. if (cb == 1)
  1062. pIcode.du.use.addReg(rTMP);
  1063. break;
  1064. case iMUL: case iIMUL:
  1065. use(SRC, pIcode, this, pstate, cb);
  1066. if (! Imm)
  1067. {
  1068. use (DST, pIcode, this, pstate, cb);
  1069. if (cb == 1)
  1070. {
  1071. pIcode.du.def.addReg(rAX);
  1072. pIcode.du1.addDef(rAX);
  1073. }
  1074. else
  1075. {
  1076. pIcode.du.def.addReg(rAX).addReg(rDX);
  1077. pIcode.du1.addDef(rAX).addDef(rDX);
  1078. }
  1079. }
  1080. else
  1081. def (DST, pIcode, this, pstate, cb);
  1082. break;
  1083. case iSIGNEX:
  1084. cb = pIcode.ll()->testFlags(SRC_B) ? 1 : 2;
  1085. if (cb == 1) /* uint8_t */
  1086. {
  1087. pIcode.du.def.addReg(rAX);
  1088. pIcode.du1.addDef(rAX);
  1089. pIcode.du.use.addReg(rAL);
  1090. }
  1091. else /* uint16_t */
  1092. {
  1093. pIcode.du.def.addReg(rDX).addReg(rAX);
  1094. pIcode.du1.addDef(rAX).addDef(rDX);
  1095. pIcode.du.use.addReg(rAX);
  1096. }
  1097. break;
  1098. case iCALLF: /* Ignore def's on CS for now */
  1099. cb = 4;
  1100. case iCALL: case iPUSH: case iPOP:
  1101. if (! Imm) {
  1102. if (pIcode.ll()->getOpcode() == iPOP)
  1103. def(DST, pIcode, this, pstate, cb);
  1104. else
  1105. use(DST, pIcode, this, pstate, cb);
  1106. }
  1107. break;
  1108. case iESC: /* operands may be larger */
  1109. use(DST, pIcode, this, pstate, cb);
  1110. break;
  1111. case iLDS: case iLES:
  1112. {
  1113. eReg r=((pIcode.ll()->getOpcode() == iLDS) ? rDS : rES);
  1114. pIcode.du.def.addReg(r);
  1115. pIcode.du1.addDef(r);
  1116. cb = 4;
  1117. // fallthrough
  1118. }
  1119. case iMOV:
  1120. use(SRC, pIcode, this, pstate, cb);
  1121. def(DST, pIcode, this, pstate, cb);
  1122. break;
  1123. case iLEA:
  1124. use(SRC, pIcode, this, pstate, 2);
  1125. def(DST, pIcode, this, pstate, 2);
  1126. break;
  1127. case iBOUND:
  1128. use(SRC, pIcode, this, pstate, 4);
  1129. use(DST, pIcode, this, pstate, cb);
  1130. break;
  1131. case iJMPF:
  1132. cb = 4;
  1133. case iJMP:
  1134. if (! Imm)
  1135. use(SRC, pIcode, this, pstate, cb);
  1136. break;
  1137. case iLOOP: case iLOOPE: case iLOOPNE:
  1138. pIcode.du.def.addReg(rCX);
  1139. pIcode.du1.addDef(rCX);
  1140. case iJCXZ:
  1141. pIcode.du.use.addReg(rCX);
  1142. break;
  1143. case iREPNE_CMPS: case iREPE_CMPS: case iREP_MOVS:
  1144. pIcode.du.addDefinedAndUsed(rCX);
  1145. pIcode.du1.addDef(rCX);
  1146. case iCMPS: case iMOVS:
  1147. pIcode.du.addDefinedAndUsed(rSI);
  1148. pIcode.du.addDefinedAndUsed(rDI);
  1149. pIcode.du1.addDef(rSI).addDef(rDI);
  1150. pIcode.du.use.addReg(rES).addReg(sseg);
  1151. break;
  1152. case iREPNE_SCAS: case iREPE_SCAS: case iREP_STOS: case iREP_INS:
  1153. pIcode.du.addDefinedAndUsed(rCX);
  1154. pIcode.du1.addDef(rCX);
  1155. case iSCAS: case iSTOS: case iINS:
  1156. pIcode.du.def.addReg(rDI);
  1157. pIcode.du1.addDef(rDI);
  1158. if (pIcode.ll()->getOpcode() == iREP_INS or pIcode.ll()->getOpcode()== iINS)
  1159. {
  1160. pIcode.du.use.addReg(rDI).addReg(rES).addReg(rDX);
  1161. }
  1162. else
  1163. {
  1164. pIcode.du.use.addReg(rDI).addReg(rES).addReg((cb == 2)? rAX: rAL);
  1165. }
  1166. break;
  1167. case iREP_LODS:
  1168. pIcode.du.addDefinedAndUsed(rCX);
  1169. pIcode.du1.addDef(rCX);
  1170. case iLODS:
  1171. {
  1172. eReg r = (cb==2)? rAX: rAL;
  1173. pIcode.du.addDefinedAndUsed(rSI);
  1174. pIcode.du1.addDef(rSI);
  1175. pIcode.du.def.addReg(r);
  1176. pIcode.du1.addDef(r);
  1177. pIcode.du.use.addReg(sseg);
  1178. }
  1179. break;
  1180. case iREP_OUTS:
  1181. pIcode.du.addDefinedAndUsed(rCX);
  1182. pIcode.du1.addDef(rCX);
  1183. case iOUTS:
  1184. pIcode.du.addDefinedAndUsed(rSI);
  1185. pIcode.du1.addDef(rSI);
  1186. pIcode.du.use.addReg(rDX).addReg(sseg);
  1187. break;
  1188. case iIN: case iOUT:
  1189. def(DST, pIcode, this, pstate, cb);
  1190. if (! Imm)
  1191. {
  1192. pIcode.du.use.addReg(rDX);
  1193. }
  1194. break;
  1195. }
  1196. for (int i = rSP; i <= rBH; i++) /* Kill all defined registers */
  1197. if (pIcode.ll()->flagDU.d & (1 << i))
  1198. pstate->f[i] = false;
  1199. }