NO

Author Topic: Static linking to FASM  (Read 4482 times)

magic

  • Guest
Static linking to FASM
« on: October 21, 2008, 10:38:41 PM »
Hi I try to use pellesC function in FASM using static linking. Every thing work well except when I going to use memory related function like malloc(), my exe created has error.

PellesC can actually compile to object code. The linking between FASM code (main) and PellesC (function) also work. But the resulting code has error. This error only happen when I use this memory related function. Using other function work well.

Please help me to resolve this matter. Thanks.


This is my FASM code (main). Compile  MS COFF format

include 'header.asm' ;Compile  MS COFF format in this header

      extrn '__imp__ExitProcess@0' as ExitProcess:dword
      extrn _DebugA@4:DWORD

section '.data' data readable writeable

     _a                                       dd        0
 
section '.code' code readable executable

     public start
     start:

               MOV      [_a],6
               PUSH     DWORD   3
               CALL     _DebugA@4
               invoke   ExitProcess,    0

And this is my PellesC code (function). Compile with  -Tx86-coff -MT -Ot -W1 -Gz -Ze

#include <windows.h>
#include <math.h>
#include <fenv.h>
#include <stdio.h>
#include <stdlib.h>

int DebugA(int a)
{
   void *temp=malloc(10); //without this two function the exe work
        free(temp);

        MessageBoxA(0,"test","Debug",0);
   return 0;
}

I link the above object code using polink /SUBSYSTEM:WINDOWS /ENTRY:start asm.obj PellesC.obj crtmt.lib.....and all other file (no problem on this because its work)
Just when I added the above memory function the exe has error. Look like the memory region corrupted.


                                                                             
« Last Edit: October 21, 2008, 11:34:14 PM by magic »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Static linking to FASM
« Reply #1 on: October 22, 2008, 09:21:58 AM »
You get the error because the execution is started directly in your code; this prevents the initialization of library functions like "malloc".
Try changing the start of your code from "start" to "_main" and remove the switch '/ENTRY:start' from linker. In this case the execution will start in the library initialization code (initializating the dynamic memory system) and then will execute your code.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

magic

  • Guest
Re: Static linking to FASM
« Reply #2 on: October 23, 2008, 09:52:03 AM »
Yes its work. Thank you for the info.