Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #108117
| From | antispam@math.uni.wroc.pl |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: main() |
| Date | 2017-04-14 02:51 +0000 |
| Organization | Politechnika Wroclawska |
| Message-ID | <ocpdff$sci$1@z-news.wcss.wroc.pl> (permalink) |
| References | <ecUHA.285656$15.165152@fx07.am4> |
bartc <bc@freeuk.com> wrote:
> I think this is not strictly part of the language but someone here may
> happen to know how it's done.
>
> The arguments to a programs entry point such as main(int n, char** args)
> are not set up by the OS, but by some function the C implementation
> arranges.
>
> On Windows at least, where such a function might use GetCommandLine() or
> __getmainargs() to obtain the command-line arguments and pass them to
> main().
>
> What's the equivalent on Linux? Or will those args just magically be on
> the stack anyway? (Because after all Linux and C are very.. close.)
In Linux program initialization (startup) is shared responsibility
of compiler, kernel, libc and dynamic linker. Linux executable has field
pointing to "ELF interpeter", that is program resposible for
initialization of dynamic linking. libc also may need some initalization.
If you properly plug your object files into the framwork, then
your main will get called with correct arguments. Otherwise you
risk crashes due to wrong initialization. To the point: on
Debian x86_64 libc provides 'crt1.o' which contains startup
routine called '_start'. This routine may look trivial (it is
just doing little register manipulations and then calls 'main').
But you need to do all required woodoo. Case in point: I was
using a Modula 2 compiler and on new version of Linux executables
from this compiler begun to crash. I looked at the problem and
the Modula 2 compiler provided its own startup routine which
turned out to be incompatible with newer version of Linux.
Arguments to main are provided by kernel, I did not check if
they are passed on the stack or in registers (details depend
on architecture). But if you want to use libc you need to
initialize things in way required by libc.
--
Waldek Hebisch
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
main() bartc <bc@freeuk.com> - 2017-04-14 00:53 +0100
Re: main() Robert Wessel <robertwessel2@yahoo.com> - 2017-04-13 19:10 -0500
Re: main() antispam@math.uni.wroc.pl - 2017-04-14 02:51 +0000
Re: main() scott@slp53.sl.home (Scott Lurndal) - 2017-04-14 12:58 +0000
Re: main() bartc <bc@freeuk.com> - 2017-04-14 14:11 +0100
Re: main() Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-04-15 07:53 -0700
Re: main() supercat@casperkitty.com - 2017-04-15 08:43 -0700
Re: main() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-15 16:50 +0100
Re: main() Keith Thompson <kst-u@mib.org> - 2017-04-15 15:31 -0700
Re: main() gordonb.y2etp@burditt.org (Gordon Burditt) - 2017-04-16 00:19 -0500
The exec*() family (Was: main()) gazelle@shell.xmission.com (Kenny McCormack) - 2017-04-15 17:01 +0000
Re: main() Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-04-14 10:04 -0400
Re: main() bartc <bc@freeuk.com> - 2017-04-14 15:52 +0100
Re: main() scott@slp53.sl.home (Scott Lurndal) - 2017-04-14 15:00 +0000
Re: main() Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-04-14 13:49 -0400
csiph-web