Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 07-09-2004, 11:47
Lunar_Dust
 
Posts: n/a
I have source code that I use, looking thru it should help you understand at least a bit, it shows the structures you talk about:

Code:
// Converts an RVA virtual address to a file offset.
DWORD RvaToOffset(LPVOID dwFileBase, DWORD Rva)
{

	IMAGE_DOS_HEADER *DosHeader;
	IMAGE_NT_HEADERS * MainPEHeader;
	LPVOID PEHeader = NULL;
	LPVOID pSection = NULL;
	IMAGE_SECTION_HEADER* Section=NULL;
	long NumberOfSections;

	DosHeader = (IMAGE_DOS_HEADER*)dwFileBase;
	PEHeader = (LPVOID)((DWORD)dwFileBase + (DWORD)DosHeader->e_lfanew);

	MainPEHeader = (IMAGE_NT_HEADERS*)PEHeader;
	NumberOfSections = MainPEHeader->FileHeader.NumberOfSections;

	pSection = (LPVOID)((DWORD)PEHeader + sizeof(IMAGE_NT_HEADERS));

	for (int Count=0;Count <= NumberOfSections;Count++)
	{
		
		Section = (IMAGE_SECTION_HEADER*)pSection;
		if (Rva >= Section->VirtualAddress)
		{
			if (Rva < (Section->VirtualAddress + Section->SizeOfRawData))
			{
				// it's in this section..
				long AddressDiff = Rva - Section->VirtualAddress;
				return (Section->PointerToRawData + AddressDiff);

			}
		}
	
	pSection = (LPVOID)((DWORD)pSection + sizeof(IMAGE_SECTION_HEADER));

	}

	return 0;

}




// Converts a file offset to a RVA address
DWORD OffsetToRva(LPVOID dwFileBase, DWORD Offset)
{

	IMAGE_DOS_HEADER *DosHeader;
	IMAGE_NT_HEADERS * MainPEHeader;
	LPVOID PEHeader = NULL;
	LPVOID pSection = NULL;
	IMAGE_SECTION_HEADER* Section=NULL;
	long NumberOfSections;

	DosHeader = (IMAGE_DOS_HEADER*)dwFileBase;
	PEHeader = (LPVOID)((DWORD)dwFileBase + (DWORD)DosHeader->e_lfanew);

	MainPEHeader = (IMAGE_NT_HEADERS*)PEHeader;
	NumberOfSections = MainPEHeader->FileHeader.NumberOfSections;

	pSection = (LPVOID)((DWORD)PEHeader + sizeof(IMAGE_NT_HEADERS));

	for (int Count=0;Count <= NumberOfSections;Count++)
	{
		
		Section = (IMAGE_SECTION_HEADER*)pSection;
		if (Offset >= Section->PointerToRawData)
		{
			if (Offset < (Section->PointerToRawData + Section->SizeOfRawData))
			{
				// it's in this section..
				long AddressDiff = Offset - Section->PointerToRawData;
				return (Section->VirtualAddress + AddressDiff);

			}
		}
	
	pSection = (LPVOID)((DWORD)pSection + sizeof(IMAGE_SECTION_HEADER));

	}

	return 0;

}



-Lunar
Reply With Quote
  #2  
Old 07-09-2004, 17:26
yaa
 
Posts: n/a
JMI, thx for the snippet ... Iczelion was indeed very clear in explaining RVAs ... and has even taken the time to explain why RVAs are used so much around. Lunar_Dust thank you for the code ... I had written myself such functions but I wasn't sure that I was doing things correctly or was taking for granted some things to always be the same (and they often are not) in PE files.


yaa
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to calculate RVA from file offset Shub-Nigurrath General Discussion 9 09-22-2009 12:33
Media Descriptor File (MDF/MDS) file format NimDa2k General Discussion 0 03-22-2009 16:49


All times are GMT +8. The time now is 01:05.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( Since 1998 )