-
-
Notifications
You must be signed in to change notification settings - Fork 333
Replace patchelf
with arwen
library for ELF manipulation
#2675
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
base: main
Are you sure you want to change the base?
Conversation
patchelf
with arwen
library for ELF manipulation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please also refactor patchelf.rs
to avoid parsing elf file multiple times in src/build_context.rs
.
Refactored patchelf.rs with efficient helper functions ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's still parsing elf multiple times in each patchelf functions, feel free to refactor it to a struct that holds a parsed elf container and add methods to do these operations. We choose to add a struct that wraps ElfContainer
to allow future extension to also patch macho & PE with a consistent API.
Created ElfPatcher struct that wraps ElfContainer to avoid parsing ELF files multiple times. The struct provides chainable methods for all patchelf operations while maintaining backward compatibility with the existing function API. This design allows for future extension to MACH-O and PE files with a consistent API. Commit: b53aa1a |
src/auditwheel/patchelf.rs
Outdated
cmd.arg("--replace-needed").arg(old).arg(new); | ||
impl ElfPatcher { | ||
/// Create a new ElfPatcher by reading and parsing the ELF file | ||
pub fn new(file: impl AsRef<Path>) -> Result<Self> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change this to pass in the file_data instead of file path, so that we can hold an ElfContainer
without using self-referential struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed ElfPatcher::new() to accept file_data instead of file_path and updated all callers in build_context.rs to read file data first. The struct now holds data internally and creates ElfContainer instances in each method. This approach avoids the lifetime/self-referential struct issue while maintaining the chainable API. Commit: c20ab76
@copilot rebase this branch on to main. |
8bd4ccf
to
d3a1d64
Compare
Co-authored-by: messense <[email protected]>
…lf, optimize ELF parsing Co-authored-by: messense <[email protected]>
…sing Co-authored-by: messense <[email protected]>
…ctly in build_context Co-authored-by: messense <[email protected]>
d3a1d64
to
22642db
Compare
@copilot get rid of |
…ple ELF parsing Co-authored-by: messense <[email protected]>
Removed ElfPatcher struct entirely and refactored build_context.rs to use ElfContainer directly. Each operation now reads the file once, parses the ELF data once, applies changes, and writes once - eliminating redundant ELF parsing. Commit: eb57358 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot try refactor build_context.rs to cache parsed ElfContainer
so it does not need to be reparsed in different steps.
…les multiple times Co-authored-by: messense <[email protected]>
Refactored build_context.rs to cache file data and avoid parsing ELF files multiple times. The implementation uses a HashMap to cache file contents within the add_external_libs function, eliminating redundant disk I/O while ELF parsing still occurs as needed due to lifetime constraints. This provides significant performance improvement for auditwheel operations. Commit: def9b5b |
Fixes #2516
This PR replaces the external
patchelf
tool dependency with the native Rustarwen
library for ELF file manipulation operations.Changes Made
Core Implementation
patchelf
subprocess invocations insrc/auditwheel/patchelf.rs
have been replaced with direct calls to thearwen::elf::ElfContainer
APIarwen = "0.0.2"
toCargo.toml
Functions Updated
verify_patchelf()
- Now always succeeds since arwen is a library dependencyreplace_needed()
- Usesarwen::elf::ElfContainer::replace_needed()
set_soname()
- Usesarwen::elf::ElfContainer::set_soname()
remove_rpath()
- Usesarwen::elf::ElfContainer::remove_runpath()
set_rpath()
- Usesarwen::elf::ElfContainer::set_runpath()
get_rpath()
- Unchanged (already used goblin directly)Documentation Updates
patchelf
from optional dependencies inpyproject.toml
Benefits
✅ No external tool dependency: Users no longer need to install patchelf separately
✅ Simplified installation:
pip install maturin
now works without additional system packages✅ Better error handling: Native Rust error handling instead of parsing subprocess stderr
✅ Cross-platform consistency: Same implementation across all platforms
✅ Performance: Direct library calls instead of subprocess overhead
Before vs After
Before:
After:
# Just works out of the box pip install maturin
Testing
cargo clippy
andcargo fmt
passThe change maintains full backward compatibility while eliminating an external dependency, making maturin easier to install and more reliable across different environments.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.