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

A

Amandil

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
 
K

Keith Thompson

Amandil said:
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.
 
R

Rich Webb

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.
 
B

BartC

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).
 
A

Amandil

Amandil said:
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.

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
 
A

Antoninus Twink

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).
 
J

jacob navia

Amandil said:
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.
 
B

BartC

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.
 
T

Tony

Amandil said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top