Assembly file errors when using i686-elf-gcc cross-compiler

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
tabz
Member
Member
Posts: 35
Joined: Fri Apr 20, 2018 9:15 am
Location: Cambridge, UK

Assembly file errors when using i686-elf-gcc cross-compiler

Post by tabz »

I compiled a cross-compiler for my Mac (macOS 10.13.3 High Sierra) targeting i686-elf using the tutorial on the wiki. When trying to compile my kernel's main c file (I neglected to make a cross-compiler before starting on my OS) with make VERBOSE=1 I get the following:
-> Compiling src/kernel/kmain.c
mkdir -p build/obj/kernel
i686-elf-gcc -std=gnu99 -Isrc/inc -nostdlib -ffreestanding -lgcc -Wall -Wextra -Werror -Wno-unused-parameter -Wno-unused-variable -c src/kernel/kmain.c -o build/obj/kernel/kmain.o
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:6:9: error: invalid alignment value
.align 32
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:7:2: error: unknown directive
.type driver_ifc, @object
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:8:2: error: unknown directive
.size driver_ifc, 64
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:26:18: error: unexpected token in '.section' directive
.section .rodata
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:33:2: error: unknown directive
.type kmain, @function
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:37:2: error: instruction requires: Not 64-bit mode
pushl %ebp
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:61:2: error: instruction requires: Not 64-bit mode
pushl -20(%ebp)
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:62:2: error: instruction requires: Not 64-bit mode
pushl -12(%ebp)
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:67:2: error: instruction requires: Not 64-bit mode
pushl $50
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:72:2: error: instruction requires: Not 64-bit mode
pushl $.LC0
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:78:2: error: instruction requires: Not 64-bit mode
pushl %eax
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:82:2: error: instruction requires: Not 64-bit mode
pushl $.LC1
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:90:2: error: instruction requires: Not 64-bit mode
pushl -16(%ebp)
^
/var/folders/2t/xt0gv1y9783gf_wwtyp3_y_00000gn/T//ccS8Jmd5.s:102:2: error: unknown directive
.size kmain, .-kmain
^
make: *** [build/obj/kernel/kmain.o] Error 1
Below is the output from i686-elf-gcc -v
Using built-in specs.
COLLECT_GCC=i686-elf-gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/i686-elf/7.3.0/lto-wrapper
Target: i686-elf
Configured with: ../gcc-7.3.0/configure --target=i686-elf --prefix=/usr/local --disable-nls --enable-languages=c,c++ --without-headers
Thread model: single
gcc version 7.3.0 (GCC)
And here is the repohttps://github.com/SamTebbs33/jaq

All google searches have yielded nothing and I'm really confused.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Assembly file errors when using i686-elf-gcc cross-compi

Post by iansjack »

Some of the error messages would seem to indicate that you are assembling 32-bit source with a 64-bit assembler.

Can we assume that you built the appropriate binutils?
simeonz
Member
Member
Posts: 360
Joined: Fri Aug 19, 2016 10:28 pm

Re: Assembly file errors when using i686-elf-gcc cross-compi

Post by simeonz »

I am not a mac user, but I am left with the impression that your gcc is trying to use the xcode's assembler, which is also called "as". This assembler is not intended to produce x86 code, so the interaction fails. Google tells me that xcode installs gas as well, but I doubt that it is x86 targeted either. You could check this with "gcc -print-prog-name=as" (which could be different from the one in the path) and then interrogate the executable you are given or use the package manager to tell you its origin.

So. One option is to build (or acquire from somewhere) a cross-targeting binutils and point the gcc configuration with the --with-as and --with-ld options, as described here. Or you could simply set the PATH and I believe the configure script should pick them up. If you want to make a test without rebuilding gcc, you could install the target specific gas and then use the -B option to tell the compiler what (extra) paths to search for binaries and executables. Also check this SO post.
tabz
Member
Member
Posts: 35
Joined: Fri Apr 20, 2018 9:15 am
Location: Cambridge, UK

Re: Assembly file errors when using i686-elf-gcc cross-compi

Post by tabz »

simeonz wrote:I am not a mac user, but I am left with the impression that your gcc is trying to use the xcode's assembler, which is also called "as". This assembler is not intended to produce x86 code, so the interaction fails. Google tells me that xcode installs gas as well, but I doubt that it is x86 targeted either. You could check this with "gcc -print-prog-name=as" (which could be different from the one in the path) and then interrogate the executable you are given or use the package manager to tell you its origin.

So. One option is to build (or acquire from somewhere) a cross-targeting binutils and point the gcc configuration with the --with-as and --with-ld options, as described here. Or you could simply set the PATH and I believe the configure script should pick them up. If you want to make a test without rebuilding gcc, you could install the target specific gas and then use the -B option to tell the compiler what (extra) paths to search for binaries and executables. Also check this SO post.
I ran "gcc -print-prog-name=as" and it did indeed look like gcc was using the xcode installation, so I reconfigured my GCC download with the "--with-as" and "--with-ld" arguments pointing to the path of the i686 as and ld. Making this then produced a working version of i686-elf-gcc, thanks to you both for the help!
Post Reply