editor/ida/linux_vtable_dump.idc
author David Anderson <dvander@alliedmods.net>
Tue Nov 11 02:21:25 2008 -0800 (2008-11-11)
changeset 2395 5f0dcfc72e44
parent 1055 7f329c30caec
permissions -rw-r--r--
Take-two at fixing bug 3410.
     1 
     2 // *****************************************************************************
     3 // - IDA Pro script -
     4 // Name: ida_vtables.idc
     5 // Desc: Recreates the methods of a class from a vtable
     6 // 
     7 // Ver: 1.0b - July 20, 2006 - By Sirmabus   
     8 // Ver: 2.0 - July 7, 2006 by BAILOPAN
     9 // �������������������������������������������������������������������������-��-
    10 //
    11 // -----------------------------------------------------------------------------
    12 
    13 #include <idc.idc>
    14 
    15 static main()
    16 {
    17 	auto pAddress, iIndex;
    18 	auto szFilePath, hFile;
    19 	auto skipAmt;
    20 
    21 	SetStatus(IDA_STATUS_WORK);
    22 
    23 	// User selected vtable block
    24 	pAddress = ScreenEA();
    25 	
    26 	if (pAddress == BADADDR)
    27 	{	
    28 		Message("** No vtable selected! Aborted **");
    29 		Warning("No vtable selected!\nSelect vtable block first.");							
    30 		SetStatus(IDA_STATUS_READY);
    31 		return;
    32 	}
    33 
    34 	skipAmt = AskLong(1, "Number of vtable entries to ignore for indexing:");
    35 
    36 	// Request output header file
    37 	SetStatus(IDA_STATUS_WAITING);
    38 	if ((szFilePath = AskFile(1, "*.txt", "Select output dump file:")) == 0)
    39 	{		
    40 		Message("Aborted.");
    41 		SetStatus(IDA_STATUS_READY);
    42 		return;
    43 	}
    44 	
    45 	// And create it..
    46 	if ((hFile = fopen(szFilePath, "wb")) != 0)
    47 	{
    48 		auto szFuncName, szFullName, BadHits;
    49 		
    50 		BadHits = 0;
    51 
    52 		// Create the header
    53 		fprintf(hFile, "// Auto reconstructed from vtable block @ 0x%08X\n// from \"%s\", by ida_vtables.idc\n", pAddress, GetInputFile());
    54 		
    55 		/* For linux, skip the first entry */
    56 		if (Dword(pAddress) == 0)
    57 		{
    58 			pAddress = pAddress + 8;
    59 		}
    60 		
    61 		pAddress = pAddress + (skipAmt * 4);
    62 
    63 		// Loop through the vtable block
    64 		while (pAddress != BADADDR)
    65 		{
    66 			auto real_addr;
    67 			real_addr = Dword(pAddress);
    68 				
    69 			szFuncName = Name(real_addr);
    70 			if (strlen(szFuncName) == 0)
    71 			{
    72 				break;
    73 			}
    74 			szFullName = Demangle(szFuncName, INF_LONG_DN);
    75 			if (szFullName == "")
    76 			{
    77 				szFullName = szFuncName;
    78 			}
    79 			if (strstr(szFullName, "_ZN") != -1)
    80 			{
    81 				fclose(hFile);
    82 				Warning("You must toggle GCC v3.x demangled names!\n");
    83 				break;
    84 			}
    85 			fprintf(hFile, "%d\t%s\n", iIndex, szFullName);
    86 						
    87 			pAddress = pAddress + 4;
    88 			iIndex++;
    89 		};
    90 
    91 		fclose(hFile);
    92 		Message("Successfully wrote %d vtable entries.\n", iIndex);
    93 	}
    94 	else
    95 	{		
    96 		Message("** Error opening \"%s\"! Aborted **\n", szFilePath);
    97 		Warning("Error creating \"%s\"!\n", szFilePath);
    98 	}
    99 
   100 	Message("\nDone.\n\n");
   101 	SetStatus(IDA_STATUS_READY);
   102 }