GCC assumes it can emit calls to memcpy(), memmove(), memset(), and memcmp() at any point - including inside your attempt at implementing one of those four functions. As the optimizer gets smarter, it will get better at creating endless recursion loops.
Various GCC bug reports suggest the following function attribute:
Code:
__attribute__((optimize("no-tree-loop-distribute-patterns")))
You can also disable this optimization at a global level, although that seems like a poor choice.
You can also implement those four functions in assembly, to be sure GCC can never create an endless recursion loop.
You can also use Clang, which seems to automatically avoid infinite recursion and/or emitting C library calls in freestanding mode.
nexos wrote:
It might be better just to use __builtin_memset IMO.
No, __builtin_memset() is only an optimization hint. The optimizer may still translate __builtin_memset() into a memset() call, and then you'll have a link error due to the undefined function.