Chinaunix首页 | 论坛 | 博客
  • 博客访问: 493694
  • 博文数量: 102
  • 博客积分: 4001
  • 博客等级: 上校
  • 技术积分: 756
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-21 16:01
文章分类

全部博文(102)

文章存档

2011年(1)

2010年(1)

2009年(56)

2008年(44)

我的朋友

分类: LINUX

2008-11-10 10:14:25

    今天有看CS630[1]的Chapter 15,发现里头的一个例程manydots.s无法正常编译。

$ gcc manydots.s -o manydots
/tmp/ccIvmRVT.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.3.1/../../../../lib/crt1.o:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.3.1/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
$ sed -i -e "s/_start/main/g" manydots.s
$ gcc manydots.s -o manydots
$ ./manydots
Segmentation fault
$ file manydots
manydots: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped


    通过上面的实验,首先根据提示发现_start有multiple definition,所以根据自己的经验,把里头的_start符号替换成main。因为用gcc编译时默认的程序入口是main,而不是 _start。资料[2]告诉我们_start是真正的程序入口,但是这个真正的入口是gcc默认链接到我们的可执行文件中的,如果我们这里又设置一个 _start符号,那就是multiple definition了(你可以通过gcc的-S选项编译一个C语言程序产生汇编代码,看看汇编代码的程序入口,刚好是main,关于谁是真正的程序入 口,你可以看看资料[2])。
    那修改了_start为main后,能够正常编译,但为什么还出现segmentation fault呢?原因是源代码mangdots.s是为32为平台写的,而我用的处理器是64位的,并且安装了64位的Ubuntu/Linux。

$ cat /proc/cpuinfo | grep "model name"
model name    : AMD Athlon(tm) 64 X2 Dual Core Processor 4200+
model name    : AMD Athlon(tm) 64 X2 Dual Core Processor 4200+
$ uname -a
Linux falcon 2.6.26-1-amd64 #1 SMP Thu Aug 28 11:13:42 UTC 2008 x86_64 GNU/Linux

  
    根据资料[3,4,5],我们发现,64位平台跟32位平台有很大的不同,包括参数传递方式,指令集都有很大的变化,那怎么能够让它正常运行呢?利用 gcc的-m32参数编译产生32位的目标代码,而不是64位的目标代码,因为32位的目标代码可以运行在64位的主机上。

$ gcc -m32 manydots.s -o manydots
$ ./manydots
How many dots do you want to see? 10
..........
$ file manydots
manydots: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped


可以看到,这样就okay了。

    实际上,我们还可以分步来做:先汇编,后链接。这样可以减少目标代码的大小,先看看原来的大小。

$ wc -c manydots
6495 manydots

 

    我们分步汇编、链接:

// 这个时候是需要一个默认的_start入口的,如果不指定,会默认设置一个程序入口地址,因为这个时候没有人给我们设置一个真正的入口_start了。
$ sed -i -e "s/main/_start/g" manydots.s
$ as --32 manydots.s -o manydots.o
$ ld -m elf_i386 manydots.o -o manydots
$ wc -c manydots
1026 manydots
$ echo "6495-1026" | bc
5469
$ ./manydots
How many dots do you want to see? 10
..........


    可以发现,这样也可以正常工作,不过目标减少了5469个字节。为什么会有这样的效果呢?资料[2]给出了详细的解释,如果感兴趣,可以研究一下。
    对了,“as --32 manydots.s -o manydots.o”可以直接用“$ gcc -m32 -c manydots.s -o manydots.o” 来做,他们两个实际上做了同一个事情,你可以通过gcc的--verbose查看:

$ gcc --verbose -m32 -c manydots.s -o manydots.o
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.1-9' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-cld --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.3.1 (Debian 4.3.1-9)
COLLECT_GCC_OPTIONS='-v' '-m32' '-c' '-o' 'manydots.o' '-mtune=generic'
 as -V -Qy --32 -o manydots.o manydots.s
GNU assembler version 2.18.0 (x86_64-linux-gnu) using BFD version (GNU Binutils for Debian) 2.18.0.20080103
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.3.1/:/usr/lib/gcc/x86_64-linux-gnu/4.3.1/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.3.1/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.3.1/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.3.1/32/:/usr/lib/gcc/x86_64-linux-gnu/4.3.1/32/:/usr/lib/gcc/x86_64-linux-gnu/4.3.1/../../../../lib32/:/lib/../lib32/:/usr/lib/../lib32/:/usr/lib/gcc/x86_64-linux-gnu/4.3.1/:/usr/lib/gcc/x86_64-linux-gnu/4.3.1/:/usr/lib/gcc/x86_64-linux-gnu/4.3.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-m32' '-c' '-o' 'manydots.o' '-mtune=generic'


    最后总结一下,在64位主机上编译产生32位目标代码的办法:
    一、办法一:直接通过gcc汇编、链接
       1、确保不要有重复的_start入口,把_start替换成main
       2、用gcc加上-m32参数进行汇编和链接
    二、办法二:分步汇编、链接
        1、汇编的时候,用gcc加上-m32参数或者用as加上--32参数。
        2、在链接的时候,用ld加上-m elf_i386参数。

[1] CS630 on ubuntu with qemu
http://oss.lzu.edu.cn/blog/blog.php?/do_showone/tid_1808.html
[2] 为你的可执行文件“减肥”
http://oss.lzu.edu.cn/blog/blog.php?do_showone/tid_1547.html
[3] GCC在AMD64平台下的参数传递
http://hi.baidu.com/bluebanboom/blog/item/381959af65ff36fbfaed5068.html
[4] Intel的64位扩展技术简介

[5] AMD64 Architecture Tech Docs
,,30_2252_739_7044,00.html

阅读(10849) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~