Who calls main() (OT, OS/C specific)

Discussion in 'C Programming' started by Amandil, Jun 17, 2009.

  1. Amandil

    Amandil Guest

    I was wondering if anyone out there can point me in the right
    direction, and tell me which newsgroup I can go to where my question
    can be answered. I do realize that my question may be off-topic, and
    is definitely operating system and compiler specific. So, here goes.

    I wanted to know, who calls main(). Many C books have the line, "In
    effect, the operating system calls main()." Actually, main() is called
    by some function (probably) within the C library (not programmmer-
    accessible), which is responsible for setting up the environment in
    which the program executes, like allocating FILE objects for stdin/
    stdout and setting up argc and argv (and envp - IS). This function
    also performs clean up afterwards, like calling exit() with the return
    value of main(). Obviously, this needn't be a function, and the
    compiler can just place code that does this at the beginning and end
    of main(). (I would see this as an inline function.)

    Obviously, there is no one answer - it's very implementation specific
    - and within standard C, there is no need to know, and it wouldn't
    help me if I did. On the other hand, someone writing a compiler does
    need to know that information, and in practice write the code as well.
    I am looking for specific code examples, for different OS's, so I can
    compare and perhaps write my own.

    Does anyone have an address for me? Thanks, and I'll appreciate not
    being flamed.

    -- Marty Wolfe
     
    Amandil, Jun 17, 2009
    #1
    1. Advertising

  2. Amandil <> writes:
    > I was wondering if anyone out there can point me in the right
    > direction, and tell me which newsgroup I can go to where my question
    > can be answered. I do realize that my question may be off-topic, and
    > is definitely operating system and compiler specific. So, here goes.
    >
    > I wanted to know, who calls main().

    [snip]
    >
    > Obviously, there is no one answer - it's very implementation specific
    > - and within standard C, there is no need to know, and it wouldn't
    > help me if I did. On the other hand, someone writing a compiler does
    > need to know that information, and in practice write the code as well.
    > I am looking for specific code examples, for different OS's, so I can
    > compare and perhaps write my own.
    >
    > Does anyone have an address for me? Thanks, and I'll appreciate not
    > being flamed.


    As you say, the answer is system-specific. More precisely, the
    *answers* are system-specific, and are likely to be very different for
    different systems. Your best bet is to ask in the newsgroup(s) for
    whatever system(s) you're interested in. comp.unix.programmer would
    be a good start for Unix-like systems.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 17, 2009
    #2
    1. Advertising

  3. Amandil

    Rich Webb Guest

    On Wed, 17 Jun 2009 12:28:35 -0700 (PDT), Amandil <>
    wrote:

    >I wanted to know, who calls main().


    Typically, it's an object module that began as assembler code. Often the
    source will be named "crt[stuff].[assembler suffix]." The "crt" part is
    "C run-time"; the optional [stuff] can be identifiers for, depending on
    the environment, things like memory models, boot-block flags, etc.

    >Obviously, there is no one answer - it's very implementation specific
    >- and within standard C, there is no need to know, and it wouldn't
    >help me if I did.


    It may, if you ever wander into the realm of freestanding
    implementations like the embedded world and need to muck about with the
    startup code.

    --
    Rich Webb Norfolk, VA
     
    Rich Webb, Jun 17, 2009
    #3
  4. Amandil

    BartC Guest

    "Amandil" <> wrote in message
    news:...

    > I wanted to know, who calls main(). Many C books have the line, "In
    > effect, the operating system calls main()." Actually, main() is called
    > by some function (probably) within the C library (not programmmer-
    > accessible), which is responsible for setting up the environment in
    > which the program executes, like allocating FILE objects for stdin/
    > stdout and setting up argc and argv (and envp - IS). This function
    > also performs clean up afterwards, like calling exit() with the return
    > value of main(). Obviously, this needn't be a function, and the
    > compiler can just place code that does this at the beginning and end
    > of main(). (I would see this as an inline function.)
    >
    > Obviously, there is no one answer - it's very implementation specific
    > - and within standard C, there is no need to know, and it wouldn't
    > help me if I did. On the other hand, someone writing a compiler does
    > need to know that information, and in practice write the code as well.
    > I am looking for specific code examples, for different OS's, so I can
    > compare and perhaps write my own.
    >
    > Does anyone have an address for me? Thanks, and I'll appreciate not
    > being flamed.


    If you examine an executable file, there should be an entry point address
    defined. That will point to the first code to be executed (and a disassembly
    will show you the code itself).

    That will be set (if you aren't creating the executable yourself) by some
    assembler/linker options (I would imagine, as I haven't done this for a
    while).

    --
    Bartc
     
    BartC, Jun 17, 2009
    #4
  5. Amandil

    Amandil Guest

    On Jun 17, 3:38 pm, Keith Thompson <> wrote:
    > Amandil <> writes:
    > > I was wondering if anyone out there can point me in the right
    > > direction, and tell me which newsgroup I can go to where my question
    > > can be answered. I do realize that my question may be off-topic, and
    > > is definitely operating system and compiler specific. So, here goes.

    >
    > > I wanted to know, who calls main().

    > [snip]
    >
    > > Obviously, there is no one answer - it's very implementation specific
    > > - and within standard C, there is no need to know, and it wouldn't
    > > help me if I did. On the other hand, someone writing a compiler does
    > > need to know that information, and in practice write the code as well.
    > > I am looking for specific code examples, for different OS's, so I can
    > > compare and perhaps write my own.

    >
    > > Does anyone have an address for me? Thanks, and I'll appreciate not
    > > being flamed.

    >
    > As you say, the answer is system-specific.  More precisely, the
    > *answers* are system-specific, and are likely to be very different for
    > different systems.  Your best bet is to ask in the newsgroup(s) for
    > whatever system(s) you're interested in.  comp.unix.programmer would
    > be a good start for Unix-like systems.
    >
    > --
    > Keith Thompson (The_Other_Keith)


    Actually, I figured the answers (plural, as you said) would actually
    be more compiler-specific than OS specific. I could try c.u.p, but
    actually I'm looking more toward a Windows solution. The idea behind
    my question - however undesirable one may consider it - is to write a
    FASM style assembler, allowing for .INCLUDE's that can help construct
    EXE/OBJ/ELF/etc. headers.
    Regarding other answers I've received, I know part of the answer. I'm
    looking for the source code to the crt_startup() or whatever, or a
    newsgroup with members that can point me to the SC. I suppose Jacob
    Navia would have an answer that I would love, but I don't seem to have
    seen any signs of him in recent months.

    -- Marty Wolfe
     
    Amandil, Jun 17, 2009
    #5
  6. On 17 Jun 2009 at 19:28, Amandil wrote:
    > I do realize that my question may be off-topic, and is definitely
    > operating system and compiler specific. So, here goes.
    >
    > I wanted to know, who calls main().


    This is not off-topic by any stretch of the imagination! If executing C
    programs isn't "topical" in a C newsgroup, then what is?

    As you guessed, this depends on the OS. For example, in a typical Linux
    environment, once exec*() is called the kernel calls
    search_binary_handler() to discover the file format of the executable.
    On a typical modern system, this will be an ELF binary, so control
    passes to the load_elf_binary() handler.

    This reads the executable and sets up up various kernel data structures
    for the new process, and arranges the memory segments - text, data,
    bss, stack. It allocates pages for the command line arguments and
    environment variables, and pushes the arguments to main() onto the
    stack. Then start_thread() begins the execution of the process by
    jumping to the address of the symbol _start inside the executable.

    _start uses the information from the stack the kernel has supplied to
    set up an argument stack for __libc_start_main, which is a function in
    libc.so.

    Then __libc_start_main does all the necessary initialization for the C
    library, sets up the thread enviornment, and finally calls main(argv,
    argc).
     
    Antoninus Twink, Jun 17, 2009
    #6
  7. Amandil

    jacob navia Guest

    Amandil wrote:
    > I was wondering if anyone out there can point me in the right
    > direction, and tell me which newsgroup I can go to where my question
    > can be answered. I do realize that my question may be off-topic, and
    > is definitely operating system and compiler specific. So, here goes.
    >
    > I wanted to know, who calls main(). Many C books have the line, "In
    > effect, the operating system calls main()." Actually, main() is called
    > by some function (probably) within the C library (not programmmer-
    > accessible), which is responsible for setting up the environment in
    > which the program executes, like allocating FILE objects for stdin/
    > stdout and setting up argc and argv (and envp - IS). This function
    > also performs clean up afterwards, like calling exit() with the return
    > value of main(). Obviously, this needn't be a function, and the
    > compiler can just place code that does this at the beginning and end
    > of main(). (I would see this as an inline function.)
    >
    > Obviously, there is no one answer - it's very implementation specific
    > - and within standard C, there is no need to know, and it wouldn't
    > help me if I did. On the other hand, someone writing a compiler does
    > need to know that information, and in practice write the code as well.
    > I am looking for specific code examples, for different OS's, so I can
    > compare and perhaps write my own.
    >
    > Does anyone have an address for me? Thanks, and I'll appreciate not
    > being flamed.
    >
    > -- Marty Wolfe


    When the linker builds an executable, it writes somewhere the
    "entry point" of the program.

    This indicates the OS where in the executable parts of the file, is the
    first instruction

    When the OS loads the executable, either because you type its name in a
    shell or double clicked in its icon, the OS loads the executable in
    memory, creates a process for it, loads the associated dlls (or shared
    objects) and sets the instruction pointer to that first instruction
    and the program starts.

    That first instructions normally setup the environment of the program,
    and initialize the run time library. Then, they call the "main"
    function.

    When main returns, the "atexit" functions are called, and the program
    finishes when the exit function is called.

    This code is called "crt startup code" and each compiler has one.

    If the program is designed for a circuit where there isno OS, it
    will be located at a specific address. When the CPU starts, it will
    look at some address, and it will find there either the program in eprom
    or a jump to the first instruction of the program. It is up to the
    program to establish the environment.


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Jun 17, 2009
    #7
  8. Amandil

    BartC Guest

    Amandil wrote:
    > On Jun 17, 3:38 pm, Keith Thompson <> wrote:
    >> Amandil <> writes:
    >>> I was wondering if anyone out there can point me in the right
    >>> direction, and tell me which newsgroup I can go to where my question
    >>> can be answered. I do realize that my question may be off-topic, and
    >>> is definitely operating system and compiler specific. So, here goes.

    >>
    >>> I wanted to know, who calls main().


    >> As you say, the answer is system-specific. More precisely, the


    > actually I'm looking more toward a Windows solution. The idea behind
    > my question - however undesirable one may consider it - is to write a
    > FASM style assembler, allowing for .INCLUDE's that can help construct
    > EXE/OBJ/ELF/etc. headers.
    > Regarding other answers I've received, I know part of the answer. I'm
    > looking for the source code to the crt_startup() or whatever, or a


    Why do you need the CRT startup routine for an assembler?

    > I suppose Jacob
    > Navia would have an answer that I would love, but I don't seem to have
    > seen any signs of him in recent months.


    I think the source code for lcc-win32 used to be available for a small fee.

    --
    Bart
     
    BartC, Jun 17, 2009
    #8
  9. Amandil

    Tony Guest

    Amandil wrote:

    > I was wondering if anyone out there can point me in the right
    > direction, and tell me which newsgroup I can go to where my question
    > can be answered. I do realize that my question may be off-topic, and
    > is definitely operating system and compiler specific. So, here goes.
    >
    > I wanted to know, who calls main().


    On a hosted implementation, the "C Run Time library" (CRT) does, which
    includes a bit/bunch of platform-specific stuff. Who starts the CRT? The
    linker defines that as the "entry point". There is actually more than one
    entry point that can be chosen by the linker depending on whether you are
    building an executable, a console program or a DLL (on Windows). The MS
    linker has available the '/ENTRY:' switch if you want to avoid using the CRT
    as the entry point but you'll have some work to do because some of the
    platform initializations must be done. I imagine that if you replace all
    that stuff, you are pretty close to a "freestanding" implementation.

    > Many C books have the line, "In
    > effect, the operating system calls main()." Actually, main() is called
    > by some function (probably) within the C library (not programmmer-
    > accessible),


    The C Run Time Library, which is platform-specific and may be accessible in
    source code form depending on the platform. Surely what happens before the
    jump to the CRT is even more implementation-dependent: things like, loading
    the program from a file, setting up the segments, creating an OS process,
    etc.

    > which is responsible for setting up the environment in
    > which the program executes, like allocating FILE objects for stdin/
    > stdout and setting up argc and argv (and envp - IS). This function
    > also performs clean up afterwards, like calling exit() with the return
    > value of main(). Obviously, this needn't be a function, and the
    > compiler can just place code that does this at the beginning and end
    > of main(). (I would see this as an inline function.)
    >
    > Obviously, there is no one answer - it's very implementation specific
    > - and within standard C, there is no need to know, and it wouldn't
    > help me if I did. On the other hand, someone writing a compiler does
    > need to know that information, and in practice write the code as well.
    > I am looking for specific code examples, for different OS's, so I can
    > compare and perhaps write my own.
    >
    > Does anyone have an address for me? Thanks, and I'll appreciate not
    > being flamed.


    I think MingW has all the source code for the startup routines included
    http://www.mingw.org/ to build the startup code and for your perusal.
     
    Tony, Jun 19, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    913
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    793
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Richard Tobin
    Replies:
    24
    Views:
    825
  4. ++imanshu

    #calls to main::BEGIN = 6 with -d:DProf

    ++imanshu, Jan 1, 2010, in forum: Perl Misc
    Replies:
    0
    Views:
    112
    ++imanshu
    Jan 1, 2010
  5. Bob
    Replies:
    5
    Views:
    289
Loading...

Share This Page