Skip to content

Instantly share code, notes, and snippets.

@plowsec
Last active April 17, 2025 08:42
Show Gist options
  • Save plowsec/ec792dd49f5fa7f12bcab6582b5e42a2 to your computer and use it in GitHub Desktop.
Save plowsec/ec792dd49f5fa7f12bcab6582b5e42a2 to your computer and use it in GitHub Desktop.
Dockerfile to statically build ltrace

Dockerfile:

FROM i386/alpine:3.18 as build-env

ENV LTRACE_DIR=/build/ltrace
ENV LIBELF_DIR=/build/libelf
ENV BUILD_PREFIX=/usr/local

# Install tools and dependencies 
RUN apk update && apk add --no-cache \
    build-base \
    autoconf \
    automake \
    libtool \
    git \
    wget \
    pkgconf \
    zlib-dev \
    zlib-static \
    bzip2-dev \
    bzip2-static \
    xz-dev \
    xz-static \
    linux-headers \
    findutils

WORKDIR /build

# --- Build libelf from source ---
RUN git clone https://github.com/WolfgangSt/libelf.git ${LIBELF_DIR}

# Create the missing sys_elf.h file in the source directory
RUN cd ${LIBELF_DIR}/lib && \
    echo '#include <elf.h>' > sys_elf.h

RUN cd ${LIBELF_DIR} && \
    autoreconf -fvi && \
    ./configure \
        --prefix=${BUILD_PREFIX} \
        --disable-shared \
        --enable-static \
        --disable-nls && \
    make && \
    make install

# --- Build ltrace using the custom libelf ---
RUN git clone --depth 1 https://github.com/dkogan/ltrace.git ${LTRACE_DIR}

RUN cd ${LTRACE_DIR} && \
    ./autogen.sh && \
    CFLAGS="-Wno-error" \
    CPPFLAGS="-I/build/libelf/lib -D__LIBELF_INTERNAL__" \
    LDFLAGS="-L/build/libelf/lib -static -static-libgcc" \
    LIBS="-lelf" \
    ./configure \
        --host=i686-pc-linux-gnu \
        --build=i686-pc-linux-gnu \
        --disable-shared && \
    sed -i 's/^AM_LDFLAGS = /AM_LDFLAGS = -all-static /' Makefile && \
    make

# check static linking
RUN file ${LTRACE_DIR}/ltrace

# minimal image
FROM scratch
COPY --from=build-env /build/ltrace/ltrace /ltrace
ENTRYPOINT ["/ltrace"]

Build instructions

docker build --platform linux/386 -t ltrace-debug .
docker create --name ltrace-extract ltrace-debug
docker cp ltrace-extract:/build/ltrace/ltrace ./ltrace
docker rm ltrace-extract
chmod +x ./ltrace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment