Putting Code of Files into Special Section with the GNU Linker


The GNU Linker (ld) is very, very powerful. This time I wanted to put all my Processor Expert generated code into its own dedicated section. This is useful for example to have a bootloader or a library inside a special area in FLASH. It was not obvious to me how to do this with the linker, with some search on the internet and some trial and errors, I finally managed that. And as always with exploring things, I have learnt something :-). So here is how I’m able to put the code of arbitrary files into its own dedicated section.

Code Section for Generated Code

Code Section for Generated Code

For this, I need to change the linker file. As in my setup Processor Expert is generating it, I need to disable it so it does not change it anymore. This is a setting in the CPU component, under Build Options:

Disabled Generation of Linker File in Processor Expert

Disabled Generation of Linker File in Processor Expert

Next, I’m modifying the linker file, and creating a new section (m_text2) where I want to put my dedicated code:

Modified Linker File with new Section

Modified Linker File with new Section

Next, I’m extending the linker file with my placement. For example to place the Cpu.o into my dedicated section, I use a new placement .generatedCode just before the .text placement:

  .generatedCode : 
  { 
    . = ALIGN(4);
    *Cpu.o (.text .text*) 
    . = ALIGN(4); 
  }  > m_text2 /* new section for my special code files */ 

  /* The program code and other data goes into INTERNAL_FLASH */
  .text :
  {
   ...

Note that I’m using ‘*Cpu.o’, that means that any object file matching this will end up in this section. So if I want to have all the object files from the Processor Expert Generated_Code folder, I simply can use this:

  .generatedCode : 
  { 
    . = ALIGN(4);
    *Generated_Code/*.o (.text .text*) 
    . = ALIGN(4); 
  }  > m_text2 /* new section for my special code files */

So this might be already all what you need.

However, if you get linker errors about duplicated symbols, then it means that the linker is pulling in things twice. To avoid that objects get linked twice, I use the EXCLUDE_FILE directive, with a modified original .text and .text* placement:

  .generatedCode : 
  { 
    . = ALIGN(4);
    *Generated_Code/*.o (.text .text*) 
    . = ALIGN(4); 
  }  > m_text2 /* new section for my special code files */ 
  
  
  /* The program code and other data goes into INTERNAL_FLASH */
  .text :
  {
    . = ALIGN(4);

    /* original placement: */
    /* *(.text)  */         /* .text sections (code) */
    /* *(.text*) */          /* .text* sections (code) */

    /* modified placement: need to exclude the ones I have in .generatedCode above */
    *(EXCLUDE_FILE(*Generated_Code/*.o).text .text*)  /* .text and .text* sections (code) */

    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    *(.glue_7)         /* glue arm to thumb code */
    *(.glue_7t)        /* glue thumb to arm code */
    *(.eh_frame)

    KEEP (*(.init))
    KEEP (*(.fini))

    . = ALIGN(4);

    _etext = .;        /* define a global symbols at end of code */
  } > m_text

And in the linker .map file I can verify that things worked:

Code of Files placed in dedicated section

Code of Files placed in dedicated section

Happy Linking 🙂

Advertisements

9 thoughts on “Putting Code of Files into Special Section with the GNU Linker

  1. Please forgive my ignorance. I understand the concept you describe here, and it is interesting.

    What I don’t understand is under what conditions would it be preferable to do things this way. What problems does it solve for the programmer?

    Thank you.

    Like

    • Say if you envision that you very likely might need to patch some code from some source files in our device later on, it is not a bad idea to have that ‘preliminary’ code in a certain section of the memory together, so you only will need to patch that area of memory.
      The other use case is if you have special code which e.g. needs to be protected or dealt in a special way in your device, you can place it into that special area. I agree that you can do this with section attributes in your code, but doing it on a file by file base is a nice other way to achieve this.

      Like

  2. Is it possible to place shared ARM GCC library objects into a custom memory section?
    When I open my project’s map file, I see the linker automatically place math lib functions (__aeabi_frsub, __mulsf3, etc_) at the start of .txt section. Then toward end of the same section after placement of my own app code, the linker places standard libray ojects such as libc.a. I want to move those objects to a special defined memory section which I specified in the linker script (.ld) file.

    Like

    • I would think that should be possible, but I have not done that myself. The easiest way to get control over this is if you would re-build the library yourself, then you can have them the way you want it.

      Like

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s