v0.3: ELF symbol parsing, sym()/addr()/symbols() globals, addrstr/here prelude helpers

This commit is contained in:
claude
2026-05-27 21:13:10 +00:00
parent 19c6154831
commit d587dbcd4c
7 changed files with 304 additions and 5 deletions
+47
View File
@@ -318,6 +318,53 @@ fn install_globals<'js>(
globals.set("prev", prev_obj)?;
}
// ---------- sym(addr) -> "name+0xN" | null ----------
{
let target = target.clone();
let f = Function::new(c.clone(), move |c: Ctx<'js>, addr: Value<'js>| -> rquickjs::Result<Value<'js>> {
let addr = coerce_addr(&c, &addr)?;
let slot = target.borrow();
let t = slot.as_ref().ok_or_else(|| throw(&c, "no target attached"))?;
let s = t.symbols().ok_or_else(|| throw(&c, "no symbol table"))?;
match s.name_for_addr(addr) {
Some(name) => Ok(rquickjs::String::from_str(c.clone(), &name)?.into_value()),
None => Ok(Value::new_null(c.clone())),
}
})?;
globals.set("sym", f)?;
}
// ---------- addr(name) -> BigInt | null ----------
{
let target = target.clone();
let f = Function::new(c.clone(), move |c: Ctx<'js>, name: String| -> rquickjs::Result<Value<'js>> {
let slot = target.borrow();
let t = slot.as_ref().ok_or_else(|| throw(&c, "no target attached"))?;
let s = t.symbols().ok_or_else(|| throw(&c, "no symbol table"))?;
match s.addr_for_name(&name) {
Some(a) => u64_to_bigint(&c, a),
None => Ok(Value::new_null(c.clone())),
}
})?;
globals.set("addr", f)?;
}
// ---------- symbols() -> { count, load_base, path } ----------
{
let target = target.clone();
let f = Function::new(c.clone(), move |c: Ctx<'js>| -> rquickjs::Result<Value<'js>> {
let slot = target.borrow();
let t = slot.as_ref().ok_or_else(|| throw(&c, "no target attached"))?;
let s = t.symbols().ok_or_else(|| throw(&c, "no symbol table"))?;
let obj = Object::new(c.clone())?;
obj.set("count", s.len() as u32)?;
obj.set("load_base", u64_to_bigint(&c, s.load_base)?)?;
obj.set("path", s.path.to_string_lossy().into_owned())?;
Ok(obj.into_value())
})?;
globals.set("symbols", f)?;
}
// ---------- JS-side prelude ---------------------------------------------
// Loaded last, so it can reference any of the native globals above.
// Lives in src/prelude.js, embedded at compile time.