Name Mangling - C Name Decoration in Microsoft Windows

C Name Decoration in Microsoft Windows

Although name mangling is not generally required or used by languages that do not support function overloading (such as C and classic Pascal), they use it in some cases to provide additional information about a function. For example, compilers targeted at Microsoft Windows platforms support a variety of calling conventions, which determine the manner in which parameters are sent to subroutines and results returned. Because the different calling conventions are not compatible with one another, compilers mangle symbols with codes detailing which convention should be used.

The mangling scheme was established by Microsoft, and has been informally followed by other compilers including Digital Mars, Borland, and GNU GCC, when compiling code for the Windows platforms. The scheme even applies to other languages, such as Pascal, D, Delphi, Fortran, and C#. This allows subroutines written in those languages to call, or be called by, existing Windows libraries using a calling convention different from their default.

When compiling the following C examples:

int _cdecl f (int x) { return 0; } int _stdcall g (int y) { return 0; } int _fastcall h (int z) { return 0; }

32 bit compilers emit, respectively:

_f _g@4 @h@4

In the stdcall and fastcall mangling schemes, the function is encoded as _name@X and @name@X respectively, where X is the number of bytes, in decimal, of the argument(s) in the parameter list (including those passed in registers, for fastcall). In the case of cdecl, the function name is merely prefixed by an underscore.

Note that the 64-bit convention on Windows (Microsoft C) is no leading underscore. This difference may in some rare cases lead to unresolved externals when porting such code to 64 bits. For example, Fortran code can use 'alias' to link against a C method by name as follows:

SUBROUTINE f !DEC$ ATTRIBUTES C, ALIAS:'_f' :: f END SUBROUTINE

This will compile and link fine under 32 bits, but generate an unresolved external '_f' under 64 bits. One work around for this is to not use 'alias' at all (in which the method names typically need to be capitalized in C and Fortran), or to use the BIND option:

SUBROUTINE f BIND(C,NAME="f") END SUBROUTINE

Read more about this topic:  Name Mangling

Famous quotes containing the words decoration and/or windows:

    If there be any man who thinks the ruin of a race of men a small matter, compared with the last decoration and completions of his own comfort,—who would not so much as part with his ice- cream, to save them from rapine and manacles, I think I must not hesitate to satisfy that man that also his cream and vanilla are safer and cheaper by placing the negro nation on a fair footing than by robbing them.
    Ralph Waldo Emerson (1803–1882)

    —and then it was
    There interposed a Fly—

    With Blue—uncertain stumbling Buzz—
    Between the light—and me—
    And then the Windows failed—and then
    I could not see to see—
    Emily Dickinson (1830–1886)