5 pbin Building gcc-2.6.2

Contents of this section

Now we have in /usr/i486-linuxelf/bin the as (gas) and ld (gld), both compiled for ELF support. Now we will compile gcc-2.6.2 to generate ELF code. We need ELF assembler because in the final step, when make generates the libgcc using the xgcc, the xgcc is an ELF compiler so it needs the ELF assembler.

Note:

gcc-2.6.2 seems to have a bug in ELF generation code of the profiler section. When you build code to be profiled (-p or -pg), gcc-2.6.2 generates a call to mcount() function. Unfortunately, this code is generated in assembly stage and gcc fails to generate it.

In fact it generates:


call _mcount

instead of


call mcount

The ELF binary format does not prepend the '_' (underscore) when compiling C to asm functions (don't know for sure, but it is likely to be true), so you will end up with a lot of

undefined reference to `_mcount' messages.

I do not use the profiler. Anyway I think the best way to correct this bug is to modify libgcc because I do not have the stomach to put my hands on the gcc :) (see Sect. plib )

5.1 Installing binutils-2.5.2.6 Preparing gcc-2.6.2 for compilation

Unpacking the archive


cd /usr/src
tar xfvz gcc-2.6.2.tar.gz
cd gcc-2.6.2

If you are short of disk space

(Now you need 30 Mbytes to compile gcc) If you are running short of disk space, you may need to erase some unneeded documentation:


rm -f ChangeLog*
rm -f gcc.info*
rm -f cpp.info*
rm -f texinfo.tex gcc.ps

Configure the gcc-2.6.2

If you have an i386:


./configure --with-elf i386-linux

for a 486:


./configure --with-elf i486-linux

Edit Makefile


    
vi Makefile

at line 154 you should find:


    
prefix = /usr/local

change it to:


    
prefix = /usr/i486-linuxelf

at line 159 you should find:


    
local_prefix = /usr/local

change it to:


    
local_prefix = /usr/i486-linuxelf

to reflect the installation path we have chosen.

Configuring gcc package to use the ELF assembler/linker in libgcc2.a

compilation


ln -s /usr/i486-linuxelf/bin/as .
ln -s /usr/i486-linuxelf/bin/ld .

with these two links, we enable the xgcc (the gcc just compiled) to make use of the ELF assembler/linker.

5.2 Compiling gcc-2.6.2

do


make LANGUAGES=c CC=gcc CFLAGS="-O2 -m486 -fomit-frame-pointer -N"

Now have a long tea-time break with a friend (better if of the opposite sex) talk about something of interest ;)

5.3 Installing gcc-2.6.2

Install the binaries

do


make install LANGUAGES=c

Link the cpp to the ELF installation

We need to link it because /usr/i486-linuxelf/bin is not in our search PATH


ln -s /usr/i486-linuxelf/lib/gcc-lib/i486-linux/2.6.2/cpp /usr/i486-linuxelf/bin

Copy float.h from your gcc jump-table installation to the ELF one

enquire could not be compiled, so float.h is a zero length file, but we can get it from the jump-table installation of gcc:


cp /usr/lib/gcc-lib/i486-linux/2.6.2/include/float.h \
   /usr/i486-linuxelf/lib/gcc-lib/i486-linux/2.6.2/include/

Build some shell scripts to use the gcc-elf in a smoother way


vi /usr/bin/gcc-elf
[start]
#! /bin/sh

/usr/i486-linuxelf/bin/gcc -L/usr/i486-linuxelf/lib -B/usr/i486-linuxelf/bin/ $*
[end]


vi /usr/bin/as-elf

[start]
#! /bin/sh

/usr/i486-linuxelf/bin/as $*
[end]


vi /usr/bin/ld-elf

[start]
#!/bin/sh

/usr/i486-linuxelf/bin/ld $*
[end]


chmod 755 /usr/bin/*-elf

Set up the compiler specs to run with ELF environment


cd /usr/i486-linuxelf/lib/gcc-lib/i486-linux/2.6.2


vi specs

in the section *startfile (startup code), at line 26 you should find:


%{pg:gcrt0.o%s} %{!pg:%{p:gcrt0.o%s} %{!p:crt0.o%s}} %{static:-static}

change it to (all on one line):


%{pg:gcrt1.o%s} %{!pg:%{p:gcrt1.o%s} %{!p:crt1.o%s}} 
   %{!never:crti.o%s crtn.o%s} %{static:-static}

in section *predefines (always 'defined' by gcc), at line 35 you should find


-Dunix -Di386 -Dlinux -Asystem(unix) -Asystem(posix) -Acpu(i386) -Amachine(i386)

change it to (all on one line):


-D__ELF__ -Dunix -Di386 -Dlinux -Asystem(unix) -Asystem(posix)
         -Acpu(i386) -Amachine(i386)

and it is done.

Now if you are short of disk space you may want to


cd /usr/src
rm -rf gcc-2.6.2

5.4 Try it now!

Just to be sure everything is okay up to now:

check your gcc installation with 'gcc-elf -v', then try to compile something:


cd /tmp

edit a file called p.c


vi p.c

[start]
main()
{
printf("prova\n");
}
[end]

'prova' means 'test' in Italian :) then compile it to the object file (only to object because we still do not have the C library)


gcc-elf -v -c p.c

You should be able to see that gcc-elf invokes all the /usr/i486-linuxelf stuff.

Now to check the kind of file gcc-elf has built, do a:


file p.o

and it should say that p.o is an


ELF 32-bit LSB relocatable i386 (386 and up) Version 1

Of course you must have the ELF definition in /etc/magic file. If gcc-elf builds ELF objects, we are on the right path!

Next Chapter, Previous Chapter

Table of contents of this chapter, General table of contents

Top of the document, Beginning of this Chapter