Advertisement
fastman92

[IDC] Fix $gp based references in MIPS executable.

Mar 8th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <idc.idc>
  2. /*
  3.     Project name: MIPS $gp reference fixer 1.1
  4.  
  5.     Author: fastman92
  6.    
  7.     Description: this script fixes $gp based offsets in MIPS executable.
  8.     Value of $gp is assumed to be constant within executable code. 
  9. */
  10.  
  11. static main()
  12. {
  13.     Message("Start of MIPS $gp based reference fixer by fastman92\n");
  14.  
  15.     auto seg, loc;
  16.     auto GP_registerValue = 0x66B1F0;   // Our $gp value.
  17.  
  18.     Message("========================================\n");
  19.    
  20.     seg = FirstSeg();   // Get address pointed by a first segment
  21.    
  22.    
  23.      
  24.     while(seg != BADADDR )
  25.     {  
  26.         Message("----------------------------------------\n");
  27.  
  28.         loc = SegStart(seg);       
  29.        
  30.         Message("Fixing $gp offsets in segment %s\n", SegName(seg));
  31.                
  32.  
  33.         while(loc != BADADDR && loc < SegEnd(seg))
  34.         {                          
  35.             if(isCode(GetFlags(loc)))
  36.             {
  37.                 auto mnem = GetMnem(loc);
  38.  
  39.                 auto stillTry = 1;
  40.                
  41.                 if(mnem == "addiu")
  42.                 {
  43.                     if(GetOpType(loc, 1) == o_reg && GetOpnd(loc, 1) == "$gp" && GetOpType(loc, 2) == o_imm)
  44.                     {      
  45.                         OpOffEx(loc, 2, REFINFO_NOBASE, -1, GP_registerValue, 0);
  46.                        
  47.                         Message("Instruction 0x%X Operand %d\n", loc, 2);
  48.                         stillTry = 0;
  49.                     }
  50.                 }
  51.                
  52.                 if(stillTry)
  53.                 {              
  54.                     auto OpIt;
  55.                    
  56.                     for(OpIt = 0; OpIt <= 1; OpIt++)
  57.                     {
  58.                         auto OpType = GetOpType(loc, OpIt);
  59.                                                
  60.                         if(OpType != o_displ)
  61.                                 continue;
  62.  
  63.                         auto Operand = GetOpnd(loc, OpIt);
  64.                        
  65.                         auto Part = substr(Operand, strlen(Operand) - 5, -1);
  66.                        
  67.                         if(Part != "($gp)")
  68.                             continue;
  69.                            
  70.                         OpOffEx(loc, OpIt, REFINFO_NOBASE, -1, GP_registerValue, 0);
  71.                    
  72.                         Message("Instruction 0x%X Operand %d\n", loc, OpIt);
  73.                     }
  74.                 }
  75.             }          
  76.            
  77.             loc = NextHead(loc, BADADDR);
  78.         }
  79.      
  80.         seg = NextSeg(seg);     // get address of the next segment
  81.     }
  82.    
  83.     Message("End of MIPS $gp based reference fixer by fastman92\n");
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement