Skip to content

Commit 6b2fd0e

Browse files
authored
Merge pull request #118 from rust-embedded/link-arg
Move link-arg setting from .cargo/config.toml to build.rs
2 parents 627ac5c + a2a454a commit 6b2fd0e

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

.cargo/config.toml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,24 @@
1010
# runner = "gdb -q -x openocd.gdb"
1111

1212
rustflags = [
13-
# This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x
14-
# See https://github.com/rust-embedded/cortex-m-quickstart/pull/95
15-
"-C", "link-arg=--nmagic",
13+
# Previously, the linker arguments --nmagic and -Tlink.x were set here.
14+
# They are now set by build.rs instead. The linker argument can still
15+
# only be set here, if a custom linker is needed.
1616

17-
# LLD (shipped with the Rust toolchain) is used as the default linker
18-
"-C", "link-arg=-Tlink.x",
19-
20-
# if you run into problems with LLD switch to the GNU linker by commenting out
21-
# this line
17+
# By default, the LLD linker is used, which is shipped with the Rust
18+
# toolchain. If you run into problems with LLD, you can switch to the
19+
# GNU linker by uncommenting this line:
2220
# "-C", "linker=arm-none-eabi-ld",
2321

24-
# if you need to link to pre-compiled C libraries provided by a C toolchain
25-
# use GCC as the linker by commenting out both lines above and then
26-
# uncommenting the three lines below
22+
# If you need to link to pre-compiled C libraries provided by a C toolchain
23+
# use GCC as the linker by uncommenting the three lines below:
2724
# "-C", "linker=arm-none-eabi-gcc",
2825
# "-C", "link-arg=-Wl,-Tlink.x",
2926
# "-C", "link-arg=-nostartfiles",
3027
]
3128

3229
[build]
33-
# Pick ONE of these compilation targets
30+
# Pick ONE of these default compilation targets
3431
# target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
3532
target = "thumbv7m-none-eabi" # Cortex-M3
3633
# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)

build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//! Cargo re-run the build script whenever `memory.x` is changed,
88
//! updating `memory.x` ensures a rebuild of the application with the
99
//! new memory settings.
10+
//!
11+
//! The build script also sets the linker flags to tell it which link script to use.
1012
1113
use std::env;
1214
use std::fs::File;
@@ -28,4 +30,14 @@ fn main() {
2830
// here, we ensure the build script is only re-run when
2931
// `memory.x` is changed.
3032
println!("cargo:rerun-if-changed=memory.x");
33+
34+
// Specify linker arguments.
35+
36+
// `--nmagic` is required if memory section addresses are not aligned to 0x10000,
37+
// for example the FLASH and RAM sections in your `memory.x`.
38+
// See https://github.com/rust-embedded/cortex-m-quickstart/pull/95
39+
println!("cargo:rustc-link-arg=--nmagic");
40+
41+
// Set the linker script to the one provided by cortex-m-rt.
42+
println!("cargo:rustc-link-arg=-Tlink.x");
3143
}

0 commit comments

Comments
 (0)