How to implement a compile-time [dispatch] table for AVR?
<p>I have the same prerequisites as Dave Durbin in <a href="https://stackoverflow.com/questions/11840651/how-can-i-implement-a-dynamic-dispatch-table-in-c/32192330#32192330">How can I implement a dynamic dispatch table in C</a>... except my target is AVR. Here are my constraints:</p>

<ul>
<li>modules are to be picked in a list, much like Linux compiled-in kernel modules</li>
<li>the number of C (can be C++) modules is known at compile-time</li>
<li>modules are to be statically linked (obviously)</li>
<li>I want the table in program memory, <a href="https://stackoverflow.com/a/32195979/4113344">not in SRAM</a></li>
</ul>

<p>Typically, the table should comprise items of this type:</p>

<pre><code>typedef struct jump_item {
    uint16_t function_id;
    void (*callback)(void);
} jump_item_t;
</code></pre>

<p>I have tried using custom sections as suggested in the answer but then the linker throws an error for an unknown symbol <code>__start_myownsection</code> (whatever section name I use though). Of course since the code targets Linux/GCC. But I think I'm close because <code>avr-gcc</code> actually <em>can</em> use <a href="http://www.nongnu.org/avr-libc/user-manual/mem_sections.html" rel="nofollow noreferrer">sections</a>, just that I haven't been able to figure out yet how to stack symbols in a user-defined section and actually pointing to the beginning of the table, as well as determine the length of the table at run-time.</p>

<p>How could <a href="https://stackoverflow.com/a/11844418/4113344">Art's answer</a> be adapted to AVR?</p>

<hr>

<p><strong>* EDIT *</strong></p>

<p>I can see at least two ways to achieve what I want using sections, either with functions "attached" to a user-defined section or tables of structures (as defined above) that all will stack up in the user-defined section. My current issues are:</p>

<ul>
<li>unused variables are optimized away at compile-time!</li>
<li>unused functions are optimized away at link-time due to linker argument <code>-gc-sections</code>, which I need to clean unused functions.</li>
</ul>

<p>I prefer the second option, something similar to this:</p>

<p>module1.c:</p>

<pre><code>const jump_item_t module1_table[] __attribute__((__progmem__, section("tbl_dispatch"))) =
{
    { 0x02, func11 },
    { 0x03, func12 },
    ...
};
</code></pre>

<p>module2.c:</p>

<pre><code>const jump_item_t module2_table[] __attribute__((__progmem__, section("tbl_dispatch"))) =
{
    { 0x12, func21 },
    { 0x13, func22 },
    ...
};
</code></pre>

<p>Note: indices aren't to be considered relevant.</p>

<p>When all modules define such variables, they're optimized away as there's nowhere any reference to these. They need to stack up in section <code>tbl_dispatch</code> though. So my question falls back to:</p>

<p><em>How can I tell the compiler from removing variables it "thinks" are unused but only with specific C/C++ modules?</em></p>

<p>The global command line I'm using so far is as follows:</p>

<pre><code>avr-gcc -g -Wall -mcall-prologues -fshort-enums -Os \
    -DF_CPU=8000000 -Wl,-relax -mmcu=... \
    *.cpp *.c -o main
</code></pre>

<hr>

<p><strong>* EDIT *</strong></p>

<p>To my disappointment, <strong>PROGMEM</strong> and custom sections don't go together. I've tried to combine them but I get disseminated jump tables in program memory... when I get these included at all. Fact is not even all tables appear in program memory.</p>

<p>Giving up.</p>

<p><em>Any</em> idea welcome.</p>