<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>AVR .initN sections and GCC -flto</title>
 <link rel="stylesheet" type="text/css" href="/style.css">
</head>
<body>
 <header>
   <h1 class="title">AVR .initN sections and GCC -flto</h1>
   <p class="date">2013-02-11</p>
 </header>
<p>While programming with avr-gcc I recently ran into a problem. Code placed into the .init3 section with <code>__attribute__((section(".init3")))</code> wasn't being included in GCC's output. The culprit ended up being GCC's -flto optimization setting. GCC's link-time optimization (LTO) feature aggressively performs garbage collection on unused bits of code (among other things). The AVR linker scripts included with binutils know to always include the .initN and .finiN sections, but by that time LTO has already discarded them. So the moral of the story is you must add <code>__attribute__((used))</code> to your function:</p>
<pre><code>void func(void) __attribute__((section(&quot;.init3&quot;), naked, used));

void func(void)
{
   /* code */
}</code></pre>
</body>
</html>