How to make ARM cross compilation on Mac OS X (err

2020-02-11 06:34发布

问题:

I'm trying to build a gcc cross compiler on Mac OS X, the target is arm-elf-eabi.

This is what I've done so far:

Install gcc and environmental setup

I tried not to use clang/llvm from Mac, so I used the gcc-4.8 from brew.

brew tap homebrew/versions 
brew install gcc48
brew install binutils

Before the building, I set the environment variables:

export CC=/usr/local/bin/gcc-4.8
export CXX=/usr/local/bin/g++-4.8
export CPP=/usr/local/bin/cpp-4.8
export LD=/usr/local/bin/gcc-4.8
export PREFIX=/opt/cross
export TARGET=arm-elf-eabi

build binutils

Downloading a binutils source, then creating a build directory, and could build binutils without an issue:

mkdir build-binutils
cd build-binutils
../binutils-2.24/configure --target=$TARGET --prefix=$PREFIX --with-sysroot --enable-64-bit-bfd --disable-shared --disable-werror --disable-nls
make configure-host
make LDFLAGS="-all-static"
sudo make install

building gcc

Downloading gcc 4.8 source, and run the build process

mkdir build-gcc
cd build-gcc
../gcc-4.8.3/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-languages=c,c++ --without-headers --disable-shared --enable-multilib --with-sysroot --disable-werror
make configure-host
make all-gcc << ERROR
make all-target-libgcc

However, I got invalid listing optionr'` error in building.

/usr/local/bin/g++-4.8 -c   -g -O2 -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE  -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings   -DHAVE_CONFIG_H -DGENERATOR_FILE -I. -Ibuild -I../../gcc-4.8.3/gcc -I../../gcc-4.8.3/gcc/build -I../../gcc-4.8.3/gcc/../include  -I../../gcc-4.8.3/gcc/../libcpp/include  \
        -o build/genconstants.o ../../gcc-4.8.3/gcc/genconstants.c
Assembler messages:
Fatal error: invalid listing option `r'
make[2]: *** [build/genconstants.o] Error 1
make[1]: *** [all-gcc] Error 2

Alternative building process

I tried to use default compiler from Mac with the following configuration:

./contrib/download_prerequisites
CFLAGS="-arch i386 -arch x86_64 -Wno-error=deprecated-declarations -Wno-error=unused-variable -Wno-error=unused-function" ../gcc-4.8.3/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-languages=c --without-headers --disable-shared --enable-multilib

Then, it works fine with make all-gcc, but I also have different error with make all-target-libgcc, so I'll stick to gcc approach instead of clang/llvm approach.

My guess is that the assembler as from Apple has something wrong with parameters that gcc gives (because there's nothing wrong with clang/llvm).

What might be wrong? How to solve this issue?

回答1:

Install homebrew, brew cask and then:

brew cask install gcc-arm-embedded


回答2:

This is the way how I make the cross compilation on Mac OS X, and execute the qemu to check if it works.

Compilation

Just follow this instruction - https://github.com/jsnyder/arm-eabi-toolchain

mkdir /opt/arm # and make it writable
PREFIX=/opt/arm PROCS=8 CC=clang make install-cross
PREFIX=/opt/arm PROCS=8 CC=clang make cross-gdb

Check if all the binary files are available.

Pre-built compiler

  • http://sourceforge.net/projects/yagarto/files/
  • https://launchpad.net/gcc-arm-embedded/+download

    qemu simulation

Source code

.text
entry: b start
arr:    .byte 1, 2, 3, 4, 5, 6
eoa:
.align
start:  
    ldr r0, =eoa
    ldr r1, =arr
    mov r3, #0
loop:
    ldrb r2, [r1], #1
    add r3, r2, r3
    cmp r1, r0
    bne loop
stop:   b stop

Build script

dd if=/dev/zero of=flash.bin bs=4096 count=4096
arm-none-eabi-as --gstabs+ -o add.o add.S
arm-none-eabi-ld -Ttext=0x0 -o add.elf add.o
arm-none-eabi-objcopy -O binary add.elf add.bin
dd if=add.bin of=flash.bin bs=4096 conv=notrunc

Execute

Start the qemu

qemu-system-arm -M connex -pflash flash.bin -gdb tcp::1234 -S

Execute the debugger

arm-none-eabi-gdb add.elf

Inside the debugger start the connection

target remote :1234

You can use n to execute the line one by one.

(gdb) target remote :1234
Remote debugging using :1234
entry () at add.S:2
2   entry: b start
(gdb) n
7       ldr r0, =eoa
(gdb) n
8       ldr r1, =arr
(gdb) n
9       mov r3, #0
(gdb) n
11      ldrb r2, [r1], #1
(gdb) n
12      add r3, r2, r3
(gdb) n
13      cmp r1, r0
(gdb) n
14      bne loop
(gdb) n
11      ldrb r2, [r1], #1
(gdb) n
12      add r3, r2, r3

References

  • Qemu flash boot up does not work
  • GDB Debugging ARM U-Boot on QEMU
  • Debugging ASM
    • --gstabs+ is the flag to use


回答3:

In the directory build-gcc/gcc, there exists an as script file to be invoked instead of the /usr/bin/as. When I renamed as to as_temp, and rerun, everything works fine. The environmental variable AS=/usr/bin/as does not seem to solve this issue.

I needed one more modification that /opt/cross/arm-elf-eabi/sys-root/usr/include should be created in order to prevent another error message.

I also found that this site is pretty useful in cross compilation on Mac OS X.



回答4:

There's a pre-configured cross-compiler, its name is Linaro, and you can get it from the official site. If you need, here's a guide which shows you how to install it and compile with it. For example, for .c files:

${PREFIX}gcc -Wall -Wextra -Os -ffreestanding -mcpu=cortex-a8 -march=armv7-a -mfpu=neon -marm -c filename.c 

where Prefix stands for the directory where you've installed the tool.