Getting avr-gcc-4.5.2 Running Under Gentoo Linux

From Wiki
Jump to: navigation, search

As of 2011/12/04, please see this Gentoo bug report comment for some additional changes.

These are the changes and processes to get avr-gcc-4.5.2 and avr-libc-1.7.0 working for the AVR processors. The change involve a patch to GCC for the C++ constructors to work correctly, and creating a symlink so the linker can find the linking scripts.

First, 'su -' to root, and create a temporary directory. Using your favorite editor or other tool, copy'n'paste the contents of the code boxes into the file names show in the code box. After those are created in the temporary directory, they can be copied to their final resting place per the commands below.

After the files are created, if you don't already have 'crossdev' installed, you'll need to emerge that in.

# emerge -q crossdev

Once 'crossdev' is installed, we want to make sure any previous avr-gcc and avr-libc installation gets blown away:

# crossdev -C avr

Now the patch gets copied to it's final resting place, as does the modified gcc-4.5.2 ebuild file:

# cp gcc-4.5.2.ebuild /usr/portage/sys-devel/gcc
# cp gcc-005-constructor.patch /usr/portage/sys-devel/gcc/files

Next, we have to rebuild the ebuild manifest to make emerge happy:

# ebuild /usr/portage/sys-devel/gcc/gcc-4.5.2.ebuild manifest

When that's all done, we can actually start the process that builds the compiler and library. This takes a little while to run (even on a Q9300 Quad Core Intel).

# USE="-openmp" crossdev -t avr --l 1.7.0 --g 4.5.2 -s4 --without-headers

Lastly, we need to create a symlink to the ldscripts directory because the ebuild doesn't do it right (There's actually a patch for that here, but I couldn't figure out how to tell anything to actually apply the patch (it would be *nice* if the people who create these patches wouldn't assume that everyone knows what they know...))

# ln -s /usr/lib/binutils/avr/2.21/ldscripts /usr/i686-pc-linux-gnu/avr/binutils-bin/2.21/ldscripts

If you skip the step of creating the symlink, you'll get this error when linking:

/usr/libexec/gcc/avr/ld: cannot open linker script file ldscripts/avr6.x: No such file or directory
collect2: ld returned 1 exit status

One last step is necessary to make sure that avr-gcc-4.5.2 is selected for the AVR compiler:

# gcc-config -l

If you see the line 'avr-4.5.2 *', you're good to go. If not, you'll need to issue the following command:

# gcc-config avr-4.5.2

That should be it. You should now be able to compile and link a program, and have it run. If you still get the error at link time, it may mean some directory has moved. Read through the link above, and see if anyone has added anything new.

If a known good program links, but doesn't run, it may mean that the patch for the C++ constructors was not applied correctly. Check the 'cross-avr-gcc-stage1.log' and 'cross-avr-gcc-stage2.log' and make sure you find 'Applying gcc-005-constructor.patch ...' somewhere in them (around line 175 in mine). If that's not there, then something went wrong in the first couple steps.

The only change in the gcc-4.5.2.ebuild file is the addition of the '[[ ${CTARGET} == *avr* ]] && epatch "${FILESDIR}"/gcc-005-constructor.patch' at line 81. Rather than pasting the whole file in, just that line can be edited in.

Code: gcc-4.5.2.ebuild
# Copyright 1999-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-devel/gcc/gcc-4.5.2.ebuild,v 1.1 2010/12/28 01:09:34 dirtyepic Exp $



# Hardened gcc 4 stuff
# arch/libc configurations known to be stable with {PIE,SSP}-by-default
PIE_GLIBC_STABLE="x86 amd64 ppc ppc64 arm ia64"
PIE_UCLIBC_STABLE="x86 arm amd64 ppc ppc64"
SSP_STABLE="amd64 x86 ppc ppc64 arm"
# uclibc need tls and nptl support for SSP support
#end Hardened stuff

inherit toolchain

DESCRIPTION="The GNU Compiler Collection"

LICENSE="GPL-3 LGPL-3 || ( GPL-3 libgcc libstdc++ gcc-runtime-library-exception-3.1 ) FDL-1.2"
KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~x86-fbsd"

        graphite? (
        lto? ( >=dev-libs/elfutils-0.143 )
        !build? (
                gcj? (
                        gtk? (
                nls? ( sys-devel/gettext )
        test? ( >=dev-util/dejagnu-1.4.4 >=sys-devel/autogen-5.5.4 )
        elibc_glibc? ( >=sys-libs/glibc-2.8 )
        amd64? ( multilib? ( gcj? ( app-emulation/emul-linux-x86-xlibs ) ) )
        ppc? ( >=${CATEGORY}/binutils-2.17 )
        ppc64? ( >=${CATEGORY}/binutils-2.17 )
if [[ ${CATEGORY} != cross-* ]] ; then
        PDEPEND="${PDEPEND} elibc_glibc? ( >=sys-libs/glibc-2.8 )"

src_unpack() {

        use vanilla && return 0

        sed -i 's/use_fixproto=yes/:/' gcc/config.gcc #PR33200

        [[ ${CHOST} == ${CTARGET} ]] && epatch "${FILESDIR}"/gcc-spec-env.patch
        [[ ${CTARGET} == *avr* ]] && epatch "${FILESDIR}"/gcc-005-constructor.patch

        [[ ${CTARGET} == *-softfloat-* ]] && epatch "${FILESDIR}"/4.4.0/gcc-4.4.0-softfloat.patch

pkg_setup() {

        if use lto ; then
                ewarn "LTO support is still experimental and unstable."
                ewarn "Any bugs resulting from the use of LTO will not be fixed."

This patch saves R20 before calling the constructors. The original bug report is here. The source for the attachment is the 'Patch to gcc/config/avr/libgcc.S saving r20 onto the stack before calling constructors' link.

Code: gcc-005-constructor.patch
--- gcc-4.5.2.orig/gcc/config/avr/libgcc.S      2009-05-23 17:16:07 +1000
+++ gcc-4.5.2/gcc/config/avr/libgcc.S   2010-08-12 09:38:05 +1000
@@ -802,7 +802,9 @@
        mov_h   r31, r29
        mov_l   r30, r28
        out     __RAMPZ__, r20
+       push    r20
        XCALL   __tablejump_elpm__
+       pop     r20
        cpi     r28, lo8(__ctors_start)
        cpc     r29, r17
@@ -843,7 +845,9 @@
        mov_h   r31, r29
        mov_l   r30, r28
        out     __RAMPZ__, r20
+       push    r20
        XCALL   __tablejump_elpm__
+       pop     r20
        cpi     r28, lo8(__dtors_end)
        cpc     r29, r17