title: Cortex-M3 realtime interrupt vector remap
body: <p>I use GCC 4.9 (arm-none-eabi) with STM32 and want to place an interrupt table into an array in order to change interrup handler adresses when it's needed in my code.<br>
  I read existing manuals and articles and did the following:</p>
  
  <p>I have to align the array, so I changed the linker script to add custom section in RAM and place it to 0x20001000 in order to automatically align it</p>
  
  <pre><code> .vectorsSection 0x20001000 :
    {    
      KEEP(*(.vectorsSection))       
    } &gt;RAM
  </code></pre>
  
  <p>Declare array to place IVT into, I declared it as extern in the header, and in .cc:</p>
  
  <pre><code>volatile word __attribute__((section (".vectorsSection"))) _vectors_[64] = {0};
  </code></pre>
  
  <p>Check that array is at the correct address:</p>
  
  <pre><code>arm-none-eabi-nm program.elf | grep _vectors_
  20001000 d _ZL9_vectors_
  </code></pre>
  
  <p>Now it comes to reallocate the table to RAM. I wrote this function</p>
  
  <pre><code>void (*new_code_entry)(void);
  .......
  static void remap_vector_table (void)
  {
    //VTOR is 0 on startup, so we change VTOR only once
    if(SCB-&gt;VTOR)
      return;
    new_code_entry = (void (*)(void))((word)&amp;_vectors_ + sizeof(word) + 1);//Skip SP and jump to Reset
    memcpy((void*)_vectors_, (void*)SCB-&gt;VTOR, sizeof _vectors_);
    SCB-&gt;VTOR = 0x1FFFFF80ul &amp; (word)(&amp;_vectors_); //Set VTOR offset
    __DSB(); //Complete all memory requests
    new_code_entry(); //Jump to new code
  }
  </code></pre>
  
  <p>I created enum from startup code in order to easy access the array.<br>
  After the final jump the code start from beginning and VTOR is 4096.<br>
  Array contain correct addresses in the same order as in the startup code.<br>
  But when it comes to </p>
  
  <pre><code>__enable_irq();
  __ISB();
  </code></pre>
  
  <p>It hangs on the first exception, to be more specific this is the callstack</p>
  
  <pre><code>5 &lt;symbol is not available&gt; 0x697b617a   
  4 &lt;signal handler called&gt;() 0xfffffff9   
  3 &lt;symbol is not available&gt; 0x200011f8  
  2 remap_vector_table() main.cc:31 0x08000cd4
  1 main() main.cc:46 0x08000d32 
  
  200011f4:  tickcounter+0   movs r0, r0
  200011f6:  tickcounter+2   movs r0, r0
  200011f8:  ; &lt;UNDEFINED&gt; instruction: 0xf0d3e321
  </code></pre>
  
  <p>tickcounter is from SysTick_Handler that is surelly was called first.
  May be I should do something with stack pointer?
  I have no clue what is wrong here.</p>