From af7169b4a70eb3f60555ced17a40780f70aaaa5c Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 25 Oct 2019 12:12:22 +0200 Subject: clang/llvm 9 fix - do not eliminate a store to a fake "const" This is *much* better (9 kbytes better) than dropping "*const" optimization trick. Signed-off-by: Denys Vlasenko --- include/libbb.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/libbb.h b/include/libbb.h index 111d1b790..05a560977 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -2153,12 +2153,32 @@ struct globals; * Magic prevents ptr_to_globals from going into rodata. * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */ extern struct globals *const ptr_to_globals; + +#if defined(__clang_major__) && __clang_major__ >= 9 +/* Clang/llvm drops assignment to "constant" storage. Silently. + * Needs serious convincing to not eliminate the store. + */ +static ALWAYS_INLINE void* not_const_pp(const void *p) +{ + void *pp; + __asm__ __volatile__( + "# forget that p points to const" + : /*outputs*/ "=r" (pp) + : /*inputs*/ "0" (p) + ); + return pp; +} +#else +static ALWAYS_INLINE void* not_const_pp(const void *p) { return (void*)p; } +#endif + /* At least gcc 3.4.6 on mipsel system needs optimization barrier */ #define barrier() __asm__ __volatile__("":::"memory") #define SET_PTR_TO_GLOBALS(x) do { \ - (*(struct globals**)&ptr_to_globals) = (void*)(x); \ + (*(struct globals**)not_const_pp(&ptr_to_globals)) = (void*)(x); \ barrier(); \ } while (0) + #define FREE_PTR_TO_GLOBALS() do { \ if (ENABLE_FEATURE_CLEAN_UP) { \ free(ptr_to_globals); \ -- cgit v1.2.3