https://raw-githubusercru&xtrtl=en&xtrhl=en-US&xtrpto=wapp
https://raw-githubusercontent-com.translate.goog/adambard/learnxinyminutes-docs/master/ru-ru/linker-ru.html.markdown?xtrsl=ru&xtrtl=en&xtrhl=en-US&xtrpto=wapp
translated with google translate via link above
category: tool tool: linker contributors:
- ["Alexander Kovalchuk", "https://github.com/Zamuhrishka"]
- ["Alexander Kovalchuk", "https://github.com/Zamuhrishka"]
Basic concepts and definitions #
Position counter - the linker has a special variable "." (dot) always contains the current output position.Functions #
ADDR(section) - returns the absolute address of the specified section. Howeverthis section must be defined before using the ADDR function.
ALIGN(exp) - returns the position counter value aligned to the boundaryexpression following exp.
SIZEOF(section) - returns the section size in bytes. FILL(param) - Specifies the fill pattern for the current section. Allother unspecified regions within the section are filled with the value specified in the function argument.
KEEP(param) - used to mark a param as unrecoverable. ENTRY(func) - defines a function that will be the entry pointto the program.
# Define the entry point to the program
ENTRY(Reset_Handler)
# Define a variable that contains the address of the top of the stack
_stack = 0x20020000;
# Define a variable that contains the value of the heap size
_Min_Heap_Size = 0x200;
# Define a variable that contains the value of the stack size
_Min_Stack_Size = 0x400;
# Description of the memory map available for this processor
# MEMORY
# {
# MEMORY_AREA_NAME (permissions) : ORIGIN = START_ADDRESS, LENGTH = SIZE
#}
# In our example, the controller contains three memory areas:
# RAM - starts at address 0x20000000 and occupies 128 KB;
# CCMRAM - starts at address 0x10000000 and occupies 64 KB;
# FLASH - starts at address 0x8000000 occupies 1024 KB;
# And RAM memory access for reading, writing and execution.
# CCMRAM is read/write only.
# FLASH memory is available for reading and execution.
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
FLASH(rx) : ORIGIN=0x8000000, LENGTH=1024K
}
# Describing output sections
SECTIONS
{
# The first section contains the interrupt vector table
.isr_vector :
{
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
# There is an option --gc-sections which allows garbage collection of unused
# input sections. And if there are sections that the garbage collector should not touch,
# then they must be specified as an argument to the KEEP() function (analogue of the keyword
# volatile).
# The entry (*(.isr_vector)) means .isr_vector sections in all object files. Because
# addressing a section in general looks like this: (FILE_NAME(SECTION_NAME))
KEEP(*(.isr_vector))
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
# The expression ">MEMORY_AREA" indicates which memory area will be placed
# this section. In our case, the .isr_vector section will be placed in FLASH memory.
}>FLASH
# TOTAL: The .isr_vector section that contains the interrupt vector table is aligned
# on a 4-byte boundary, marked as inaccessible to the garbage collector and placed at the beginning
# FLASH memory of the microcontroller.
# The second section contains the program code.
.text :
{
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
# Specify that this section will store the .text areas of all
# object files
*(.text)
*(.text*)
# Protect the .init and .fini sections from the garbage collector
KEEP (*(.init))
KEEP (*(.fini))
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
# The _etext variable is defined, which stores the address of the end of the .text section and which
# can be accessed in the source code of the program via a declaration
# volaile unsigned int extern _etext;
_text = .;
}>FLASH
# TOTAL: The .text section, which contains the program code, is aligned on a 4-byte boundary,
# includes: all program code sections 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 vector table.
The text, .init and .fini sections. are stored in memory in the order in which they are
declared in the script.
# The third section contains constant data.
.rodata :
{
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
# Specify that this section will store the .rodata areas of all
# object files
*(.rodata)
*(.rodata*)
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
}>FLASH
# Store the absolute address of the .data section in the _sidata variable
_sidata = LOADADDR(.data);
# The fourth section contains initialized variables.
.data :
{
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
# Save the address of the current position (the beginning of the section) in the _sdata variable
_sdata = .;
# Specify that this section will store the .data areas of all
# object files
*(.data)
*(.data*)
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
# Store the address of the current position (end of section) in the _sdata variable
_edata = .;
# The AT function indicates that the given sector is stored in one area of memory
# (in our case FLASH), but it will be executed from another memory area (in our case RAM).
# There are two types of addresses:
# * VMA (Virtual memory address) is the run-time address where the amp is waiting for
# see data.
# * LMA (Load memory address) is the address where the linker stores data.
#Startup code should copy the .data section from LMA addresses to VMA addresses.
} >RAMAT>FLASH
# The fifth section contains zero-initialized variables.
.bss :
{
# Store in the variable _sbss and __bss_start__ the address of the current position (beginning of the section)
_sbss = .;
__bss_start__ = _sbss;
# Specify that this section will store the .bss areas of all
# object files
*(.bss)
*(.bss*)
# Align the current position to a 4-byte boundary.
. = ALIGN(4);
# Store in the variable _ebss and __bss_end__ the address of the current position (beginning of the section)
_ebss = .;
__bss_end__ = _ebss;
}>RAM
# The sixth section contains the heap and stack. 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
}