1. 环境如下:
$gcc -v:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
gcc version:
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
2. 源代码:
经典HelloWorld的汇编程序
/*
*TinyHelloWorld.c
*/
char* str = "Hello World!\n";
void print(){
asm( "movl $13,%%edx \n\t"
"movl %0,%%ecx \n\t"
"movl $0,%%ebx \n\t"
"movl $4,%%eax \n\t"
"int $0x80 \n\t"
::"r"(str):"edx","ecx","ebx");
}
void exit() {
asm( "movl $42,%ebx \n\t"
"movl $1,%eax \n\t"
"int $0x80 \n\t");
}
void nomain() {
print();
exit();
}
3. 编译报错
$gcc -c -fno-builtin TinyHelloWorld.c
其中,”-fno-builtin” 用来关闭GCC内置函数(built-in function)优化功能。
Error如下:
TinyHelloWorld.c: Assembler messages:
TinyHelloWorld.c:5: Error: unsupported instruction `mov'
问题原因:
在64位系统下去编译32位的目标文件,这样是非法的。
解决方案:
用”-m32”强制用32位ABI去编译,即可编译通过。
$gcc -c -fno-builtin -m32 TinyHelloWorld.c
4. 链接报错
$ld -static -T TinyHelloWorld.lds -e nomain -o TinyHelloWorld TinyHelloWorld.o
其中:
“-T TinyHelloWorld.lds”是TinyHelloWorld的链接控制脚本;
-e 是指定程序入口函数为nomain();
-static 表示ld是静态链接的方式链接程序,而不是用默认的动态链接方式;
-o 表示指定输出文件名为”TinyHelloWorld”
Error如下:
ld: i386 architecture of input file `TinyHelloWorld.o' is incompatible with i386:x86-64 output
问题原因:
输入目标文件`TinyHelloWorld.o’是32位系统的,然而我们的平台是64位的(默认链接脚本位于/usr/lib/ldscripts下,x86_64平台默认链接64位可执行文件用的是elf_x86_64.x,链接32位可执行文件用的是elf32_x86_64.x),如果直接ld肯定不匹配,所以需要指定链接脚本与输入目标文件对应的。
解决方案:
链接的时候加上“-m elf_i386”,因为输入目标文件为i386平台。
$ld -static -m elf_i386 -T TinyHelloWorld.lds -e nomain -o TinyHelloWorld TinyHelloWorld.o
转载自:https://blog.youkuaiyun.com/neuq_jtxw007/article/details/78112672