Wheels, reinventing. (Was Re: [uClibc] Understanding what the build wrapper does...)

Rob Landley rob at landley.net
Fri Sep 5 16:56:34 MDT 2003

On Friday 05 September 2003 01:43, Erik Andersen wrote:
> On Wed Sep 03, 2003 at 10:44:34PM -0400, Rob Landley wrote:
> > In my continuing efforts to reinvent the wheel (or at least make a gcc
> > command line that cross-compiles hello.c against uclibc without the
> > wrapper, so I can understand what the heck is going on.)
> >
> > I got to this point:
> >
> > ---
> > #/bin/sh
> >
> > DIR=~/pending/sub
> > GCCDIR==`gcc -v 2>&1 | head -n 1`
> > GCCDIR=${GCCDIR:0:$[${#GCCDIR}-6]}
> > echo $GCCDIR
> >
> > gcc -nostdlib -L$DIR/lib -Wl,--dynamic-linker,/lib/ld-uClibc.so.0 \
> >     -Wl,--rpath-link,$DIR/lib -nostdinc -isystem $DIR/usr/include \
> >     -iwithprefix include $DIR/usr/lib/crti.o $GCCDIR/crtbegin.o \
> >     $DIR/usr/lib/crt1.o hello.c -lc -lgcc $GCCDIR/crtend.o
> > $DIR/usr/lib/crtn.o
> >
> > ---
> >
> > $DIR is where I've loopback mounted root_fs-i386 for uClibc-0.9.20, and
> > GCCDIR parses out the install path of gcc on the host system so I can
> > explicitly tell it where the heck things live.
> I think you are rather close.  What I find most helpful is trying
> to compile the same program (i.e. hello.c) vs glibc which
> supplying the "-v" argument to gcc, which causes it to print out
> _exactly_ what it is doing.  Then (after supplying -nostdlib and
> -nostdinc) you will want to do exactly the same thing, but
> swapping the paths out so it uses uClibc.  We had to use the "-v"
> option a lot to get the compiler wrapper to do everything it is
> supposed to do.

-v is nice to know, but not remarkably helpful.  It's using collect2 (which 
has no man page) rather than ld to do the linking.

Through an hour of brute force and man mage searches, and learning to use the 
nm command, and looking through source code, I finally got it to work with 
this pair of command lines:

gcc -c hello.c
ld --verbose -o hello -nostdlib --dynamic-linker /lib/ld-uClibc.so.0 \
--library-path $DIR/lib --rpath-link $DIR/lib hello.o $DIR/usr/lib/crt0.o -lc

It's 2201 bytes dynamically linked, and I put it in the chroot root_fs thingy 
and it actually ran, too!

BUT: to do it, I had to create a symlink in the mounted root_fs:
cd $DIR/lib
ln -s libc.so.0 libc.so

I don't know why I needed to do this, I just know that this is what ld was 
looking for when I told it "--library-path $DIR/lib -lc".  It wouldn't look 
at the "libc.so.0" version, it wanted "libc.so"

Am I missing something?  Should that symlink already have been there?  Is my 
copy of ld broken or something?  (Red Hat 9 stock compiler toybox...)

I'm confused, but this is actually normal for me...


P.S. _THAT_ was a pain.  Now, to get a cross compiled root environment ala lfs 
chapter 5 built from a shell script... :)

More information about the uClibc mailing list