Skip to content

Commit 8bffd88

Browse files
committed
add helper function to declare an extern static for a weak symbol
1 parent a551b4d commit 8bffd88

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

src/tools/miri/src/shims/extern_static.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
2929
Ok(())
3030
}
3131

32+
/// Extern statics that are initialized with function pointers to the symbols of the same name.
33+
fn weak_symbol_extern_statics(
34+
this: &mut MiriInterpCx<'mir, 'tcx>,
35+
names: &[&str],
36+
) -> InterpResult<'tcx> {
37+
for name in names {
38+
assert!(this.is_dyn_sym(name), "{name} is not a dynamic symbol");
39+
let layout = this.machine.layouts.const_raw_ptr;
40+
let ptr = this.fn_ptr(FnVal::Other(DynSym::from_str(name)));
41+
let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, this), layout);
42+
Self::alloc_extern_static(this, name, val)?;
43+
}
44+
Ok(())
45+
}
46+
3247
/// Sets up the "extern statics" for this machine.
3348
pub fn init_extern_statics(this: &mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx> {
3449
// "__rust_no_alloc_shim_is_unstable"
@@ -58,12 +73,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
5873
}
5974
"android" => {
6075
Self::null_ptr_extern_statics(this, &["bsd_signal"])?;
61-
// "signal" -- just needs a non-zero pointer value (function does not even get called),
62-
// but we arrange for this to call the `signal` function anyway.
63-
let layout = this.machine.layouts.const_raw_ptr;
64-
let ptr = this.fn_ptr(FnVal::Other(DynSym::from_str("signal")));
65-
let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, this), layout);
66-
Self::alloc_extern_static(this, "signal", val)?;
76+
Self::weak_symbol_extern_statics(this, &["signal"])?;
6777
}
6878
"windows" => {
6979
// "_tls_used"

src/tools/miri/src/shims/foreign_items.rs

+9
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
151151
Ok(None)
152152
}
153153

154+
fn is_dyn_sym(&self, name: &str) -> bool {
155+
let this = self.eval_context_ref();
156+
match this.tcx.sess.target.os.as_ref() {
157+
os if this.target_os_is_unix() => shims::unix::foreign_items::is_dyn_sym(name, os),
158+
"windows" => shims::windows::foreign_items::is_dyn_sym(name),
159+
_ => false,
160+
}
161+
}
162+
154163
/// Emulates a call to a `DynSym`.
155164
fn emulate_dyn_sym(
156165
&mut self,

src/tools/miri/src/shims/unix/foreign_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use shims::unix::freebsd::foreign_items as freebsd;
1515
use shims::unix::linux::foreign_items as linux;
1616
use shims::unix::macos::foreign_items as macos;
1717

18-
fn is_dyn_sym(name: &str, target_os: &str) -> bool {
18+
pub fn is_dyn_sym(name: &str, target_os: &str) -> bool {
1919
match name {
2020
// Used for tests.
2121
"isatty" => true,

src/tools/miri/src/shims/unix/linux/foreign_items.rs

-2
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
113113
// have the right type.
114114

115115
let sys_getrandom = this.eval_libc("SYS_getrandom").to_target_usize(this)?;
116-
117116
let sys_statx = this.eval_libc("SYS_statx").to_target_usize(this)?;
118-
119117
let sys_futex = this.eval_libc("SYS_futex").to_target_usize(this)?;
120118

121119
if args.is_empty() {

src/tools/miri/src/shims/windows/foreign_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::*;
1515
use shims::foreign_items::EmulateForeignItemResult;
1616
use shims::windows::handle::{Handle, PseudoHandle};
1717

18-
fn is_dyn_sym(name: &str) -> bool {
18+
pub fn is_dyn_sym(name: &str) -> bool {
1919
// std does dynamic detection for these symbols
2020
matches!(
2121
name,

0 commit comments

Comments
 (0)