# CC = gcc
CFLAGS = -g -zexecstack
SFLASG = 
SRC = $(wildcard *.c) $(wildcard *.s)
TGT = $(subst .c,,$(subst. .S,,$(SRC)))
BUILD_DIR = build
DUMP_DIR = dump


pframe:
	curl -o pframe.tgz  https://dept-info.labri.fr/~thibault/SecuLog/pframe.tgz && \
		tar -xf pframe.tgz &&\
		rm -rf pframe.tgz

.gdbinit:

configure: pframe .gdbinit
	$(shell echo "python import pframe" > .gdbinit) 

$(BUILD_DIR)/%: %.c
	$(shell mkdir -p $(BUILD_DIR))
	$(CC) $(CFLAGS) -o $@ $<

$(BUILD_DIR)/%: %.S
	$(shell mkdir -p $(BUILD_DIR))
	$(CC) -g $< -o $@ -static -nostdlib

build: $(addprefix $(BUILD_DIR)/, $(TGT))

PHONY: gdb
gdb: build/shellcode configure
	PYTHONPATH=${PWD}/pframe${PYTHONPATH:+:${PYTHONPATH}} \
		   setarch -R gdb ./$(BUILD_DIR)/shellcode --command=shellcode.gdb

PHONY: clean
clean:
	@rm -rf $(BUILD_DIR) pframe .gdbinit opcode.txt
