Enhance Dockerfile.optimized to include binutils for objcopy command and improve UPX compression strategy. Added steps to remove residual padding and compare compression results, ensuring the smallest binary size.

This commit is contained in:
2025-07-05 05:28:15 +00:00
parent 6fd3eac4b4
commit 0babcb18ff

View File

@@ -7,7 +7,8 @@ FROM alpine:latest AS build
# musl-dev: C library headers for static linking # musl-dev: C library headers for static linking
# linux-headers: Kernel headers for system calls # linux-headers: Kernel headers for system calls
# upx: Ultimate Packer for eXecutables - binary compression tool # upx: Ultimate Packer for eXecutables - binary compression tool
RUN apk add --no-cache build-base musl-dev linux-headers upx # binutils: For objcopy command
RUN apk add --no-cache build-base musl-dev linux-headers upx binutils
# Set working directory for all subsequent operations # Set working directory for all subsequent operations
WORKDIR /src WORKDIR /src
@@ -58,10 +59,17 @@ RUN gcc -static -Os -s \
--remove-section=.note.* \ --remove-section=.note.* \
--remove-section=.eh_frame \ --remove-section=.eh_frame \
bruteforce \ bruteforce \
# UPX compression with maximum settings: # === Remove residual padding BEFORE compression === \
# --lzma: Use LZMA compression algorithm (better ratio than default) && objcopy --pad-to=0 --gap-fill=0 bruteforce bruteforce.pad \
# --ultra-brute: Try all compression methods and use the best result && mv bruteforce.pad bruteforce \
&& upx --lzma --ultra-brute bruteforce # === UPX variant shoot-out: keep smaller of two compressions === \
&& cp bruteforce bruteforce.ultra && upx --ultra-brute bruteforce.ultra \
&& cp bruteforce bruteforce.lzma && upx --lzma --best --no-align bruteforce.lzma \
&& if [ $(stat -c%s bruteforce.ultra) -le $(stat -c%s bruteforce.lzma) ]; then \
mv bruteforce.ultra bruteforce; rm bruteforce.lzma; \
else \
mv bruteforce.lzma bruteforce; rm bruteforce.ultra; \
fi
# Final stage: Start with completely empty image (scratch) # Final stage: Start with completely empty image (scratch)
# This ensures absolute minimum size - no OS layer at all # This ensures absolute minimum size - no OS layer at all