Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 12-24-2004, 01:06
Shub-Nigurrath's Avatar
Shub-Nigurrath Shub-Nigurrath is offline
VIP
 
Join Date: Mar 2004
Location: Obscure Kadath
Posts: 919
Rept. Given: 60
Rept. Rcvd 419 Times in 94 Posts
Thanks Given: 68
Thanks Rcvd at 330 Times in 100 Posts
Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499
how to calculate RVA from file offset

Hi,
anyone could post here any how to convert a file offset to its' memory equivalent RVA address .. practically what the RVA converter from Lazarus does...but the question is "how".

I'm coding it in C and already have a library/class which gives me all the PE header fields..but I'm not sure of the formula to use.

Any help is welcome!
__________________
Ŝħůb-Ňìĝùŕřaŧħ ₪)
There are only 10 types of people in the world: Those who understand binary, and those who don't
http://www.accessroot.com
Reply With Quote
  #2  
Old 12-24-2004, 02:06
Eggi
 
Posts: n/a
Code:
DWORD OffsetToRVA(DWORD offset, IMAGE_SECTION_HEADER *is_hdr, unsigned scount){
	// Find section holding the Offset
	for(unsigned i = 0; i < scount;i++)
		if((offset >= is_hdr[i].PointerToRawData) && (offset <= is_hdr[i].PointerToRawData +is_hdr[i].SizeOfRawData)){
			// Convert Offset to RVA
			return offset+is_hdr[i].VirtualAddress-is_hdr[i].PointerToRawData;
		}
		return 0;
}
should work...
is_hdr is a array of IMAGE_SECTION_HEADERS and scount is the number of sections (file header)

Last edited by Eggi; 12-24-2004 at 03:12.
Reply With Quote
  #3  
Old 12-24-2004, 04:39
Shub-Nigurrath's Avatar
Shub-Nigurrath Shub-Nigurrath is offline
VIP
 
Join Date: Mar 2004
Location: Obscure Kadath
Posts: 919
Rept. Given: 60
Rept. Rcvd 419 Times in 94 Posts
Thanks Given: 68
Thanks Rcvd at 330 Times in 100 Posts
Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499
10x mate, nice to receive answers here from you ;-)
__________________
Ŝħůb-Ňìĝùŕřaŧħ ₪)
There are only 10 types of people in the world: Those who understand binary, and those who don't
http://www.accessroot.com
Reply With Quote
  #4  
Old 12-24-2004, 04:43
diablo2oo2's Avatar
diablo2oo2 diablo2oo2 is offline
Family
 
Join Date: Mar 2004
Posts: 232
Rept. Given: 7
Rept. Rcvd 111 Times in 26 Posts
Thanks Given: 2
Thanks Rcvd at 20 Times in 7 Posts
diablo2oo2 Reputation: 100-199 diablo2oo2 Reputation: 100-199
for masm:
http://pe-lib.cjb.net

there is a newer (unofficial) version of this (0.3d see attachment).

code:

Code:
invoke plOpenFile,PL_NO_OPEN_DIALOG,chr$("C:\File.exe")
invoke plOffsetToRVA,00000200h
invoke plCloseFile
Attached Files
File Type: rar PeLibrary.rar (8.1 KB, 38 views)
__________________
Thinking In Bytes
Reply With Quote
  #5  
Old 12-24-2004, 18:14
Shub-Nigurrath's Avatar
Shub-Nigurrath Shub-Nigurrath is offline
VIP
 
Join Date: Mar 2004
Location: Obscure Kadath
Posts: 919
Rept. Given: 60
Rept. Rcvd 419 Times in 94 Posts
Thanks Given: 68
Thanks Rcvd at 330 Times in 100 Posts
Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499 Shub-Nigurrath Reputation: 400-499
well, what Eggi posted suits my need excellently, the only thing is that the result must be added to the imagebase but it's ok for the tests I did..
__________________
Ŝħůb-Ňìĝùŕřaŧħ ₪)
There are only 10 types of people in the world: Those who understand binary, and those who don't
http://www.accessroot.com
Reply With Quote
  #6  
Old 12-25-2004, 11:46
xixiaolou
 
Posts: n/a
As I know:
For language such as VC++ MFC, Delphi, VB, the compiler use RVA as event pointer, so File Offset = RVA
For language as asm and on, you must use code above to convert RVA to offset
Reply With Quote
  #7  
Old 01-07-2005, 20:25
LAVA
 
Posts: n/a
I've read this article from (iczelion).
It has a section about converting RVA to offset (asm code)
I think that it will do what you want(with a little change).

h--p://spiff.tripnet.se/~iczelion/pe-tut7.html
Reply With Quote
  #8  
Old 01-07-2005, 23:56
SiNTAX SiNTAX is offline
Friend
 
Join Date: Sep 2002
Posts: 22
Rept. Given: 2
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 0
Thanks Rcvd at 0 Times in 0 Posts
SiNTAX Reputation: 0
This might come in handy too, straight from the evil empire:

Microsoft Portable Executable and Common Object File Format Specification
-- hxxp://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx

It doesn't have example code, but it's nice to have around as a reference.
Reply With Quote
  #9  
Old 01-08-2005, 05:08
Claudio
 
Posts: n/a
There is another PE library.
pe library v1.2 - by death
http://www.polarhome.com:793/~execution/02/ex-pel12.zip

This is a c++ library for pe manipulation. also, the compiled samples are available below.
http://www.polarhome.com:793/~execution/02/pelsamples.zip
Reply With Quote
  #10  
Old 09-22-2009, 12:33
LaBBa LaBBa is offline
VIP
 
Join Date: Jul 2003
Posts: 150
Rept. Given: 0
Rept. Rcvd 16 Times in 4 Posts
Thanks Given: 0
Thanks Rcvd at 11 Times in 11 Posts
LaBBa Reputation: 16
i know this is a long time ago post and it was answered but still i found somthing good when i was looking for an answer to this question..

Understanding RVAs and Import Tables - by Sunshine
http://www.sunshine2k.de/Tuts/tut_rvait.htm

the only thing he didn't say is that the RVA is calculated first by
VA(the disassbler shown address) - ImageBase = RVA
and then it shows how to calc the offset from the RVA...

Regards,
LaBBa.
Reply With Quote
Reply


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 the exact size of a piece of code? zaratustra General Discussion 10 09-25-2004 13:28
How to obtain the file offset from an RVA??? yaa General Discussion 3 07-09-2004 17:26


All times are GMT +8. The time now is 12:20.


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