我正在尝试理解C中共享库的以下行为
机器一
$ cat one.c
#include<stdio.h>
int main() {
printf ("%d", 45);
}
$ gcc one.c -o one -O3
$ ldd one
linux-gate.so.1 => (0x00331000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00bc2000)
/lib/ld-linux.so.2 (0x006dc000)
$ cat two.c
int main() {
int i = 0;
}
$ gcc two.c -o two -O3
$ ldd two
linux-gate.so.1 => (0x006f7000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00110000)
/lib/ld-linux.so.2 (0x00eb0000)
$
第二台机器
$ cat three.c
#include<stdio.h>
int main() {
printf ("%d", 45);
}
$ gcc three.c -o three -O3
$ ldd three
/usr/lib/libcwait.so (0xb7ffd000)
libc.so.6 => /lib/tls/i686/nosegneg/libc.so.6 (0x002de000)
/lib/ld-linux.so.2 (0x002bf000)
$
目前有几个我不完全理解的事情:
What does the address given in brackets (for example,
(0x002de000)
) mean?These addresses are different even for the same library on the same machine, which suggests these are addresses of the locations in memory where these libraries are loaded. But, if that is true, why are these libraries loaded in memory at all (I did not execute the programs yet, shouldn't they be loaded only at runtime?).
Why does
two
need any libraries at all? I have used-O3
, and the assembler output is$ gcc two.c -S -O3 $ cat two.s .file "two.c" .text .p2align 4,,15 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp popl %ebp ret .size main, .-main .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3" .section .note.GNU-stack,"",@progbits $
What is the need of any libraries at all?
On Machine Two, why is
/usr/lib/libcwait.so
being used instead oflinux-gate.so.1
?I think this is because the kernel on Machine Two is very old (2.6.9) and the library
linux-gate.so.1
is not available. Is that the reason?
two.c
可能被优化为一个 0 字节(或几乎为 0 字节)的程序。但是没有任何供应商会构建针对微不足道的程序进行优化的编译器。 - Fred Foo