Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Resolve #119 and enhance ELF Python bindings
  • Loading branch information
romainthomas committed Jun 8, 2018
1 parent 163cd3b commit cd1cc45
Show file tree
Hide file tree
Showing 63 changed files with 406 additions and 184 deletions.
2 changes: 1 addition & 1 deletion api/c/ELF/Binary.cpp
Expand Up @@ -63,7 +63,7 @@ void init_c_binary(Elf_Binary_t* c_binary, Binary* binary) {
}

Elf_Binary_t* elf_parse(const char *file) {
Binary* binary = Parser::parse(file);
Binary* binary = Parser::parse(file).release();
Elf_Binary_t* c_binary = static_cast<Elf_Binary_t*>(malloc(sizeof(Elf_Binary_t)));
memset(c_binary, 0, sizeof(Elf_Binary_t));
init_c_binary(c_binary, binary);
Expand Down
2 changes: 1 addition & 1 deletion api/c/MachO/Parser.cpp
Expand Up @@ -23,7 +23,7 @@
using namespace LIEF::MachO;

Macho_Binary_t** macho_parse(const char *file) {
FatBinary* macho_binaries = Parser::parse(file);
FatBinary* macho_binaries = Parser::parse(file).release();

Macho_Binary_t** c_macho_binaries = static_cast<Macho_Binary_t**>(
malloc((macho_binaries->size() + 1) * sizeof(Macho_Binary_t**)));
Expand Down
2 changes: 1 addition & 1 deletion api/c/PE/Binary.cpp
Expand Up @@ -50,7 +50,7 @@ void init_c_binary(Pe_Binary_t* c_binary, Binary* binary) {
}

Pe_Binary_t* pe_parse(const char *file) {
Binary* binary = Parser::parse(file);
Binary* binary = Parser::parse(file).release();
Pe_Binary_t* c_binary = static_cast<Pe_Binary_t*>(malloc(sizeof(Pe_Binary_t)));
std::memset(c_binary, 0, sizeof(Pe_Binary_t));
init_c_binary(c_binary, binary);
Expand Down
4 changes: 2 additions & 2 deletions api/python/ART/objects/pyParser.cpp
Expand Up @@ -27,13 +27,13 @@ void create<Parser>(py::module& m) {

// Parser (Parser)
m.def("parse",
static_cast<File* (*) (const std::string&)>(&Parser::parse),
static_cast<std::unique_ptr<File> (*) (const std::string&)>(&Parser::parse),
"Parse the given filename and return an " RST_CLASS_REF(lief.ART.File) " object"
"filename"_a,
py::return_value_policy::take_ownership);

m.def("parse",
static_cast<File* (*) (const std::vector<uint8_t>&, const std::string&)>(&Parser::parse),
static_cast<std::unique_ptr<File> (*) (const std::vector<uint8_t>&, const std::string&)>(&Parser::parse),
"Parse the given raw data and return an " RST_CLASS_REF(lief.ART.File) " object\n\n"
"raw"_a, py::arg("name") = "",
py::return_value_policy::take_ownership);
Expand Down
4 changes: 2 additions & 2 deletions api/python/Abstract/objects/pyParser.cpp
Expand Up @@ -22,13 +22,13 @@
void init_LIEF_Parser_class(py::module& m) {

m.def("parse",
static_cast<LIEF::Binary* (*) (const std::string&)>(&LIEF::Parser::parse),
static_cast<std::unique_ptr<LIEF::Binary> (*) (const std::string&)>(&LIEF::Parser::parse),
"Parse the given binary and return a " RST_CLASS_REF(lief.Binary) " object",
"filepath"_a,
py::return_value_policy::take_ownership);

m.def("parse",
static_cast<LIEF::Binary* (*) (const std::vector<uint8_t>&, const std::string&)>(&LIEF::Parser::parse),
static_cast<std::unique_ptr<LIEF::Binary> (*) (const std::vector<uint8_t>&, const std::string&)>(&LIEF::Parser::parse),
"Parse the given binary and return a " RST_CLASS_REF(lief.Binary) " object",
"raw"_a, "name"_a = "",
py::return_value_policy::take_ownership);
Expand Down
4 changes: 2 additions & 2 deletions api/python/DEX/objects/pyParser.cpp
Expand Up @@ -26,13 +26,13 @@ template<>
void create<Parser>(py::module& m) {

m.def("parse",
static_cast<File* (*) (const std::string&)>(&Parser::parse),
static_cast<std::unique_ptr<File> (*) (const std::string&)>(&Parser::parse),
"Parse the given filename and return a " RST_CLASS_REF(lief.DEX.File) " object"
"filename"_a,
py::return_value_policy::take_ownership);

m.def("parse",
static_cast<File* (*) (const std::vector<uint8_t>&, const std::string&)>(&Parser::parse),
static_cast<std::unique_ptr<File> (*) (const std::vector<uint8_t>&, const std::string&)>(&Parser::parse),
"Parse the given raw data and return a " RST_CLASS_REF(lief.DEX.File) " object\n\n"
"raw"_a, py::arg("name") = "",
py::return_value_policy::take_ownership);
Expand Down
2 changes: 1 addition & 1 deletion api/python/ELF/CMakeLists.txt
Expand Up @@ -22,7 +22,7 @@ set(LIEF_PYTHON_ELF_SRC
"${CMAKE_CURRENT_LIST_DIR}/objects/pyGnuHash.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pySysvHash.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyBuilder.cpp"
"${CMAKE_CURRENT_LIST_DIR}/pyELFStructures.cpp"
"${CMAKE_CURRENT_LIST_DIR}/pyEnums.cpp"
"${CMAKE_CURRENT_LIST_DIR}/pySizes.cpp"
)

Expand Down
9 changes: 8 additions & 1 deletion api/python/ELF/objects/pyAndroidNote.cpp
Expand Up @@ -21,13 +21,17 @@
#include "LIEF/ELF/hash.hpp"
#include "LIEF/ELF/AndroidNote.hpp"

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (AndroidNote::*)(void) const;

template<class T>
using setter_t = void (AndroidNote::*)(T);

void init_ELF_AndroidNote_class(py::module& m) {
template<>
void create<AndroidNote>(py::module& m) {

py::class_<AndroidNote, Note>(m, "AndroidNote")

Expand Down Expand Up @@ -65,3 +69,6 @@ void init_ELF_AndroidNote_class(py::module& m) {
return str;
});
}

}
}
9 changes: 8 additions & 1 deletion api/python/ELF/objects/pyBinary.cpp
Expand Up @@ -21,6 +21,9 @@

#include "pyELF.hpp"

namespace LIEF {
namespace ELF {

template<class T>
using no_const_getter = T (Binary::*)(void);

Expand All @@ -33,7 +36,9 @@ using getter_t = T (Binary::*)(void) const;
template<class T>
using setter_t = void (Binary::*)(T);

void init_ELF_Binary_class(py::module& m) {

template<>
void create<Binary>(py::module& m) {

// Binary object
py::class_<Binary, LIEF::Binary>(m, "Binary", "ELF binary representation")
Expand Down Expand Up @@ -518,3 +523,5 @@ void init_ELF_Binary_class(py::module& m) {
return str;
});
}
}
}
7 changes: 6 additions & 1 deletion api/python/ELF/objects/pyBuilder.cpp
Expand Up @@ -20,8 +20,11 @@
#include <string>
#include <sstream>

namespace LIEF {
namespace ELF {

void init_ELF_Builder_class(py::module& m) {
template<>
void create<Builder>(py::module& m) {
py::class_<Builder>(m, "Builder")
.def(py::init<Binary*>(),
"Constructor that takes a " RST_CLASS_REF(lief.ELF.Binary) "",
Expand All @@ -47,3 +50,5 @@ void init_ELF_Builder_class(py::module& m) {
py::return_value_policy::reference_internal);

}
}
}
10 changes: 9 additions & 1 deletion api/python/ELF/objects/pyDynamicEntry.cpp
Expand Up @@ -21,13 +21,18 @@
#include <string>
#include <sstream>

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (DynamicEntry::*)(void) const;

template<class T>
using setter_t = void (DynamicEntry::*)(T);

void init_ELF_DynamicEntry_class(py::module& m) {

template<>
void create<DynamicEntry>(py::module& m) {

// DynamicEntry object
py::class_<DynamicEntry, LIEF::Object>(m, "DynamicEntry")
Expand Down Expand Up @@ -64,3 +69,6 @@ void init_ELF_DynamicEntry_class(py::module& m) {
return str;
});
}

}
}
10 changes: 9 additions & 1 deletion api/python/ELF/objects/pyDynamicEntryArray.cpp
Expand Up @@ -23,13 +23,18 @@
#include <string>
#include <sstream>

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (DynamicEntryArray::*)(void) const;

template<class T>
using setter_t = void (DynamicEntryArray::*)(T);

void init_ELF_DynamicEntryArray_class(py::module& m) {

template<>
void create<DynamicEntryArray>(py::module& m) {

// Dynamic Entry Array object
py::class_<DynamicEntryArray, DynamicEntry>(m, "DynamicEntryArray")
Expand Down Expand Up @@ -92,3 +97,6 @@ void init_ELF_DynamicEntryArray_class(py::module& m) {
return str;
});
}

}
}
10 changes: 9 additions & 1 deletion api/python/ELF/objects/pyDynamicEntryFlags.cpp
Expand Up @@ -23,13 +23,18 @@
#include <string>
#include <sstream>

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (DynamicEntryFlags::*)(void) const;

template<class T>
using setter_t = void (DynamicEntryFlags::*)(T);

void init_ELF_DynamicEntryFlags_class(py::module& m) {

template<>
void create<DynamicEntryFlags>(py::module& m) {

py::class_<DynamicEntryFlags, DynamicEntry>(m, "DynamicEntryFlags")
.def(py::init<>())
Expand Down Expand Up @@ -96,3 +101,6 @@ void init_ELF_DynamicEntryFlags_class(py::module& m) {
return str;
});
}

}
}
10 changes: 9 additions & 1 deletion api/python/ELF/objects/pyDynamicEntryLibrary.cpp
Expand Up @@ -23,13 +23,18 @@
#include <string>
#include <sstream>

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (DynamicEntryLibrary::*)(void) const;

template<class T>
using setter_t = void (DynamicEntryLibrary::*)(T);

void init_ELF_DynamicEntryLibrary_class(py::module& m) {

template<>
void create<DynamicEntryLibrary>(py::module& m) {

//
// Dynamic Entry Library object
Expand Down Expand Up @@ -63,3 +68,6 @@ void init_ELF_DynamicEntryLibrary_class(py::module& m) {
return str;
});
}

}
}
10 changes: 9 additions & 1 deletion api/python/ELF/objects/pyDynamicEntryRpath.cpp
Expand Up @@ -23,13 +23,18 @@
#include <string>
#include <sstream>

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (DynamicEntryRpath::*)(void) const;

template<class T>
using setter_t = void (DynamicEntryRpath::*)(T);

void init_ELF_DynamicEntryRpath_class(py::module& m) {

template<>
void create<DynamicEntryRpath>(py::module& m) {

//
// Dynamic Entry RPATH object
Expand Down Expand Up @@ -102,3 +107,6 @@ void init_ELF_DynamicEntryRpath_class(py::module& m) {
return str;
});
}

}
}
10 changes: 9 additions & 1 deletion api/python/ELF/objects/pyDynamicEntryRunPath.cpp
Expand Up @@ -23,13 +23,18 @@
#include <string>
#include <sstream>

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (DynamicEntryRunPath::*)(void) const;

template<class T>
using setter_t = void (DynamicEntryRunPath::*)(T);

void init_ELF_DynamicEntryRunPath_class(py::module& m) {

template<>
void create<DynamicEntryRunPath>(py::module& m) {

//
// Dynamic Entry RUNPATH object
Expand Down Expand Up @@ -101,3 +106,6 @@ void init_ELF_DynamicEntryRunPath_class(py::module& m) {
return str;
});
}

}
}
10 changes: 9 additions & 1 deletion api/python/ELF/objects/pyDynamicSharedObject.cpp
Expand Up @@ -23,13 +23,18 @@
#include <string>
#include <sstream>

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (DynamicSharedObject::*)(void) const;

template<class T>
using setter_t = void (DynamicSharedObject::*)(T);

void init_ELF_DynamicSharedObject_class(py::module& m) {

template<>
void create<DynamicSharedObject>(py::module& m) {

//
// Dynamic Shared Object object
Expand Down Expand Up @@ -62,3 +67,6 @@ void init_ELF_DynamicSharedObject_class(py::module& m) {
return str;
});
}

}
}
10 changes: 7 additions & 3 deletions api/python/ELF/objects/pyGnuHash.cpp
Expand Up @@ -21,14 +21,18 @@
#include "LIEF/ELF/hash.hpp"
#include "LIEF/ELF/GnuHash.hpp"

namespace LIEF {
namespace ELF {

template<class T>
using getter_t = T (GnuHash::*)(void) const;

template<class T>
using setter_t = void (GnuHash::*)(T);

void init_ELF_GnuHash_class(py::module& m) {

template<>
void create<GnuHash>(py::module& m) {

py::class_<GnuHash, LIEF::Object>(m, "GnuHash")
.def(py::init<>())
Expand Down Expand Up @@ -106,8 +110,8 @@ void init_ELF_GnuHash_class(py::module& m) {
std::string str = stream.str();
return str;
});
}



}
}

0 comments on commit cd1cc45

Please sign in to comment.