Crash when cross compiling for ARM with GCC-8-2-0 and -ftree-loop-distribute-patterns

Martin Sebor msebor@gmail.com
Wed Oct 16 18:18:00 GMT 2019


On 10/16/19 7:17 AM, Josef Wolf wrote:
> Hello all,
> 
> I experience target crashing when cross compiling for ARM with
> -ftree-loop-distribute-patterns, which is enabled by the -O3 flag.
> 
> The crash happens in the startup code, before main() is called. This startup
> code looks like this:
> 
>   extern unsigned long _sidata; /* Set by the linker */
>   extern unsigned long _sdata;  /* Set by the linker */
>   extern unsigned long _sbss; /* Set by the linker */
>   extern unsigned long _ebss;  /* Set by the linker */
>   
>    void Reet_Handler (void)
>    {
>      unsigned long *src = &_sidata
>      unsigned long *src = &_sdata
>    
>      /* Copy data segment into RAM */
>      if (src != dst) {
>        while (dst < &_edata)
>          *(dst++) = *(src++);
>      }
>    
>      /* Zero BSS segment */
>      dst = &_sbss;
>      while (dst < &_ebss)
>        *(dst++) = 0;
>    
>      main();
>    }
> 
> 
> With -ftree-loop-distribute-patterns those two loops are replaced by calls to
> memcpy() and memset().
> 
> The memcpy function finishes just fine. But the memset function doesn't seem
> to finish.  It looks like this:
> 
>    void memset (void *s, int c, size_t n)
>    {
>      int i;
>      for (i=0; i<n; i++)
>        ((char *)s)[i] = c;
>    }

This is probably not the cause of the crash but it's worth keeping
in mind.  The standard memset function returns void* and (unless
disabled) recent versions of GCC will issue a warning:

conflicting types for built-in function 'memset'; expected 'void *(void 
*, int,  unsigned int)' [-Wbuiltin-declaration-mismatch]

GCC expects a conforming memset and memcpy implementation even in
freestanding/embedded environments so defining these functions in
a different way could cause trouble.

> 
> Any ideas why this function is crashing? I can't see anything suspicious here.

I doubt it's the cause of the crash either but only addresses of
bytes of the same object can be used in relational expressions
(i.e., the two less-than controlling expressions).  Using address
to unrelated objects is undefined.  Concerns about invalidating
code like the above prevents compilers from implementing useful
optimizations.

GCC doesn't issue a warning for this bug yet but it might in
the future.   To avoid the undefined behavior and future
warnings, convert the addresses to uintptr_t first and compare
those instead.

Martin



More information about the Gcc-help mailing list