Tuesday, April 30, 2024

Generate cross compile toolchain in Gentoo

Gentoo crossdev

Table of Contents

1. Introcution

Crosstool-ng is commonly used to build a cross-compiler toolchain in various Linux distributions. In Gentoo, the cross-compiler environment generator, crossdev, has been made for our convinience. It is a set of bash scripts that utilize emerge to provide a system integrated cross-compilation capability.

In this article, we are going to walk through building cross-compiler toolchain for an operating system and the embedded target (bare matel).

Emerge to install crossdev.

$ sudo emerge --ask sys-devel/crossdev

 

 Before using crossdev, care must be taken to ensure crossdev environments use separate repositories. We can install the eselect-repository package and use it to create a repository for crossdev:

$ sudo emerge --ask app-eselect/eselect-repository
$ sudo eselect repository create crossdev

This can also be done manually. Refer to Gentoo wiki: Crossdev for information.

After installation, we can use the command "crossdev -v help" to list the supported targets.

$ crossdev -t help
Target (-t) takes a tuple ARCHITECTURE-VENDOR-OS-LIBC:

Supported Architectures (ARCHITECTURE):
   - alpha
   - arm / armeb / aarch64
   - hppa (parisc)
   - ia64
   - i386 / i486 / i586 / i686 (x86)
   - loongarch64
   - m68k
   - mips / mipsel / mips64 / mips64el
   - or1k
   - powerpc (ppc) / powerpc64 (ppc64)
   - riscv32 / riscv64
   - sparc / sparc64
   - s390 / s390x
   - sh / sh[1-5] / sh64
   - x86_64 (amd64)
Supported C Libraries (LIBC):
   - glibc (gnu)
   - klibc       [prob wont work]
   - musl
   - newlib      [bare metal/no operating system]
   - uclibc      [not all arches are ported]
Special Targets (full tuple):
   - avr      http://www.nongnu.org/avr-libc/
   - bfin     http://blackfin.uclinux.org/
   - h8300    http://h8300-hms.sourceforge.net/
   - mingw64  http://mingw-w64.sourceforge.net/
   - mmix     http://www-cs-faculty.stanford.edu/~knuth/mmix.html
   - msp430   http://www.ti.com/msp430
   - nds32    http://github.com/nds32
   - nios2    http://www.altera.com/products/ip/processors/nios2/ni2-index.html
   - xc16x    http://www.infineon.com/
   - ppu / spu (cell) [Cell/Playstation 3 targets]
Softfloat toolchains:
   Include 'softfloat' in the 'vendor' field
 e.g. armeb-softfloat-linux-uclibc  powerpc-booya_softfloat-linux-gnu

Note that the target option (-t) takes a tuple ARCHITECTURE-VENDOR-OS-LIBC. For example, if we want to build the stable (-S) toolchain for an system running Arm 64-bit extension that will be executed in Linux with GNU C library, we can build with the following command.

$ sudo crossdev -S -t aarch64-unknown-linux-gnu

For more tuples, please refer to Tuples.

If the target is bare matel development, we build the tools with assigning stage4 (-s4). The messages similar to the following should be seen.

$ sudo crossdev -S -s4 -t arm-none-eabi
-------------------------------------------------------------------------------------
 * crossdev version:      20240209
 * Host Portage ARCH:     amd64
 * Host Portage System:   x86_64-pc-linux-gnu (i686-pc-linux-gnu x86_64-pc-linux-gnu)
 * Target Portage ARCH:   arm
 * Target System:         arm-none-eabi
 * Stage:                 4 (C/C++ compiler)
 * USE=multilib:          yes
 * Target ABIs:           default

 * binutils:              binutils-[stable]
 * gcc:                   gcc-[stable]
 * libc:                  newlib-[latest]

 * CROSSDEV_OVERLAY:      /var/db/repos/crossdev
 * PORT_LOGDIR:           /var/log/portage
 * PORTAGE_CONFIGROOT:    /
 * Portage flags:
  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -
 * leaving sys-libs/newlib in /var/db/repos/crossdev
 * leaving sys-devel/binutils in /var/db/repos/crossdev
 * leaving sys-devel/gcc in /var/db/repos/crossdev
 * leaving dev-debug/gdb in /var/db/repos/crossdev
 * leaving metadata/layout.conf alone in /var/db/repos/crossdev
  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -
 * Log: /var/log/portage/cross-arm-none-eabi-binutils.log
 * Emerging cross-binutils ...                                                     [ ok ]
 * Log: /var/log/portage/cross-arm-none-eabi-gcc-stage1.log
 * Emerging cross-gcc-stage1 ...                                                   [ ok ]
 * Log: /var/log/portage/cross-arm-none-eabi-newlib.log
 * Emerging cross-newlib ...                                                       [ ok ]
 * Log: /var/log/portage/cross-arm-none-eabi-gcc-stage2.log
 * Emerging cross-gcc-stage2 ...                                                   [ ok ]

Test the hello world program with the following code.

#include <stdio.h>
int main()
{
        printf("Hello from the crsoss world\n");
        return 0;
}

Cross compile for the target platform with proper spec strings.

arm-none-eabi-gcc --specs=nosys.specs -lc -lrdimon -o hello hello.c

Then, we may try to execute the hello world program in Qemu.

$ qemu-arm hello
Hello from the crsoss world

Test the program with Arm 64-bit extension.

$ aarch64-unknown-linux-gnu-gcc -o hello64 -static hello.c
$ qemu-aarch64 hello64
Hello from the crsoss world

Author: Winfred Lu

Created: 2024-04-30 Tue 15:05

Validate

No comments: