title: UsageFault when branching to a function pointer on Cortex-M0
body: <p>I'm running code on an STM32F0 (ARM Cortex-M0). I define a function pointer to a <a href="https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html" rel="nofollow noreferrer">nested function</a>:</p>
  
  <pre><code>void My_Async_Func(void *handle, void (*complete)(bool success)) {
      /* 
       *  \/ A nested function \/
       */
      void receiveHandler(void) {
          // This function lies at an even-numbered address
      }
      /* ... */
      uart-&gt;rxDoneHandler = &amp;receiveHandler;
      Go(uart);
  }
  </code></pre>
  
  <p>The nested function appears to be screwing things up. Later when I call that <code>rxDoneHandler</code> pointer, it tries to branch to <code>0x8c0c6c0c</code> and I get a UsageFault. According to the <a href="http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka12544.html" rel="nofollow noreferrer">ARM docs</a>, this is because I'm branching to an <em>even-numbered</em> address on a processor that only supports the Thumb instruction set, which requires you to only branch to <em>odd-numbered</em> addresses. </p>
  
  <p>I'm using GCC arm-none-eabi 4.9.3. My CFLAGS are:</p>
  
  <pre><code>arm-none-eabi-gcc -mcpu=cortex-m0 -mthumb -mfloat-abi=soft -O0 -g3 -Wall -fmessage-length=0 -ffunction-sections -c -MMD -MP 
  </code></pre>
  
  <h3>Things I tried</h3>
  
  <p>I tried calling gcc with <code>-mthumb-interwork</code> as suggested <a href="https://stackoverflow.com/a/32976472/1161906">here</a>. Same result.</p>
  
  <p>I tried manually setting the alignment of the function with an attribute like </p>
  
  <pre><code>__attribute__((aligned(16))) void receiveHandler(void) {
  
  }
  </code></pre>
  
  <p>Same result. </p>
  
  <p>I tried manually adding 1 to the pointer when I call it like</p>
  
  <pre><code>(uart-&gt;rxDoneHandler + 1)();
  </code></pre>
  
  <p>Same result. </p>
  
  <p>Am I doing something wrong, or is this a bug in the compiler? </p>