aboutsummaryrefslogtreecommitdiff
blob: b09c3b0570a06ab223161325a68f1009c4a667d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright 2003-2006 Ned Ludd <solar@linbsd.net>
# Distributed under the terms of the GNU General Public License v2
####################################################################

check_gcc = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; \
	then echo "$(1)"; else echo "$(2)"; fi)
check_gcc_many = $(foreach flag,$(1),$(call check_gcc,$(flag)))

####################################################################
# Avoid CC overhead when installing
ifneq ($(MAKECMDGOALS),install)
_WFLAGS   := \
	-Wdeclaration-after-statement \
	-Wextra \
	-Wsequence-point \
	-Wstrict-overflow
WFLAGS    := -Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \
             -Wmissing-declarations -Wmissing-prototypes -Wwrite-strings \
             -Wbad-function-cast -Wnested-externs -Wcomment -Winline \
             -Wchar-subscripts -Wcast-align -Wno-format-nonliteral \
             $(call check_gcc_many,$(_WFLAGS))
endif

CFLAGS    ?= -O2 -pipe
override CPPFLAGS  += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
LDFLAGS   +=
LIBS      :=
DESTDIR    =
PREFIX     = $(DESTDIR)/usr
DATADIR    = $(PREFIX)/share
MANDIR     = $(DATADIR)/man
DOCDIR     = $(DATADIR)/doc
PKGDOCDIR  = $(DOCDIR)/pax-utils
STRIP     := strip
MKDIR     := mkdir -p
INS_EXE   := install -m755
INS_DATA  := install -m644

PKG_CONFIG ?= pkg-config

ifeq ($(USE_CAP),yes)
LIBCAPS_CFLAGS := $(shell $(PKG_CONFIG) --cflags libcap)
LIBCAPS_LIBS   := $(shell $(PKG_CONFIG) --libs libcap)
CPPFLAGS-pspax.c += $(LIBCAPS_CFLAGS) -DWANT_SYSCAP
LIBS-pspax       += $(LIBCAPS_LIBS)
endif

ifeq ($(USE_DEBUG),yes)
override CPPFLAGS += -DEBUG
endif

ifeq ($(USE_SECCOMP),yes)
LIBSECCOMP_CFLAGS := $(shell $(PKG_CONFIG) --cflags libseccomp)
LIBSECCOMP_LIBS   := $(shell $(PKG_CONFIG) --libs libseccomp)
override CPPFLAGS += $(LIBSECCOMP_CFLAGS) -DWANT_SECCOMP
LIBS              += $(LIBSECCOMP_LIBS)
endif

ifdef PV
override CPPFLAGS  += -DVERSION=\"$(PV)\"
else
VCSID     := $(shell git describe --tags HEAD)
endif
override CPPFLAGS  += -DVCSID='"$(VCSID)"'

####################################################################
ELF_TARGETS  = scanelf dumpelf $(shell echo | $(CC) -dM -E - | grep -q __svr4__ || echo pspax)
ELF_OBJS     = paxelf.o
MACH_TARGETS = scanmacho
MACH_OBJS    = paxmacho.o
COMMON_OBJS  = paxinc.o security.o xfuncs.o
TARGETS      = $(ELF_TARGETS) $(MACH_TARGETS)
SCRIPTS_SH   = lddtree symtree
SCRIPTS_PY   = lddtree
OBJS         = $(ELF_OBJS) $(MACH_OBJS) $(COMMON_OBJS) $(TARGETS:%=%.o)
MPAGES       = $(TARGETS:%=man/%.1)
SOURCES      = $(OBJS:%.o=%.c)

all: $(OBJS) $(TARGETS)
	@:

DEBUG_FLAGS = \
	-nopie \
	-fsanitize=address \
	-fsanitize=leak \
	-fsanitize=undefined
debug: clean
	$(MAKE) CFLAGS="$(CFLAGS) -g3 -ggdb $(call check_gcc_many,$(DEBUG_FLAGS))" all
	@-chpax  -permsx $(ELF_TARGETS)
	@-paxctl -permsx $(ELF_TARGETS)

analyze: clean
	scan-build $(MAKE) all

fuzz: clean
	$(MAKE) AFL_HARDEN=1 CC=afl-gcc all
	@rm -rf findings
	@printf '\nNow run:\n%s\n' \
		"afl-fuzz -t 100 -i tests/fuzz/small/ -o findings/ ./scanelf -s '*' -axetrnibSDIYZB @@"

compile.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(CPPFLAGS-$<) -o $@ -c $<

ifeq ($(V),)
Q := @
else
Q :=
endif
%.o: %.c
ifeq ($(V),)
	@echo $(compile.c)
endif
	$(Q)$(compile.c) $(WFLAGS)

$(ELF_TARGETS): %: $(ELF_OBJS) $(COMMON_OBJS) %.o
	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(LIBS) $(LIBS-$@)

$(MACH_TARGETS): %: $(MACH_OBJS) $(COMMON_OBJS) %.o
	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(LIBS) $(LIBS-$@)

%.so: %.c
	$(CC) -shared -fPIC -o $@ $<

depend:
	$(CC) $(CFLAGS) -MM $(SOURCES) > .depend

clean:
	-rm -f $(OBJS) $(TARGETS)

distclean: clean
	-rm -f *~ core *.o
	-cd man && $(MAKE) clean
strip: all
	$(STRIP) $(TARGETS)
strip-more:
	$(STRIP) --strip-unneeded $(TARGETS)

install: all
	$(MKDIR) $(PREFIX)/bin/ $(MANDIR)/man1/ $(PKGDOCDIR)/
	for sh in $(SCRIPTS_SH) ; do $(INS_EXE) $$sh.sh $(PREFIX)/bin/$$sh || exit $$? ; done
ifneq ($(USE_PYTHON),no)
	for py in $(SCRIPTS_PY) ; do $(INS_EXE) $$py.py $(PREFIX)/bin/$$py || exit $$? ; done
endif
	$(INS_EXE) $(TARGETS) $(PREFIX)/bin/
	$(INS_DATA) README BUGS TODO $(PKGDOCDIR)/
	$(INS_DATA) $(MPAGES) $(MANDIR)/man1/

PN = pax-utils
P = $(PN)-$(PV)
dist:
	./make-tarball.sh $(DISTCHECK) $(PV)
distcheck:
	$(MAKE) dist DISTCHECK=--check

-include .depend

check test:
	$(MAKE) -C tests

.PHONY: all check clean dist install test

#
# All logic related to autotools is below here
#
GEN_MARK_START = \# @@@ GEN START @@@ \#
GEN_MARK_END   = \# @@@ GEN END @@@ \#
EXTRA_DIST     = $(shell git ls-files | grep -v ^travis/)
autotools-update:
	$(MAKE) -C man -j
	sed -i.tmp '/^$(GEN_MARK_START)$$/,/^$(GEN_MARK_END)$$/d' Makefile.am
	@rm -f Makefile.am.tmp
	( \
		echo "$(GEN_MARK_START)"; \
		printf 'dist_man_MANS +='; \
		printf ' \\\n\t%s' `printf '%s\n' man/*.1 | LC_ALL=C sort`; \
		echo; \
		printf 'EXTRA_DIST +='; \
		printf ' \\\n\t%s' $(EXTRA_DIST); \
		echo; \
		echo "$(GEN_MARK_END)"; \
	) >> Makefile.am
autotools:
ifeq ($(SKIP_AUTOTOOLS_UPDATE),)
	$(MAKE) autotools-update
endif
	./autogen.sh --from=make

.PHONY: autotools autotools-update _autotools-update