Skip to content

Commit 96b76f9

Browse files
bors[bot]adamgreigjaparic
committed
Merge #87
87: Update linker script to put .data into FLASH r=japaric a=adamgreig This is the updated linker script and test from #86. Sadly it looks like LLD does not use the `ALIGN(4)` in the `.data` section header to align the LMA (unlike gcc), so we still need the `. = ALIGN(4)` at the end of `.rodata` to ensure LMA alignment. I've added a failing example which the ci script ensures fails when `.data` causes overflow. It just checks for non-zero return from cargo rather than specifically grepping for the linker error messages about `.data` overflowing; since we only have the one failing example I could remove the loop and just specifically check it and grep for the relevant error from gcc and lld if people think that would be better. Co-authored-by: Adam Greig <[email protected]> Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 230d422 + 97b184d commit 96b76f9

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

cortex-m-rt/ci/script.sh

+23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ main() {
1111
main
1212
state
1313
)
14+
local fail_examples=(
15+
data_overflow
16+
)
1417
if [ $TRAVIS_RUST_VERSION = nightly ]; then
1518
# linking with GNU LD
1619
for ex in "${examples[@]}"; do
@@ -22,6 +25,15 @@ main() {
2225
-C link-arg=-nostartfiles \
2326
-C link-arg=-Wl,-Tlink.x
2427
done
28+
for ex in "${fail_examples[@]}"; do
29+
! cargo rustc --target $TARGET --example $ex -- \
30+
-C link-arg=-nostartfiles \
31+
-C link-arg=-Wl,-Tlink.x
32+
33+
! cargo rustc --target $TARGET --example $ex --release -- \
34+
-C link-arg=-nostartfiles \
35+
-C link-arg=-Wl,-Tlink.x
36+
done
2537

2638
cargo rustc --target $TARGET --example device --features device -- \
2739
-C link-arg=-nostartfiles \
@@ -43,6 +55,17 @@ main() {
4355
-Z linker-flavor=ld.lld \
4456
-C link-arg=-Tlink.x
4557
done
58+
for ex in "${fail_examples[@]}"; do
59+
! cargo rustc --target $TARGET --example $ex -- \
60+
-C linker=rust-lld \
61+
-Z linker-flavor=ld.lld \
62+
-C link-arg=-Tlink.x
63+
64+
! cargo rustc --target $TARGET --example $ex --release -- \
65+
-C linker=rust-lld \
66+
-Z linker-flavor=ld.lld \
67+
-C link-arg=-Tlink.x
68+
done
4669

4770
cargo rustc --target $TARGET --example device --features device -- \
4871
-C linker=rust-lld \

cortex-m-rt/examples/data_overflow.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//! This is not an example; this is a linker overflow detection test
2+
//! which should fail to link due to .data overflowing FLASH.
3+
4+
#![deny(warnings)]
5+
#![no_main]
6+
#![no_std]
7+
8+
#[macro_use(entry)]
9+
extern crate cortex_m_rt as rt;
10+
extern crate panic_abort;
11+
12+
use core::ptr;
13+
14+
entry!(main);
15+
16+
// This large static array uses most of .rodata
17+
static RODATA: [u8; 48*1024] = [1u8; 48*1024];
18+
19+
// This large mutable array causes .data to use the rest of FLASH
20+
// without also overflowing RAM.
21+
static mut DATA: [u8; 16*1024] = [1u8; 16*1024];
22+
23+
fn main() -> ! {
24+
unsafe {
25+
let _bigdata = ptr::read_volatile(&RODATA as *const u8);
26+
let _bigdata = ptr::read_volatile(&DATA as *const u8);
27+
}
28+
29+
loop {}
30+
}

cortex-m-rt/link.x.in

+7-7
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,24 @@ SECTIONS
8484
} > FLASH
8585

8686
/* ### .rodata */
87-
.rodata :
87+
.rodata : ALIGN(4)
8888
{
8989
*(.rodata .rodata.*);
9090

91-
/* 4-byte align the end (VMA) of this section */
92-
/* WHY? To my knowledge there's no way to indicate the alignment of *LMA* so we align *this*
93-
section with the goal of using its end address as the LMA of .data */
91+
/* 4-byte align the end (VMA) of this section.
92+
This is required by LLD to ensure the LMA of the following .data
93+
section will have the correct alignment. */
9494
. = ALIGN(4);
9595
} > FLASH
9696

9797
/* ## Sections in RAM */
9898
/* ### .data */
99-
.data : AT(ADDR(.rodata) + SIZEOF(.rodata)) /* LMA */
99+
.data : ALIGN(4)
100100
{
101101
*(.data .data.*);
102102

103103
. = ALIGN(4); /* 4-byte align the end (VMA) of this section */
104-
} > RAM
104+
} > RAM AT > FLASH
105105

106106
/* VMA of .data */
107107
__sdata = ADDR(.data);
@@ -111,7 +111,7 @@ SECTIONS
111111
__sidata = LOADADDR(.data);
112112

113113
/* ### .bss */
114-
.bss :
114+
.bss : ALIGN(4)
115115
{
116116
*(.bss .bss.*);
117117

cortex-m-rt/memory.x

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/* Device specific memory layout */
22

3+
/* This file is used to build the cortex-m-rt examples,
4+
but not other applications using cortex-m-rt. */
5+
36
MEMORY
47
{
58
/* FLASH and RAM are mandatory memory regions */
9+
/* Update examples/data_overflow.rs if you change these sizes. */
610
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
711
RAM : ORIGIN = 0x20000000, LENGTH = 20K
812

0 commit comments

Comments
 (0)