+# Meson currently only handles pie as a boolean for now, so if the user
+# has explicitly disabled PIE we need to extend our cflags.
+#
+# -no-pie is supposedly a linker flag that has no effect on the compiler
+# command line, but some distros, that didn't quite know what they were
+# doing, made local changes to gcc's specs file that turned it into
+# a compiler command-line flag.
+#
+# What about linker flags? For a static build, no PIE is implied by -static
+# which we added above (and if it's not because of the same specs patching,
+# there's nothing we can do: compilation will fail, report a bug to your
+# distro and do not use --disable-pie in the meanwhile). For dynamic linking,
+# instead, we can't add -no-pie because it overrides -shared: the linker then
+# tries to build an executable instead of a shared library and fails. So
+# don't add -no-pie anywhere and cross fingers. :(
+if not get_option('b_pie')
+ qemu_common_flags += cc.get_supported_arguments('-fno-pie', '-no-pie')
+endif
+
+if not get_option('stack_protector').disabled()
+ stack_protector_probe = '''
+ int main(int argc, char *argv[])
+ {
+ char arr[64], *p = arr, *c = argv[argc - 1];
+ while (*c) {
+ *p++ = *c++;
+ }
+ return 0;
+ }'''
+ have_stack_protector = false
+ foreach arg : ['-fstack-protector-strong', '-fstack-protector-all']
+ # We need to check both a compile and a link, since some compiler
+ # setups fail only on a .c->.o compile and some only at link time
+ if cc.compiles(stack_protector_probe, args: ['-Werror', arg]) and \
+ cc.links(stack_protector_probe, args: ['-Werror', arg])
+ have_stack_protector = true
+ qemu_cflags += arg
+ qemu_ldflags += arg
+ break
+ endif
+ endforeach
+ get_option('stack_protector') \
+ .require(have_stack_protector, error_message: 'Stack protector not supported')
+endif
+
+coroutine_backend = get_option('coroutine_backend')
+ucontext_probe = '''
+ #include <ucontext.h>
+ #ifdef __stub_makecontext
+ #error Ignoring glibc stub makecontext which will always fail
+ #endif
+ int main(void) { makecontext(0, 0, 0); return 0; }'''
+
+# On Windows the only valid backend is the Windows specific one.
+# For POSIX prefer ucontext, but it's not always possible. The fallback
+# is sigcontext.
+supported_backends = []
+if targetos == 'windows'
+ supported_backends += ['windows']
+else
+ if targetos != 'darwin' and cc.links(ucontext_probe)
+ supported_backends += ['ucontext']
+ endif
+ supported_backends += ['sigaltstack']
+endif
+
+if coroutine_backend == 'auto'
+ coroutine_backend = supported_backends[0]
+elif coroutine_backend not in supported_backends
+ error('"@0@" backend requested but not available. Available backends: @1@' \
+ .format(coroutine_backend, ', '.join(supported_backends)))
+endif
+
+# Compiles if SafeStack *not* enabled
+safe_stack_probe = '''
+ int main(void)
+ {
+ #if defined(__has_feature)
+ #if __has_feature(safe_stack)
+ #error SafeStack Enabled
+ #endif
+ #endif
+ return 0;
+ }'''
+if get_option('safe_stack') != not cc.compiles(safe_stack_probe)
+ safe_stack_arg = get_option('safe_stack') ? '-fsanitize=safe-stack' : '-fno-sanitize=safe-stack'
+ if get_option('safe_stack') != not cc.compiles(safe_stack_probe, args: safe_stack_arg)
+ error(get_option('safe_stack') \
+ ? 'SafeStack not supported by your compiler' \
+ : 'Cannot disable SafeStack')
+ endif
+ qemu_cflags += safe_stack_arg
+ qemu_ldflags += safe_stack_arg
+endif
+if get_option('safe_stack') and coroutine_backend != 'ucontext'
+ error('SafeStack is only supported with the ucontext coroutine backend')
+endif
+
+if get_option('sanitizers')
+ if cc.has_argument('-fsanitize=address')
+ qemu_cflags = ['-fsanitize=address'] + qemu_cflags
+ qemu_ldflags = ['-fsanitize=address'] + qemu_ldflags
+ endif
+
+ # Detect static linking issue with ubsan - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84285
+ if cc.links('int main(int argc, char **argv) { return argc + 1; }',
+ args: [qemu_ldflags, '-fsanitize=undefined'])
+ qemu_cflags = ['-fsanitize=undefined'] + qemu_cflags
+ qemu_ldflags = ['-fsanitize=undefined'] + qemu_ldflags
+ endif
+endif
+
+# Thread sanitizer is, for now, much noisier than the other sanitizers;
+# keep it separate until that is not the case.
+if get_option('tsan')
+ if get_option('sanitizers')
+ error('TSAN is not supported with other sanitizers')
+ endif
+ if not cc.has_function('__tsan_create_fiber',
+ args: '-fsanitize=thread',
+ prefix: '#include <sanitizer/tsan_interface.h>')
+ error('Cannot enable TSAN due to missing fiber annotation interface')
+ endif
+ qemu_cflags = ['-fsanitize=thread'] + qemu_cflags
+ qemu_ldflags = ['-fsanitize=thread'] + qemu_ldflags
+endif
+