title: Getting the gcc linker to place multiple functions at the same address
body: <p>I have an embedded system with internal and external flash. The controller is an MSP430F22x2, the external flash is connected using SPI. I can load data from external flash but the external flash isn't memory mapped. Since the internal flash is not big enough I want to add functions into external flash. So the external flash has to be copied into internal flash, so that the functions can be executed. It is essentially like poor mans paging.</p>
  
  <p>To do all this I have a function, that loads the right part of flash and then calls the function. It manages an internal page stack, seperate to the 'real' stack. If I want to call a function on another page I have to jump to this function. The function then calls the function on the now loaded page. When the function returns the execution continues in the management function (because it was called from there) which then loads the right page and jumps back.</p>
  
  <p>The problem is, that the functions are all at the same location in memory, since they are copied. But the data in ram can't overlap. My current solution is to use seperate linker scripts for each parts of external code and setting the addresses of the ram sections manually. This is of course time consuming and everything has to be changed if a part needs more ram etc.</p>
  
  <p>The separate linker scripts look like this:</p>
  
  <pre><code>ENTRY(part)
  SECTIONS
  {
      .data 0x2A4 :
        {
          . = ALIGN(2);
          *(.data .rodata*)
        }
      .text 0x4000 :
        {
          *(.text)
        }
  }
  </code></pre>
  
  <p>Nothing fancy. Everything is set to specific addresses, no overflow is checked. To be able to call the functions I export the symbol table and add them to the main linker script. This is all automated using batch scripts.</p>
  
  <p><a href="https://i.stack.imgur.com/d7TTb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/d7TTb.png" alt="enter image description here"></a></p>
  
  <p>So I need a linker script that essentially places multiple .text sections overlapping in the same address range and the .data sections continuosly.</p>
  
  <p>I don't know if there is a way to do this. Really any solution (linker scripts, batch scripts etc.) would help me. The only thing I can't change is the hardware.</p>