flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > High Level Languages > Compile FASM to obj file

Author
Thread Post new topic Reply to topic
Teddy



Joined: 06 Jul 2013
Posts: 4
Compile FASM to obj file
Hello there,

I'm writing a managed (.NET) library to compile FASM code into a .NET application. After thinking how I can interact with the compiler within my app, I think I'm going to compile FASM as an .obj file (MS COFF), then I will write a managed wrapper in C"++/CLI and then, the compiler will be able to be used in a program written in C# or even VB.NET.

Unfortunately, I'm encountering some issues when I try to compile FASM in MS COFF format. From the example given in the downloadable archive, MS COFF uses a different way to call Win32 API like:


Code:
extrn '__imp__MessageBoxA@16' as MessageBox:dword



In the FASM.ASM file for WIN32, the import section is written in .data.

So, even if I change the format of the into format MS COFF, the compile operation fails.

Do you have some advices to compile FASM to an obj file ?

Thanks for reading my post.
Teddy.
Post 06 Jul 2013, 14:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14854
Location: Lost in translation
I'm not sure what you are asking so here is the fasm example for MS COFF format:

Code:

; example of making Win32 COFF object file

format MS COFF

extrn '__imp__MessageBoxA@16' as MessageBox:dword

section '.text' code readable executable

 public _demo

 _demo:
        push    0
        push    _caption
        push    _message
        push    0
        call    [MessageBox]
        ret

section '.data' data readable writeable

 _caption db 'Win32 assembly',0
 _message db 'Coffee time!',0

Post 06 Jul 2013, 21:17
View user's profile Send private message Visit poster's website Reply with quote
Teddy



Joined: 06 Jul 2013
Posts: 4
Hello Revolution.

Thanks for your reply.

I'm sorry if my first message was confused. To summarize, I want to compile "FASM compiler" to get an ".obj" file, so I can then use it in a library written in C++. I thought using MS COFF format (I think this is the best for that, but I'm not completely sure).

I'm encountering some issues when I try to compile FASM for Win32 with the MS COFF format, because it seems the import section is slightly different between the legacy PE format and the MS COFF format.

Do you have any advice to get an .obj format or something else I can use in my C++ library ?

Teddy.
Post 06 Jul 2013, 22:31
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2628
Location: dank orb
I started with FASM DLL, and then put:

Code:
format MS COFF

extrn '__imp__CreateFileA'              as CreateFile:dword
extrn '__imp__ReadFile'                 as ReadFile:dword
extrn '__imp__WriteFile'                as WriteFile:dword
extrn '__imp__CloseHandle'              as CloseHandle:dword
extrn '__imp__SetFilePointer'           as SetFilePointer:dword
extrn '__imp__GetCommandLineA'          as GetCommandLineA:dword
extrn '__imp__GetEnvironmentVariableA'  as GetEnvironmentVariable:dword
extrn '__imp__GetStdHandle'             as GetStdHandle:dword
extrn '__imp__VirtualAlloc'             as VirtualAlloc:dword
extrn '__imp__VirtualFree'              as VirtualFree:dword
extrn '__imp__GetTickCount'             as GetTickCount:dword
extrn '__imp__GetSystemTime'            as GetSystemTime:dword
extrn '__imp__GlobalMemoryStatus'       as GlobalMemoryStatus:dword

...at the start of FASM.ASM. Then I removed the '.idata' and '.edata' and '.reloc' sections. It compiled, but I haven't run any tests on it. Of course, it'd also be possible to write a wrapper for the DLL.

Oh, you'd want to specify the export functions as public.

Code:
public fasm_GetVersion
public fasm_Assemble
public fasm_AssembleFile

Post 06 Jul 2013, 23:08
View user's profile Send private message Visit poster's website Reply with quote
Teddy



Joined: 06 Jul 2013
Posts: 4
Hello bitRAKE.

Thanks for your message.

What a brilliant idea to start working on the FASM DLL project !
I managed to perform the same actions you did to compile the project.

After several tests, MS COFF format requires the mangled names of the exported functions. Here are the mangled names I used to get it perfectly working:


Code:
extrn '__imp__CreateFileA@28'                   as CreateFile:dword
extrn '__imp__ReadFile@20'                              as ReadFile:dword
extrn '__imp__WriteFile@20'                             as WriteFile:dword
extrn '__imp__CloseHandle@4'                    as CloseHandle:dword
extrn '__imp__SetFilePointer@16'                as SetFilePointer:dword
extrn '__imp__GetCommandLineA@0'                as GetCommandLineA:dword
extrn '__imp__GetEnvironmentVariableA@12'       as GetEnvironmentVariable:dword
extrn '__imp__GetStdHandle@4'                   as GetStdHandle:dword
extrn '__imp__VirtualAlloc@16'                  as VirtualAlloc:dword
extrn '__imp__VirtualFree@12'                   as VirtualFree:dword
extrn '__imp__GetTickCount@0'                   as GetTickCount:dword
extrn '__imp__GetSystemTime@4'                  as GetSystemTime:dword
extrn '__imp__GlobalMemoryStatus@4'             as GlobalMemoryStatus:dword



I analyzed the file kernel32.dll using IDA to get these names.
Post 07 Jul 2013, 01:00
View user's profile Send private message Reply with quote
Teddy



Joined: 06 Jul 2013
Posts: 4
Just a little precision. If I correctly understood, the calling convention of FASM DLL is __stdcall right ?

I'm saying that because I had to edit the ret 20 instruction of the end of the Fasm_Assemble function into ret to make it __cdecl.
Post 07 Jul 2013, 16:12
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2628
Location: dank orb
Your understanding is correct.

_________________
The generation of random numbers is too important to be left to chance - Robert R Coveyou
Post 07 Jul 2013, 17:56
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.