@@ -33,6 +33,12 @@ cxx_core_flags_list = [
3333 ' -fstack-reuse=none' , # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90348
3434]
3535
36+ cxx_harden_flags_list = [
37+ ' -fcf-protection=full' ,
38+ ' -fstack-protector-all' ,
39+ ' -Wstack-protector' ,
40+ ]
41+
3642cxx_warn_flags_list = [
3743 ' -Wconditional-uninitialized' ,
3844 ' -Wdate-time' ,
@@ -55,8 +61,33 @@ cxx_warn_flags_list = [
5561 ' -Wvla' ,
5662]
5763
64+ # C++ linker flags
65+ cxx_harden_lflags_darwin_list = [
66+ ' -fixup_chains' ,
67+ ]
68+
69+ cxx_harden_lflags_linux_list = [
70+ ' -z,now' ,
71+ ' -z,relro' ,
72+ ' -z,separate-code' ,
73+ ]
74+
75+ cxx_harden_lflags_win64_list = [
76+ ' --dynamicbase' ,
77+ ' --enable-reloc-section' ,
78+ ' --high-entropy-va' ,
79+ ' --nxcompat' ,
80+ ]
81+
82+ # C++ preprocessor flags
83+ cxx_harden_release_preproc = [
84+ ' -D_FORTIFY_SOURCE=3' ,
85+ ' -U_FORTIFY_SOURCE' ,
86+ ]
87+
5888# Check compiler support for flags and apply them
5989cxx_flags = []
90+
6091foreach flag : cxx_core_flags_list + cxx_warn_flags_list
6192 if cxx.has_multi_arguments([' -Werror' , flag])
6293 cxx_flags += flag
@@ -68,7 +99,59 @@ if cxx.has_multi_arguments(['-Werror', '-Wformat', '-Wformat-security'])
6899 cxx_flags += [' -Wformat' , ' -Wformat-security' ]
69100endif
70101
71- add_project_arguments (cxx_flags, language : ' cpp' )
102+ harden_build = get_option (' harden_build' )
103+ if harden_build
104+ foreach flag : cxx_harden_flags_list
105+ if cxx.has_multi_arguments([' -Werror' , flag])
106+ cxx_flags += flag
107+ endif
108+ endforeach
109+
110+ if host_machine .cpu_family() == ' aarch64'
111+ if cxx.has_multi_arguments(' -Werror' , ' -mbranch-protection=bti' )
112+ cxx_flags += ' -mbranch-protection=bti'
113+ endif
114+ endif
115+
116+ # -fstack-clash-protection is a no-op on windows, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458
117+ if host_machine .system() != ' windows'
118+ if cxx.has_multi_arguments(' -Werror' , ' -fstack-clash-protection' )
119+ cxx_flags += ' -fstack-clash-protection'
120+ endif
121+ endif
122+ endif
123+
124+ cxx_lflags = []
125+
126+ if harden_build
127+ cxx_harden_lflags = []
128+ if host_machine .system() == ' darwin'
129+ cxx_harden_lflags = cxx_harden_lflags_darwin_list
130+ elif host_machine .system() == ' windows'
131+ cxx_harden_lflags = cxx_harden_lflags_win64_list
132+ else
133+ cxx_harden_lflags = cxx_harden_lflags_linux_list
134+ endif
135+ foreach flag : cxx_harden_lflags
136+ flag = ' -Wl,' + flag
137+ if cxx.has_multi_link_arguments([' -Werror' , flag])
138+ cxx_lflags += flag
139+ endif
140+ endforeach
141+ endif
142+
143+ cxx_preproc = []
144+
145+ if harden_build
146+ if get_option (' buildtype' ) != ' debug'
147+ foreach flag : cxx_harden_release_preproc
148+ cxx_preproc += flag
149+ endforeach
150+ endif
151+ endif
152+
153+ add_project_arguments (cxx_flags + cxx_preproc, language : ' cpp' )
154+ add_project_link_arguments (cxx_lflags, language : ' cpp' )
72155
73156rustdeps_build_dir = get_option (' rustdeps_build_dir' )
74157grovedb_cxx_build_dir = get_option (' grovedb_cxx_build_dir' )
@@ -259,6 +342,7 @@ install_headers(libgrovedb_headers, subdir: 'grovedb')
259342summary ({
260343 ' C++ compiler' : cxx.get_id(),
261344 ' C++ flags' : ' ' .join(cxx_flags),
345+ ' C++ linker flags' : ' ' .join(cxx_lflags),
262346 ' C++ standard' : get_option (' cpp_std' ),
263347}, section : ' Compiler' )
264348
0 commit comments