Skip to content

Commit

Permalink
Refactor PE's relocation API
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Jan 13, 2024
1 parent 03e9230 commit ada2528
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 279 deletions.
4 changes: 4 additions & 0 deletions api/python/lief/MachO.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ class Binary(lief.Binary):
@property
def has_main_command(self) -> bool: ...
@property
def has_nx_heap(self) -> bool: ...
@property
def has_nx_stack(self) -> bool: ...
@property
def has_rpath(self) -> bool: ...
@property
def has_segment_split_info(self) -> bool: ...
Expand Down
57 changes: 29 additions & 28 deletions api/python/lief/PE.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import lief.PE.LoadConfigurationV1 # type: ignore
import lief.PE.OptionalHeader # type: ignore
import lief.PE.Pogo # type: ignore
import lief.PE.Relocation # type: ignore
import lief.PE.RelocationEntry # type: ignore
import lief.PE.ResourceDialog # type: ignore
import lief.PE.ResourceNode # type: ignore
import lief.PE.ResourcesManager # type: ignore
Expand Down Expand Up @@ -1470,33 +1471,6 @@ class PogoEntry(lief.Object):
def __init__(self) -> None: ...
def copy(self) -> lief.PE.PogoEntry: ...

class RELOCATIONS_BASE_TYPES:
ABSOLUTE: ClassVar[RELOCATIONS_BASE_TYPES] = ...
ARM_MOV32: ClassVar[RELOCATIONS_BASE_TYPES] = ...
ARM_MOV32A: ClassVar[RELOCATIONS_BASE_TYPES] = ...
ARM_MOV32T: ClassVar[RELOCATIONS_BASE_TYPES] = ...
DIR64: ClassVar[RELOCATIONS_BASE_TYPES] = ...
HIGH: ClassVar[RELOCATIONS_BASE_TYPES] = ...
HIGH3ADJ: ClassVar[RELOCATIONS_BASE_TYPES] = ...
HIGHADJ: ClassVar[RELOCATIONS_BASE_TYPES] = ...
HIGHLOW: ClassVar[RELOCATIONS_BASE_TYPES] = ...
IA64_IMM64: ClassVar[RELOCATIONS_BASE_TYPES] = ...
LOW: ClassVar[RELOCATIONS_BASE_TYPES] = ...
MIPS_JMPADDR: ClassVar[RELOCATIONS_BASE_TYPES] = ...
MIPS_JMPADDR16: ClassVar[RELOCATIONS_BASE_TYPES] = ...
REL: ClassVar[RELOCATIONS_BASE_TYPES] = ...
RISCV_HI20: ClassVar[RELOCATIONS_BASE_TYPES] = ...
RISCV_LOW12I: ClassVar[RELOCATIONS_BASE_TYPES] = ...
RISCV_LOW12S: ClassVar[RELOCATIONS_BASE_TYPES] = ...
SECTION: ClassVar[RELOCATIONS_BASE_TYPES] = ...
THUMB_MOV32: ClassVar[RELOCATIONS_BASE_TYPES] = ...
__name__: Any
def __init__(self, *args, **kwargs) -> None: ...
@staticmethod
def from_value(arg: int, /) -> lief.PE.RELOCATIONS_BASE_TYPES: ...
@property
def value(self) -> int: ...

class RESOURCE_LANGS:
AFRIKAANS: ClassVar[RESOURCE_LANGS] = ...
ALBANIAN: ClassVar[RESOURCE_LANGS] = ...
Expand Down Expand Up @@ -1624,9 +1598,36 @@ class Relocation(lief.Object):
def entries(self) -> lief.PE.Relocation.it_entries: ...

class RelocationEntry(lief.Relocation):
class BASE_TYPES:
ABSOLUTE: ClassVar[RelocationEntry.BASE_TYPES] = ...
ARM_MOV32: ClassVar[RelocationEntry.BASE_TYPES] = ...
ARM_MOV32A: ClassVar[RelocationEntry.BASE_TYPES] = ...
ARM_MOV32T: ClassVar[RelocationEntry.BASE_TYPES] = ...
DIR64: ClassVar[RelocationEntry.BASE_TYPES] = ...
HIGH: ClassVar[RelocationEntry.BASE_TYPES] = ...
HIGH3ADJ: ClassVar[RelocationEntry.BASE_TYPES] = ...
HIGHADJ: ClassVar[RelocationEntry.BASE_TYPES] = ...
HIGHLOW: ClassVar[RelocationEntry.BASE_TYPES] = ...
IA64_IMM64: ClassVar[RelocationEntry.BASE_TYPES] = ...
LOW: ClassVar[RelocationEntry.BASE_TYPES] = ...
MIPS_JMPADDR: ClassVar[RelocationEntry.BASE_TYPES] = ...
MIPS_JMPADDR16: ClassVar[RelocationEntry.BASE_TYPES] = ...
REL: ClassVar[RelocationEntry.BASE_TYPES] = ...
RISCV_HI20: ClassVar[RelocationEntry.BASE_TYPES] = ...
RISCV_LOW12I: ClassVar[RelocationEntry.BASE_TYPES] = ...
RISCV_LOW12S: ClassVar[RelocationEntry.BASE_TYPES] = ...
SECTION: ClassVar[RelocationEntry.BASE_TYPES] = ...
THUMB_MOV32: ClassVar[RelocationEntry.BASE_TYPES] = ...
UNKNOWN: ClassVar[RelocationEntry.BASE_TYPES] = ...
__name__: Any
def __init__(self, *args, **kwargs) -> None: ...
@staticmethod
def from_value(arg: int, /) -> lief.PE.RelocationEntry.BASE_TYPES: ...
@property
def value(self) -> int: ...
data: int
position: int
type: lief.PE.RELOCATIONS_BASE_TYPES
type: lief.PE.RelocationEntry.BASE_TYPES
def __init__(self) -> None: ...

class Repro(Debug):
Expand Down
24 changes: 0 additions & 24 deletions api/python/src/PE/enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,6 @@ void init_enums(nb::module_& m) {
.value(PY_ENUM(SYMBOL_STORAGE_CLASS::IMAGE_SYM_CLASS_WEAK_EXTERNAL))
.value(PY_ENUM(SYMBOL_STORAGE_CLASS::IMAGE_SYM_CLASS_CLR_TOKEN));


enum_<RELOCATIONS_BASE_TYPES>(m, "RELOCATIONS_BASE_TYPES")
.value("ABSOLUTE", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_ABSOLUTE)
.value("HIGH", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_HIGH)
.value("LOW", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_LOW)
.value("HIGHLOW", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_HIGHLOW)
.value("HIGHADJ", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_HIGHADJ)
.value("MIPS_JMPADDR", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_MIPS_JMPADDR)
.value("ARM_MOV32A", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_ARM_MOV32A)
.value("ARM_MOV32", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_ARM_MOV32)
.value("RISCV_HI20", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_RISCV_HI20)
.value("ARM_MOV32T", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_ARM_MOV32T)
.value("THUMB_MOV32", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_THUMB_MOV32)
.value("RISCV_LOW12I", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_RISCV_LOW12I)
.value("RISCV_LOW12S", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_RISCV_LOW12S)
.value("SECTION", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_SECTION)
.value("REL", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_REL)
.value("MIPS_JMPADDR16", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_MIPS_JMPADDR16)
.value("IA64_IMM64", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_IA64_IMM64)
.value("DIR64", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_DIR64)
.value("HIGH3ADJ", RELOCATIONS_BASE_TYPES::IMAGE_REL_BASED_HIGH3ADJ);

enum_<EXTENDED_WINDOW_STYLES>(m, "EXTENDED_WINDOW_STYLES")
.value(PY_ENUM(EXTENDED_WINDOW_STYLES::WS_EX_DLGMODALFRAME))
.value(PY_ENUM(EXTENDED_WINDOW_STYLES::WS_EX_NOPARENTNOTIFY))
Expand All @@ -143,8 +121,6 @@ void init_enums(nb::module_& m) {
.value(PY_ENUM(EXTENDED_WINDOW_STYLES::WS_EX_STATICEDGE))
.value(PY_ENUM(EXTENDED_WINDOW_STYLES::WS_EX_APPWINDOW));



enum_<WINDOW_STYLES>(m, "WINDOW_STYLES")
.value(PY_ENUM(WINDOW_STYLES::WS_OVERLAPPED))
.value(PY_ENUM(WINDOW_STYLES::WS_POPUP))
Expand Down
39 changes: 33 additions & 6 deletions api/python/src/PE/objects/pyRelocationEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "LIEF/PE/RelocationEntry.hpp"

#include "enums_wrapper.hpp"

#include <string>
#include <sstream>
#include <nanobind/stl/string.h>
Expand All @@ -25,14 +27,39 @@ namespace LIEF::PE::py {

template<>
void create<RelocationEntry>(nb::module_& m) {
nb::class_<RelocationEntry, LIEF::Relocation>(m, "RelocationEntry",
nb::class_<RelocationEntry, LIEF::Relocation> entry(m, "RelocationEntry",
R"delim(
Class which represents an entry of the PE relocation table.
It extends the :class:`lief.Relocation` object to provide an uniform API across the file formats
)delim"_doc)
.def(nb::init<>())
It extends the :class:`lief.Relocation` object to provide an uniform API across the file formats.
)delim"_doc);

#define ENTRY(X) .value(to_string(RelocationEntry::BASE_TYPES::X), RelocationEntry::BASE_TYPES::X)
enum_<RelocationEntry::BASE_TYPES>(entry, "BASE_TYPES")
ENTRY(UNKNOWN)
ENTRY(ABSOLUTE)
ENTRY(HIGH)
ENTRY(LOW)
ENTRY(HIGHLOW)
ENTRY(HIGHADJ)
ENTRY(MIPS_JMPADDR)
ENTRY(ARM_MOV32A)
ENTRY(ARM_MOV32)
ENTRY(RISCV_HI20)
ENTRY(SECTION)
ENTRY(REL)
ENTRY(ARM_MOV32T)
ENTRY(THUMB_MOV32)
ENTRY(RISCV_LOW12I)
ENTRY(RISCV_LOW12S)
ENTRY(MIPS_JMPADDR16)
ENTRY(IA64_IMM64)
ENTRY(DIR64)
ENTRY(HIGH3ADJ);
#undef ENTRY

entry
.def(nb::init<>())
.def_prop_rw("data",
nb::overload_cast<>(&RelocationEntry::data, nb::const_),
nb::overload_cast<uint16_t>(&RelocationEntry::data),
Expand All @@ -50,8 +77,8 @@ void create<RelocationEntry>(nb::module_& m) {

.def_prop_rw("type",
nb::overload_cast<>(&RelocationEntry::type, nb::const_),
nb::overload_cast<RELOCATIONS_BASE_TYPES>(&RelocationEntry::type),
"Type of the relocation (see: " RST_CLASS_REF(lief.PE.RELOCATIONS_BASE_TYPES) ")"_doc)
nb::overload_cast<RelocationEntry::BASE_TYPES>(&RelocationEntry::type),
"Type of the relocation"_doc)

LIEF_DEFAULT_STR(RelocationEntry);
}
Expand Down
3 changes: 0 additions & 3 deletions doc/sphinx/api/cpp/pe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,6 @@ Enums
.. doxygenenum:: LIEF::PE::SYMBOL_COMPLEX_TYPES
:project: lief

.. doxygenenum:: LIEF::PE::RELOCATIONS_BASE_TYPES
:project: lief

.. doxygenenum:: LIEF::PE::RELOCATIONS_I386
:project: lief

Expand Down
7 changes: 0 additions & 7 deletions doc/sphinx/api/python/pe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,6 @@ SYMBOL_STORAGE_CLASS

----------

RELOCATIONS_BASE_TYPES
~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: lief.PE.RELOCATIONS_BASE_TYPES

----------

FIXED_VERSION_FILE_SUB_TYPES
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
37 changes: 22 additions & 15 deletions include/LIEF/PE/Relocation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,18 @@
#include "LIEF/visibility.h"
#include "LIEF/iterators.hpp"

#include "LIEF/PE/enums.hpp"

namespace LIEF {
namespace PE {

class Parser;
class Builder;
class RelocationEntry;

namespace details {
struct pe_base_relocation_block;
}

//! Class which represents the *Base Relocation Block*
//! Usually, we find this structure in the ``.reloc`` section
//! We usually find this structure in the ``.reloc`` section
class LIEF_API Relocation : public Object {

friend class Parser;
friend class Builder;

Expand All @@ -48,27 +43,39 @@ class LIEF_API Relocation : public Object {
using it_entries = ref_iterator<entries_t&, RelocationEntry*>;
using it_const_entries = const_ref_iterator<const entries_t&, RelocationEntry*>;

Relocation();
Relocation() = default;
Relocation(const Relocation& other);
Relocation& operator=(Relocation other);
Relocation(const details::pe_base_relocation_block& header);
~Relocation() override;
~Relocation() override = default;

void swap(Relocation& other);

//! The RVA for which the offset of the relocation entries (RelocationEntry) is added
uint32_t virtual_address() const;
uint32_t virtual_address() const {
return virtual_address_;
}

//! The total number of bytes in the base relocation block.
//! ``block_size = sizeof(BaseRelocationBlock) + nb_of_relocs * sizeof(uint16_t = RelocationEntry)``
uint32_t block_size() const;
uint32_t block_size() const {
return block_size_;
}

//! Iterator over the RelocationEntry
it_const_entries entries() const;
it_entries entries();

void virtual_address(uint32_t virtual_address);
void block_size(uint32_t block_size);
it_const_entries entries() const {
return entries_;
}
it_entries entries() {
return entries_;
}

void virtual_address(uint32_t virtual_address) {
virtual_address_ = virtual_address;
}
void block_size(uint32_t block_size) {
block_size_ = block_size;
}

RelocationEntry& add_entry(const RelocationEntry& entry);

Expand Down
Loading

0 comments on commit ada2528

Please sign in to comment.