make.sh: Refactor stripper

This should *actually* exclude everything we want to exclude and
*actually* strip everything to the maximum.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
This commit is contained in:
Michael Tremer
2022-02-04 16:47:38 +00:00
committed by Peter Müller
parent f35f213850
commit 19054331c5
3 changed files with 50 additions and 55 deletions

View File

@@ -29,16 +29,6 @@ VER = ipfire
THISAPP = strip THISAPP = strip
TARGET = $(DIR_INFO)/$(THISAPP) TARGET = $(DIR_INFO)/$(THISAPP)
ifeq "$(TOOLCHAIN)" "1"
SHELL = /bin/bash
STRIP = /usr/bin/strip
ROOT = $(TOOLS_DIR)
else
SHELL = $(TOOLS_DIR)/bin/bash
STRIP = $(TOOLS_DIR)/bin/strip
ROOT = /
endif
############################################################################### ###############################################################################
# Top-level Rules # Top-level Rules
############################################################################### ###############################################################################
@@ -56,18 +46,19 @@ md5 :
############################################################################### ###############################################################################
$(TARGET) : $(TARGET) :
ifeq "$(TOOLCHAIN)" "1"
# Strip everything in the toolchain
$(DIR_SRC)/src/stripper $(TOOLS_DIR)
else
# Don't strip VDR binaries, because they use a weird plugin system # Don't strip VDR binaries, because they use a weird plugin system
# which does not work when unneeded symbols get stripped from # which does not work when unneeded symbols get stripped from
# /usr/sbin/vdr. # /usr/sbin/vdr.
STRIP=$(STRIP) $(SHELL) $(DIR_SRC)/src/stripper \ $(DIR_SRC)/src/stripper / \
$(ROOT) \
--exclude=$(TOOLS_DIR) \ --exclude=$(TOOLS_DIR) \
--exclude=/dev \
--exclude=/proc \
--exclude=/sys \
--exclude=/tmp \ --exclude=/tmp \
--exclude=/usr/src \ --exclude=/usr/src \
--exclude=/usr/lib/vdr \ --exclude=/usr/lib/vdr \
--exclude=/usr/sbin/vdr \ --exclude=/usr/sbin/vdr \
--exclude=/var/tmp \ --exclude=/var/tmp \
--exclude=/usr/lib/go --exclude=/usr/lib/go
endif

View File

@@ -556,6 +556,11 @@ enterchroot() {
local PATH="${TOOLS_DIR}/ccache/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/bin" local PATH="${TOOLS_DIR}/ccache/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/bin"
# Prepend any custom changes to PATH
if [ -n "${CUSTOM_PATH}" ]; then
PATH="${CUSTOM_PATH}:${PATH}"
fi
PATH="${PATH}" chroot ${LFS} env -i \ PATH="${PATH}" chroot ${LFS} env -i \
HOME="/root" \ HOME="/root" \
TERM="${TERM}" \ TERM="${TERM}" \
@@ -695,7 +700,7 @@ lfsmake2() {
local PS1='\u:\w$ ' local PS1='\u:\w$ '
enterchroot \ enterchroot \
${EXTRA_PATH}bash -x -c "cd /usr/src/lfs && \ bash -x -c "cd /usr/src/lfs && \
make -f $* \ make -f $* \
LFS_BASEDIR=/usr/src install" \ LFS_BASEDIR=/usr/src install" \
>> ${LOGFILE} 2>&1 & >> ${LOGFILE} 2>&1 &
@@ -1693,7 +1698,7 @@ buildinstaller() {
lfsmake2 memtest lfsmake2 memtest
lfsmake2 installer lfsmake2 installer
# use toolchain bash for chroot to strip # use toolchain bash for chroot to strip
EXTRA_PATH=${TOOLS_DIR}/bin/ lfsmake2 strip CUSTOM_PATH="${TOOLS_DIR}/bin" lfsmake2 strip
} }
buildpackages() { buildpackages() {

View File

@@ -1,49 +1,58 @@
#!/usr/bin/env bash #!/usr/bin/env bash
dirs="" paths=()
excludes="/dev /proc /sys /run" excludes=()
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "${1}" in case "${1}" in
--exclude=*) --exclude=*)
excludes="${excludes} ${1#*=}" excludes+=( "!" "-path" "${1#*=}/*" )
;; ;;
*) *)
dirs="${dirs} ${1}" paths+=( "${1}" )
;; ;;
esac esac
shift shift
done done
function _strip() { function _strip() {
local file=${1} local file="${1}"
local strip="${STRIP-strip}" local args=()
local exclude l # Fetch the filetype
for exclude in ${excludes}; do local type="$(readelf -h "${file}" 2>/dev/null)"
l=${#exclude}
if [ "${file:0:${l}}" = "${exclude}" ]; then case "${type}" in
# Libraries and Relocatable binaries
*Type:*"DYN (Shared object file)"*)
args+=( "--strip-all" )
;;
# Binaries
*Type:*"EXEC (Executable file)"*)
args+=( "--strip-all" )
;;
# Static libraries
*Type:*"REL (Relocatable file)"*)
args+=( "--strip-debug" "--remove-section=.comment" "--remove-section=.note" )
;;
# Skip any unrecognised files
*)
return 0 return 0
fi ;;
done esac
# Fetch any capabilities # Fetch any capabilities
local capabilities="$(getfattr --no-dereference --name="security.capability" \ local capabilities="$(getfattr --no-dereference --name="security.capability" \
--absolute-names --dump "${file}" 2>/dev/null)" --absolute-names --dump "${file}" 2>/dev/null)"
local cmd=( "${strip}" )
case "$(file -bi ${file})" in
application/x-archive*)
cmd+=( "--strip-debug" "--remove-section=.comment" "--remove-section=.note" )
;;
*)
cmd+=( "--strip-all" )
;;
esac
echo "Stripping ${file}..." echo "Stripping ${file}..."
${cmd[*]} ${file} if ! strip "${args[@]}" "${file}"; then
return 1
fi
# Restore capabilities # Restore capabilities
if [ -n "${capabilities}" ]; then if [ -n "${capabilities}" ]; then
@@ -51,18 +60,8 @@ function _strip() {
fi fi
} }
for dir in ${dirs}; do for path in ${paths[@]}; do
# Strip shared objects. for file in $(find / -xdev "${excludes[@]}" -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 \) 2>/dev/null); do
find ${dir} -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \ _strip "${file}" || exit $?
| file -N -f - | sed -n -e 's/^\(.*\):[ ]*.*ELF.*, not stripped.*/\1/p' | done
while read file; do
_strip ${file} || exit $?
done || exit $?
# Strip static archives.
find ${dir} -name \*.a -a -exec file {} \; \
| grep 'current ar archive' | sed -n -e 's/^\(.*\):[ ]*current ar archive/\1/p' |
while read file; do
_strip ${file} || exit $?
done || exit $?
done done