flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Compile FASM to obj file

Author
Thread Post new topic Reply to topic
Teddy



Joined: 06 Jul 2013
Posts: 4
Teddy 06 Jul 2013, 14:05
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: 20344
Location: In your JS exploiting you and your system
revolution 06 Jul 2013, 21:17
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
Teddy 06 Jul 2013, 22:31
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: 4043
Location: vpcmpistri
bitRAKE 06 Jul 2013, 23:08
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
Teddy 07 Jul 2013, 01:00
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
Teddy 07 Jul 2013, 16:12
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: 4043
Location: vpcmpistri
bitRAKE 07 Jul 2013, 17:56
Your understanding is correct.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
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


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

Website powered by rwasa.