Linux ubuntu 14.04 x86_64平台 gcc编译错误 Error: unsupported instruction `mov' 及ld链接错误
2017-09-27 14:42
3643 查看
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" 4000 "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://stackoverflow.com/questions/11372024/what-does-the-gcc-error-message-error-unsupported-for-mov-mean
https://www.linuxquestions.org/questions/programming-9/assembly-error-i386-architecture-incompatible-with-i386-x86-64-output-827609/
相关文章推荐
- Linux x86_64平台下程序链接错误和-fPIC
- GCC 编译错误 relocation truncated to fit: R_X86_64_32S against `.bss'
- make bzImage时出现错误gcc: error: elf_i386/elf_x86_64: No such file or directory
- xcode上编译c语言程序报错:ld: x duplicate symbol for architecture x86_64 clang: error: linker command failed
- 创建pjsip module 的python 版本时遇到的错误: error: ld returned 1 exit status error: ommand 'x86_64-linux-g
- iOS编译错误#ld: warning: ignoring file# 之 Undefined symbols for architecture x86_64 - ld: symbol(s) not found for architecture x86_64
- ubuntu 一个常见的错误 error: ommand ‘x86_64-linux-gnu-gcc’ failed
- Ubuntu 16.04使用NASM编译时用ld链接程序出现:i386 架构于输入文件 sandbox.o 与 i386:x86-64 输出不兼容(I386 architecture in the input file sandbox.o is not compatible with i386: x86-64 output)
- x86_64平台编译链接汇编程序
- 在安装linux X86-64的Oracle10201时,在链接过程中出现gcc错误
- xcode上编译c语言程序报错:ld: x duplicate symbol for architecture x86_64 clang: error: linker command failed w
- GCC 编译错误 relocation truncated to fit: R_X86_64_32S against `.bss'
- xcode上编译c语言程序报错:ld: x duplicate symbol for architecture x86_64 clang: error: linker command failed w
- GCC 编译错误 relocation truncated to fit: R_X86_64_32S against `.bss'
- GCC 编译错误 relocation truncated to fit: R_X86_64_32S against `.bss'
- [iOS]编译报错:ld: symbol(s) not found for architecture x86_64
- 各平台基于GCC的链接库编译小结
- redhat-5.6-x86_64、gcc-4.9.2 编译 Qt-4.8.6 备记
- VS2008编译链接错误LINK : fatal error LNK1000: Internal error during IncrBuildImage
- coreseek 在gcc 4.9+ 上编译不通过 [sphinxexpr.o] Error 1 错误解决方案