Skip to content

Commit

Permalink
Add bindings to add a Mach-O library
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Dec 9, 2024
1 parent 9448caf commit 11c4d4d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 45 deletions.
1 change: 1 addition & 0 deletions api/rust/cargo/lief/src/elf/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ impl Binary {
self.ptr.as_mut().unwrap().write_with_config(output.to_str().unwrap(), config.to_ffi());
}

/// Add a library as dependency
pub fn add_library<'a>(&'a mut self, library: &str) -> Library<'a> {
Library::from_ffi(self.ptr.as_mut().unwrap().add_library(library))
}
Expand Down
7 changes: 6 additions & 1 deletion api/rust/cargo/lief/src/macho/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use super::commands::thread_command::ThreadCommand;
use super::commands::two_level_hints::TwoLevelHints;
use super::commands::uuid::UUID;
use super::commands::version_min::VersionMin;
use super::commands::CommandsIter;
use super::commands::{CommandsIter, Dylib};
use super::header::Header;
use super::relocation::Relocations;
use super::section::Sections;
Expand Down Expand Up @@ -318,6 +318,11 @@ impl Binary {
pub fn write_with_config(&mut self, output: &Path, config: Config) {
self.ptr.as_mut().unwrap().write_with_config(output.to_str().unwrap(), config.to_ffi());
}

/// Insert a new shared library through a `LC_LOAD_DYLIB` command
pub fn add_library<'a>(&'a mut self, libname: &str) -> Dylib<'a> {
Dylib::from_ffi(self.ptr.as_mut().unwrap().add_library(libname))
}
}

impl generic::Binary for Binary {
Expand Down
1 change: 1 addition & 0 deletions api/rust/cargo/lief/tests/macho_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ fn test_mut_api() {
let path = utils::get_macho_sample("FAT_MachO_x86_x86-64_library_libc++abi.dylib").unwrap();
let Binary::MachO(fat) = Binary::parse(path.to_str().unwrap()).unwrap() else { panic!("Expecting an ELF"); };
for mut bin in fat.iter() {
bin.add_library("this_is_a_dylib.dylib");
let tmpfile = tempfile::NamedTempFile::new().unwrap();
bin.write(tmpfile.path());
}
Expand Down
4 changes: 4 additions & 0 deletions api/rust/include/LIEF/rust/MachO/Binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ class MachO_Binary : public AbstractBinary {
});
}

auto add_library(std::string name) {
return details::try_unique<MachO_Dylib>(impl().add_library(name)->cast<LIEF::MachO::DylibCommand>());
}

static bool is_exported(const MachO_Symbol& symbol) {
return lief_t::is_exported(static_cast<const LIEF::MachO::Symbol&>(symbol.get()));
}
Expand Down
1 change: 1 addition & 0 deletions doc/sphinx/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@

- :rust:method:`lief::macho::Binary::write [struct]`
- :rust:method:`lief::macho::Binary::write_with_config [struct]`
- :rust:method:`lief::macho::Binary::add_library [struct]`

* Thanks to :github_user:`Huntragon` Rust bindings can be used without openssl (see: :pr:`1105`)
* Rust precompiled Linux packages are now supported for Debian 10 & Ubuntu 19.10.
Expand Down
Loading

0 comments on commit 11c4d4d

Please sign in to comment.