Skip to content

Commit a188510

Browse files
CopilotJohnAmadis
andcommitted
Fix hard fault: move .dmod.inputs/.dmod.outputs inside .data to fix startup copy
The startup data copy in startup.s copies bytes linearly from ROM (__data_init_start__) to RAM (__data_start__ → __data_end__). Previously, dmod-system.ld was included between .data and __data_end__, creating .inputs and .outputs as separate sections with "> ram AT > rom". The ". = ALIGN(16)" in dmod-system.ld only advances the VMA (RAM) location counter, creating 0–12 byte alignment gaps in RAM that have no corresponding gaps in the ROM layout. This caused the startup copy to misplace the .inputs bytes in RAM, corrupting the Dmod_ApiRegistration_t entries (swapping .Function/.Signature pointers or mixing entries from adjacent registrations). The corrupted .Function value (e.g. 0xd3aefbda) was then used to "connect" the module's Dmod_EnterCritical output pointer, triggering a hard fault at runtime. Fix: inline the .dmod.inputs and .dmod.outputs content directly inside the .data section in linker/common.ld. Alignment padding inside a single section is inserted identically in both the VMA and LMA representations, so the startup byte-for-byte copy correctly initialises both sub-sections in RAM. Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent a6320a9 commit a188510

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

linker/common.ld

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,40 @@ SECTIONS
149149
. = ALIGN(4);
150150
*(.data .data.* .gnu.linkonce.d.*)
151151

152-
} > ram AT > rom
152+
/*
153+
* Include DMOD input/output sections inside .data so that the startup
154+
* data copy (from __data_init_start__ to __data_end__) correctly handles
155+
* them. Placing these sections outside .data with "> ram AT > rom" would
156+
* create VMA alignment gaps (the ". = ALIGN(16)" below) that have no
157+
* corresponding LMA gaps, causing the startup copy to corrupt the .inputs
158+
* section and produce garbage function-pointer values at runtime
159+
* (e.g. Dmod_EnterCritical = 0xd3aefbda → hard fault).
160+
*
161+
* When the subsections live inside a single .data section the linker
162+
* inserts the same alignment padding in both the VMA (RAM) and LMA (ROM)
163+
* representations of the section, so the byte-for-byte startup copy
164+
* remains correct.
165+
*/
166+
167+
. = ALIGN(16);
168+
__inputs_start = .;
169+
PROVIDE(__dmod_inputs_start = .);
170+
KEEP(*(.dmod.inputs))
171+
__inputs_end = .;
172+
PROVIDE(__dmod_inputs_end = .);
173+
__inputs_size = __inputs_end - __inputs_start;
174+
PROVIDE(__dmod_inputs_size = __inputs_end - __inputs_start);
175+
176+
. = ALIGN(16);
177+
__outputs_start = .;
178+
PROVIDE(__dmod_outputs_start = .);
179+
KEEP(*(.dmod.outputs))
180+
__outputs_end = .;
181+
PROVIDE(__dmod_outputs_end = .);
182+
__outputs_size = __outputs_end - __outputs_start;
183+
PROVIDE(__dmod_outputs_size = __outputs_end - __outputs_start);
153184

154-
INCLUDE dmod-system.ld
185+
} > ram AT > rom
155186

156187
. = ALIGN(4);
157188
__data_end__ = .;

0 commit comments

Comments
 (0)