ld не может найти lib c при компиляции первого прохода (cross?) gcc - PullRequest
0 голосов
/ 12 января 2020

Я готовлю нативный набор инструментов для использования в пользовательской системе сборки. Я взял за основу книгу LFS 8.4. Самым большим отличием от книги является то, что я не использую символическую ссылку /tools в системе, так как не хочу касаться системы. Я написал процедуру в сценарии оболочки:

#!/bin/bash

set -euo pipefail
set -x

# prepare build dir
mkdir toolchain
cd toolchain

# mk dest dirs
mkdir -p root/tools

# Fetch sources
mkdir fetch
cd fetch
wget http://ftp.gnu.org/gnu/binutils/binutils-2.32.tar.xz
wget http://ftp.gnu.org/gnu/gcc/gcc-8.3.0/gcc-8.3.0.tar.xz
wget http://ftp.gnu.org/gnu/mpfr/mpfr-3.1.6.tar.xz
wget http://ftp.gnu.org/gnu/gmp/gmp-6.1.2.tar.xz
wget http://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
cd ..

export PATH=/achm/toolchain/root/bin:/bin:/usr/bin

# pass1 binutils
mkdir -p binutils/build
cd binutils
tar xaf ../fetch/binutils-*
cd build
../binutils-2.32/configure --prefix=/achm/toolchain/root/tools --with-sysroot=/achm/toolchain/root --with-lib-path=/achm/toolchain/root/tools/lib --target=x86_64-lfs-linux-gnu --disable-nls --disable-werror
make -j8
mkdir /achm/toolchain/root/tools/lib
ln -s lib /achm/toolchain/root/tools/lib64
make install
cd ../..

# pass 1 gcc
mkdir gcc
cd gcc
tar xaf ../fetch/gcc-*
cd gcc-*
tar xaf ../../fetch/mpfr-*
tar xaf ../../fetch/gmp-*
tar xaf ../../fetch/mpc-*
mv mpfr-* mpfr
mv gmp-* gmp
mv mpc-* mpc

for file in gcc/config/{linux,i386/linux{,64}}.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/achm/toolchain/root/tools&@g' \
      -e 's@/usr@/achm/toolchain/root/tools@g' $file.orig > $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/achm/build/toolchain/root/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig
done

#sed -e '/m64=/s/lib64/lib/' -i.orig gcc/config/i386/t-linux64 #TODO: This fails probably becouse we are on a 9p fs
cat gcc/config/i386/t-linux64 |sed -e '/m64=/s/lib64/lib/' > tmp.sed
mv tmp.sed gcc/config/i386/t-linux64

mkdir build
cd build
../configure --target=x86_64-lfs-linux-gnu --prefix=/achm/toolchain/root/tools --with-glibc-version=2.11 --with-sysroot=/achm/toolchain/root --with-newlib --without-headers --with-local-prefix=/achm/toolchain/root/tools --with-native-system-header-dir=/achm/toolchain/root/tools/include --disable-nls --disable-shared --disable-multilib --disable-decimal-float --disable-threads --disable-libatomic --disable-libgomp --disable-mpx --disable-libquadmath --disable-libssp --disable-libvtv --disable-libstdcxx --enable-languages=c,c++
make -j1

Вопросы: 1. В разделе 5.4 LFS:

Немного подкорректировав название рабочей платформы, изменив целевой триплет поля vendor с помощью переменной LFS_TGT гарантирует, что при первой сборке Binutils и G CC будет создан совместимый кросс-линкер и кросс-компилятор. Вместо создания двоичных файлов для другой архитектуры кросс-компоновщик и кросс-компилятор будут создавать двоичные файлы, совместимые с текущим оборудованием. Правильно ли я понимаю, что мы создаем кросс-компилятор, предназначенный для одной и той же машины только с разницей в косметику c в триплете?

Когда я запускаю написанный мной скрипт, он не работает при компиляции g cc (make). Компилятор первого этапа xg cc вызывает ld, который не может найти файлы, принадлежащие lib c:
$ ./toolchain.sh
(...)
make[3]: Leaving directory '/achm/toolchain/gcc/gcc-8.3.0/build/x86_64-lfs-linux-gnu/libgcc'
make[2]: Leaving directory '/achm/toolchain/gcc/gcc-8.3.0/build/x86_64-lfs-linux-gnu/libgcc'
Checking multilib configuration for libmpx...
mkdir -p -- x86_64-lfs-linux-gnu/libmpx
Configuring in x86_64-lfs-linux-gnu/libmpx
configure: creating cache ./config.cache
checking for --enable-version-specific-runtime-libs... no
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-lfs-linux-gnu
checking target system type... x86_64-lfs-linux-gnu
checking for target support for Intel MPX runtime library... yes
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for x86_64-lfs-linux-gnu-strip... /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/strip
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... mawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking for x86_64-lfs-linux-gnu-gcc... /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/xgcc -B/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/ -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/include -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/sys-include   
checking for C compiler default output file name... 
configure: error: in `/achm/toolchain/gcc/gcc-8.3.0/build/x86_64-lfs-linux-gnu/libmpx':
configure: error: C compiler cannot create executables
See `config.log' for more details.
make[1]: *** [Makefile:12021: configure-target-libmpx] Error 1
make[1]: Leaving directory '/achm/toolchain/gcc/gcc-8.3.0/build'
make: *** [Makefile:891: all] Error 2
$ /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/xgcc -B/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/ -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/include -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/sys-include
xgcc: fatal error: no input files
compilation terminated.
$ cat hello_world.c 
#include <stdio.h>
int main()
{
    printf("hello\n");
    return 0;
}
$ /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/xgcc -B/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/ -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/include -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/sys-include hello_world.c 
hello_world.c:1:10: fatal error: stdio.h: No such file or directory
 #include <stdio.h>
          ^~~~~~~~~
compilation terminated.
$ cat minimal.c 
int main()
{
    return 0;
}
$ /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/xgcc -B/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/ -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/include -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/sys-include minimal.c     
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: cannot find crt1.o: No such file or directory
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: cannot find crti.o: No such file or directory
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: cannot find -lc
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
$ /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/xgcc -B/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ -B/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/ -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/include -isystem /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/sys-include minimal.c -Wl,--verbose
GNU ld (GNU Binutils) 2.32
(...)
SEARCH_DIR("=/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib64"); SEARCH_DIR("/achm/toolchain/root/tools/lib"); SEARCH_DIR("=/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib");
(...)
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: mode elf_x86_64
attempt to open crt1.o failed
attempt to open crti.o failed
attempt to open /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/crtbegin.o succeeded
/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/crtbegin.o
attempt to open /tmp/cceUDM0P.o succeeded
/tmp/cceUDM0P.o
attempt to open /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/libgcc.so failed
attempt to open /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/libgcc.a succeeded
/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/libgcc.a
attempt to open /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/libc.so failed
attempt to open /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/libc.a failed
attempt to open /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/libc.so failed
attempt to open /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/libc.a failed
attempt to open /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/libc.so failed
attempt to open /achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/libc.a failed
attempt to open /achm/toolchain/root/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib64/libc.so failed
attempt to open /achm/toolchain/root/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib64/libc.a failed
attempt to open /achm/toolchain/root/tools/lib/libc.so failed
attempt to open /achm/toolchain/root/tools/lib/libc.a failed
attempt to open /achm/toolchain/root/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/libc.so failed
attempt to open /achm/toolchain/root/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/lib/libc.a failed
attempt to open /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/libgcc.so failed
attempt to open /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/libgcc.a succeeded
/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/libgcc.a
attempt to open /achm/toolchain/gcc/gcc-8.3.0/build/./gcc/crtend.o succeeded
/achm/toolchain/gcc/gcc-8.3.0/build/./gcc/crtend.o
attempt to open crtn.o failed
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: cannot find crt1.o: No such file or directory
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: cannot find crti.o: No such file or directory
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: cannot find -lc
/achm/toolchain/root/tools/x86_64-lfs-linux-gnu/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status

Эти файлы являются частью lib c. Поскольку lib c не была скомпилирована, следует использовать хост, но если я правильно понимаю, мы создаем кросс-компилятор.

Должен ли я просто скопировать библиотеку c в /achm/toolchain/root/tools/lib/, добавить флаг, чтобы компоновщик просматривал каталоги lib хоста, или я что-то не так делаю?
...