Subject: Re: linking gcc & vc++ object files
From: [email-masked] (george montanaro)

3 Aug 1998 20:03:53 -0700
Message-ID: <9808031328.AA08246.cygnus.gnu-win32@porsche>
References: <[email-masked]>
Reply-To: [email-masked]
To: [email-masked]
Cc: [email-masked]

 > We are linking object files generated by gcc and object files  
 > generated by vc++ on Windows-NT. In order to get object files  
 > generated by gcc to link with object files generated by VC++ we have  
 > to append a "@n" suffix (n seems to be always a multiple of 4) to the  
 > function names (in the gcc related source files) whos object  
 > definition is in the files generated by VC++.
 > 
 > QUESTION: What is the "@n" all about?

David,

I was confused by this as well when I first began using cygwin32.  The
answer lies in the way the function's parameters are pushed onto and
popped from the stack.  In most (all?) Unix systems the caller is
responsible both for pushing arguments on the stack before the call
and popping arguments off the stack after the call.  If the programmer
makes a mistake and calls a function with the wrong number of
arguments the tragedy is limited to that single call, because the
compiler knows how many bytes to pop after the call.

In the Win32 API, however, the function call interface is different.
The caller pushes the arguments on the stack, but the called function
pops the arguments before it returns.  Seems a bit bogus to me, but
that's the way the folks in Redmond did it, so we're stuck with it.
If you call such a function with the wrong number of arguments (say,
two long ints instead of three) the caller will push a different
number of bytes onto the stack than the callee pops.  This completely
hoses the stack if it happens.

To try and prevent complete chaos, the function names are generated in
the object files with the number of bytes being passed by the caller
appended to the name, and the functions names in the library have
appended to them the number of bytes the function will pop before it
returns.  If you mistakenly call function foo with two long ints
instead of three, your code will attempt to link with a function named
foo@8.  However, there will only be a function in the library named
foo@12.  You'll get a link error, which will alert you to go back and
check how you called function foo.

If you compile your code with -DWIN32 I think you'll find that gcc
will generate the proper names for those functions that require it.
Take a look in some of the Windows header files.  You should see some
dependence on the definition of that macro.  There's an extra keyword
in the function prototypes that gets enabled if WIN32 is defined.

-- 
Skip Montanaro
([email-masked], 518 387 6792)
([email-masked], 518 372 5583)
-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".