Skip to content

Commit 3d0c847

Browse files
committed
Auto merge of #74941 - dylanmckay:replace-broken-avr-unknown-unknown-target, r=oli-obk
[AVR] Replace broken 'avr-unknown-unknown' target with 'avr-unknown-gnu-atmega328' target The `avr-unknown-unknown` target has never worked correctly, always trying to invoke the host linker and failing. It aimed to be a mirror of AVR-GCC's default handling of the `avr-unknown-unknown' triple (assume bare minimum chip features, silently skip linking runtime libraries, etc). This behaviour is broken-by-default as it will cause a miscompiled executable when flashed. This patch improves the AVR builtin target specifications to instead expose only a 'avr-unknown-gnu-atmega328' target. This target system is `gnu`, as it uses the AVR-GCC frontend along with avr-binutils. The target triple ABI is 'atmega328'. In the future, it should be possible to replace the dependency on AVR-GCC and binutils by using the in-progress AVR LLD and compiler-rt support. Perhaps at that point it would make sense to add an 'avr-unknown-unknown-atmega328' target as a better default when implemented. There is no current intention to add in-tree AVR target specifications for other AVR microcontrollers - this one can serve as a reference implementation for other devices via `rustc --print target-spec-json avr-unknown-gnu-atmega328p`. There should be no users of the existing 'avr-unknown-unknown' Rust target as a custom target specification JSON has always been recommended, and the avr-unknown-unknown target could never pass the linking step anyway.
2 parents 118860a + c9ead8c commit 3d0c847

File tree

9 files changed

+62
-54
lines changed

9 files changed

+62
-54
lines changed

library/panic_unwind/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ cfg_if::cfg_if! {
6565
// - os=none ("bare metal" targets)
6666
// - os=uefi
6767
// - nvptx64-nvidia-cuda
68-
// - avr-unknown-unknown
68+
// - arch=avr
6969
#[path = "dummy.rs"]
7070
mod real_imp;
7171
}

library/std/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn main() {
8383
// - os=none ("bare metal" targets)
8484
// - mipsel-sony-psp
8585
// - nvptx64-nvidia-cuda
86-
// - avr-unknown-unknown
86+
// - arch=avr
8787
// - tvos (aarch64-apple-tvos, x86_64-apple-tvos)
8888
// - uefi (x86_64-unknown-uefi, i686-unknown-uefi)
8989
// - JSON targets

src/doc/rustc/src/platform-support.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ target | std | host | notes
165165
`armv7-wrs-vxworks-eabihf` | ? | |
166166
`armv7a-none-eabihf` | * | | ARM Cortex-A, hardfloat
167167
`armv7s-apple-ios` | ✓[^apple] | |
168-
`avr-unknown-unknown` | ? | | AVR
168+
`avr-unknown-gnu-atmega328` | | | AVR. Requires `-Z build-std=core`
169169
`hexagon-unknown-linux-musl` | ? | |
170170
`i386-apple-ios` | ✓[^apple] | | 32-bit x86 iOS
171171
`i686-apple-darwin` | ✓ | ✓ | 32-bit OSX (10.7+, Lion+)
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
2+
3+
/// A base target for AVR devices using the GNU toolchain.
4+
///
5+
/// Requires GNU avr-gcc and avr-binutils on the host system.
6+
pub fn target(target_cpu: String) -> TargetResult {
7+
Ok(Target {
8+
arch: "avr".to_string(),
9+
data_layout: "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8".to_string(),
10+
llvm_target: "avr-unknown-unknown".to_string(),
11+
target_endian: "little".to_string(),
12+
target_pointer_width: "16".to_string(),
13+
linker_flavor: LinkerFlavor::Gcc,
14+
target_os: "unknown".to_string(),
15+
target_env: "".to_string(),
16+
target_vendor: "unknown".to_string(),
17+
target_c_int_width: 16.to_string(),
18+
options: TargetOptions {
19+
cpu: target_cpu.clone(),
20+
exe_suffix: ".elf".to_string(),
21+
22+
linker: Some("avr-gcc".to_owned()),
23+
dynamic_linking: false,
24+
executables: true,
25+
linker_is_gnu: true,
26+
has_rpath: false,
27+
position_independent_executables: false,
28+
eh_frame_header: false,
29+
pre_link_args: vec![(
30+
LinkerFlavor::Gcc,
31+
vec![
32+
format!("-mmcu={}", target_cpu),
33+
// We want to be able to strip as much executable code as possible
34+
// from the linker command line, and this flag indicates to the
35+
// linker that it can avoid linking in dynamic libraries that don't
36+
// actually satisfy any symbols up to that point (as with many other
37+
// resolutions the linker does). This option only applies to all
38+
// following libraries so we're sure to pass it as one of the first
39+
// arguments.
40+
"-Wl,--as-needed".to_string(),
41+
],
42+
)]
43+
.into_iter()
44+
.collect(),
45+
late_link_args: vec![(LinkerFlavor::Gcc, vec!["-lgcc".to_owned()])]
46+
.into_iter()
47+
.collect(),
48+
..TargetOptions::default()
49+
},
50+
})
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use crate::spec::TargetResult;
2+
3+
pub fn target() -> TargetResult {
4+
super::avr_gnu_base::target("atmega328".to_owned())
5+
}

src/librustc_target/spec/avr_unknown_unknown.rs

-17
This file was deleted.

src/librustc_target/spec/freestanding_base.rs

-31
This file was deleted.

src/librustc_target/spec/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ mod android_base;
5151
mod apple_base;
5252
mod apple_sdk_base;
5353
mod arm_base;
54+
mod avr_gnu_base;
5455
mod cloudabi_base;
5556
mod dragonfly_base;
5657
mod freebsd_base;
57-
mod freestanding_base;
5858
mod fuchsia_base;
5959
mod haiku_base;
6060
mod hermit_base;
@@ -581,7 +581,7 @@ supported_targets! {
581581
("aarch64-fuchsia", aarch64_fuchsia),
582582
("x86_64-fuchsia", x86_64_fuchsia),
583583

584-
("avr-unknown-unknown", avr_unknown_unknown),
584+
("avr-unknown-gnu-atmega328", avr_unknown_gnu_atmega328),
585585

586586
("x86_64-unknown-l4re-uclibc", x86_64_unknown_l4re_uclibc),
587587

src/test/codegen/avr/avr-func-addrspace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -O --target=avr-unknown-unknown --crate-type=rlib
1+
// compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib
22
// needs-llvm-components: avr
33

44
// This test validates that function pointers can be stored in global variables

0 commit comments

Comments
 (0)