aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Adam Greig <adam@adamgreig.com> 2018-08-12 01:09:21 +0100
committerGravatar Adam Greig <adam@adamgreig.com> 2018-08-12 01:20:18 +0100
commit277436ec86b3e52fe070f54f10cca64e3800177f (patch)
treec82956a7730b299c2cf529ad9919dcd5c996533f
parentb4186cfe5fc9a8e51d73549cddbf0f49db3a49ba (diff)
downloadcortex-m-277436ec86b3e52fe070f54f10cca64e3800177f.tar.gz
cortex-m-277436ec86b3e52fe070f54f10cca64e3800177f.tar.zst
cortex-m-277436ec86b3e52fe070f54f10cca64e3800177f.zip
Update linker script to put .data into FLASH
Previously .data's LMA was specified by a computated address instead of placing it into FLASH explicitly, which means FLASH overflows caused by .data would not be detected by the linker. Fixes #86.
-rw-r--r--cortex-m-rt/ci/script.sh23
-rw-r--r--cortex-m-rt/examples/data_overflow.rs42
-rw-r--r--cortex-m-rt/link.x.in14
-rw-r--r--cortex-m-rt/memory.x4
4 files changed, 76 insertions, 7 deletions
diff --git a/cortex-m-rt/ci/script.sh b/cortex-m-rt/ci/script.sh
index 2515b8f..2baec51 100644
--- a/cortex-m-rt/ci/script.sh
+++ b/cortex-m-rt/ci/script.sh
@@ -11,6 +11,9 @@ main() {
main
state
)
+ local fail_examples=(
+ data_overflow
+ )
if [ $TRAVIS_RUST_VERSION = nightly ]; then
# linking with GNU LD
for ex in "${examples[@]}"; do
@@ -22,6 +25,15 @@ main() {
-C link-arg=-nostartfiles \
-C link-arg=-Wl,-Tlink.x
done
+ for ex in "${fail_examples[@]}"; do
+ ! cargo rustc --target $TARGET --example $ex -- \
+ -C link-arg=-nostartfiles \
+ -C link-arg=-Wl,-Tlink.x
+
+ ! cargo rustc --target $TARGET --example $ex --release -- \
+ -C link-arg=-nostartfiles \
+ -C link-arg=-Wl,-Tlink.x
+ done
cargo rustc --target $TARGET --example device --features device -- \
-C link-arg=-nostartfiles \
@@ -43,6 +55,17 @@ main() {
-Z linker-flavor=ld.lld \
-C link-arg=-Tlink.x
done
+ for ex in "${fail_examples[@]}"; do
+ ! cargo rustc --target $TARGET --example $ex -- \
+ -C linker=rust-lld \
+ -Z linker-flavor=ld.lld \
+ -C link-arg=-Tlink.x
+
+ ! cargo rustc --target $TARGET --example $ex --release -- \
+ -C linker=rust-lld \
+ -Z linker-flavor=ld.lld \
+ -C link-arg=-Tlink.x
+ done
cargo rustc --target $TARGET --example device --features device -- \
-C linker=rust-lld \
diff --git a/cortex-m-rt/examples/data_overflow.rs b/cortex-m-rt/examples/data_overflow.rs
new file mode 100644
index 0000000..f01f91c
--- /dev/null
+++ b/cortex-m-rt/examples/data_overflow.rs
@@ -0,0 +1,42 @@
+//! This is not an example; this is a linker overflow detection test
+//! which should fail to link due to .data overflowing FLASH.
+
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+#[macro_use(entry, exception)]
+extern crate cortex_m_rt as rt;
+extern crate panic_abort;
+
+use core::ptr;
+
+use rt::ExceptionFrame;
+
+entry!(main);
+
+// This large static array uses most of .rodata
+static RODATA: [u8; 48*1024] = [1u8; 48*1024];
+
+// This large mutable array causes .data to use the rest of FLASH
+// without also overflowing RAM.
+static mut DATA: [u8; 16*1024] = [1u8; 16*1024];
+
+fn main() -> ! {
+ unsafe {
+ let _bigdata = ptr::read_volatile(&RODATA as *const u8);
+ let _bigdata = ptr::read_volatile(&DATA as *const u8);
+ }
+
+ loop {}
+}
+
+exception!(HardFault, hard_fault);
+
+fn hard_fault(_ef: &ExceptionFrame) -> ! {
+ loop {}
+}
+
+exception!(*, default_handler);
+
+fn default_handler(_irqn: i16) {}
diff --git a/cortex-m-rt/link.x.in b/cortex-m-rt/link.x.in
index 5fa7dbf..dfcf262 100644
--- a/cortex-m-rt/link.x.in
+++ b/cortex-m-rt/link.x.in
@@ -84,24 +84,24 @@ SECTIONS
} > FLASH
/* ### .rodata */
- .rodata :
+ .rodata : ALIGN(4)
{
*(.rodata .rodata.*);
- /* 4-byte align the end (VMA) of this section */
- /* WHY? To my knowledge there's no way to indicate the alignment of *LMA* so we align *this*
- section with the goal of using its end address as the LMA of .data */
+ /* 4-byte align the end (VMA) of this section.
+ This is required by LLD to ensure the LMA of the following .data
+ section will have the correct alignment. */
. = ALIGN(4);
} > FLASH
/* ## Sections in RAM */
/* ### .data */
- .data : AT(ADDR(.rodata) + SIZEOF(.rodata)) /* LMA */
+ .data : ALIGN(4)
{
*(.data .data.*);
. = ALIGN(4); /* 4-byte align the end (VMA) of this section */
- } > RAM
+ } > RAM AT > FLASH
/* VMA of .data */
__sdata = ADDR(.data);
@@ -111,7 +111,7 @@ SECTIONS
__sidata = LOADADDR(.data);
/* ### .bss */
- .bss :
+ .bss : ALIGN(4)
{
*(.bss .bss.*);
diff --git a/cortex-m-rt/memory.x b/cortex-m-rt/memory.x
index 3d97414..6268ea6 100644
--- a/cortex-m-rt/memory.x
+++ b/cortex-m-rt/memory.x
@@ -1,8 +1,12 @@
/* Device specific memory layout */
+/* This file is used to build the cortex-m-rt examples,
+ but not other applications using cortex-m-rt. */
+
MEMORY
{
/* FLASH and RAM are mandatory memory regions */
+ /* Update examples/data_overflow.rs if you change these sizes. */
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
RAM : ORIGIN = 0x20000000, LENGTH = 20K