2019-08-03 17:14:03 +00:00
|
|
|
|
---
|
|
|
|
|
category: tool
|
2024-12-19 00:02:09 +00:00
|
|
|
|
name: Linker script
|
2019-08-03 17:14:03 +00:00
|
|
|
|
contributors:
|
|
|
|
|
- ["Alexander Kovalchuk", "https://github.com/Zamuhrishka"]
|
2024-05-13 08:25:24 +00:00
|
|
|
|
translators:
|
|
|
|
|
- ["Anuj Shah", "https://github.com/ShahAnuj2610"]
|
2024-12-19 00:02:09 +00:00
|
|
|
|
filename: learn.ld
|
2019-08-03 17:14:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2024-05-13 08:25:24 +00:00
|
|
|
|
**Position counter** - the linker has a special variable
|
2024-12-19 00:02:09 +00:00
|
|
|
|
"`.`" (dot) always contains the current output position.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
`ADDR (section)` - returns the absolute address of the specified section. However
|
2024-05-13 08:25:24 +00:00
|
|
|
|
this section must be defined before using the ADDR function.
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
`ALIGN (exp)` - returns the value of the position counter aligned to the border
|
2024-05-13 08:25:24 +00:00
|
|
|
|
following the exp expression.
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
`SIZEOF (section)` - returns the size of the section in bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
`FILL (param)` - defines the fill pattern for the current section. All
|
2024-05-13 08:25:24 +00:00
|
|
|
|
other unspecified regions within the section are filled with the value indicated
|
|
|
|
|
in function argument.
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
`KEEP (param)` - used to mark param as fatal.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
`ENTRY (func)` - defines the function that will be the entry point
|
2024-05-13 08:25:24 +00:00
|
|
|
|
into the program.
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# Determine the entry point to the program
|
|
|
|
|
ENTRY(Reset_Handler)
|
|
|
|
|
|
|
|
|
|
# Define a variable that contains the address of the top of the stack
|
|
|
|
|
_estack = 0x20020000;
|
|
|
|
|
# Define a variable that contains a heap size value
|
|
|
|
|
_Min_Heap_Size = 0x200;
|
|
|
|
|
# Define a variable that contains the value of the stack size
|
|
|
|
|
_Min_Stack_Size = 0x400;
|
|
|
|
|
|
|
|
|
|
# Description of the memory card available for this processor
|
|
|
|
|
# MEMORY
|
|
|
|
|
# {
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# MEMORY_DOMAIN_NAME (access rights) : ORIGIN = START_ADDRESS, LENGTH = SIZE
|
2024-05-13 08:25:24 +00:00
|
|
|
|
# }
|
|
|
|
|
# In our example, the controller contains three memory areas:
|
|
|
|
|
# RAM - starts with the address 0x20000000 and takes 128 KB;
|
|
|
|
|
# CCMRAM - starts with the address 0x10000000 and occupies 64 KB;
|
|
|
|
|
# FLASH - starts with the address 0x8000000; takes 1024 Kb;
|
|
|
|
|
# Moreover, RAM memory access for reading, writing and execution.
|
|
|
|
|
# CCMRAM memory is read-write only.
|
|
|
|
|
# FLASH memory is available for reading and execution.
|
|
|
|
|
MEMORY
|
|
|
|
|
{
|
2024-12-19 00:02:09 +00:00
|
|
|
|
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
|
|
|
|
|
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
|
|
|
|
|
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K
|
2024-05-13 08:25:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# We describe output sections
|
|
|
|
|
SECTIONS
|
|
|
|
|
{
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# The first section contains a table of interrupt vectors
|
2024-05-13 08:25:24 +00:00
|
|
|
|
.isr_vector :
|
|
|
|
|
{
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# There is an option --gc-sections, which allows you to collect garbage from unused
|
|
|
|
|
# input sections. And if there are sections that the garbage collector should not touch,
|
|
|
|
|
# you need to specify them as an argument to the KEEP () function (analogue of the keyword
|
|
|
|
|
# volatile).
|
|
|
|
|
# The entry (* (. Isr_vector)) means the .isr_vector sections in all object files. Because
|
|
|
|
|
# appeal to the section in general terms looks like this: (FILE_NAME (SECTION_NAME))
|
2024-05-13 08:25:24 +00:00
|
|
|
|
KEEP(*(.isr_vector))
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# The expression "> MEMORY AREA" indicates which area of memory will be placed
|
|
|
|
|
# this section. In our section, the .isr_vector section will be located in FLASH memory.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
} >FLASH
|
|
|
|
|
|
|
|
|
|
# TOTAL: The .isr_vector section that contains the table of interrupt vectors is aligned
|
|
|
|
|
# on the border of 4 bytes, marked as inaccessible to the garbage collector and placed at the beginning
|
|
|
|
|
# FLASH microcontroller memory.
|
|
|
|
|
|
|
|
|
|
# The second section contains the program code.
|
|
|
|
|
.text :
|
|
|
|
|
{
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
|
|
|
|
|
# We indicate that in this section the .text areas of all
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# object files
|
2024-05-13 08:25:24 +00:00
|
|
|
|
*(.text)
|
|
|
|
|
*(.text*)
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Protect the .init and .fini sections from the garbage collector
|
2024-05-13 08:25:24 +00:00
|
|
|
|
KEEP (*(.init))
|
|
|
|
|
KEEP (*(.fini))
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# The variable _etext is defined, which stores the address of the end of the .text section and which
|
|
|
|
|
# may be available in the source code of the program through the announcement
|
|
|
|
|
# volaile unsigned int extern _etext;
|
2024-05-13 08:25:24 +00:00
|
|
|
|
_etext = .;
|
|
|
|
|
} >FLASH
|
|
|
|
|
|
|
|
|
|
# TOTAL: The .text section that contains the program code is aligned on the border of 4 bytes,
|
|
|
|
|
# includes: all sections with program code in all object files and protected
|
|
|
|
|
# from the garbage collector of the .init and .fini sections in all object files, located in FLASH
|
|
|
|
|
# microcontroller memory immediately after the table of vectors.
|
|
|
|
|
# The text, .init, and .fini sections. are located in memory in the order in which they
|
|
|
|
|
# declared in the script.
|
|
|
|
|
|
|
|
|
|
# The third section contains constant data.
|
|
|
|
|
.rodata :
|
|
|
|
|
{
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# We indicate that in this section areas .rodata will be stored
|
|
|
|
|
# object files
|
2024-05-13 08:25:24 +00:00
|
|
|
|
*(.rodata)
|
|
|
|
|
*(.rodata*)
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
} >FLASH
|
|
|
|
|
|
|
|
|
|
# Save the absolute address of the .data section in the _sidata variable
|
|
|
|
|
_sidata = LOADADDR(.data);
|
|
|
|
|
|
|
|
|
|
# The fourth section contains initialized variables.
|
|
|
|
|
.data :
|
|
|
|
|
{
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Save the address of the current position (beginning of the section) in the variable _sdata
|
2024-05-13 08:25:24 +00:00
|
|
|
|
_sdata = .;
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# We indicate that in this section the .data areas of all
|
|
|
|
|
# object files
|
2024-05-13 08:25:24 +00:00
|
|
|
|
*(.data)
|
|
|
|
|
*(.data*)
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
|
|
|
|
|
# Save the address of the current position (end of section) in the variable _sdata
|
|
|
|
|
_edata = .;
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# AT function indicates that this sector is stored in one memory area
|
|
|
|
|
# (in our case, FLASH), and it will be executed from another area of memory (in our case, RAM).
|
|
|
|
|
# There are two types of addresses:
|
|
|
|
|
# * VMA (Virtual memory address) - this is the run-time address at which the compiler expects
|
|
|
|
|
# see data.
|
|
|
|
|
# * LMA (Load memory address) is the address at which the linker stores data.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
#Startup must code to copy the .data section from the LMA addresses to the VMA addresses.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
|
|
|
|
|
} >RAM AT> FLASH
|
|
|
|
|
|
|
|
|
|
# The fifth section contains zero-initialized variables.
|
|
|
|
|
.bss :
|
|
|
|
|
{
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Save the address of the current position (beginning of the section) in the variable _sbss and __bss_start__
|
2024-05-13 08:25:24 +00:00
|
|
|
|
_sbss = .;
|
|
|
|
|
__bss_start__ = _sbss;
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# We indicate that in this section the .bss areas of all
|
|
|
|
|
# object files
|
2024-05-13 08:25:24 +00:00
|
|
|
|
*(.bss)
|
|
|
|
|
*(.bss*)
|
|
|
|
|
|
2024-12-19 00:02:09 +00:00
|
|
|
|
# Align the current position to the border of 4 bytes.
|
2024-05-13 08:25:24 +00:00
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
|
|
|
|
|
# Save the address of the current position (beginning of the section) in the variable _ebss and __bss_end__
|
|
|
|
|
_ebss = .;
|
|
|
|
|
__bss_end__ = _ebss;
|
|
|
|
|
} >RAM
|
|
|
|
|
|
|
|
|
|
# The sixth section contains a bunch and a stack. It is located at the very end of RAM.
|
|
|
|
|
._user_heap_stack :
|
|
|
|
|
{
|
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
PROVIDE ( end = . );
|
|
|
|
|
PROVIDE ( _end = . );
|
|
|
|
|
. = . + _Min_Heap_Size;
|
|
|
|
|
. = . + _Min_Stack_Size;
|
|
|
|
|
. = ALIGN(4);
|
|
|
|
|
} >RAM
|
|
|
|
|
}
|
|
|
|
|
```
|