ASH: why ash_ptr_to_globals_misc is declared as const pointer

Ralf Friedl Ralf.Friedl at online.de
Mon Jan 22 21:59:41 UTC 2018


Mike Frysinger wrote:
> On 20 Jan 2018 19:03, Ralf Friedl wrote:
>> Mike Frysinger wrote:
>>> the pointer itself is the thing that is const, not the memory it points to.
>>> this lets the compiler optimize the loads by generating relocations via the
>>> pointer ... there's the fixup at the initial load time, but after that, it's
>>> just offsets to a constant memory location.  but removing the const markings,
>>> the compiler has to reload the pointer everytime.
>> Actually, why does the comipler have to reload the pointer? In your
>> example, there is no other function called between accesses to the
>> pointer that might change the pointer, and anti-aliasing should let the
>> compiler know that the memory modified through the pointer can't be the
>> pointer itself because of the type difference.
> when i say reload, i don't mean in a single function.  i agree as you say that
> the compiler has no need based on its memory model.  i meant that on every call,
> the pointer has to be loaded because the compiler cannot know that it hasn't
> changed.  by having the pointer be const, gcc knows that it can be referred to
> directly.
>
> it's a pretty esoteric optimization/hack, but busybox has a lot of these things,
> and when you add them up together, it makes for a difference.  i'm not sure how
> this can be written portably though since we're pushing the limits of the
> compiler :/.
You say you don't mean in a single function, but your example with 
raise_exception shows that it does happen within a single function, so 
the question is, why does the compiler not recognize it?

Maybe because anti-aliasing is not enabled? I tried to compile busybox 
with -O3 instead of -O2, and I get at least some warnings, and together 
with -Wall they abort the compilation. I had to add 
"-Wno-error=format-overflow -Wno-error=format-truncation" to 
EXTRA_CFLAGS for gcc 7.2.1. One could argue whether gcc should warn for 
snprintf (in contrast to sprintf), but at the moment it does, at least 
unless the return value is used.


More information about the busybox mailing list