LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-01-2011, 10:10 AM   #1
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Rep: Reputation: 5
Question How a C program starts in linux?


The question is very simple:

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()
 
Old 03-01-2011, 10:45 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I think you're asking about the executable format and how executables are loaded:

http://en.wikipedia.org/wiki/Executa...inkable_Format
http://www.linuxjournal.com/article/1059
http://wiki.osdev.org/ELF#Loading_ELF_Binaries

Last edited by dugan; 03-01-2011 at 10:54 AM.
 
Old 03-01-2011, 11:28 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 03-01-2011, 11:46 AM   #4
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by dugan View Post
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 View Post
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?
 
Old 03-01-2011, 01:43 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
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.

--- rod.
 
Old 03-01-2011, 01:51 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by sree_ec View Post
After allocating space for these sections inside memory, how the OS launches main()? can you explain this part?
It puts the address of the first instruction in the program counter.
 
Old 03-01-2011, 02:25 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by sree_ec View Post
After allocating space for these sections inside memory, how the OS launches main()? can you explain this part?
The OS doesn't launch main(). It is two steps removed from that.

I have an example handy from a recent thread on a related subject.
http://www.linuxquestions.org/questi...8/#post4272950

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

Last edited by johnsfine; 03-01-2011 at 02:28 PM.
 
1 members found this post helpful.
Old 03-07-2011, 06:03 AM   #8
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by johnsfine View Post
The OS doesn't launch main(). It is two steps removed from that.

I have an example handy from a recent thread on a related subject.
http://www.linuxquestions.org/questi...8/#post4272950

(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?
 
Old 03-07-2011, 07:16 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by sree_ec View Post
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.
 
1 members found this post helpful.
Old 03-07-2011, 07:31 AM   #10
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Sorry for confusing you.
Even though you did not understand the question, I think I got my answer right here
Quote:
That is all well before the kernel gives control to ld-linux in this process.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting a program whenever X starts cwwilson721 Slackware 7 04-02-2006 10:52 PM
AutoLoading at program after Blackbox starts? jensca Linux - Newbie 1 05-26-2005 06:21 PM
Program starts on wrong desktop johnmart Linux - Newbie 2 12-07-2004 10:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration