Hi,

Recently some discussion has come up that the binutils and gcc built
in Ch. 6 are linking to the glibc in /tools.  This was brought up by
Greg Schafer in this post

http://linuxfromscratch.org/pipermail/lfs-dev/2006-January/054970.html

I've done some analysis of the LFS build pre-utf8 and found that he's
right.  Attached is a file showing some analysis I did that stems from
Greg's analysis at DIY.  Links to Greg's original analysis and current
implementation are shown at the bottom of the attached file.

Fleshing out the sanity check shows that while the new dynamic linker
in /lib/ld-linux.so.2 is being used, linking is being done to
/tools/lib libraries and start files.  This includes
/tools/lib/libc.so.6 which is obviously not what we want since we just
installed /lib/libc.so.6 in 6.11.

I'm not going to repeat the analysis shown in the file.  The major
result, though, is that -L/usr/lib is needed to find /lib/libc.so.6
and /usr/lib/libc.so.  Further, by adding -B/usr/lib/, the glibc start
files /usr/lib/crt{1,i,n}.o are found.  The important test cases to
view in the file are 1. (current LFS implementation), 2. (current DIY
implementation), 7 and 8 (altered LFS implementations that produce the
correct linking).

Some other analysis is done of Greg's technique to drop the necessity
to keep the binutils-pass2 source and build dirs.

I'm not proposing this as the only solution.  I'm mostly regurgitating
some analysis that shows that the current LFS implementation is
flawed.  Hopefully some others with solid knowledge of the toolchain
will weigh in on this situation.  The 6.1.1 *startfile_prefix_spec
didn't work for me, but I may have done something wrong.  That's in
the analysis, too, as 3.

Anyway, I await your comments.

--
Dan
Dan Nicholson
2006-12-07

This is an analysis of the toolchain in Ch. 6 of the LFS book.  Some questions
have arisen that the gcc and binutils from 6.13 and 6.14 are linking to the
glibc in /tools.  Much of this analysis has to do with the Re-adjusting the
Toolchain section 6.12 and is based upon work done by Greg Schafer at
DIY-Linux.  The current sanity check in 6.12 is extended to show some subtle
flaws.

1. Current LFS.  Install ld and ldscripts with LIB_PATH=/usr/lib:/lib to
   tools, change /tools/lib/ld-linux.so.2 to /lib/ld-linux.so.2 in gcc specs

# cc dummy.c -specs=/tmp/specs.1 -Wl,--verbose &> foo1
# readelf -l a.out | grep "ld-linux"
      [Requesting program interpreter: /lib/ld-linux.so.2]
# grep "SEARCH_DIR" foo1
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/usr/lib"); 
SEARCH_DIR("/lib");
# grep "succeeded" foo1
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crt1.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crti.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtbegin.o succeeded
attempt to open /tmp/ccq5neP7.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libc.so 
succeeded
attempt to open /tools/lib/libc.so.6 succeeded
attempt to open /tools/lib/libc_nonshared.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtend.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crtn.o succeeded
# grep "libc.so.6" foo1
attempt to open /tools/lib/libc.so.6 succeeded
/tools/lib/libc.so.6
ld-linux.so.2 needed by /tools/lib/libc.so.6
# grep "^found.*at" foo1
found ld-linux.so.2 at /lib/ld-linux.so.2

2. Current DIY.  Symbolic link to adjusted ld, add -L/usr/lib to *link: in
   gcc specs, change ld-linux.so.2 to /lib in gcc specs.  Add -B/usr/lib/
   and -B/usr/bin/ to gcc command.

# cc dummy.c -specs=/tmp/specs.2 -B/usr/lib/ -B/usr/bin/ -Wl,--verbose &> foo2
# readelf -l a.out | grep "ld-linux"                              [Requesting 
program interpreter: /lib/ld-linux.so.2]
# grep "SEARCH_DIR" foo2
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/usr/lib"); 
SEARCH_DIR("/lib");
# grep "succeeded" foo2
attempt to open /usr/lib/crt1.o succeeded
attempt to open /usr/lib/crti.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtbegin.o succeeded
attempt to open /tmp/cccZrgil.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /usr/lib/libgcc_s.so succeeded
attempt to open /usr/lib/libc.so succeeded
attempt to open /lib/libc.so.6 succeeded
attempt to open /usr/lib/libc_nonshared.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /usr/lib/libgcc_s.so succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtend.o succeeded
attempt to open /usr/lib/crtn.o succeeded
# grep "libc.so.6" foo2
attempt to open /lib/libc.so.6 succeeded
/lib/libc.so.6
ld-linux.so.2 needed by /lib/libc.so.6
# grep "^found.*at" foo2
found ld-linux.so.2 at /lib/ld-linux.so.2

3. Old LFS.  Install adjusted ld and ldscripts to /tools, add /usr/lib/ to
   *startfile_prefix_spec: and change ld-linux.so.2 to /lib in gcc specs.

# cc dummy.c -specs=/tmp/specs.3 -Wl,--verbose &> foo3
# readelf -l a.out | grep "ld-linux"
      [Requesting program interpreter: /lib/ld-linux.so.2]
# grep "SEARCH_DIR" foo3
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/usr/lib"); 
SEARCH_DIR("/lib");
# grep "succeeded" foo3
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crt1.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crti.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtbegin.o succeeded
attempt to open /tmp/ccMVLpk5.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libc.so 
succeeded
attempt to open /tools/lib/libc.so.6 succeeded
attempt to open /tools/lib/libc_nonshared.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtend.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crtn.o succeeded
# grep "libc.so.6" foo3
attempt to open /tools/lib/libc.so.6 succeeded
/tools/lib/libc.so.6
ld-linux.so.2 needed by /tools/lib/libc.so.6
# grep "^found.*at" foo3
found ld-linux.so.2 at /lib/ld-linux.so.2

4. Current LFS w/ symbolic link to adjusted linker ld-new in /usr/bin

# cc dummy.c -specs=/tmp/specs.1 -B/usr/bin/ -Wl,--verbose &> foo4
# readelf -l a.out | grep "ld-linux"
      [Requesting program interpreter: /lib/ld-linux.so.2]
# grep "SEARCH_DIR" foo4
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/tools/lib");
# grep "succeeded" foo4
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crt1.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crti.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtbegin.o succeeded
attempt to open /tmp/cc85t5Dp.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libc.so 
succeeded
attempt to open /tools/lib/libc.so.6 succeeded
attempt to open /tools/lib/libc_nonshared.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtend.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crtn.o succeeded
# grep "libc.so.6" foo4
attempt to open /tools/lib/libc.so.6 succeeded
/tools/lib/libc.so.6
ld-linux.so.2 needed by /tools/lib/libc.so.6
# grep "^found.*at" foo4
found ld-linux.so.2 at /tools/lib/ld-linux.so.2

5. Current LFS w/ symbolic link to ld-new and -B/usr/bin/

# cc dummy.c -specs=/tmp/specs.1 -B/usr/bin/ -Wl,--verbose &> foo5
# readelf -l a.out | grep "ld-linux"                              [Requesting 
program interpreter: /lib/ld-linux.so.2]
# grep "SEARCH_DIR" foo5
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/usr/lib"); 
SEARCH_DIR("/lib");
# grep "succeeded" foo5
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crt1.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crti.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtbegin.o succeeded
attempt to open /tmp/ccm45JOy.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libc.so 
succeeded
attempt to open /tools/lib/libc.so.6 succeeded
attempt to open /tools/lib/libc_nonshared.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtend.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../crtn.o succeeded
# grep "libc.so.6" foo5
attempt to open /tools/lib/libc.so.6 succeeded
/tools/lib/libc.so.6
ld-linux.so.2 needed by /tools/lib/libc.so.6
# grep "found.*at" foo5
found ld-linux.so.2 at /lib/ld-linux.so.2

6. Current LFS w/ -B/usr/lib/

# cc dummy.c -specs=/tmp/specs.1 -B/usr/lib/ -Wl,--verbose &> foo6
# readelf -l a.out | grep "ld-linux"                              [Requesting 
program interpreter: /lib/ld-linux.so.2]
# grep "SEARCH_DIR" foo6
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/usr/lib"); 
SEARCH_DIR("/lib");
# grep "SEARCH_DIR" foo6
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/usr/lib"); 
SEARCH_DIR("/lib");
# grep "succeeded" foo6
attempt to open /usr/lib/crt1.o succeeded
attempt to open /usr/lib/crti.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtbegin.o succeeded
attempt to open /tmp/ccmS8UCx.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libc.so 
succeeded
attempt to open /tools/lib/libc.so.6 succeeded
attempt to open /tools/lib/libc_nonshared.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../libgcc_s.so 
succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtend.o succeeded
attempt to open /usr/lib/crtn.o succeeded
# grep "libc.so.6" foo6
attempt to open /tools/lib/libc.so.6 succeeded
/tools/lib/libc.so.6
ld-linux.so.2 needed by /tools/lib/libc.so.6
# grep "^found.*at" foo6
found ld-linux.so.2 at /lib/ld-linux.so.2

7. Current LFS w/ -B/usr/lib/ and -L/usr/lib (at command line)

# cc dummy.c -specs=/tmp/specs.1 -B/usr/lib/ -L/usr/lib -Wl,--verbose &> foo7
# readelf -l a.out | grep "ld-linux"
      [Requesting program interpreter: /lib/ld-linux.so.2]
# grep "SEARCH_DIR" foo7
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/usr/lib"); 
SEARCH_DIR("/lib");
# grep "succeeded" foo7
attempt to open /usr/lib/crt1.o succeeded
attempt to open /usr/lib/crti.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtbegin.o succeeded
attempt to open /tmp/ccmDvWUj.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /usr/lib/libgcc_s.so succeeded
attempt to open /usr/lib/libc.so succeeded
attempt to open /lib/libc.so.6 succeeded
attempt to open /usr/lib/libc_nonshared.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /usr/lib/libgcc_s.so succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtend.o succeeded
attempt to open /usr/lib/crtn.o succeeded
# grep "libc.so.6" foo7
attempt to open /lib/libc.so.6 succeeded
/lib/libc.so.6
ld-linux.so.2 needed by /lib/libc.so.6
# grep "^found.*at" foo7
found ld-linux.so.2 at /lib/ld-linux.so.2

8. Current LFS w/ -B/usr/lib/ and -L/usr/lib in *link: in gcc specs ( almost
   same as 2. above except ld is installed at readjust time to both /tools/bin
   and /tools/$machine/bin )

# cc dummy.c -specs=/tmp/specs.2 -B/usr/lib/ -Wl,--verbose &> foo8
# readelf -l a.out | grep "ld-linux"                              [Requesting 
program interpreter: /lib/ld-linux.so.2]
# grep "SEARCH_DIR" foo8
SEARCH_DIR("/tools/i686-pc-linux-gnu/lib"); SEARCH_DIR("/usr/lib"); 
SEARCH_DIR("/lib");
# grep "succeeded" foo8
attempt to open /usr/lib/crt1.o succeeded
attempt to open /usr/lib/crti.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtbegin.o succeeded
attempt to open /tmp/cc81FklT.o succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /usr/lib/libgcc_s.so succeeded
attempt to open /usr/lib/libc.so succeeded
attempt to open /lib/libc.so.6 succeeded
attempt to open /usr/lib/libc_nonshared.a succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/libgcc.a succeeded
attempt to open /usr/lib/libgcc_s.so succeeded
attempt to open /tools/lib/gcc/i686-pc-linux-gnu/4.0.2/crtend.o succeeded
attempt to open /usr/lib/crtn.o succeeded
# grep "libc.so.6" foo8
attempt to open /lib/libc.so.6 succeeded
/lib/libc.so.6
ld-linux.so.2 needed by /lib/libc.so.6
# grep "^found.*at" foo8
found ld-linux.so.2 at /lib/ld-linux.so.2

Conclusions:

-B/usr/lib/ is needed to find the glibc start files /usr/lib/crt(1,i,n}.o.
-L/usr/lib is needed to find the glibc libraries /usr/lib/libc.so and
   /lib/libc.so.6.

Saving of binutils-pass2 directories can be circumvented with the following
procedure.
   1. copy readjusted ld to /tools at build time with
                cp -v ld/ld-new /tools/bin
   2. at readjust time, make symlink to readjusted ld (ld-new from 1.)
                ln -sv /tools/bin/ld-new /usr/bin/ld
   3. force gcc to use this ld rather then the /tools adjusted linkers in
      /tools/bin/ld or /tools/i686-pc-linux-gnu/bin/ld by adding -B/usr/bin/
      to the gcc command

In order to use the above gcc adjustments in the following gcc and binutils,
add CC="gcc -B/usr/bin/ -B/usr/lib/" ../configure-...  This assumes that the
-L/usr/lib has been added to *link: in the usual gcc specs location in /tools.

A further enhancement can be made by specifying a temporary specs file rather
than editing the specs file in /tools.  This can be performed with
                gcc -dumpspecs | sed ... > /tmp/new_spec_file
and adding CC="gcc ... -specs=/tmp/new_spec_file".

The above procedure is used in DIY-Linux 
http://www.diy-linux.org/x86-reference-build/

See all the steps at
http://www.diy-linux.org/x86-reference-build/temptools.html#tt-binutils-pass2
http://www.diy-linux.org/x86-reference-build/chroot.html#c-readjust-toolchain
http://www.diy-linux.org/x86-reference-build/chroot.html#c-gcc
-- 
http://linuxfromscratch.org/mailman/listinfo/lfs-dev
FAQ: http://www.linuxfromscratch.org/faq/
Unsubscribe: See the above information page

Reply via email to