Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exported symbols invisible to dlsym on x86_64-apple-darwin #29

Closed
littledivy opened this issue Sep 30, 2024 · 0 comments
Closed

Exported symbols invisible to dlsym on x86_64-apple-darwin #29

littledivy opened this issue Sep 30, 2024 · 0 comments
Labels
bug Something isn't working mach-o

Comments

@littledivy
Copy link
Member

littledivy commented Sep 30, 2024

Root cause of denoland/deno#25879 and denoland/deno#25940. Only reproducible on x86_64-apple-darwin

extern "C" {
    fn dlsym(handle: isize, symbol: *const u8) -> usize;
    fn dlerror() -> *const u8;
}

#[no_mangle]
pub extern "C" fn exported_sym() {
    println!("Hello from exported_sym");
}

const RTLD_DEFAULT: isize = -2;

fn main() {
    if unsafe { dlsym(RTLD_DEFAULT, "exported_sym\0".as_ptr()) } == 0 {
        let err = unsafe { dlerror() };
        let err = unsafe { std::ffi::CStr::from_ptr(err as _) };
        let err = err.to_str().unwrap();
        eprintln!("Error: {}", err);
        panic!("Symbol not found");
    }
}
$ rustc --target x86_64-apple-darwin -Clink-arg="-Wl,-exported_symbol,_exported_sym" rtld_default.rs
$ ./rtld_default # Works

$ sui ./rtld_default test.txt ./patched
$ ./patched
Error: dlsym(RTLD_DEFAULT, exported_sym): symbol not found
thread 'main' panicked at rtld_default.rs:19:9:
Symbol not found
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

$ file ./patched
./patched: Mach-O 64-bit executable x86_64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mach-o
Projects
None yet
Development

No branches or pull requests

1 participant