Skip to content

Commit

Permalink
Add relocation in the Abstract layer
Browse files Browse the repository at this point in the history
The abstracted attributes are:
  * Address: virtual address where the relocation occurs
  * Size: size in bits of the relocation

See: LIEF::Relocation / lief.Relocation and abstract_reader

Resolve: #53
  • Loading branch information
romainthomas committed Sep 5, 2017
1 parent 58bf7a2 commit 9503f2f
Show file tree
Hide file tree
Showing 42 changed files with 782 additions and 211 deletions.
45 changes: 13 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ set_source_files_properties(${mbedtls_src_x509} PROPERTIES GENERATED TRUE)
set_source_files_properties(${mbedtls_src_tls} PROPERTIES GENERATED TRUE)
set_source_files_properties(${ELG_SOURCE_DIR}/easylogging++.cc PROPERTIES GENERATED TRUE)

set(LIEF_SRC
set(LIBLIEF_SOURCE_FILES
"${ELG_SOURCE_DIR}/easylogging++.cc"
"${mbedtls_src_crypto}"
"${mbedtls_src_x509}"
Expand All @@ -250,22 +250,6 @@ set(LIEF_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/visitors/json.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/visitors/hash.cpp")

set(LIEF_ABSTRACT_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/Abstract/Binary.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Abstract/Symbol.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Abstract/EnumToString.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Abstract/Header.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Abstract/Section.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Abstract/Section.tcc"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Abstract/Parser.cpp")

# Grouping basic LIEF source files
# ================================
source_group("Source Files\\Abstract" FILES ${LIEF_ABSTRACT_SRC})

set(LIBLIEF_SOURCE_FILES
"${LIEF_SRC}"
"${LIEF_ABSTRACT_SRC}")

# Grouping basic headers together
# ===============================
Expand Down Expand Up @@ -295,37 +279,34 @@ set(LIEF_VISITOR_INCLUDE_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/visitors/Hash.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/visitors/json.hpp")

set(LIEF_ABSTRACT_INCLUDE_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/Abstract.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/Binary.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/Symbol.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/EnumToString.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/Header.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/Section.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/Parser.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/enums.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/Abstract/type_traits.hpp")

set(LIEF_INCLUDE_FILES
${LIEF_INC_FILES}
${LIEF_VISITOR_INCLUDE_FILES}
${LIEF_ABSTRACT_INCLUDE_FILES})
)

source_group("Header Files" FILES ${LIEF_INC_FILES})
source_group("Source Files" FILES ${LIBLIEF_SOURCE_FILES})

source_group("Header Files\\visitors" FILES ${LIEF_VISITOR_INCLUDE_FILES})
source_group("Header Files\\Abstract" FILES ${LIEF_ABSTRACT_INCLUDE_FILES})

add_library(LIB_LIEF_STATIC STATIC ${LIBLIEF_SOURCE_FILES} ${LIEF_INCLUDE_FILES}) # Static one
add_library(LIB_LIEF_SHARED SHARED ${LIBLIEF_SOURCE_FILES} ${LIEF_INCLUDE_FILES}) # Shared one

# Abstract part
include("${CMAKE_CURRENT_SOURCE_DIR}/src/Abstract/CMakeLists.txt")

# ELF Part
if (LIEF_ELF)
include("${CMAKE_CURRENT_SOURCE_DIR}/src/ELF/CMakeLists.txt")
endif()

# PE Part
if (LIEF_PE)
include("${CMAKE_CURRENT_SOURCE_DIR}/src/PE/CMakeLists.txt")
endif()

# MachO part
if (LIEF_MACHO)
include("${CMAKE_CURRENT_SOURCE_DIR}/src/MachO/CMakeLists.txt")
endif()
Expand Down Expand Up @@ -360,9 +341,9 @@ set(LIEF_PRIVATE_INCLUDE_DIR
# Grouping external projects
# ==========================
source_group("mbedtls\\crypto" FILES ${mbedtls_src_crypto})
source_group("mbedtls\\x509" FILES ${mbedtls_src_x509})
source_group("mbedtls\\tls" FILES ${mbedtls_src_tls})
source_group("easylogging" FILES ${ELG_SOURCE_DIR}/easylogging++.cc)
source_group("mbedtls\\x509" FILES ${mbedtls_src_x509})
source_group("mbedtls\\tls" FILES ${mbedtls_src_tls})
source_group("easylogging" FILES ${ELG_SOURCE_DIR}/easylogging++.cc)

# Library definition
# ==================
Expand Down
25 changes: 25 additions & 0 deletions api/python/Abstract/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

set(LIEF_PYTHON_ABSTRACT_SRC
"${CMAKE_CURRENT_LIST_DIR}/init.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyBinary.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyHeader.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pySection.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyParser.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pySymbol.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyRelocation.cpp"
"${CMAKE_CURRENT_LIST_DIR}/pyEnums.cpp"
)


set(LIEF_PYTHON_ABSTRACT_HDR
"${CMAKE_CURRENT_LIST_DIR}/init.hpp"
)

source_group("Source Files\\Abstract" FILES ${LIEF_PYTHON_ABSTRACT_SRC})
source_group("Header Files\\Abstract" FILES ${LIEF_PYTHON_ABSTRACT_HDR})

target_sources(pyLIEF PRIVATE "${LIEF_PYTHON_ABSTRACT_SRC}" "${LIEF_PYTHON_ABSTRACT_HDR}")
target_include_directories(pyLIEF PUBLIC "${CMAKE_CURRENT_LIST_DIR}")


1 change: 1 addition & 0 deletions api/python/Abstract/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ void init_LIEF_module(py::module& m) {
init_LIEF_Section_class(m);
init_LIEF_Symbol_class(m);
init_LIEF_Parser_class(m);
init_LIEF_Relocation_class(m);
}
1 change: 1 addition & 0 deletions api/python/Abstract/init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ void init_LIEF_Binary_class(py::module&);
void init_LIEF_Section_class(py::module&);
void init_LIEF_Symbol_class(py::module&);
void init_LIEF_Parser_class(py::module&);
void init_LIEF_Relocation_class(py::module&);

#endif
5 changes: 5 additions & 0 deletions api/python/Abstract/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ void init_LIEF_Binary_class(py::module& m) {
"Return a list in **read only** of binary's abstract " RST_CLASS_REF(lief.Section) "",
py::return_value_policy::reference_internal)

.def_property_readonly("relocations",
static_cast<it_t<it_relocations>>(&Binary::relocations),
"Return an iterator over abstract " RST_CLASS_REF(lief.Relocation) "",
py::return_value_policy::reference_internal)

.def_property_readonly("exported_functions",
[] (const Binary& binary) {
const std::vector<std::string>& exported_functions = binary.get_exported_functions();
Expand Down
64 changes: 64 additions & 0 deletions api/python/Abstract/objects/pyRelocation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright 2017 R. Thomas
* Copyright 2017 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "init.hpp"

#include "LIEF/visitors/Hash.hpp"

#include "LIEF/Abstract/Relocation.hpp"

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

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

void init_LIEF_Relocation_class(py::module& m) {
py::class_<LIEF::Relocation>(m, "Relocation")
.def(py::init(),
"Default constructor")

.def(py::init<uint64_t, uint8_t>(),
"Constructor from :attr:`~lief.Relocation.address` and :attr:`~lief.Relocation.size`",
"address"_a, "size"_a)

.def_property("address",
static_cast<getter_t<uint64_t>>(&LIEF::Relocation::address),
static_cast<setter_t<uint64_t>>(&LIEF::Relocation::address),
"Relocation's address")

.def_property("size",
static_cast<getter_t<size_t>>(&LIEF::Relocation::size),
static_cast<setter_t<size_t>>(&LIEF::Relocation::size),
"Relocation's size (in **bits**)")

.def("__eq__", &LIEF::Relocation::operator==)
.def("__ne__", &LIEF::Relocation::operator!=)
.def("__hash__",
[] (const LIEF::Relocation& relocation) {
return LIEF::Hash::hash(relocation);
})

.def("__str__",
[] (const LIEF::Relocation& entry)
{
std::ostringstream stream;
stream << entry;
std::string str = stream.str();
return str;
});


}
23 changes: 4 additions & 19 deletions api/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,10 @@ if (LIEF_ENABLE_JSON)
)
endif()


set(LIEF_PYTHON_ABSTRACT_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/init.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/objects"
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/objects/pyBinary.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/objects/pyHeader.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/objects/pySection.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/objects/pyParser.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/objects/pySymbol.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/pyEnums.cpp"
)

set(LIEF_PYTHON_SRC
${LIEF_PYTHON_BASIC_SRC}
${LIEF_PYTHON_ABSTRACT_SRC})
)

set(LIEF_PYTHON_ABSTRACT_HDR
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract/init.hpp")

set(LIEF_PYTHON_BASIC_HDR
"${CMAKE_CURRENT_SOURCE_DIR}/pyIterators.hpp"
Expand All @@ -94,22 +80,21 @@ set(LIEF_PYTHON_BASIC_HDR

set(LIEF_PYTHON_HDR
${LIEF_PYTHON_BASIC_HDR}
${LIEF_PYTHON_ABSTRACT_HDR})
)

source_group("Header Files\\Abstract" FILES ${LIEF_PYTHON_ABSTRACT_HDR})
source_group("Header Files" FILES ${LIEF_PYTHON_BASIC_HDR})
source_group("Source Files\\Abstract" FILES ${LIEF_PYTHON_ABSTRACT_SRC})

add_library(pyLIEF SHARED ${LIEF_PYTHON_SRC} ${LIEF_PYTHON_HDR})

target_include_directories(pyLIEF PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/"
"${CMAKE_CURRENT_SOURCE_DIR}/Abstract"
"${PYTHON_INCLUDE_DIR}"
"${PYBIND11_SOURCE_DIR}/include")

add_definitions(-DELPP_NO_DEFAULT_LOG_FILE)

include("${CMAKE_CURRENT_SOURCE_DIR}/Abstract/CMakeLists.txt")

if(LIEF_ELF)
include("${CMAKE_CURRENT_SOURCE_DIR}/ELF/CMakeLists.txt")
endif()
Expand Down
12 changes: 1 addition & 11 deletions api/python/ELF/objects/pyRelocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@ using setter_t = void (Relocation::*)(T);

void init_ELF_Relocation_class(py::module& m) {
// Relocation object
py::class_<Relocation>(m, "Relocation")
py::class_<Relocation, LIEF::Relocation>(m, "Relocation")
.def(py::init<>())
.def(py::init<uint64_t, uint32_t, int64_t, bool>(),
"address"_a, "type"_a = 0, "addend"_a = 0, "is_rela"_a = false)

.def_property("address",
static_cast<getter_t<uint64_t>>(&Relocation::address),
static_cast<setter_t<uint64_t>>(&Relocation::address),
"Address (or offset) of the relocation")

.def_property("addend",
static_cast<getter_t<int64_t>>(&Relocation::addend),
static_cast<setter_t<int64_t>>(&Relocation::addend),
Expand Down Expand Up @@ -75,11 +70,6 @@ void init_ELF_Relocation_class(py::module& m) {
static_cast<getter_t<bool>>(&Relocation::is_rel),
"``True`` if the relocation doesn't use the :attr:`~lief.ELF.Relocation.addend` proprety")

.def_property_readonly("size",
static_cast<getter_t<uint32_t>>(&Relocation::size),
"Size in **bits** of the value patched by the relocation")


.def("__eq__", &Relocation::operator==)
.def("__ne__", &Relocation::operator!=)
.def("__hash__",
Expand Down
8 changes: 1 addition & 7 deletions api/python/MachO/objects/pyRelocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using setter_t = void (Relocation::*)(T);

void init_MachO_Relocation_class(py::module& m) {

py::class_<Relocation>(m, "Relocation")
py::class_<Relocation, LIEF::Relocation>(m, "Relocation")

.def_property("address",
static_cast<getter_t<uint64_t>>(&Relocation::address),
Expand All @@ -56,12 +56,6 @@ void init_MachO_Relocation_class(py::module& m) {
"the instruction to the address contained in the instruction.",
py::return_value_policy::reference_internal)

.def_property("size",
static_cast<getter_t<uint8_t>>(&Relocation::size),
static_cast<setter_t<uint8_t>>(&Relocation::size),
"Indicates the length (in bits) of the item containing the address to be relocated.",
py::return_value_policy::reference_internal)

.def_property("type",
static_cast<getter_t<uint8_t>>(&Relocation::type),
static_cast<setter_t<uint8_t>>(&Relocation::type),
Expand Down
7 changes: 5 additions & 2 deletions api/python/PE/objects/pyRelocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ using getter_t = T (Relocation::*)(void) const;
template<class T>
using setter_t = void (Relocation::*)(T);

template<class T>
using it_t = T (Relocation::*)(void);

void init_PE_Relocation_class(py::module& m) {
py::class_<Relocation>(m, "Relocation")
.def(py::init<>())
Expand All @@ -36,8 +39,8 @@ void init_PE_Relocation_class(py::module& m) {
static_cast<setter_t<uint32_t>>(&Relocation::virtual_address))

.def_property_readonly("entries",
&Relocation::entries,
py::return_value_policy::reference)
static_cast<it_t<it_relocation_entries>>(&Relocation::entries),
py::return_value_policy::reference_internal)

.def("add_entry",
&Relocation::add_entry,
Expand Down
15 changes: 11 additions & 4 deletions api/python/PE/objects/pyRelocationEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,27 @@ template<class T>
using setter_t = void (RelocationEntry::*)(T);

void init_PE_RelocationEntry_class(py::module& m) {
py::class_<RelocationEntry>(m, "RelocationEntry")
py::class_<RelocationEntry, LIEF::Relocation>(m, "RelocationEntry")
.def(py::init<>())

.def_property("data",
static_cast<getter_t<uint16_t>>(&RelocationEntry::data),
static_cast<setter_t<uint16_t>>(&RelocationEntry::data))
static_cast<setter_t<uint16_t>>(&RelocationEntry::data),
"Raw data of the relocation:\n\n"

"\t\t * The **high** 4 bits store the relocation :attr:`~lief.PE.RelocationEntry.type`\n\n"
"\t\t * The **low** 12 bits store the relocation offset (:attr:`~lief.PE.RelocationEntry.position`)\n\n"
)

.def_property("position",
static_cast<getter_t<uint16_t>>(&RelocationEntry::position),
static_cast<setter_t<uint16_t>>(&RelocationEntry::position))
static_cast<setter_t<uint16_t>>(&RelocationEntry::position),
"Offset - relative to :attr:`~lief.PE.Relocation.virtual_address` - where the relocation occurs")

.def_property("type",
static_cast<getter_t<RELOCATIONS_BASE_TYPES>>(&RelocationEntry::type),
static_cast<setter_t<RELOCATIONS_BASE_TYPES>>(&RelocationEntry::type))
static_cast<setter_t<RELOCATIONS_BASE_TYPES>>(&RelocationEntry::type),
"Type of the relocation")


.def("__eq__", &RelocationEntry::operator==)
Expand Down
2 changes: 2 additions & 0 deletions api/python/pyIterators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void init_LIEF_iterators(py::module& m) {
// ========
init_ref_iterator<LIEF::it_sections>(m);
init_ref_iterator<LIEF::it_symbols>(m);
init_ref_iterator<LIEF::it_relocations>(m);

// ELF
// ===
Expand All @@ -48,6 +49,7 @@ void init_LIEF_iterators(py::module& m) {
init_ref_iterator<LIEF::PE::it_sections>(m);
init_ref_iterator<LIEF::PE::it_data_directories>(m);
init_ref_iterator<LIEF::PE::it_relocations>(m);
init_ref_iterator<LIEF::PE::it_relocation_entries>(m);
init_ref_iterator<LIEF::PE::it_imports>(m);
init_ref_iterator<LIEF::PE::it_import_entries>(m);
init_ref_iterator<LIEF::PE::it_export_entries>(m);
Expand Down
Loading

0 comments on commit 9503f2f

Please sign in to comment.