2007年8月21日星期二

Installing gcc as a cross-compiler

Installing gcc as a cross-compiler

A guide for an i686-pentium host and powerpc-linux target with altivec support



Created by Mauricio Alvarez
Revised by Friman Sánchez
Last revision: 19:39 (GMT) 07.10.2003

INDEX

1. Introduction
2. Preparing the installation
3. Required Packages
4. Building Binutils
5. Building a bootstrap gcc
6. Building the linux kernel
7. Building the runtime library
8. Building the final gcc cross-compiler
9. Building the GDB debugger
10. Using gcc with altivec extensions
11. References



1. Introduction.

The goal of this document is to describe the process of installing gcc 3.3.1 as a cross-compiler for a PowerPC target with altivec support on a Linux pentium host.

Mos of the information was taken from The Crossgcc Wiki, Crosstool web page and Cross Development mini-howto for PPC Linux

To do:
- Build the cross-development tools for a 64 bits PowerPC architecture (IBM 970)

A note about crosstool
I suggest the use of crosstool scripts because they automate the process of downloading, building, compiling, patching and installing gcc, binutils, glibc and linux kernel.

In order to use crosstool for a powerpc-linux-gnualtivec target just edit demo.sh script, comment all the procssor lines and create a new entry like this:

# PPC altivec
eval `cat powerpc-altivec.dat` `cat gcc3.3.1-glibc2.3.2.dat` sh all.sh --notest

File powerpc-altivec.dat has to be like that:

TARGET=powerpc-linux-gnualtivec
TARGET_CFLAGS="-O"
GCC_EXTRA_CONFIG="--enable-altivec"

But, if you want to do all the process by hand or you want to build a Newlib based compiler (not supported yet by crosstool) here is a step by step guide:

2. Preparing the installation

Important Note. For building a cross-development tools based on gcc-3.x it is necessary to have a recent version of gcc installed on the host workstation. Try to use the latest version (nowadays: gcc-3.3.1)

Select a target

For PowerPC there are some targets available:
from gcc installation manual
  • powerpc-linux: PowerPC based systems running linux OS and using Glibc runtime library
  • powerpc-elf: PowerPC big endian mode running System V.4
  • powerpc-eabi: Embedded PowerPC in big endian mode using Newlib runtime library
  • powerpc-eabisim: Embedded powerpc systems in big endian mode for use under PSIM/GDB simulator.
  • powerpc-eabialtivec: Embedded PowerPC with big endian mode using Newlib and automatic setting of altivec gcc switches.
  • powerpc-eabisimaltivec:
  • powerpc-linux-gnualtivec: PowerPC running linux and automatic setting of -maltivec and -mabi=altivec gcc switches.

In this guide we a re going to use the powerpc-linux-gnualtivec for systems running Linux and using Glibc and powerpc-eabialtivec for embedded systems using Newlib

$export TARGET=powerpc-linux-gnualtivec
or
$export TARGET=powerpc-eabisimaltivec


Suggested directories


Base directory: indentify processor, platform and OS
$mkdir i686-pc-linux-gnu
$ cd i
686-pc-linux-gnu
$mkdir cross-gcc
$ cd cross-gcc

Source code directory: to store tarballs
$mkdir tarballs
$export TARBALLS=
/i686-pc-linux-gnu/cross-gcc/tarballs

Build directories: to store specific configurations of tools.
$mkdir build/$TARGET

Final installation directory
$mkdir result/$TARGET
$export PREFIX=/i686-pc-linux-gnu/cross-gcc/result/{TARGET}
$export PATH=$PATH:${PREFIX}/bin

3. Required packages

binutils: cross-linker, cross-assembler and so on
current version: binutils-2.14.90.0.6
download from ftp://ftp.kernel.org/pub/linux/devel/binutils

gcc-core: basic C compiler
current stable version: gcc-core-3.3.1
download from (good europe gnu mirror site) ftp://ftp.lip6.fr/pub/gcc/releases/gcc-3.3.1/gcc-core-3.3.1.tar.gz

linux kernel headers: in order to build the final gcc and run time library for systems using Glibc
current stable version: 2.4.22
download from bkbits, more information on the penguinpcc kernel source page
or download it from Linus kernel three: ftp://ftp.kernel.org/pub/linux/kernel/v2.4/linux-2.4.22.tar.gz

Run time library:
Glibc:
C run time library for target platforms running linux
current version: glibc-2.3.2
and glibc-linuxthreads-2.3.2.tar.gz
download from: ftp://ftp.gnu.org/gnu/glibc

Newlib:
C run time library por embedded devices that does not run linux
current version: newlib-1.11.0
download from : ftp://sources.redhat.com/pub/newlib

Gnu Debugger GDB: it includes PSIM PowerPC ISA emulator
current version gdb-5.3
download it from: ftp://ftp.gnu.org/gnu/gdb

4. Building binutils

$ cd build/${TARGET}
$ tar xvfz ${TARBALLS}/binutils-2.10.90.0.6.tar.gz
$ mkdir build-binutils
$ cd build-binutils
$ ../binutils-2.14.90.0.6/configure --prefix=${PREFIX} --target=${TARGET}
$ make
$ make install

5. Building a bootstrap gcc

Build a simple C compiler that does not need kernel and libary headers in order to compile the run tine library. See a longer explanation about that in http://billgatliff.com/twiki/bin/view/Crossgcc/BuildBootstrapGcc

for gcc 3.3.1 is necessary to apply a patch.
download it from http://penguinppc.org/embedded/cross-compiling/gcc-3.3.1-crossppc.diff

$ cd build/${TARGET}
$ tar xvfz ${TARBALLS}/gcc-core-3.3.1.tar.gz
$ patch gcc-3.3.1/gcc/config/rs6000/linux.h gcc-3.3.1-crossppc.diff
$ mkdir build-bootstrap
$ cd build-bootstrap
$ ../gcc-3.3.1/configure
--target=${TARGET} --prefix=${PREFIX} --without-headers --with-newlib --with-gnu-as --with-gnu-ld --disable-shared --disable-threads
$ make all-gcc
$ make install-gcc

6. Building the Linux kernel

Only for systems using glibc abd only for obtaining linux headers

Obtain a PowerPC Linux kernel (using bk appliation and bkbits kernel tree). More information on penguinpcc kernel source page
Also is possible to obtain a kernel from kernel.org

$ cd build/{TARGET}
$ mkdir linuppc-2.4
$ bk clone bk://ppc.bkbits.net/linuxppc-2.4 linuxppc-2.4
$ cd linuxppc-2.4

edit Makefile and put
ARCH := ppc
CROSS_COMPILE = powerpc-linux-gnualtivec
$ make menuconfig (the default options are OK, compilation is only for headers not for the kernel itself)
$ make dep
$ make clean
$ make zImage


The Install the linux kernel headers
$ mkdir -p ${PREFIX}/${TARGET}/include
$ cp -r build/${TARGET}/linuxppc-2.4/include/linux ${PREFIX}/${TARGET}/include/linux
$ cp -r
build/${TARGET}/linuxppc-2.4/include/asm-ppc ${PREFIX}/${TARGET}/include/asm
$ cp -r
build/${TARGET}/linuxppc-2.4/include/asm-generic ${PREFIX}/${TARGET}/include/asm-generic

7. Building the runtime library.

7.1 For Glibc: systems running linux

there is a bug building glibc 2.3.2 with gcc 3.3.1
scanf.c in function sscanf
sscanf: 37 error ´va_start´ used in function with fixed args
in order to resolve it edit glibc-2.3.2/stdio-common/sscanf.c
and replace the declaration for sscanf to
int sscanf (const char *s, const char *format, ...)

$ cd build/${TARGET}
$ tar xvfz ${TARBALLS}/glibc-2.3.2.tar.gz
$cd glibc-2.3.2
$ tar xvfz
${TARBALLS}/glibc-linuxthreads-2.3.2.tar.gz
$ cd ..
$ mkdir build-glibc
$ cd build-glibc
$ BUILD_CC=gcc CC=${TARGET}-gcc AR=${TARGET}-ar RANLIB=${TARGET}-ranlib ../glibc-2.3.2/configure --prefix=$PREFIX --host=$TARGET --build=i686-pc-linux-gnu --disable-debug --disable-profile --enable-add-ons=linuxthreads --with-headers=${PREFIX}/${TARGET}/include
$make
$make install_root=${PREFIX}/${TARGET} prefix="" install
$ echo "GROUP ( libc.so.6 libc_nonshared.a )" > ${PREFIX}/${TARGET}/lib/libc.so
$ echo "GROUP (libpthread.so.0 libpthread_nonshared.a )" > ${PREFIX}/${TARGET}/lib/libpthread.so

(and may be do something with libgcc_s.so?)

7.2 Newlibs: embedded systems or cross-compiling for emulation in GDB

$ tar xzvf newlib-1.11.0.tar.gz
$ mkdir build-newlib && cd build-newlib
$ ../newlib-1.11.0/configure --target=$TARGET --prefix=$PREFIX
$ make all
$ make install


8. Building the final gcc cross-compiler

$ cd build/${TARGET}
$ mv gcc-3.3.1 gcc-core-3.3.1
$ tar xvfz ${TARBALLS}/gcc-3.3.1.tar.gz
$ patch gcc-3.3.1/gcc/config/rs6000/linux.h gcc-3.3.1-crossppc.diff
$ mkdir build-gcc
$ cd build-gcc
$ ../gcc-3.3.1/configure --target=${TARGET} --prefix=${PREFIX} --enable-languages=c,c++ --with-sysroot=${PREFIX}/${TARGET}/include --enable-altivec --with-local-prefix=${PREFIX}/${TARGET} --with-gnu-as --with-gnu-ld
$ make
$ make install


from gcc installation manual
--enable-altivec
Specify that the target supports AltiVec vector enhancements. This option will adjust the ABI for AltiVec enhancements, as well as generate AltiVec code when appropriate. This option is only available for PowerPC systems.


--with-cpu=cpu
Default value for mcpu compilation switch. Options for PowerPC can be:

Motorola PowerPC 7450 (MPC7450): --with-cpu=7450

Motorola PowerPC (taken from http://www.mot.com/powerpc)

Motorola G5: 8540, 8560 High-end SoCs
Motorola G4+ (Altivec Technology): 7451, 7455, 7457 (2003, 2Ghz)
Motorola G4 (Altivec Technology): 7400,7410,7450,7440
Motorola G3: 750, 740, 745, 755
Motorola G2: 603, 604, 8240, 8245, 8247(proposal), 8250/55, 8260/64/65/66 8270/75 8280 5xxx, 82xx,
Motorola G1: 601, 8xx, 5xx

6xx, 7xx, 7xxx: High performance microprocessor targeting computing and high-end embedded systems
8xx, 8xxx: Integrated processor targeting communications ad consumer markets
5xx, 5xxx: Integrated processor targeting transportation market.

IBM PowerPC
PowerPC 970: 64 bits High performance processor processor --with-cpu=????
PowerPC 750: High performance processor for mainstream desktops, high end workstations. --with-cpu=750
PowerPC 405: Embedded core for custom logic applications --with-cpu=405

    9. Build the debugger

    $ cd build/${TARGET}
    $ tar xvfz ${TRABALLS}/gdb-5.3.tar.gz
    $ mkdir build-gdb
    $ cd build-gdb
    $../gdb-5.3/configure --target=$TARGET --prefix=$PREFIX --enable-sim-powerpc
    $ make
    $ make install

    PSIM
    PSIM is an ISA emulator for PowerPC now included with GDB, it supports Altivec extensions when configured with a target like powerpc-eabisimaltivec and the --enable-sim-powerpc option.

    10. Using gcc with altivec extensions

    from gcc user manual

    -maltivec
    These switches enable or disable the use of built-in functions that allow access to the AltiVec instruction set. You may also need to set -mabi=altivec to adjust the current ABI with AltiVec ABI enhancements.


    -mabi=altivec
    Extend the current ABI with AltiVec ABI extensions. This does not change the default ABI, instead it adds the AltiVec ABI extensions to the current ABI.

    If you use a powerpc-*-gnualtivec target-maltivec and -mabi=altivec are set automatically by the cross-compiler


    Code Example
    Taken from Velocity Engine Tutorial http://developer.apple.com/hardware/ve/tutorial.html

    Download the code example 2comp.c
    #include 
    #include

    vector signed long vec_2sComp (vector signed long x);

    void main ()
    {
    vector signed long v1 = {-2,0,2,8};
    vector signed long v2;

    v2 = vec_2sComp (v1);

    printf ("Numbers %9.8vld\n", v1);
    printf ("2's Comp %9.8vld\n", v2);
    }


    vector signed long vec_2sComp (vector signed long x)
    {
    vector signed long one = {1,1,1,1};
    x = vec_nor (x, x);
    x = vec_add (x, one);
    return x;
    }

    Compiling altivec programs

    $powerpc-linux-gcc -maltivec -mabi=altivec -o 2comp 2comp.c

    Some notes about the GCC implementation of Altivec from GCC manual

    - It is necessary to include in the source code the library for using the altivec built-in funcitons n gcc 3.3
    #include 
    - When compiling it is necessary to add the -maltivec and -mabi=altivec gcc switches

    - Also there are some differences between the specification in Motorola's AltiVec Programming Interface Manual and gcc implementation:
    • Vector constants are done with braces {0,0,0,0} not with parentheses
    • Vector initializers require no casts if the vector constant is of the same type as the variable it is initializing
    • Vector initializers for constants require explicit declaration
      vector signed long one = (vector signed long) {1,1,1,1};
          vector signed long one = (vector signed long) {1}; generate an internal compiler error (gcc-3.3.1 bug)

    Simulating with GDB
    ( put something here)

    11. References

    GCC

    Building and Testing gcc/glibc cross toolchains
    http://www.kegel.com/crosstool/

    Cross Development mini-howto for PPC Linux
    http://penguinppc.org/embedded/cross-compiling/

    Crossgcc
    http://billgatliff.com/twiki/bin/view/Crossgcc/WebHome

    Building a GCC PowerPC/Linux Cross Compiler For SPARC/Solaris
    http://www.borg.umn.edu/~grant/Linux/cross.html

    Altivec

    Motorola Altivec manuals and source code examples
    http://www.mot.com/altivec

    Motorola Altivec Programming Interface Manual: High level (C and C++) programming
    http://e-www.motorola.com/files/32bit/doc/ref_manual/ALTIVECPIM.pdf

    Motorola Altivec Programming Environment Manual: Assembly programming
    http://e-www.motorola.com/files/32bit/doc/ref_manual/ALTIVECPEM.pdf

    Simd tech
    http://www.simdtech.org

    Mac developper connection: Velocity Engine
    http://developer.apple.com/hardware/ve/

    没有评论: