5.7. Glibc-2.12.2

The Glibc package contains the main C library. This library provides the basic routines for allocating memory, searching directories, opening and closing files, reading and writing files, string handling, pattern matching, arithmetic, and so on.

Approximate build time: 6.9 SBU
Required disk space: 371 MB

5.7.1. Installation of Glibc

Fix a bug that prevents Glibc from building with GCC-4.5.3:

patch -Np1 -i ../glibc-2.12.2-gcc_fix-1.patch

Add PaX support to Glibc:

patch -Np1 -i ../glibc-2.12.2-pt_pax-1.patch
patch -Np1 -i ../glibc-2.12.2-dl_execstack-1.patch
patch -Np1 -i ../glibc-2.12.2-localedef_trampoline-1.patch

The Glibc documentation recommends building Glibc outside of the source directory in a dedicated build directory:

mkdir -v ../glibc-build
cd ../glibc-build

Because Glibc no longer supports i386, its developers say to use the compiler flag -march=i486 when building it for x86 machines. There are several ways to accomplish that, but testing shows that the flag is best placed inside the build variable “CFLAGS”. Instead of overriding completely what Glibc's internal build system uses for CFLAGS, append the new flag to the existing contents of CFLAGS by making use of the special file configparms. The -mtune=native flag is also necessary to reset a reasonable value for -mtune that is changed when setting -march.

case `uname -m` in
  i?86) echo "CFLAGS += -march=i486 -mtune=native" > configparms ;;
esac

Next, prepare Glibc for compilation:

../glibc-2.12.2/configure --prefix=/tools \
    --host=$LFS_TGT --build=$(../glibc-2.12.2/scripts/config.guess) \
    --disable-profile --enable-add-ons \
    --enable-kernel=2.6.22.5 --with-headers=/tools/include \
    libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes

The meaning of the configure options:

--host=$LFS_TGT, --build=$(../glibc-2.12.2/scripts/config.guess)

The combined effect of these switches is that Glibc's build system configures itself to cross-compile, using the cross-linker and cross-compiler in /tools.

--disable-profile

This builds the libraries without profiling information. Omit this option if profiling on the temporary tools is necessary.

--enable-add-ons

This tells Glibc to use the NPTL add-on as its threading library.

--enable-kernel=2.6.22.5

This tells Glibc to compile the library with support for 2.6.22.5 and later Linux kernels. Workarounds for older kernels are not enabled.

--with-headers=/tools/include

This tells Glibc to compile itself against the headers recently installed to the tools directory, so that it knows exactly what features the kernel has and can optimize itself accordingly.

libc_cv_forced_unwind=yes

The linker installed during Section 5.4, “Binutils-2.21.1 - Pass 1” was cross-compiled and as such cannot be used until Glibc has been installed. This means that the configure test for force-unwind support will fail, as it relies on a working linker. The libc_cv_forced_unwind=yes variable is passed in order to inform configure that force-unwind support is available without it having to run the test.

libc_cv_c_cleanup=yes

Simlarly, we pass libc_cv_c_cleanup=yes through to the configure script so that the test is skipped and C cleanup handling support is configured.

During this stage the following warning might appear:

configure: WARNING:
*** These auxiliary programs are missing or
*** incompatible versions: msgfmt
*** some features will be disabled.
*** Check the INSTALL file for required versions.

The missing or incompatible msgfmt program is generally harmless. This msgfmt program is part of the Gettext package which the host distribution should provide.

The Glibc package is compiled in two stages. In the first stage only the libraries are compiled with our -fPIC enabled GCC. In the second stage the programs are compiled with additional hardening.

Preprare the configparms file to build the Glibc libraries:

cat >> configparms << "EOF"
build-programs=no
LDFLAGS.so += -Wl,--warn-shared-textrel,--fatal-warnings
EOF

Compile the libraries:

make

In this next stage hardening options are added to the compiler options. The sln program is statically linked, so the -fPIE option is reversed with the -fno-PIE option. The +link variable is redefined to link the programs with -pie, and essentially behaves like +link-pie found in the Makeconfig file, but with the addition of --warn-shared-textrel --fatal-warnings to warn us if the programs were not properly compiled.

Prepare the configparms file to build the Glibc programs:

cat > configparms << "EOF"
CC += -fPIE -fstack-protector-all -D_FORTIFY_SOURCE=2
CXX += -fPIE -fstack-protector-all -D_FORTIFY_SOURCE=2
CFLAGS-sln.c += -fno-PIC -fno-PIE
+link = $(CC) -pie -Wl,-O1 -nostdlib -nostartfiles -o $@ \
    $(sysdep-LDFLAGS) $(config-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \
    $(combreloc-LDFLAGS) $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \
    -Wl,-z,now -Wl,--warn-shared-textrel,--fatal-warnings \
    $(addprefix $(csu-objpfx),S$(start-installed-name)) \
    $(+preinit) $(+prectorS) \
    $(filter-out $(addprefix $(csu-objpfx),start.o \
    S$(start-installed-name))\
    $(+preinit) $(link-extra-libs) \
    $(common-objpfx)libc% $(+postinit),$^) \
    $(link-extra-libs) $(link-libc) $(+postctorS) $(+postinit)
EOF
case `uname -m` in
  i?86) echo "CFLAGS += -march=i486 -mtune=native" >> configparms ;;
esac

Compile the programs:

make

This package does come with a test suite, however, it cannot be run at this time because we do not have a C++ compiler yet.

[Note]

Note

The test suite also requires locale data to be installed in order to run successfully. Locale data provides information to the system regarding such things as the date, time, and currency formats accepted and output by system utilities. If the test suites are not being run in this chapter (as per the recommendation), there is no need to install the locales now. The appropriate locales will be installed in the next chapter. To install the Glibc locales anyway, use instructions from Section 6.9, “Glibc-2.12.2.”

Install the package:

make install

Details on this package are located in Section 6.9.4, “Contents of Glibc.”