|
| 1 | +/* # Developer notes |
| 2 | + |
| 3 | +- Symbols that start with a double underscore (__) are considered "private" |
| 4 | + |
| 5 | +- Symbols that start with a single underscore (_) are considered "semi-public"; they can be |
| 6 | + overridden in a user linker script, but should not be referred from user code (e.g. `extern "C" { |
| 7 | + static mut __sbss }`). |
| 8 | + |
| 9 | +- `EXTERN` forces the linker to keep a symbol in the final binary. We use this to make sure a |
| 10 | + symbol if not dropped if it appears in or near the front of the linker arguments and "it's not |
| 11 | + needed" by any of the preceding objects (linker arguments) |
| 12 | + |
| 13 | +- `PROVIDE` is used to provide default values that can be overridden by a user linker script |
| 14 | + |
| 15 | +- On alignment: it's important for correctness that the VMA boundaries of both .bss and .data *and* |
| 16 | + the LMA of .data are all 4-byte aligned. These alignments are assumed by the RAM initialization |
| 17 | + routine. There's also a second benefit: 4-byte aligned boundaries means that you won't see |
| 18 | + "Address (..) is out of bounds" in the disassembly produced by `objdump`. |
| 19 | +*/ |
| 20 | + |
| 21 | +/* Provides information about the memory layout of the device */ |
| 22 | +/* This will be provided by the user (see `memory.x`) or by a Board Support Crate */ |
| 23 | +INCLUDE memory.x |
| 24 | + |
| 25 | +/* # Entry point = reset vector */ |
| 26 | +ENTRY(Reset); |
| 27 | +EXTERN(__RESET_VECTOR); /* depends on the `Reset` symbol */ |
| 28 | + |
| 29 | +/* # Exception vectors */ |
| 30 | +/* This is effectively weak aliasing at the linker level */ |
| 31 | +/* The user can override any of these aliases by defining the corresponding symbol themselves (cf. |
| 32 | + the `exception!` macro) */ |
| 33 | +EXTERN(__EXCEPTIONS); /* depends on all the these PROVIDED symbols */ |
| 34 | + |
| 35 | +EXTERN(DefaultHandler); |
| 36 | + |
| 37 | +PROVIDE(NonMaskableInt = DefaultHandler); |
| 38 | +EXTERN(HardFaultTrampoline); |
| 39 | +PROVIDE(MemoryManagement = DefaultHandler); |
| 40 | +PROVIDE(BusFault = DefaultHandler); |
| 41 | +PROVIDE(UsageFault = DefaultHandler); |
| 42 | +PROVIDE(SecureFault = DefaultHandler); |
| 43 | +PROVIDE(SVCall = DefaultHandler); |
| 44 | +PROVIDE(DebugMonitor = DefaultHandler); |
| 45 | +PROVIDE(PendSV = DefaultHandler); |
| 46 | +PROVIDE(SysTick = DefaultHandler); |
| 47 | + |
| 48 | +PROVIDE(DefaultHandler = DefaultHandler_); |
| 49 | +PROVIDE(HardFault = HardFault_); |
| 50 | + |
| 51 | +/* # Interrupt vectors */ |
| 52 | +EXTERN(__INTERRUPTS); /* `static` variable similar to `__EXCEPTIONS` */ |
| 53 | + |
| 54 | +/* # Pre-initialization function */ |
| 55 | +/* If the user overrides this using the `pre_init!` macro or by creating a `__pre_init` function, |
| 56 | + then the function this points to will be called before the RAM is initialized. */ |
| 57 | +PROVIDE(__pre_init = DefaultPreInit); |
| 58 | + |
| 59 | +/* # Sections */ |
| 60 | +SECTIONS |
| 61 | +{ |
| 62 | + PROVIDE(_stack_start = ORIGIN(RAM) + LENGTH(RAM)); |
| 63 | + |
| 64 | + /* ## Sections in FLASH */ |
| 65 | + /* ### Vector table */ |
| 66 | + .vector_table ORIGIN(FLASH) : |
| 67 | + { |
| 68 | + /* Initial Stack Pointer (SP) value */ |
| 69 | + LONG(_stack_start); |
| 70 | + |
| 71 | + /* Reset vector */ |
| 72 | + KEEP(*(.vector_table.reset_vector)); /* this is the `__RESET_VECTOR` symbol */ |
| 73 | + __reset_vector = .; |
| 74 | + |
| 75 | + /* Exceptions */ |
| 76 | + KEEP(*(.vector_table.exceptions)); /* this is the `__EXCEPTIONS` symbol */ |
| 77 | + __eexceptions = .; |
| 78 | + |
| 79 | + /* Device specific interrupts */ |
| 80 | + KEEP(*(.vector_table.interrupts)); /* this is the `__INTERRUPTS` symbol */ |
| 81 | + } > FLASH |
| 82 | + |
| 83 | + PROVIDE(_stext = ADDR(.vector_table) + SIZEOF(.vector_table)); |
| 84 | + |
| 85 | + /* ### .text */ |
| 86 | + .text _stext : |
| 87 | + { |
| 88 | + *(.text .text.*); |
| 89 | + *(.HardFaultTrampoline); |
| 90 | + *(.HardFault.*); |
| 91 | + . = ALIGN(4); |
| 92 | + __etext = .; |
| 93 | + } > FLASH |
| 94 | + |
| 95 | + /* ### .rodata */ |
| 96 | + .rodata __etext : ALIGN(4) |
| 97 | + { |
| 98 | + *(.rodata .rodata.*); |
| 99 | + |
| 100 | + /* 4-byte align the end (VMA) of this section. |
| 101 | + This is required by LLD to ensure the LMA of the following .data |
| 102 | + section will have the correct alignment. */ |
| 103 | + . = ALIGN(4); |
| 104 | + __erodata = .; |
| 105 | + } > FLASH |
| 106 | + |
| 107 | + /* ## Sections in RAM */ |
| 108 | + /* ### .data */ |
| 109 | + .data : AT(__erodata) ALIGN(4) |
| 110 | + { |
| 111 | + . = ALIGN(4); |
| 112 | + __sdata = .; |
| 113 | + *(.data .data.*); |
| 114 | + . = ALIGN(4); /* 4-byte align the end (VMA) of this section */ |
| 115 | + __edata = .; |
| 116 | + } > RAM |
| 117 | + |
| 118 | + /* LMA of .data */ |
| 119 | + __sidata = LOADADDR(.data); |
| 120 | + |
| 121 | + /* ### .bss */ |
| 122 | + .bss : ALIGN(4) |
| 123 | + { |
| 124 | + . = ALIGN(4); |
| 125 | + __sbss = .; |
| 126 | + *(.bss .bss.*); |
| 127 | + . = ALIGN(4); /* 4-byte align the end (VMA) of this section */ |
| 128 | + __ebss = .; |
| 129 | + } > RAM |
| 130 | + |
| 131 | + /* ### .uninit */ |
| 132 | + .uninit (NOLOAD) : ALIGN(4) |
| 133 | + { |
| 134 | + . = ALIGN(4); |
| 135 | + *(.uninit .uninit.*); |
| 136 | + . = ALIGN(4); |
| 137 | + } > RAM |
| 138 | + |
| 139 | + /* Place the heap right after `.uninit` */ |
| 140 | + . = ALIGN(4); |
| 141 | + __sheap = .; |
| 142 | + |
| 143 | + /* ## .got */ |
| 144 | + /* Dynamic relocations are unsupported. This section is only used to detect relocatable code in |
| 145 | + the input files and raise an error if relocatable code is found */ |
| 146 | + .got (NOLOAD) : |
| 147 | + { |
| 148 | + KEEP(*(.got .got.*)); |
| 149 | + } |
| 150 | + |
| 151 | + /* ## Discarded sections */ |
| 152 | + /DISCARD/ : |
| 153 | + { |
| 154 | + /* Unused exception related info that only wastes space */ |
| 155 | + *(.ARM.exidx); |
| 156 | + *(.ARM.exidx.*); |
| 157 | + *(.ARM.extab.*); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/* Do not exceed this mark in the error messages below | */ |
| 162 | +/* # Alignment checks */ |
| 163 | +ASSERT(ORIGIN(FLASH) % 4 == 0, " |
| 164 | +ERROR(cortex-m-rt): the start of the FLASH region must be 4-byte aligned"); |
| 165 | + |
| 166 | +ASSERT(ORIGIN(RAM) % 4 == 0, " |
| 167 | +ERROR(cortex-m-rt): the start of the RAM region must be 4-byte aligned"); |
| 168 | + |
| 169 | +ASSERT(__sdata % 4 == 0 && __edata % 4 == 0, " |
| 170 | +BUG(cortex-m-rt): .data is not 4-byte aligned"); |
| 171 | + |
| 172 | +ASSERT(__sidata % 4 == 0, " |
| 173 | +BUG(cortex-m-rt): the LMA of .data is not 4-byte aligned"); |
| 174 | + |
| 175 | +ASSERT(__sbss % 4 == 0 && __ebss % 4 == 0, " |
| 176 | +BUG(cortex-m-rt): .bss is not 4-byte aligned"); |
| 177 | + |
| 178 | +ASSERT(__sheap % 4 == 0, " |
| 179 | +BUG(cortex-m-rt): start of .heap is not 4-byte aligned"); |
| 180 | + |
| 181 | +/* # Position checks */ |
| 182 | + |
| 183 | +/* ## .vector_table */ |
| 184 | +ASSERT(__reset_vector == ADDR(.vector_table) + 0x8, " |
| 185 | +BUG(cortex-m-rt): the reset vector is missing"); |
| 186 | + |
| 187 | +ASSERT(__eexceptions == ADDR(.vector_table) + 0x40, " |
| 188 | +BUG(cortex-m-rt): the exception vectors are missing"); |
| 189 | + |
| 190 | +ASSERT(SIZEOF(.vector_table) > 0x40, " |
| 191 | +ERROR(cortex-m-rt): The interrupt vectors are missing. |
| 192 | +Possible solutions, from most likely to less likely: |
| 193 | +- Link to a svd2rust generated device crate |
| 194 | +- Disable the 'device' feature of cortex-m-rt to build a generic application (a dependency |
| 195 | +may be enabling it) |
| 196 | +- Supply the interrupt handlers yourself. Check the documentation for details."); |
| 197 | + |
| 198 | +/* ## .text */ |
| 199 | +ASSERT(ADDR(.vector_table) + SIZEOF(.vector_table) <= _stext, " |
| 200 | +ERROR(cortex-m-rt): The .text section can't be placed inside the .vector_table section |
| 201 | +Set _stext to an address greater than the end of .vector_table (See output of `nm`)"); |
| 202 | + |
| 203 | +ASSERT(_stext + SIZEOF(.text) < ORIGIN(FLASH) + LENGTH(FLASH), " |
| 204 | +ERROR(cortex-m-rt): The .text section must be placed inside the FLASH memory. |
| 205 | +Set _stext to an address smaller than 'ORIGIN(FLASH) + LENGTH(FLASH)'"); |
| 206 | + |
| 207 | +/* # Other checks */ |
| 208 | +ASSERT(SIZEOF(.got) == 0, " |
| 209 | +ERROR(cortex-m-rt): .got section detected in the input object files |
| 210 | +Dynamic relocations are not supported. If you are linking to C code compiled using |
| 211 | +the 'cc' crate then modify your build script to compile the C code _without_ |
| 212 | +the -fPIC flag. See the documentation of the `cc::Build.pic` method for details."); |
| 213 | +/* Do not exceed this mark in the error messages above | */ |
0 commit comments