add helper function to declare an extern static for a weak symbol

This commit is contained in:
Ralf Jung 2024-05-04 14:26:11 +02:00
parent 49cd705189
commit 823e31d9fa
5 changed files with 27 additions and 10 deletions

View file

@ -29,6 +29,21 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
Ok(())
}
/// Extern statics that are initialized with function pointers to the symbols of the same name.
fn weak_symbol_extern_statics(
this: &mut MiriInterpCx<'mir, 'tcx>,
names: &[&str],
) -> InterpResult<'tcx> {
for name in names {
assert!(this.is_dyn_sym(name), "{name} is not a dynamic symbol");
let layout = this.machine.layouts.const_raw_ptr;
let ptr = this.fn_ptr(FnVal::Other(DynSym::from_str(name)));
let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, this), layout);
Self::alloc_extern_static(this, name, val)?;
}
Ok(())
}
/// Sets up the "extern statics" for this machine.
pub fn init_extern_statics(this: &mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx> {
// "__rust_no_alloc_shim_is_unstable"
@ -58,12 +73,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
}
"android" => {
Self::null_ptr_extern_statics(this, &["bsd_signal"])?;
// "signal" -- just needs a non-zero pointer value (function does not even get called),
// but we arrange for this to call the `signal` function anyway.
let layout = this.machine.layouts.const_raw_ptr;
let ptr = this.fn_ptr(FnVal::Other(DynSym::from_str("signal")));
let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, this), layout);
Self::alloc_extern_static(this, "signal", val)?;
Self::weak_symbol_extern_statics(this, &["signal"])?;
}
"windows" => {
// "_tls_used"

View file

@ -151,6 +151,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
Ok(None)
}
fn is_dyn_sym(&self, name: &str) -> bool {
let this = self.eval_context_ref();
match this.tcx.sess.target.os.as_ref() {
os if this.target_os_is_unix() => shims::unix::foreign_items::is_dyn_sym(name, os),
"windows" => shims::windows::foreign_items::is_dyn_sym(name),
_ => false,
}
}
/// Emulates a call to a `DynSym`.
fn emulate_dyn_sym(
&mut self,

View file

@ -15,7 +15,7 @@ use shims::unix::freebsd::foreign_items as freebsd;
use shims::unix::linux::foreign_items as linux;
use shims::unix::macos::foreign_items as macos;
fn is_dyn_sym(name: &str, target_os: &str) -> bool {
pub fn is_dyn_sym(name: &str, target_os: &str) -> bool {
match name {
// Used for tests.
"isatty" => true,

View file

@ -113,9 +113,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
// have the right type.
let sys_getrandom = this.eval_libc("SYS_getrandom").to_target_usize(this)?;
let sys_statx = this.eval_libc("SYS_statx").to_target_usize(this)?;
let sys_futex = this.eval_libc("SYS_futex").to_target_usize(this)?;
if args.is_empty() {

View file

@ -15,7 +15,7 @@ use crate::*;
use shims::foreign_items::EmulateForeignItemResult;
use shims::windows::handle::{Handle, PseudoHandle};
fn is_dyn_sym(name: &str) -> bool {
pub fn is_dyn_sym(name: &str) -> bool {
// std does dynamic detection for these symbols
matches!(
name,