The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] why the leading under score added to function names?
@ 2012-02-20 20:52 arnold
  2012-02-20 23:21 ` Dave Horsfall
  0 siblings, 1 reply; 17+ messages in thread
From: arnold @ 2012-02-20 20:52 UTC (permalink / raw)


Hi All.

Recently at work I helped someone figure out that when working with ld,
the name of a function "foo" gets turned into "_foo" by the compiler.
(It took this old-timer 15 minutes to solve a problem he had been working
on for two days!)

I'm pretty sure this dates back to PDP-11 days.  I'm wondering "why?".
Why did the C compiler prepend an underscore to function names?

Thanks,

Arnold



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-20 20:52 [TUHS] why the leading under score added to function names? arnold
@ 2012-02-20 23:21 ` Dave Horsfall
  2012-02-21  0:34   ` Brantley Coile
  0 siblings, 1 reply; 17+ messages in thread
From: Dave Horsfall @ 2012-02-20 23:21 UTC (permalink / raw)


On Mon, 20 Feb 2012, arnold at skeeve.com wrote:

[...]

> I'm pretty sure this dates back to PDP-11 days.  I'm wondering "why?".
> Why did the C compiler prepend an underscore to function names?

Sure was the PDP-11 :-)  I vaguely recall that it was to make sure that
user functions did not conflict with predefined assembler functions, as
that would be a pain to diagnose (much like having swap overlap root).

-- Dave



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-20 23:21 ` Dave Horsfall
@ 2012-02-21  0:34   ` Brantley Coile
  2012-02-21  2:50     ` Warner Losh
  0 siblings, 1 reply; 17+ messages in thread
From: Brantley Coile @ 2012-02-21  0:34 UTC (permalink / raw)


correct.  we could link to assembler code with _entry points and not worry about symbol collisions in the rest of the code. 

iPhone email

On Feb 20, 2012, at 6:23 PM, "Dave Horsfall" <dave at horsfall.org> wrote:

> On Mon, 20 Feb 2012, arnold at skeeve.com wrote:
> 
> [...]
> 
>> I'm pretty sure this dates back to PDP-11 days.  I'm wondering "why?".
>> Why did the C compiler prepend an underscore to function names?
> 
> Sure was the PDP-11 :-)  I vaguely recall that it was to make sure that
> user functions did not conflict with predefined assembler functions, as
> that would be a pain to diagnose (much like having swap overlap root).
> 
> -- Dave
> _______________________________________________
> TUHS mailing list
> TUHS at minnie.tuhs.org
> https://minnie.tuhs.org/mailman/listinfo/tuhs



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-21  0:34   ` Brantley Coile
@ 2012-02-21  2:50     ` Warner Losh
  2012-02-21  3:33       ` John Cowan
                         ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Warner Losh @ 2012-02-21  2:50 UTC (permalink / raw)


And this convention went away with ELF binaries.  No more _foo for function foo.

Also, the fortran compiler would emit entry_ to as to not conflict either.  Made calling C from Fortran, and vice versa, a lot of fun...

Warner

On Feb 20, 2012, at 5:34 PM, Brantley Coile wrote:

> correct.  we could link to assembler code with _entry points and not worry about symbol collisions in the rest of the code. 
> 
> iPhone email
> 
> On Feb 20, 2012, at 6:23 PM, "Dave Horsfall" <dave at horsfall.org> wrote:
> 
>> On Mon, 20 Feb 2012, arnold at skeeve.com wrote:
>> 
>> [...]
>> 
>>> I'm pretty sure this dates back to PDP-11 days.  I'm wondering "why?".
>>> Why did the C compiler prepend an underscore to function names?
>> 
>> Sure was the PDP-11 :-)  I vaguely recall that it was to make sure that
>> user functions did not conflict with predefined assembler functions, as
>> that would be a pain to diagnose (much like having swap overlap root).
>> 
>> -- Dave
>> _______________________________________________
>> TUHS mailing list
>> TUHS at minnie.tuhs.org
>> https://minnie.tuhs.org/mailman/listinfo/tuhs
> _______________________________________________
> TUHS mailing list
> TUHS at minnie.tuhs.org
> https://minnie.tuhs.org/mailman/listinfo/tuhs
> 
> 




^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-21  2:50     ` Warner Losh
@ 2012-02-21  3:33       ` John Cowan
  2012-02-21  3:41       ` Steve Nickolas
  2012-02-21 18:18       ` ron
  2 siblings, 0 replies; 17+ messages in thread
From: John Cowan @ 2012-02-21  3:33 UTC (permalink / raw)


Warner Losh scripsit:

> Also, the fortran compiler would emit entry_ to as to not conflict
> either.  Made calling C from Fortran, and vice versa, a lot of fun...

Unix f77 emitted _foo_, that is, what C saw as foo_.  In effect you
could call arbitrary Fortran routines from C, but not really vice versa:
in order to do so, the C functions had to have names ending in _ and use
the f77 type system.

-- 
A rabbi whose congregation doesn't want         John Cowan
to drive him out of town isn't a rabbi,         http://www.ccil.org/~cowan
and a rabbi who lets them do it                 cowan at ccil.org
isn't a man.    --Jewish saying



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-21  2:50     ` Warner Losh
  2012-02-21  3:33       ` John Cowan
@ 2012-02-21  3:41       ` Steve Nickolas
  2012-02-21 18:18       ` ron
  2 siblings, 0 replies; 17+ messages in thread
From: Steve Nickolas @ 2012-02-21  3:41 UTC (permalink / raw)


On Mon, 20 Feb 2012, Warner Losh wrote:

> And this convention went away with ELF binaries.  No more _foo for 
> function foo.
>
> Also, the fortran compiler would emit entry_ to as to not conflict 
> either.  Made calling C from Fortran, and vice versa, a lot of fun...
>
> Warner

Watcom C did the postfix style, dunno if it still does.

-uso.



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-21  2:50     ` Warner Losh
  2012-02-21  3:33       ` John Cowan
  2012-02-21  3:41       ` Steve Nickolas
@ 2012-02-21 18:18       ` ron
  2 siblings, 0 replies; 17+ messages in thread
From: ron @ 2012-02-21 18:18 UTC (permalink / raw)


An HTML attachment was scrubbed...
URL: <http://minnie.tuhs.org/pipermail/tuhs/attachments/20120221/3310edb0/attachment.html>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-26 13:28           ` Ronald Natalie
@ 2012-02-27  1:30             ` random832
  0 siblings, 0 replies; 17+ messages in thread
From: random832 @ 2012-02-27  1:30 UTC (permalink / raw)




On Sun, Feb 26, 2012, at 08:28, Ronald Natalie wrote:
> 
> WinMainCRTStartup isn't the replacement for main.

I never said it was. My point was that there is no "main" anywhere in a
program that starts with WinMain, whereas John Cowan claimed that a
"main" exists in such programs which does the things that
WinMainCRTStartup in fact does.

> main - for non-MFC console apps
> wmain - same thing but with wchar_t arguments (SOMETHING C/C++ standards
> hasn't ever addressed to my satisfaction).

I suspect this is partly because the unix world is a multibyte world,
and POSIX has "filenames don't have to be valid in any character set,
they're just bytes".

But Windows never had a satisfactory solution to multibyte vs wide in
pipes, either, anyway.

> WinMain - MFC main function

This existed long before MFC, I suspect.

> wWinMain - Ditto, with wchar_t
> 
> Actually the bulk of the CRT involves converting between the command line
> argument as a string and argc/argv (something UNIX does by the OS), and
> some gook necessary to support C++.

Well, not "by the OS" per se. More like, the OS only supports passing
argc/argv at any level, so the shell converts user-typed strings to
argv.

(A big gripe I have with the CRT is that its _spawn/_exec functions
don't quote strings containing spaces/etc so they can be round-tripped
by the CRT's argv initialization. And the argv code itself doesn't let
you have "wildcards in strings without quotes, literal stars in strings
with them", you have to choose between wildcards or not)

> The  fake UNIX environment (POSIX) (read/write/seek, etc...) actually is
> NOT initialized here, but when it is actually referenced.

mainCRTstartup does in fact call _ioinit() in the version I looked at. I
don't recall if WinMainCRTstartup does or not, it's not in front of me
now.
-- 
Random832




^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-25 22:15         ` Random832
@ 2012-02-26 13:28           ` Ronald Natalie
  2012-02-27  1:30             ` random832
  0 siblings, 1 reply; 17+ messages in thread
From: Ronald Natalie @ 2012-02-26 13:28 UTC (permalink / raw)



On Feb 25, 2012, at 5:15 PM, Random832 wrote:
>> 
> 
> This is not true in my experience. If it was ever true, it's not true
> today (with MSVC, anyway. GCC may be different, but if there is a
> 'system-provided main()' it's GCC, or cygwin or mingw, and not anything
> from microsoft, that is providing it). The procedure "provided by
> windows" (provided by MSVC, actually) that does that is in fact called
> WinMainCRTStartup.

WinMainCRTStartup isn't the replacement for main.   It's the replacement for begin or location zero back in the old days.  (Anybody remember seeing p&P6 printed by errant programs?).   There are different versions of that CRT startup (actually all compiled from the same module with ifdefs) that start:

main - for non-MFC console apps
wmain - same thing but with wchar_t arguments (SOMETHING C/C++ standards hasn't ever addressed to my satisfaction).
WinMain - MFC main function
wWinMain - Ditto, with wchar_t

Actually the bulk of the CRT involves converting between the command line argument as a string and argc/argv (something UNIX does by the OS), and some gook necessary to support C++.

The  fake UNIX environment (POSIX) (read/write/seek, etc...) actually is NOT initialized here, but when it is actually referenced.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://minnie.tuhs.org/pipermail/tuhs/attachments/20120226/8dc5369c/attachment.html>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-25 20:39       ` John Cowan
@ 2012-02-25 22:15         ` Random832
  2012-02-26 13:28           ` Ronald Natalie
  0 siblings, 1 reply; 17+ messages in thread
From: Random832 @ 2012-02-25 22:15 UTC (permalink / raw)


I'm going to need to train myself to use the "reply to list" button. 
Though in this case I think part of the problem was duplicate-filtering 
which lost the copy that had the appropriate header.

On 2/25/2012 3:39 PM, John Cowan wrote:
>  Random832 scripsit:
>
>>  For instance, this is, according to Raymond Chen, why they added
>>  WinMain rather than extending main (they didn't know if extensions to
>>  main would be allowed).
>  That doesn't sound very reasonable to me.  When you link a Windows
>  program, it still has a main() procedure provided by Windows which does
>  setup and then invokes WinMain().

This is not true in my experience. If it was ever true, it's not true
today (with MSVC, anyway. GCC may be different, but if there is a
'system-provided main()' it's GCC, or cygwin or mingw, and not anything
from microsoft, that is providing it). The procedure "provided by
windows" (provided by MSVC, actually) that does that is in fact called
WinMainCRTStartup. When you link a windows _console_ program, a
different function (called mainCRTStartup) which does the same setup
(plus opening a console if none is open and setting up stdio which the
WinMain version doesn't) and then invokes main().

The startup function is an interesting read - it has more work to do on
windows than the unix equivalent, because systems like signals (which
are in ANSI), file descriptor based I/O (which is not ANSI but
nevertheless is implemented in MSVC), and such, that are "naturally
occuring" on unix have to be set up. The whole program executes in a
__try/__except block to allow them to make signals work, for example.




^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-25 18:45     ` Random832
  2012-02-25 19:24       ` Steve Nickolas
@ 2012-02-25 20:39       ` John Cowan
  2012-02-25 22:15         ` Random832
  1 sibling, 1 reply; 17+ messages in thread
From: John Cowan @ 2012-02-25 20:39 UTC (permalink / raw)


Random832 scripsit:

> For instance, this is, according to Raymond Chen, why they added      
> WinMain rather than extending main (they didn't know if extensions to 
> main would be allowed).                                               

That doesn't sound very reasonable to me.  When you link a Windows
program, it still has a main() procedure provided by Windows which does
setup and then invokes WinMain().

> so it's possible that MSVC's C library was indeed, to some small
> degree, based on Unix.

Without doubt.  After all, there was no other source of C libraries
before ANSI; compare the Whitesmiths library, which was "meticulously
incompatibled".

-- 
Samuel Johnson on playing the violin:           John Cowan
"Difficult do you call it, Sir?                 cowan at ccil.org
 I wish it were impossible."                    http://www.ccil.org/~cowan



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-25 18:45     ` Random832
@ 2012-02-25 19:24       ` Steve Nickolas
  2012-02-25 20:39       ` John Cowan
  1 sibling, 0 replies; 17+ messages in thread
From: Steve Nickolas @ 2012-02-25 19:24 UTC (permalink / raw)


On Sat, 25 Feb 2012, Random832 wrote:

> On 2/22/2012 11:30 PM, Greg 'groggy' Lehey wrote:
>> On Thursday, 23 February 2012 at  8:22:17 +1100, Dave Horsfall wrote:
>>> On Wed, 22 Feb 2012, arnold at skeeve.com wrote:
>>>> It also shows the consequences a small, apparently local decision
>>>> can have: here we are 40+ years later and GCC on Windows is still
>>>> preprending underscores to function names!
>>> When it comes to Windoze, nothing surprises me any more.  Unix has
>>> evolved over the years, but Windoze was spat out and hatched.
>> I'm no friend of Microsoft either, but gcc isn't exactly Microsoft.
>> What Arnold mentions here is Unix history in action.
>> 
> Yes and no. IIRC, gcc doesn't do that on, for example, Linux ELF. it's done 
> on windows, I assume, in deference to the windows 32-bit ABI for cdecl 
> calling convention functions.
>
> Now, as far as where windows gets that from (just because something evolved 
> within one company doesn't mean it didn't evolve), supposedly early versions 
> of Microsoft C (pre-ANSI) were very conservative in terms of adhering to the 
> "standard" set by Unix C and K&R - this could have extended to the prepending 
> of underscores. For instance, this is, according to Raymond Chen, why they 
> added WinMain rather than extending main (they didn't know if extensions to 
> main would be allowed). I would also guess it's why MSVC stdio is implemented 
> on top of an imitation of Unix system calls which is in turn implemented on 
> top of DOS/windows; and why MSVC time_t is defined as seconds since 1970. 
> There are comments in the code referring to XENIX in various places relating 
> to I/O and timestamps, so it's possible that MSVC's C library was indeed, to 
> some small degree, based on Unix.

Though DOS's file i/o calls, starting in 2.0, were much the same as 
Unix's already.  Its open, close, read, write have the same basic 
semantics and at one point it was possible to have DOS require a syntax 
like \dev\con for devices.

-uso.



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-23  4:30   ` Greg 'groggy' Lehey
@ 2012-02-25 18:45     ` Random832
  2012-02-25 19:24       ` Steve Nickolas
  2012-02-25 20:39       ` John Cowan
  0 siblings, 2 replies; 17+ messages in thread
From: Random832 @ 2012-02-25 18:45 UTC (permalink / raw)


On 2/22/2012 11:30 PM, Greg 'groggy' Lehey wrote:
> On Thursday, 23 February 2012 at  8:22:17 +1100, Dave Horsfall wrote:
>> On Wed, 22 Feb 2012, arnold at skeeve.com wrote:
>>> It also shows the consequences a small, apparently local decision
>>> can have: here we are 40+ years later and GCC on Windows is still
>>> preprending underscores to function names!
>> When it comes to Windoze, nothing surprises me any more.  Unix has
>> evolved over the years, but Windoze was spat out and hatched.
> I'm no friend of Microsoft either, but gcc isn't exactly Microsoft.
> What Arnold mentions here is Unix history in action.
>
Yes and no. IIRC, gcc doesn't do that on, for example, Linux ELF. it's 
done on windows, I assume, in deference to the windows 32-bit ABI for 
cdecl calling convention functions.

Now, as far as where windows gets that from (just because something 
evolved within one company doesn't mean it didn't evolve), supposedly 
early versions of Microsoft C (pre-ANSI) were very conservative in terms 
of adhering to the "standard" set by Unix C and K&R - this could have 
extended to the prepending of underscores. For instance, this is, 
according to Raymond Chen, why they added WinMain rather than extending 
main (they didn't know if extensions to main would be allowed). I would 
also guess it's why MSVC stdio is implemented on top of an imitation of 
Unix system calls which is in turn implemented on top of DOS/windows; 
and why MSVC time_t is defined as seconds since 1970. There are comments 
in the code referring to XENIX in various places relating to I/O and 
timestamps, so it's possible that MSVC's C library was indeed, to some 
small degree, based on Unix.



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-22 21:22 ` Dave Horsfall
  2012-02-22 22:47   ` A. P. Garcia
@ 2012-02-23  4:30   ` Greg 'groggy' Lehey
  2012-02-25 18:45     ` Random832
  1 sibling, 1 reply; 17+ messages in thread
From: Greg 'groggy' Lehey @ 2012-02-23  4:30 UTC (permalink / raw)


On Thursday, 23 February 2012 at  8:22:17 +1100, Dave Horsfall wrote:
> On Wed, 22 Feb 2012, arnold at skeeve.com wrote:
>> It also shows the consequences a small, apparently local decision
>> can have: here we are 40+ years later and GCC on Windows is still
>> preprending underscores to function names!
>
> When it comes to Windoze, nothing surprises me any more.  Unix has
> evolved over the years, but Windoze was spat out and hatched.

I'm no friend of Microsoft either, but gcc isn't exactly Microsoft.
What Arnold mentions here is Unix history in action.

Greg
--
Sent from my desktop computer
Finger grog at FreeBSD.org for PGP public key.
See complete headers for address and phone numbers.
This message is digitally signed.  If your Microsoft MUA reports
problems, please read http://tinyurl.com/broken-mua
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://minnie.tuhs.org/pipermail/tuhs/attachments/20120223/bb41d2c1/attachment.sig>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-22 21:22 ` Dave Horsfall
@ 2012-02-22 22:47   ` A. P. Garcia
  2012-02-23  4:30   ` Greg 'groggy' Lehey
  1 sibling, 0 replies; 17+ messages in thread
From: A. P. Garcia @ 2012-02-22 22:47 UTC (permalink / raw)


On Wed, Feb 22, 2012 at 3:22 PM, Dave Horsfall <dave at horsfall.org> wrote:

> On Wed, 22 Feb 2012, arnold at skeeve.com wrote:
>
> > This is interesting. It shows that (apparently) early on, assembler was
> > viewed as the primary programming language.
>
> Well, C didn't exactly spring from Zeus's brow :-)  A good chunk of the
> C library was in assembler, as were quite a number of programs.
>

relevant:
http://cm.bell-labs.com/who/dmr/hist.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://minnie.tuhs.org/pipermail/tuhs/attachments/20120222/fc7b9729/attachment.html>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
  2012-02-22 19:17 arnold
@ 2012-02-22 21:22 ` Dave Horsfall
  2012-02-22 22:47   ` A. P. Garcia
  2012-02-23  4:30   ` Greg 'groggy' Lehey
  0 siblings, 2 replies; 17+ messages in thread
From: Dave Horsfall @ 2012-02-22 21:22 UTC (permalink / raw)


On Wed, 22 Feb 2012, arnold at skeeve.com wrote:

> This is interesting. It shows that (apparently) early on, assembler was
> viewed as the primary programming language.

Well, C didn't exactly spring from Zeus's brow :-)  A good chunk of the
C library was in assembler, as were quite a number of programs.

> It also shows the consequences a small, apparently local decision can have:
> here we are 40+ years later and GCC on Windows is still preprending
> underscores to function names!

When it comes to Windoze, nothing surprises me any more.  Unix has evolved
over the years, but Windoze was spat out and hatched.

-- Dave



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [TUHS] why the leading under score added to function names?
@ 2012-02-22 19:17 arnold
  2012-02-22 21:22 ` Dave Horsfall
  0 siblings, 1 reply; 17+ messages in thread
From: arnold @ 2012-02-22 19:17 UTC (permalink / raw)


Hi All.

This is interesting. It shows that (apparently) early on, assembler was
viewed as the primary programming language.

It also shows the consequences a small, apparently local decision can have:
here we are 40+ years later and GCC on Windows is still preprending
underscores to function names!

In 15 minutes I helped the guy at work solve a problem he'd been working
on for two days!

Thanks everyone,

Arnold

> From: Brantley Coile <brantley at coraid.com>
> To: Dave Horsfall <dave at horsfall.org>
> Date: Mon, 20 Feb 2012 18:34:26 -0600
> Cc: The Eunuchs Hysterical Society <tuhs at tuhs.org>
> Subject: Re: [TUHS] why the leading under score added to function names?
>
> correct.  we could link to assembler code with _entry points and not
i> worry about symbol collisions in the rest of the code. 
>
> iPhone email
>
> On Feb 20, 2012, at 6:23 PM, "Dave Horsfall" <dave at horsfall.org> wrote:
>
> > On Mon, 20 Feb 2012, arnold at skeeve.com wrote:
> > 
> > [...]
> > 
> >> I'm pretty sure this dates back to PDP-11 days.  I'm wondering "why?".
> >> Why did the C compiler prepend an underscore to function names?
> > 
> > Sure was the PDP-11 :-)  I vaguely recall that it was to make sure that
> > user functions did not conflict with predefined assembler functions, as
> > that would be a pain to diagnose (much like having swap overlap root).
> > 
> > -- Dave



^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2012-02-27  1:30 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-20 20:52 [TUHS] why the leading under score added to function names? arnold
2012-02-20 23:21 ` Dave Horsfall
2012-02-21  0:34   ` Brantley Coile
2012-02-21  2:50     ` Warner Losh
2012-02-21  3:33       ` John Cowan
2012-02-21  3:41       ` Steve Nickolas
2012-02-21 18:18       ` ron
2012-02-22 19:17 arnold
2012-02-22 21:22 ` Dave Horsfall
2012-02-22 22:47   ` A. P. Garcia
2012-02-23  4:30   ` Greg 'groggy' Lehey
2012-02-25 18:45     ` Random832
2012-02-25 19:24       ` Steve Nickolas
2012-02-25 20:39       ` John Cowan
2012-02-25 22:15         ` Random832
2012-02-26 13:28           ` Ronald Natalie
2012-02-27  1:30             ` random832

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).