Unsupported x86-64 instruction set error when compiling C file

12,678

Solution 1

Supply -m32 instead of -march=i686.

Solution 2

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");

Share:
12,678
BigBerger
Author by

BigBerger

Updated on July 24, 2022

Comments

  • BigBerger
    BigBerger almost 2 years

    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?