• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright 2022 The Pigweed Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 *     https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17/* This relatively simplified linker script will work with many ARMv7-M and
18 * ARMv8-M cores that have on-board memory-mapped RAM and FLASH. For more
19 * complex projects and devices, it's possible this linker script will not be
20 * sufficient as-is.
21 *
22 * This linker script is likely not suitable for a project with a bootloader.
23 */
24
25/* Provide useful error messages when required configurations are not set. */
26#ifndef PW_BOOT_VECTOR_TABLE_BEGIN
27#error "PW_BOOT_VECTOR_TABLE_BEGIN is not defined, and is required to use pw_boot_cortex_m"
28#endif  // PW_BOOT_VECTOR_TABLE_BEGIN
29
30#ifndef PW_BOOT_VECTOR_TABLE_SIZE
31#error "PW_BOOT_VECTOR_TABLE_SIZE is not defined, and is required to use pw_boot_cortex_m"
32#endif  // PW_BOOT_VECTOR_TABLE_SIZE
33
34#ifndef PW_BOOT_CODE_BEGIN
35#error "PW_BOOT_CODE_BEGIN is not defined, and is required to use pw_boot_cortex_m"
36#endif  // PW_BOOT_CODE_BEGIN
37
38#ifndef PW_BOOT_CODE_SIZE
39#error "PW_BOOT_CODE_SIZE is not defined, and is required to use pw_boot_cortex_m"
40#endif  // PW_BOOT_CODE_SIZE
41
42#ifndef PW_BOOT_RAM_BEGIN
43#error "PW_BOOT_RAM_BEGIN is not defined, and is required to use pw_boot_cortex_m"
44#endif  // PW_BOOT_RAM_BEGIN
45
46#ifndef PW_BOOT_RAM_SIZE
47#error "PW_BOOT_RAM_SIZE is not defined, and is required to use pw_boot_cortex_m"
48#endif  // PW_BOOT_RAM_SIZE
49
50#ifndef PW_BOOT_HEAP_SIZE
51#error "PW_BOOT_HEAP_SIZE is not defined, and is required to use pw_boot_cortex_m"
52#endif  // PW_BOOT_HEAP_SIZE
53
54#ifndef PW_BOOT_MIN_STACK_SIZE
55#error "PW_BOOT_MIN_STACK_SIZE is not defined, and is required to use pw_boot_cortex_m"
56#endif  // PW_BOOT_MIN_STACK_SIZE
57
58
59/* Note: This technically doesn't set the firmware's entry point. Setting the
60 *       firmware entry point is done by setting vector_table[1]
61 *       (Reset_Handler). However, this DOES tell the compiler how to optimize
62 *       when --gc-sections is enabled.
63 */
64ENTRY(pw_boot_Entry)
65
66MEMORY
67{
68  /* TODO(b/234892223): Make it possible for projects to freely customize
69   * memory regions.
70   */
71
72  /* Vector Table */
73  VECTOR_TABLE(rx) : \
74    ORIGIN = PW_BOOT_VECTOR_TABLE_BEGIN, \
75    LENGTH = PW_BOOT_VECTOR_TABLE_SIZE
76  /* External RAM being used for code. */
77  TEXT_EXTERNAL_RAM(rx) : \
78    ORIGIN = PW_BOOT_CODE_BEGIN, \
79    LENGTH = PW_BOOT_CODE_SIZE
80  /* External DDR RAM */
81  EXTERNAL_RAM(rwx) : \
82    ORIGIN = PW_BOOT_RAM_BEGIN, \
83    LENGTH = PW_BOOT_RAM_SIZE
84
85  /* Each memory region above has an associated .*.unused_space section that
86   * overlays the unused space at the end of the memory segment. These segments
87   * are used by pw_bloat.bloaty_config to create the utilization data source
88   * for bloaty size reports.
89   *
90   * These sections MUST be located immediately after the last section that is
91   * placed in the respective memory region or lld will issue a warning like:
92   *
93   *   warning: ignoring memory region assignment for non-allocatable section
94   *      '.VECTOR_TABLE.unused_space'
95   *
96   * If this warning occurs, it's also likely that LLD will have created quite
97   * large padded regions in the ELF file due to bad cursor operations. This
98   * can cause ELF files to balloon from hundreds of kilobytes to hundreds of
99   * megabytes.
100   *
101   * Attempting to add sections to the memory region AFTER the unused_space
102   * section will cause the region to overflow.
103   */
104}
105
106SECTIONS
107{
108  /* This is the link-time vector table. If used, the VTOR (Vector Table Offset
109   * Register) MUST point to this memory location in order to be used. This can
110   * be done by ensuring this section exists at the default location of the VTOR
111   * so it's used on reset, or by explicitly setting the VTOR in a bootloader
112   * manually to point to &pw_boot_vector_table_addr before interrupts are
113   * enabled.
114   *
115   * The ARMv7-M architecture requires this is at least aligned to 128 bytes,
116   * and aligned to a power of two that is greater than 4 times the number of
117   * supported exceptions. 512 has been selected as it accommodates this
118   * device's vector table.
119   */
120  .vector_table : ALIGN(512)
121  {
122    pw_boot_vector_table_addr = .;
123    KEEP(*(.vector_table))
124  } >VECTOR_TABLE
125
126  /* Represents unused space in the VECTOR_TABLE segment. This MUST be the last
127   * section assigned to the VECTOR_TABLE region.
128   */
129  .VECTOR_TABLE.unused_space (NOLOAD) : ALIGN(4)
130  {
131    . = ABSOLUTE(ORIGIN(VECTOR_TABLE) + LENGTH(VECTOR_TABLE));
132  } >VECTOR_TABLE
133
134  /* Main executable code. */
135  .code : ALIGN(4)
136  {
137   CREATE_OBJECT_SYMBOLS
138    __text_load = LOADADDR(.code);
139    /* Application code. */
140    *(.text)
141    *(.text*)
142    KEEP(*(.init))
143    KEEP(*(.fini))
144
145    . = ALIGN(4);
146    /* Constants.*/
147    *(.rodata)
148    *(.rodata*)
149
150    /* .preinit_array, .init_array, .fini_array are used by libc.
151     * Each section is a list of function pointers that are called pre-main and
152     * post-exit for object initialization and tear-down.
153     * Since the region isn't explicitly referenced, specify KEEP to prevent
154     * link-time garbage collection. SORT is used for sections that have strict
155     * init/de-init ordering requirements. */
156    . = ALIGN(4);
157    PROVIDE_HIDDEN(__preinit_array_start = .);
158    KEEP(*(.preinit_array*))
159    PROVIDE_HIDDEN(__preinit_array_end = .);
160
161    PROVIDE_HIDDEN(__init_array_start = .);
162    KEEP(*(SORT(.init_array.*)))
163    KEEP(*(.init_array*))
164    PROVIDE_HIDDEN(__init_array_end = .);
165
166    PROVIDE_HIDDEN(__fini_array_start = .);
167    KEEP(*(SORT(.fini_array.*)))
168    KEEP(*(.fini_array*))
169    PROVIDE_HIDDEN(__fini_array_end = .);
170  } >TEXT_EXTERNAL_RAM
171
172  /* Used by unwind-arm/ */
173  .ARM : ALIGN(4) {
174    __exidx_start = .;
175    *(.ARM.exidx*)
176    __exidx_end = .;
177  } >TEXT_EXTERNAL_RAM
178
179  /* Explicitly initialized global and static data. (.data)*/
180  .static_init_ram : ALIGN(4)
181  {
182    *(.data)
183    *(.data*)
184    . = ALIGN(4);
185  } >EXTERNAL_RAM AT> TEXT_EXTERNAL_RAM
186
187  /* Represents unused space in the TEXT_EXTERNAL_RAM segment. This MUST be the
188   * last section assigned to the TEXT_EXTERNAL_RAM region.
189   */
190  .TEXT_EXTERNAL_RAM.unused_space (NOLOAD) : ALIGN(4)
191  {
192    . = ABSOLUTE(ORIGIN(TEXT_EXTERNAL_RAM) + LENGTH(TEXT_EXTERNAL_RAM));
193  } >TEXT_EXTERNAL_RAM
194
195  /* The .zero_init_ram, .heap, and .stack sections below require (NOLOAD)
196   * annotations for LLVM lld, but not GNU ld, because LLVM's lld intentionally
197   * interprets the linker file differently from ld:
198   *
199   * https://discourse.llvm.org/t/lld-vs-ld-section-type-progbits-vs-nobits/5999/3
200   *
201   * Zero initialized global/static data (.bss) is initialized in
202   * pw_boot_Entry() via memset(), so the section doesn't need to be loaded from
203   * flash. The .heap and .stack sections don't require any initialization,
204   * as they only represent allocated memory regions, so they also do not need
205   * to be loaded.
206   */
207  .zero_init_ram (NOLOAD) : ALIGN(4)
208  {
209    *(.bss)
210    *(.bss*)
211    *(COMMON)
212    . = ALIGN(4);
213  } >EXTERNAL_RAM
214
215  .heap (NOLOAD) : ALIGN(4)
216  {
217    pw_boot_heap_low_addr = .;
218    . = . + PW_BOOT_HEAP_SIZE;
219    . = ALIGN(4);
220    pw_boot_heap_high_addr = .;
221  } >EXTERNAL_RAM
222
223  /* Link-time check for stack overlaps.
224   *
225   * The ARMv7-M architecture may require 8-byte alignment of the stack pointer
226   * rather than 4 in some contexts and implementations, so this region is
227   * 8-byte aligned (see ARMv7-M Architecture Reference Manual DDI0403E
228   * section B1.5.7).
229   */
230  .stack (NOLOAD) : ALIGN(8)
231  {
232    /* Set the address that the main stack pointer should be initialized to. */
233    pw_boot_stack_low_addr = .;
234    HIDDEN(_stack_size = ORIGIN(EXTERNAL_RAM) + LENGTH(EXTERNAL_RAM) - .);
235    /* Align the stack to a lower address to ensure it isn't out of range. */
236    HIDDEN(_stack_high = (. + _stack_size) & ~0x7);
237    ASSERT(_stack_high - . >= PW_BOOT_MIN_STACK_SIZE,
238           "Error: Not enough RAM for desired minimum stack size.");
239    . = _stack_high;
240    pw_boot_stack_high_addr = .;
241  } >EXTERNAL_RAM
242
243  /* Represents unused space in the EXTERNAL_RAM segment. This MUST be the last
244   * section assigned to the EXTERNAL_RAM region.
245   */
246  .EXTERNAL_RAM.unused_space (NOLOAD) : ALIGN(4)
247  {
248    . = ABSOLUTE(ORIGIN(EXTERNAL_RAM) + LENGTH(EXTERNAL_RAM));
249  } >EXTERNAL_RAM
250
251  /* Discard unwind info. */
252  .ARM.extab 0x0 (INFO) :
253  {
254    KEEP(*(.ARM.extab*))
255  }
256}
257
258/* Symbols used by core_init.c: */
259/* Start of .static_init_ram in TEXT_EXTERNAL_RAM. */
260_pw_static_init_flash_start = LOADADDR(.static_init_ram);
261
262/* Region of .static_init_ram in RAM. */
263_pw_static_init_ram_start = ADDR(.static_init_ram);
264_pw_static_init_ram_end = _pw_static_init_ram_start + SIZEOF(.static_init_ram);
265
266/* Region of .zero_init_ram. */
267_pw_zero_init_ram_start = ADDR(.zero_init_ram);
268_pw_zero_init_ram_end = _pw_zero_init_ram_start + SIZEOF(.zero_init_ram);
269
270/* arm-none-eabi expects `end` symbol to point to start of heap for sbrk. */
271PROVIDE(end = _pw_zero_init_ram_end);
272
273/* These symbols are used by pw_bloat.bloaty_config to create the memoryregions
274 * data source for bloaty in this format (where the optional _N defaults to 0):
275 * pw_bloat_config_memory_region_NAME_{start,end}{_N,} */
276pw_bloat_config_memory_region_VECTOR_TABLE_start = ORIGIN(VECTOR_TABLE);
277pw_bloat_config_memory_region_VECTOR_TABLE_end =
278    ORIGIN(VECTOR_TABLE) + LENGTH(VECTOR_TABLE);
279pw_bloat_config_memory_region_FLASH_start = ORIGIN(TEXT_EXTERNAL_RAM);
280pw_bloat_config_memory_region_FLASH_end = ORIGIN(TEXT_EXTERNAL_RAM) + LENGTH(TEXT_EXTERNAL_RAM);
281pw_bloat_config_memory_region_RAM_start = ORIGIN(EXTERNAL_RAM);
282pw_bloat_config_memory_region_RAM_end = ORIGIN(EXTERNAL_RAM) + LENGTH(EXTERNAL_RAM);
283
284/* Symbol mapping used by MSS Startup and Debugger. */
285PROVIDE (__smartfusion2_memory_remap = 2);  /* Remap according to LMA (this script) */
286PROVIDE (__mirrored_nvm = 1);   /* GDB will load to LMA directly no need to load again. */
287PROVIDE (_estack = pw_boot_stack_high_addr);
288PROVIDE (__stack_start__ = pw_boot_stack_low_addr);
289PROVIDE (__vector_table_load = LOADADDR(.vector_table));
290PROVIDE (__vector_table_start = pw_boot_vector_table_addr);
291PROVIDE (__vector_table_vma_base_address = __vector_table_start);  /* required by debugger for start address */
292PROVIDE (__text_end =  __exidx_end);
293PROVIDE (__heap_start__ = pw_boot_heap_low_addr);
294PROVIDE (_eheap = pw_boot_heap_high_addr);
295PROVIDE (_etext =  __exidx_end);
296PROVIDE (_evector_table = pw_boot_vector_table_addr);
297PROVIDE (__data_start = _pw_static_init_ram_start);
298PROVIDE (_edata = _pw_static_init_ram_end);
299PROVIDE (__text_start = __text_load);
300PROVIDE (__bss_start__ = _pw_zero_init_ram_start);
301PROVIDE (__bss_end__ = _pw_static_init_ram_end);
302PROVIDE (__data_load = _pw_static_init_flash_start);
303