Skip to content

Commit

Permalink
Rust: Add add_library for ELF binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Dec 9, 2024
1 parent a11b1b7 commit 9448caf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
6 changes: 5 additions & 1 deletion api/rust/cargo/lief/src/elf/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use lief_ffi as ffi;
use crate::Error;
use super::builder::Config;
use super::hash::{Sysv, Gnu};
use super::dynamic::{self, DynamicEntries};
use super::dynamic::{self, DynamicEntries, Library};
use super::header::Header;
use super::section::{Sections, Section};
use super::segment::Segments;
Expand Down Expand Up @@ -300,6 +300,10 @@ 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());
}

pub fn add_library<'a>(&'a mut self, library: &str) -> Library<'a> {
Library::from_ffi(self.ptr.as_mut().unwrap().add_library(library))
}
}

impl generic::Binary for Binary {
Expand Down
1 change: 1 addition & 0 deletions api/rust/cargo/lief/tests/elf_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ fn test_api() {
fn test_mut_api() {
let path = utils::get_elf_sample("elf_reader.mips.elf").unwrap();
let Binary::ELF(mut bin) = Binary::parse(path.to_str().unwrap()).unwrap() else { panic!("Expecting an ELF"); };
bin.add_library("this_is_a_test.so");
let tmpfile = tempfile::NamedTempFile::new().unwrap();
bin.write(tmpfile.path());

Expand Down
4 changes: 4 additions & 0 deletions api/rust/include/LIEF/rust/ELF/Binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ class ELF_Binary : public AbstractBinary {
return impl().is_targeting_android();
}

auto add_library(std::string library) {
return std::make_unique<ELF_DynamicEntryLibrary>(impl().add_library(library));
}

void write(std::string output) { impl().write(output); }
void write_with_config(std::string output, ELF_Binary_write_config_t config) {
impl().write(output, LIEF::ELF::Builder::config_t {
Expand Down
2 changes: 1 addition & 1 deletion api/rust/include/LIEF/rust/ELF/DynamicEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "LIEF/rust/Mirror.hpp"
#include "LIEF/rust/helpers.hpp"

class ELF_DynamicEntry : public Mirror<LIEF::ELF::DynamicEntry>{
class ELF_DynamicEntry : public Mirror<LIEF::ELF::DynamicEntry> {
friend class ELF_DynamicEntryRpath;
friend class ELF_DynamicEntryArray;
friend class ELF_DynamicEntryFlags;
Expand Down
17 changes: 17 additions & 0 deletions doc/sphinx/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@

:Rust:

* Mutable API are progressively introduced:

- ELF:

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

- PE:

- :rust:method:`lief::pe::Binary::write [struct]`

- MachO:

- :rust:method:`lief::macho::Binary::write [struct]`
- :rust:method:`lief::macho::Binary::write_with_config [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.
Before, they require at least Debian 11 & Ubuntu 20.04
Expand Down

0 comments on commit 9448caf

Please sign in to comment.