xref: /aosp_15_r20/external/AFLplusplus/GNUmakefile.llvm (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1# american fuzzy lop++ - LLVM instrumentation
2# -----------------------------------------
3#
4# Written by Laszlo Szekeres <[email protected]> and
5#            Michal Zalewski
6#
7# LLVM integration design comes from Laszlo Szekeres.
8#
9# Copyright 2015, 2016 Google Inc. All rights reserved.
10#
11# Licensed under the Apache License, Version 2.0 (the "License");
12# you may not use this file except in compliance with the License.
13# You may obtain a copy of the License at:
14#
15#   https://www.apache.org/licenses/LICENSE-2.0
16#
17
18# For Heiko:
19#TEST_MMAP=1
20HASH=\#
21
22PREFIX      ?= /usr/local
23HELPER_PATH ?= $(PREFIX)/lib/afl
24BIN_PATH    ?= $(PREFIX)/bin
25DOC_PATH    ?= $(PREFIX)/share/doc/afl
26MISC_PATH   ?= $(PREFIX)/share/afl
27MAN_PATH    ?= $(PREFIX)/share/man/man8
28
29BUILD_DATE  ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+%Y-%m-%d" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+%Y-%m-%d" 2>/dev/null || date -u "+%Y-%m-%d")
30
31VERSION     = $(shell grep '^$(HASH)define VERSION ' ./config.h | cut -d '"' -f2)
32
33SYS = $(shell uname -s)
34
35ifeq "$(SYS)" "OpenBSD"
36  LLVM_CONFIG ?= $(BIN_PATH)/llvm-config
37  HAS_OPT = $(shell test -x $(BIN_PATH)/opt && echo 0 || echo 1)
38  ifeq "$(HAS_OPT)" "1"
39    $(warning llvm_mode needs a complete llvm installation (versions 6.0 up to 13) -> e.g. "pkg_add llvm-7.0.1p9")
40  endif
41else
42  LLVM_CONFIG ?= llvm-config
43endif
44
45LLVMVER  = $(shell $(LLVM_CONFIG) --version 2>/dev/null | sed 's/git//' | sed 's/svn//' )
46LLVM_MAJOR = $(shell $(LLVM_CONFIG) --version 2>/dev/null | sed 's/\..*//' )
47LLVM_MINOR = $(shell $(LLVM_CONFIG) --version 2>/dev/null | sed 's/.*\.//' | sed 's/git//' | sed 's/svn//' | sed 's/ .*//' )
48LLVM_UNSUPPORTED = $(shell $(LLVM_CONFIG) --version 2>/dev/null | grep -E -q '^[0-2]\.|^3.[0-8]\.' && echo 1 || echo 0 )
49LLVM_TOO_NEW = $(shell $(LLVM_CONFIG) --version 2>/dev/null | grep -E -q '^1[8-9]|^2[0-9]' && echo 1 || echo 0 )
50LLVM_TOO_OLD = $(shell $(LLVM_CONFIG) --version 2>/dev/null | grep -E -q '^[1-9]\.|^1[012]\.' && echo 1 || echo 0 )
51LLVM_NEW_API = $(shell $(LLVM_CONFIG) --version 2>/dev/null | grep -E -q '^1[0-9]' && echo 1 || echo 0 )
52LLVM_NEWER_API = $(shell $(LLVM_CONFIG) --version 2>/dev/null | grep -E -q '^1[6-9]' && echo 1 || echo 0 )
53LLVM_13_OK = $(shell $(LLVM_CONFIG) --version 2>/dev/null | grep -E -q '^1[3-9]' && echo 1 || echo 0 )
54LLVM_HAVE_LTO = $(shell $(LLVM_CONFIG) --version 2>/dev/null | grep -E -q '^1[2-9]' && echo 1 || echo 0 )
55LLVM_BINDIR = $(shell $(LLVM_CONFIG) --bindir 2>/dev/null)
56LLVM_LIBDIR = $(shell $(LLVM_CONFIG) --libdir 2>/dev/null)
57LLVM_STDCXX = gnu++11
58LLVM_APPLE_XCODE = $(shell $(CC) -v 2>&1 | grep -q Apple && echo 1 || echo 0)
59LLVM_LTO   = 0
60
61ifeq "$(LLVMVER)" ""
62  $(warning [!] llvm_mode needs llvm-config, which was not found. Set LLVM_CONFIG to its path and retry.)
63endif
64
65ifeq "$(LLVM_UNSUPPORTED)" "1"
66  $(error llvm_mode only supports llvm from version 3.8 onwards)
67endif
68
69ifeq "$(LLVM_TOO_NEW)" "1"
70  $(warning you are using an in-development llvm version - this might break llvm_mode!)
71endif
72
73ifeq "$(LLVM_TOO_OLD)" "1"
74  $(warning you are using an outdated LLVM version! Please use at least LLVM 13 or newer!)
75  $(shell sleep 2)
76endif
77
78# No switching the meaning of LLVM_TOO_OLD
79LLVM_TOO_OLD=1
80
81ifeq "$(LLVM_MAJOR)" "9"
82  $(info [+] llvm_mode detected llvm 9, enabling neverZero implementation)
83  LLVM_TOO_OLD=0
84endif
85
86ifeq "$(LLVM_NEW_API)" "1"
87  $(info [+] llvm_mode detected llvm 10+, enabling neverZero implementation and c++14)
88  LLVM_STDCXX = c++14
89  LLVM_TOO_OLD=0
90endif
91
92ifeq "$(LLVM_NEWER_API)" "1"
93  $(info [+] llvm_mode detected llvm 16+, enabling c++17)
94  LLVM_STDCXX = c++17
95endif
96
97ifeq "$(LLVM_HAVE_LTO)" "1"
98  $(info [+] llvm_mode detected llvm 12+, enabling afl-lto LTO implementation)
99  LLVM_LTO = 1
100endif
101
102ifeq "$(LLVM_LTO)" "0"
103  $(info [+] llvm_mode detected llvm < 12, afl-lto LTO will not be build.)
104endif
105
106ifeq "$(LLVM_APPLE_XCODE)" "1"
107  $(warning llvm_mode will not compile with Xcode clang...)
108endif
109
110# We were using llvm-config --bindir to get the location of clang, but
111# this seems to be busted on some distros, so using the one in $PATH is
112# probably better.
113
114CC         = $(LLVM_BINDIR)/clang
115CXX        = $(LLVM_BINDIR)/clang++
116
117# llvm-config --bindir may not providing a valid path, so ...
118ifeq "$(shell test -e $(CC) || echo 1 )" "1"
119  # however we must ensure that this is not a "CC=gcc make"
120  ifeq "$(shell command -v $(CC) 2> /dev/null)" ""
121    # we do not have a valid CC variable so we try alternatives
122    ifeq "$(shell test -e '$(BIN_DIR)/clang' && echo 1)" "1"
123      # we found one in the local install directory, lets use these
124      CC         = $(BIN_DIR)/clang
125    else
126      # hope for the best
127      $(warning we have trouble finding clang - llvm-config is not helping us)
128      CC         = clang
129    endif
130  endif
131endif
132# llvm-config --bindir may not providing a valid path, so ...
133ifeq "$(shell test -e $(CXX) || echo 1 )" "1"
134  # however we must ensure that this is not a "CXX=g++ make"
135  ifeq "$(shell command -v $(CXX) 2> /dev/null)" ""
136    # we do not have a valid CXX variable so we try alternatives
137    ifeq "$(shell test -e '$(BIN_DIR)/clang++' && echo 1)" "1"
138      # we found one in the local install directory, lets use these
139      CXX        = $(BIN_DIR)/clang++
140    else
141      # hope for the best
142      $(warning we have trouble finding clang++ - llvm-config is not helping us)
143      CXX        = clang++
144    endif
145  endif
146endif
147
148# sanity check.
149# Are versions of clang --version and llvm-config --version equal?
150CLANGVER = $(shell $(CC) --version | sed -E -ne '/^.*version\ (1?[0-9]\.[0-9]\.[0-9]).*/s//\1/p')
151
152# I disable this because it does not make sense with what we did before (marc)
153# We did exactly set these 26 lines above with these values, and it would break
154# "CC=gcc make" etc. usages
155ifeq "$(findstring clang, $(shell $(CC) --version 2>/dev/null))" ""
156  CC_SAVE := $(LLVM_BINDIR)/clang
157else
158  CC_SAVE := $(CC)
159endif
160ifeq "$(findstring clang, $(shell $(CXX) --version 2>/dev/null))" ""
161  CXX_SAVE := $(LLVM_BINDIR)/clang++
162else
163  CXX_SAVE := $(CXX)
164endif
165
166CLANG_BIN := $(CC_SAVE)
167CLANGPP_BIN := $(CXX_SAVE)
168
169ifeq "$(CC_SAVE)" "$(LLVM_BINDIR)/clang"
170  USE_BINDIR = 1
171else
172  ifeq "$(CXX_SAVE)" "$(LLVM_BINDIR)/clang++"
173    USE_BINDIR = 1
174  else
175    USE_BINDIR = 0
176  endif
177endif
178
179# On old platform we cannot compile with clang because std++ libraries are too
180# old. For these we need to use gcc/g++, so if we find REAL_CC and REAL_CXX
181# variable we override the compiler variables here
182ifneq "$(REAL_CC)" ""
183  CC  = $(REAL_CC)
184endif
185ifneq "$(REAL_CXX)" ""
186  CXX = $(REAL_CXX)
187endif
188
189#
190# Now it can happen that CC points to clang - but there is no clang on the
191# system. Then we fall back to cc
192#
193ifeq "$(shell command -v $(CC) 2>/dev/null)" ""
194  CC = cc
195endif
196ifeq "$(shell command -v $(CXX) 2>/dev/null)" ""
197  CXX = c++
198endif
199
200
201# After we set CC/CXX we can start makefile magic tests
202
203#ifeq "$(shell echo 'int main() {return 0; }' | $(CC) -x c - -march=native -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
204#	CFLAGS_OPT = -march=native
205#endif
206
207ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -flto=full -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
208        AFL_CLANG_FLTO ?= -flto=full
209else
210 ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -flto=thin -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
211        AFL_CLANG_FLTO ?= -flto=thin
212 else
213  ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -flto -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
214        AFL_CLANG_FLTO ?= -flto
215  endif
216 endif
217endif
218
219ifeq "$(LLVM_LTO)" "1"
220  ifneq "$(AFL_CLANG_FLTO)" ""
221    ifeq "$(AFL_REAL_LD)" ""
222      ifneq "$(shell readlink $(LLVM_BINDIR)/ld.lld 2>&1)" ""
223        AFL_REAL_LD = $(LLVM_BINDIR)/ld.lld
224      else ifneq "$(shell command -v ld.lld 2>/dev/null)" ""
225        AFL_REAL_LD = $(shell command -v ld.lld)
226        TMP_LDLDD_VERSION = $(shell $(AFL_REAL_LD) --version | awk '{ print $$2 }')
227        ifeq "$(LLVMVER)" "$(TMP_LDLDD_VERSION)"
228          $(warning ld.lld found in a weird location ($(AFL_REAL_LD)), but its the same version as LLVM so we will allow it)
229        else
230          $(warning ld.lld found in a weird location ($(AFL_REAL_LD)) and its of a different version than LLMV ($(TMP_LDLDD_VERSION) vs. $(LLVMVER)) - cannot enable LTO mode)
231          AFL_REAL_LD=
232          LLVM_LTO = 0
233        endif
234        undefine TMP_LDLDD_VERSION
235      else
236        $(warning ld.lld not found, cannot enable LTO mode)
237        LLVM_LTO = 0
238      endif
239    endif
240  else
241    $(warning clang option -flto is not working - maybe LLVMgold.so not found - cannot enable LTO mode)
242    LLVM_LTO = 0
243  endif
244endif
245
246AFL_CLANG_FUSELD=
247ifeq "$(LLVM_LTO)" "1"
248  ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -fuse-ld=`command -v ld` -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
249    AFL_CLANG_FUSELD=1
250    ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -fuse-ld=ld.lld --ld-path=$(AFL_REAL_LD) -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
251      AFL_CLANG_LDPATH=1
252    endif
253  else
254    $(warning -fuse-ld is not working, cannot enable LTO mode)
255    LLVM_LTO = 0
256  endif
257endif
258
259ifeq "$(shell echo 'int main() {return 0; }' | $(CLANG_BIN) -x c - -fdebug-prefix-map=$(CURDIR)=llvm_mode -o .test 2>/dev/null && echo 1 || echo 0 ; rm -f .test )" "1"
260        AFL_CLANG_DEBUG_PREFIX = -fdebug-prefix-map="$(CURDIR)=llvm_mode"
261else
262        AFL_CLANG_DEBUG_PREFIX =
263endif
264
265CFLAGS          ?= -O3 -funroll-loops -fPIC
266# -D_FORTIFY_SOURCE=1
267CFLAGS_SAFE     := -Wall -g -Wno-cast-qual -Wno-variadic-macros -Wno-pointer-sign \
268                   -I ./include/ -I ./instrumentation/ \
269                   -DAFL_PATH=\"$(HELPER_PATH)\" -DBIN_PATH=\"$(BIN_PATH)\" \
270                   -DLLVM_BINDIR=\"$(LLVM_BINDIR)\" -DVERSION=\"$(VERSION)\" \
271                   -DLLVM_LIBDIR=\"$(LLVM_LIBDIR)\" -DLLVM_VERSION=\"$(LLVMVER)\" \
272                   -DAFL_CLANG_FLTO=\"$(AFL_CLANG_FLTO)\" -DAFL_REAL_LD=\"$(AFL_REAL_LD)\" \
273                   -DAFL_CLANG_LDPATH=\"$(AFL_CLANG_LDPATH)\" -DAFL_CLANG_FUSELD=\"$(AFL_CLANG_FUSELD)\" \
274                   -DCLANG_BIN=\"$(CLANG_BIN)\" -DCLANGPP_BIN=\"$(CLANGPP_BIN)\" -DUSE_BINDIR=$(USE_BINDIR) \
275                   -Wno-unused-function $(AFL_CLANG_DEBUG_PREFIX)
276ifndef LLVM_DEBUG
277  CFLAGS_SAFE += -Wno-deprecated
278endif
279
280ifdef CODE_COVERAGE
281  override CFLAGS_SAFE += -D__AFL_CODE_COVERAGE=1
282  override LDFLAGS += -ldl
283endif
284
285override CFLAGS += $(CFLAGS_SAFE)
286
287ifdef AFL_TRACE_PC
288  $(info Compile option AFL_TRACE_PC is deprecated, just set AFL_LLVM_INSTRUMENT=PCGUARD to activate when compiling targets )
289endif
290
291CXXFLAGS          ?= -O3 -funroll-loops -fPIC
292# -D_FORTIFY_SOURCE=1
293override CXXFLAGS += -Wall -g -I ./include/ \
294                     -DVERSION=\"$(VERSION)\" -Wno-variadic-macros -Wno-deprecated-copy-with-dtor \
295                     -DLLVM_MINOR=$(LLVM_MINOR) -DLLVM_MAJOR=$(LLVM_MAJOR)
296
297ifneq "$(shell $(LLVM_CONFIG) --includedir) 2> /dev/null" ""
298  CLANG_CFL  = -I$(shell $(LLVM_CONFIG) --includedir)
299endif
300ifneq "$(LLVM_CONFIG)" ""
301  CLANG_CFL += -I$(shell dirname $(LLVM_CONFIG))/../include
302endif
303CLANG_CPPFL  = `$(LLVM_CONFIG) --cxxflags` -fno-rtti -fno-exceptions -fPIC $(CXXFLAGS) $(CPPFLAGS) -Wno-deprecated-declarations
304CLANG_LFL    = `$(LLVM_CONFIG) --ldflags` $(LDFLAGS)
305
306# wasm fuzzing: disable thread-local storage and unset LLVM debug flag
307ifdef WAFL_MODE
308  $(info Compiling libraries for use with WAVM)
309  CLANG_CPPFL += -DNDEBUG -DNO_TLS
310endif
311
312# User teor2345 reports that this is required to make things work on MacOS X.
313ifeq "$(SYS)" "Darwin"
314  CLANG_LFL += -Wl,-flat_namespace -Wl,-undefined,suppress
315  override LLVM_HAVE_LTO := 0
316  override LLVM_LTO := 0
317else
318  CLANG_CPPFL += -Wl,-znodelete
319endif
320
321ifeq "$(SYS)" "OpenBSD"
322  CLANG_LFL += `$(LLVM_CONFIG) --libdir`/libLLVM.so
323  CLANG_CPPFL += -mno-retpoline
324  CFLAGS += -mno-retpoline
325  # Needed for unwind symbols
326  LDFLAGS += -lc++abi -lpthread
327endif
328
329ifeq "$(shell echo '$(HASH)include <sys/ipc.h>@$(HASH)include <sys/shm.h>@int main() { int _id = shmget(IPC_PRIVATE, 65536, IPC_CREAT | IPC_EXCL | 0600); shmctl(_id, IPC_RMID, 0); return 0;}' | tr @ '\n' | $(CC) -x c - -o .test2 2>/dev/null && echo 1 || echo 0 ; rm -f .test2 )" "1"
330        SHMAT_OK=1
331else
332        SHMAT_OK=0
333        CFLAGS_SAFE += -DUSEMMAP=1
334        LDFLAGS += -Wno-deprecated-declarations
335endif
336
337ifeq "$(TEST_MMAP)" "1"
338        SHMAT_OK=0
339        CFLAGS_SAFE += -DUSEMMAP=1
340        LDFLAGS += -Wno-deprecated-declarations
341endif
342
343PROGS_ALWAYS = ./afl-cc ./afl-compiler-rt.o ./afl-compiler-rt-32.o ./afl-compiler-rt-64.o
344PROGS        = $(PROGS_ALWAYS) ./afl-llvm-pass.so ./SanitizerCoveragePCGUARD.so ./split-compares-pass.so ./split-switches-pass.so ./cmplog-routines-pass.so ./cmplog-instructions-pass.so ./cmplog-switches-pass.so ./afl-llvm-dict2file.so ./compare-transform-pass.so ./afl-ld-lto ./afl-llvm-lto-instrumentlist.so ./SanitizerCoverageLTO.so ./injection-pass.so
345
346# If prerequisites are not given, warn, do not build anything, and exit with code 0
347ifeq "$(LLVMVER)" ""
348  NO_BUILD = 1
349endif
350
351ifneq "$(LLVM_UNSUPPORTED)$(LLVM_APPLE_XCODE)" "00"
352  NO_BUILD = 1
353endif
354
355ifeq "$(NO_BUILD)" "1"
356  TARGETS = test_shm $(PROGS_ALWAYS) afl-cc.8
357else
358  TARGETS = test_shm test_deps $(PROGS) afl-cc.8 test_build all_done
359endif
360
361LLVM_MIN_4_0_1 = $(shell awk 'function tonum(ver, a) {split(ver,a,"."); return a[1]*1000000+a[2]*1000+a[3]} BEGIN { exit tonum(ARGV[1]) >= tonum(ARGV[2]) }' $(LLVMVER) 4.0.1; echo $$?)
362
363.PHONY: all
364all: $(TARGETS)
365
366.PHONY: test_shm
367ifeq "$(SHMAT_OK)" "1"
368test_shm:
369	@echo "[+] shmat seems to be working."
370	@rm -f .test2
371else
372test_shm:
373	@echo "[-] shmat seems not to be working, switching to mmap implementation"
374endif
375
376.PHONY: no_build
377no_build:
378	@printf "%b\\n" "\\033[0;31mPrerequisites are not met, skipping build llvm_mode\\033[0m"
379
380.PHONY: test_deps
381test_deps:
382	@echo "[*] Checking for working 'llvm-config'..."
383 ifneq "$(LLVM_APPLE_XCODE)" "1"
384	@type $(LLVM_CONFIG) >/dev/null 2>&1 || ( echo "[-] Oops, can't find 'llvm-config'. Install clang or set \$$LLVM_CONFIG or \$$PATH beforehand."; echo "    (Sometimes, the binary will be named llvm-config-11 or something like that.)"; exit 1 )
385 endif
386	@echo "[*] Checking for working '$(CC)'..."
387	@type $(CC) >/dev/null 2>&1 || ( echo "[-] Oops, can't find '$(CC)'. Make sure that it's in your \$$PATH (or set \$$CC and \$$CXX)."; exit 1 )
388	@echo "[*] Checking for matching versions of '$(CC)' and '$(LLVM_CONFIG)'"
389ifneq "$(CLANGVER)" "$(LLVMVER)"
390	@echo "[!] WARNING: we have llvm-config version $(LLVMVER) and a clang version $(CLANGVER)"
391else
392	@echo "[*] We have llvm-config version $(LLVMVER) with a clang version $(CLANGVER), good."
393endif
394	@echo "[*] Checking for './afl-showmap'..."
395	@test -f ./afl-showmap || ( echo "[-] Oops, can't find './afl-showmap'. Be sure to compile AFL first."; exit 1 )
396	@echo "[+] All set and ready to build."
397
398instrumentation/afl-common.o: ./src/afl-common.c
399	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(LDFLAGS)
400
401./afl-cc: src/afl-cc.c instrumentation/afl-common.o
402	$(CC) $(CLANG_CFL) $(CFLAGS) $(CPPFLAGS) $< instrumentation/afl-common.o -o $@ -DLLVM_MINOR=$(LLVM_MINOR) -DLLVM_MAJOR=$(LLVM_MAJOR) $(LDFLAGS) -DCFLAGS_OPT=\"$(CFLAGS_OPT)\" -lm
403	@ln -sf afl-cc ./afl-c++
404	@ln -sf afl-cc ./afl-gcc
405	@ln -sf afl-cc ./afl-g++
406	@ln -sf afl-cc ./afl-clang
407	@ln -sf afl-cc ./afl-clang++
408	@ln -sf afl-cc ./afl-clang-fast
409	@ln -sf afl-cc ./afl-clang-fast++
410ifneq "$(AFL_CLANG_FLTO)" ""
411ifeq "$(LLVM_LTO)" "1"
412	@ln -sf afl-cc ./afl-clang-lto
413	@ln -sf afl-cc ./afl-clang-lto++
414	@ln -sf afl-cc ./afl-lto
415	@ln -sf afl-cc ./afl-lto++
416endif
417endif
418
419instrumentation/afl-llvm-common.o: instrumentation/afl-llvm-common.cc instrumentation/afl-llvm-common.h
420	$(CXX) $(CFLAGS) $(CPPFLAGS) `$(LLVM_CONFIG) --cxxflags` -fno-rtti -fPIC -std=$(LLVM_STDCXX) -c $< -o $@
421
422./afl-llvm-pass.so: instrumentation/afl-llvm-pass.so.cc instrumentation/afl-llvm-common.o | test_deps
423ifeq "$(LLVM_MIN_4_0_1)" "0"
424	$(info [!] N-gram branch coverage instrumentation is not available for llvm version $(LLVMVER))
425endif
426	$(CXX) $(CLANG_CPPFL) -Wdeprecated -fno-rtti -fPIC -std=$(LLVM_STDCXX) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
427
428./SanitizerCoveragePCGUARD.so: instrumentation/SanitizerCoveragePCGUARD.so.cc instrumentation/afl-llvm-common.o | test_deps
429ifeq "$(LLVM_13_OK)" "1"
430	-$(CXX) $(CLANG_CPPFL) -fno-rtti -fPIC -std=$(LLVM_STDCXX) -shared $< -o $@ $(CLANG_LFL) -Wno-deprecated-copy-dtor -Wdeprecated instrumentation/afl-llvm-common.o
431endif
432
433./afl-llvm-lto-instrumentlist.so: instrumentation/afl-llvm-lto-instrumentlist.so.cc instrumentation/afl-llvm-common.o
434ifeq "$(LLVM_LTO)" "1"
435	$(CXX) $(CLANG_CPPFL) -fno-rtti -fPIC -std=$(LLVM_STDCXX) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
436endif
437
438./afl-ld-lto: src/afl-ld-lto.c
439ifeq "$(LLVM_LTO)" "1"
440	$(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@
441endif
442
443./SanitizerCoverageLTO.so: instrumentation/SanitizerCoverageLTO.so.cc instrumentation/afl-llvm-common.o
444ifeq "$(LLVM_LTO)" "1"
445	$(CXX) $(CLANG_CPPFL) -Wno-writable-strings -fno-rtti -fPIC -std=$(LLVM_STDCXX) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
446	$(CLANG_BIN) $(CFLAGS_SAFE) $(CPPFLAGS) -Wno-unused-result -O0 $(AFL_CLANG_FLTO) -fPIC -c instrumentation/afl-llvm-rt-lto.o.c -o ./afl-llvm-rt-lto.o
447	@$(CLANG_BIN) $(CFLAGS_SAFE) $(CPPFLAGS) -Wno-unused-result -O0 $(AFL_CLANG_FLTO) -m64 -fPIC -c instrumentation/afl-llvm-rt-lto.o.c -o ./afl-llvm-rt-lto-64.o 2>/dev/null; if [ "$$?" = "0" ]; then : ; fi
448	@$(CLANG_BIN) $(CFLAGS_SAFE) $(CPPFLAGS) -Wno-unused-result -O0 $(AFL_CLANG_FLTO) -m32 -fPIC -c instrumentation/afl-llvm-rt-lto.o.c -o ./afl-llvm-rt-lto-32.o 2>/dev/null; if [ "$$?" = "0" ]; then : ; fi
449endif
450
451# laf
452./split-switches-pass.so:	instrumentation/split-switches-pass.so.cc instrumentation/afl-llvm-common.o | test_deps
453	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
454./compare-transform-pass.so:	instrumentation/compare-transform-pass.so.cc instrumentation/afl-llvm-common.o | test_deps
455	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
456./split-compares-pass.so:	instrumentation/split-compares-pass.so.cc instrumentation/afl-llvm-common.o | test_deps
457	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
458# /laf
459
460./cmplog-routines-pass.so:	instrumentation/cmplog-routines-pass.cc instrumentation/afl-llvm-common.o | test_deps
461	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
462
463./cmplog-instructions-pass.so:	instrumentation/cmplog-instructions-pass.cc instrumentation/afl-llvm-common.o | test_deps
464	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
465
466./cmplog-switches-pass.so:	instrumentation/cmplog-switches-pass.cc instrumentation/afl-llvm-common.o | test_deps
467	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
468
469afl-llvm-dict2file.so:	instrumentation/afl-llvm-dict2file.so.cc instrumentation/afl-llvm-common.o | test_deps
470	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
471
472./injection-pass.so:	instrumentation/injection-pass.cc instrumentation/afl-llvm-common.o | test_deps
473	$(CXX) $(CLANG_CPPFL) -shared $< -o $@ $(CLANG_LFL) instrumentation/afl-llvm-common.o
474
475.PHONY: document
476document:
477	$(CLANG_BIN) -D_AFL_DOCUMENT_MUTATIONS $(CFLAGS_SAFE) $(CPPFLAGS) $(CLANG_CFL) -O3 -Wno-unused-result -fPIC -c instrumentation/afl-compiler-rt.o.c -o ./afl-compiler-rt.o
478	@$(CLANG_BIN) -D_AFL_DOCUMENT_MUTATIONS $(CFLAGS_SAFE) $(CPPFLAGS) $(CLANG_CFL) -O3 -Wno-unused-result -m32 -fPIC -c instrumentation/afl-compiler-rt.o.c -o ./afl-compiler-rt-32.o 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
479	@$(CLANG_BIN) -D_AFL_DOCUMENT_MUTATIONS $(CFLAGS_SAFE) $(CPPFLAGS) $(CLANG_CFL) -O3 -Wno-unused-result -m64 -fPIC -c instrumentation/afl-compiler-rt.o.c -o ./afl-compiler-rt-64.o 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
480
481./afl-compiler-rt.o: instrumentation/afl-compiler-rt.o.c
482	$(CC) $(CLANG_CFL) $(CFLAGS_SAFE) $(CPPFLAGS) -O3 -Wno-unused-result -fPIC -c $< -o $@
483
484./afl-compiler-rt-32.o: instrumentation/afl-compiler-rt.o.c
485	@printf "[*] Building 32-bit variant of the runtime (-m32)... "
486	@$(CC) $(CLANG_CFL) $(CFLAGS_SAFE) $(CPPFLAGS) -O3 -Wno-unused-result -m32 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
487
488./afl-compiler-rt-64.o: instrumentation/afl-compiler-rt.o.c
489	@printf "[*] Building 64-bit variant of the runtime (-m64)... "
490	@$(CC) $(CLANG_CFL) $(CFLAGS_SAFE) $(CPPFLAGS) -O3 -Wno-unused-result -m64 -fPIC -c $< -o $@ 2>/dev/null; if [ "$$?" = "0" ]; then echo "success!"; else echo "failed (that's fine)"; fi
491
492.PHONY: test_build
493test_build: $(PROGS)
494	@echo "[*] Testing the CC wrapper and instrumentation output..."
495	unset AFL_USE_ASAN AFL_USE_MSAN AFL_INST_RATIO; ASAN_OPTIONS=detect_leaks=0 AFL_QUIET=1 AFL_PATH=. AFL_LLVM_LAF_ALL=1 ./afl-cc $(CFLAGS) $(CPPFLAGS) ./test-instr.c -o test-instr $(LDFLAGS)
496	ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr0 ./test-instr < /dev/null
497	echo 1 | ASAN_OPTIONS=detect_leaks=0 ./afl-showmap -m none -q -o .test-instr1 ./test-instr
498	@rm -f test-instr
499	@cmp -s .test-instr0 .test-instr1; DR="$$?"; rm -f .test-instr0 .test-instr1; if [ "$$DR" = "0" ]; then echo; echo "Oops, the instrumentation does not seem to be behaving correctly!"; echo; echo "Please post to https://github.com/AFLplusplus/AFLplusplus/issues to troubleshoot the issue."; echo; exit 1; fi
500	@echo "[+] All right, the instrumentation seems to be working!"
501
502.PHONY: all_done
503all_done: test_build
504	@echo "[+] All done! You can now use './afl-cc' to compile programs."
505
506.NOTPARALLEL: clean
507
508.PHONY: install
509install: all
510	@install -d -m 755 $${DESTDIR}$(BIN_PATH) $${DESTDIR}$(HELPER_PATH) $${DESTDIR}$(DOC_PATH) $${DESTDIR}$(MISC_PATH)
511	@if [ -f ./afl-cc ]; then set -e; install -m 755 ./afl-cc $${DESTDIR}$(BIN_PATH); ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-c++; fi
512	@rm -f $${DESTDIR}$(HELPER_PATH)/afl-llvm-rt*.o $${DESTDIR}$(HELPER_PATH)/afl-gcc-rt*.o
513	@if [ -f ./afl-compiler-rt.o ]; then set -e; install -m 755 ./afl-compiler-rt.o $${DESTDIR}$(HELPER_PATH); fi
514	@if [ -f ./afl-lto ]; then set -e; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-lto; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-lto++; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang-lto; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang-lto++; install -m 755 ./afl-llvm-rt-lto*.o ./afl-llvm-lto-instrumentlist.so $${DESTDIR}$(HELPER_PATH); fi
515	@if [ -f ./afl-ld-lto ]; then set -e; install -m 755 ./afl-ld-lto $${DESTDIR}$(BIN_PATH); fi
516	@if [ -f ./afl-compiler-rt-32.o ]; then set -e; install -m 755 ./afl-compiler-rt-32.o $${DESTDIR}$(HELPER_PATH); fi
517	@if [ -f ./afl-compiler-rt-64.o ]; then set -e; install -m 755 ./afl-compiler-rt-64.o $${DESTDIR}$(HELPER_PATH); fi
518	@if [ -f ./compare-transform-pass.so ]; then set -e; install -m 755 ./*.so $${DESTDIR}$(HELPER_PATH); fi
519	@if [ -f ./compare-transform-pass.so ]; then set -e; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang-fast ; ln -sf ./afl-c++ $${DESTDIR}$(BIN_PATH)/afl-clang-fast++ ; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang ; ln -sf ./afl-c++ $${DESTDIR}$(BIN_PATH)/afl-clang++ ; fi
520	@if [ -f ./SanitizerCoverageLTO.so ]; then set -e; ln -sf afl-cc $${DESTDIR}$(BIN_PATH)/afl-clang-lto ; ln -sf ./afl-c++ $${DESTDIR}$(BIN_PATH)/afl-clang-lto++ ; fi
521	set -e; install -m 644 ./dynamic_list.txt $${DESTDIR}$(HELPER_PATH)
522	install -m 644 instrumentation/README.*.md $${DESTDIR}$(DOC_PATH)/
523
524%.8: %
525	@echo .TH $* 8 $(BUILD_DATE) "AFL++" > ./$@
526	@echo .SH NAME >> ./$@
527	@printf "%s" ".B $* \- " >> ./$@
528	@./$* -h 2>&1 | head -n 1 | sed -e "s/$$(printf '\e')[^m]*m//g" >> ./$@
529	@echo .B $* >> ./$@
530	@echo >> ./$@
531	@echo .SH SYNOPSIS >> ./$@
532	@./$* -h 2>&1 | head -n 3 | tail -n 1 | sed 's/^\.\///' >> ./$@
533	@echo >> ./$@
534	@echo .SH OPTIONS >> ./$@
535	@echo .nf >> ./$@
536	@./$* -h 2>&1 | tail -n +4 >> ./$@
537	@echo >> ./$@
538	@echo .SH AUTHOR >> ./$@
539	@echo "AFL++ was written by Michal \"lcamtuf\" Zalewski and is maintained by Marc \"van Hauser\" Heuse <[email protected]>, Dominik Maier <[email protected]>, Andrea Fioraldi <[email protected]> and Heiko \"hexcoder-\" Eissfeldt <[email protected]>" >> ./$@
540	@echo  The homepage of AFL++ is: https://github.com/AFLplusplus/AFLplusplus >> ./$@
541	@echo >> ./$@
542	@echo .SH LICENSE >> ./$@
543	@echo Apache License Version 2.0, January 2004 >> ./$@
544	@ln -sf afl-cc.8 ./afl-c++.8
545	@ln -sf afl-cc.8 ./afl-clang-fast.8
546	@ln -sf afl-cc.8 ./afl-clang-fast++.8
547ifneq "$(AFL_CLANG_FLTO)" ""
548ifeq "$(LLVM_LTO)" "1"
549	@ln -sf afl-cc.8 ./afl-clang-lto.8
550	@ln -sf afl-cc.8 ./afl-clang-lto++.8
551	@ln -sf afl-cc.8 ./afl-lto.8
552	@ln -sf afl-cc.8 ./afl-lto++.8
553endif
554endif
555
556.PHONY: clean
557clean:
558	rm -f *.o *.so *~ a.out core core.[1-9][0-9]* .test2 test-instr .test-instr0 .test-instr1 *.dwo
559	rm -f $(PROGS) afl-common.o ./afl-c++ ./afl-lto ./afl-lto++ ./afl-clang-lto* ./afl-clang-fast* ./afl-clang*.8 ./ld ./afl-ld ./afl-compiler-rt*.o ./afl-llvm-rt*.o instrumentation/*.o
560