#
# See the file README for details. The impatient can try
#
# ./configure.pl && make
#
# Calling make without previous configure.pl will give
# an amazing amount of uncomprehensible error messages.
#
# all: infection html_deps
all: infection scanner html_deps

scanner_entry_point_deps = src/scanner/entry_point/objdump.pl
scanner_segment_deps = $(TEVWH_TMP)/scanner/segment
scanner_filesize_deps = $(TEVWH_TMP)/scanner/filesize

include config-$(HOST).mak

calc: $(TEVWH_OUT_XML)/calc.xml
$(TEVWH_OUT_XML)/calc.xml: etc/calc.pl 
	. ./config-$(HOST).sh && etc/calc.pl "-xml=$@"

HTML = file://$(TEVWH_PWD)/_html/index.html
DVI = file://$(TEVWH_PWD)/_dvi/$(TEVWH_DIRNAME).dvi

DSL = etc/ldp.dsl
SCANNER_BIG = '' /bin /sbin /usr/bin /usr/sbin /usr/lib
SCANNER_SMALL = _infected $(TEVWH_TMP)

# .S is assembly source code
# .asm is disassember output
.SUFFIXES: .inc .gdb .S .asm

personal:
	- rm "${HOME}/.aspell.english.pws"
	sort etc/dict.txt > etc/dict.txt.tmp \
	&& mv -f etc/dict.txt.tmp etc/dict.txt
	aspell create personal < etc/dict.txt 

mkdir: ; etc/mkdir.sh | xargs mkdir -p 2> /dev/null

infection: \
	mkdir \
	packages \
	magic_elf \
	evil_magic \
	segments \
	sections \
	segment_padding \
	entry_point \
	infection_$(TEVWH_ARCH)

infection_ppc:
infection_alpha:
infection_sparc:
infection_i386: \
	additional_cs \
	doing_it_in_c  \
	stub_revisited \
	suspicious_code

scanner: \
	$(TEVWH_OUT)/scanner/file \
	scanner_entry_point \
	scanner_segment \
	scanner_filesize \
	$(TEVWH_OUT)/scanner/find-shell

packages: \
	$(TEVWH_OUT)/packages/man-all \
	$(TEVWH_OUT)/packages/man-section \
	$(TEVWH_OUT)/packages/uname \
	$(TEVWH_OUT)/packages/${TEVWH_HAVE_PATH_DISTID} \
	$(TEVWH_OUT)/packages/du \
	$(TEVWH_OUT)/packages/simple \
	packages_$(TEVWH_OS_PKG_SYS)
packages_:
packages_distid: \
	$(TEVWH_OUT)/packages/distid
packages_rpm: \
	$(TEVWH_OUT)/packages/rpm/custom \
	$(TEVWH_OUT)/packages/verify \
	$(TEVWH_OUT)/packages/verify-all
packages_deb: \
	$(TEVWH_OUT)/packages/deb/status \
	$(TEVWH_OUT)/packages/deb/perl \
	$(TEVWH_OUT)/packages/deb/hard \
	$(TEVWH_OUT)/packages/verify
packages_SunOS: \
	$(TEVWH_OUT)/packages/SunOS/version \
	$(TEVWH_OUT)/packages/verify
packages_slackware:
packages_FreeBSD: \
	$(TEVWH_OUT)/packages/FreeBSD/file_list \
	$(TEVWH_OUT)/packages/verify

magic_elf: \
	$(TEVWH_OUT)/magic_elf/magic_elf \
	$(TEVWH_OUT)/magic_elf/magic_elf_static \
	$(TEVWH_OUT)/magic_elf/strings \
	$(TEVWH_OUT)/magic_elf/od \
	$(TEVWH_OUT)/magic_elf/gdb \
	$(TEVWH_OUT)/magic_elf/addr_of_main \
	$(TEVWH_OUT)/magic_elf/other_perl \
	$(TEVWH_OUT)/magic_elf/other_exe \
	$(TEVWH_OUT)/magic_elf/objdump.asm \
	magic_elf_$(TEVWH_HAVE_PATH_HEXDUMP) \
	magic_elf_$(TEVWH_HAVE_PATH_XXD) \
	magic_elf_$(TEVWH_HAVE_PATH_NDISASM) \
	magic_elf_$(TEVWH_UNAME)
magic_elf_:
magic_elf_hexdump: \
	$(TEVWH_OUT)/magic_elf/hexdump
magic_elf_xxd: \
	$(TEVWH_OUT)/magic_elf/xxd

magic_elf_: 
magic_elf_ndisasm: \
	$(TEVWH_OUT)/magic_elf/ndisasm.asm
magic_elf_SunOS:
magic_elf_FreeBSD \
magic_elf_Linux: \
	$(TEVWH_OUT)/magic_elf/other_mem

evil_magic: \
	$(TEVWH_OUT)/evil_magic/write.gdb \
	$(TEVWH_OUT)/evil_magic/ofs_entry \
	$(TEVWH_OUT)/evil_magic/static_main.gdb \
	$(TEVWH_OUT)/evil_magic/static_write.gdb \
	$(TEVWH_OUT)/evil_magic/nm \
	$(TEVWH_OUT)/evil_magic/gdb_nm \
	$(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE) \
	$(TEVWH_OUT)/evil_magic/od \
	$(TEVWH_OUT)/evil_magic/e_entry \
	$(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE).asm \
	$(TEVWH_OUT)/evil_magic/evil_magic.c \
	$(TEVWH_OUT)/evil_magic/cc \
	$(TEVWH_OUT)/evil_magic/self_modify

segments: \
	$(TEVWH_OUT)/segments/objdump \
	segments_$(TEVWH_HAVE_PATH_READELF) \
	segments_$(TEVWH_HAVE_PATH_ELFDUMP) \
	$(TEVWH_OUT)/segments/sh/objdump
segments_:
segments_readelf: \
	$(TEVWH_OUT)/segments/readelf \
	$(TEVWH_OUT)/segments/sh/readelf
segments_elfdump: \
	$(TEVWH_OUT)/segments/elfdump

sections: \
	$(TEVWH_OUT)/sections/objdump \
	sections_$(TEVWH_HAVE_PATH_READELF) \
	sections_$(TEVWH_HAVE_PATH_ELFDUMP) \
	$(TEVWH_OUT)/sections/sh/objdump
sections_:
sections_readelf: \
	$(TEVWH_OUT)/sections/readelf \
	$(TEVWH_OUT)/sections/sh/readelf
sections_elfdump: \
	$(TEVWH_OUT)/sections/elfdump

segment_padding: \
	$(TEVWH_OUT)/segment_padding/sysconf \
	$(TEVWH_OUT)/segment_padding/objdump \
	segment_padding_$(TEVWH_HAVE_PATH_READELF) \
	$(TEVWH_OUT)/segment_padding/scan \
	$(TEVWH_OUT)/segment_padding/test-e1i1 \
	segment_padding_$(TEVWH_ARCH)
segment_padding_:
segment_padding_readelf: \
	$(TEVWH_OUT)/segment_padding/readelf
segment_padding_ppc:
segment_padding_alpha:
segment_padding_sparc:
segment_padding_i386: \
	$(TEVWH_OUT)/segment_padding/test-e2i1 \
	$(TEVWH_OUT)/segment_padding/test-e3i1

entry_point: \
	$(TEVWH_OUT)/entry_point/sh.gdb \
	$(TEVWH_OUT)/entry_point/ldd \
	entry_point_$(TEVWH_ARCH)
entry_point_alpha:
entry_point_ppc:
entry_point_sparc: 
entry_point_i386: \
	$(TEVWH_OUT)/entry_point/nm \
	$(TEVWH_OUT)/entry_point/sh.disasm \
	$(TEVWH_OUT)/entry_point/e2.disasm \
	$(TEVWH_OUT)/entry_point/e3.disasm \
	$(TEVWH_OUT)/entry_point/entry_point \

#	$(TEVWH_PRE)/entry_point/__libc_start_main \

additional_cs: \
	$(TEVWH_OUT)/additional_cs/hexdump \
	$(TEVWH_OUT)/additional_cs/test-e3i1 \
	$(TEVWH_OUT)/additional_cs/scan \
	$(TEVWH_OUT)/additional_cs/objdump \
	additional_cs_$(TEVWH_HAVE_PATH_READELF)
additional_cs_:
additional_cs_readelf: \
	$(TEVWH_OUT)/additional_cs/readelf

doing_it_in_c: \
	$(TEVWH_OUT)/doing_it_in_c/get_relocate_ofs.disasm \
	$(TEVWH_OUT)/doing_it_in_c/begin.disasm \
	$(TEVWH_OUT)/doing_it_in_c/addr \
	$(TEVWH_OUT)/doing_it_in_c/e3i2.disasm \
	$(TEVWH_OUT)/doing_it_in_c/e3i3.disasm \
	$(TEVWH_OUT)/doing_it_in_c/e3i4.disasm \
	$(TEVWH_OUT)/doing_it_in_c/test-e3i2 \
	$(TEVWH_OUT)/doing_it_in_c/test-e3i3 \
	$(TEVWH_OUT)/doing_it_in_c/test-e3i4

stub_revisited: \
	$(TEVWH_OUT)/stub_revisited/magic_elf.disasm \
	$(TEVWH_OUT)/stub_revisited/sh.disasm \
	$(TEVWH_OUT)/stub_revisited/sh_ret.disasm \
	$(TEVWH_OUT)/stub_revisited/$(TEVWH_NAME_SH)_infected.disasm \
	$(TEVWH_OUT)/stub_revisited/stack

#	$(TEVWH_PRE)/stub_revisited/__libc_start_main

suspicious_code: \
	$(TEVWH_OUT)/suspicious_code/disasm

clean:
	rm -rf config.xml config-* pre/ tmp/ out/ _* $(TEVWH_DIRNAME).junk
	find . -type f \
		-name '*.bak' \
		-o -name '*.orig' \
		-o -name '*~' \
		-o -name '*.fot' \
		-o -name '*infected' \
		-o -name '*.core' \
		-o -name 'core.*' \
		-o -name core \
		-o -name log \
	| xargs rm -f

$(TEVWH_OUT_XML)/ulink.xml: etc/ulink.pl etc/http.dat etc/ftp.dat
	etc/ulink.pl -PROTO=http -DIR=$(TEVWH_OUT_XML) etc/http.dat > $@
	etc/ulink.pl -PROTO=ftp -DIR=$(TEVWH_OUT_XML) etc/ftp.dat >> $@

html_deps: mkdir $(TEVWH_OUT_XML)/ulink.xml $(TEVWH_OUT_XML)/calc.xml

dsl: $(DSL) etc/patch_dsl.pl
	etc/patch_dsl.pl -dsl=$(DSL)

intro: html_deps
	etc/mkhtml.sh intro
platform: html_deps
	etc/mkhtml.sh platform
html: html_deps dsl intro platform

# target dvi is completely broken
dvi: html_deps
	- [ -d _dvi ] && rm -r _dvi
	mkdir _$@
	cd _$@; jw -f docbook -b dvi ../intro/main.xml

mozilla:
	mozilla $(HTML) &
netscape:
	netscape $(HTML) &
links:
	links $(HTML)
lynx:
	lynx $(HTML)
xdvi: dvi
	xdvi $(DVI)

fileref:
	. ./config-$(HOST).sh && \
	etc/fileref.pl `find intro platform -type f -name '*.xml'`

dist_html:
	tar -C .. -zcf $(TEVWH_DIRNAME)-`date "+%Y-%m-%d"`.html.tar.gz \
		$(TEVWH_DIRNAME)/_html --exclude-from=etc/tar-exclude
dist_out:
	tar -C .. -zcf $(TEVWH_OS_CODE)-`date "+%Y-%m-%d"`.out.tar.gz \
		$(TEVWH_DIRNAME)/$(TEVWH_OUT) \
		$(TEVWH_DIRNAME)/$(TEVWH_PRE) \
		$(TEVWH_DIRNAME)/config-$(HOST).csh \
		$(TEVWH_DIRNAME)/config-$(HOST).mak \
		$(TEVWH_DIRNAME)/config-$(HOST).sed \
		$(TEVWH_DIRNAME)/config-$(HOST).sh \
		$(TEVWH_DIRNAME)/config-$(HOST).xml \
		--exclude-from=etc/tar-exclude
dist_src: clean
	tar -C .. -zcf $(TEVWH_DIRNAME)-`date "+%Y-%m-%d"`.tar.gz \
		${TEVWH_DIRNAME} --exclude-from=etc/tar-exclude
dist: dist_html dist_out dist_src

$(TEVWH_OUT)/packages/man-all: \
	$(TEVWH_PRE)/packages/man-all/$(TEVWH_UNAME).sh
	$(TEVWH_PRE)/packages/man-all/$(TEVWH_UNAME).sh > $@
$(TEVWH_OUT)/packages/man-section: \
	$(TEVWH_PRE)/packages/man-section/$(TEVWH_UNAME).sh
	$? > $@
$(TEVWH_OUT)/packages/uname: \
	src/packages/uname.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/distid: \
	$(TEVWH_PRE)/packages/distid.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/du: \
	$(TEVWH_PRE)/packages/$(TEVWH_OS_PKG_SYS)/du.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/simple: \
	$(TEVWH_PRE)/packages/$(TEVWH_OS_PKG_SYS)/simple.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/verify: \
	$(TEVWH_PRE)/packages/$(TEVWH_OS_PKG_SYS)/verify.sh
	-$? > $@ 2>&1
$(TEVWH_OUT)/packages/verify-all: \
	$(TEVWH_PRE)/packages/$(TEVWH_OS_PKG_SYS)/verify-all.sh
# takes too long and produces too much output, so never call it
#	-$? > $@ 2>&1

$(TEVWH_OUT)/packages/rpm/custom: \
	$(TEVWH_PRE)/packages/rpm/custom.sh
	$? > $@
$(TEVWH_OUT)/packages/deb/simple: \
	$(TEVWH_PRE)/packages/deb/simple.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/deb/status: \
	$(TEVWH_PRE)/packages/deb/status.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/deb/perl: \
	$(TEVWH_PRE)/packages/deb/perl.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/deb/hard: \
	$(TEVWH_PRE)/packages/deb/hard.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/SunOS/version: \
	$(TEVWH_PRE)/packages/SunOS/version.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/FreeBSD/file_list: \
	$(TEVWH_PRE)/packages/FreeBSD/file_list.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/packages/FreeBSD/verify: \
	$(TEVWH_PRE)/packages/FreeBSD/verify.sh
	$? > $@ 2>&1

$(TEVWH_TMP)/magic_elf/magic_elf \
$(TEVWH_OUT)/magic_elf/magic_elf: \
	$(TEVWH_PRE)/magic_elf/magic_elf.c \
	$(TEVWH_PRE)/magic_elf/cc.sh
	$(TEVWH_PRE)/magic_elf/cc.sh > $(TEVWH_OUT)/magic_elf/magic_elf
$(TEVWH_TMP)/magic_elf/magic_elf_static \
$(TEVWH_OUT)/magic_elf/magic_elf_static: \
	$(TEVWH_PRE)/magic_elf/magic_elf.c \
	$(TEVWH_PRE)/magic_elf/cc_static.sh
	$(TEVWH_PRE)/magic_elf/cc_static.sh \
	> $(TEVWH_OUT)/magic_elf/magic_elf_static
$(TEVWH_OUT)/magic_elf/strings: \
	$(TEVWH_TMP)/magic_elf/magic_elf \
	$(TEVWH_PRE)/magic_elf/strings.sh
	$(TEVWH_PRE)/magic_elf/strings.sh > $@
$(TEVWH_OUT)/magic_elf/od: \
	$(TEVWH_OUT)/magic_elf/magic_elf \
	$(TEVWH_PRE)/magic_elf/od/$(TEVWH_UNAME).sh
	$(TEVWH_PRE)/magic_elf/od/$(TEVWH_UNAME).sh > $@
$(TEVWH_OUT)/magic_elf/hexdump: \
	$(TEVWH_OUT)/magic_elf/magic_elf \
	$(TEVWH_PRE)/magic_elf/hexdump.sh \
	$(TEVWH_PRE)/format.hex
	- $(TEVWH_PRE)/magic_elf/hexdump.sh > $@
$(TEVWH_OUT)/magic_elf/xxd: \
	$(TEVWH_OUT)/magic_elf/magic_elf \
	$(TEVWH_PRE)/magic_elf/xxd.sh
	- $(TEVWH_PRE)/magic_elf/xxd.sh > $@
$(TEVWH_TMP)/magic_elf/addr_of_main: \
	$(TEVWH_PRE)/magic_elf/addr_of_main.c
	$(TEVWH_PATH_CC) $(TEVWH_CFLAGS) -o $@ $?
$(TEVWH_OUT)/magic_elf/addr_of_main: \
	$(TEVWH_TMP)/magic_elf/addr_of_main
	$? > $@ 2>&1
$(TEVWH_OUT)/magic_elf/ndisasm.asm: \
	$(TEVWH_OUT)/magic_elf/magic_elf \
	$(TEVWH_OUT)/magic_elf/addr_of_main \
	$(TEVWH_PRE)/magic_elf/ndisasm.sh
	$(TEVWH_PRE)/magic_elf/ndisasm.sh > $@
$(TEVWH_OUT)/magic_elf/objdump.asm: \
	$(TEVWH_TMP)/magic_elf/magic_elf \
	$(TEVWH_OUT)/magic_elf/addr_of_main \
	$(TEVWH_PRE)/magic_elf/objdump.sh \
	$(TEVWH_PRE)/magic_elf/objdump_format.pl
	$(TEVWH_PRE)/magic_elf/objdump.sh > $@
$(TEVWH_OUT)/magic_elf/gdb: \
	$(TEVWH_OUT)/magic_elf/magic_elf \
	$(TEVWH_PRE)/magic_elf/gdb.sh \
	$(TEVWH_PRE)/magic_elf/gdb_core.sh \
	$(TEVWH_PRE)/magic_elf/gdb_format.pl
	$(TEVWH_PRE)/magic_elf/gdb.sh > $@
$(TEVWH_OUT)/magic_elf/other_perl: \
	$(TEVWH_PRE)/magic_elf/other_perl.pl
	$? > $@ 2>&1
$(TEVWH_OUT)/magic_elf/other_mem: \
	$(TEVWH_PRE)/magic_elf/other_mem.sh
	$? > $@ 2>&1
$(TEVWH_OUT)/magic_elf/other_exe: \
	$(TEVWH_PRE)/magic_elf/other_exe.sh
	$? > $@ 2>&1

$(TEVWH_OUT)/evil_magic/write.gdb: \
	$(TEVWH_TMP)/magic_elf/magic_elf \
	$(TEVWH_OUT)/magic_elf/gdb \
	$(TEVWH_PRE)/magic_elf/gdb.sh \
	$(TEVWH_PRE)/magic_elf/gdb_core.sh \
	$(TEVWH_PRE)/magic_elf/gdb_format.pl \
	$(TEVWH_PRE)/evil_magic/first_gdb_func.sed \
	$(TEVWH_PRE)/evil_magic/gdb_write.sh
	$(TEVWH_PRE)/evil_magic/gdb_write.sh > $@
$(TEVWH_OUT)/evil_magic/static_main.gdb: \
	$(TEVWH_OUT)/magic_elf/magic_elf_static \
	$(TEVWH_PRE)/magic_elf/gdb.sh \
	$(TEVWH_PRE)/magic_elf/gdb_core.sh \
	$(TEVWH_PRE)/magic_elf/gdb_format.pl
	$(TEVWH_PRE)/magic_elf/gdb.sh \
		"$(TEVWH_TMP)/magic_elf/magic_elf_static" > $@
$(TEVWH_OUT)/evil_magic/static_write.gdb: \
	$(TEVWH_TMP)/magic_elf/magic_elf_static \
	$(TEVWH_OUT)/magic_elf/gdb \
	$(TEVWH_PRE)/magic_elf/gdb.sh \
	$(TEVWH_PRE)/magic_elf/gdb_core.sh \
	$(TEVWH_PRE)/magic_elf/gdb_format.pl \
	$(TEVWH_PRE)/evil_magic/first_gdb_func.sed \
	$(TEVWH_PRE)/evil_magic/static_write.sh
	$(TEVWH_PRE)/evil_magic/static_write.sh > $@
$(TEVWH_OUT)/evil_magic/nm: \
	$(TEVWH_PRE)/evil_magic/nm.sh \
	$(TEVWH_TMP)/magic_elf/magic_elf_static
	$(TEVWH_PRE)/evil_magic/nm.sh > $@
$(TEVWH_OUT)/evil_magic/gdb_nm: \
	$(TEVWH_PRE)/evil_magic/gdb_nm.sh \
	$(TEVWH_TMP)/magic_elf/magic_elf_static \
	$(TEVWH_PRE)/magic_elf/gdb_core.sh \
	$(TEVWH_PRE)/magic_elf/gdb_format.pl
	$(TEVWH_PRE)/evil_magic/gdb_nm.sh \
		$(TEVWH_TMP)/magic_elf/magic_elf_static > $@
$(TEVWH_TMP)/evil_magic/$(TEVWH_ASM_STYLE) \
$(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE): \
	src/evil_magic/$(TEVWH_ASM).S \
	$(TEVWH_PRE)/evil_magic/$(TEVWH_ASM_STYLE).sh
	$(TEVWH_PRE)/evil_magic/$(TEVWH_ASM_STYLE).sh \
		> $(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE)
$(TEVWH_OUT)/evil_magic/od: \
	$(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE) \
	$(TEVWH_PRE)/evil_magic/od.sh
	$(TEVWH_PRE)/evil_magic/od.sh > $@
$(TEVWH_TMP)/evil_magic/e_entry: \
	src/evil_magic/e_entry.c
	$(TEVWH_PATH_CC) $(TEVWH_CFLAGS) -o $@ $? 
$(TEVWH_OUT)/evil_magic/e_entry: \
	$(TEVWH_TMP)/evil_magic/$(TEVWH_ASM_STYLE) \
	$(TEVWH_TMP)/evil_magic/e_entry \
	$(TEVWH_PRE)/evil_magic/e_entry.sh
	$(TEVWH_PRE)/evil_magic/e_entry.sh > $@
$(TEVWH_TMP)/evil_magic/ofs_entry: \
	$(TEVWH_PRE)/evil_magic/ofs_entry.c
	$(TEVWH_PATH_CC) $(TEVWH_CFLAGS) -o $@ $?
$(TEVWH_OUT)/evil_magic/ofs_entry: \
	$(TEVWH_TMP)/evil_magic/ofs_entry
	$? > $@
$(TEVWH_OUT)/evil_magic/att.asm: \
	$(TEVWH_TMP)/evil_magic/$(TEVWH_ASM_STYLE) \
	$(TEVWH_PRE)/evil_magic/objdump.sh
	$(TEVWH_PRE)/evil_magic/objdump.sh > $@
$(TEVWH_OUT)/evil_magic/intel.asm: \
	$(TEVWH_TMP)/evil_magic/$(TEVWH_ASM_STYLE) \
	$(TEVWH_PRE)/evil_magic/ndisasm.sh
	$(TEVWH_PRE)/evil_magic/ndisasm.sh > $@
$(TEVWH_OUT)/evil_magic/evil_magic.c: \
	$(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE).asm \
	src/platform/disasm.pl
	src/platform/disasm.pl \
		< $(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE).asm \
		> $@
$(TEVWH_OUT)/evil_magic/cc: \
	$(TEVWH_OUT)/evil_magic/evil_magic.c \
	$(TEVWH_PRE)/evil_magic/cc.sh
	$(TEVWH_PRE)/evil_magic/cc.sh > $@ 2>&1
$(TEVWH_TMP)/evil_magic/att_func.o: \
	src/evil_magic/func/$(TEVWH_ASM).S
	$(TEVWH_PATH_CC) -c $(TEVWH_CFLAGS) -o $@ $?
$(TEVWH_OUT)/evil_magic/att_func.asm: \
	$(TEVWH_PRE)/magic_elf/objdump_format.pl \
	$(TEVWH_TMP)/evil_magic/att_func.o
	$(TEVWH_PATH_OBJDUMP) -d $(TEVWH_TMP)/evil_magic/att_func.o \
		| $(TEVWH_PRE)/magic_elf/objdump_format.pl > $@
$(TEVWH_TMP)/evil_magic/intel_func: \
	src/evil_magic/func/$(TEVWH_ASM).S
	$(TEVWH_PATH_NASM) -f bin -o $@ $?
$(TEVWH_OUT)/evil_magic/intel_func.asm: \
	$(TEVWH_TMP)/evil_magic/intel_func
	$(TEVWH_PATH_NDISASM) -U $? > $@
$(TEVWH_OUT)/evil_magic/func.inc: \
	$(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE)_func.asm \
	src/platform/disasm.pl
	src/platform/disasm.pl -identifier=in_code \
		$(TEVWH_OUT)/evil_magic/$(TEVWH_ASM_STYLE)_func.asm > $@
$(TEVWH_OUT)/evil_magic/self_modify: \
	$(TEVWH_TMP)/evil_magic/self_modify
$(TEVWH_TMP)/evil_magic/self_modify: \
	$(TEVWH_OUT)/evil_magic/func.inc \
	src/evil_magic/self_modify.c
	$(TEVWH_PATH_CC) $(TEVWH_CFLAGS) -I $(TEVWH_OUT)/evil_magic -o $@ \
		src/evil_magic/self_modify.c
$(TEVWH_OUT)/evil_magic/self_modify: \
	$(TEVWH_TMP)/evil_magic/self_modify
	$? > $@

$(TEVWH_OUT)/segments/objdump: \
	$(TEVWH_TMP)/evil_magic \
	$(TEVWH_PRE)/segments/objdump.sh
	$(TEVWH_PRE)/segments/objdump.sh > $@
$(TEVWH_OUT)/segments/readelf: \
	$(TEVWH_TMP)/evil_magic \
	$(TEVWH_PRE)/segments/readelf.sh
	$(TEVWH_PRE)/segments/readelf.sh > $@
$(TEVWH_OUT)/segments/elfdump: \
	$(TEVWH_TMP)/evil_magic \
	$(TEVWH_PRE)/segments/elfdump.sh
	$(TEVWH_PRE)/segments/elfdump.sh > $@
$(TEVWH_OUT)/sections/objdump: \
	$(TEVWH_TMP)/evil_magic \
	$(TEVWH_PRE)/sections/objdump.sh
	$(TEVWH_PRE)/sections/objdump.sh > $@
$(TEVWH_OUT)/sections/readelf: \
	$(TEVWH_TMP)/evil_magic \
	$(TEVWH_PRE)/sections/readelf.sh
	$(TEVWH_PRE)/sections/readelf.sh > $@
$(TEVWH_OUT)/sections/elfdump: \
	$(TEVWH_TMP)/evil_magic \
	$(TEVWH_PRE)/sections/elfdump.sh
	$(TEVWH_PRE)/sections/elfdump.sh > $@
$(TEVWH_OUT)/segments/sh/objdump: \
	$(TEVWH_PRE)/segments/sh/objdump.sh
	$(TEVWH_PRE)/segments/sh/objdump.sh > $@
$(TEVWH_OUT)/segments/sh/readelf: \
	$(TEVWH_PRE)/segments/sh/readelf.sh
	$(TEVWH_PRE)/segments/sh/readelf.sh > $@
$(TEVWH_OUT)/sections/sh/objdump: \
	$(TEVWH_PRE)/sections/sh/objdump.sh
	$(TEVWH_PRE)/sections/sh/objdump.sh > $@
$(TEVWH_OUT)/sections/sh/readelf: \
	$(TEVWH_PRE)/sections/sh/readelf.sh
	$(TEVWH_PRE)/sections/sh/readelf.sh > $@

$(TEVWH_TMP)/segment_padding/sysconf: \
	src/segment_padding/sysconf.c
	$(TEVWH_PATH_CC) $(TEVWH_CFLAGS) -o $@ $?
$(TEVWH_OUT)/segment_padding/sysconf: \
	$(TEVWH_TMP)/segment_padding/sysconf
	$? > $@
$(TEVWH_OUT)/segment_padding/objdump: \
	$(TEVWH_TMP)/segment_padding/e1i1/$(TEVWH_NAME_SH)_infected \
	$(TEVWH_PRE)/segment_padding/objdump.sh
	$(TEVWH_PRE)/segment_padding/objdump.sh > $@ 2>&1
$(TEVWH_OUT)/segment_padding/readelf: \
	$(TEVWH_TMP)/segment_padding/e1i1/$(TEVWH_NAME_SH)_infected \
	$(TEVWH_PRE)/segment_padding/readelf.sh
	$(TEVWH_PRE)/segment_padding/readelf.sh > $@ 2>&1
$(TEVWH_OUT)/segment_padding/scan: \
	$(TEVWH_TMP)/segment_padding/e1i1/$(TEVWH_NAME_SH)_infected \
	$(TEVWH_PRE)/segment_padding/scan_dist.sh \
	$(scanner_segment_deps)
	-$(TEVWH_PRE)/segment_padding/scan_dist.sh 2> $@
	
$(TEVWH_OUT)/scanner/file: \
	$(TEVWH_PRE)/scanner/file.sh
	$? > $@
$(TEVWH_OUT)/scanner/big.static \
$(TEVWH_OUT)/scanner/big.dynamic: \
	$(TEVWH_PRE)/scanner/find_exec.sh
	nice $? $(TEVWH_OUT)/scanner/big $(SCANNER_BIG) \
	> $(TEVWH_OUT)/scanner/big.wc
$(TEVWH_OUT)/scanner/small.static \
$(TEVWH_OUT)/scanner/small.dynamic: \
	$(TEVWH_PRE)/scanner/find_exec.sh
	nice $? $(TEVWH_OUT)/scanner/small $(SCANNER_SMALL) \
	> $(TEVWH_OUT)/scanner/small.wc
$(TEVWH_OUT)/scanner/segment/cc \
$(TEVWH_TMP)/scanner/segment: \
	$(scanner_segment_cc_deps) \
	$(TEVWH_PRE)/scanner/segment/cc.sh
	$(TEVWH_PRE)/scanner/segment/cc.sh segment \
		> $(TEVWH_OUT)/scanner/segment/cc
$(TEVWH_OUT)/scanner/filesize/cc \
$(TEVWH_TMP)/scanner/filesize: \
	$(scanner_filesize_cc_deps) \
	$(TEVWH_PRE)/scanner/segment/cc.sh
	$(TEVWH_PRE)/scanner/segment/cc.sh filesize \
		> $(TEVWH_OUT)/scanner/filesize/cc
$(TEVWH_OUT)/scanner/find-shell: \
	$(TEVWH_OUT)/scanner/segment/big.dynamic.full \
	$(TEVWH_OUT)/scanner/segment/big.static.full \
	src/scanner/find-shell.lst \
	$(TEVWH_PRE)/scanner/find-shell.sh
	$(TEVWH_PRE)/scanner/find-shell.sh > $@

$(TEVWH_OUT)/entry_point/sh.gdb: \
	$(TEVWH_PRE)/entry_point/gdb_core.sh \
	$(TEVWH_PRE)/magic_elf/gdb_format.pl \
	$(TEVWH_PRE)/entry_point/gdb/$(TEVWH_UNAME).sh
	$(TEVWH_PRE)/entry_point/gdb/$(TEVWH_UNAME).sh > $@
$(TEVWH_OUT)/entry_point/sh.disasm: \
	$(TEVWH_PRE)/entry_point/$(TEVWH_ASM_STYLE).sh \
	${TEVWH_TMP}/evil_magic/e_entry \
	$(TEVWH_PRE)/magic_elf/objdump_format.pl
	$(TEVWH_PRE)/entry_point/$(TEVWH_ASM_STYLE).sh > $@ 2>&1
$(TEVWH_OUT)/entry_point/entry_point: \
	$(TEVWH_PRE)/entry_point/entry_point.sh \
	$(TEVWH_TMP)/segment_padding/e1i1/$(TEVWH_NAME_SH)_infected \
	$(TEVWH_TMP)/segment_padding/e2i1/$(TEVWH_NAME_SH)_infected
	$(TEVWH_PRE)/entry_point/entry_point.sh > $@ 2>&1
$(TEVWH_OUT)/entry_point/e2.disasm: \
	$(TEVWH_PRE)/entry_point/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/segment_padding/e2i1/$(TEVWH_NAME_SH)_infected
	$(TEVWH_PRE)/entry_point/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/segment_padding/e2i1/$(TEVWH_NAME_SH)_infected \
	> $@ 2>&1
$(TEVWH_OUT)/entry_point/ldd: \
	$(TEVWH_PRE)/entry_point/ldd.sh
	$?  > $@ 2>&1
$(TEVWH_OUT)/entry_point/nm: \
	$(TEVWH_PRE)/entry_point/nm.sh
	$?  > $@ 2>&1
#$(TEVWH_PRE)/entry_point/__libc_start_main: \
#	$(TEVWH_OUT)/entry_point/nm \
#	$(TEVWH_PRE)/entry_point/get_libc_start_main.sh
#	$(TEVWH_PRE)/entry_point/get_libc_start_main.sh $@
$(TEVWH_OUT)/entry_point/e3.disasm: \
	$(TEVWH_PRE)/entry_point/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/segment_padding/e3i1/$(TEVWH_NAME_SH)_infected
	$(TEVWH_PRE)/entry_point/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/segment_padding/e3i1/$(TEVWH_NAME_SH)_infected \
	> $@ 2>&1

$(TEVWH_OUT)/additional_cs/hexdump: \
	$(TEVWH_PRE)/additional_cs/hexdump.sh \
	$(TEVWH_PRE)/format.hex
	$(TEVWH_PRE)/additional_cs/hexdump.sh > $@ 2>&1

$(TEVWH_OUT)/additional_cs/objdump: \
	$(TEVWH_TMP)/additional_cs/e3i1/$(TEVWH_NAME_SH)_infected
	(  $(TEVWH_PATH_LS) -l $?; $(TEVWH_PATH_OBJDUMP) -fp $? ) > $@
$(TEVWH_OUT)/additional_cs/readelf: \
	$(TEVWH_TMP)/additional_cs/e3i1/$(TEVWH_NAME_SH)_infected
	(  $(TEVWH_PATH_LS) -l $?; $(TEVWH_PATH_READELF) -l $? ) > $@
$(TEVWH_OUT)/additional_cs/scan: \
	$(TEVWH_TMP)/additional_cs/e3i1/$(TEVWH_NAME_SH)_infected \
	$(TEVWH_PRE)/additional_cs/scan_dist.sh \
	$(scanner_segment_deps)
	-$(TEVWH_PRE)/additional_cs/scan_dist.sh 2> $@

$(TEVWH_OUT)/doing_it_in_c/get_relocate_ofs.disasm: \
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i2/$(TEVWH_NAME_SH)_infected
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh > $@ 2>&1
$(TEVWH_OUT)/doing_it_in_c/begin.disasm: \
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i2/$(TEVWH_NAME_SH)_infected
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i2/infector "begin" > $@
$(TEVWH_OUT)/doing_it_in_c/addr: \
	$(TEVWH_PRE)/doing_it_in_c/addr.c
	$(TEVWH_PATH_CC) $(TEVWH_CFLAGS) -o $(TEVWH_TMP)/doing_it_in_c/addr \
		$? > $@ 2>&1 \
	&& $(TEVWH_TMP)/doing_it_in_c/addr >> $@ 2>&1
$(TEVWH_OUT)/doing_it_in_c/e3i2.disasm: \
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i2/$(TEVWH_NAME_SH)_infected
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i2/infector infection 4 > $@ 2>&1
$(TEVWH_OUT)/doing_it_in_c/e3i3.disasm: \
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i2/$(TEVWH_NAME_SH)_infected
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i2/infector infection 1 > $@ 2>&1
$(TEVWH_OUT)/doing_it_in_c/e3i4.disasm: \
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i3/$(TEVWH_NAME_SH)_infected
	$(TEVWH_PRE)/doing_it_in_c/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i3/infector infection 2 > $@ 2>&1

$(TEVWH_OUT)/stub_revisited/sh.disasm: \
	$(TEVWH_PRE)/stub_revisited/$(TEVWH_ASM_STYLE).sh
	( $? 2>&1 ) | head > $@
$(TEVWH_OUT)/stub_revisited/sh_ret.disasm: \
	$(TEVWH_PRE)/stub_revisited/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_PRE)/stub_revisited/$(TEVWH_ASM_STYLE)_ret.sh
	$(TEVWH_PRE)/stub_revisited/$(TEVWH_ASM_STYLE)_ret.sh > $@ 2>&1
$(TEVWH_OUT)/stub_revisited/magic_elf.disasm: \
	$(TEVWH_PRE)/stub_revisited/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/magic_elf/magic_elf
	( $(TEVWH_PRE)/stub_revisited/$(TEVWH_ASM_STYLE).sh $(TEVWH_TMP)/magic_elf/magic_elf 2>&1 ) \
	| head \
	> $@
$(TEVWH_OUT)/stub_revisited/$(TEVWH_NAME_SH)_infected.disasm: \
	$(TEVWH_PRE)/stub_revisited/$(TEVWH_ASM_STYLE).sh \
	$(TEVWH_TMP)/doing_it_in_c/e3i2/$(TEVWH_NAME_SH)_infected
	( $(TEVWH_PRE)/stub_revisited/$(TEVWH_ASM_STYLE).sh \
	  $(TEVWH_TMP)/doing_it_in_c/e3i2/$(TEVWH_NAME_SH)_infected 2>&1 ) \
	| $(TEVWH_PATH_SED) 11q \
	> $@
$(TEVWH_OUT)/stub_revisited/stack: \
	$(TEVWH_PRE)/stub_revisited/stack.sh \
	$(TEVWH_TMP)/magic_elf/magic_elf
	$(TEVWH_PRE)/stub_revisited/stack.sh > $@ 2>&1
#$(TEVWH_PRE)/stub_revisited/__libc_start_main: \
#	$(TEVWH_OUT)/stub_revisited/stack \
#	$(TEVWH_PRE)/stub_revisited/get_libc_start_main.sh
#	$(TEVWH_PRE)/stub_revisited/get_libc_start_main.sh \
#		$@ $(TEVWH_OUT)/stub_revisited/stack

$(TEVWH_OUT)/suspicious_code/disasm: \
	src/suspicious_code/dumpsection.pl \
	$(TEVWH_PRE)/suspicious_code/$(TEVWH_ASM_STYLE).sh
	$(TEVWH_PRE)/suspicious_code/$(TEVWH_ASM_STYLE).sh > $@ 2>&1
