NASM Forum > Summer of Code Ideas

Direct output to PE32, PE64, DLL32 and DLL64 with Macro Sets

(1/5) > >>

nalsakas:
I named these macro sets as Nasm PE macros, and their include file is "pe.inc". I have already made too much afford to make these set of macros happen. These macros uses bin file format output. It is flat binary. Macros fills in necessary headers.

Sample usage:

PE32

SECTION DATA

SECTION CODE
START
 instructions
END

But in order to go one step forward I need a few features from nasm which aren't develop yet (as far as I know).

Request 1:

First is I need to test if a label is defined before or after the current test  location. I need a preprocessor directive,  something like %iflabel  /  %endif.

Example 1:

label1:
    instructions....

    %iflabel label2
        do something
    %endif

label2:
    instructions....

%$label3:
    instructions....


Request 2:

I need to test if a single line macro defined after current line? %ifdef doesn't work in these situations.

Example 2:

%ifdef %$sometihng
   .... This isn't working
%endif


%assign %$something otherthings


Request 3:
I could get the last value of a single line macro (a %define statement). In order for me to fill in section table of pe file I need to know in advance how many sections are defined in pe file. I need to get last declared value of section number counter, each section increments the value by one.

Example 3:

; PE Section Header
; How to get last value of %$SECTION_COUNT???
; It is not even defined yet


%assign %$SECTION_COUNT 0

%$SECTION_1
%assign %$SECTION_COUNT %$SECTION_COUNT + 1

%$SECTION_2
%assign %$SECTION_COUNT %$SECTION_COUNT + 1


 





   

nalsakas:
I have nearly completed PE macro sets. As I mentioned earlier there are a few limitations of nasm. Because of that I can't produce more than one section in PE header. But that is not a big problem for me. I still can produce one section PE file. Resulting executable will have only one section. And will be smaller.

Because of that explicitly defining a SECTION isn't needed. So I will remove SECTION macro from future versions.

IMPORT and EXPORT macros are included and are ready. I am still working on RESOURCE macros.

There is example source codes you can find below. This is how source code of asm will look if you use PE macros.

In order to produce output,  use -f bin assembler switch.

Since labels in asm source code are offset based I had to invent VA() macro in order to turn offset based labels into virtual addresses. It is usualy linkers job but since we don't use linker in this project we have to manually turn offsets into VA's.

Example PE32 file:

%include "pe.inc"

; For 32-bit executable use PE32
; For 32-bit dll use DLL32
; 64-bit PE and DLL's are not ready yet
PE32

; Data declarations
Title db 'Title of MessageBox',0

; enty point of executable
START
; machine intructions
  ...
  push VA(Title)
  push 0
  call [VA(MessageBoxA)]

  LocalFunction:
     ...
     ret


; Setup import directory if you need to
IMPORT
  ; write down imported dll's
  LIB user32.dll
    ; write down imported functions
    FUNC MessageBoxA
  ENDIMPORT
 
  LIB kernel32.dll
    FUNC ExitProcess
    ...
  ENDLIB
ENDIMPORT

; Setup Export Directory if you need to
EXPORT
   ; write down local functions to export
   FUNC LocalFunction
   ...
ENDEXPORT


; Setup Resource Directory if you need to
RESOURCE
  ; I am working on details
ENDRESOURCE

END
; End of executable

nalsakas:
Although my work on RESOURCE directory macros continues, I have published pe macros on github. Anyone wants to give it a try link is here.

https://github.com/nalsakas/pe.git

With these macro sets one can produce PE32, DLL32 files with IMPORT and EXPORT directory support. RESOURCE directory currently supports only raw resources, user defined resources and MENU resources. MENU support recently added.

Example use of menus:

MENU menuname
    MENUITEM 'File', 101h

    POPUP 'Edit'
        MENUITEM 'Copy' ,200h
        MENUITEM 'Paste', 201h
    ENDPOPUP

    MENUITEM 'Help', 300h
ENDMENU

nalsakas:
#VERSION 0.2, 11/06/2015
- README file updated
- Sample applications added.
- EXPORT macro now needs a module name.
- Bugs cleared from EXPORT macro.

There was a bug in EXPORT macro. It is corrected.

EXPORT macro now needs export module name which is mandatory by pe format. User has to type filename here. I  was try to get file name from __FILE__ macro but it returns path too. So I removed default implementation.

3 sample code examples added to the project. One is simple MessageBox, other is simple window with menu, and last one is a simple DLL exports a function.

My work on DIALOG resource continues...

Get latest work here:
https://github.com/nalsakas/pe.git

shaynox:
Wow, very interesting, I will use it certainly in future for a better of executable's system understanding and for more customizing x)

I'm a little noob in eng, so can you shortly describe what is VA/RVA ?

Navigation

[0] Message Index

[#] Next page

Go to full version