m68k: Simple loop compiles into boundless recursion with -O2
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.
> 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
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;
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 833 bytes
Desc: OpenPGP digital signature
More information about the Gcc-help