Function attribute for optimization level
Eric van Gyzen
vangyzen at FreeBSD.org
Sun Jun 4 22:44:35 UTC 2017
_thr_rtld_init() calls memcpy() for the sole purpose of resolving its PLT entry.
With clang 4.0 and the current code, compiler optimization defeats this attempt
by completely eliding the call. Other compilers or code might emit inline
instructions instead of the library call, also defeating the purpose.
I propose adding "__no_optimization" to sys/cdefs.h. The patch is below.
Empirical testing shows that clang 3.7 and later support "optnone", and gcc 4.6
and later support "optimize()". Clang 3.4 does not support either, so it takes
the define-to-empty case. I did not test clang 3.5 or 3.6.
Side note: GCC 4.6 with optimize(0) on amd64 emits two movq instructions for
memset(x,0,16), but GCC 5 emits a call to memset().
I have done no research to see if other popular codebases have such a
definition. If you know of one, please tell me; I would gladly adopt an already
common name for this proposal, for the sake of portability.
Thanks in advance for your feedback.
Eric
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index 9cdc03c861cb..e370f6d6459e 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -396,6 +396,14 @@
#define __unreachable() ((void)0)
#endif
+#if __has_attribute(optnone)
+#define __no_optimization __attribute__((optnone))
+#elif __has_attribute(optimize)
+#define __no_optimization __attribute__((optimize(0)))
+#else
+#define __no_optimization
+#endif
+
/* XXX: should use `#if __STDC_VERSION__ < 199901'. */
#if !__GNUC_PREREQ__(2, 7) && !defined(__INTEL_COMPILER)
#define __func__ NULL
More information about the freebsd-hackers
mailing list