Skip to content

generate_linker_script: align .data LMA to 4-byte boundary#831

Merged
Grazfather merged 1 commit intomainfrom
fix_data_alignment
Jan 4, 2026
Merged

generate_linker_script: align .data LMA to 4-byte boundary#831
Grazfather merged 1 commit intomainfrom
fix_data_alignment

Conversation

@Grazfather
Copy link
Collaborator

@Grazfather Grazfather commented Jan 4, 2026

The startup code for riscv uses word-aligned loads (The lw instruction) to copy .data from flash to RAM. When the .text section ended at an unaligned address, .data's flash location (LMA) became misaligned, causing hardware faults during startup when copying the .data section into RAM.

This was triggered by certain code combinations that caused .text to end at addresses like 0x1bc1 instead of 0x1bc4. The unaligned load would fault before any user code executed.

riscv64-unknown-elf-objdump -h zig-out/firmware/lana_tny_dma.elf | grep data -A2
  2 .data         00000100  20000000  00001bc1  00003000  2**0

Fixed by adding . = ALIGN(4); at the end of .text section to ensure the next section's load address in flash is properly aligned for word loads.

@Grazfather Grazfather marked this pull request as ready for review January 4, 2026 19:27
@tact1m4n3
Copy link
Collaborator

Should we add the same for bss?

The startup code uses word-aligned loads (lw instruction) to copy .data
from flash to RAM. When .text section ended at an unaligned address,
.data's flash location (LMA) became misaligned, causing hardware faults
during startup before main() was even reached.

This was triggered by certain code combinations that caused .text to end
at addresses like 0x1bc1 instead of 0x1bc4. The unaligned load would
fault before any user code executed.

Fix: Add `. = ALIGN(4);` at the end of .text section to ensure the next
section's load address in flash is properly aligned for word loads.
@Grazfather Grazfather enabled auto-merge (squash) January 4, 2026 20:22
@Grazfather Grazfather merged commit 9bbd91a into main Jan 4, 2026
52 checks passed
@Grazfather Grazfather deleted the fix_data_alignment branch January 4, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants