flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > FASM AS LIB

Author
Thread Post new topic Reply to topic
master92



Joined: 29 Dec 2008
Posts: 10
master92 29 Dec 2008, 22:59
Hello

I'm working on an application which uses FASM as backend. The application should be ONE executable and I don't want to deliver it with the FASM.exe or the FASM.dll I've seen a few posts ago. The first method I thought of would be to include the DLL as it is into the .data section of the executable and then load it directly from memory. But this method seems not to be very nice, firstly it's not very memory friendly (the dll resides in memory 2 times) and there's another better way I think: Creating a Library (.lib) file and then link it static into the executable.
So I converted the FASM Dll and made some little changes (see comments in code), exported it as MS COFF and linked it with a 3rd party linker (i tried the lib.exe from MASM32 package and polib.exe from Pelles C Compiler). Wether FASM nor the linker produced any errors, the .lib was created successfully.
The .lib is linked into my application without errors, too, but when I call the Assemble function, the debugger shows me a memory access violation (read error at adress 290465279, which is far away from the memory area allocated for FASM or the mapped executable in memory).
It would great if someone could help me!

Here's the FASM.ASM (from the FASM DLL package, with some extensions (commented) for FASM version 1.67.27 that were posted by windwakr in that thread) with my specific changes. (Please copy it into an editor because the overview of this big source file is not that good here.)
The rest of the changes I made you should see yourself, I'have commented them properly:

Code:

; flat assembler DLL interface for Win32
; Copyright (c) 1999-2006, Tomasz Grysztar.
; All rights reserved.

; modified for assembling into .lib file
; for direct executable integration

; --------------------------------

format MS COFF

; NO ENTRY POINT NEEDED FOR LIB
; entry DLLEntryPoint
; -----------------------------

include '..\..\include\win32a.inc'
include 'fasm.ash'

; EXTERNAL (resolved successfully by linker)

extrn '_GetEnvironmentVariableA@12' as GetEnvironmentVariable:Dword
extrn '_CreateFileA@28' as CreateFile:Dword
extrn '_ReadFile@20' as ReadFile:Dword
extrn '_SetFilePointer@16' as SetFilePointer:Dword
extrn '_CloseHandle@4' as CloseHandle:Dword
extrn '_WriteFile@20' as WriteFile:Dword
extrn '_GetSystemTime@4' as GetSystemTime:Dword

section '.data' data readable writeable

include '..\variable.inc'

state dd ?
esp_save dd ?
source dd ?
source_position dd ?
first_write dd ?
first_write_length dd ?
second_write dd ?
second_write_length dd ?
display_pipe dd ?

systime SYSTEMTIME

tmp dd ?
buffer rb 1000h

section '.code' code readable executable

public Assembler_Init as '_Assembler_Init' ; --> Original DLL ENTRY point
Assembler_Init:
        mov     eax,TRUE
    ret     12

public Assembler_GetVersion as '_Assembler_GetVersion'
Assembler_GetVersion:
     mov     eax,VERSION_MAJOR + VERSION_MINOR shl 16
    ret

public AssembleFile as '_AssembleFile'
AssembleFile:

        mov     eax,[lpSource]
      mov     [input_file],eax
    mov     [output_file],null_byte

 jmp     setup_assembler

public Assemble as '_Assemble'
Assemble:

  virtual at esp+4
    lpSource dd ?
    lpMemory dd ?
    cbMemorySize dd ?
    nPassesLimit dd ?
    hDisplayPipe dd ?
  end virtual

        mov     eax,[lpSource]
      mov     [source],eax
        mov     [source_position],0

     mov     [input_file],null_byte
      mov     [output_file],null_byte

  setup_assembler:

   mov     eax,[nPassesLimit]
  cmp     eax,10000h
  ja      invalid_parameter
   or      eax,eax
     jz      invalid_parameter
   mov     [passes_limit],ax

       mov     eax,[lpMemory]
      mov     ecx,[cbMemorySize]
  mov     [state],eax
 mov     [eax+FASM_STATE.condition],FASM_WORKING
     sub     ecx,sizeof.FASM_STATE
       jbe     out_of_memory
       add     eax,sizeof.FASM_STATE
       mov     [memory_start],eax
  mov     edx,ecx
     shr     edx,2
       sub     ecx,edx
     add     eax,ecx
     mov     [memory_end],eax
    mov     [additional_memory],eax
     add     eax,edx
     mov     [additional_memory_end],eax

     mov     [first_write],0
     mov     [second_write],0

        mov     eax,[hDisplayPipe]
  mov     [display_pipe],eax

      push    ebp
 mov     eax,esp
     mov     [esp_save],eax
      and     eax,not 0FFFh
       add     eax,1000h-10000h
    mov     [stack_limit],eax

       call    preprocessor
        call    parser
      call    assembler
   call    formatter

       mov     ebx,[state]
 mov     [ebx+FASM_STATE.condition],FASM_OK

  done:
       pop     ebp
 mov     eax,[ebx+FASM_STATE.condition]
      ret     20

  general_error:
      mov     esp,[esp_save]
      mov     ebx,[state]
 mov     [ebx+FASM_STATE.condition],eax
      jmp     done

  assembler_error:
  mov     esp,[esp_save]
      mov     ebx,[state]
 mov     [ebx+FASM_STATE.error_code],eax
     mov     eax,[current_line]
  mov     [ebx+FASM_STATE.error_line],eax
     mov     eax,FASM_ERROR
      jmp     general_error

  get_environment_variable:
        invoke  GetEnvironmentVariable,esi,buffer,1000h
     retn

  open:
     cmp     byte [edx],0
        je      open_memory
 invoke  CreateFile,edx,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0
     cmp     eax,-1
      je      file_error
  mov     ebx,eax
     clc
 retn
    file_error:
 stc
 retn
    open_memory:
        xor     ebx,ebx
     ret
  read:
  or      ebx,ebx
     jz      read_memory
 mov     ebp,ecx
     invoke  ReadFile,ebx,edx,ecx,tmp,0
  or      eax,eax
     jz      file_error
  cmp     ebp,[tmp]
   jne     file_error
  clc
 retn
    read_memory:
        push    esi edi
     mov     esi,[source]
        add     esi,[source_position]
       mov     edi,edx
     call    move_block
  pop     edi esi
     clc
 retn
    move_block:
 mov     al,cl
       shr     ecx,2
       rep     movsd
       mov     cl,al
       and     cl,11b
      rep     movsb
       retn
  lseek:
        or      ebx,ebx
     jz      seek_memory
 movzx   eax,al
      invoke  SetFilePointer,ebx,edx,0,eax
        cmp     eax,-1
      je      file_error
  retn
    seek_memory:
        push    esi
 mov     esi,[source]
        mov     ecx,edx
     or      al,al
       jz      seek_forward
        add     esi,[source_position]
       cmp     al,2
        je      seek_source_end
    seek_forward:
    sub     ecx,1
       jc      seek_complete
    seek_in_source:
    lodsb
       or      al,al
       loopnz  seek_in_source
      jnz     seek_complete
       dec     esi
    seek_complete:
       mov     eax,esi
     sub     eax,[source]
        mov     [source_position],eax
       pop     esi
 retn
    seek_source_end:
    lodsb
       or      al,al
       jnz     seek_source_end
     dec     esi
 sub     esi,edx
     cmp     esi,[source]
        jae     seek_complete
       mov     esi,[source]
        jmp     seek_complete
  create:
      or      ebx,-1
      clc
 retn
  write:
        cmp     [first_write],0
     jne     make_second_write
   mov     [first_write],edx
   mov     [first_write_length],ecx
    clc
 retn
    make_second_write:
  cmp     [second_write],0
    jne     cannot_write
        mov     [second_write],edx
  mov     [second_write_length],ecx
   clc
 retn
    cannot_write:
       stc
 retn
  close:
        or      ebx,ebx
     jz      file_closed
 cmp     ebx,-1
      je      output_ready
        invoke  CloseHandle,ebx
    file_closed:
     retn
    output_ready:
       mov     ebx,[state]
 cmp     [second_write],0
    jne     two_part_output
     mov     eax,[first_write]
   mov     [ebx+FASM_STATE.output_data],eax
    mov     eax,[first_write_length]
    mov     [ebx+FASM_STATE.output_length],eax
  retn
    two_part_output:
    mov     eax,[second_write]
  mov     [ebx+FASM_STATE.output_data],eax
    shuffle_output:
 mov     ecx,[first_write_length]
    cmp     ecx,[second_write_length]
   ja      small_second_part
   sub     [second_write_length],ecx
   mov     esi,[first_write]
   mov     edi,[second_write]
  call    xchg_block
  mov     [second_write],edi
  jmp     shuffle_output
    xchg_block:
       shr     ecx,1
       jnc     xchgb_ok
    mov     al,[edi]
    xchg    al,[esi]
    stosb
       inc     esi
      xchgb_ok:
  shr     ecx,1
       jnc     xchgw_ok
    mov     ax,[edi]
    xchg    ax,[esi]
    stosw
       add     esi,2
      xchgw_ok:
        jz      xchgd_ok
      xchgd:
        mov     eax,[edi]
   xchg    eax,[esi]
   stosd
       add     esi,4
       loop    xchgd
      xchgd_ok:
        ret
    small_second_part:
   mov     edi,[second_write]
  mov     esi,edi
     add     edi,[first_write_length]
    cmp     edi,[first_write]
   jbe     move_second_part
    mov     edi,[first_write]
   add     edi,[first_write_length]
    move_second_part:
       push    edi
 mov     ecx,[second_write_length]
   lea     eax,[edi+ecx]
       cmp     eax,[display_buffer]
        ja      out_of_memory
       call    move_block
  mov     edi,[second_write]
  mov     esi,[first_write]
   mov     ecx,[first_write_length]
    call    move_block
  pop     esi
 mov     ecx,[second_write_length]
   call    move_block
  mov     ecx,edi
     sub     ecx,[ebx+FASM_STATE.output_data]
    mov     [ebx+FASM_STATE.output_length],ecx
  retn

  display_block:
    mov     eax,[display_pipe]
  or      eax,eax
     jz      display_ok
  invoke  WriteFile,eax,esi,ecx,tmp,NULL
    display_ok:
       retn

  make_timestamp:
   invoke  GetSystemTime,systime
       movzx   ecx,[systime.wYear]
 mov     eax,ecx
     sub     eax,1970
    mov     ebx,365
     mul     ebx
 mov     ebp,eax
     mov     eax,ecx
     sub     eax,1969
    shr     eax,2
       add     ebp,eax
     mov     eax,ecx
     sub     eax,1901
    mov     ebx,100
     div     ebx
 sub     ebp,eax
     mov     eax,ecx
     xor     edx,edx
     sub     eax,1601
    mov     ebx,400
     div     ebx
 add     ebp,eax
     movzx   ecx,[systime.wMonth]
        mov     eax,ecx
     dec     eax
 mov     ebx,30
      mul     ebx
 add     ebp,eax
     cmp     ecx,8
       jbe     months_correction
   mov     eax,ecx
     sub     eax,7
       shr     eax,1
       add     ebp,eax
     mov     ecx,8
      months_correction:
       mov     eax,ecx
     shr     eax,1
       add     ebp,eax
     cmp     ecx,2
       jbe     day_correction_ok
   sub     ebp,2
       movzx   ecx,word [systime.wYear]
    test    ecx,11b
     jnz     day_correction_ok
   xor     edx,edx
     mov     eax,ecx
     mov     ebx,100
     div     ebx
 or      edx,edx
     jnz     day_correction
      mov     eax,ecx
     mov     ebx,400
     div     ebx
 or      edx,edx
     jnz     day_correction_ok
      day_correction:
      inc     ebp
      day_correction_ok:
 movzx   eax,[systime.wDay]
  dec     eax
 add     eax,ebp
     mov     ebx,24
      mul     ebx
 movzx   ecx,[systime.wHour]
 add     eax,ecx
     mov     ebx,60
      mul     ebx
 movzx   ecx,[systime.wMinute]
       add     eax,ecx
     mov     ebx,60
      mul     ebx
 movzx   ecx,[systime.wSecond]
       add     eax,ecx
     retn

include 'errors.inc'

include '..\expressi.inc'
include '..\preproce.inc'
include '..\parser.inc'
include '..\assemble.inc'
include '..\formats.inc'
include '..\x86_64.inc'
include '..\tables.inc'

include '..\version.inc'

;FOR FASM Version 1.67.27
include '..\messages.inc'
;----- 

copyright db 'Copyright (c) 1999-2006, Tomasz Grysztar',0Dh,0Ah
null_byte db 0

;FOR FASM Version 1.67.27
predefinitions db 0 
;-----

;--------------------------------------------------
;
; IMPORT SECTION NOT SUPPORTED FOR MS COFF CREATION
;
;section '.idata' import data readable writeable
;
;  library kernel32,'KERNEL32.DLL'
;
;  include '..\..\include\api\kernel32.inc'
;
;--------------------------------------------------
;
; EXPORT SECTION NOT NEEDED FOR MS COFF CREATION
;
;section '.edata' export data readable
;
;  export 'FASM.DLL',\
;       fasm_GetVersion,'fasm_GetVersion',\
;     fasm_Assemble,'fasm_Assemble',\
;         fasm_AssembleFile,'fasm_AssembleFile'
;
;--------------------------------------------------
;
; NOT SUPPORTED FOR MS COFF CREATION
;
;section '.reloc' fixups data discardable
;
;section '.rsrc' resource data readable
;
;  directory RT_VERSION,versions
;
;  resource versions,\
;      1,LANG_NEUTRAL,version
;
;  versioninfo version,VOS__WINDOWS32,VFT_APP,VFT2_UNKNOWN,LANG_ENGLISH+SUBLANG_DEFAULT,0,\
;           'FileDescription','flat assembler',\
;          'LegalCopyright',<'Copyright ',0A9h,' 2001-2006 Tomasz Grysztar.'>,\
;          'FileVersion',VERSION_STRING,\
;          'ProductVersion',VERSION_STRING,\
;       'OriginalFilename','FASM.DLL'

    


I think the Assembler_Init function (I created out of the original DLL Entry) is not necessary, because it has no real function except returning true =)

Here's the make.bat I used for assembling and linking (using lib.exe from MASM32 package)

Code:

@echo off
echo Creating assembler.lib
echo.
cd fasm
echo Assembling...
fasm.exe SOURCE\LIBRARY\FASM.ASM "..\assembler.obj"
cd..
echo.
echo Linking...
\masm32\bin\link -lib "assembler.obj" "/out:assembler.lib"

del "assembler.obj"
echo.
pause

    


My test application is written in PureBasic which uses FASM as backend, too. I don't know If anyone here knows it, but I think the source is easy to understand. The IncludeBinary in the DataSection directive means, that the specified file resides in the .data section of the executable, which is loaded with the executable. I created a Label (Input:). ?Input is the memory pointer to that Label. So ?Input points to the beginning of the ASM source in memory which has been integrated (.data section) into the executable.

Here's the test code:

Code:

DataSection
Input:
IncludeBinary "D:\Projects\assembler\fasm\EXAMPLES\HELLO\HELLO.ASM"
Data.b 0 ;an additional 0 Byte is added
EndDataSection

Import "assembler.lib"
Assembler_Init()
Assemble(*lpSource,*lpMemory,cbMemorySize.l,nPassesLimit.l,hDisplayPipe.l)
EndImport
 

*mem = AllocateMemory(4096*1024*8)

Debug Assembler_Init()

Debug Assemble(?Input,*mem,4096*1024*8,100,0)

FreeMemory(*mem)

    

I'm sure the error is not in this code because the code works and assembles properly when calling the function from the original dll

Ok, this post got very long, but It would be great if someone could find out the error. Thanks.

Bye

P.S. If my English is not that good, sorry, i'm German.
Post 29 Dec 2008, 22:59
View user's profile Send private message ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 30 Dec 2008, 01:49
wrote:
... read error at adress 290465279 ...
Can you give more information about the error. Is the value you give hex or decimal? My guess is decimal since it is so large. Is that value the instruction pointer or a data pointer? At what stage of the process does it fail? Does it pass the preprocessor stage? Parser? Assembler? Formatter? Where? Is the error in the assembler lib or your wrapper code?

BTW, you can use print statements (or int3 instructions) to show if various parts have been reached.
Post 30 Dec 2008, 01:49
View user's profile Send private message Visit poster's website Reply with quote
master92



Joined: 29 Dec 2008
Posts: 10
master92 30 Dec 2008, 10:54
revolution wrote:
Can you give more information about the error. Is the value you give hex or decimal? My guess is decimal since it is so large. Is that value the instruction pointer or a data pointer? At what stage of the process does it fail? Does it pass the preprocessor stage? Parser? Assembler? Formatter? Where? Is the error in the assembler lib or your wrapper code?

BTW, you can use print statements (or int3 instructions) to show if various parts have been reached.


The read error address the debugger (from PureBasic, no ASM debugger) gives out is decimal, but as I said the memory area at this address is far away from the area that has been allocated with AllocateMemory in my test application (which is translated to the HeapAlloc WINAPI function) and far away from the area the executable is mapped into memory (I checked that with the PureBasic debugger). I don't know what kind of pointer that actually is. I only know that a instruction in the Assemble function causes to read at that specified memory address but this address actually isn't allocated for this process. I'm not very familiar with ASM debugging, I've been using the debugger of my programming language, and this one only shows me that the memory access violation occurs in the Assemble function. The error must have something to do with the assembler lib because my wrapper code works when calling the fasm_Assemble function with the same parameters from the original dll.
As I said, I'm sad, but I don't know how to efficiently debug the ASM Code, I think I'll write some Messagebox macro to see at which part of the ASM Code the error occurs.

Thanks

P.S.: Because I heard the word wrapper:
I'm not going to integrate FASM into my executable and publish that as my own assembler =)
This is only a test application. The final executable will be something like a compiler or something similar, in any case the ASM Code is generated at runtime dependent on the user's input and FASM is used to translate it into executable code.
Post 30 Dec 2008, 10:54
View user's profile Send private message ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 30 Dec 2008, 11:42
I recommend Ollydbg for debugging ASM.
Post 30 Dec 2008, 11:42
View user's profile Send private message Visit poster's website Reply with quote
master92



Joined: 29 Dec 2008
Posts: 10
master92 30 Dec 2008, 12:01
revolution wrote:
I recommend Ollydbg for debugging ASM.


I'll test it. Thx.
Post 30 Dec 2008, 12:01
View user's profile Send private message ICQ Number Reply with quote
master92



Joined: 29 Dec 2008
Posts: 10
master92 31 Dec 2008, 00:07
Ok, it crashes directly after entering the Assemble function.

When i put a return statement right after the Assemble label the Assemble function returns properly ( who would have expected that =) )

Code:
Assemble:
ret

virtual at esp+4 ...
    


But when I try

Code:
Assemble:
invoke MessageBox,0,"TEST","TEST",MB_OK

ret
    


the code crashes at this MessageBox !!??!!
I didn't understand the complex interface of Ollydbg, the only thing I could find out that it crashes right after entering the Assemble function (which I also found out with my MessageBox debugging method...)

Anyone a solution? It think it could have to do with the calling convention, the way parameters are passed etc...
Functions that do not call anything (like the Assembler_Init function) return properly but otherwise the program crashes...
Post 31 Dec 2008, 00:07
View user's profile Send private message ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 31 Dec 2008, 01:55
Be more specific about what happens when you say "crashes". The are many things that can make it "crash". Examine the code at the point of the "crash" with Ollydbg. Just let it run (F9) and when it crashes you get to see the where it happened with all the gory details of registers contents and instruction location etc.
Post 31 Dec 2008, 01:55
View user's profile Send private message Visit poster's website Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 244
Location: US
Yardman 31 Dec 2008, 07:12
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 03:34; edited 1 time in total
Post 31 Dec 2008, 07:12
View user's profile Send private message Reply with quote
master92



Joined: 29 Dec 2008
Posts: 10
master92 01 Jan 2009, 14:49
Ok, after some debugging and hundreds of test tries I actually don't know how to go on.

OllyDBG shows the following messagebox (actually the same message as the PureBasic debugger shows):
Error
Don't know how to continue because memory at address 57A425FF is not readable. Try to change EIP or pass exception to program.

@Yardman
I made all the changes you suggested but there's no noticeable difference...

So the last thing I tried was the following simple code:

Code:

format MS COFF

include 'fasm\include\win32a.inc'

extrn '_MessageBoxA@16' as MessageBox:Dword


section '.data' data readable writeable

_teststring: db "TEST",0

section '.code' code readable executable


public _test@0
_test@0:

invoke MessageBox,0,_teststring,_teststring,0

ret    


with the following simple PureBasic code:

Code:
Import "test.lib" 
test() As "_test@0"
EndImport

test()
    


The error occurs even in this simple messagebox code!
Perhaps there's some incompatibility with FASM and the two linkers I tried... I actually don't know how to continue...

Another part of my application consists of a parser engine written in MASM originally designed as a DLL. I had no problems converting it into a lib. I even didn't had to change very much of the code, only renaming the DllMain into Parser_Init and the fdwReason variable testing in DllMain.
And I actually thought here it would be easy, too, but the first thing was that FASM doesn't allow me to import functions when exporting aa a MS COFF object file (MASM supports that, it import the whole lib into the object file!!!). The problem was not that big, only write some EXTRN statements and import the needed LIBS when linking the final executable...
But now I've that problem and I don't find out why this fucking invalid memory access occurs.
Anyone a suggestion where the problem is or is there another way to convert FASM into a library file?
Post 01 Jan 2009, 14:49
View user's profile Send private message ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 01 Jan 2009, 15:02
I don't have PureBasic so I can't test this for you. Perhaps you can zip the exe and attach it. It may give a clue about what has been linked etc.
Post 01 Jan 2009, 15:02
View user's profile Send private message Visit poster's website Reply with quote
master92



Joined: 29 Dec 2008
Posts: 10
master92 01 Jan 2009, 19:36
revolution wrote:
I don't have PureBasic so I can't test this for you. Perhaps you can zip the exe and attach it. It may give a clue about what has been linked etc.


The Purebasic Compiler uses FASM as backend, too. It generates ASM output and sends it to FASM, which generates a MS COFF file which is linked by a 3rd party compiler (I think polib.exe from Pelles C Compiler).

Here's the zip (attachment) with the sourcecode, the linked lib (linked with MASM32 Library Manager) and the final test executable.

The generated ASM output (for FASM) by the PureBasic Compiler (exports as MS COFF, so I think a additional linker is required for linking into executable (must import the Windows Library kernel32.lib and the test.lib)),
but I already compiled this one and added the executable into the attachment!
So the fully source is provided for FASM. (The error must be somewhere... I hope...)

Code:

; 
; PureBasic 4.20 (Windows - x86) generated code
; 
; (c) 2008 Fantaisie Software
; 
; The header must remain intact for Re-Assembly
; 
; :System
; KERNEL32
; :Import
; D:\Projekte\Compiler\Development\assembler\test.lib
; 
format MS COFF
; 
extrn _test@0
extrn _ExitProcess@4
extrn _GetModuleHandleA@4
extrn _HeapCreate@12
extrn _HeapDestroy@4
; 
extrn _memset
extrn _PB_StringBase
extrn PB_StringBase
extrn _SYS_InitString@0
; 
extrn _PB_StringBasePosition
public _PB_Instance
public _PB_ExecutableType
public _PB_MemoryBase
public PB_Instance
public PB_MemoryBase
public _PB_EndFunctions
public _PB_DEBUGGER_LineNumber
public _PB_DEBUGGER_IncludedFiles

macro pb_public symbol
{
  public  _#symbol
  public symbol
_#symbol:
symbol:
}

macro    pb_align value { rb (value-1) - ($-_PB_DataSection + value-1) mod value }
macro pb_bssalign value { rb (value-1) - ($-_PB_BSSSection  + value-1) mod value }
public PureBasicStart
; 
section '.code' code readable executable
; 
; 
PureBasicStart:
; 
  PUSH   dword I_BSSEnd-I_BSSStart
  PUSH   dword 0
  PUSH   dword I_BSSStart
  CALL  _memset
  ADD    esp,12
  PUSH   dword 0
  CALL  _GetModuleHandleA@4
  MOV    [_PB_Instance],eax
  PUSH   dword 0
  PUSH   dword 4096
  PUSH   dword 0
  CALL  _HeapCreate@12
  MOV    [PB_MemoryBase],eax
  CALL  _SYS_InitString@0
; :
; Import "test.lib" 
; test() As "_test@0"
; EndImport
; 
; test()
  CALL  _test@0
_PB_EOP_NoValue:
  PUSH   dword 0
_PB_EOP:
  CALL  _PB_EndFunctions
  PUSH   dword [PB_MemoryBase]
  CALL  _HeapDestroy@4
  CALL  _ExitProcess@4
_PB_EndFunctions:
  RET
; 
; 
section '.data' data readable writeable
; 
_PB_DataSection:
_PB_DEBUGGER_LineNumber: dd -1
_PB_DEBUGGER_IncludedFiles: dd 0
_PB_ExecutableType: dd 0
public _SYS_StaticStringStart
_SYS_StaticStringStart:
pb_public PB_NullString
  db     0
public _SYS_StaticStringEnd
_SYS_StaticStringEnd:
align 4
align 4
s_s:
  dd     0
  dd     -1
align 4
; 
section '.bss' readable writeable
_PB_BSSSection:
align 4
; 
I_BSSStart:
_PB_MemoryBase:
PB_MemoryBase: rd 1
_PB_Instance:
PB_Instance: rd 1
; 
align 4
PB_DataPointer rd 1
align 4
align 4
align 4
align 4
I_BSSEnd:
section '.data' data readable writeable
SYS_EndDataSection:

    


Description: Sourcecode, .lib and final executable
Download
Filename: test.zip
Filesize: 2.02 KB
Downloaded: 462 Time(s)

Post 01 Jan 2009, 19:36
View user's profile Send private message ICQ Number Reply with quote
master92



Joined: 29 Dec 2008
Posts: 10
master92 01 Jan 2009, 23:19
Ok, some debugging information... (the little MessageBox testapp)

this part seems to be relevant:
The calling of the MessageBox function in my test app
(disassembled by OllyDbg)

PUSH 0
PUSH test.00403004 ; ASCII "TEST"
PUSH test.00403004 ; ASCII "TEST"
PUSH 0
CALL DWORD PTR DS:[402068]

The memory access violation occurs on that CALL
The four push statements pass the four parameters to the stack...
the call parameter usually jumps to the specified address...
OllyDbg translates the DS:[402068] memory pointer into the absolute hex address 30A425FF which is far away from the mapped executable code in memory...
but I think there's no possibility influencing that... because the memory addresses are translated by the linker I think...
Memory addresses in .OBJ files are not fixed, they are relocatable and generated by the linker when linking the final executable as far as I know...
I give up... I think I do not get my .lib in that way...

Anyone a suggestion how to merge FASM into a working .lib file (probably another way, another linker, a completely different solution)? The problem is that FASM cannot output library files... so I initially decided to output it as MS COFF and link it with another linker... But it seems that my way doesn't work...
Post 01 Jan 2009, 23:19
View user's profile Send private message ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 02 Jan 2009, 00:32
To solve your problem you have to have this line
Code:
stdcall MessageBox,0,_teststring,_teststring,0    
Don't use invoke, your code is not linking directly with the Win32 API.
Post 02 Jan 2009, 00:32
View user's profile Send private message Visit poster's website Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 244
Location: US
Yardman 02 Jan 2009, 01:36
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 03:35; edited 1 time in total
Post 02 Jan 2009, 01:36
View user's profile Send private message Reply with quote
master92



Joined: 29 Dec 2008
Posts: 10
master92 02 Jan 2009, 14:20
All problems have been solved Very Happy Very Happy Very Happy

Quote:

Don't use invoke, your code is not linking directly with the Win32 API.


Yeah stdcall worked...
And Yardman's solution too...
I thought invoke and stdcall would be the same...
So I'd have to replace alle invokes in FASM source with stdcall...
And I didn't know that PureBasic could import .obj files directly... Works fine...

Now I have two working solutions... Thank you for your help! Great forum here...
Post 02 Jan 2009, 14:20
View user's profile Send private message ICQ Number 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.