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

Question about rewriting a macho / elf file #116

Open
wolfv opened this issue Mar 28, 2023 · 3 comments
Open

Question about rewriting a macho / elf file #116

wolfv opened this issue Mar 28, 2023 · 3 comments

Comments

@wolfv
Copy link

wolfv commented Mar 28, 2023

Hi, I am currently browsing around in the Rust ecosystem to find the best solution to rewrite some RPaths' in Rust (ideally). I need to do that for a package manager in order to make shared libraries properly relocatable. Basically I would love to have the native Rust equivalent for install_name_tool and patchelf.

I was wondering if you think that faerie is the right crate for this or know of alternatives? I've looked at and used goblin a bit, too, but didn't find it easy to emit ELF / MachO files.

@philipc
Copy link
Collaborator

philipc commented Mar 29, 2023

I don't think faerie is the right crate. faerie is for emitting relocatable object files, not for modifying existing linked executables. You could build something using goblin, but I'm not sure how much work it would be.

There's also the object crate, which has an elfcopy example. That basically reconstructs the entire file, which is a perhaps a bit heavy handed for the type of modification you want. I'm not sure how easy it would be to adapt it to only change small amounts of the file.

@wolfv
Copy link
Author

wolfv commented Mar 29, 2023

Thanks a lot for your answer! I did have a look at the source code of install_name_tool and it also basically rewrites the entire file. I was thinking we could maybe just patch the commands in place (the new strings are generally shorter than the ones encoded in the binary) and fill up the rest with \0. I think goblin and scroll would do a great job for that.

My initial tests produced MachO files that were crashing otools so I stopped, and used install_name_tool in the meantime, but I might get back to it.

@wolfv
Copy link
Author

wolfv commented Mar 29, 2023

Also we found some code in apple_codesign that seems to emit a MachO file: https://github.com/indygreg/apple-platform-rs/blob/731463114f2ac85aeaf8b1603327ca11e7a435f2/apple-codesign/src/macho_signing.rs#L39 so we might also have a look there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants