Linux ubuntu 14.04 x86_64平臺 gcc編譯錯誤 Error: unsupported instruction `mov' 及ld鏈接錯誤

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://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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章