title: gcc/ld: Allow Code Placement And Removal of Unused Functions
body: <p>I am trying to allow specifying the location of particular symbols in my output binary without giving up the garbage collection feature of ld.  For example: if I have in <code>MyInclude.h</code></p>
  
  <pre><code>#ifndef MY_INCLUDE_H_
  #define MY_INCLUDE_H_
  
  void CalledFunc(void);
  void UncalledFunc(void);
  
  #endif
  </code></pre>
  
  <p>and a main program:</p>
  
  <pre><code>#include "MyInclude.h"
  int main(void)
  {
      CalledFunc();
      return 0;
  }
  </code></pre>
  
  <p>compiling with gcc with <code>ffunction-sections -fdata-sections</code> and linking with <code>--gc-sections</code> shows in the map file that <code>.text.UncalledFunc</code> has been removed.</p>
  
  <p>I now have a need where I have to place certain functions in different sections. In this example if <code>UncalledFunc()</code> did happen to be called, I want it to be in a special section.  I have a linker parameter file that looks something like:</p>
  
  <pre><code>MEMORY
  {
      FLASH (rx) : ORIGIN = 0x10000000, LENGTH = 512K
  }
  
  SECTIONS
  {
      .text.myregion ORIGIN(FLASH):
      {
          *(.text.myregion)
          *(.text.myregion*)
      } &gt;FLASH
      ASSERT ( . &lt;= 0x10010000, "Too much stuff in myregion!")
      .text :
      {
          *(.text)
          *(.text*)
      } &gt;FLASH
  }
  </code></pre>
  
  <p>And <code>UncalledFunc()</code> and <code>CalledFunc()</code> defined as:</p>
  
  <pre><code>void CalledFunc(void) __attribute__ ((section (".text.myregion")))
  {
      /* ... */
  }
  void UncalledFunc(void) __attribute__ ((section (".text.myregion")))
  {
      /* ... */
  }
  </code></pre>
  
  <p>In this case, it seems the function attribute overrides the per function section parameter sent to GCC. As such since both functions are in the same input section, they both appear in the output image. Is there a way to tell the linker to place <code>UncalledFunc()</code> in <code>.text.myregion</code> if it is called but still remove it if it is not? As shown above, <code>myregion</code> has limited space and it would optimal to not place anything there that wasn't necessary.</p>