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 /* Used by unwind-arm/ */ 172 .ARM : ALIGN(4) { 173 __exidx_start = .; 174 *(.ARM.exidx*) 175 __exidx_end = .; 176 } >FLASH 177 178 /* Explicitly initialized global and static data. (.data)*/ 179 .static_init_ram : ALIGN(4) 180 { 181 *(.data) 182 *(.data*) 183 . = ALIGN(4); 184 } >RAM AT> FLASH 185 186 /* Represents unused space in the FLASH segment. This MUST be the last section 187 * assigned to the FLASH region. 188 */ 189 .FLASH.unused_space (NOLOAD) : ALIGN(4) 190 { 191 . = ABSOLUTE(ORIGIN(FLASH) + LENGTH(FLASH)); 192 } >FLASH 193 194 /* The .zero_init_ram, .heap, and .stack sections below require (NOLOAD) 195 * annotations for LLVM lld, but not GNU ld, because LLVM's lld intentionally 196 * interprets the linker file differently from ld: 197 * 198 * https://discourse.llvm.org/t/lld-vs-ld-section-type-progbits-vs-nobits/5999/3 199 * 200 * Zero initialized global/static data (.bss) is initialized in 201 * pw_boot_Entry() via memset(), so the section doesn't need to be loaded from 202 * flash. The .heap and .stack sections don't require any initialization, 203 * as they only represent allocated memory regions, so they also do not need 204 * to be loaded. 205 */ 206 .zero_init_ram (NOLOAD) : ALIGN(4) 207 { 208 *(.bss) 209 *(.bss*) 210 *(COMMON) 211 . = ALIGN(4); 212 } >RAM 213 214 .heap (NOLOAD) : ALIGN(4) 215 { 216 pw_boot_heap_low_addr = .; 217 . = . + PW_BOOT_HEAP_SIZE; 218 . = ALIGN(4); 219 pw_boot_heap_high_addr = .; 220 } >RAM 221 222 /* Link-time check for stack overlaps. 223 * 224 * The ARMv7-M architecture may require 8-byte alignment of the stack pointer 225 * rather than 4 in some contexts and implementations, so this region is 226 * 8-byte aligned (see ARMv7-M Architecture Reference Manual DDI0403E 227 * section B1.5.7). 228 */ 229 .stack (NOLOAD) : ALIGN(8) 230 { 231 /* Set the address that the main stack pointer should be initialized to. */ 232 pw_boot_stack_low_addr = .; 233 HIDDEN(_stack_size = ORIGIN(RAM) + LENGTH(RAM) - .); 234 /* Align the stack to a lower address to ensure it isn't out of range. */ 235 HIDDEN(_stack_high = (. + _stack_size) & ~0x7); 236 ASSERT(_stack_high - . >= PW_BOOT_MIN_STACK_SIZE, 237 "Error: Not enough RAM for desired minimum stack size."); 238 . = _stack_high; 239 pw_boot_stack_high_addr = .; 240 } >RAM 241 242 /* Represents unused space in the RAM segment. This MUST be the last section 243 * assigned to the RAM region. 244 */ 245 .RAM.unused_space (NOLOAD) : ALIGN(4) 246 { 247 . = ABSOLUTE(ORIGIN(RAM) + LENGTH(RAM)); 248 } >RAM 249 250 /* Discard unwind info. */ 251 .ARM.extab 0x0 (INFO) : 252 { 253 KEEP(*(.ARM.extab*)) 254 } 255} 256 257/* Symbols used by core_init.c: */ 258/* Start of .static_init_ram in FLASH. */ 259_pw_static_init_flash_start = LOADADDR(.static_init_ram); 260 261/* Region of .static_init_ram in RAM. */ 262_pw_static_init_ram_start = ADDR(.static_init_ram); 263_pw_static_init_ram_end = _pw_static_init_ram_start + SIZEOF(.static_init_ram); 264 265/* Region of .zero_init_ram. */ 266_pw_zero_init_ram_start = ADDR(.zero_init_ram); 267_pw_zero_init_ram_end = _pw_zero_init_ram_start + SIZEOF(.zero_init_ram); 268 269/* arm-none-eabi expects `end` symbol to point to start of heap for sbrk. */ 270PROVIDE(end = _pw_zero_init_ram_end); 271 272/* These symbols are used by pw_bloat.bloaty_config to create the memoryregions 273 * data source for bloaty in this format (where the optional _N defaults to 0): 274 * pw_bloat_config_memory_region_NAME_{start,end}{_N,} */ 275pw_bloat_config_memory_region_VECTOR_TABLE_start = ORIGIN(VECTOR_TABLE); 276pw_bloat_config_memory_region_VECTOR_TABLE_end = 277 ORIGIN(VECTOR_TABLE) + LENGTH(VECTOR_TABLE); 278pw_bloat_config_memory_region_FLASH_start = ORIGIN(FLASH); 279pw_bloat_config_memory_region_FLASH_end = ORIGIN(FLASH) + LENGTH(FLASH); 280pw_bloat_config_memory_region_RAM_start = ORIGIN(RAM); 281pw_bloat_config_memory_region_RAM_end = ORIGIN(RAM) + LENGTH(RAM); 282