m68k: Simple loop compiles into boundless recursion with -O2

Liu Hao lh_mouse@126.com
Fri Jan 15 02:23:13 GMT 2021

在 2021/1/14 上午12:30, Fredrik Noring 写道:
> Many thanks, Alexander,
>> Please invoke objdump with -dr instead to see the relocations.
> Indeed:
>   1a:	4eb9 0000 0000 	jsr 0 <memset2>
> 			1c: R_68K_32	memset
>> The relocation associated with this instruction should point to memset.
>> Most likely the compiler is optimizing your memset2 function to call
>> the standard function 'memset'.
>> When implementing memset itself you need to pass -ffreestanding to GCC,
>> which will disable this optimization.

I used to run into the same issue around CRT code on x86. Use of `-ffreestanding` disables a number
of optimizations, for example, the compiler cannot optimize

    int data[4];
    memset(&data, 0, sizeof(data));

to a series of store operations, but leave it as a function call, which is rather overkill.

The issue in the original post can be resolved by writing through a pointer to `volatile char` like

    void *memset2(void *s, int c, unsigned int n)
        volatile char *b = s;
        for (unsigned int i = 0; i < n; i++)
            b[i] = c;
        return s;

Best regards,

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20210115/3f85495f/attachment.sig>

More information about the Gcc-help mailing list