ld_so: use mutex for _r_debug

Uses a `spin::Mutex` for the global static `_r_debug` instance to remove
warning about a global mutable static.

Guarantees exclusive mutable access to the `_r_debug` variable.
This commit is contained in:
elle
2025-09-07 20:47:32 +00:00
parent a434c0ef12
commit 5d77f617f5
4 changed files with 19 additions and 8 deletions
+10 -1
View File
@@ -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<RTLDDebug> = spin::Mutex::new(RTLDDebug::NEW);
+6 -2
View File
@@ -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)
}
};
+2 -2
View File
@@ -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)
+1 -3
View File
@@ -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 {