Skip to content

Commit b78acd5

Browse files
committed
[cortex-m] Beautify linkerscript output
1 parent 9ea3b06 commit b78acd5

File tree

6 files changed

+105
-89
lines changed

6 files changed

+105
-89
lines changed

src/modm/platform/core/cortex/linker.macros

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,56 @@
1515
%% endmacro
1616

1717
%% macro prefix()
18+
OUTPUT_FORMAT("elf32-littlearm")
19+
OUTPUT_ARCH(arm)
20+
ENTRY(Reset_Handler)
21+
1822
MEMORY
1923
{
20-
%% for memory in (memories + cont_ram_regions)
24+
%% for memory in memories
2125
{{ memory.name | upper }} ({{ memory.access }}) : ORIGIN = {{ "0x%08X" % memory.start }}, LENGTH = {{ memory.size }}
22-
%% endfor
26+
%% endfor
27+
%% for memory in cont_ram_regions
28+
%% if memory.cont_name != memory.name
29+
{{ memory.cont_name | upper }} ({{ memory.access }}) : ORIGIN = {{ "0x%08X" % memory.start }}, LENGTH = {{ memory.size }}
30+
%% endif
31+
%% endfor
32+
%% if linkerscript_memory
2333
{{ linkerscript_memory | indent(first=True) }}
34+
%% endif
2435
}
2536

26-
%% for memory in memories
37+
%% for memory in memories
2738
__{{memory.name}}_start = ORIGIN({{memory.name|upper}});
2839
__{{memory.name}}_end = ORIGIN({{memory.name|upper}}) + LENGTH({{memory.name|upper}});
29-
%% endfor
30-
31-
OUTPUT_FORMAT("elf32-littlearm")
32-
OUTPUT_ARCH(arm)
33-
ENTRY(Reset_Handler)
40+
%% endfor
41+
%#
3442

35-
/* the handler stack is used for main program as well as interrupts */
43+
/* The handler stack is used for main program as well as interrupts */
3644
MAIN_STACK_SIZE = {{ options[":platform:cortex-m:main_stack_size"] }};
3745
PROCESS_STACK_SIZE = {{ process_stack_size }};
3846
TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
3947
%% endmacro
4048

4149

4250
%% macro section_vector_rom(memory)
51+
/* Read-only vector table in {{memory}} */
4352
.text :
4453
{
4554
__vector_table_rom_start = .;
4655
__vector_table_ram_load = .;
47-
/* Initial stack address, Reset and NMI handler */
4856
KEEP(*(.vector_rom))
57+
__vector_table_rom_end = .;
4958
} >{{memory}}
5059
%% endmacro
5160

5261

5362
%% macro section_vector_ram(memory, table_copy)
63+
/* Read-Write vector table in {{memory}} */
5464
%% do table_copy.append("vector_table_ram")
5565
.vectors (NOLOAD) :
5666
{
5767
__vector_table_ram_start = .;
58-
/* Vector table in RAM, only if remapped */
5968
KEEP(*(.vector_ram))
6069
. = ALIGN(4);
6170
__vector_table_ram_end = .;
@@ -64,10 +73,11 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
6473

6574

6675
%% macro section_stack(memory, start=None)
76+
/* Main stack in {{memory}} */
6777
.stack (NOLOAD) :
6878
{
6979
%% if start != None
70-
. = {{ start }};
80+
. += {{ start }};
7181
%% endif
7282
__stack_start = .;
7383

@@ -85,6 +95,7 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
8595

8696

8797
%% macro section_tables(memory, copy, zero, heap)
98+
/* Memory layout configuration tables */
8899
.rodata :
89100
{
90101
. = ALIGN(4);
@@ -104,35 +115,27 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
104115
__table_copy_intern_end = .;
105116

106117
__table_zero_extern_start = .;
118+
%% if linkerscript_extern_zero
107119
{{ linkerscript_extern_zero | indent(8, first=True) }}
120+
%% endif
108121
__table_zero_extern_end = .;
109122
__table_copy_extern_start = .;
123+
%% if linkerscript_extern_copy
110124
{{ linkerscript_extern_copy | indent(8, first=True) }}
125+
%% endif
111126
__table_copy_extern_end = .;
112127

113-
/* SRAM properties bit mask (16-bit):
114-
*
115-
* - 0x0001: accessible via S-Bus
116-
* - 0x0002: accessible via D-Bus
117-
* - 0x0004: accessible via I-Bus
118-
* - 0x0008: accessible via DMA
119-
* - 0x0010: accessible via DMA2D
120-
*
121-
* - 0x1FE0: reserved
122-
*
123-
* - 0x2000: Fast memory (Core- or Tightly-Coupled)
124-
* - 0x4000: non-volatile (or battery backed) memory
125-
* - 0x8000: external memory
126-
*/
128+
/* See `modm:architecture:memory` for bitmask */
127129
__table_heap_start = .;
128130
%% for section in heap
129131
LONG({{ section.prop }})
130132
LONG(__{{ section.name }}_start)
131133
LONG(__{{ section.name }}_end)
132134
%% endfor
135+
%% if linkerscript_extern_heap
133136
{{ linkerscript_extern_heap | indent(8, first=True) }}
137+
%% endif
134138
__table_heap_end = .;
135-
136139
} >{{memory}}
137140
%% endmacro
138141

@@ -147,9 +150,9 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
147150
. = ALIGN(4);
148151
__{{name}}_end = .;
149152
} >{{memory}}
150-
151153
%% do table_copy.extend(sections)
152154
%% for section in sections
155+
%#
153156
.{{section}} :
154157
{
155158
__{{section}}_load = LOADADDR(.{{section}});
@@ -172,8 +175,8 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
172175
. = ALIGN(4);
173176
__{{section}}_end = .;
174177
} >{{ placement if placement else memory }}
178+
%#
175179
%% endfor
176-
177180
.{{name}} (NOLOAD) :
178181
{
179182
. = ALIGN(4);
@@ -187,20 +190,22 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
187190
%% macro all_heap_sections(table_copy, table_zero, table_heap)
188191
%% for cont_region in cont_ram_regions
189192
%% for region in cont_region.contains
193+
/* Sections in {{ region.name|upper }} */
190194
%% if region.index
191-
{{ section(cont_region.name|upper + " AT >FLASH", "data_"+region.name, table_copy) }}
195+
{{ section(cont_region.cont_name|upper + " AT >FLASH", "data_"+region.name, table_copy) }}
192196
%% do table_zero.append("bss_"+region.name)
193197
%% endif
194-
195-
{{ section_heap(region.name|upper, "heap_"+region.name, cont_region.name|upper,
198+
{{ section_heap(region.name|upper, "heap_"+region.name, cont_region.cont_name|upper,
196199
sections=(["bss_"+region.name] if region.index else []) + ["noinit_"+region.name]) }}
197200
%% do table_heap.insert(region.index, {"name": "heap_"+region.name, "prop": "0x2013" if "dtcm" in region.name else "0x001f"})
201+
%#
198202
%% endfor
199203
%% endfor
200204
%% endmacro
201205

202206

203207
%% macro section_rom(memory)
208+
/* Read-only sections in {{memory}} */
204209
.text :
205210
{
206211
*(.text .text.* .gnu.linkonce.t.*)
@@ -219,7 +224,6 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
219224
__init_array_end = .;
220225

221226
__hardware_init_start = .;
222-
/* Table symbols are alphabetically sorted! */
223227
KEEP(*(SORT(.hardware_init.order_*)))
224228
KEEP(*(SORT(.hardware_init)))
225229
. = ALIGN(4);
@@ -233,13 +237,15 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
233237
__build_id = .;
234238
KEEP(*(.note.gnu.build-id))
235239
} >{{memory}}
240+
236241
/* We do not call static destructors ever */
237242
/DISCARD/ :
238243
{
239244
*(.fini_array .fini_array.*)
240245
}
241-
242246
%% if with_cpp_exceptions
247+
%#
248+
/* C++ exception unwind tables */
243249
.exidx :
244250
{
245251
. = ALIGN(4);
@@ -255,9 +261,8 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
255261
KEEP(*(.eh_frame*))
256262
} >{{memory}}
257263
%% else
258-
/* Unwind tables are used to unwind the stack for C++ exceptions. Even
259-
* though we disabled exceptions, pre-compiled libraries such as libstdc++
260-
* still have to raise exceptions. */
264+
%#
265+
/* C++ exception unwind tables are discarded */
261266
/DISCARD/ :
262267
{
263268
*(.ARM.extab* .gnu.linkonce.armextab.*)
@@ -266,16 +271,18 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
266271
}
267272
%% endif
268273
%% if not with_heap
274+
%#
275+
/* Catch use of dynamic memory without `modm:platform:heap` module. */
269276
/DISCARD/ :
270277
{
271-
/* See `modm:platform:heap` module for an explanation! */
272278
*({{no_heap_section}})
273279
}
274280
%% endif
275281
%% endmacro
276282

277283

278284
%% macro section_ram(memory, rom, table_copy, table_zero, sections_data=[], sections_bss=[])
285+
/* Read-write sections in {{memory}} */
279286
%% do table_copy.append("data")
280287
.data :
281288
{
@@ -286,9 +293,9 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
286293
. = ALIGN(4);
287294
__data_end = .;
288295
} >{{memory}} AT >{{rom}}
289-
290296
%% do table_copy.extend(sections_data)
291297
%% for section in sections_data
298+
%#
292299
.{{section}} :
293300
{
294301
__{{section}}_load = LOADADDR(.{{section}});
@@ -298,7 +305,7 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
298305
__{{section}}_end = .;
299306
} >{{memory}} AT >{{rom}}
300307
%% endfor
301-
308+
%#
302309
%% do table_zero.append("bss")
303310
.bss (NOLOAD) :
304311
{
@@ -307,9 +314,9 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
307314
. = ALIGN(4);
308315
__bss_end = .;
309316
} >{{memory}}
310-
311317
%% do table_zero.extend(sections_bss)
312318
%% for section in sections_bss
319+
%#
313320
.{{section}} (NOLOAD) :
314321
{
315322
__{{section}}_start = . ;
@@ -318,7 +325,7 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
318325
__{{section}}_end = .;
319326
} >{{memory}}
320327
%% endfor
321-
328+
%#
322329
.noinit (NOLOAD) :
323330
{
324331
__noinit_start = .;

src/modm/platform/core/cortex/module.lb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ def common_memories(env):
9494
if cont and (cont[-1]["start"] + cont[-1]["size"] == m["start"]):
9595
cont[-1]["size"] += m["size"]
9696
cont[-1]["contains"].append({**m, "index": index})
97+
if not cont[-1]["cont_name"].startswith("cont_"):
98+
cont[-1]["cont_name"] = "cont_" + cont[-1]["cont_name"]
9799
else:
98100
cont.append(dict(m))
99101
cont[-1]["contains"] = [{**m, "index": index}]
100-
cont[-1]["name"] = "cont_" + cont[-1]["name"]
102+
cont[-1]["cont_name"] = cont[-1]["name"]
101103
index += 1
102104

103105
properties = {

src/modm/platform/core/cortex/ram.ld.in

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
%% set table_zero = []
88

99
%% if vector_table_location == "ram"
10-
/* Round up the number of vector table entries to the nearest power-of-two and multiply by 8. */
11-
__vec_alignment = (1 << LOG2CEIL({{ highest_irq + 16 }})) * 8;
12-
/* compute the vector table offset from start of RAM */
13-
__vec_offset = ALIGN(TOTAL_STACK_SIZE, __vec_alignment);
14-
%% else
15-
__vec_offset = TOTAL_STACK_SIZE;
10+
/* Computes stack offset so that vector table is aligned */
11+
__stack_offset = ALIGN(TOTAL_STACK_SIZE, (1 << LOG2CEIL({{ highest_irq + 16 }})) * 8) - TOTAL_STACK_SIZE;
12+
%#
1613
%% endif
1714

1815
SECTIONS
@@ -23,25 +20,29 @@ SECTIONS
2320

2421
{{ linker.section_rom("FLASH") }}
2522

26-
27-
{{ linker.section_stack(cont_ram_regions[0].name|upper, "__vec_offset - TOTAL_STACK_SIZE") }}
23+
{{ linker.section_stack(cont_ram_regions[0].cont_name|upper, "__stack_offset" if vector_table_location == "ram" else None) }}
2824

2925
%% if vector_table_location == "ram"
30-
{{ linker.section_vector_ram(cont_ram_regions[0].name|upper, table_copy) }}
26+
{{ linker.section_vector_ram(cont_ram_regions[0].cont_name|upper, table_copy) }}
3127
%% endif
3228

33-
{{ linker.section_ram(cont_ram_regions[0].name|upper, "FLASH", table_copy, table_zero,
29+
{{ linker.section_ram(cont_ram_regions[0].cont_name|upper, "FLASH", table_copy, table_zero,
3430
sections_data=["fastdata", "fastcode", "data_" + cont_ram_regions[0].contains[0].name],
3531
sections_bss=["bss_" + cont_ram_regions[0].contains[0].name]) }}
3632

3733
{{ linker.all_heap_sections(table_copy, table_zero, table_heap) }}
34+
3835
%% if with_crashcatcher
39-
g_crashCatcherStack = . - 500;
36+
%#
37+
/* Bottom of crash stack for `modm:platform:fault` */
38+
g_crashCatcherStack = . - 500;
39+
%#
4040
%% endif
4141

42-
42+
%% if linkerscript_sections
4343
{{ linkerscript_sections | indent(first=True) }}
44-
44+
%#
45+
%% endif
4546

4647
{{ linker.section_tables("FLASH", table_copy, table_zero, table_heap) }}
4748

0 commit comments

Comments
 (0)