1/* 2 * Copyright 2023 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 linker script is derived from pw_boot_cortex_m/basic_cortex_m.ld for use 18 * with the NXP MIMXRT595-EVK, booting from FLASH. 19 */ 20 21/* Provide useful error messages when required configurations are not set. */ 22#ifndef PW_BOOT_VECTOR_TABLE_BEGIN 23#error "PW_BOOT_VECTOR_TABLE_BEGIN is not defined, and is required to use pw_boot_cortex_m" 24#endif // PW_BOOT_VECTOR_TABLE_BEGIN 25 26#ifndef PW_BOOT_VECTOR_TABLE_SIZE 27#error "PW_BOOT_VECTOR_TABLE_SIZE is not defined, and is required to use pw_boot_cortex_m" 28#endif // PW_BOOT_VECTOR_TABLE_SIZE 29 30#ifndef PW_BOOT_FLASH_BEGIN 31#error "PW_BOOT_FLASH_BEGIN is not defined, and is required to use pw_boot_cortex_m" 32#endif // PW_BOOT_FLASH_BEGIN 33 34#ifndef PW_BOOT_FLASH_SIZE 35#error "PW_BOOT_FLASH_SIZE is not defined, and is required to use pw_boot_cortex_m" 36#endif // PW_BOOT_FLASH_SIZE 37 38#ifndef PW_BOOT_RAM_BEGIN 39#error "PW_BOOT_RAM_BEGIN is not defined, and is required to use pw_boot_cortex_m" 40#endif // PW_BOOT_RAM_BEGIN 41 42#ifndef PW_BOOT_RAM_SIZE 43#error "PW_BOOT_RAM_SIZE is not defined, and is required to use pw_boot_cortex_m" 44#endif // PW_BOOT_RAM_SIZE 45 46#ifndef PW_BOOT_HEAP_SIZE 47#error "PW_BOOT_HEAP_SIZE is not defined, and is required to use pw_boot_cortex_m" 48#endif // PW_BOOT_HEAP_SIZE 49 50#ifndef PW_BOOT_MIN_STACK_SIZE 51#error "PW_BOOT_MIN_STACK_SIZE is not defined, and is required to use pw_boot_cortex_m" 52#endif // PW_BOOT_MIN_STACK_SIZE 53 54 55/* Note: This technically doesn't set the firmware's entry point. Setting the 56 * firmware entry point is done by setting vector_table[1] 57 * (Reset_Handler). However, this DOES tell the compiler how to optimize 58 * when --gc-sections is enabled. 59 */ 60ENTRY(pw_boot_Entry) 61 62MEMORY 63{ 64 /* Flash Config for bootloader */ 65 FLASH_CONFIG(rx) : \ 66 ORIGIN = 0x08000400, \ 67 LENGTH = 0x00000200 68 /* Vector Table (typically in flash) */ 69 VECTOR_TABLE(rx) : \ 70 ORIGIN = PW_BOOT_VECTOR_TABLE_BEGIN, \ 71 LENGTH = PW_BOOT_VECTOR_TABLE_SIZE 72 /* Internal Flash */ 73 FLASH(rx) : \ 74 ORIGIN = PW_BOOT_FLASH_BEGIN, \ 75 LENGTH = PW_BOOT_FLASH_SIZE 76 /* Internal SRAM */ 77 RAM(rwx) : \ 78 ORIGIN = PW_BOOT_RAM_BEGIN, \ 79 LENGTH = PW_BOOT_RAM_SIZE 80 /* USB SRAM */ 81 USB_SRAM(rw) : \ 82 ORIGIN = 0x40140000, \ 83 LENGTH = 0x00004000 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 .flash_config : 109 { 110 . = ALIGN(4); 111 KEEP(*(.flash_conf)) 112 } >FLASH_CONFIG 113 114 /* This is the link-time vector table. If used, the VTOR (Vector Table Offset 115 * Register) MUST point to this memory location in order to be used. This can 116 * be done by ensuring this section exists at the default location of the VTOR 117 * so it's used on reset, or by explicitly setting the VTOR in a bootloader 118 * manually to point to &pw_boot_vector_table_addr before interrupts are 119 * enabled. 120 * 121 * The ARMv8-M architecture requires this is at least aligned to 128 bytes, 122 * and aligned to a power of two that is greater than 4 times the number of 123 * supported exceptions. 512 has been selected as it accommodates this 124 * device's vector table. 125 */ 126 .vector_table : ALIGN(512) 127 { 128 pw_boot_vector_table_addr = .; 129 KEEP(*(.vector_table)) 130 } >VECTOR_TABLE 131 132 /* Represents unused space in the VECTOR_TABLE segment. This MUST be the last 133 * section assigned to the VECTOR_TABLE region. 134 */ 135 .VECTOR_TABLE.unused_space (NOLOAD) : ALIGN(4) 136 { 137 . = ABSOLUTE(ORIGIN(VECTOR_TABLE) + LENGTH(VECTOR_TABLE)); 138 } >VECTOR_TABLE 139 140 /* Main executable code. */ 141 .code : ALIGN(4) 142 { 143 . = ALIGN(4); 144 /* Application code. */ 145 *(.text) 146 *(.text*) 147 KEEP(*(.init)) 148 KEEP(*(.fini)) 149 150 . = ALIGN(4); 151 /* Constants.*/ 152 *(.rodata) 153 *(.rodata*) 154 155 /* Glue ARM to Thumb code, and vice-versa */ 156 *(.glue_7) 157 *(.glue_7t) 158 159 /* Exception handling frame */ 160 *(.eh_frame) 161 162 /* .preinit_array, .init_array, .fini_array are used by libc. 163 * Each section is a list of function pointers that are called pre-main and 164 * post-exit for object initialization and tear-down. 165 * Since the region isn't explicitly referenced, specify KEEP to prevent 166 * link-time garbage collection. SORT is used for sections that have strict 167 * init/de-init ordering requirements. */ 168 . = ALIGN(4); 169 PROVIDE_HIDDEN(__preinit_array_start = .); 170 KEEP(*(.preinit_array*)) 171 PROVIDE_HIDDEN(__preinit_array_end = .); 172 173 PROVIDE_HIDDEN(__init_array_start = .); 174 KEEP(*(SORT(.init_array.*))) 175 KEEP(*(.init_array*)) 176 PROVIDE_HIDDEN(__init_array_end = .); 177 178 PROVIDE_HIDDEN(__fini_array_start = .); 179 KEEP(*(SORT(.fini_array.*))) 180 KEEP(*(.fini_array*)) 181 PROVIDE_HIDDEN(__fini_array_end = .); 182 } >FLASH 183 184 /* Used by unwind-arm/ */ 185 .ARM : ALIGN(4) { 186 __exidx_start = .; 187 *(.ARM.exidx*) 188 __exidx_end = .; 189 } >FLASH 190 191 /* Explicitly initialized global and static data. (.data)*/ 192 .static_init_ram : ALIGN(4) 193 { 194 *(CodeQuickAccess) 195 *(DataQuickAccess) 196 *(.data) 197 *(.data*) 198 . = ALIGN(4); 199 } >RAM AT> FLASH 200 201 /* Represents unused space in the FLASH segment. This MUST be the last section 202 * assigned to the FLASH region. 203 */ 204 .FLASH.unused_space (NOLOAD) : ALIGN(4) 205 { 206 . = ABSOLUTE(ORIGIN(FLASH) + LENGTH(FLASH)); 207 } >FLASH 208 209 /* The .zero_init_ram, .heap, and .stack sections below require (NOLOAD) 210 * annotations for LLVM lld, but not GNU ld, because LLVM's lld intentionally 211 * interprets the linker file differently from ld: 212 * 213 * https://discourse.llvm.org/t/lld-vs-ld-section-type-progbits-vs-nobits/5999/3 214 * 215 * Zero initialized global/static data (.bss) is initialized in 216 * pw_boot_Entry() via memset(), so the section doesn't need to be loaded from 217 * flash. The .heap and .stack sections don't require any initialization, 218 * as they only represent allocated memory regions, so they also do not need 219 * to be loaded. 220 */ 221 .zero_init_ram (NOLOAD) : ALIGN(4) 222 { 223 *(.bss) 224 *(.bss*) 225 *(COMMON) 226 . = ALIGN(4); 227 } >RAM 228 229 .heap (NOLOAD) : ALIGN(4) 230 { 231 pw_boot_heap_low_addr = .; 232 . = . + PW_BOOT_HEAP_SIZE; 233 . = ALIGN(4); 234 pw_boot_heap_high_addr = .; 235 } >RAM 236 237 /* Link-time check for stack overlaps. 238 * 239 * The ARMv8-M architecture requires 8-byte alignment of the stack pointer 240 * rather than 4 in some contexts, so this region is 8-byte aligned (see 241 * ARMv8-M Architecture Reference Manual DDI0553 section B3.8). 242 */ 243 .stack (NOLOAD) : ALIGN(8) 244 { 245 /* Set the address that the main stack pointer should be initialized to. */ 246 pw_boot_stack_low_addr = .; 247 HIDDEN(_stack_size = ORIGIN(RAM) + LENGTH(RAM) - .); 248 /* Align the stack to a lower address to ensure it isn't out of range. */ 249 HIDDEN(_stack_high = (. + _stack_size) & ~0x7); 250 ASSERT(_stack_high - . >= PW_BOOT_MIN_STACK_SIZE, 251 "Error: Not enough RAM for desired minimum stack size."); 252 . = _stack_high; 253 pw_boot_stack_high_addr = .; 254 } >RAM 255 256 /* Represents unused space in the RAM segment. This MUST be the last section 257 * assigned to the RAM region. 258 */ 259 .RAM.unused_space (NOLOAD) : ALIGN(4) 260 { 261 . = ABSOLUTE(ORIGIN(RAM) + LENGTH(RAM)); 262 } >RAM 263 264 m_usb_bdt (NOLOAD) : 265 { 266 . = ALIGN(512); 267 *(m_usb_bdt) 268 } >USB_SRAM 269 270 m_usb_global (NOLOAD) : 271 { 272 *(m_usb_global) 273 } >USB_SRAM 274 275 /* Represents unused space in the USB_SRAM segment. This MUST be the last 276 * section assigned to the USB_SRAM region. 277 */ 278 .USB_SRAM.unused_space (NOLOAD) : ALIGN(4) 279 { 280 . = ABSOLUTE(ORIGIN(USB_SRAM) + LENGTH(USB_SRAM)); 281 } >USB_SRAM 282 283 /* Discard unwind info. */ 284 .ARM.extab 0x0 (INFO) : 285 { 286 KEEP(*(.ARM.extab*)) 287 } 288 289 .ARM.attributes 0 : { *(.ARM.attributes) } 290} 291 292/* Symbols used by core_init.c: */ 293/* Start of .static_init_ram in FLASH. */ 294_pw_static_init_flash_start = LOADADDR(.static_init_ram); 295 296/* Region of .static_init_ram in RAM. */ 297_pw_static_init_ram_start = ADDR(.static_init_ram); 298_pw_static_init_ram_end = _pw_static_init_ram_start + SIZEOF(.static_init_ram); 299 300/* Region of .zero_init_ram. */ 301_pw_zero_init_ram_start = ADDR(.zero_init_ram); 302_pw_zero_init_ram_end = _pw_zero_init_ram_start + SIZEOF(.zero_init_ram); 303 304/* Size of image for bootloader header. */ 305_pw_image_size = _pw_static_init_flash_start + (_pw_static_init_ram_end - _pw_static_init_ram_start) - pw_boot_vector_table_addr; 306 307/* arm-none-eabi expects `end` symbol to point to start of heap for sbrk. */ 308PROVIDE(end = _pw_zero_init_ram_end); 309