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 <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2019-10-25 12:12:22 +02:00
parent e1a7c97ac6
commit af7169b4a7
5 changed files with 27 additions and 7 deletions

View File

@ -411,7 +411,7 @@ extern struct test_statics *const test_ptr_to_statics;
#define leaving (S.leaving ) #define leaving (S.leaving )
#define INIT_S() do { \ #define INIT_S() do { \
(*(struct test_statics**)&test_ptr_to_statics) = xzalloc(sizeof(S)); \ (*(struct test_statics**)not_const_pp(&test_ptr_to_statics)) = xzalloc(sizeof(S)); \
barrier(); \ barrier(); \
} while (0) } while (0)
#define DEINIT_S() do { \ #define DEINIT_S() do { \

View File

@ -2153,12 +2153,32 @@ struct globals;
* Magic prevents ptr_to_globals from going into rodata. * Magic prevents ptr_to_globals from going into rodata.
* If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */ * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
extern struct globals *const ptr_to_globals; 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 */ /* At least gcc 3.4.6 on mipsel system needs optimization barrier */
#define barrier() __asm__ __volatile__("":::"memory") #define barrier() __asm__ __volatile__("":::"memory")
#define SET_PTR_TO_GLOBALS(x) do { \ #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(); \ barrier(); \
} while (0) } while (0)
#define FREE_PTR_TO_GLOBALS() do { \ #define FREE_PTR_TO_GLOBALS() do { \
if (ENABLE_FEATURE_CLEAN_UP) { \ if (ENABLE_FEATURE_CLEAN_UP) { \
free(ptr_to_globals); \ free(ptr_to_globals); \

View File

@ -304,7 +304,7 @@ void lbb_prepare(const char *applet
IF_FEATURE_INDIVIDUAL(, char **argv)) IF_FEATURE_INDIVIDUAL(, char **argv))
{ {
#ifdef __GLIBC__ #ifdef __GLIBC__
(*(int **)&bb_errno) = __errno_location(); (*(int **)not_const_pp(&bb_errno)) = __errno_location();
barrier(); barrier();
#endif #endif
applet_name = applet; applet_name = applet;

View File

@ -203,7 +203,7 @@ extern struct lineedit_statics *const lineedit_ptr_to_statics;
#define delbuf (S.delbuf ) #define delbuf (S.delbuf )
#define INIT_S() do { \ #define INIT_S() do { \
(*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \ (*(struct lineedit_statics**)not_const_pp(&lineedit_ptr_to_statics)) = xzalloc(sizeof(S)); \
barrier(); \ barrier(); \
cmdedit_termw = 80; \ cmdedit_termw = 80; \
IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \ IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \

View File

@ -489,7 +489,7 @@ extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc;
#define random_gen (G_misc.random_gen ) #define random_gen (G_misc.random_gen )
#define backgndpid (G_misc.backgndpid ) #define backgndpid (G_misc.backgndpid )
#define INIT_G_misc() do { \ #define INIT_G_misc() do { \
(*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ (*(struct globals_misc**)not_const_pp(&ash_ptr_to_globals_misc)) = xzalloc(sizeof(G_misc)); \
barrier(); \ barrier(); \
curdir = nullstr; \ curdir = nullstr; \
physdir = nullstr; \ physdir = nullstr; \
@ -1542,7 +1542,7 @@ extern struct globals_memstack *BB_GLOBAL_CONST ash_ptr_to_globals_memstack;
#define g_stacknleft (G_memstack.g_stacknleft) #define g_stacknleft (G_memstack.g_stacknleft)
#define stackbase (G_memstack.stackbase ) #define stackbase (G_memstack.stackbase )
#define INIT_G_memstack() do { \ #define INIT_G_memstack() do { \
(*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ (*(struct globals_memstack**)not_const_pp(&ash_ptr_to_globals_memstack)) = xzalloc(sizeof(G_memstack)); \
barrier(); \ barrier(); \
g_stackp = &stackbase; \ g_stackp = &stackbase; \
g_stacknxt = stackbase.space; \ g_stacknxt = stackbase.space; \
@ -2165,7 +2165,7 @@ extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var;
#endif #endif
#define INIT_G_var() do { \ #define INIT_G_var() do { \
unsigned i; \ unsigned i; \
(*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ (*(struct globals_var**)not_const_pp(&ash_ptr_to_globals_var)) = xzalloc(sizeof(G_var)); \
barrier(); \ barrier(); \
for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
varinit[i].flags = varinit_data[i].flags; \ varinit[i].flags = varinit_data[i].flags; \