1//! Provides the `extern static` that this platform expects.
23use crate::*;
45impl<'tcx> MiriMachine<'tcx> {
6fn alloc_extern_static(
7 ecx: &mut MiriInterpCx<'tcx>,
8 name: &str,
9 val: ImmTy<'tcx>,
10 ) -> InterpResult<'tcx> {
11let place = ecx.allocate(val.layout, MiriMemoryKind::ExternStatic.into())?;
12 ecx.write_immediate(*val, &place)?;
13Self::add_extern_static(ecx, name, place.ptr());
14 interp_ok(())
15 }
1617/// Zero-initialized pointer-sized extern statics are pretty common.
18 /// Most of them are for weak symbols, which we all set to null (indicating that the
19 /// symbol is not supported, and triggering fallback code which ends up calling
20 /// some other shim that we do support).
21fn null_ptr_extern_statics(ecx: &mut MiriInterpCx<'tcx>, names: &[&str]) -> InterpResult<'tcx> {
22for name in names {
23let val = ImmTy::from_int(0, ecx.machine.layouts.usize);
24Self::alloc_extern_static(ecx, name, val)?;
25 }
26 interp_ok(())
27 }
2829/// Extern statics that are initialized with function pointers to the symbols of the same name.
30fn weak_symbol_extern_statics(
31 ecx: &mut MiriInterpCx<'tcx>,
32 names: &[&str],
33 ) -> InterpResult<'tcx> {
34for name in names {
35assert!(ecx.is_dyn_sym(name), "{name} is not a dynamic symbol");
36let layout = ecx.machine.layouts.const_raw_ptr;
37let ptr = ecx.fn_ptr(FnVal::Other(DynSym::from_str(name)));
38let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, ecx), layout);
39Self::alloc_extern_static(ecx, name, val)?;
40 }
41 interp_ok(())
42 }
4344/// Sets up the "extern statics" for this machine.
45pub fn init_extern_statics(ecx: &mut MiriInterpCx<'tcx>) -> InterpResult<'tcx> {
46// "__rust_no_alloc_shim_is_unstable"
47let val = ImmTy::from_int(0, ecx.machine.layouts.u8); // always 0, value does not matter
48Self::alloc_extern_static(ecx, "__rust_no_alloc_shim_is_unstable", val)?;
4950// "__rust_alloc_error_handler_should_panic"
51let val = ecx.tcx.sess.opts.unstable_opts.oom.should_panic();
52let val = ImmTy::from_int(val, ecx.machine.layouts.u8);
53Self::alloc_extern_static(ecx, "__rust_alloc_error_handler_should_panic", val)?;
5455if ecx.target_os_is_unix() {
56// "environ" is mandated by POSIX.
57let environ = ecx.machine.env_vars.unix().environ();
58Self::add_extern_static(ecx, "environ", environ);
59 }
6061match ecx.tcx.sess.target.os.as_ref() {
62"linux" => {
63Self::null_ptr_extern_statics(
64 ecx,
65&["__cxa_thread_atexit_impl", "__clock_gettime64"],
66 )?;
67Self::weak_symbol_extern_statics(ecx, &["getrandom", "statx"])?;
68 }
69"freebsd" => {
70Self::null_ptr_extern_statics(ecx, &["__cxa_thread_atexit_impl"])?;
71 }
72"android" => {
73Self::null_ptr_extern_statics(ecx, &["bsd_signal"])?;
74Self::weak_symbol_extern_statics(ecx, &["signal", "getrandom"])?;
75 }
76"windows" => {
77// "_tls_used"
78 // This is some obscure hack that is part of the Windows TLS story. It's a `u8`.
79let val = ImmTy::from_int(0, ecx.machine.layouts.u8);
80Self::alloc_extern_static(ecx, "_tls_used", val)?;
81 }
82"illumos" | "solaris" => {
83Self::weak_symbol_extern_statics(ecx, &["pthread_setname_np"])?;
84 }
85_ => {} // No "extern statics" supported on this target
86}
87 interp_ok(())
88 }
89}