flat assembler
Message board for the users of flat assembler.

Index > Windows > Org

Author
Thread Post new topic Reply to topic
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka
I've this code:
Code:
 ;  
 ;      manual PE  
 ;  
        image_base      equ 0x400000
        alignment       equ 0x4  
        stack_reserve   equ 0x1000  
        stack_commit    equ 0x1000  
        heap_reserve    equ 0x1000  
        heap_commit     equ 0x1000  
 ;  
 ;      dos header  
 ;      pe header  
 ;      optional header  
 ;      directory entries  
 ;      import header  
 ;      code directory  
 ;      import directory  
 ;

 format binary as "exe" 
        use32  
          
        dos_header:  
          
                dw 'MZ'                 ; DOS signature  
                db 0x3A dup(0)          ; unused  
                dd 0x40                 ; PE header address  
          
        pe_header:  
          
                db 'PE',0,0             ; PE signature  
                dw 0x014C               ; cpu (386)  
                dw 1                    ; number of sections  
                dd 0                    ; timestamp  
                dd 0                    ; symbol table address  
                dd 0                    ; number of symbols  
                dw sizeof.header        ; size of optional header  
                dw 0x010F               ; characteristics  
                  
        optional_header:  
                  
                dw 0x010B               ; magic  
                dw 0                    ; linker version  
                dd 0                    ; size of code section  
                dd 0                    ; size of initialised data  
                dd 0                    ; size of uninitialise data  
                dd code_directory       ; entry point address  
                dd 0                    ; base of code  
                dd 0                    ; base of data  
                dd image_base           ; base of image  
                dd alignment            ; section alignment  
                dd alignment            ; file alignment  
                dw 0                    ; os version major  
                dw 0                    ; os version minor  
                dw 0                    ; image version major  
                dw 0                    ; image version minor  
                dw 4                    ; subsystem version major  
                dw 0                    ; subsystem version minor  
                dd 0                    ; win32 version (reserved)  
                dd sizeof.image         ; image size  
                dd code_directory       ; header size  
                dd 0                    ; checksum  
                dw 0x0002               ; subsystem (GUI)  
                dw 0                    ; dll characteristics  
                dd stack_reserve        ; stack reserve size  
                dd stack_commit         ; stack commit size  
                dd heap_reserve         ; heap reserve size  
                dd heap_commit          ; heap commit size  
                dd 0                    ; loader flags (obsolete)  
                dd 16                   ; number of directory entries  
                  
        directory_entries:  
                  
                dq 0                    ; export  
                dd import_directory     ; import section rva  
                dd sizeof.import        ; import section size  
                dq 14 dup(0)            ; the rest  
                  
        import_header:  
                  
                dq '.import'            ; name  
                dd sizeof.import        ; virtual size  
                dd code_directory       ; rva  
                dd sizeof.import        ; raw size  
                dd code_directory       ; raw pointer to data  
                dd 0                    ; pointer to relocations  
                dd 0                    ; pointer to line numbers  
                dw 0                    ; number of relocations  
                dw 0                    ; number of line numbers  
                dd 0x0E0000020          ; characteristics  
                align alignment 
                  
        code_directory:  

                push 0  
                push title+image_base  
                push message+image_base  
                push 0  
                call[MessageBoxA+image_base]
                push 0
                call[ExitProcess+image_base]
                title: db 'Title',0 
                message: db 'Hello world',0
                  
        import_directory:  

                MessageBoxA dd 0x7E4507EA
                ExitProcess dd 0x7C81CAFA
        file_end:  
                  
                sizeof.import = file_end-import_directory  
                sizeof.header = import_header-optional_header  
                sizeof.image = file_end  
    


and I try to remove image_base from pointers. But when I do it and add org my program crashes:

Code:
        code_directory:  
                org image_base
                push 0  
                push title
                push message
                push 0  
                call[MessageBoxA]
                push 0
                call[ExitProcess]
                title: db 'Title',0 
                message: db 'Hello world',0     


How can I correctly use org in this example?
Post 18 Jul 2012, 12:22
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
Please post complete changed code.
Did you adapt your values for
* MessageBoxA
* ExitProcess
?

You could move org to the line directly in front of title:
but have to be aware that ALL following lines will be affected by org.
Post 19 Jul 2012, 15:41
View user's profile Send private message Send e-mail Reply with quote
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka
Here is a complete code:
Code:
 ;
 ;      manual PE   
 ;   
        image_base      equ 0x400000 
        alignment       equ 0x4   
        stack_reserve   equ 0x1000   
        stack_commit    equ 0x1000   
        heap_reserve    equ 0x1000   
        heap_commit     equ 0x1000   
 ;   
 ;      dos header   
 ;      pe header   
 ;      optional header   
 ;      directory entries   
 ;      import header   
 ;      code directory   
 ;      import directory   
 ; 

 format binary as "exe"  
        use32   
           
        dos_header:   
           
                dw 'MZ'                 ; DOS signature   
                db 0x3A dup(0)          ; unused   
                dd 0x40                 ; PE header address   
           
        pe_header:   
           
                db 'PE',0,0             ; PE signature   
                dw 0x014C               ; cpu (386)   
                dw 1                    ; number of sections   
                dd 0                    ; timestamp   
                dd 0                    ; symbol table address   
                dd 0                    ; number of symbols   
                dw sizeof.header        ; size of optional header   
                dw 0x010F               ; characteristics   
                   
        optional_header:   
                   
                dw 0x010B               ; magic   
                dw 0                    ; linker version   
                dd 0                    ; size of code section   
                dd 0                    ; size of initialised data   
                dd 0                    ; size of uninitialise data   
                dd code_directory       ; entry point address   
                dd 0                    ; base of code   
                dd 0                    ; base of data   
                dd image_base           ; base of image   
                dd alignment            ; section alignment   
                dd alignment            ; file alignment   
                dw 0                    ; os version major   
                dw 0                    ; os version minor   
                dw 0                    ; image version major   
                dw 0                    ; image version minor   
                dw 4                    ; subsystem version major   
                dw 0                    ; subsystem version minor   
                dd 0                    ; win32 version (reserved)   
                dd sizeof.image         ; image size   
                dd code_directory       ; header size   
                dd 0                    ; checksum   
                dw 0x0002               ; subsystem (GUI)   
                dw 0                    ; dll characteristics   
                dd stack_reserve        ; stack reserve size   
                dd stack_commit         ; stack commit size   
                dd heap_reserve         ; heap reserve size   
                dd heap_commit          ; heap commit size   
                dd 0                    ; loader flags (obsolete)   
                dd 16                   ; number of directory entries   
                   
        directory_entries:   
                   
                dq 0                    ; export   
                dd import_directory     ; import section rva   
                dd sizeof.import        ; import section size   
                dq 14 dup(0)            ; the rest   
                   
        import_header:   
                   
                dq '.import'            ; name   
                dd sizeof.import        ; virtual size   
                dd code_directory       ; rva   
                dd sizeof.import        ; raw size   
                dd code_directory       ; raw pointer to data   
                dd 0                    ; pointer to relocations   
                dd 0                    ; pointer to line numbers   
                dw 0                    ; number of relocations   
                dw 0                    ; number of line numbers   
                dd 0x0E0000020          ; characteristics   
                align alignment  
                   
         code_directory:
                org image_base 
                push 0   
                push title 
                push message 
                push 0   
                call[MessageBoxA] 
                push 0 
                call[ExitProcess] 
                title: db 'Title',0  
                message: db 'Hello world',0
                   
        import_directory:   

                MessageBoxA dd 0x7E4507EA 
                ExitProcess dd 0x7C81CAFA 
        file_end:   
                   
                sizeof.import = file_end-import_directory   
                sizeof.header = import_header-optional_header   
                sizeof.image = file_end    

And here is a problem because org changes all lines in code.
Post 20 Jul 2012, 11:54
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
file_end changes after org and sizeof.image is calculated wrong.

You could do correct it's calculation by:

sizeof.image = file_end-image_base+code_directory-doc_header

Anyway I am not sure why you want to use org in this context.
To use "...+image_base" is the better alternative I think.

Anyway it is calculated during compile not in runtime.
So there is no disadvantage about the add.
Post 20 Jul 2012, 15:15
View user's profile Send private message Send e-mail Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Kazyaka,

Import directory entry should point to somewhat different structure than simple list of some constants. You may consult PE/COFF specification for further directions. Another option is to resort to ready-made hand-written PEs that are abundant here and there.

P.S. Moderators, this thread has nothing to do with macros.
Post 26 Jul 2012, 20:26
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-2020, Tomasz Grysztar.

Powered by rwasa.