<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  <!ENTITY % general-entities SYSTEM "../general.ent">
  %general-entities;
]>

<sect1 id="ch-system-stripping">
  <?dbhtml filename="stripping.html"?>

  <title>Stripping</title>

  <para>This section is optional.  If the intended user is not a
  programmer and does not plan to do
  any debugging of the system software, the system's size can be decreased
  by some 2 GB by removing the debugging symbols, and some unnecessary symbol table
  entries, from binaries and libraries. This causes no real inconvenience for
  a typical Linux user.</para>

  <para>Most people who use the commands mentioned below do not
  experience any difficulties. However, it is easy to make a mistake and
  render the new system unusable. So before running the
  <command>strip</command> commands, it is a good idea to make a
  backup of the LFS system in its current state.</para>

  <para>A <command>strip</command> command with the
  <parameter>--strip-unneeded</parameter> option removes all debug symbols from
  a binary or library.  It also removes all symbol table entries not normally
  needed by the linker (for static libraries) or dynamic linker (for
  dynamically linked binaries and shared libraries). Using
  <parameter>--strip-debug</parameter> does not remove symbol table entries
  that may be needed by some applications.  The difference between
  <literal>unneeded</literal> and <literal>debug</literal> is very small.
  For example, an unstripped <filename class='libraryfile'>libc.a</filename>
  is 22.4 MB. After stripping with <parameter>--strip-debug</parameter> it
  is 5.9 MB. Using <parameter>--strip-unneeded</parameter> only reduces the
  size further to 5.8 MB.</para>

  <para>The debugging symbols from selected libraries are compressed with
  <application>Zstd</application> and preserved in separate files.  That
  debugging information is needed to run regression tests with <ulink
  url='&blfs-book;/general/valgrind.html'>valgrind</ulink> or <ulink
  url='&blfs-book;/general/gdb.html'>gdb</ulink> later, in BLFS.
  </para>

  <para>Note that <command>strip</command> will overwrite the binary or library
  file it is processing.  This can crash the processes using code or data from
  the file.  If the process running <command>strip</command> is
  affected, the binary or library being stripped can be destroyed; this can
  make the system completely unusable.  To avoid this problem we copy some libraries
  and binaries into <filename class="directory">/tmp</filename>, strip them
  there, then reinstall them with the <command>install</command> command.
  (The related entry in <xref linkend="pkgmgmt-upgrade-issues"/> gives the
  rationale for using the <command>install</command> command here.)</para>

  <note><para>The ELF loader's name is ld-linux-aarch64.so.1 on
  little-endian systems and ld-linux-aarch64_be.so.1 on big-endian systems.
  The construct below selects the
  correct name for the current architecture, excluding anything ending
  with <literal>g</literal>, in case the commands below have already been
  run.</para></note>

  <important>
    <para>
      If there is any package whose version is different from the version
      specified by the book (either following a security advisory or
      satisfying personal preference), it may be necessary to update the
      library file name in <envar>save_usrlib</envar> or
      <envar>online_usrlib</envar>.
      <emphasis role='bold'>Failing to do so may render the system
      completely unusable.</emphasis>
    </para>
  </important>

<!-- also of interest are libgfortan, libgo, libgomp, and libobjc from GCC -->

<!--<screen><userinput>save_lib="ld-2.25.so libc-2.25.so libpthread-2.25.so libthread_db-1.0.so"-->
<screen><userinput>save_usrlib="$(cd /usr/lib; ls ld-linux*[^g])
             libc.so.6
             libthread_db.so.1
             libquadmath.so.&libquadmath-version;
             libstdc++.so.&libstdcpp-version;
             libitm.so.&libitm-version;
             libatomic.so.&libatomic-version;"

cd /usr/lib

for LIB in $save_usrlib; do
    objcopy --only-keep-debug --compress-debug-sections=zstd $LIB $LIB.dbg
    cp $LIB /tmp/$LIB
    strip --strip-debug /tmp/$LIB
    objcopy --add-gnu-debuglink=$LIB.dbg /tmp/$LIB
    install -vm755 /tmp/$LIB /usr/lib
    rm /tmp/$LIB
done

online_usrbin="bash find strip"
online_usrlib="libbfd-&binutils-version;.so
               libsframe.so.&libsframe-version;
               libhistory.so.&readline-soversion;
               libncursesw.so.&ncurses-version;
               libm.so.6
               libreadline.so.&readline-soversion;
               libz.so.&zlib-version;
               libzstd.so.&zstd-version;
               $(cd /usr/lib; find libnss*.so* -type f)"

for BIN in $online_usrbin; do
    cp /usr/bin/$BIN /tmp/$BIN
    strip --strip-debug /tmp/$BIN
    install -vm755 /tmp/$BIN /usr/bin
    rm /tmp/$BIN
done

for LIB in $online_usrlib; do
    cp /usr/lib/$LIB /tmp/$LIB
    strip --strip-debug /tmp/$LIB
    install -vm755 /tmp/$LIB /usr/lib
    rm /tmp/$LIB
done

for i in $(find /usr/lib -type f -name \*.so* ! -name \*dbg) \
         $(find /usr/lib -type f -name \*.a)                 \
         $(find /usr/{bin,sbin,libexec} -type f); do
    case "$online_usrbin $online_usrlib $save_usrlib" in
        *$(basename $i)* )
            ;;
        * ) strip --strip-debug $i
            ;;
    esac
done

unset BIN LIB save_usrlib online_usrbin online_usrlib
</userinput></screen>

  <para>A large number of files will be flagged as errors because their file
  format is not recognized. These warnings can be safely ignored. They
  indicate that those files are scripts, not binaries.</para>

</sect1>