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