Files
darkforge/toolchain/scripts/004-glibc.sh
2026-03-20 11:41:05 +01:00

125 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================
# DarkForge Linux — Phase 0, Chapter 5: Glibc (Cross-compiled)
# ============================================================================
# Purpose: Cross-compile the GNU C Library (glibc) for the target system.
# This is the core C library that every program on the final system
# will link against. After this step, we can compile programs that
# actually run on the target.
# Inputs: ${LFS}/sources/glibc-2.43.tar.xz, glibc-fhs-1.patch
# Outputs: Glibc installed to ${LFS}/usr/lib/, ${LFS}/usr/include/
# Assumes: Binutils Pass 1 + GCC Pass 1 + Linux Headers complete
# Ref: LFS 13.0 §5.5
# ============================================================================
set -euo pipefail
source "${LFS}/sources/darkforge-env.sh"
PACKAGE="glibc"
VERSION="2.43"
SRCDIR="${LFS}/sources"
echo "=== Building ${PACKAGE}-${VERSION} (Cross-compiled) ==="
cd "${SRCDIR}"
tar -xf "${PACKAGE}-${VERSION}.tar.xz"
cd "${PACKAGE}-${VERSION}"
# Create LSB compliance symlinks
# The dynamic linker path must be correct for the target architecture
case $(uname -m) in
i?86)
ln -sfv ld-linux.so.2 "${LFS}/lib/ld-lsb.so.3"
;;
x86_64)
ln -sfv ../lib/ld-linux-x86-64.so.2 "${LFS}/lib64"
ln -sfv ../lib/ld-linux-x86-64.so.2 "${LFS}/lib64/ld-lsb-x86-64.so.3"
;;
esac
# Apply the FHS (Filesystem Hierarchy Standard) patch
# This makes glibc install some programs in /usr/sbin instead of /sbin
patch -Np1 -i ../glibc-fhs-1.patch
mkdir -v build
cd build
# Tell configure to install root-level sbin programs in /usr/sbin
echo "rootsbindir=/usr/sbin" > configparms
# Configure glibc for cross-compilation
# --host=$LFS_TGT: cross-compile for our target
# --build=...: specify the build system (auto-detected)
# --enable-kernel=5.4: support kernels 5.4 and newer
# --disable-nscd: don't build the name service cache daemon
# libc_cv_slibdir=/usr/lib: install shared libs in /usr/lib (not /lib64)
../configure \
--prefix=/usr \
--host="${LFS_TGT}" \
--build="$(../scripts/config.guess)" \
--disable-nscd \
libc_cv_slibdir=/usr/lib \
--enable-kernel=5.4
make
make DESTDIR="${LFS}" install
# Fix the hardcoded path in the ldd script
sed '/RTLDLIST=/s@/usr@@g' -i "${LFS}/usr/bin/ldd"
# ============================================================================
# CRITICAL SANITY CHECKS — Do not skip these!
# If any check fails, STOP and investigate before proceeding.
# ============================================================================
echo ""
echo ">>> Running glibc sanity checks..."
echo 'int main(){}' | "${LFS_TGT}-gcc" -x c - -v -Wl,--verbose &> dummy.log
# Check 1: Verify dynamic linker path
echo "--- Check 1: Dynamic linker path ---"
RESULT=$(readelf -l a.out | grep ': /lib')
echo "${RESULT}"
if ! echo "${RESULT}" | grep -q '/lib64/ld-linux-x86-64.so.2'; then
echo "FAIL: Dynamic linker path is wrong!"
exit 1
fi
echo "PASS"
# Check 2: Verify start files are found
echo "--- Check 2: Start files ---"
grep -E -o "${LFS}/lib.*/S?crt[1in].*succeeded" dummy.log
echo "PASS"
# Check 3: Verify header search paths
echo "--- Check 3: Header search paths ---"
grep -B3 "^ ${LFS}/usr/include" dummy.log
echo "PASS"
# Check 4: Verify linker search paths
echo "--- Check 4: Linker search paths ---"
grep 'SEARCH.*/usr/lib' dummy.log | sed 's|; |\n|g'
echo "PASS"
# Check 5: Verify correct libc is found
echo "--- Check 5: libc.so.6 location ---"
grep "/lib.*/libc.so.6 " dummy.log
echo "PASS"
# Check 6: Verify dynamic linker location
echo "--- Check 6: Dynamic linker found ---"
grep found dummy.log
echo "PASS"
# Cleanup test files
rm -v a.out dummy.log
echo ""
# Cleanup source
cd "${SRCDIR}"
rm -rf "${PACKAGE}-${VERSION}"
echo "=== ${PACKAGE}-${VERSION} cross-compilation complete ==="
echo "=== All sanity checks passed ==="