Fix jumptable parsing:
[dyninst.git] / parseAPI / src / IA_powerDetails.C
1 /*
2  * See the dyninst/COPYRIGHT file for copyright information.
3  * 
4  * We provide the Paradyn Tools (below described as "Paradyn")
5  * on an AS IS basis, and do not warrant its validity or performance.
6  * We reserve the right to update, modify, or discontinue this
7  * software at any time.  We shall have no obligation to supply such
8  * updates or modifications or any other form of support to you.
9  * 
10  * By your use of Paradyn, you understand and agree that we (or any
11  * other person or entity with proprietary rights in Paradyn) are
12  * under no obligation to provide either maintenance services,
13  * update services, notices of latent defects, or correction of
14  * defects for Paradyn.
15  * 
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  * 
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  * 
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29  */
30
31 #include "IA_powerDetails.h"
32 #include "Visitor.h"
33 #include "Register.h"
34 #include "Dereference.h"
35 #include "Immediate.h"
36 #include "BinaryFunction.h"
37 #include "debug_parse.h"
38 #include <deque>
39 #include <boost/bind.hpp>
40 #include <algorithm>
41 #include <iterator>
42 #include <boost/iterator/indirect_iterator.hpp>
43
44
45 using namespace Dyninst;
46 using namespace InstructionAPI;
47 using namespace Dyninst::InsnAdapter;
48 using namespace Dyninst::ParseAPI;
49
50
51 namespace Dyninst
52 {
53   namespace InsnAdapter
54   {
55     namespace detail
56     {
57       class TOCandOffsetExtractor : public Dyninst::InstructionAPI::Visitor
58       {
59       public:
60         TOCandOffsetExtractor(Address TOCvalue) : toc_contents(TOCvalue) {}
61         virtual ~TOCandOffsetExtractor() {}
62         virtual void visit(BinaryFunction* b) {
63           Address arg1 = m_stack.front();
64           m_stack.pop_front();
65           Address arg2 = m_stack.front();
66           m_stack.pop_front();
67           if(b->isAdd()) {
68             result = arg1 + arg2;
69           } else if(b->isMultiply()) {
70             result = arg1 * arg2;
71           } else {
72             assert(!"unexpected binary function!");
73             result = 0;
74           }
75           parsing_printf("\tTOC visitor visiting binary function, result is 0x%lx\n",
76                          result);
77           m_stack.push_front(result);
78         }
79         virtual void visit(Immediate* i) {
80           Address tmp = i->eval().convert<Address>();
81           result = tmp;
82           parsing_printf("\tTOC visitor visiting immediate, result is 0x%lx\n",
83                          result);
84           m_stack.push_front(tmp);
85         }
86         virtual void visit(RegisterAST* r) {
87           if(r->getID() == toc_reg->getID()) {
88             m_stack.push_front(toc_contents);
89           } else {
90             m_stack.push_front(0);
91           }
92           result = m_stack.front();
93           parsing_printf("\tTOC visitor visiting register, result is 0x%lx\n",
94                          result);
95         }
96         virtual void visit(Dereference*) {}
97         void clear() {
98           m_stack.clear();
99           result = 0;
100         }
101         std::deque<Address> m_stack;
102         Address result;
103         Address toc_contents;
104         RegisterAST::Ptr toc_reg;
105       };
106     }
107   }
108 };
109
110
111 bool IA_powerDetails::findTableAddrNoTOC(const IA_IAPI* blockToCheck)
112 {
113   std::set<RegisterAST::Ptr> regs;
114   std::set<RegisterAST::Ptr> writeregs, readregs;
115   RegisterAST::Ptr writereg, readreg;
116   int dfgreg;
117   std::set<RegisterAST::Ptr>::iterator itw, itr;
118   std::set<int>::iterator itd;
119   toc_visitor->clear();
120   bool foundAddis = false;
121   bool foundAddi = false;
122   bool foundDep = false;
123   while(patternIter != blockToCheck->allInsns.begin())
124     {
125       patternIter--;
126       // Do backward dataflow analysis to match the registers that compute the jump table address 
127       parsing_printf("\tchecking insn %s at 0x%lx\n", patternIter->second->format().c_str(),
128                      patternIter->first);
129       // Ignore rlwinm instruction since its used for the index and not the table start.
130       // Also, remove it from the backwards DFG
131       if(patternIter->second->getOperation().getID() == power_op_rlwinm){
132         patternIter->second->getWriteSet(writeregs);
133         for(itw=writeregs.begin(); itw!= writeregs.end(); itw++){
134           writereg=*(itw);
135           for(itd=dfgregs.begin(); itd!= dfgregs.end(); itd++){
136             dfgreg = *itd;
137             if (writereg->getID() == dfgreg) {
138               parsing_printf("found Match - erasing rlwinm  \n");
139               dfgregs.erase(*itd);
140             }
141           }
142         }
143         continue;
144       }
145
146       writeregs.clear();
147       patternIter->second->getWriteSet(writeregs);
148       foundDep =false;
149
150       for(itw=writeregs.begin(); itw!= writeregs.end(); itw++){
151         writereg=*(itw); 
152         parsing_printf("Register Written %s \n", writereg->format().c_str());
153         for(itd=dfgregs.begin(); itd!= dfgregs.end(); itd++){
154           dfgreg = *itd;
155           parsing_printf("DFG has %d \n", dfgreg);
156           if (writereg->getID() == dfgreg) {
157             parsing_printf("found Match \n");
158             dfgregs.erase(*itd);
159             readregs.clear();
160             patternIter->second->getReadSet(readregs);
161             for(itr=readregs.begin(); itr!= readregs.end(); itr++){
162               readreg=*(itr); 
163               dfgregs.insert(readreg->getID());
164               parsing_printf("Reading %s \n", readreg->format().c_str());
165             }
166             foundDep = true;
167             break;
168           }
169         }
170       }
171       // We look for addi-addis combination. 
172       // These instruction can occur in any order and in any block before the indirect branch. 
173       // Also, there may be more than one addi instruction.
174       // Hence, we use adjustTableStartAddress to keep track of immediate values from addi instructions.
175       if(foundDep && !foundAddis && 
176          (patternIter->second->getOperation().getID() == power_op_addi || 
177           patternIter->second->getOperation().getID() == power_op_addic))
178         {
179           std::set<RegisterAST::Ptr> tmpregs;
180           patternIter->second->getReadSet(tmpregs);
181           if(tmpregs.size() != 1) {
182             continue;
183           }
184           regs.clear();
185           patternIter->second->getReadSet(regs);
186           if(regs.size() != 1) {
187             continue;
188           }
189           parsing_printf("\tfound 0x%lx: %s, checking for addis previous\n",
190                          patternIter->first,
191                          patternIter->second->format().c_str());
192           foundAddi = true;
193           toc_visitor->clear();
194           patternIter->second->getOperand(2).getValue()->apply(toc_visitor.get());
195           adjustTableStartAddress += toc_visitor->result;
196         }
197       else if(foundDep && !foundAddi && patternIter->second->getOperation().getID() == power_op_addis)
198         {
199           std::set<RegisterAST::Ptr> tmpregs;
200           patternIter->second->getReadSet(tmpregs);
201           if(tmpregs.size() != 1) {
202             continue;
203           }
204           regs.clear();
205           patternIter->second->getReadSet(regs);
206           if(regs.size() != 1) {
207             continue;
208           }
209           parsing_printf("\tfound 0x%lx: %s, checking for addi previous\n",
210                          patternIter->first,
211                          patternIter->second->format().c_str());
212           foundAddis = true;
213           toc_visitor->clear();
214           patternIter->second->getOperand(2).getValue()->apply(toc_visitor.get());
215           tableStartAddress = toc_visitor->result;
216           tableStartAddress *= 10000;
217           tableStartAddress &= 0xFFFF0000;
218            
219         } else if( foundDep && foundAddi &&
220                    patternIter->second &&
221                    (patternIter->second->getOperation().getID() == power_op_addis) &&
222                    patternIter->second->isWritten(*(regs.begin())))
223         {
224           foundAddis = true;
225           parsing_printf("\tfound 0x%lx: %s, setting tableStartAddress\n",
226                          patternIter->first,
227                          patternIter->second->format().c_str());
228           toc_visitor->clear();
229           patternIter->second->getOperand(2).getValue()->apply(toc_visitor.get());
230           tableStartAddress += (toc_visitor->result * 0x10000) & 0xFFFF0000;
231           parsing_printf("\ttableStartAddress = 0x%lx\n",
232                          tableStartAddress);
233           break;
234         } else if( foundDep && foundAddis && 
235                    patternIter->second &&
236                    ((patternIter->second->getOperation().getID() == power_op_addi) ||
237                     (patternIter->second->getOperation().getID() == power_op_addic)) &&
238                    patternIter->second->isWritten(*(regs.begin())))
239         {
240           foundAddi = true;
241           parsing_printf("\tfound 0x%lx: %s, setting tableStartAddress\n",
242                          patternIter->first,
243                          patternIter->second->format().c_str());
244           toc_visitor->clear();
245           patternIter->second->getOperand(2).getValue()->apply(toc_visitor.get());
246           tableStartAddress += toc_visitor->result;
247           parsing_printf("\ttableStartAddress = 0x%lx\n", tableStartAddress);
248           break;
249         }
250     }
251   if (!foundAddi || !foundAddis)
252     tableStartAddress = 0;
253   else
254     tableStartAddress += adjustTableStartAddress;
255
256   parsing_printf(" TABLE START 0x%lx 0x%lx %ld\n", tableStartAddress, adjustTableStartAddress, adjustTableStartAddress);
257
258   // If we've found an addi/addis combination and it's a relative table, look for a mfspr/thunk combination that
259   // feeds that...
260   if(tableStartAddress && tableIsRelative)
261     {
262       parsing_printf("\ttableStartAddress non-zero, tableIsRelative true\n");
263       bool foundThunk = false;
264       bool foundMFSPR = false;
265       Address GOTaddress = 0;
266       while(patternIter != blockToCheck->allInsns.begin())
267         {
268           patternIter--;
269           if(patternIter->second->getOperation().getID() == power_op_mfspr &&
270              patternIter->second->isWritten(*(regs.begin())))
271             {
272               foundMFSPR = true;
273               break;
274             }
275         }
276       while(patternIter != blockToCheck->allInsns.begin())
277         {
278           patternIter--;
279           if(patternIter->second->getCategory() == c_CallInsn) // mid-block call, must be a thunk
280             {
281               patternIter++;
282               parsing_printf("\tfound thunk/mfspr combo, adjusting tableStartAddress by 0x%lx\n", patternIter->first);
283               GOTaddress = tableStartAddress + patternIter->first;
284               foundThunk = true;
285               break;
286             }
287         }
288       if(foundThunk && foundMFSPR)
289         {
290           toc_visitor->toc_reg = *(regs.begin());
291           toc_reg = toc_visitor->toc_reg;
292           toc_visitor->toc_contents = GOTaddress;
293           tableStartAddress = 0;
294           patternIter = currentBlock->curInsnIter;
295           parsing_printf("\t calling parseRelativeTableIdiom with toc_reg %s\n", toc_visitor->toc_reg->format().c_str());
296           return parseRelativeTableIdiom();
297         }
298     }
299   else
300     {
301       parsing_printf("\ttableStartAddress = 0x%lx, tableIsRelative = %s\n", tableStartAddress,
302                      tableIsRelative ? "true" : "false");
303     }
304   return tableStartAddress == 0;
305 }
306
307 bool IA_powerDetails::parseRelativeTableIdiom()
308 {
309   bool foundAddress = false;
310   while(patternIter != currentBlock->allInsns.begin())
311     {
312       patternIter--;
313       parsing_printf("\t checking 0x%lx: %s for lwz/ld\n", patternIter->first, patternIter->second->format().c_str());
314       if((patternIter->second->getOperation().getID() == power_op_lwz ||
315           patternIter->second->getOperation().getID() == power_op_ld) &&
316          patternIter->second->isRead(toc_reg))
317         {
318           toc_visitor->clear();
319           patternIter->second->getOperand(1).getValue()->apply(toc_visitor.get());
320           parsing_printf("%s[%d]: setting jumpStartAddress to 0x%lx, insn %s, TOC 0x%lx\n", FILE__, __LINE__,
321                          toc_visitor->result, patternIter->second->format().c_str(), toc_visitor->toc_contents);
322           jumpStartAddress = toc_visitor->result;
323           foundAddress = true;
324           tableStartAddress = jumpStartAddress;
325           adjustEntry = 0;
326           break;
327         }
328     }
329   if(patternIter == currentBlock->allInsns.begin())
330     {
331       if (foundAddress) {
332         return true;
333       } else {
334
335         // If we've already backed up to the beginning, we're not going to find a legit table
336         // start address; bail now.
337         parsing_printf("%s[%d]: jumpStartAddress insn was first in block w/relative table, ret false\n",
338                        FILE__, __LINE__);
339         return false;
340       } 
341     }
342   // Anyone know what this does?
343   patternIter--;
344   if((patternIter->second->getOperation().getID() == power_op_lwz ||
345       patternIter->second->getOperation().getID() == power_op_ld))
346     {
347       toc_visitor->clear();
348       patternIter->second->getOperand(1).getValue()->apply(toc_visitor.get());
349       adjustEntry = toc_visitor->result;
350       foundAdjustEntry = true;
351       parsing_printf("%s[%d]: setting adjustEntry to 0x%lx, insn %s, TOC 0x%lx\n", FILE__, __LINE__,
352                      toc_visitor->result, patternIter->second->format().c_str(), toc_visitor->toc_contents);
353     }
354
355   while(patternIter != currentBlock->allInsns.begin()){
356     patternIter--;
357     if((patternIter->second->getOperation().getID() == power_op_lwz ||
358         patternIter->second->getOperation().getID() == power_op_ld) &&
359        patternIter->second->isRead(toc_reg))
360       {
361         toc_visitor->clear();
362         patternIter->second->getOperand(1).getValue()->apply(toc_visitor.get());
363         tableStartAddress = toc_visitor->result;
364         parsing_printf("%s[%d]: setting tableStartAddress to 0x%lx, insn %s, TOC 0x%lx\n", FILE__, __LINE__,
365                        toc_visitor->result, patternIter->second->format().c_str(), toc_visitor->toc_contents);
366         break;
367       }
368   }
369   return true;
370 }
371
372 namespace detail_ppc
373 {
374     bool isNonCallEdge(ParseAPI::Edge* e)
375     {
376         return e->type() != CALL;
377     }
378     bool leadsToVisitedBlock(ParseAPI::Edge* e, const std::set<Block*>& visited)
379     {
380         Block* src = e->src();
381         return visited.find(src) != visited.end();
382     }
383   void processPredecessor(Edge* e, std::set<Block*>& visited, std::deque<Block*>& worklist)
384   {
385     parsing_printf("\t\tblock %x, edge type %s\n",
386                    e->src()->start(),
387                    format(e->type()).c_str());
388     
389     // FIXME debugging assert
390     assert(isNonCallEdge(e));
391     
392     // FIXME check this algorithm... O(log n) lookup in visited
393     if(!leadsToVisitedBlock(e, visited))
394     {
395       worklist.push_back(e->src());
396       visited.insert(e->src());
397     }  
398   }
399 };
400
401 bool IA_powerDetails::scanForAdjustOrBase(IA_IAPI::allInsns_t::const_iterator start,
402                                           IA_IAPI::allInsns_t::const_iterator end,
403                                           RegisterAST::Ptr &jumpAddrReg) {
404   std::set<RegisterAST::Ptr> scratchRegs;
405   std::set<RegisterAST::Ptr> loadRegs;
406   loadRegs.insert(jumpAddrReg);
407   for (; start != end; --start) {
408     InstructionAPI::Instruction::Ptr insn = start->second;
409     parsing_printf("\t\t Examining 0x%lx / %s\n",
410                    start->first, start->second->format().c_str());
411
412     if ((insn->getOperation().getID() == power_op_ld ||
413          insn->getOperation().getID() == power_op_ldx) &&
414         insn->isWritten(jumpAddrReg)) {
415       scratchRegs.clear();
416       insn->getReadSet(scratchRegs);
417       loadRegs.insert(scratchRegs.begin(), scratchRegs.end());
418       parsing_printf("Found a load; now have %d load regs\n", loadRegs.size());
419     }
420     else if(insn->getOperation().getID() == power_op_addi &&
421             !foundAdjustEntry) {
422       parsing_printf("Found add immediate (%d load regs)...\n", loadRegs.size());
423       scratchRegs.clear();
424       insn->getWriteSet(scratchRegs);
425       
426       bool found = false;
427       // This is apparently broken
428       for (std::set<RegisterAST::Ptr>::iterator iter = loadRegs.begin(); iter != loadRegs.end(); ++iter) {
429         RegisterAST *tmp = (*iter).get();
430         RegisterAST *cmp = (*(scratchRegs.begin())).get();
431         if (*tmp == *cmp) {
432           found = true;
433           break;
434         }
435       }
436       if (!found) continue;
437
438       parsing_printf("... that adds to a load reg\n");
439       foundAdjustEntry = true;
440       toc_visitor->clear();
441       parsing_printf("... with operand %s\n", insn->getOperand(1).format(insn->getArch(), start->first).c_str());
442       insn->getOperand(1).getValue()->apply(toc_visitor.get());
443       adjustEntry = toc_visitor->result;
444       if (!adjustEntry)
445       insn->getOperand(2).getValue()->apply(toc_visitor.get());
446       adjustEntry = toc_visitor->result;
447     }
448     else if((insn->getOperation().getID() == power_op_lwz ||
449              insn->getOperation().getID() == power_op_ld) &&
450             insn->isRead(toc_reg) &&
451             insn->isWritten(jumpAddrReg))
452       {
453         parsing_printf("\t found TOC load at %s\n", insn->format().c_str());
454         toc_visitor->clear();
455         insn->getOperand(1).getValue()->apply(toc_visitor.get());
456         tableStartAddress = toc_visitor->result;
457         break;
458       }
459   }
460   return true;
461 }
462
463 // Like the above, but a wider net
464 bool IA_powerDetails::findTableBase(IA_IAPI::allInsns_t::const_iterator start,
465                                     IA_IAPI::allInsns_t::const_iterator end) {
466   for (; start != end; --start) {
467     parsing_printf("\t\t Examining 0x%lx / %s\n",
468                    start->first, start->second->format().c_str());
469     if((start->second->getOperation().getID() == power_op_lwz ||
470         start->second->getOperation().getID() == power_op_ld) &&
471        start->second->isRead(toc_reg)) {
472       parsing_printf("\t found TOC load at %s\n", start->second->format().c_str());
473       toc_visitor->clear();
474       start->second->getOperand(1).getValue()->apply(toc_visitor.get());
475       tableStartAddress = toc_visitor->result;
476       break;
477     }
478   }
479   return true;
480 }
481
482
483
484 // This should only be called on a known indirect branch...
485 bool IA_powerDetails::parseJumpTable(Block* currBlk,
486                                      std::vector<std::pair< Address, EdgeTypeEnum> >& outEdges)
487 {
488
489   Address initialAddress = currentBlock->current;
490   toc_reg.reset(new RegisterAST(ppc32::r2));
491
492   TOC_address = currentBlock->_obj->cs()->getTOC();
493   toc_visitor.reset(new detail::TOCandOffsetExtractor(TOC_address));
494   toc_visitor->toc_reg = toc_reg;
495     
496   // If there are no prior instructions then we can't be looking at a
497   // jump through a jump table.
498   if(currentBlock->allInsns.size() < 2) {
499     parsing_printf("%s[%d]: allInsns.size() == %d, ret false", FILE__, __LINE__, currentBlock->allInsns.size());
500     return false;
501   }
502
503
504   // Check if the previous instruction is a move to CTR or LR;
505   // if it is, then this is the pattern we're familiar with.  The
506   // register being moved into CTR or LR has the address to jump to.
507   patternIter = currentBlock->curInsnIter;
508   patternIter--;
509   RegisterAST::Ptr jumpAddrReg;
510   static RegisterAST::Ptr linkReg(new RegisterAST(ppc32::lr));
511   static RegisterAST::Ptr countReg(new RegisterAST(ppc32::ctr));
512   std::set<RegisterAST::Ptr> regs;
513   if(patternIter->second->getOperation().getID() == power_op_mtspr &&
514      (patternIter->second->isWritten(linkReg) ||
515       patternIter->second->isWritten(countReg)))
516     {
517       regs.clear();
518       patternIter->second->getReadSet(regs);
519       if(regs.size() != 1) {
520         parsing_printf("expected mtspr to read 1 register, insn is %s\n", patternIter->second->format().c_str());
521         return false;
522       }
523       jumpAddrReg = *(regs.begin());
524       parsing_printf("%s[%d]: JUMPREG %s mtspr at prev insn %s \n", FILE__, __LINE__, jumpAddrReg->format().c_str(), patternIter->second->format().c_str());
525       dfgregs.insert(jumpAddrReg->getID());
526     }
527   else
528     {
529       parsing_printf("%s[%d]: couldn't find mtspr at prev insn %s, ret false", FILE__, __LINE__,
530                      patternIter->second->format().c_str());
531       return false;
532     }
533   assert(jumpAddrReg);
534   // In the pattern we've seen, if the instruction previous to this is
535   // an add with a result that ends up being used as the jump address,
536   // then we're adding a relative value we got from the table to a base
537   // address to get the jump address; in other words, the contents of
538   // the jump table are relative.
539   tableIsRelative = false;
540   if(patternIter != currentBlock->allInsns.begin())
541     {
542       patternIter--;
543       if(patternIter->second->getOperation().getID() == power_op_add &&
544          patternIter->second->isWritten(*(regs.begin())))
545         {
546           tableIsRelative = true;
547         }
548     }
549   parsing_printf(" TableIsRelative %d\n", tableIsRelative);
550
551   patternIter = currentBlock->curInsnIter;
552     
553   jumpStartAddress = 0;
554   adjustEntry = 0;
555   tableStartAddress = 0;
556   adjustTableStartAddress = 0;
557   foundAdjustEntry = false;
558     
559   parsing_printf("\t TOC_address 0x%lx\n", TOC_address);
560   if(!TOC_address)
561     {
562       // Find addi-addis instructions to determine the jump table start address.
563       // These instructions can be anywhere in the function before the 
564       // indirect jump.Hence parse through the current block and previous block
565       // till we reach the function entry.
566       Block* worklistBlock = currBlk;
567       std::set <Block*> visited;
568       std::deque<Block*> worklist;
569       worklist.insert(worklist.begin(), worklistBlock);
570       visited.insert(worklistBlock);
571       Intraproc epred;
572       parsing_printf("Looking for table start address over blocks to function entry\n");
573       while(!worklist.empty())
574         {
575           worklistBlock= worklist.front();
576           worklist.pop_front();
577           parsing_printf("\tAddress low 0x%lx high 0x%lx current block 0x%lx low 0x%lx high 0x%lx \n", worklistBlock->low(), worklistBlock->high(), currentBlock->current, currBlk->low(), currBlk->high());
578           Address blockStart = worklistBlock->start();
579           const unsigned char* b = (const unsigned char*)(currentBlock->_isrc->getPtrToInstruction(blockStart));
580           parsing_printf(" Block start 0x%lx \n", blockStart);
581           InstructionDecoder dec(b, worklistBlock->size(), currentBlock->_isrc->getArch());
582           IA_IAPI IABlock(dec, blockStart, currentBlock->_obj, currentBlock->_cr, currentBlock->_isrc, worklistBlock);
583
584           while(IABlock.getInstruction() && !IABlock.hasCFT()) {
585             IABlock.advance();
586           }
587
588           patternIter = IABlock.curInsnIter;
589           findTableAddrNoTOC(&IABlock);
590           if(!jumpStartAddress)
591             {
592               jumpStartAddress = tableStartAddress;
593             }
594           if (tableStartAddress != 0) {
595             jumpStartAddress = tableStartAddress;
596             parsing_printf("\t\tjumpStartAddress 0x%lx \n", jumpStartAddress);
597             break;
598           }
599           std::for_each(boost::make_filter_iterator(epred, worklistBlock->sources().begin(), worklistBlock->sources().end()),
600                         boost::make_filter_iterator(epred, worklistBlock->sources().end(), worklistBlock->sources().end()),
601                         boost::bind(detail_ppc::processPredecessor, _1, boost::ref(visited), boost::ref(worklist)));
602
603         }
604
605     }
606   else if (tableIsRelative) {
607     if(!parseRelativeTableIdiom())
608       {
609         return false;
610       }
611   } else {
612     parsing_printf("\t Table is not relative and we know the TOC is 0x%lx, searching for table base\n",
613                    TOC_address);
614     foundAdjustEntry = false;
615     bool done = false;
616   
617     scanForAdjustOrBase(patternIter, currentBlock->allInsns.begin(), jumpAddrReg);
618
619     if (!tableStartAddress) {
620       // Keep looking in the immediate predecessor - XLC
621       for (Block::edgelist::const_iterator e_iter = currBlk->sources().begin(); 
622            e_iter != currBlk->sources().end(); ++e_iter) {
623         Address blockStart = (*e_iter)->src()->start();
624         const unsigned char* b = (const unsigned char*)(currentBlock->_isrc->getPtrToInstruction(blockStart));
625         InstructionDecoder dec(b, (*e_iter)->src()->size(), currentBlock->_isrc->getArch());
626         IA_IAPI IABlock(dec, blockStart, currentBlock->_obj, currentBlock->_cr, currentBlock->_isrc, (*e_iter)->src());
627         
628         // Cache instructions
629         while(IABlock.getInstruction() && !IABlock.hasCFT()) {
630           IABlock.advance();
631         }
632
633         IA_IAPI::allInsns_t::const_iterator localIter = IABlock.curInsnIter;
634         findTableBase(localIter, IABlock.allInsns.begin());
635       }
636     }     
637   }
638     
639   const Block::edgelist & sourceEdges = currBlk->sources();
640   if(sourceEdges.size() != 1 || (*sourceEdges.begin())->type() == CALL) {
641     parsing_printf("%s[%d]: jump table not properly guarded, ret false\n", FILE__, __LINE__);
642     return false;
643   }
644
645
646     
647   // We could also set this = jumpStartAddress...
648   if (tableStartAddress == 0)  {
649     parsing_printf("%s[%d]: couldn't find table start addr, ret false\n", FILE__, __LINE__);
650     return false;
651         
652   }
653     
654   parsing_printf("%s[%d]: table start addr is 0x%x\n", FILE__, __LINE__, tableStartAddress);
655   int maxSwitch = 0;
656   
657   Block* sourceBlock = (*sourceEdges.begin())->src();
658   Address blockStart = sourceBlock->start();
659   const unsigned char* b = (const unsigned char*)(currentBlock->_isrc->getPtrToInstruction(blockStart));
660   InstructionDecoder dec(b, sourceBlock->size(), currentBlock->_isrc->getArch());
661   IA_IAPI prevBlock(dec, blockStart,currentBlock->_obj,currentBlock->_cr,currentBlock->_isrc, sourceBlock);
662   while(!prevBlock.hasCFT() && prevBlock.getInstruction()) {
663     prevBlock.advance();
664   }
665
666   parsing_printf("%s[%d]: checking for max switch...\n", FILE__, __LINE__);
667   bool foundBranch = false;
668   IA_IAPI::allInsns_t::reverse_iterator iter;
669   for(iter = prevBlock.allInsns.rbegin(); iter != prevBlock.allInsns.rend(); iter++)
670
671     {
672       parsing_printf("\t\tchecking insn 0x%x: %s for cond branch + compare\n", iter->first,
673                      iter->second->format().c_str());
674       if(iter->second->getOperation().getID() == power_op_bc) // make this a true cond. branch check
675         {
676           foundBranch = true;
677         } else if(foundBranch && 
678                   (iter->second->getOperation().getID() == power_op_cmpi ||
679                    iter->second->getOperation().getID() == power_op_cmpli))
680         {
681           maxSwitch = iter->second->getOperand(2).getValue()->eval().convert<int>() + 1;
682           break;
683                 
684         }
685     } 
686
687   parsing_printf("%s[%d]: After checking: max switch %d\n", FILE__, __LINE__, maxSwitch);
688   if(!maxSwitch){
689     return false;
690   }
691
692   Address jumpStart = 0;
693   Address tableStart = 0;
694   bool is64 = (currentBlock->_isrc->getAddressWidth() == 8);
695   std::vector<std::pair< Address, EdgeTypeEnum> > edges;
696
697   if(TOC_address)
698     {
699       if (tableIsRelative) {
700         void *jumpStartPtr = currentBlock->_isrc->getPtrToData(jumpStartAddress);
701         parsing_printf("%s[%d]: jumpStartPtr (0x%lx) = %p\n", FILE__, __LINE__, jumpStartAddress, jumpStartPtr);
702         if (jumpStartPtr)
703           jumpStart = (is64
704                        ? *((Address  *)jumpStartPtr)
705                        : *((uint32_t *)jumpStartPtr));
706         parsing_printf("%s[%d]: jumpStart 0x%lx, initialAddr 0x%lx\n",
707                        FILE__, __LINE__, jumpStart, initialAddress);
708         if (jumpStartPtr == NULL) {
709           return false;
710         }
711       }
712       void *tableStartPtr = currentBlock->_isrc->getPtrToData(tableStartAddress);
713       parsing_printf("%s[%d]: tableStartPtr (0x%lx) = %p\n", FILE__, __LINE__, tableStartAddress, tableStartPtr);
714       tableStart = *((Address *)tableStartPtr);
715       if (tableStartPtr)
716         tableStart = (is64
717                       ? *((Address  *)tableStartPtr)
718                       : *((uint32_t *)tableStartPtr));
719       else {
720         return false;
721       }
722       parsing_printf("\t... tableStart 0x%lx\n", tableStart);
723
724       bool tableData = false;
725       for(int i=0;i<maxSwitch;i++){
726         Address tableEntry = adjustEntry + tableStart + (i * 4 /*instruction::size()*/);
727         parsing_printf("\t\tTable entry at 0x%lx\n", tableEntry);
728         if (currentBlock->_isrc->isValidAddress(tableEntry)) {
729           int jumpOffset;
730           if (tableData) {
731             jumpOffset = *((int *)currentBlock->_isrc->getPtrToData(tableEntry));
732           }
733           else {
734             jumpOffset = *((int *)currentBlock->_isrc->getPtrToInstruction(tableEntry));
735           }
736
737           parsing_printf("\t\t\tjumpOffset 0x%lx\n", jumpOffset);
738           Address res = (Address)(jumpStart + jumpOffset);
739
740           if (currentBlock->_isrc->isCode(res)) {
741             edges.push_back(std::make_pair((Address)(jumpStart+jumpOffset), INDIRECT));
742             parsing_printf("\t\t\tEntry of 0x%lx\n", (Address)(jumpStart + jumpOffset));
743           }
744         }
745         else {
746           parsing_printf("\t\tAddress not valid!\n");
747         }
748       }
749     }
750   // No TOC, so we're on Power32 Linux.  Do the ELF thing.
751   else
752     {
753       jumpStart = jumpStartAddress;
754       tableStart = tableStartAddress;
755       parsing_printf(" jumpStartaddress 0x%lx tableStartAddress 0x%lx \n", jumpStartAddress, tableStartAddress);
756       if(toc_visitor->toc_contents)
757         {
758           void* tmp = NULL;
759           if(currentBlock->_isrc->isValidAddress(jumpStartAddress))
760             {
761               tmp = currentBlock->_isrc->getPtrToData(jumpStartAddress);
762               if(!tmp)
763                 {
764                   tmp = currentBlock->_isrc->getPtrToInstruction(jumpStartAddress);
765                 }
766               if(tmp)
767                 {
768                   jumpStart = *((Address*)tmp);
769                   parsing_printf("\t\tjumpStart adjusted to 0x%lx\n", jumpStart);
770                 }
771             }
772           if(currentBlock->_isrc->isValidAddress(tableStartAddress))
773             {
774               tmp = currentBlock->_isrc->getPtrToData(tableStartAddress);
775               if(!tmp)
776                 {
777                   tmp = currentBlock->_isrc->getPtrToInstruction(tableStartAddress);
778                 }
779               if(tmp)
780                 {
781                   tableStart = *((Address*)tmp);
782                   parsing_printf("\t\ttableStart adjusted to 0x%lx\n", jumpStart);
783                 }
784             }
785         }
786       if (jumpStart == 0) {
787         // If jump table address is a relocation entry, this will be filled by the loader
788         // This case is common in shared library where the table address is in the GOT section which is filled by the loader
789         // Find the relocation entry for this address and look up its value
790
791         Block* sourceBlock = (*sourceEdges.begin())->src();
792         Address blockStart = sourceBlock->start();
793         const unsigned char* b = (const unsigned char*)(currentBlock->_isrc->getPtrToInstruction(blockStart));
794         InstructionDecoder dec(b, sourceBlock->size(), currentBlock->_isrc->getArch());
795         IA_IAPI IABlock(dec, blockStart,currentBlock->_obj,currentBlock->_cr,currentBlock->_isrc, sourceBlock);
796
797         SymtabCodeSource *scs = dynamic_cast<SymtabCodeSource *>(IABlock._obj->cs());
798         SymtabAPI::Symtab * symtab = scs->getSymtabObject();
799         std::vector<SymtabAPI::Region *> regions;
800         symtab->getAllRegions(regions);
801         for (unsigned index = 0 ; index < regions.size(); index++) {
802           if (regions[index]->getRegionType() == SymtabAPI::Region::RT_RELA || 
803               regions[index]->getRegionType() == SymtabAPI::Region::RT_REL) {
804             std::vector<SymtabAPI::relocationEntry> relocs =
805               regions[index]->getRelocations(); 
806             parsing_printf(" \t\trelocation size %d looking for 0x%lx\n", relocs.size(), jumpStartAddress);
807             for (unsigned i = 0; i < relocs.size(); ++i) {
808               parsing_printf(" \t 0x%lx => 0x%lx addend 0x%lx \n", relocs[i].rel_addr(),relocs[i].target_addr(), relocs[i].addend());
809               if (relocs[i].rel_addr() == jumpStartAddress) {
810                 jumpStart = relocs[i].addend();
811                 break;
812               }
813             }
814             break;
815           }
816         }
817         
818         
819       }
820       if (tableStart == 0) tableStart = jumpStart;
821
822       if (!tableIsRelative) {
823         jumpStart = 0;
824       }
825       parsing_printf(" jumpStartaddress 0x%lx tableStartAddress 0x%lx \n", jumpStart, tableStart);
826
827       int entriesAdded = 0;
828       for(int i = 0; i < maxSwitch; i++)
829         {
830           void* ptr = NULL;
831           Address tableEntry = tableStart + i*4; // instruction::size();
832           if(currentBlock->_isrc->isValidAddress(tableEntry))
833             {
834               ptr = currentBlock->_isrc->getPtrToInstruction(tableEntry);
835             }
836           if(ptr)
837             {
838               int jumpOffset = *((int *)ptr);
839               edges.push_back(std::make_pair((Address)(jumpStart+jumpOffset), INDIRECT));
840               parsing_printf("\t\t\t[0x%lx] -> 0x%lx (0x%lx in table)\n", tableEntry,
841                              jumpStart+jumpOffset,
842                              jumpOffset);
843               ++entriesAdded;
844             }
845           else
846             {
847               parsing_printf("\t\t\t[0x%lx] -> [INVALID]\n", tableEntry);
848             }
849         }
850       if(!entriesAdded)
851         {
852           parsing_printf("%s[%d]: no entries added from jump table, returning false\n", FILE__, __LINE__);
853           return false;
854         }
855       parsing_printf("%s[%d]: Found %d entries in jump table, returning success\n", FILE__, __LINE__, entriesAdded);
856     }
857
858   // Sanity check entries in res
859   for (std::vector<std::pair<Address, EdgeTypeEnum> >::iterator iter = edges.begin();
860        iter != edges.end(); iter++) {
861     if ((iter->first) % 4) {
862       parsing_printf("Warning: found unaligned jump table destination 0x%lx for jump at 0x%lx, disregarding table\n",
863                      iter->first, initialAddress);
864       return false;
865     }
866   }
867   // If we have found a jump table, add the targets to outEdges   
868   for (std::vector<std::pair<Address, EdgeTypeEnum> >::iterator iter = edges.begin();
869        iter != edges.end(); iter++) {
870     parsing_printf("Adding out edge %d/0x%lx\n", iter->second, iter->first);
871     outEdges.push_back(*iter);
872   }
873   return true;
874 }