Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Jan 22, 2024
1 parent bb2bd6c commit 5841680
Show file tree
Hide file tree
Showing 66 changed files with 2,612 additions and 1,888 deletions.
66 changes: 21 additions & 45 deletions api/c/ELF/DynamicEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
for (size_t i = 0; i < dyn_entries.size(); ++i) {
DynamicEntry& entry = dyn_entries[i];
switch(entry.tag()) {
case DYNAMIC_TAGS::DT_NEEDED:
case DynamicEntry::TAG::NEEDED:
{

auto* e = static_cast<Elf_DynamicEntry_Library_t*>(
malloc(sizeof(Elf_DynamicEntry_Library_t)));

Expand All @@ -48,7 +47,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
break;
}

case DYNAMIC_TAGS::DT_SONAME:
case DynamicEntry::TAG::SONAME:
{
auto* e = static_cast<Elf_DynamicEntry_SharedObject_t*>(
malloc(sizeof(Elf_DynamicEntry_SharedObject_t)));
Expand All @@ -61,37 +60,37 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
break;
}

case DYNAMIC_TAGS::DT_RPATH:
case DynamicEntry::TAG::RPATH:
{
auto* e = static_cast<Elf_DynamicEntry_Rpath_t*>(
malloc(sizeof(Elf_DynamicEntry_Rpath_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
e->value = entry.value();
e->rpath = reinterpret_cast<DynamicEntryRpath*>(&entry)->name().c_str();
e->rpath = reinterpret_cast<DynamicEntryRpath*>(&entry)->rpath().c_str();

c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);

break;
}

case DYNAMIC_TAGS::DT_RUNPATH:
case DynamicEntry::TAG::RUNPATH:
{
auto* e = static_cast<Elf_DynamicEntry_RunPath_t*>(
malloc(sizeof(Elf_DynamicEntry_RunPath_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
e->value = entry.value();
e->runpath = reinterpret_cast<DynamicEntryRunPath*>(&entry)->name().c_str();
e->runpath = reinterpret_cast<DynamicEntryRunPath*>(&entry)->runpath().c_str();

c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);

break;
}

case DYNAMIC_TAGS::DT_INIT_ARRAY:
case DYNAMIC_TAGS::DT_FINI_ARRAY:
case DYNAMIC_TAGS::DT_PREINIT_ARRAY:
case DynamicEntry::TAG::INIT_ARRAY:
case DynamicEntry::TAG::FINI_ARRAY:
case DynamicEntry::TAG::PREINIT_ARRAY:
{
auto* e = static_cast<Elf_DynamicEntry_Array_t*>(
malloc(sizeof(Elf_DynamicEntry_Array_t)));
Expand All @@ -109,47 +108,28 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
break;
}

case DYNAMIC_TAGS::DT_FLAGS:
case DynamicEntry::TAG::FLAGS:
{
auto* e = static_cast<Elf_DynamicEntry_Flags_t*>(
malloc(sizeof(Elf_DynamicEntry_Flags_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
e->value = entry.value();
const DynamicEntryFlags::flags_list_t& flags = reinterpret_cast<DynamicEntryFlags*>(&entry)->flags();
e->flags = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS*>(malloc((flags.size() + 1) * sizeof(enum LIEF_ELF_DYNAMIC_FLAGS)));
e->flags_1 = nullptr;

auto it = std::begin(flags);

for (size_t i = 0; it != std::end(flags); ++i, ++it) {
e->flags[i] = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS>(*it);
}

e->flags[flags.size()] = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS>(0);
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);

break;
}

case DYNAMIC_TAGS::DT_FLAGS_1:
case DynamicEntry::TAG::FLAGS_1:
{
auto* e = static_cast<Elf_DynamicEntry_Flags_t*>(
malloc(sizeof(Elf_DynamicEntry_Flags_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
e->value = entry.value();
const DynamicEntryFlags::flags_list_t& flags = reinterpret_cast<DynamicEntryFlags*>(&entry)->flags();
e->flags_1 = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS_1*>(malloc((flags.size() + 1) * sizeof(enum LIEF_ELF_DYNAMIC_FLAGS_1)));
e->flags = nullptr;

auto it = std::begin(flags);

for (size_t i = 0; it != std::end(flags); ++i, ++it) {
e->flags_1[i] = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS_1>(*it);
}

e->flags_1[flags.size()] = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS_1>(0);
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);

break;
Expand All @@ -170,7 +150,6 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
}

c_binary->dynamic_entries[dyn_entries.size()] = nullptr;

}


Expand All @@ -179,59 +158,56 @@ void destroy_dynamic_entries(Elf_Binary_t* c_binary) {

Elf_DynamicEntry_t **dynamic_entries = c_binary->dynamic_entries;
for (size_t idx = 0; dynamic_entries[idx] != nullptr; ++idx) {
switch(static_cast<DYNAMIC_TAGS>(dynamic_entries[idx]->tag)) {
case DYNAMIC_TAGS::DT_NEEDED:
switch(DynamicEntry::TAG(dynamic_entries[idx]->tag)) {
case DynamicEntry::TAG::NEEDED:
{
free(reinterpret_cast<Elf_DynamicEntry_Library_t*>(dynamic_entries[idx]));
break;
}

case DYNAMIC_TAGS::DT_SONAME:
case DynamicEntry::TAG::SONAME:
{
free(reinterpret_cast<Elf_DynamicEntry_SharedObject_t*>(dynamic_entries[idx]));
break;
}

case DYNAMIC_TAGS::DT_RPATH:
case DynamicEntry::TAG::RPATH:
{
free(reinterpret_cast<Elf_DynamicEntry_Rpath_t*>(dynamic_entries[idx]));
break;
}

case DYNAMIC_TAGS::DT_RUNPATH:
case DynamicEntry::TAG::RUNPATH:
{
free(reinterpret_cast<Elf_DynamicEntry_RunPath_t*>(dynamic_entries[idx]));
break;
}

case DYNAMIC_TAGS::DT_INIT_ARRAY:
case DYNAMIC_TAGS::DT_FINI_ARRAY:
case DYNAMIC_TAGS::DT_PREINIT_ARRAY:
case DynamicEntry::TAG::INIT_ARRAY:
case DynamicEntry::TAG::FINI_ARRAY:
case DynamicEntry::TAG::PREINIT_ARRAY:
{
Elf_DynamicEntry_Array_t* entry_array=reinterpret_cast<Elf_DynamicEntry_Array_t*>(dynamic_entries[idx]);
free(entry_array->array);
free(entry_array);
break;
}

case DYNAMIC_TAGS::DT_FLAGS:
case DynamicEntry::TAG::FLAGS:
{
Elf_DynamicEntry_Flags_t* entry_flags=reinterpret_cast<Elf_DynamicEntry_Flags_t*>(dynamic_entries[idx]);
free(entry_flags->flags);
free(entry_flags);
break;
}
case DYNAMIC_TAGS::DT_FLAGS_1:
case DynamicEntry::TAG::FLAGS_1:
{
Elf_DynamicEntry_Flags_t* entry_flags=reinterpret_cast<Elf_DynamicEntry_Flags_t*>(dynamic_entries[idx]);
free(entry_flags->flags_1);
free(entry_flags);
break;
}

default:
{

free(dynamic_entries[idx]);
}

Expand Down
13 changes: 3 additions & 10 deletions api/c/ELF/EnumToString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
#include "LIEF/ELF/EnumToString.h"
#include "LIEF/ELF/EnumToString.hpp"
#include "LIEF/ELF/DynamicEntry.hpp"
#include "LIEF/ELF/DynamicEntryFlags.hpp"

#include "LIEF/ELF/enums.h"
#include "LIEF/ELF/enums.hpp"
Expand Down Expand Up @@ -42,7 +44,7 @@ extern "C"
}

const char* DYNAMIC_TAGS_to_string(enum LIEF_ELF_DYNAMIC_TAGS e) {
return LIEF::ELF::to_string(static_cast<LIEF::ELF::DYNAMIC_TAGS>(e));
return LIEF::ELF::to_string(LIEF::ELF::DynamicEntry::TAG(e));
}

const char* ELF_SECTION_TYPES_to_string(enum LIEF_ELF_ELF_SECTION_TYPES e) {
Expand All @@ -68,13 +70,4 @@ extern "C"
const char* OS_ABI_to_string(enum LIEF_ELF_OS_ABI e) {
return LIEF::ELF::to_string(static_cast<LIEF::ELF::OS_ABI>(e));
}

const char* DYNAMIC_FLAGS_to_string(enum LIEF_ELF_DYNAMIC_FLAGS e) {
return LIEF::ELF::to_string(static_cast<LIEF::ELF::DYNAMIC_FLAGS>(e));
}

const char* DYNAMIC_FLAGS_1_to_string(enum LIEF_ELF_DYNAMIC_FLAGS_1 e) {
return LIEF::ELF::to_string(static_cast<LIEF::ELF::DYNAMIC_FLAGS_1>(e));
}

}
3 changes: 0 additions & 3 deletions api/c/include/LIEF/ELF/DynamicEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ struct Elf_DynamicEntry_RunPath_t {
const char* runpath;
};


struct Elf_DynamicEntry_Flags_t {
uint64_t tag;
uint64_t value;
enum LIEF_ELF_DYNAMIC_FLAGS *flags;
enum LIEF_ELF_DYNAMIC_FLAGS_1 *flags_1;
};

typedef struct Elf_DynamicEntry_t Elf_DynamicEntry_t;
Expand Down
2 changes: 0 additions & 2 deletions api/c/include/LIEF/ELF/EnumToString.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ LIEF_API const char* ELF_SYMBOL_TYPES_to_string(enum LIEF_ELF_ELF_SYMBOL_TYPES e
LIEF_API const char* ELF_CLASS_to_string(enum LIEF_ELF_ELF_CLASS e);
LIEF_API const char* ELF_DATA_to_string(enum LIEF_ELF_ELF_DATA e);
LIEF_API const char* OS_ABI_to_string(enum LIEF_ELF_OS_ABI e);
LIEF_API const char* DYNAMIC_FLAGS_to_string(enum LIEF_ELF_DYNAMIC_FLAGS e);
LIEF_API const char* DYNAMIC_FLAGS_1_to_string(enum LIEF_ELF_DYNAMIC_FLAGS_1 e);

#ifdef __cplusplus
}
Expand Down
14 changes: 7 additions & 7 deletions api/python/examples/elf_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,18 @@ def print_dynamic_entries(binary):
f_value = "|{:<16} | 0x{:<8x}| {:<20}|"
print(f_title.format("Tag", "Value", "Info"))
for entry in dynamic_entries:
if entry.tag == ELF.DYNAMIC_TAGS.NULL:
if entry.tag == ELF.DynamicEntry.TAG.NULL:
continue

if entry.tag in [ELF.DYNAMIC_TAGS.SONAME, ELF.DYNAMIC_TAGS.NEEDED, ELF.DYNAMIC_TAGS.RUNPATH, ELF.DYNAMIC_TAGS.RPATH]:
if entry.tag in [ELF.DynamicEntry.TAG.SONAME, ELF.DynamicEntry.TAG.NEEDED, ELF.DynamicEntry.TAG.RUNPATH, ELF.DynamicEntry.TAG.RPATH]:
print(f_value.format(str(entry.tag).split(".")[-1], entry.value, entry.name))
elif type(entry) is ELF.DynamicEntryArray: # [ELF.DYNAMIC_TAGS.INIT_ARRAY,ELF.DYNAMIC_TAGS.FINI_ARRAY]:
elif type(entry) is ELF.DynamicEntryArray: # [ELF.DynamicEntry.TAG.INIT_ARRAY,ELF.DynamicEntry.TAG.FINI_ARRAY]:
print(f_value.format(str(entry.tag).split(".")[-1], entry.value, ", ".join(map(hex, entry.array))))
elif entry.tag == ELF.DYNAMIC_TAGS.FLAGS:
flags_str = " - ".join([str(ELF.DYNAMIC_FLAGS(s)).split(".")[-1] for s in entry.flags])
elif entry.tag == ELF.DynamicEntry.TAG.FLAGS:
flags_str = " - ".join([str(ELF.DynamicEntryFlags.FLAG(s)).split(".")[-1] for s in entry.flags])
print(f_value.format(str(entry.tag).split(".")[-1], entry.value, flags_str))
elif entry.tag == ELF.DYNAMIC_TAGS.FLAGS_1:
flags_str = " - ".join([str(ELF.DYNAMIC_FLAGS_1(s)).split(".")[-1] for s in entry.flags])
elif entry.tag == ELF.DynamicEntry.TAG.FLAGS_1:
flags_str = " - ".join([str(ELF.DynamicEntryFlags.FLAG(s)).split(".")[-1] for s in entry.flags])
print(f_value.format(str(entry.tag).split(".")[-1], entry.value, flags_str))
else:
print(f_value.format(str(entry.tag).split(".")[-1], entry.value, ""))
Expand Down
7 changes: 7 additions & 0 deletions api/python/lief/ART.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class STORAGE_MODES:
UNCOMPRESSED: ClassVar[STORAGE_MODES] = ...
__name__: Any
def __init__(self, *args, **kwargs) -> None: ...
def __ge__(self, other) -> bool: ...
def __gt__(self, other) -> bool: ...
def __hash__(self) -> int: ...
def __index__(self) -> Any: ...
def __int__(self) -> int: ...
def __le__(self, other) -> bool: ...
def __lt__(self, other) -> bool: ...

def android_version(art_version: int) -> lief.Android.ANDROID_VERSIONS: ...
@overload
Expand Down
7 changes: 7 additions & 0 deletions api/python/lief/Android.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class ANDROID_VERSIONS:
def __init__(self, *args, **kwargs) -> None: ...
@staticmethod
def from_value(arg: int, /) -> lief.Android.ANDROID_VERSIONS: ...
def __ge__(self, other) -> bool: ...
def __gt__(self, other) -> bool: ...
def __hash__(self) -> int: ...
def __index__(self) -> Any: ...
def __int__(self) -> int: ...
def __le__(self, other) -> bool: ...
def __lt__(self, other) -> bool: ...
@property
def value(self) -> int: ...

Expand Down
28 changes: 28 additions & 0 deletions api/python/lief/DEX.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class ACCESS_FLAGS:
VOLATILE: ClassVar[ACCESS_FLAGS] = ...
__name__: Any
def __init__(self, *args, **kwargs) -> None: ...
def __ge__(self, other) -> bool: ...
def __gt__(self, other) -> bool: ...
def __hash__(self) -> int: ...
def __index__(self) -> Any: ...
def __int__(self) -> int: ...
def __le__(self, other) -> bool: ...
def __lt__(self, other) -> bool: ...

class Class(lief.Object):
class it_fields:
Expand Down Expand Up @@ -249,6 +256,13 @@ class MapItem(lief.Object):
def __init__(self, *args, **kwargs) -> None: ...
@staticmethod
def from_value(arg: int, /) -> lief.DEX.MapItem.TYPES: ...
def __ge__(self, other) -> bool: ...
def __gt__(self, other) -> bool: ...
def __hash__(self) -> int: ...
def __index__(self) -> Any: ...
def __int__(self) -> int: ...
def __le__(self, other) -> bool: ...
def __lt__(self, other) -> bool: ...
@property
def value(self) -> int: ...
def __init__(self, *args, **kwargs) -> None: ...
Expand Down Expand Up @@ -322,6 +336,13 @@ class Type(lief.Object):
VOID_T: ClassVar[Type.PRIMITIVES] = ...
__name__: Any
def __init__(self, *args, **kwargs) -> None: ...
def __ge__(self, other) -> bool: ...
def __gt__(self, other) -> bool: ...
def __hash__(self) -> int: ...
def __index__(self) -> Any: ...
def __int__(self) -> int: ...
def __le__(self, other) -> bool: ...
def __lt__(self, other) -> bool: ...

class TYPES:
ARRAY: ClassVar[Type.TYPES] = ...
Expand All @@ -330,6 +351,13 @@ class Type(lief.Object):
UNKNOWN: ClassVar[Type.TYPES] = ...
__name__: Any
def __init__(self, *args, **kwargs) -> None: ...
def __ge__(self, other) -> bool: ...
def __gt__(self, other) -> bool: ...
def __hash__(self) -> int: ...
def __index__(self) -> Any: ...
def __int__(self) -> int: ...
def __le__(self, other) -> bool: ...
def __lt__(self, other) -> bool: ...
def __init__(self, *args, **kwargs) -> None: ...
@staticmethod
def pretty_name(primitive: lief.DEX.Type.PRIMITIVES) -> str: ...
Expand Down

0 comments on commit 5841680

Please sign in to comment.