diff --git a/src/ld_so/debug.rs b/src/ld_so/debug.rs index d48e827a07..0c569fac04 100644 --- a/src/ld_so/debug.rs +++ b/src/ld_so/debug.rs @@ -63,6 +63,15 @@ impl RTLDDebug { } } +/// SAFETY: safe as long as caller wraps the instance in a mutex, +/// or similar structure that guarantees exclusive mutable access. +/// Separate instances must not contain pointers to the same LinkMap instance. +unsafe impl Send for RTLDDebug {} +/// SAFETY: safe as long as caller wraps the instance in a mutex, +/// or similar structure that guarantees exclusive mutable access. +/// Separate instances must not contain pointers to the same LinkMap instance. +unsafe impl Sync for RTLDDebug {} + #[repr(C)] struct LinkMap { /* These members are part of the protocol with the debugger. @@ -126,4 +135,4 @@ impl LinkMap { pub extern "C" fn _dl_debug_state() {} #[no_mangle] -pub static mut _r_debug: RTLDDebug = RTLDDebug::NEW; +pub static _r_debug: spin::Mutex = spin::Mutex::new(RTLDDebug::NEW); diff --git a/src/ld_so/dso.rs b/src/ld_so/dso.rs index 0cfa202b3d..b1639de7ab 100644 --- a/src/ld_so/dso.rs +++ b/src/ld_so/dso.rs @@ -505,7 +505,9 @@ impl DSO { } else { bounds.1 - bounds.0 }; - _r_debug.insert_first(addr, path, addr + l_ld as usize); + _r_debug + .lock() + .insert_first(addr, path, addr + l_ld as usize); slice::from_raw_parts_mut(addr as *mut u8, size) } else { let (start, end) = bounds; @@ -535,7 +537,9 @@ impl DSO { } trace!(" = {:p}", ptr); ptr::write_bytes(ptr as *mut u8, 0, size); - _r_debug.insert(ptr as usize, path, ptr as usize + l_ld as usize); + _r_debug + .lock() + .insert(ptr as usize, path, ptr as usize + l_ld as usize); slice::from_raw_parts_mut(ptr as *mut u8, size) } }; diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index b60a188c52..6ea71c3dfd 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -593,7 +593,7 @@ impl Linker { Resolve::Now }; - unsafe { _r_debug.state = RTLDState::RT_ADD }; + _r_debug.lock().state = RTLDState::RT_ADD; _dl_debug_state(); let mut new_objects = Vec::new(); @@ -724,7 +724,7 @@ impl Linker { self.register_object(obj); } - unsafe { _r_debug.state = RTLDState::RT_CONSISTENT }; + _r_debug.lock().state = RTLDState::RT_CONSISTENT; _dl_debug_state(); Ok(loaded_dso) diff --git a/src/ld_so/start.rs b/src/ld_so/start.rs index 26e1a0f3a3..e5d3c41411 100644 --- a/src/ld_so/start.rs +++ b/src/ld_so/start.rs @@ -208,9 +208,7 @@ pub unsafe extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: us }; // we might need global lock for this kind of stuff - unsafe { - _r_debug.r_ldbase = ld_entry; - } + _r_debug.lock().r_ldbase = ld_entry; // TODO: Fix memory leak, although minimal. unsafe {