ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How does a C program start in linux? Is main() the first function called in the a c application by kernel. I understand it is the first called function written by the application programmer, but the question is to understand the what all kernel does and what all functions it calls before calling main()
There is something called the C startup code. It is executed before main(), and may perform any number of things, and is compiler and architecture dependent. It may do such things as create argc & argv & initialize global variables.
--- rod.
Thank you for the links.
After allocating space for these sections inside memory, how the OS launches main()? can you explain this part?
---------- Post added 03-01-11 at 11:17 PM ----------
Quote:
Originally Posted by theNbomr
There is something called the C startup code. It is executed before main(), and may perform any number of things, and is compiler and architecture dependent. It may do such things as create argc & argv & initialize global variables.
--- rod.
I really would like to more know about those things... Any good read you can suggest on this?
I really would like to more know about those things... Any good read you can suggest on this?
The source code for your compiler. Often, the startup code is customizable, and is distributed in source form (often in assembler language), especially for cases like embedded systems, where there may be hardware-specific requirements, such as initializing hardware.
The OS does not call main(), the startup code does. It isn't necessarily the case that there even is an OS. Compilers exist for 'bare metal' architectures.
(For the binary in that thread) the beginning of the output from the readelf -l command is:
Code:
readelf -l hw32
Elf file type is EXEC (Executable file)
Entry point 0x80482f0
There are 7 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x000e0 0x000e0 R E 0x4
INTERP 0x000114 0x08048114 0x08048114 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x08048000 0x08048000 0x00588 0x00588 R E 0x1000
From the kernel's point of view, what matters is the line that says: Requesting program interpreter: /lib/ld-linux.so.2
IIUC, that means the kernel actually loads and gives control to that .so file, rather than really starting your executable. That .so file finishes the job of loading your program (finding and loading all other .so files that must be present before your code starts).
Next look at the lines Entry point 0x80482f0
...
LOAD 0x000000 0x08048000 0x08048000 0x00588 0x00588 R E 0x1000
IIUC, once ld-linux.so has finished loading your program it transfers control to that entry point, which is in that one of the sections loaded from the main executable file.
But that still isn't main()
You also should see in the link time details of the post I indicated above, where it says: /usr/lib/gcc/x86_64-redhat-linux/4.1.2/32/crtbegin.o
There is a C startup module included by gcc when it gives the command to the linker. I believe that is the module named crtbegin.o in that example. So the linker makes the entry point of that module be the entry point of the entire binary.
The code in crtbegin.o does various initialization required by the C runtime library. Then it calls main()
So if I'm getting the details right, the OS gives control to ld-linux, which gives control to crtbegin, which calls main().
(For the binary in that thread) the beginning of the output from the readelf -l command is:
Code:
readelf -l hw32
Elf file type is EXEC (Executable file)
Entry point 0x80482f0
There are 7 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x000e0 0x000e0 R E 0x4
INTERP 0x000114 0x08048114 0x08048114 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x08048000 0x08048000 0x00588 0x00588 R E 0x1000
From the kernel's point of view, what matters is the line that says: Requesting program interpreter: /lib/ld-linux.so.2
IIUC, that means the kernel actually loads and gives control to that .so file, rather than really starting your executable. That .so file finishes the job of loading your program (finding and loading all other .so files that must be present before your code starts).
Next look at the lines Entry point 0x80482f0
...
LOAD 0x000000 0x08048000 0x08048000 0x00588 0x00588 R E 0x1000
IIUC, once ld-linux.so has finished loading your program it transfers control to that entry point, which is in that one of the sections loaded from the main executable file.
But that still isn't main()
You also should see in the link time details of the post I indicated above, where it says: /usr/lib/gcc/x86_64-redhat-linux/4.1.2/32/crtbegin.o
There is a C startup module included by gcc when it gives the command to the linker. I believe that is the module named crtbegin.o in that example. So the linker makes the entry point of that module be the entry point of the entire binary.
The code in crtbegin.o does various initialization required by the C runtime library. Then it calls main()
So if I'm getting the details right, the OS gives control to ld-linux, which gives control to crtbegin, which calls main().
Thank you for the detailed answer.
But one more question, from where the new process starts. Is it when ctbegin calls main or from inside ld-linux library ? Who is forking and exec ing?
from where the new process starts. Is it when ctbegin calls main or from inside ld-linux library ?
I'm not sure there is an actual technical question there vs. a point of view on the meaning of "process starts".
There might be some explicit definition of that inside the Linux kernel. I don't know.
Any reasonable definition I can think of for the point at which a process starts would be somewhere before the kernel gives control to ld-linux inside that process.
Quote:
Who is forking and exec ing?
I not sure I understand that question either:
The fork occurs in a different process (that is the parent of this process) and the exec occurs in this process while this process is still executing in the parent's image. That is all well before the kernel gives control to ld-linux in this process.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.