28

According the the gcc build instructions you can build binutils concurrently with building gcc (as well as gmp,mpc,etc).

Here's what that page says :

If you also intend to build binutils (either to upgrade an existing installation or for use in place of the corresponding tools of your OS), unpack the binutils distribution either in the same directory or a separate one. In the latter case, add symbolic links to any components of the binutils you intend to build alongside the compiler (bfd, binutils, gas, gprof, ld, opcodes, ...) to the directory containing the GCC sources.

Likewise the GMP, MPFR and MPC libraries can be automatically built together with GCC. Unpack the GMP, MPFR and/or MPC source distributions in the directory containing the GCC sources and rename their directories to gmp, mpfr and mpc, respectively (or use symbolic links with the same name).

This works fine for gmp,mpc, mpfr, but I can't seem to get it to build all of binutils. Nor can I figure out how to get it to build the new gold linker from binutils. The versions in question are gcc-4.4.2 and binutils-2.20.

A step by step instruction would be great (for me, and for others who run into this issue as well).

Siguza
  • 21,155
  • 6
  • 52
  • 89
bdbaddog
  • 3,357
  • 2
  • 22
  • 33
  • You may also be interested in looking at how [Linux From Scratch](http://www.linuxfromscratch.org/lfs/view/development/index.html) does it. It uses a 2 step bootstrap for binutils, GCC and glibc (another major GCC dependency). – Ciro Santilli OurBigBook.com May 16 '15 at 07:25

2 Answers2

30

This should still be supported OK, as it's commonly used for building cross-compilers.

In fact, I've just done this with gcc 4.6.0 and binutils 2.21 (with gmp, mpc and mpfr at appropriate versions), and the following seemed to work fine:

  • Get all the archives of the stuff you're going to build (gcc-4.6.0.tar.bz2, binutils-2.21.tar.bz2 etc) into a new dir, e.g. src

  • Un-tar them all in this directory, so you end up with gcc-4.6.0/ binutils-2.21/ gmp-5.0.2/ and more sitting alongside one another

    tar jxvf gcc-4.6.0.tar.bz2 ... (unpack others here, watch file lists scroll past)

  • cd gcc-4.6.0 and symlink the gmp, mpc and mpfr directories without their version numbers in the links, e.g:

    ln -s ../gmp-5.0.2 gmp

  • Now symlink everything from the binutils dir which doesn't exist in the gcc dir, so anything which already exists will take priority but the binutils tools will look be visible to the build:

    for file in ../binutils-2.21/* ; do ln -s "${file}" ; done

  • Change up a dir and make a build directory to build all this in separately to the sources (this always used to be the recommended method, and it tends to still be more reliable than building inside the source dir):

    cd .. ; mkdir build

  • At this point you should have a set of directories and links which looks something like this:

    binutils-2.21/ build/ gcc-4.6.0/ gmp -> ../gmp-5.0.2 mpc -> ../mpc-0.9 mpfr -> ../mpfr-3.0.1 bfd -> ../binutils-2.21/bfd binutils -> ../binutils-2.21/binutils gas -> ../binutils-2.21/gas ... (lots more symlinks for binutils here, plus existing gcc stuff) gmp-5.0.2/ mpc-0.9/ mpfr-3.0.1/

  • Configure the whole lot from this dir, with whatever options you need to pass to configure:

    ../gcc-4.6.0/configure --prefix=/foo/bar --enable-languages=c,c++,ada

  • Build, wait, install (you'll probably want to use make -j4 or so here to get some builds in parallel as it's going to take a while) :

    make -j4 ; make install

Add the destination to your path if it's not already (and perhaps the lib dir to LD_LIBRARY_PATH if this is outside of those specified in /etc/ld.so.conf, as mentioned in the messages about installing libraries during the make install step), and everything should be up and running with this new version.

It's probably worth checking that you're using this installed version once you've opened a new shell, with :

    `which gcc`

and

    `which as`

..as well as that the version is as you expect with:

    `gcc --version`

and

    `as --version`

..as well as (of course) testing that the installed version builds executables fine with some simple examples before you let it loose on your code-base :)

Edit: The comments below contain some sets of versions which are known to work together. Not all combinations will work, so you might need to go through some trial and error for different combinations to those mentioned!

Much later edit: gdb is also possible to include in this build (again requires compatible component versions - see comments). Add this as the last thing after binutils in a similar way, using for f in ../gdb-8.1.1/* ; do ln -s "${f}" ; done and the build will automatically pick it up.

Edit as of 2023-05-03: Latest released version gcc 13.1.0, binutils 2.40, gdb 13.1, cloog 0.18.1, isl 0.24, gmp 6.1.0, mpc 1.2.1, mpfr 4.1.0 all build together fine, at least for c,c++,fortran as the language set.

David Gardner
  • 6,952
  • 4
  • 35
  • 37
  • 2
    A note to myself and others: Doing this with gcc 4.7.1 requires an older version of mpfr than the currently-most-recent. I've used mpfr-2.4.2 with success but anything > 3.0 didn't work for me. (List of all components : binutils-2.22, gcc-4.7.1, gmp-5.0.5, mpc-1.0, mpfr-2.4.2) – David Gardner Jul 23 '12 at 08:48
  • 2
    Another note: Doing this with gcc 4.7.2, with binutils-2.22 and contrib/download_prerequisites for gmp,mpc,mpfr; recipe works, but gcc fails to build with -flto, resulting in: collect2: fatal error: cannot find 'ld'. To rectify that, make a symbolic link ld -> ld-new in your ...prefix/bin directory. – Dmitry Chichkov Oct 16 '12 at 00:12
  • If you want to enable the 'graphite' optimizations (see http://gcc.gnu.org/wiki/Graphite) then you'll also need the ISL and CLooG libraries, and to symlink them in in the same way as the other libraries (mpfr, mpc and so on). I'll update the instructions to include this shortly. – David Gardner Jun 10 '13 at 09:19
  • 2
    gcc 4.9 build works with : binutils 2.24, cloog 0.18.1, gmp 5.1.3 isl 0.12.2 mpc 1.0.2 mpfr 3.1.2. Use the "--disable-werror" option to avoid the warnings when building the bfd version from binutils due to macro warnings. (These warnings are fixed in the current development version from their git repo) – David Gardner Apr 29 '14 at 15:19
  • @DavidGardner I'just built GCC 4.8.3 along with companion libs and binutils as descibed above and works fine. I was wondering if is it possible to include gdb in the batch? Gdb configure script exposes similar options that of gcc, an some of that I can't interpret in the context of gdb. – Gyorgy Szekely Oct 16 '14 at 14:03
  • @GyorgySzekely That should be possible. It will be a case of finding suitable versions which work together. (I'm having a go now to see if the latest versions work as with gcc 4.9 above) – David Gardner Oct 20 '14 at 11:46
  • @GyorgySzekely In theory this should work but it's giving me bfd errors during the build at the moment. If I find a combination of version which works then I'll let you know! – David Gardner Nov 07 '14 at 15:26
  • gcc 4.9.2 combined-tree build works OK with : mpc-1.0.2, mpfr-3.1.2, gmp-5.1.3, isl-0.12.2, cloog-0.18.3, binutils-2.24 and the --disable-werror option as before. binutils 2.25 doesn't play nicely in this build for me yet. – David Gardner Feb 09 '15 at 16:07
  • For a cross compiler: some projects (eg, gccgo) will use the wrong objcopy, objdump, etc unless you have a cross binutils in your path; with that, you can then build with binutils in tree. – Brian Vandenberg Apr 10 '15 at 06:35
  • gcc-5.1.0 combined-tree builds OK with : binutils 2.24 + cloog 0.18.3 + isl 0.14.1 + mpc 1.0.3 + mpfr 3.1.2 + gmp 5.1.3 + --disable-werror option to configure script. binutils 2.25 still doesn't play nicely in this build yet -- causes the stage2 build to fail with "C compiler cannot create executables" during the initial config run. – David Gardner Apr 23 '15 at 15:26
  • 1
    I removed the link to the osdev site matrix for the cross compiler gcc/binutils version compatibility from the answer which was edited into it (osdev.org has [a large binutils / GCC compatibility matrix](http://wiki.osdev.org/Cross-Compiler_Successful_Builds)), as that is different in a couple of ways: (1) It is to build a cross-compiler gcc, which has different restrictions on versions, and (2) The binutils build is not combined in-tree with the gcc build but a separate step beforehand. Hence it's not quite the same as my answer - not to say that it's not valid in that case :) – David Gardner Jun 11 '15 at 08:23
  • For info : The versions of cloog, isl, mpc, mpfr, gmp used if you use the download_prerequisites script and those in the "infrastructure" directory alongside the gcc source downloads are actually the only ones necessary and tested for the builds... I should probably change these instructions to reflect this! – David Gardner Oct 22 '15 at 08:52
  • Another working components set for the latest gcc : gcc 8.2.0, binutils 2.31, gdb 8.1.1, and the standard "infrastructure" components of cloog 0.18.1, isl 0.18, gmp 6.1.0, mpc 1.0.3, mpfr 3.1.4. No special options to `configure` required at all, and I've only tested this with `--enable-languages=c,c++,fortran` so other languages may have trouble with it which I haven't yet hit. – David Gardner Aug 06 '18 at 12:08
  • Some more version sets which work for this build method : gcc 9.3.0, binutils 2.32, gdb 8.3.1, cloog 0.18.1, isl 0.18, gmp 6.1.0, mpc 1.0.3, mpfr 3.1.4. – David Gardner May 11 '20 at 14:19
  • gcc 10.1.0 builds with binutils 2.34 and the above infrastructure component versions, but won't build with any released version of gdb I can find at the moment. – David Gardner May 11 '20 at 14:20
  • gcc 13.1.0, binutils 2.40, gdb 13.1, cloog 0.18.1, isl 0.24, gmp 6.1.0, mpc 1.2.1, mpfr 4.1.0 all build together fine, at least for c,c++,fortran as the language set. – David Gardner May 03 '23 at 09:10
5

What you want to do is called a "combined tree" or "in-tree binutils" build. You can find documentation on how to proceed here and there.

F'x
  • 12,105
  • 7
  • 71
  • 123