免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2992 | 回复: 4
打印 上一主题 下一主题

atexit的代码是如何来的呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-07 12:30 |只看该作者 |倒序浏览
1.c

  1. int main()
  2. {
  3. printf("Hello World!\n");
  4. return 0;
  5. }
复制代码


gcc -V -pg 1.c -o 1

  1. [root@proxy ~/3]# gcc -v -pg 1.c -o 1
  2. Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
  3. gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)
  4. /usr/lib/gcc-lib/i386-redhat-linux/2.96/cpp0 -lang-c -v -D__GNUC__=2 -D__GNUC_MINOR__=96 -D__GNUC_PATCHLEVEL__=0 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__NO_INLINE__ -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -D__tune_i386__ 1.c /tmp/cchoxKb5.i
  5. GNU CPP version 2.96 20000731 (Red Hat Linux 7.1 2.96-98) (cpplib) (i386 Linux/ELF)
  6. ignoring nonexistent directory "/usr/local/include"
  7. ignoring nonexistent directory "/usr/i386-redhat-linux/include"
  8. #include "..." search starts here:
  9. #include <...> search starts here:
  10. /usr/lib/gcc-lib/i386-redhat-linux/2.96/include
  11. /usr/include
  12. End of search list.
  13. /usr/lib/gcc-lib/i386-redhat-linux/2.96/cc1 /tmp/cchoxKb5.i -quiet -dumpbase 8.c -version -p -o /tmp/cc6iNLJ3.s
  14. GNU C version 2.96 20000731 (Red Hat Linux 7.1 2.96-98) (i386-redhat-linux) compiled by GNU C version 2.96 20000731 (Red Hat Linux 7.1 2.96-98).
  15. as -V -Qy -o /tmp/ccTQbTp1.o /tmp/cc6iNLJ3.s
  16. GNU assembler version 2.11.90.0.8 (i386-redhat-linux) using BFD version 2.11.90.0.8
  17. /usr/lib/gcc-lib/i386-redhat-linux/2.96/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o 1 /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../gcrt1.o /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o /usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbegin.o -L/usr/lib/gcc-lib/i386-redhat-linux/2.96 -L/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../.. /tmp/ccTQbTp1.o -lgcc -lc -lgcc /usr/lib/gcc-lib/i386-redhat-linux/2.96/crtend.o /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crtn.o
复制代码


objdump -S 1会发现连接了一个atext代码,不知从何而来,在gcrt1.o,crtbegin.o,crtend.o,cetn.o中没有找到

  1. 080485dc <atexit>:
  2. 80485dc:       55                      push   %ebp
  3. 80485dd:       89 e5                   mov    %esp,%ebp
  4. 80485df:       53                      push   %ebx
  5. 80485e0:       52                      push   %edx
  6. 80485e1:       e8 00 00 00 00          call   80485e6 <atexit+0xa>
  7. 80485e6:       5b                      pop    %ebx
  8. 80485e7:       81 c3 ba 10 00 00       add    $0x10ba,%ebx
  9. 80485ed:       50                      push   %eax
  10. 80485ee:       8b 83 30 00 00 00       mov    0x30(%ebx),%eax
  11. 80485f4:       31 d2                   xor    %edx,%edx
  12. 80485f6:       85 c0                   test   %eax,%eax
  13. 80485f8:       74 02                   je     80485fc <atexit+0x20>
  14. 80485fa:       8b 10                   mov    (%eax),%edx
  15. 80485fc:       52                      push   %edx
  16. 80485fd:       6a 00                   push   $0x0
  17. 80485ff:       ff 75 08                pushl  0x8(%ebp)
  18. 8048602:       e8 cd fd ff ff          call   80483d4 <_init+0x48>
  19. 8048607:       83 c4 10                add    $0x10,%esp
  20. 804860a:       8b 5d fc                mov    0xfffffffc(%ebp),%ebx
  21. 804860d:       c9                      leave  
  22. 804860e:       c3                      ret   
  23. 804860f:       90                      nop   
复制代码

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
2 [报告]
发表于 2008-12-07 13:26 |只看该作者
atexit是标准库里的东西,libc.a里面查查就有

论坛徽章:
0
3 [报告]
发表于 2008-12-07 20:08 |只看该作者
发现链接时会使用/usr/lib/libc.so

  1. [root@proxy /usr/lib]# cat libc.so
  2. /* GNU ld script
  3.    Use the shared library, but some functions are only in
  4.    the static library, so try that secondarily.  */
  5. GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
  6. [root@proxy /usr/lib]# ar t libc_nonshared.a
  7. atexit.oS
  8. stat.oS
  9. fstat.oS
  10. lstat.oS
  11. mknod.oS
  12. stat64.oS
  13. fstat64.oS
  14. lstat64.oS
  15. [root@proxy /usr/lib]# ar x libc_nonshared.a atexit.oS
  16. [root@proxy /usr/lib]# objdump -S atexit.oS

  17. atexit.oS:     file format elf32-i386

  18. Disassembly of section .text:

  19. 00000000 <atexit>:
  20.    0:   55                      push   %ebp
  21.    1:   89 e5                   mov    %esp,%ebp
  22.    3:   53                      push   %ebx
  23.    4:   52                      push   %edx
  24.    5:   e8 00 00 00 00          call   a <atexit+0xa>
  25.    a:   5b                      pop    %ebx
  26.    b:   81 c3 03 00 00 00       add    $0x3,%ebx
  27.   11:   50                      push   %eax
  28.   12:   8b 83 00 00 00 00       mov    0x0(%ebx),%eax
  29.   18:   31 d2                   xor    %edx,%edx
  30.   1a:   85 c0                   test   %eax,%eax
  31.   1c:   74 02                   je     20 <atexit+0x20>
  32.   1e:   8b 10                   mov    (%eax),%edx
  33.   20:   52                      push   %edx
  34.   21:   6a 00                   push   $0x0
  35.   23:   ff 75 08                pushl  0x8(%ebp)
  36.   26:   e8 fc ff ff ff          call   27 <atexit+0x27>
  37.   2b:   83 c4 10                add    $0x10,%esp
  38.   2e:   8b 5d fc                mov    0xfffffffc(%ebp),%ebx
  39.   31:   c9                      leave  
  40.   32:   c3                      ret   
  41.   33:   90                      nop   
  42. [root@proxy /usr/lib]#
复制代码


不知为何要将这些函数独立出来?不能共享?

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
4 [报告]
发表于 2008-12-07 22:08 |只看该作者
原帖由 qtdszws 于 2008-12-7 20:08 发表
发现链接时会使用/usr/lib/libc.so

[root@proxy /usr/lib]# cat libc.so
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily ...

不共享?什么意思?

论坛徽章:
0
5 [报告]
发表于 2008-12-08 10:30 |只看该作者

  1. [root@proxy ~/3]# readelf -s /lib/libc
  2. libc-2.2.4.so        libcgi.a             libcrypt-2.2.4.so    libcrypto.so.2
  3. libc.so.6            libcom_err.so.2      libcrypt.so.1        
  4. libc.txt             libcom_err.so.2.0    libcrypto.so.0.9.6b  
  5. [root@proxy ~/3]# readelf -s /lib/libc.so.6 |grep atexit
  6.   1010: 000317e4    54 FUNC    GLOBAL DEFAULT   11 [email]atexit@GLIBC_2.0[/email]
  7.   1200: 000316f0    64 FUNC    GLOBAL DEFAULT   11 __cxa_atexit@@GLIBC_2.1.3
复制代码


在libc.so.6中找到了atexit的代码,为何还要单独复制一份到目标文件呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP