Skip to content

Commit f84c23c

Browse files
bors[bot]Anatol Ulrich
andauthored
Merge #357
357: fix issue #336 r=jonas-schievink a=spookyvision Introduce special cased symbol names to satisfy the macos linker Co-authored-by: Anatol Ulrich <anatol.ulrich@ferrous-systems.com>
2 parents a57f6c1 + d91fa12 commit f84c23c

4 files changed

Lines changed: 45 additions & 9 deletions

File tree

.github/workflows/ci.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,19 @@ jobs:
4444
- name: Check that crates build with unstable-test feature
4545
run: RUSTFLAGS='--deny warnings' cargo check --all --features unstable-test
4646
shell: bash
47-
- name: Run tests on Ubuntu/Windows
48-
if: matrix.os != 'macOS-latest'
47+
- name: Run tests on Ubuntu
48+
if: matrix.os == 'linux-latest'
4949
run: cargo test --workspace --features unstable-test
5050
- name: Run tests on macOS
5151
# NOTE defmt does not build for macOS because its `cortex-m-rt` dependency doesn't
5252
# (see https://github.com/rust-embedded/cortex-m-rt/issues/74), so we cannot use
5353
# `cargo test --workspace` and have to build the test suites individually instead
5454
if: matrix.os == 'macOS-latest'
5555
run: cargo test -p defmt -p defmt-decoder -p defmt-parser -p defmt-macros -p defmt-logger -p defmt-print --all-features
56-
56+
- name: Run tests on Windows
57+
# NOTE there might be a linker bug on the CI's current Windows installation, still investigating
58+
if: matrix.os == 'windows-latest'
59+
run: cargo test -p defmt -p defmt-decoder -p defmt-parser -p defmt-macros -p defmt-logger -p defmt-print --all-features
5760
no-std:
5861
runs-on: ubuntu-latest
5962
steps:

firmware/defmt-rtt/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ unsafe fn handle() -> &'static Channel {
217217
},
218218
};
219219

220-
#[link_section = ".uninit.defmt-rtt.BUFFER"]
220+
#[cfg_attr(target_os = "macos", link_section = ".uninit,defmt-rtt.BUFFER")]
221+
#[cfg_attr(not(target_os = "macos"), link_section = ".uninit.defmt-rtt.BUFFER")]
221222
static mut BUFFER: [u8; SIZE] = [0; SIZE];
222223

223224
static NAME: &[u8] = b"defmt\0";

macros/src/lib.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ use syn::{
1818
WhereClause, WherePredicate,
1919
};
2020

21+
use std::collections::hash_map::DefaultHasher;
22+
use std::hash::{Hash, Hasher};
23+
2124
/// Checks if any attribute in `attrs_to_check` is in `reject_list` and returns a compiler error if there's a match
2225
///
2326
/// The compiler error will indicate that the attribute conflicts with `attr_name`
@@ -194,7 +197,8 @@ pub fn timestamp(ts: TokenStream) -> TokenStream {
194197
// Unique symbol name to prevent multiple `timestamp!` invocations in the crate graph.
195198
// Uses `#symname` to ensure it is not discarded by the linker.
196199
#[no_mangle]
197-
#[link_section = ".defmt.end.timestamp"]
200+
#[cfg_attr(target_os = "macos", link_section = ".defmt,end.timestamp")]
201+
#[cfg_attr(not(target_os = "macos"), link_section = ".defmt.end.timestamp")]
198202
static __DEFMT_MARKER_TIMESTAMP_WAS_DEFINED: &u8 = &#symname;
199203
};
200204
)
@@ -984,13 +988,16 @@ pub fn internp(ts: TokenStream) -> TokenStream {
984988
let ls = lit.value();
985989

986990
let sym = symbol::Symbol::new("prim", &ls).mangle();
987-
let section = format!(".defmt.prim.{}", sym);
991+
992+
let section = mksection(false, "prim.", &sym);
993+
let section_macos = mksection(true, "prim.", &sym);
988994

989995
if cfg!(feature = "unstable-test") {
990996
quote!({ defmt::export::fetch_add_string_index() as u8 })
991997
} else {
992998
quote!({
993-
#[link_section = #section]
999+
#[cfg_attr(target_os = "macos", link_section = #section_macos)]
1000+
#[cfg_attr(not(target_os = "macos"), link_section = #section)]
9941001
#[export_name = #sym]
9951002
static S: u8 = 0;
9961003
&S as *const u8 as u8
@@ -1039,12 +1046,30 @@ pub fn write(ts: TokenStream) -> TokenStream {
10391046
.into()
10401047
}
10411048

1049+
/// work around restrictions on length and allowed characters imposed by macos linker
1050+
/// returns (note the comma character for macos):
1051+
/// under macos: ".defmt," + 16 character hex digest of symbol's hash
1052+
/// otherwise: ".defmt." + prefix + symbol
1053+
fn mksection(macos: bool, prefix: &str, symbol: &str) -> String {
1054+
let mut sub_section = format!(".{}{}", prefix, symbol);
1055+
1056+
if macos {
1057+
let mut hasher = DefaultHasher::new();
1058+
sub_section.hash(&mut hasher);
1059+
sub_section = format!(",{:x}", hasher.finish());
1060+
}
1061+
1062+
format!(".defmt{}", sub_section)
1063+
}
1064+
10421065
fn mkstatic(varname: Ident2, string: &str, tag: &str) -> TokenStream2 {
10431066
let sym = symbol::Symbol::new(tag, string).mangle();
1044-
let section = format!(".defmt.{}", sym);
1067+
let section = mksection(false, "", &sym);
1068+
let section_macos = mksection(true, "", &sym);
10451069

10461070
quote!(
1047-
#[link_section = #section]
1071+
#[cfg_attr(target_os = "macos", link_section = #section_macos)]
1072+
#[cfg_attr(not(target_os = "macos"), link_section = #section)]
10481073
#[export_name = #sym]
10491074
static #varname: u8 = 0;
10501075
)

tests/ui.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@ fn ui() {
77
{
88
let t = trybuild::TestCases::new();
99
t.compile_fail("tests/ui/*.rs");
10+
11+
// TODO once the corresponding fix in cortex-m-rt has been released,
12+
// ( https://github.com/rust-embedded/cortex-m-rt/pull/306
13+
// https://github.com/rust-embedded/cortex-m-rt/pull/310 )
14+
// re-enable this test (deleted in commit d20ec32) and remove the macos special casing in `ci.yml`
15+
// also check out improved approach re: linker sections in the second cortex-m-rt PR
16+
// t.pass("tests/basic_usage.rs");
1017
}
1118
}

0 commit comments

Comments
 (0)