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 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/* Note: This technically doesn't set the firmware's entry point. Setting the 26 * firmware entry point is done by setting vector_table[1] 27 * (Reset_Handler). However, this DOES tell the compiler how to optimize 28 * when --gc-sections is enabled. 29 */ 30ENTRY(Reset) 31 32MEMORY 33{ 34 /* TODO: b/234892223 - Make it possible for projects to freely customize 35 * memory regions. 36 */ 37 38 /* Vector Table (typically in flash) */ 39 VECTOR_TABLE(rx) : ORIGIN = 0x00000000, LENGTH = 1024 40 /* Internal Flash */ 41 FLASH(rx) : ORIGIN = 0x00000400, LENGTH = 255K 42 /* Internal SRAM */ 43 RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 16K 44 45 /* Each memory region above has an associated .*.unused_space section that 46 * overlays the unused space at the end of the memory segment. These segments 47 * are used by pw_bloat.bloaty_config to create the utilization data source 48 * for bloaty size reports. 49 * 50 * These sections MUST be located immediately after the last section that is 51 * placed in the respective memory region or lld will issue a warning like: 52 * 53 * warning: ignoring memory region assignment for non-allocatable section 54 * '.VECTOR_TABLE.unused_space' 55 * 56 * If this warning occurs, it's also likely that LLD will have created quite 57 * large padded regions in the ELF file due to bad cursor operations. This 58 * can cause ELF files to balloon from hundreds of kilobytes to hundreds of 59 * megabytes. 60 * 61 * Attempting to add sections to the memory region AFTER the unused_space 62 * section will cause the region to overflow. 63 */ 64} 65 66SECTIONS 67{ 68 /* This is the link-time vector table. If used, the VTOR (Vector Table Offset 69 * Register) MUST point to this memory location in order to be used. This can 70 * be done by ensuring this section exists at the default location of the VTOR 71 * so it's used on reset, or by explicitly setting the VTOR in a bootloader 72 * manually to point to &pw_boot_vector_table_addr before interrupts are 73 * enabled. 74 * 75 * The ARMv7-M architecture requires this is at least aligned to 128 bytes, 76 * and aligned to a power of two that is greater than 4 times the number of 77 * supported exceptions. 512 has been selected as it accommodates most 78 * devices' vector tables. 79 */ 80 .vector_table : ALIGN(512) 81 { 82 LONG(pw_boot_stack_high_addr); 83 pw_boot_vector_table_addr = .; 84 KEEP(*(.vector_table)) 85 KEEP(*(.vector_table.reset_vector)) 86 KEEP(*(.vector_table.exceptions)) 87 KEEP(*(.vector_table.interrupts)) 88 } >VECTOR_TABLE 89 90 /* Represents unused space in the VECTOR_TABLE segment. This MUST be the last 91 * section assigned to the VECTOR_TABLE region. 92 */ 93 .VECTOR_TABLE.unused_space (NOLOAD) : ALIGN(4) 94 { 95 . = ABSOLUTE(ORIGIN(VECTOR_TABLE) + LENGTH(VECTOR_TABLE)); 96 } >VECTOR_TABLE 97 98 /* Main executable code. */ 99 .code : ALIGN(4) 100 { 101 . = ALIGN(4); 102 /* cortex-m-rt expects these to be nearby each other because short branches 103 * are used. 104 */ 105 *(.PreResetTrampoline); 106 *(.Reset); 107 *(.HardFaultTrampoline); 108 *(.HardFault.*); 109 110 /* Application code. */ 111 *(.text) 112 *(.text*) 113 KEEP(*(.init)) 114 KEEP(*(.fini)) 115 116 . = ALIGN(4); 117 /* Constants.*/ 118 *(.rodata) 119 *(.rodata*) 120 121 /* .preinit_array, .init_array, .fini_array are used by libc. 122 * Each section is a list of function pointers that are called pre-main and 123 * post-exit for object initialization and tear-down. 124 * Since the region isn't explicitly referenced, specify KEEP to prevent 125 * link-time garbage collection. SORT is used for sections that have strict 126 * init/de-init ordering requirements. */ 127 . = ALIGN(4); 128 PROVIDE_HIDDEN(__preinit_array_start = .); 129 KEEP(*(.preinit_array*)) 130 PROVIDE_HIDDEN(__preinit_array_end = .); 131 132 PROVIDE_HIDDEN(__init_array_start = .); 133 KEEP(*(SORT(.init_array.*))) 134 KEEP(*(.init_array*)) 135 PROVIDE_HIDDEN(__init_array_end = .); 136 137 PROVIDE_HIDDEN(__fini_array_start = .); 138 KEEP(*(SORT(.fini_array.*))) 139 KEEP(*(.fini_array*)) 140 PROVIDE_HIDDEN(__fini_array_end = .); 141 } >FLASH 142 143 /* Used by unwind-arm/ */ 144 .ARM : ALIGN(4) { 145 __exidx_start = .; 146 *(.ARM.exidx*) 147 __exidx_end = .; 148 } >FLASH 149 150 /* Explicitly initialized global and static data. (.data)*/ 151 .static_init_ram : ALIGN(4) 152 { 153 *(.data) 154 *(.data*) 155 . = ALIGN(4); 156 } >RAM AT> FLASH 157 158 /* Represents unused space in the FLASH segment. This MUST be the last section 159 * assigned to the FLASH region. 160 */ 161 .FLASH.unused_space (NOLOAD) : ALIGN(4) 162 { 163 . = ABSOLUTE(ORIGIN(FLASH) + LENGTH(FLASH)); 164 } >FLASH 165 166 /* The .zero_init_ram, .heap, and .stack sections below require (NOLOAD) 167 * annotations for LLVM lld, but not GNU ld, because LLVM's lld intentionally 168 * interprets the linker file differently from ld: 169 * 170 * https://discourse.llvm.org/t/lld-vs-ld-section-type-progbits-vs-nobits/5999/3 171 * 172 * Zero initialized global/static data (.bss) is initialized in 173 * pw_boot_Entry() via memset(), so the section doesn't need to be loaded from 174 * flash. The .heap and .stack sections don't require any initialization, 175 * as they only represent allocated memory regions, so they also do not need 176 * to be loaded. 177 */ 178 .zero_init_ram (NOLOAD) : ALIGN(4) 179 { 180 *(.bss) 181 *(.bss*) 182 *(COMMON) 183 . = ALIGN(4); 184 } >RAM 185 186 .heap (NOLOAD) : ALIGN(4) 187 { 188 pw_boot_heap_low_addr = .; 189 . = . + 0; 190 . = ALIGN(4); 191 pw_boot_heap_high_addr = .; 192 } >RAM 193 194 /* Link-time check for stack overlaps. 195 * 196 * The ARMv7-M architecture may require 8-byte alignment of the stack pointer 197 * rather than 4 in some contexts and implementations, so this region is 198 * 8-byte aligned (see ARMv7-M Architecture Reference Manual DDI0403E 199 * section B1.5.7). 200 */ 201 .stack (NOLOAD) : ALIGN(8) 202 { 203 /* Set the address that the main stack pointer should be initialized to. */ 204 pw_boot_stack_low_addr = .; 205 HIDDEN(_stack_size = ORIGIN(RAM) + LENGTH(RAM) - .); 206 /* Align the stack to a lower address to ensure it isn't out of range. */ 207 HIDDEN(_stack_high = (. + _stack_size) & ~0x7); 208 ASSERT(_stack_high - . >= 1K, 209 "Error: Not enough RAM for desired minimum stack size."); 210 . = _stack_high; 211 pw_boot_stack_high_addr = .; 212 } >RAM 213 214 /* Represents unused space in the RAM segment. This MUST be the last section 215 * assigned to the RAM region. 216 */ 217 .RAM.unused_space (NOLOAD) : ALIGN(4) 218 { 219 . = ABSOLUTE(ORIGIN(RAM) + LENGTH(RAM)); 220 } >RAM 221 222 /* Discard unwind info. */ 223 .ARM.extab 0x0 (INFO) : 224 { 225 KEEP(*(.ARM.extab*)) 226 } 227} 228 229/* Symbols used by core_init.c: */ 230/* Start of .static_init_ram in FLASH. */ 231_pw_static_init_flash_start = LOADADDR(.static_init_ram); 232 233/* Region of .static_init_ram in RAM. */ 234_pw_static_init_ram_start = ADDR(.static_init_ram); 235_pw_static_init_ram_end = _pw_static_init_ram_start + SIZEOF(.static_init_ram); 236 237/* Region of .zero_init_ram. */ 238_pw_zero_init_ram_start = ADDR(.zero_init_ram); 239_pw_zero_init_ram_end = _pw_zero_init_ram_start + SIZEOF(.zero_init_ram); 240 241/* Symbols needed for the Rust cortex-m-rt crate. */ 242__sbss = _pw_zero_init_ram_start; 243__ebss = _pw_zero_init_ram_end; 244__sdata = _pw_static_init_ram_start; 245__edata = _pw_static_init_ram_end; 246__sidata = LOADADDR(.static_init_ram); 247__pre_init = DefaultPreInit; 248DefaultHandler = DefaultHandler_; 249NonMaskableInt = DefaultHandler; 250MemoryManagement = DefaultHandler; 251BusFault = DefaultHandler; 252UsageFault = DefaultHandler; 253SVCall = DefaultHandler; 254DebugMonitor = DefaultHandler; 255PendSV = DefaultHandler; 256SysTick = DefaultHandler; 257HardFault = HardFault_; 258 259/* arm-none-eabi expects `end` symbol to point to start of heap for sbrk. */ 260PROVIDE(end = _pw_zero_init_ram_end); 261 262/* These symbols are used by pw_bloat.bloaty_config to create the memoryregions 263 * data source for bloaty in this format (where the optional _N defaults to 0): 264 * pw_bloat_config_memory_region_NAME_{start,end}{_N,} */ 265pw_bloat_config_memory_region_VECTOR_TABLE_start = ORIGIN(VECTOR_TABLE); 266pw_bloat_config_memory_region_VECTOR_TABLE_end = 267 ORIGIN(VECTOR_TABLE) + LENGTH(VECTOR_TABLE); 268pw_bloat_config_memory_region_FLASH_start = ORIGIN(FLASH); 269pw_bloat_config_memory_region_FLASH_end = ORIGIN(FLASH) + LENGTH(FLASH); 270pw_bloat_config_memory_region_RAM_start = ORIGIN(RAM); 271pw_bloat_config_memory_region_RAM_end = ORIGIN(RAM) + LENGTH(RAM); 272 273SECTIONS 274{ 275 /* 276 * This section stores metadata that may be used during tokenized string 277 * decoding. This metadata describes properties that may affect how the 278 * tokenized string is encoded or decoded -- the maximum length of the hash 279 * function and the sizes of certain integer types. 280 * 281 * Metadata is declared as key-value pairs. See the metadata variable in 282 * tokenize.cc for further details. 283 */ 284 .pw_tokenizer.info 0x0 (INFO) : 285 { 286 KEEP(*(.pw_tokenizer.info)) 287 } 288 289 /* 290 * Tokenized string entries are stored in this section. Each entry contains 291 * the original string literal and the calculated token that represents it. In 292 * the compiled code, the token and a compact argument list encoded in a 293 * uint32_t are used in place of the format string. The compiled code 294 * contains no references to the tokenized string entries in this section. 295 * 296 * The tokenized string entry format is specified by the 297 * pw::tokenizer::internal::Entry class in 298 * pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h. 299 * 300 * The section contents are declared with KEEP so that they are not removed 301 * from the ELF. These are never emitted in the final binary or loaded into 302 * memory. 303 */ 304 .pw_tokenizer.entries 0x0 (INFO) : 305 { 306 KEEP(*(.pw_tokenizer.entries.*)) 307 } 308} 309