Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libraries that are linked by default

Tags:

c

linux

gcc

My code is in test.c:

int main(){
return 0;
}

The dynamically shared libraries the executable compiled from it depends on are:

$ gcc -o test test.c
$ ldd test
    linux-gate.so.1 =>  (0x00783000)
    libc.so.6 => /lib/libc.so.6 (0x00935000)
    /lib/ld-linux.so.2 (0x00ea5000)
  1. I was wondering what roles the three libraries are playing?
  2. Which library does the function main belong to? /lib/libc.so.6?
  3. Which library does return belong to? /lib/libc.so.6?
  4. Are the three libraries all that are dynamically linked by default by gcc?
  5. How can I find out static libraries that gcc links to by default?

Thanks!

like image 633
Tim Avatar asked Aug 06 '11 22:08

Tim


3 Answers

  1. linux-gate.so isn't really a shared lib, but a part of the kernel that acts like one and makes fast system calls possible. ld-linux.so is a piece of code that makes loading other shared libraries possible. libc.so is the C library, containing standard functions like printf and strcpy.
  2. main doesn't belong to any library. It belongs to your program, in the sense that its assembled version is stored entirely in the test binary file.
  3. return is not a function but a C language construct.
  4. No, it also links in libgcc, which is apparently not a shared library on your system (or it would show up) and some startup code. g++ would additionally link in libstdc++.so (the C++ standard library) and libm.so (the math part of the C standard library).
like image 140
Fred Foo Avatar answered Oct 22 '22 02:10

Fred Foo


  1. linux-gate is a virtual shared object that acts as a connection to system calls within the kernel. libc is glibc, which provides functions such as printf() and so on. ld-linux is the glibc loader, which allows loading of other shared objects.

  2. main() belongs to your code. It is called by crt1.o which is linked into the executable by gcc (well, ld specifically).

  3. return is not a function but rather a language construct, so gcc turns it directly into code contained within the object (and eventually executable) file. As an aside, the value returned from main() is caught by crt1.o and turned into a program result code.

like image 40
Ignacio Vazquez-Abrams Avatar answered Oct 22 '22 02:10

Ignacio Vazquez-Abrams


Exelent description about how does linux execute my main()? There you will find the answer and probably a lot more!

like image 2
Fredrik Pihl Avatar answered Oct 22 '22 01:10

Fredrik Pihl