Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Putting Code of Files into Special Section with the GNU Linker

DZone's Guide to

Putting Code of Files into Special Section with the GNU Linker

· IoT Zone
Free Resource

Cisco IoT makes digital transformation a reality in factories, transportation, and utilities. Learn how to start integrating with Cisco DevNet.

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:

01.   .generatedCode :
02.   {
03.   . = ALIGN(4);
04.   *Cpu.o (.text .text*)
05.   . = ALIGN(4);
06.   }  > m_text2 /* new section for my special code files */
07.  
08.   /* The program code and other data goes into INTERNAL_FLASH */
09.   .text :
10.   {
11.    ...

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:

1.   .generatedCode :
2.   {
3.   . = ALIGN(4);
4.   *Generated_Code/*.o (.text .text*)
5.   . = ALIGN(4);
6.   }  > 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:

01.   .generatedCode :
02.   {
03.   . = ALIGN(4);
04.   *Generated_Code/*.o (.text .text*)
05.   . = ALIGN(4);
06.   }  > m_text2 /* new section for my special code files */
07.    
08.    
09.   /* The program code and other data goes into INTERNAL_FLASH */
10.   .text :
11.   {
12.   . = ALIGN(4);
13.  
14.    /* original placement: */
15.   /* *(.text)  */  /* .text sections (code) */
16.   /* *(.text*) */  /* .text* sections (code) */
17.  
18.   /* modified placement: need to exclude the ones I have in .generatedCode above */
19.   *(EXCLUDE_FILE(*Generated_Code/*.o).text .text*)  /* .text and .text* sections (code) */
20.  
21.   *(.rodata)  /* .rodata sections (constants, strings, etc.) */
22.   *(.rodata*)  /* .rodata* sections (constants, strings, etc.) */
23.   *(.glue_7)  /* glue arm to thumb code */
24.   *(.glue_7t)  /* glue thumb to arm code */
25.   *(.eh_frame)
26.  
27.   KEEP (*(.init))
28.   KEEP (*(.fini))
29.  
30.   . = ALIGN(4);
31.  
32.   _etext = .;  /* define a global symbols at end of code */
33.   } > 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 :-)

Cisco is a software company. Surprised? Don’t be. Join DevNet to explore APIs, tools, and techniques that developers are using to add collaboration, IoT, security, network priority, and more!

Topics:

Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}