flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] experiments with PE.INC

Author
Thread Post new topic Reply to topic
VEG



Joined: 06 Feb 2013
Posts: 80
VEG 22 May 2017, 12:08
I'm experimenting with pe.inc. I've tried to rename the PE address space and encountered a strange error, and I can't understand what's wrong.

How to reproduce (or you can get premodified pe.inc):
1. Open pe.inc
2. Replace PE:: to XXX::
3. Replace PE: to XXX: everywhere (but don't replace where it is not a whole word, like here: load TYPE:word).
4. Replace "define CheckSumBlocks XXX,0,HeadersEnd" to "define CheckSumBlocks PE,0,HeadersEnd".
5. Try to compile win32.asm example. Error:
Quote:
win32.asm [25]:
section '.idata' import data readable writeable
macro section [3] macro section [14]:
store SECTION_SIZE at XXX:SectionTable.VirtualSize+SECTION_INDEX*SectionTable.ENTRY_LENGTH
Processed: store SECTION_SIZE at XXX:SectionTable.VirtualSize+SECTION_INDEX*SectionTable.ENTRY_LENGTH
Error: address out of range.

Trying to debug it...
1. Replace:
Code:
                if DEFINED_SECTION | SECTION_SIZE > 0
                        SECTION_INDEX = SECTION_INDEX + 1
                end if    
to
Code:
                if DEFINED_SECTION | SECTION_SIZE > 0
                        SECTION_INDEX = SECTION_INDEX + 1
                        repeat 1, n:SECTION_INDEX
                                display 'section: ', `n, 0x0A
                        end repeat
                end if    

2. Replace:
Code:
                NUMBER_OF_SECTIONS := SECTION_INDEX    
to
Code:
                NUMBER_OF_SECTIONS := SECTION_INDEX
                repeat 1, n:SECTION_INDEX
                        display 'postpone: ', `n, 0x0A
                end repeat    
3. The result:
Code:
section: 1
section: 2
section: 3
section: 1
postpone: 1    

What's wrong? Why SECTION_INDEX resets to the 0 before the last section?


Description: Premodified pe.inc
Download
Filename: pe.inc
Filesize: 28.64 KB
Downloaded: 611 Time(s)



Last edited by VEG on 22 May 2017, 13:31; edited 2 times in total
Post 22 May 2017, 12:08
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8358
Location: Kraków, Poland
Tomasz Grysztar 22 May 2017, 13:30
VEG wrote:
What's wrong? Why SECTION_INDEX resets to the 0 before the last section?
These macros use "namespace PE" construction in many places and it is only safe when "PE" is defined as a global symbol, otherwise "namespace PE" is going to switch to the namespace of a local "PE".
The "PE::" label has an additional function there of defining a global "PE" symbol which serves a role of a base for global namespace (this still can be fooled if you define a local "PE" symbol somewhere and there are better ways to make macros that are foolproof against that, but that is another story). So to make them work correctly when you replace "PE::" with "XXX::" you need to ensure that "PE" is a global symbol in some other way:
Code:
XXX::
PE := 1    
Post 22 May 2017, 13:30
View user's profile Send private message Visit poster's website Reply with quote
VEG



Joined: 06 Feb 2013
Posts: 80
VEG 22 May 2017, 13:35
Oh, thank you for the explanation.

Actually, I was trying to move the PE address space into the PE namespace, and to rename it to avoid ambiguity (for a reader of the source, not for the fasmg) of the same name for two different things. IMHO, something like "PE.Header::" or even "PE.Section0::" would be more clear for a programmer who will read the pe.inc. The latter variant opens a door for the idea when all following sections will be accessible using names like "PE.Section1::", "PE.Section2::", etc. It even conforms with the PE documentation which says that sections start numbering from 1:
pecoff.docx wrote:
The number of entries in the section table is given by the NumberOfSections field in the file header. Entries in the section table are numbered starting from one (1).
But it's just some thoughts. It doesn't mean that it is a good idea Smile
Post 22 May 2017, 13:35
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8358
Location: Kraków, Poland
Tomasz Grysztar 22 May 2017, 13:45
This not a bad idea. The PE.INC was one the first sets of formatting macros I made for fasmg and at the time I wanted to write something simple and compact to include in the examples. But then the x86 examples became much more complex anyway. I think that I could in the future fork PE.INC into a basic version included in the examples and a more "advanced" version in the Windows headers package.
Post 22 May 2017, 13:45
View user's profile Send private message Visit poster's website 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.