Skip to content

Commit

Permalink
Re-scope ELF enums
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Jan 28, 2024
1 parent 6b49ec5 commit 2a6e3a2
Show file tree
Hide file tree
Showing 192 changed files with 8,772 additions and 11,789 deletions.
9 changes: 4 additions & 5 deletions api/c/ELF/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace LIEF {
namespace ELF {
void init_c_binary(Elf_Binary_t* c_binary, Binary* binary) {
c_binary->handler = reinterpret_cast<void*>(binary);
c_binary->type = static_cast<enum LIEF_ELF_ELF_CLASS>(binary->type());
c_binary->type = static_cast<uint32_t>(binary->type());
c_binary->interpreter = nullptr;
if (binary->has_interpreter()) {
const std::string& interp = binary->interpreter();
Expand All @@ -48,7 +48,6 @@ void init_c_binary(Elf_Binary_t* c_binary, Binary* binary) {
reinterpret_cast<char*>(const_cast<char*>(c_binary->interpreter))[interp.size()] = '\0';
}


init_c_header(c_binary, binary);
init_c_sections(c_binary, binary);
init_c_segments(c_binary, binary);
Expand Down Expand Up @@ -81,9 +80,9 @@ Elf_Binary_t* elf_parse(const char *file) {
int elf_binary_save_header(Elf_Binary_t* binary) {
Header& hdr = reinterpret_cast<Binary*>(binary->handler)->header();

hdr.file_type(static_cast<LIEF::ELF::E_TYPE>(binary->header.file_type));
hdr.machine_type(static_cast<LIEF::ELF::ARCH>(binary->header.machine_type));
hdr.object_file_version(static_cast<LIEF::ELF::VERSION>(binary->header.object_file_version));
hdr.file_type(LIEF::ELF::Header::FILE_TYPE(binary->header.file_type));
hdr.machine_type(LIEF::ELF::ARCH(binary->header.machine_type));
hdr.object_file_version(LIEF::ELF::Header::VERSION(binary->header.object_file_version));
hdr.program_headers_offset(binary->header.program_headers_offset);
hdr.section_headers_offset(binary->header.section_headers_offset);
hdr.processor_flag(binary->header.processor_flags);
Expand Down
1 change: 0 additions & 1 deletion api/c/ELF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ target_sources(LIB_LIEF PRIVATE
DynamicEntry.cpp
Header.cpp
Binary.cpp
EnumToString.cpp
utils.cpp
)
85 changes: 28 additions & 57 deletions api/c/ELF/DynamicEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,69 +34,68 @@ 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)));

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

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

case DYNAMIC_TAGS::DT_SONAME:
case DynamicEntry::TAG::SONAME:
{
auto* e = static_cast<Elf_DynamicEntry_SharedObject_t*>(
malloc(sizeof(Elf_DynamicEntry_SharedObject_t)));

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

c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);
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->tag = static_cast<uint64_t>(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->tag = static_cast<uint64_t>(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)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
e->tag = static_cast<uint64_t>(entry.tag());
e->value = entry.value();
const std::vector<uint64_t>& array = reinterpret_cast<DynamicEntryArray*>(&entry)->array();
e->array = static_cast<uint64_t*>(malloc((array.size() + 1) * sizeof(uint64_t)));
Expand All @@ -109,55 +108,33 @@ 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->tag = static_cast<uint64_t>(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->tag = static_cast<uint64_t>(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;
}




default:
{
c_binary->dynamic_entries[i] =
Expand All @@ -170,7 +147,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 +155,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 All @@ -244,5 +217,3 @@ void destroy_dynamic_entries(Elf_Binary_t* c_binary) {

}
}


80 changes: 0 additions & 80 deletions api/c/ELF/EnumToString.cpp

This file was deleted.

11 changes: 4 additions & 7 deletions api/c/ELF/Header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace ELF {
void init_c_header(Elf_Binary_t* c_binary, Binary* binary) {

const Header& hdr = binary->header();
c_binary->header.file_type = static_cast<enum LIEF_ELF_E_TYPE>(hdr.file_type());
c_binary->header.machine_type = static_cast<enum LIEF_ELF_ARCH>(hdr.machine_type());
c_binary->header.object_file_version = static_cast<enum LIEF_ELF_VERSION>(hdr.object_file_version());
c_binary->header.file_type = static_cast<uint32_t>(hdr.file_type());
c_binary->header.machine_type = static_cast<uint32_t>(hdr.machine_type());
c_binary->header.object_file_version = static_cast<uint32_t>(hdr.object_file_version());
c_binary->header.program_headers_offset = hdr.program_headers_offset();
c_binary->header.section_headers_offset = hdr.section_headers_offset();
c_binary->header.processor_flags = hdr.processor_flag();
Expand All @@ -35,10 +35,7 @@ void init_c_header(Elf_Binary_t* c_binary, Binary* binary) {
c_binary->header.name_string_table_idx = hdr.section_name_table_idx();
c_binary->header.entrypoint = hdr.entrypoint();
const Header::identity_t& ident = hdr.identity();
std::copy(
std::begin(ident),
std::end(ident),
c_binary->header.identity);
std::copy(std::begin(ident), std::end(ident), c_binary->header.identity);

}

Expand Down
2 changes: 1 addition & 1 deletion api/c/ELF/Section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void init_c_sections(Elf_Binary_t* c_binary, Binary* binary) {

c_binary->sections[i]->name = b_section.fullname().c_str();
c_binary->sections[i]->flags = b_section.flags();
c_binary->sections[i]->type = static_cast<enum LIEF_ELF_ELF_SECTION_TYPES>(b_section.type());
c_binary->sections[i]->type = static_cast<uint32_t>(b_section.type());
c_binary->sections[i]->virtual_address = b_section.virtual_address();
c_binary->sections[i]->offset = b_section.file_offset();
c_binary->sections[i]->original_size = b_section.original_size();
Expand Down
5 changes: 1 addition & 4 deletions api/c/ELF/Segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void init_c_segments(Elf_Binary_t* c_binary, Binary* binary) {
span<const uint8_t> segment_content = segment.content();

c_binary->segments[i] = static_cast<Elf_Segment_t*>(malloc(sizeof(Elf_Segment_t)));
c_binary->segments[i]->type = static_cast<enum LIEF_ELF_SEGMENT_TYPES>(segment.type());
c_binary->segments[i]->type = static_cast<uint32_t>(segment.type());
c_binary->segments[i]->flags = static_cast<uint32_t>(segment.flags());
c_binary->segments[i]->virtual_address = segment.virtual_address();
c_binary->segments[i]->virtual_size = segment.virtual_size();
Expand All @@ -44,10 +44,7 @@ void init_c_segments(Elf_Binary_t* c_binary, Binary* binary) {

}



void destroy_segments(Elf_Binary_t* c_binary) {

Elf_Segment_t **segments = c_binary->segments;
for (size_t idx = 0; segments[idx] != nullptr; ++idx) {
free(segments[idx]);
Expand Down
Loading

0 comments on commit 2a6e3a2

Please sign in to comment.