How to make ARM cross compilation on Mac OS X (error: invalid listing option `r' - cross compiling error)

12,706

Solution 1

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.

Solution 2

Install homebrew, brew cask and then:

brew cask install gcc-arm-embedded

Solution 3

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

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

Share:
12,706

Related videos on Youtube

prosseek
Author by

prosseek

A software engineer/programmer/researcher/professor who loves everything about software building. Programming Language: C/C++, D, Java/Groovy/Scala, C#, Objective-C, Python, Ruby, Lisp, Prolog, SQL, Smalltalk, Haskell, F#, OCaml, Erlang/Elixir, Forth, Rebol/Red Programming Tools and environments: Emacs, Eclipse, TextMate, JVM, .NET Programming Methodology: Refactoring, Design Patterns, Agile, eXtreme Computer Science: Algorithm, Compiler, Artificial Intelligence

Updated on September 14, 2022

Comments

  • prosseek
    prosseek over 1 year

    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?