开发者

Libraries that are linked by default

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开发者_StackOverflow社区 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!


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


  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.


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


linux-gate is a virtual library that provides access to system functions. Its full name is the Linux Virtual Dynamic Shared Object. It's used by libc.

libc is the C run time. It's what actually calls main() for you. (It's possible to bypass this if you don't use any C functions.)

ld-linux is the dynamic linker, which actually knows how to load and call the C run time for you.

main lives in test.o, not in a library.

return is a keyword, not a function. It directs the compiler to emit code to cause the function to return control to its caller.


ld-linux.so provides the magic that helps ldd work. libc.so is part of the C runtime library. Among other things, the runtime library contains the actual entry point that calls main.

main is provided by your code. return is not a function, it's a keyword in the C language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜