1/* 2 * Copyright 2020 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_FLASH_BEGIN 35#error "PW_BOOT_FLASH_BEGIN is not defined, and is required to use pw_boot_cortex_m" 36#endif // PW_BOOT_FLASH_BEGIN 37 38#ifndef PW_BOOT_FLASH_SIZE 39#error "PW_BOOT_FLASH_SIZE is not defined, and is required to use pw_boot_cortex_m" 40#endif // PW_BOOT_FLASH_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 (typically in flash) */ 73 VECTOR_TABLE(rx) : \ 74 ORIGIN = PW_BOOT_VECTOR_TABLE_BEGIN, \ 75 LENGTH = PW_BOOT_VECTOR_TABLE_SIZE 76 /* Internal Flash */ 77 FLASH(rx) : \ 78 ORIGIN = PW_BOOT_FLASH_BEGIN, \ 79 LENGTH = PW_BOOT_FLASH_SIZE 80 /* Internal SRAM */ 81 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 most 118 * devices' vector tables. 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 . = ALIGN(4); 138 /* Application code. */ 139 *(.text) 140 *(.text*) 141 KEEP(*(.init)) 142 KEEP(*(.fini)) 143 144 . = ALIGN(4); 145 /* Constants.*/ 146 *(.rodata) 147 *(.rodata*) 148 149 /* .preinit_array, .init_array, .fini_array are used by libc. 150 * Each section is a list of function pointers that are called pre-main and 151 * post-exit for object initialization and tear-down. 152 * Since the region isn't explicitly referenced, specify KEEP to prevent 153 * link-time garbage collection. SORT is used for sections that have strict 154 * init/de-init ordering requirements. */ 155 . = ALIGN(4); 156 PROVIDE_HIDDEN(__preinit_array_start = .); 157 KEEP(*(.preinit_array*)) 158 PROVIDE_HIDDEN(__preinit_array_end = .); 159 160 PROVIDE_HIDDEN(__init_array_start = .); 161 KEEP(*(SORT(.init_array.*))) 162 KEEP(*(.init_array*)) 163 PROVIDE_HIDDEN(__init_array_end = .); 164 165 PROVIDE_HIDDEN(__fini_array_start = .); 166 KEEP(*(SORT(.fini_array.*))) 167 KEEP(*(.fini_array*)) 168 PROVIDE_HIDDEN(__fini_array_end = .); 169 } >FLASH 170 171 /* GNU build ID section. Used by pw_build_info. */ 172 .note.gnu.build-id : 173 { 174 . = ALIGN(4); 175 gnu_build_id_begin = .; 176 *(.note.gnu.build-id); 177 } >FLASH 178 179 /* Used by unwind-arm/ */ 180 .ARM : ALIGN(4) { 181 __exidx_start = .; 182 *(.ARM.exidx*) 183 __exidx_end = .; 184 } >FLASH 185 186 /* Explicitly initialized global and static data. (.data)*/ 187 .static_init_ram : ALIGN(4) 188 { 189 *(.data) 190 *(.data*) 191 . = ALIGN(4); 192 } >RAM AT> FLASH 193 194 /* Represents unused space in the FLASH segment. This MUST be the last section 195 * assigned to the FLASH region. 196 */ 197 .FLASH.unused_space (NOLOAD) : ALIGN(4) 198 { 199 . = ABSOLUTE(ORIGIN(FLASH) + LENGTH(FLASH)); 200 } >FLASH 201 202 /* The .zero_init_ram, .heap, and .stack sections below require (NOLOAD) 203 * annotations for LLVM lld, but not GNU ld, because LLVM's lld intentionally 204 * interprets the linker file differently from ld: 205 * 206 * https://discourse.llvm.org/t/lld-vs-ld-section-type-progbits-vs-nobits/5999/3 207 * 208 * Zero initialized global/static data (.bss) is initialized in 209 * pw_boot_Entry() via memset(), so the section doesn't need to be loaded from 210 * flash. The .heap and .stack sections don't require any initialization, 211 * as they only represent allocated memory regions, so they also do not need 212 * to be loaded. 213 */ 214 .zero_init_ram (NOLOAD) : ALIGN(4) 215 { 216 *(.bss) 217 *(.bss*) 218 *(COMMON) 219 . = ALIGN(4); 220 } >RAM 221 222 .heap (NOLOAD) : ALIGN(4) 223 { 224 pw_boot_heap_low_addr = .; 225 . = . + PW_BOOT_HEAP_SIZE; 226 . = ALIGN(4); 227 pw_boot_heap_high_addr = .; 228 } >RAM 229 230 /* Link-time check for stack overlaps. 231 * 232 * The ARMv7-M architecture may require 8-byte alignment of the stack pointer 233 * rather than 4 in some contexts and implementations, so this region is 234 * 8-byte aligned (see ARMv7-M Architecture Reference Manual DDI0403E 235 * section B1.5.7). 236 */ 237 .stack (NOLOAD) : ALIGN(8) 238 { 239 /* Set the address that the main stack pointer should be initialized to. */ 240 pw_boot_stack_low_addr = .; 241 HIDDEN(_stack_size = ORIGIN(RAM) + LENGTH(RAM) - .); 242 /* Align the stack to a lower address to ensure it isn't out of range. */ 243 HIDDEN(_stack_high = (. + _stack_size) & ~0x7); 244 ASSERT(_stack_high - . >= PW_BOOT_MIN_STACK_SIZE, 245 "Error: Not enough RAM for desired minimum stack size."); 246 . = _stack_high; 247 pw_boot_stack_high_addr = .; 248 } >RAM 249 250 /* Represents unused space in the RAM segment. This MUST be the last section 251 * assigned to the RAM region. 252 */ 253 .RAM.unused_space (NOLOAD) : ALIGN(4) 254 { 255 . = ABSOLUTE(ORIGIN(RAM) + LENGTH(RAM)); 256 } >RAM 257 258 /* Discard unwind info. */ 259 .ARM.extab 0x0 (INFO) : 260 { 261 KEEP(*(.ARM.extab*)) 262 } 263} 264 265/* Symbols used by core_init.c: */ 266/* Start of .static_init_ram in FLASH. */ 267_pw_static_init_flash_start = LOADADDR(.static_init_ram); 268 269/* Region of .static_init_ram in RAM. */ 270_pw_static_init_ram_start = ADDR(.static_init_ram); 271_pw_static_init_ram_end = _pw_static_init_ram_start + SIZEOF(.static_init_ram); 272 273/* Region of .zero_init_ram. */ 274_pw_zero_init_ram_start = ADDR(.zero_init_ram); 275_pw_zero_init_ram_end = _pw_zero_init_ram_start + SIZEOF(.zero_init_ram); 276 277/* arm-none-eabi expects `end` symbol to point to start of heap for sbrk. */ 278PROVIDE(end = _pw_zero_init_ram_end); 279 280/* These symbols are used by pw_bloat.bloaty_config to create the memoryregions 281 * data source for bloaty in this format (where the optional _N defaults to 0): 282 * pw_bloat_config_memory_region_NAME_{start,end}{_N,} */ 283pw_bloat_config_memory_region_VECTOR_TABLE_start = ORIGIN(VECTOR_TABLE); 284pw_bloat_config_memory_region_VECTOR_TABLE_end = 285 ORIGIN(VECTOR_TABLE) + LENGTH(VECTOR_TABLE); 286pw_bloat_config_memory_region_FLASH_start = ORIGIN(FLASH); 287pw_bloat_config_memory_region_FLASH_end = ORIGIN(FLASH) + LENGTH(FLASH); 288pw_bloat_config_memory_region_RAM_start = ORIGIN(RAM); 289pw_bloat_config_memory_region_RAM_end = ORIGIN(RAM) + LENGTH(RAM); 290