简体   繁体   中英

Unsupported x86-64 instruction set error when compiling C file

I am trying to follow the tutorials in this link.

When I get down to the part where I start making a test.c file, I try to run the first compilation line.

gcc -c -g -Os -march=i686 -ffreestanding -Wall -Werror test.c -o test.o

Here is the contents of test.c

__asm__(".code16\n");
__asm__("jmpl $0x0000, $main\n");

void main() {
}

When I call the first compilation line, it shows me this error.

test.c:1:0: error: CPU you selected does not support x86-64 instruction set
 __asm__(".code16\n");
 ^

Can anyone tell me why this is happening? And if possible, how to fix it?

I am running Ubuntu Desktop x64, thank you in advance for your help.

EDIT:

I have changed the first compilation line to:

gcc -c -g -Os -m32 -ffreestanding -Wall -Werror test.c -o test.o

And it seems to work fine. However, there are two more lines that are giving me trouble.

ld -static -Ttest.ld -nostdlib --nmagic -o test.elf test.o

and

objcopy -O binary test.elf test.bin

The first one throws me the error of.

ld: i386 architecture of input file `test.o' is incompatible with i386:x86-64 output

And because of this, I have not tried the final line of compilation.

Here is the code for the test.ld file.

ENTRY(main);
SECTIONS
{
    . = 0x7C00;
    .text : AT(0x7C00)
    {
        *(.text);
    }
    .sig : AT(0x7DFE)
    {
        SHORT(0xaa55);
    }
} 

Any suggestions on how to fix this?

供应-m32而不是-march=i686

in fact add -m32 you can keep -march=i686 ...

gcc -c -g -Os -march=i686 -m32 -ffreestanding -Wall -Werror test.c -o test.o

works

gcc -c -g -Os -march=i686 -m16 -ffreestanding -Wall -Werror test.c -o test.o

works

gcc -c -g -Os -march=i686 -m64 -ffreestanding -Wall -Werror test.c -o test.o

fails with ;

test.c:1:0: error: CPU you selected does not support x86-64 instruction set asm (".code16\\n");

gcc -std=c99 -c -g -Os -march=i686 -m32 -ffreestanding -Wall -Werror test.c -o test.o
ld -static -T test.ld -m elf_i386 -nostdlib --nmagic -o test.elf test.o

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM