Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site ucbvax.BERKELEY.EDU Path: utzoo!decvax!ittatc!dcdwest!sdcsvax!ucbvax!edison.UUCP!jso From: jso@edison.UUCP (John Owens) Newsgroups: mod.computers.vax Subject: Re: main() and entry points in C Message-ID: <8606231334.AA04569@edison.UUCP> Date: Mon, 23-Jun-86 09:34:42 EDT Article-I.D.: edison.8606231334.AA04569 Posted: Mon Jun 23 09:34:42 1986 Date-Received: Tue, 24-Jun-86 17:58:58 EDT References: <8606180820.AA08276@ucbvax.Berkeley.EDU> Sender: daemon@ucbvax.BERKELEY.EDU Organization: The ARPA Internet Lines: 82 Approved: info-vax@sri-kl.arpa -- So I pulled out my 4.2bsd documentation and checked out ld. It says this: -- -- [In the command description] The entry point of the output is the -- beginning of the first routine (unless the -e option is specified). -- -- [The description of -e] The following argument is taken to be the -- name of the entry point of the loaded program; location 0 is the -- default. -- -- Note that these two statements are not obviously consistent [....] -- the "first routine" would have to be at 0, hence its address would be -- at 0 - but that's NULL!) Nor do they account for main(). I see no -- reference at all to _start(). Your 4.2bsd documentation set (if you got it from Berkeley) refers to VAXen only, where the first routine *is* at location 0; the loader just loads sequentially. (C does *not* guarantee that address 0 doesn't contain anything, but that's another discussion.) The loader itself knows nothing about main or start; those are features of C, and have nothing to do with any other language that ld might load. You just might be writing in assembler.... -- Since you mentioned System V, I checked some AT&T 3B2 ld documentation I -- have here. The only references to the entry point are as follows: -- [....] -- Again, no reference to _start() - or to what the entry point would be if -- -e were left out. The documentation of the cc command doesn't say either. -- But note that your "simple" example, and hardwired entry point, are -- apparently NOT ld's doing, but cc's! The documentation seems to be lacking here. (I've never been very fond of ATTIS's rewritten documentation.) The definition of the C language really does require that main be the starting point; I suppose that didn't need to be part of the man page. start is not a user-visible feature, and certainly doesn't have to have that name. The hardwired entry point is a ld feature; the reference to main a feature of C. Nonetheless, the entry point will still be the first routine. Read on.... -- I tried your little foo program out on our local Celerity (4.2bsd). -- "cc foo" produces "Undefined: _main", and running the resulting a.out -- produces an immediate "Invalid address". However, a foo.o gets left -- around. So I did an "ld foo.o". This led to "Undefined: _printf". When cc invokes ld, it looks something like this: /bin/ld /lib/crt0.o foo.o -lc [-X flags and such left off] The crt0 file is loaded at address 0, and refers to main and exit. foo.o must satisfy main, and /lib/libc.a will satisfy printf and exit. -- Well, getting there. I tried -- "ld foo.o /lib/libc.a". No errors! Running a.out produces "Hello world", -- followed by an access violation. Adding an explict exit(0) fixes that. This is certainly not supported. You were lucky. It's dependent on the implementation of the exec(2) system call whether or not you'll get your command line arguments this way. -- [...] but are you -- still going to bet that the first routine WON'T end up as the entry point? I won't bet on anything if the loader isn't invoked properly.... -- At worst, I was claiming that a lot of non-portable C code got written -- under Unix (since K&R certainly contains nothing to indicate that -- there can be an entry point other than main()). And if you don't -- believe THAT, then you haven't looked at much Unix code. That code you've been looking at is going to have a hard time being ported to most UNIX systems then, much less any other system with a C compiler. I've been porting, adapting, and randomly mangling C code for UNIX from a variety of sources for years, and haven't run into a single program that doesn't have an entry point of main(). Would you refer me to such a program that I might have access to, like something from USENET, a USENIX tape, or a System V or BSD distribution? -- -- Jerry John Owens @ General Electric Company edison!jso%virginia@CSNet-Relay.ARPA [old arpa] edison!jso@virginia.EDU [w/ nameservers] jso@edison.UUCP [w/ uucp domains] {cbosgd allegra ncsu xanth}!uvacs!edison!jso [roll your own]