Unsupported x86-64 instruction set error when compiling a C file

I am trying to follow the tutorials in this link.

When I go to the part where I start making the test.c file, I try to run the first line of compilation.

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 someone tell me why this is happening? And if possible, how to fix it?

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

EDIT:

I changed the first compilation line to:

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

And it works fine. However, there are two more lines that bring me trouble.

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

and

 objcopy -O binary test.elf test.bin 

The first causes an error.

 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 compilation line.

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?

+5
source share
3 answers

Put -m32 instead of -march=i686 .

+11
source

actually add -m32 you can save -march = i686 ...

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

work

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

work

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

fails:

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

+4
source
 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 
-2
source

Source: https://habr.com/ru/post/1210006/


All Articles