Linker Directives Basics - Based on the ARM Cortex -M3/M4 Processor.
Before going to the Linker script, we need to understand the few basic concepts of the embedded system.
- Compilation process or Build process
- Definition of Cross-compilation and cross-compiler Tool chain
- Different data and sections of program
- Write Linker script
================================================
Compilation Process:
Link: GCC
Compilation is the method of converting the source code into object code. During compilation process, compiler tests the source code for syntactic or structural errors and produces the object code if the source code is error-free.
Structure of the relocatable object file.
Let’s Understand the Four important Sections in the ELF File.
- .text – Our c program is stored in this section. We only have read and execute permission to this section and not write permission.
- .data – All initialized global and static variables are stored in the .data section. (Read and Write Permission)
- .rodata – Constants and literals are stored in the .rodata section. (Read Permission)
- .bss – This section contains uninitialized global and static variables. (Read and Write Permission)
Executable File Structure
Different data and sections of program
- In Global variable declaration: initialized global variable or uninitialized global variable.
- In Static variable declaration: local static or global static and also initialized or uninitialized
- Constant variable: global constant or Local constant
- Global and Static variables initialized with zero
- Local variables
register
." so it is compiler dependent.Write Linker script
- linker scripts are written using GNU linker command language
- GNU linker script has the extension of .ld
Linker script commands
- ENTRY
- MEMORY
- SECTIONS
- KEEP
- ALIGN
- AT>
MEMORY"
command describes the location and size of blocks of memory in the target.MEMORY { name (attr) : ORIGIN = origin, LENGTH = len ... }
MEMORY { /* Code flash area */ iROM_0 : ORIGIN = 0x00000000, LENGTH = 48k /* Data flash */ iROM_1 : ORIGIN = 0x01000000, LENGTH = 32k
}
SECTIONS:
In the following example, the command script arranges the output file into three consecutive sections, named
.text
,.data
, and.bss
, taking the input for each from the correspondingly named sections of all the input files:SECTIONS { .text : { *(.text) } .data : { *(.data) } .bss : { *(.bss) *(COMMON) } }
- .text for code & constants
- .bss for uninitialized data
- .stack for local variables
- .data for initialized data
ALIGN:
You can increase an output section’s alignment by using ALIGN.SECTIONS {
/* Start of internal RAM area (iRAM) */ .data :> iRAM_0 /* initialized data */ .sldata align(4) :>. /* user defined segment for initialized data */
}We need to Know which type of Memory Alignment required in your microcontroller Manual.
Please refer the Reference manual for the Same
ARM processors support the following data types:
- Byte 8 bits
- Halfword 16 bits
- Word 32 bits
ARM instructions are exactly one word and are aligned on a four-byte boundary. Thumb® instructions are exactly one halfword and are aligned on a two-byte boundary. opcodes are a variable number of bytes in length and can appear at any byte alignmentExample of .ld as follows
ENTRY(Reset_Handler)
MEMORY
{
ROM : ORIGIN = 0x00000000, LENGTH = 16K /* Data flash */ ROM_1 : ORIGIN = 0x01000000, LENGTH = 16K /* Code segment*/ RAM_0 : ORIGIN = 0X80000000, LENGTH = 32k /* Data segment*/ iRAM_R : ORIGIN = 0xFEF00000, LENGTH = 64k /*Retension RAM*/ }
SECTIONS
{
__text_start__=.:
.text : /* collecting .text memory address from different object files and
making single .text segment in the final object file or
relocatable object files to final executable file*/
{
{ *(.text)
}
.=ALIGN(4);
__text_End__=.;
__data_start__=.:
.data :{ { *(.data) }
}
.=ALIGN(4);
__data_End__=.;
__bss_start__=.:
.bss :{ { *(.bss) *(COMMON) }
}
.=ALIGN(4);
__bss_End__=.;
__rodata_start__=.:
.rodata:{ { *(.rodata) }
}
.=ALIGN(4);
__rodata_End__=.;
/*Optional Compiler directives are used*/
}
Memory Regions are calculated by using labels:
text segment total memory calculated by using following labels __text_start__=.;__text_End__=.;
__text_start__=.; Start address of Text segment of merged elf file
__text_End__=.; End address of Text segment of merged elf file
=====================================================================
AT command specifies the exact load address of the section.
The AT> keyword takes the name of a memory region as an argument.
Every loadable or allocatable output section has two addresses.
- The first is the VMA, or virtual memory address. This is the address the section will have when the output file is run.
- The second is the LMA, or load memory address. This is the address at which the section will be loaded.
An example of when they might be different is when a data section is loaded into ROM,
and then copied into RAM when the program starts up (this technique is often used to initialize
global variables in a ROM based system).
In this case the ROM address would be the LMA, and the RAM address would be the VMA.
{
.data :
{ _sdata = .;
*(.data*);
_edata = .;
} > ram AT >rom
}
Linker detail: Linker basic
================================================================================================
No comments:
Post a Comment