|
发表于 2006-7-30 11:46:14
|
显示全部楼层
我strace了一下gcc hello.c,发现gcc调用了/usr/bin/ld来进行链接。也更加肯定了gcc -v 里面没有链接的显示。- wangyao@fisherman:~$ strace -f -F -o gccfile gcc hello.c
- wangyao@fisherman:~$ cat gccfile | grep execve | grep -v '\-1' | grep execve\(
- 2303 execve("/usr/bin/gcc", ["gcc", "hello.c"], [/* 33 vars */]) = 0
- 2304 execve("/usr/lib/gcc/i486-linux-gnu/4.0.4/cc1", ["/usr/lib/gcc/i486-linux-gnu/4.0."..., "-quiet", "hello.c", "-quiet", "-dumpbase", "hello.c", "-mtune=i686", "-auxbase", "hello", "-o", "/tmp/ccYXSGzF.s"], [/* 35 vars */]) = 0
- 2305 execve("/usr/bin/as", ["as", "-Qy", "--32", "-o", "/tmp/ccYxJmff.o", "/tmp/ccYXSGzF.s"], [/* 35 vars */]) = 0
- 2306 execve("/usr/lib/gcc/i486-linux-gnu/4.0.4/collect2", ["/usr/lib/gcc/i486-linux-gnu/4.0."..., "--eh-frame-hdr", "-m", "elf_i386", "-dynamic-linker", "/lib/ld-linux.so.2", "/usr/lib/gcc/i486-linux-gnu/4.0."..., "/usr/lib/gcc/i486-linux-gnu/4.0."..., "/usr/lib/gcc/i486-linux-gnu/4.0."..., "-L/usr/lib/gcc/i486-linux-gnu/4."..., "-L/usr/lib/gcc/i486-linux-gnu/4."..., "-L/usr/lib/gcc/i486-linux-gnu/4."..., "-L/usr/lib/gcc/i486-linux-gnu/4."..., "-L/lib/../lib", "-L/usr/lib/../lib", "/tmp/ccYxJmff.o", ...], [/* 37 vars */]) = 0
- 2307 execve("/usr/bin/ld", ["/usr/bin/ld", "--eh-frame-hdr", "-m", "elf_i386", "-dynamic-linker", "/lib/ld-linux.so.2", "/usr/lib/gcc/i486-linux-gnu/4.0."..., "/usr/lib/gcc/i486-linux-gnu/4.0."..., "/usr/lib/gcc/i486-linux-gnu/4.0."..., "-L/usr/lib/gcc/i486-linux-gnu/4."..., "-L/usr/lib/gcc/i486-linux-gnu/4."..., "-L/usr/lib/gcc/i486-linux-gnu/4."..., "-L/usr/lib/gcc/i486-linux-gnu/4."..., "-L/lib/../lib", "-L/usr/lib/../lib", "/tmp/ccYxJmff.o", ...], [/* 38 vars */] <unfinished ...>
复制代码
gcc -v的显示只到collect2,最后还是调用了/usr/bin/ld进行的链接。
gcc编译过程中,打开的文件有:(只是打开的,不包含执行)- wangyao@fisherman:~$ cat gccfile | grep -v "\-1"| grep open | cut -f2 -d""" | sort -u
- a.out
- /etc/ld.so.cache
- /etc/ld.so.conf
- /etc/mtab
- hello.c
- /lib/ld-linux.so.2
- /lib/libc.so.6
- /lib/tls/libc.so.6
- /proc/meminfo
- /tmp/cc7SXMz9.ld
- /tmp/ccK56sru.o
- /tmp/ccTsZfiP.c
- /tmp/ccYxJmff.o
- /tmp/ccYXSGzF.s
- /usr/include/bits/stdio_lim.h
- /usr/include/bits/sys_errlist.h
- /usr/include/bits/types.h
- /usr/include/bits/typesizes.h
- /usr/include/bits/wchar.h
- /usr/include/bits/wordsize.h
- /usr/include/features.h
- /usr/include/_G_config.h
- /usr/include/gconv.h
- /usr/include/gnu/stubs.h
- /usr/include/libio.h
- /usr/include/stdio.h
- /usr/include/sys/cdefs.h
- /usr/include/wchar.h
- /usr/lib/gcc/i486-linux-gnu/4.0.4/crtbegin.o
- /usr/lib/gcc/i486-linux-gnu/4.0.4/crtend.o
- /usr/lib/gcc/i486-linux-gnu/4.0.4/include/stdarg.h
- /usr/lib/gcc/i486-linux-gnu/4.0.4/include/stddef.h
- /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crt1.o
- /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crti.o
- /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crtn.o
- /usr/lib/gcc/i486-linux-gnu/4.0.4/libgcc.a
- /usr/lib/gcc/i486-linux-gnu/4.0.4/libgcc_s.so
- /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libc.so
- /usr/lib/gconv/EUC-CN.so
- /usr/lib/gconv/gconv-modules
- /usr/lib/gconv/libGB.so
- /usr/lib/libbfd-2.16.91.so
- /usr/lib/libc_nonshared.a
- /usr/lib/locale/locale-archive
- /usr/share/locale/locale.alias
- /usr/share/locale/zh_CN/LC_MESSAGES/gcc-4.0.mo
- /usr/share/locale/zh_CN/LC_MESSAGES/ld.mo
复制代码
VirusCamp已经测试过,ld-linux.so.2只是运行是必须的,为什么在编译的时候还要访问呢?
原因是我们在编译程序的时候,指定了动态链接器。那么在程序运行期间,如果会要使用动态链接库的话,就会调用指定的动态链接器来链接动态链接库。我们没有必要一定指定/lib/ld-linux.so.2,正如VirusCamp测试的,我们可以指定/lib/ld-linux.so.test。但是,如果这个动态的链接器不存在的话,那么就无法链接动态链接库,程序就无法运行。
这样,对于ld跟ld-linux.so的区别应该明白了!!! |
|