Skip to content

Commit

Permalink
Add rust bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Apr 27, 2024
1 parent 6d1a230 commit d447511
Show file tree
Hide file tree
Showing 245 changed files with 19,628 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,6 @@ build/
*.whl
lief.egg-info/
dist/
api/rust/cargo/target/*
api/rust/examples/target/*
Cargo.lock
4 changes: 4 additions & 0 deletions .gitlab-ci.yml
@@ -0,0 +1,4 @@
include:
- project: 'LIEF/ci'
ref: main
file: 'main.yml'
41 changes: 21 additions & 20 deletions CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.24)
enable_language(C)
enable_language(CXX)

Expand Down Expand Up @@ -98,6 +98,25 @@ message(STATUS "CMAKE_LINK_LIBRARY_FILE_FLAG: ${CMAKE_LINK_LIBRARY_FILE_FLA
message(STATUS "CMAKE_LINK_INTERFACE_LIBRARIES: ${CMAKE_LINK_INTERFACE_LIBRARIES}")
message(STATUS "CMAKE_CXX_IMPLICIT_LINK_LIBRARIES: ${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "CMAKE_MSVC_RUNTIME_LIBRARY: ${CMAKE_MSVC_RUNTIME_LIBRARY}")

if(LIEF_INSTALL)
if(UNIX)
include(GNUInstallDirs)
set(CMAKE_INSTALL_LIBDIR "lib")
else()
if(WIN32)
set(CMAKE_INSTALL_LIBDIR "lib")
set(CMAKE_INSTALL_DATADIR "share")
set(CMAKE_INSTALL_INCLUDEDIR "include")
set(CMAKE_INSTALL_BINDIR "bin")
set(CMAKE_INSTALL_DATAROOTDIR "share")
message(STATUS "Setting installation destination on Windows to: ${CMAKE_INSTALL_PREFIX}")
else()
message(FATAL_ERROR "System not UNIX nor WIN32 - not implemented yet")
endif()
endif()
endif()

# LIEF Source definition
# ======================
Expand Down Expand Up @@ -414,6 +433,7 @@ if(LIEF_DOC)
add_subdirectory(doc)
endif()


# Post-build operations
# ======================
if(BUILD_SHARED_LIBS AND CMAKE_BUILD_TYPE MATCHES "Release")
Expand Down Expand Up @@ -465,25 +485,6 @@ if(LIEF_INSTALL)
endif()
endif()

# Installation
# ======================

if(UNIX)
include(GNUInstallDirs)
set(CMAKE_INSTALL_LIBDIR "lib")
else()
if(WIN32)
set(CMAKE_INSTALL_LIBDIR "lib")
set(CMAKE_INSTALL_DATADIR "share")
set(CMAKE_INSTALL_INCLUDEDIR "include")
set(CMAKE_INSTALL_BINDIR "bin")
set(CMAKE_INSTALL_DATAROOTDIR "share")
message(STATUS "Setting installation destination on Windows to: ${CMAKE_INSTALL_PREFIX}")
else()
message(FATAL_ERROR "System not UNIX nor WIN32 - not implemented yet")
endif()
endif()

install(
TARGETS LIB_LIEF lief_spdlog
EXPORT LIEFExport
Expand Down
23 changes: 17 additions & 6 deletions README.md
Expand Up @@ -131,17 +131,22 @@ import lief

# ELF
binary = lief.parse("/usr/bin/ls")
print(binary)
for section in binary.sections:
print(section.name, section.virtual_address)

# PE
binary = lief.parse("C:\\Windows\\explorer.exe")
print(binary)

if rheader := pe.rich_header:
print(rheader.key)

# Mach-O
binary = lief.parse("/usr/bin/ls")
print(binary)
for fixup in binary.dyld_chained_fixups:
print(fixup)
```


### C++

```cpp
Expand All @@ -150,17 +155,23 @@ print(binary)
int main(int argc, char** argv) {
// ELF
if (std::unique_ptr<const LIEF::ELF::Binary> elf = LIEF::ELF::Parser::parse("/bin/ls")) {
std::cout << *elf << '\n';
for (const LIEF::ELF::Section& section : elf->sections()) {
std::cout << section->name() << ' ' << section->virtual_address() << '\n';
}
}

// PE
if (std::unique_ptr<const LIEF::PE::Binary> pe = LIEF::PE::Parser::parse("C:\\Windows\\explorer.exe")) {
std::cout << *pe << '\n';
if (const LIEF::PE::RichHeader* rheader : pe->rich_header()) {
std::cout << rheader->key() << '\n';
}
}

// Mach-O
if (std::unique_ptr<LIEF::MachO::FatBinary> macho = LIEF::MachO::Parser::parse("/bin/ls")) {
std::cout << *macho << '\n';
for (const LIEF::MachO::DyldChainedFixups& fixup : macho->dyld_chained_fixups()) {
std::cout << fixup << '\n';
}
}

return 0;
Expand Down
4 changes: 4 additions & 0 deletions api/CMakeLists.txt
Expand Up @@ -6,3 +6,7 @@ if(LIEF_C_API)
add_subdirectory(c)
endif()

if(LIEF_RUST_API)
add_subdirectory(rust)
endif()

18 changes: 18 additions & 0 deletions api/rust/CMakeLists.txt
@@ -0,0 +1,18 @@
target_include_directories(LIB_LIEF PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>")

add_subdirectory(src)

if(LIEF_INSTALL)
install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT headers)

install(
FILES ${CMAKE_CURRENT_SOURCE_DIR}/autocxx_ffi.rs
DESTINATION ${CMAKE_INSTALL_LIBDIR}/LIEF/)
endif()



0 comments on commit d447511

Please sign in to comment.