Fastcall name decoration in Windows does not port easily

363 views Asked by At

I'm using mingw in Windows to compile code in C and assembly, several functions in which have the fastcall calling convention (as Microsoft defines it). If I use __fastcall in the declaration, mingw does what Windows does and name decorates:

An at sign (@) is prefixed to names; an at sign followed by the number of bytes (in decimal) in the parameter list is suffixed to names

This works fine. I have labels in assembly in the form:

.global @myfunction@4
@myfunction@4:
    ....code....

But this proves a big problem when I port to Linux (x86, 32 bit). Gcc suddenly does not like __fastcall (or __cdecl for that matter) and does not like @ in labels at all. I'm not sure how I can unify the two issues - either get gcc in Linux to like @ or get mingw in Windows to not add the @.

Also: I can use __attribute__(__cdecl__) in place of __cdecl but I'm puzzled as to where it goes. I assumed before the function name itself but I see people putting it after the declaration and before the semicolon. Can I do either?

1

There are 1 answers

0
carveone On

Related answer: Adding leading underscores to assembly symbols with GCC on Win32?

Name decoration appears to be a common theme when porting between operating systems, platforms and even processors on the same platform (IA32 to IA64 for example loses the underscore).

The way I solved this was to remove the @ decoration from all the function that used it as I didn't need to export them other than for testing. The other functions were redefined from function to _function using macros (that's what macro assemblers are for after all).

In this case I renamed the assembly code from .s to .sx (Windows platform) and uses the gcc preprocessor to check for _WIN32 and thus redefine export global symbols to have leading underscores. The same for calls to _calloc and _free.