Skip to content

Instantly share code, notes, and snippets.

@ljmccarthy
Created September 27, 2023 14:37
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 ljmccarthy/eca3aa556c410f9941e82da70b468937 to your computer and use it in GitHub Desktop.
Save ljmccarthy/eca3aa556c410f9941e82da70b468937 to your computer and use it in GitHub Desktop.
Run C programs for fun!
#!/bin/sh
#
# Run C programs for fun!
#
# Disables all optimizations, enables warnings, debug info and some hardening options.
#
if [ $# -lt 1 ]; then
echo "usage: $0 FILE.c"
exit 1
fi
if [ -z "$CC" ]; then
CC=gcc
fi
SRC="$1"
DST="$(mktemp "$TMPDIR/${SRC%.*}-XXXXXXXX")"
shift
GCC_O0_OPTS_DISABLED=$("$CC" -Q -O0 --help=optimizers | awk -v ORS=" " '($1 ~ /^-f/) && ($2 ~ /enabled/) {print "-fno-" substr($1,3)}')
"$CC" -pipe -O0 -ggdb -std=c17 -Wall -Wextra -Wpedantic \
-D_FORTIFY_SOURCE=3 -fPIE -pie -fstack-protector-strong -fstack-clash-protection $GCC_O0_OPTS_DISABLED \
-o "$DST" "$SRC" && "$DST" "$@"
retval=$?
rm -f "$DST"
exit $retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment