flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > problem linking 64bit .obj file to c code

Author
Thread Post new topic Reply to topic
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 30 Aug 2009, 17:35
I don't know what I'm doing wrong with linking and fasm generated .obj file to my c code. I keep getting this error:
error LNK2001: unresolved external symbol "unsigned int __cdecl fasm_rndintq32(unsigned int)" (?fasm_rndintq32@@YAII@Z)

Here is the source to my fasm program:
Code:
; This is example of mixing C code and assembly
; definitions of macros we use
format MS64 COFF

include '%fasminc%\win64w.inc'

public fasm_rndintq32 as '_fasm_rndintq32@4'
;unsigned int fasm_rndintq32(unsigned int);

section '.text' code readable executable

;section '.data' data readable writeable

proc fasm_rndintq32 c rndidum:DWORD
     mov    eax, $12345678
     ret
endp    


I assume it has something to do with the function name. Anybody have any other idea's?

_________________
Gimme a sledge hammer! I'LL FIX IT!
Post 30 Aug 2009, 17:35
View user's profile Send private message 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 Aug 2009, 17:42
madmatt wrote:
... (?fasm_rndintq32@@YAII@Z)...
public fasm_rndintq32 as '_fasm_rndintq32@4'
Shouldn't that be like this:
Code:
public fasm_rndintq32 as '?fasm_rndintq32@@YAII@Z'    
I'm no C expert but just a guess?
Post 30 Aug 2009, 17:42
View user's profile Send private message Visit poster's website Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael 30 Aug 2009, 18:13
madmatt, you're compiling as C++, not C! Put extern "C" before declaration, e.g.:
Code:
extern "C" unsigned int fasm_rndintq32(unsigned int);    
And if function's name is mangled as "_foo@4", then it's stdcall function - put __stdcall between return type and name in C/C++ code, and, of couse, remove that c in assembly code.
Post 30 Aug 2009, 18:13
View user's profile Send private message Reply with quote
chaoscode



Joined: 21 Nov 2006
Posts: 64
chaoscode 30 Aug 2009, 18:23
the problem is the naming convention and the use of the same funktion with diffenent parameters (in German we call it "Überladen" the englisch translation would be "Overloading" or sth simmilar)

in C there was a underscore before the function and thats all.

but in c++ you can have more functions with the same name but diffenrent type of Parameters.
e.g.:

char* translate(double d);
char* translate(float f);
char* translate(void* target, char* mem1, int[] stuff);

and the compiler generates from the parameters an additional string that the compiler appends to the Function name so that it can identyfi every function.

see
http://www.daniweb.com/forums/thread8072.html#
http://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B_Name_Mangling
Post 30 Aug 2009, 18:23
View user's profile Send private message ICQ Number Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael 30 Aug 2009, 19:30
chaoscode wrote:
in C there was a underscore before the function and thats all
Yep, that's true, albeit only for cdecl functions. For stdcall ones there is an underscore before and @<size of arguments in bytes> after the name (e.g. _ShowWindow@8 ). For fastcall ones, there is a @ before the name and same thing as in stdcall (in this case, size of arguments passed in registers) after (@MyRetardedFunction@0).
Post 30 Aug 2009, 19:30
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 30 Aug 2009, 20:04
Fanael
Quote:
madmatt, you're compiling as C++, not C! Put extern "C" before declaration

This helped.

chaoscode
Quote:
in C there was a underscore before the function and thats all.

This gave me an idea to remove the underscore from the function name string, and with the above idea, I've managed to get it to compile without error.

I'll repost the source In case anybody needs to use it. (thanks to 'vid' for the original 32bit sources Smile , thanks to Fanael and chaoscode also Smile )
[EDIT: correction]
Code:
; This is example of mixing C code and assembly
; definitions of macros we use
format MS64 COFF

include '%fasminc%\win64w.inc'

public fasm_rndintq32 as 'fasm_rndintq32'
;extern "C" unsigned int fasm_rndintq32(unsigned int);

section '.text' code readable executable

;section '.data' data readable writeable

proc fasm_rndintq32 c rndidum:DWORD
     mov    eax, $12345678
     ret
endp    

_________________
Gimme a sledge hammer! I'LL FIX IT!
Post 30 Aug 2009, 20:04
View user's profile Send private message 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.