
all: elf/boa-mips-mini

.PHONY: clean
clean:
	rm -fv dectest32 dectest64 rec

elf/boa-mips-mini: mk-boa-mips-mini.py
	python3 mk-boa-mips-mini.py $@

dectest32: dectest.c types.h
	gcc -m32 -fno-pic -no-pie -fno-omit-frame-pointer -o dectest32 -O0 dectest.c

dectest64: dectest.c types.h
	gcc -m64 -fno-pic -no-pie -fno-omit-frame-pointer -o dectest64 -O0 dectest.c

rec: rec.c types_rec.h
	gcc -m64 -fno-pic -no-pie -fno-omit-frame-pointer -o rec -O0 rec.c

strings: strings.c
	gcc -m64 -fno-pic -no-pie -fno-omit-frame-pointer -o strings -O0 strings.c

# unmapped __stack_chk_fail/guard make the canary-fail bl an unresolvable noreturn thunk, like stripped firmware
elf/arm-stackchk: arm-stackchk.c
	arm-linux-gnueabi-gcc -march=armv7-a -O1 -fstack-protector-all -nostdlib -nostartfiles \
		-Wl,-e,checkval -Wl,--defsym,__stack_chk_fail=0x00bad000 -Wl,--defsym,__stack_chk_guard=0x00bad100 \
		-o elf/arm-stackchk arm-stackchk.c

elf/arm64-tailcall-local: arm64-tailcall-local.c
	aarch64-linux-gnu-gcc -O2 -fPIC -nostdlib -nostartfiles -shared -fno-stack-protector \
		-o elf/arm64-tailcall-local.so arm64-tailcall-local.c

elf/join-hfa-arm64: join-hfa-arm64.c
	aarch64-linux-gnu-gcc -O2 -c \
		-fno-stack-protector \
		-fno-asynchronous-unwind-tables \
		-fno-unwind-tables \
		-fno-ident \
		-o elf/join-hfa-arm64 join-hfa-arm64.c

elf/function-pointer-field-arm64: function-pointer-field-arm64.c
	aarch64-linux-gnu-gcc -O2 -c \
		-fno-stack-protector \
		-fno-asynchronous-unwind-tables \
		-fno-unwind-tables \
		-fno-ident \
		-o elf/function-pointer-field-arm64 \
		function-pointer-field-arm64.c

# -O1 on purpose: at -O2 gcc tail-calls the formatter, and a tail branch is not
# R_ANAL_OP_TYPE_CALL, so the vararg pass would silently never fire. Fortify off keeps
# the callee at the plain libc name rather than __snprintf_chk. The arm64 and ppc64
# varargs fixtures predate this recipe: they are -O0 and are not rebuilt from this source.
elf/x86_64-varargs.so: varargs.c
	x86_64-linux-gnu-gcc -shared -fPIC -O1 -U_FORTIFY_SOURCE \
		-Wl,-z,lazy,-z,norelro,-z,max-page-size=0x1000,--build-id=none \
		-o $@ varargs.c

elf/arm64-noreturn-etrel.o: arm64-noreturn-etrel.c
	aarch64-linux-gnu-gcc -O2 -c \
		-fstack-protector-all \
		-fno-asynchronous-unwind-tables \
		-fno-unwind-tables \
		-fno-ident \
		-fcf-protection=none \
		-o $@ arm64-noreturn-etrel.c
	aarch64-linux-gnu-strip --strip-debug $@

elf/noreturn-prototypes-x86_64.o: noreturn-prototypes.c
	x86_64-linux-gnu-gcc -O2 -c \
		-fno-stack-protector \
		-fno-asynchronous-unwind-tables \
		-fno-unwind-tables \
		-fno-ident \
		-o $@ noreturn-prototypes.c

# ccode regressions; built with clang's cross target so no full cross gcc is needed
elf/x86_64-pthread-lock.so: pthread-lock.c
	clang --target=x86_64-unknown-linux-gnu -shared -nostdlib -fuse-ld=lld -o $@ pthread-lock.c

elf/x86_64-cast-test.o: cast-test.c
	clang --target=x86_64-unknown-linux-gnu -O0 -c -o $@ cast-test.c
