Skip to content

Commit

Permalink
libelfmaster: Basic ELF binary loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamled committed Apr 9, 2019
1 parent 456ef9b commit 4992c94
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions ch4/inc/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
#include <bfd.h>
#include "loader.hpp"

extern "C" {
#include <libelfmaster.h>
}

static int load_binary_bfd(std::string &fname, Binary *bin, Binary::BinaryType type);

static int load_binary_lem(std::string &fname, Binary *bin);

int
load_binary(std::string &fname, Binary *bin, Binary::BinaryType type)
{
Expand Down Expand Up @@ -293,3 +299,53 @@ load_binary_bfd(std::string &fname, Binary *bin, Binary::BinaryType type)

return ret;
}

static int
load_binary_lem(std::string &fname, Binary *bin)
{
int ret;
elf_error_t error;
elfobj_t obj;

if(elf_open_object(fname.c_str(), &obj, ELF_LOAD_F_FORENSICS, &error) == false) {
return -1;
}

bin->filename = std::string(fname);
bin->type = Binary::BIN_TYPE_ELF;
bin->entry = elf_entry_point(&obj);
// This gets set in the BFD path to the BFD taret name
// which AFAICT is entirely specific to BFD / GNU binutils.
// In the case of loading the binary with libelfmaster just skip it.
bin->type_str = "unknown";


switch(obj.arch) {
case i386:
bin->arch_str = "X86";
bin->arch = Binary::ARCH_X86;
bin->bits = 32;
break;
case x64:
bin->arch_str = "X86_64";
bin->arch = Binary::ARCH_X86;
bin->bits = 64;
break;
case unsupported:
default:
fprintf(stderr, "unsupported architecture (%d)\n",
obj.arch);
goto fail;
}

ret = 0;
goto cleanup;

fail:
ret = -1;

cleanup:
elf_close_object(&obj);

return ret;
}

0 comments on commit 4992c94

Please sign in to comment.