-rw-r--r-- 4642 saferewrite-20260201/README-elfulator raw
The following line in ./compilers will be skipped by default:
c sparc32 elfulator: diet-sparc32 sparc-linux-gcc -Os
Here's how to make this line work, along with
c sparc64 elfulator: diet-sparc64 sparc64-linux-gcc -Os
c arm32 elfulator: diet-arm32 arm-linux-gnueabi-gcc -Os
c arm64 elfulator: diet-arm64 aarch64-linux-gnu-gcc -Os
if you want to try those via elfulator. Beware that unrolling via
elfulator increases costs; see README-resources.
These instructions assume that you have already followed steps 1 through
3 of README and are in the saferewrite directory.
Set up environment variables to use below:
VENV=saferewrite
BUILDROOT=buildroot-2025.11.1
DIETLIBC=dietlibc-0.35
export LIBRARY_PATH=$HOME/unicorn/build
export LD_LIBRARY_PATH=$HOME/unicorn/build
export CPATH=$HOME/unicorn/include
Download and run buildroot to compile sparc-linux-gcc and
sparc64-linux-gcc (for sparc64 it is also possible to use Debian
packages, but compiling with buildroot seems more future-proof):
( cd
wget https://buildroot.org/downloads/$BUILDROOT.tar.xz
tar -xf $BUILDROOT.tar.xz
cd $BUILDROOT
echo BR2_sparc=y > configs/sparc_defconfig
echo BR2_sparc64=y > configs/sparc64_defconfig
make sparc_defconfig O=build-sparc
time make O=build-sparc
make sparc64_defconfig O=build-sparc64
time make O=build-sparc64
)
Link the cross-compilers into the saferewrite environment:
ln -s $HOME/$BUILDROOT/build-sparc/host/bin/sparc-* $HOME/.virtualenvs/$VENV/bin/
ln -s $HOME/$BUILDROOT/build-sparc64/host/bin/sparc64-* $HOME/.virtualenvs/$VENV/bin/
Test cross-compiling and running a small program:
( echo '#include <unistd.h>'
echo 'int main() { write(1,"works\n",6); return 0; }'
) > ok.c
sparc-linux-gcc -o ok ok.c -static
./ok
sparc64-linux-gcc -o ok ok.c -static
./ok
If you see "sparc-linux-gcc: command not found" and "sparc64-linux-gcc:
command not found", switch to the saferewrite environment---
. $HOME/.virtualenvs/$VENV/bin/activate
---and then run the test again. Each "./ok" run should print "works".
Download and compile dietlibc (plus a SPARC patch) for various
architectures, with amd64 first:
( cd
wget https://cr.yp.to/mirror/www.fefe.de/dietlibc/$DIETLIBC.tar.xz
tar -xf $DIETLIBC.tar.xz
cd $DIETLIBC
sed -i 's/glob /globl /' sparc/memcmp.S
time make -j8
time make -j8 ARCH=arm CROSS=arm-linux-gnueabi- CFLAGS='-Os -static' all
time make -j8 ARCH=aarch64 CROSS=aarch64-linux-gnu- CFLAGS='-Os -static' all
time make -j8 ARCH=sparc CROSS=sparc-linux- CFLAGS='-Os -static' all
time make -j8 ARCH=sparc64 CROSS=sparc64-linux- CFLAGS='-Os -static -Wno-implicit-function-declaration' all
)
Link the diet program into the saferewrite environment:
ln -s $HOME/$DIETLIBC/bin-x86_64/diet $HOME/.virtualenvs/$VENV/bin/diet-amd64
ln -s $HOME/$DIETLIBC/bin-arm/diet $HOME/.virtualenvs/$VENV/bin/diet-arm32
ln -s $HOME/$DIETLIBC/bin-aarch64/diet $HOME/.virtualenvs/$VENV/bin/diet-arm64
ln -s $HOME/$DIETLIBC/bin-sparc/diet $HOME/.virtualenvs/$VENV/bin/diet-sparc32
ln -s $HOME/$DIETLIBC/bin-sparc64/diet $HOME/.virtualenvs/$VENV/bin/diet-sparc64
Test cross-compiling and running with dietlibc:
diet-amd64 gcc -o ok ok.c
./ok
diet-arm32 arm-linux-gnueabi-gcc -o ok ok.c
./ok
diet-arm64 aarch64-linux-gnu-gcc -o ok ok.c
./ok
diet-sparc32 sparc-linux-gcc -o ok ok.c
./ok
diet-sparc64 sparc64-linux-gcc -o ok ok.c
./ok
Each "./ok" run should print "works".
Download unicorn, switch to the version that these instructions were
tested with, apply a patch, and compile:
cp unicorn-patch $HOME
( cd
git clone https://github.com/unicorn-engine/unicorn.git
cd unicorn
git checkout 8a2846369c4070c948d8b1d3b84069de4a686b1c
patch -p1 < ../unicorn-patch
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
time make -j8
)
In the saferewrite directory, compile elfulator:
gcc -Os -o elfulator elfulator.c setjmp.s -lunicorn
Test running dietlibc cross-compiled executables under elfulator:
diet-amd64 gcc -o ok ok.c
./elfulator ok `wc -c < ok`
diet-arm32 arm-linux-gnueabi-gcc -o ok ok.c
./elfulator ok `wc -c < ok`
diet-arm64 aarch64-linux-gnu-gcc -o ok ok.c
./elfulator ok `wc -c < ok`
diet-sparc32 sparc-linux-gcc -o ok ok.c
./elfulator ok `wc -c < ok`
diet-sparc64 sparc64-linux-gcc -o ok ok.c
./elfulator ok `wc -c < ok`
Each run should again print "works".