Distro Planets

Steinar H. Gunderson explains __builtin_memcpy decompilation patterns

Key Takeaway

Inlined and unrolled memcpy operations can be identified in decompiled code as repetitive block transfers.

Summary

In the twelfth part of his decompilation patterns series, Steinar H. Gunderson discusses how GCC's __builtin_memcpy appears in decompiled output. He explains that what looks like manual loop-based data copying is often an optimized, unrolled memory copy. The article provides guidance on how to simplify these patterns back into readable C code.

Identifying unrolled memcpy

  • Decompiled code often shows repetitive assignments (e.g., copying 16 bytes at a time) instead of a single function call.
  • This pattern stems from GCC's __builtin_memcpy, which can occur implicitly during struct or array initializations.
  • Depending on variable alignment, the resulting assembly may use standard load words (lw) or more complex sequences like lwr/lwl.
  • To clean up the code, developers should replace these loops with a standard __builtin_memcpy call or direct array initialization.

Original reference: blog.sesse.net →