1# Copyright (C) 1998-2022 Free Software Foundation, Inc.
2# Copyright The GNU Toolchain Authors.
3# This file is part of the GNU C Library.
4
5# The GNU C Library is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9
10# The GNU C Library is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Lesser General Public License for more details.
14
15# You should have received a copy of the GNU Lesser General Public
16# License along with the GNU C Library; if not, see
17# <https://www.gnu.org/licenses/>.
18
19#
20#	Sub-makefile for debug portion of the library.
21#
22subdir	:= debug
23
24include ../Makeconfig
25
26headers	:= execinfo.h
27
28# Note that ptsname_r_chk and getlogin_r are not here, but in
29# login/Makefile instead.  If that subdir is omitted from the
30# build, its _FORTIFY_SOURCE support will be too.
31routines  = backtrace backtracesyms backtracesymsfd noophooks \
32	    memcpy_chk memmove_chk mempcpy_chk memset_chk stpcpy_chk \
33	    strcat_chk strcpy_chk strncat_chk strncpy_chk stpncpy_chk \
34	    sprintf_chk vsprintf_chk snprintf_chk vsnprintf_chk \
35	    printf_chk fprintf_chk vprintf_chk vfprintf_chk \
36	    gets_chk chk_fail readonly-area fgets_chk fgets_u_chk \
37	    read_chk pread_chk pread64_chk recv_chk recvfrom_chk \
38	    readlink_chk readlinkat_chk getwd_chk getcwd_chk \
39	    realpath_chk fread_chk fread_u_chk \
40	    wctomb_chk wcscpy_chk wmemcpy_chk wmemmove_chk wmempcpy_chk \
41	    wcpcpy_chk wcsncpy_chk wcscat_chk wcsncat_chk wmemset_chk \
42	    wcpncpy_chk \
43	    swprintf_chk vswprintf_chk wprintf_chk fwprintf_chk \
44	    vwprintf_chk vfwprintf_chk fgetws_chk fgetws_u_chk \
45	    confstr_chk getgroups_chk ttyname_r_chk \
46	    gethostname_chk getdomainname_chk wcrtomb_chk mbsnrtowcs_chk \
47	    wcsnrtombs_chk mbsrtowcs_chk wcsrtombs_chk mbstowcs_chk \
48	    wcstombs_chk asprintf_chk vasprintf_chk dprintf_chk \
49	    vdprintf_chk obprintf_chk vobprintf_chk \
50	    longjmp_chk ____longjmp_chk \
51	    fdelt_chk poll_chk ppoll_chk \
52	    explicit_bzero_chk \
53	    stack_chk_fail fortify_fail \
54	    $(static-only-routines)
55static-only-routines := stack_chk_fail_local
56
57# Don't add stack_chk_fail_local.o to libc.a since __stack_chk_fail_local
58# is an alias of __stack_chk_fail in stack_chk_fail.o.
59elide-routines.o := stack_chk_fail_local
60
61# Building the stack-protector failure routines with stack protection
62# is not required since we have already failed a stack check and are
63# exiting the process.  However, the local aliases which jump to the
64# real routines should still be compiled with stack protection
65# (stack_chk_fail_local.c), so that the statically linked parts of the
66# library have the expected flags.
67
68CFLAGS-stack_chk_fail.c += $(no-stack-protector)
69
70CFLAGS-backtrace.c += -fno-omit-frame-pointer -funwind-tables
71CFLAGS-sprintf_chk.c += $(libio-mtsafe)
72CFLAGS-snprintf_chk.c += $(libio-mtsafe)
73CFLAGS-vsprintf_chk.c += $(libio-mtsafe)
74CFLAGS-vsnprintf_chk.c += $(libio-mtsafe)
75CFLAGS-asprintf_chk.c += $(libio-mtsafe)
76CFLAGS-vasprintf_chk.c += $(libio-mtsafe)
77CFLAGS-obprintf_chk.c += $(libio-mtsafe)
78CFLAGS-dprintf_chk.c += $(libio-mtsafe) -fexceptions
79CFLAGS-vdprintf_chk.c += $(libio-mtsafe) -fexceptions
80CFLAGS-printf_chk.c += $(libio-mtsafe) -fexceptions
81CFLAGS-fprintf_chk.c += $(libio-mtsafe) -fexceptions
82CFLAGS-vprintf_chk.c += $(libio-mtsafe) -fexceptions
83CFLAGS-vfprintf_chk.c += $(libio-mtsafe) -fexceptions
84CFLAGS-gets_chk.c += $(libio-mtsafe) -fexceptions
85CFLAGS-fgets_chk.c += $(libio-mtsafe) -fexceptions
86CFLAGS-fgets_u_chk.c += $(libio-mtsafe) -fexceptions
87CFLAGS-fread_chk.c += $(libio-mtsafe) -fexceptions
88CFLAGS-fread_u_chk.c += $(libio-mtsafe) -fexceptions
89CFLAGS-swprintf_chk.c += $(libio-mtsafe)
90CFLAGS-vswprintf_chk.c += $(libio-mtsafe)
91CFLAGS-wprintf_chk.c += $(libio-mtsafe) -fexceptions
92CFLAGS-fwprintf_chk.c += $(libio-mtsafe) -fexceptions
93CFLAGS-vwprintf_chk.c += $(libio-mtsafe) -fexceptions
94CFLAGS-vfwprintf_chk.c += $(libio-mtsafe) -fexceptions
95CFLAGS-fgetws_chk.c += $(libio-mtsafe) -fexceptions
96CFLAGS-fgetws_u_chk.c += $(libio-mtsafe) -fexceptions
97CFLAGS-read_chk.c += -fexceptions -fasynchronous-unwind-tables
98CFLAGS-pread_chk.c += -fexceptions -fasynchronous-unwind-tables
99CFLAGS-pread64_chk.c += -fexceptions -fasynchronous-unwind-tables
100CFLAGS-recv_chk.c += -fexceptions -fasynchronous-unwind-tables
101CFLAGS-recvfrom_chk.c += -fexceptions -fasynchronous-unwind-tables
102
103# Need to make sure the settings here override what configure might have
104# set up for us, so keep the CFLAGS/CPPFLAGS split logical as the order is:
105# <user CFLAGS> <test CFLAGS> <user CPPFLAGS> <test CPPFLAGS>
106CFLAGS-tst-longjmp_chk.c += -fexceptions -fasynchronous-unwind-tables
107CPPFLAGS-tst-longjmp_chk.c += -D_FORTIFY_SOURCE=1
108CFLAGS-tst-longjmp_chk2.c += -fexceptions -fasynchronous-unwind-tables
109CPPFLAGS-tst-longjmp_chk2.c += -D_FORTIFY_SOURCE=1
110CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
111CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
112CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
113CPPFLAGS-tst-read-chk-cancel.c += -D_FORTIFY_SOURCE=2
114
115# _FORTIFY_SOURCE tests.
116# Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and
117# preprocessor conditions based on tst-fortify.c.
118#
119# To add a new test condition, define a cflags-$(cond) make variable to set
120# CFLAGS for the file.
121
122tests-all-chk = tst-fortify
123tests-c-chk =
124tests-cc-chk =
125
126CFLAGS-tst-fortify.c += -Wno-format -Wno-deprecated-declarations -Wno-error
127
128# No additional flags for the default tests.
129define cflags-default
130endef
131
132define cflags-lfs
133CFLAGS-tst-fortify-$(1)-lfs-$(2).$(1) += -D_FILE_OFFSET_BITS=64
134endef
135
136define cflags-nongnu
137CFLAGS-tst-fortify-$(1)-nongnu-$(2).$(1) += -D_LARGEFILE64_SOURCE=1
138endef
139
140src-chk-nongnu = \#undef _GNU_SOURCE
141
142# We know these tests have problems with format strings, this is what
143# we are testing.  Disable that warning.  They are also testing
144# deprecated functions (notably gets) so disable that warning as well.
145# And they also generate warnings from warning attributes, which
146# cannot be disabled via pragmas, so require -Wno-error to be used.
147define gen-chk-test
148tests-$(1)-chk += tst-fortify-$(1)-$(2)-$(3)
149CFLAGS-tst-fortify-$(1)-$(2)-$(3).$(1) += -D_FORTIFY_SOURCE=$(3) -Wno-format \
150					  -Wno-deprecated-declarations \
151					  -Wno-error
152$(eval $(call cflags-$(2),$(1),$(3)))
153$(objpfx)tst-fortify-$(1)-$(2)-$(3).$(1): tst-fortify.c Makefile
154	( echo "/* Autogenerated from Makefile.  */"; \
155	  echo "$(src-chk-$(2))"; \
156	  echo "#include \"tst-fortify.c\"" ) > $$@.tmp
157	mv $$@.tmp $$@
158endef
159
160chk-extensions = c cc
161chk-types = default lfs nongnu
162chk-levels = 1 2 3
163
164$(foreach e,$(chk-extensions), \
165  $(foreach t,$(chk-types), \
166    $(foreach l,$(chk-levels), \
167      $(eval $(call gen-chk-test,$(e),$(t),$(l))))))
168
169tests-all-chk += $(tests-c-chk) $(tests-cc-chk)
170
171define link-cc
172LDLIBS-$(1) = -lstdc++
173endef
174$(foreach t,$(tests-cc-chk), $(eval $(call link-cc,$(t))))
175
176# backtrace_symbols only works if we link with -rdynamic.  backtrace
177# requires unwind tables on most architectures.
178CFLAGS-tst-backtrace2.c += -funwind-tables
179CFLAGS-tst-backtrace3.c += -funwind-tables
180CFLAGS-tst-backtrace4.c += -funwind-tables
181CFLAGS-tst-backtrace5.c += -funwind-tables
182CFLAGS-tst-backtrace6.c += -funwind-tables
183LDFLAGS-tst-backtrace2 = -rdynamic
184LDFLAGS-tst-backtrace3 = -rdynamic
185LDFLAGS-tst-backtrace4 = -rdynamic
186LDFLAGS-tst-backtrace5 = -rdynamic
187LDFLAGS-tst-backtrace6 = -rdynamic
188
189CFLAGS-tst-ssp-1.c += -fstack-protector-all
190
191tests = backtrace-tst \
192	tst-longjmp_chk \
193	test-strcpy_chk \
194	test-stpcpy_chk \
195	tst-longjmp_chk2 \
196	tst-backtrace2 \
197	tst-backtrace3 \
198	tst-backtrace4 \
199	tst-backtrace5 \
200	tst-backtrace6 \
201	tst-realpath-chk \
202	$(tests-all-chk)
203
204ifeq ($(have-ssp),yes)
205tests += tst-ssp-1
206endif
207
208ifeq ($(have-thread-library), yes)
209tests += tst-read-chk-cancel
210endif
211
212ifeq (,$(CXX))
213tests-unsupported = $(tests-cc-chk)
214endif
215
216extra-libs = libpcprofile
217extra-libs-others = $(extra-libs)
218
219libpcprofile-routines = pcprofile
220libpcprofile-inhibit-o = $(filter-out .os,$(object-suffixes))
221
222others = pcprofiledump
223install-bin = pcprofiledump
224install-bin-script = xtrace
225
226generated += xtrace
227
228include ../Rules
229
230ifeq ($(run-built-tests),yes)
231LOCALES := de_DE.UTF-8
232include ../gen-locales.mk
233
234define chk-gen-locales
235$(objpfx)$(1).out: $(gen-locales)
236endef
237$(foreach t, $(tests-all-chk), $(eval $(call chk-gen-locales,$(t))))
238endif
239
240sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
241
242$(objpfx)pcprofiledump: $(objpfx)pcprofiledump.o
243
244$(objpfx)xtrace: xtrace.sh
245	rm -f $@.new
246	sed -e 's|@VERSION@|$(version)|' -e 's|@SLIBDIR@|$(sLIBdir)|' \
247	    -e 's|@BINDIR@|$(bindir)|' -e 's|@PKGVERSION@|$(PKGVERSION)|' \
248	    -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \
249	&& rm -f $@ && mv $@.new $@ && chmod +x $@
250
251$(objpfx)tst-read-chk-cancel: $(shared-thread-library)
252