Adding leading underscores to assembly symbols with GCC on Win32?

23,093

Solution 1

One option, though dangerous, is to convince GCC to omit the ABI-required leading underscore.

  • -fleading-underscore

    This option and its counterpart, -fno-leading-underscore, forcibly change the way C symbols are represented in the object file. One use is to help link with legacy assembly code.

    Warning: the -fleading-underscore switch causes GCC to generate code that is not binary compatible with code generated without that switch. Use it to conform to a non-default application binary interface. Not all targets provide complete support for this switch.

Another, safer option, is to explicitly tell GCC the name to use.

5.39 Controlling Names Used in Assembler Code

You can specify the name to be used in the assembler code for a C function or variable by writing the asm (or __asm__) keyword after the declarator as follows:

     int foo asm ("myfoo") = 2;

This specifies that the name to be used for the variable foo in the assembler code should be ``myfoo' rather than the usual \``_foo'.

On systems where an underscore is normally prepended to the name of a C function or variable, this feature allows you to define names for the linker that do not start with an underscore.

It does not make sense to use this feature with a non-static local variable since such variables do not have assembler names. If you are trying to put the variable in a particular register, see Explicit Reg Vars. GCC presently accepts such code with a warning, but will probably be changed to issue an error, rather than a warning, in the future.

You cannot use asm in this way in a function definition; but you can get the same effect by writing a declaration for the function before its definition and putting asm there, like this:

 extern func () asm ("FUNC");

 func (x, y)
      int x, y;
 /* ... */

It is up to you to make sure that the assembler names you choose do not conflict with any other assembler symbols. Also, you must not use a register name; that would produce completely invalid assembler code. GCC does not as yet have the ability to store static variables in registers. Perhaps that will be added.

In your case,

extern int bar(int x) asm("bar");

should tell GCC that "bar uses asm name ``bar`', even though it's a ccall function".

Solution 2

You can use the C preprocessor to preprocess your assembly and use a macro to add the missing underscores on Windows. First, you need to rename your assembly file from bar.s to bar.S (capital 'S'). This tells gcc to use cpp to preprocess the file.

To add the missing underscores, you can define a macro "cdecl" like this:

#if defined(__WIN32__)
# define cdecl(s) _##s
#else
# define cdecl(s) s
#endif

Then use it like this:

.global cdecl(bar)
cdecl(bar):
    movl 4(%esp), %eax
    addl %eax, %eax
    ret

Note that Mac OSX also requires leading underscores, so you can update the first line of the macro like this:

#if defined(__WIN32__) || defined(__APPLE__)

Solution 3

can you declare it twice?

.global bar
.global _bar

I haven't written assembly in awhile, but does the .global identifier just act sort of like a label?

Solution 4

Compilers for the ELF target do not add leading underscores by default. You could add -fleading-underscore when compiling to ELF format (under Linux). Use a conditional in the makefile.

Reference: http://opencores.org/openrisc,gnu_toolchain (do an on-page search for "leave global names unchanged")

Share:
23,093
Admin
Author by

Admin

Updated on March 13, 2020

Comments

  • Admin
    Admin about 4 years

    I have a piece of C code that calls a function defined in assembly. By way of example, let's say foo.c contains:

    int bar(int x);  /* returns 2x */
    int main(int argc, char *argv[]) { return bar(7); }
    

    And bar.s contains the implementation of bar() in x86 assembly:

    .global bar
    bar:    movl 4(%esp), %eax
            addl %eax, %eax
            ret
    

    On Linux I can easily compile and link these sources with GCC as follows:

    % gcc -o test foo.c bar.s
    % ./test; echo $?
    14
    

    On Windows with MinGW this fails with an error of "undefined reference to `bar'". It turns out the cause for this is that on Windows all identifiers of functions with C calling convention are prefixed with an underscore, but since "bar" is defined in assembly, it doesn't get this prefix and linking fails. (So the error message is actually complaining about missing the symbol _bar, not bar.)

    To summarize:

    % gcc -c foo.c bar.s
    % nm foo.o bar.o
    foo.o:
    00000000 b .bss
    00000000 d .data
    00000000 t .text
             U ___main
             U _bar
    00000000 T _main
    
    bar.o:
    00000000 b .bss
    00000000 d .data
    00000000 t .text
    00000000 T bar
    

    The question now is: how can I resolve this nicely? If I were writing for Windows only, I could just add the underscore to the identifier in bar.s, but then the code breaks on Linux. I have looked at gcc's -fleading-underscore and -fno-leading-underscore options but neither appears to do anything (at least on Windows).

    The only alternative I see now is passing the assembly file through the C preprocessor and redefining all the declared symbols manually if WIN32 is defined, but that's not very pretty either.

    Does anyone have a clean solution for this? Perhaps a compiler option I oversaw? Maybe the GNU assembler supports a way to specific that this particular symbol refers to a function using C calling convention and should be mangled as such? Any other ideas?