在 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 > 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 this: 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, LH_Mouse