flat assembler
Message board for the users of flat assembler.

Index > Windows > File position

Author
Thread Post new topic Reply to topic
score_under



Joined: 27 Aug 2009
Posts: 27
score_under 27 Aug 2009, 20:33
Simple question, probably not a simple answer Very Happy
How do I get the position of a label in the resultant PE file of the generated piece of code?

In this case:
Code:
Characteristics      dd 0x0
TimeDateStamp        dd %t
MajorVersion         dw 0x0
MinorVersion         dw 0x0
DebugType            dd 0x1
DataSize             dd EndDbg-CoffHeader
DataRVA              dd rva CoffHeader
DataSeek             dd (File position ??)    

I am experimenting with trying to implement debug information (the kind that Olly allows Wink ) in the EXE file. Of course the next step is to edit the part of the PE header that points to the debug information table, but there's really nothing you can do on FASM to edit the PE header directly. (Or if there is, it needs to be much better documented)

(If there is not, one of the first things I would like to see is a custom section alignment in the file and in virtual memory)

Also edit- http://www.jorgon.freeserve.co.uk/TestbugHelp/Optimisation.htm#hint How about implementing that too? Smile
Post 27 Aug 2009, 20:33
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 27 Aug 2009, 23:38
See my thread on building the PE format & header completely manually with FASM macros here.

However before you go there let me emphasize what you're looking for (because the manual PE macro code is kinda hard to grasp at first). You're looking for a way to obtain the "current file position", am I right?

It's not that hard so let me explain. First, you have to OVERRIDE the "org" directive, because you'll need to update an internal (read: probably local, for convenience) variable.

Code:
; Override 'org' directive to find out the file offset
fileoffset=0 ; this does not store the current offset, rather it stores the offset where the '$$' is in the FILE
macro org value*
{
 fileoffset=fileoffset+$-$$
 org value  ; use the normal 'org' functionality here
}    
Then when you want to find the current offset, use "fileoffset-$$+$", or in case of a label, use "fileoffset-$$+label".

Important!
Keep in mind that if you want to use FASM's multi-pass method of obtaining file offsets, then you will have to declare label offsets IN THE FILE, not use "fileoffset-$$+label" directly, because it would use the CURRENT (wrong) fileoffset -- in the meantime, this variable could get updated!

So for example if you have this:
Code:
dd fileoffset-$$+label

label:    
It is WRONG. You should use this instead:
Code:
dd label_fileoffset

label:
label_fileoffset = fileoffset-$$+label  ; this uses the proper 'fileoffset' found HERE, not at the beginning of the file!    


Personally I recommend you make fileoffset "local" to not conflict with possible variables with that name. To do that, enclose the macro in another macro or "rept 1" instruction (in case of macro, you purge it afterwards) just to declare it local like this:

Code:
rept 1
{
  local fileoffset
  macro somemacro
  \{
    ; blah
  \}
}    
or this:
Code:
macro init
{
  local fileoffset
  macro somemacro
  \{
    ; blah
  \}
}
init
purge init    
Post 27 Aug 2009, 23:38
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 30 Aug 2009, 14:25
By the way, if you use "org" inside virtual you'll also have to override the virtual directive! (my manual PE is now updated to include that). Example:

Code:
; isvirtual is set to + if we're in a virtual directive
$isvirtual equ

macro virtual [arg]
{ common
  virtual arg

  $isvirtual equ +
}
macro end [arg*]
{ common
  end arg

  match =virtual, arg \{ restore $isvirtual \}
}

; Override 'org' directive to find out the file offset
org 0
fileoffset=0 ; this does not store the current offset, rather it stores the offset where the '$$' is in the FILE
macro org value*
{
  match , $isvirtual \{ fileoffset=fileoffset+$-$$ \}
  org value
}    
Post 30 Aug 2009, 14:25
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.