Skip to content

Instantly share code, notes, and snippets.

@cattyhouse
Forked from krasCGQ/build-cpio
Created January 19, 2024 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cattyhouse/a57f6ab0ccf590dc75d627ca24546697 to your computer and use it in GitHub Desktop.
Save cattyhouse/a57f6ab0ccf590dc75d627ca24546697 to your computer and use it in GitHub Desktop.
Scripts to build static-PIE binary of the following; only works on Alpine Linux and other Musl libc based Distros, as some may segfault when done with Glibc.
#!/usr/bin/env dash
#
# SPDX-License-Identifier: Unlicense
#
CC=clang
LD=ld.lld
STRIP=llvm-strip
# -fuse-ld= tells compiler to use specific linker above
USE_LD=$(echo $LD | cut -d. -f2)
# Limits to at least Intel Nehalem and newer
ARCH=x86-64-v2
# Optimize binary for this specific CPU
TUNE=skylake
# Compiler optimizations and hardenings
CFLAGS="-O3 -g0 -fcf-protection=full -fstack-clash-protection -fstack-protector-all"
# LLVM-specific hardening, automatically init values with zeroes
CFLAGS="$CFLAGS -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang"
# Disable unused command line warning (happens during final link)
CFLAGS="$CFLAGS -Wno-unused-command-line-argument"
# Linker optimization and static-PIE linking
LDFLAGS="-Wl,-O3 -static-pie"
# Removes all symbols
STRIPFLAGS="-s"
# Comment: Probably --disable-rpath does nothing to static build?
./configure \
CC=$CC \
CFLAGS="-march=$ARCH -mtune=$TUNE $CFLAGS" \
LDFLAGS="-fuse-ld=$USE_LD $LDFLAGS" \
--prefix=/usr \
--disable-rpath
# Remove unused declaration to fix static linking
grep program_name src/global.c && sed /program_name/d -i src/global.c
make -j"$(nproc --all)"
$STRIP $STRIPFLAGS src/cpio
ls -l src/cpio
sha256sum src/cpio
#!/usr/bin/env dash
#
# SPDX-License-Identifier: Unlicense
#
CC=clang
LD=ld.lld
STRIP=llvm-strip
# -fuse-ld= tells compiler to use specific linker above
USE_LD=$(echo $LD | cut -d. -f2)
# Limits to at least Intel Nehalem and newer
ARCH=x86-64-v2
# Optimize binary for this specific CPU
TUNE=skylake
# Compiler optimizations and hardenings
CFLAGS="-O3 -g0 -fcf-protection=full -fstack-clash-protection -fstack-protector-all"
# LLVM-specific hardening, automatically init values with zeroes
CFLAGS="$CFLAGS -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang"
# Linker optimization and static-PIE linking
LDFLAGS="-Wl,-O3 -static-pie"
# Removes all symbols
STRIPFLAGS="-s"
# shellcheck disable=SC2086
$CC -march=$ARCH -mtune=$TUNE $CFLAGS \
-fuse-ld="$USE_LD" $LDFLAGS \
dtbtool.c -o dtbToolLineage
$STRIP $STRIPFLAGS dtbToolLineage
ls -l dtbToolLineage
sha256sum dtbToolLineage
#!/usr/bin/env dash
#
# SPDX-License-Identifer: Unlicense
#
CC=clang
LD=ld.lld
STRIP=llvm-strip
# -fuse-ld= tells compiler to use specific linker above
USE_LD=$(echo $LD | cut -d. -f2)
# Limits to at least Intel Nehalem and newer
ARCH=x86-64-v2
# Optimize binary for this specific CPU
TUNE=skylake
# Compiler optimizations and hardenings
CFLAGS="-O3 -g0 -fcf-protection=full -fstack-clash-protection -fstack-protector-all"
# LLVM-specific hardening, automatically init values with zeroes
CFLAGS="$CFLAGS -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang"
# Disable unused command line warning (happens during final link)
CFLAGS="$CFLAGS -Wno-unused-command-line-argument"
# Defaults as specified in Makefile
CPPFLAGS="-Ilibfdt -I. -DFDT_ASSUME_MASK=0"
# Disable linking with libyaml and valgrind
CPPFLAGS="$CPPFLAGS -DNO_VALGRIND -DNO_YAML"
# Linker optimization and static-PIE linking
LDFLAGS="-Wl,-O3 -static-pie"
# Removes all symbols
STRIPFLAGS="-s"
# Fix building/linking of the AOSP fork (upstream dtc isn't affected)
grep dtc-parser.h dtc-lexer.l && sed s/dtc-parser/dtc-parser.tab/ -i dtc-lexer.l
make -j"$(nproc --all)" \
CC=$CC \
CPPFLAGS="$CPPFLAGS" \
EXTRA_CFLAGS="-march=$ARCH -mtune=$TUNE $CFLAGS" \
LDFLAGS="-fuse-ld=$USE_LD $LDFLAGS" \
dtc
$STRIP $STRIPFLAGS dtc
ls -l dtc
sha256sum dtc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment