Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ELF ABI Version
  • Loading branch information
romainthomas committed Dec 30, 2018
1 parent f20fa5e commit 8d7ec26
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
26 changes: 24 additions & 2 deletions api/python/ELF/objects/pyHeader.cpp
Expand Up @@ -61,11 +61,33 @@ void create<Header>(py::module& m) {
"Return header's " RST_CLASS_REF(lief.ELF.OS_ABI) "."
)

.def_property_readonly("identity",
.def_property("identity_abi_version",
static_cast<getter_t<uint32_t>>(&Header::identity_abi_version),
static_cast<setter_t<uint32_t>>(&Header::identity_abi_version),
"Return ABI version (integer)."
)

.def_property("identity",
static_cast<Header::identity_t& (Header::*)(void)>(&Header::identity),
[] (Header& header, const py::object& obj) {
if (py::isinstance<py::str>(obj)) {
header.identity(obj.cast<std::string>());
return;
}

if (py::isinstance<py::list>(obj)) {
header.identity(obj.cast<Header::identity_t>());
return;
}

std::string error_str = py::repr(obj).cast<std::string>();
error_str = error_str + " is not supported!";
throw py::type_error(error_str.c_str());

},
"Return header's identity.",
py::return_value_policy::reference_internal
)
)

.def_property("file_type",
static_cast<getter_t<E_TYPE>>(&Header::file_type),
Expand Down
3 changes: 2 additions & 1 deletion examples/python/elf_reader.py
Expand Up @@ -74,12 +74,13 @@ def print_header(binary):

if header.machine_type == lief.ELF.ARCH.HEXAGON:
eflags_str = " - ".join([str(s).split(".")[-1] for s in header.hexagon_flags_list])

print(identity)
print(format_ide.format("Magic:", identity[0], identity[1], identity[2], identity[3]))
print(format_str.format("Class:", str(header.identity_class).split(".")[-1]))
print(format_str.format("Endianness:", str(header.identity_data).split(".")[-1]))
print(format_str.format("Version:", str(header.identity_version).split(".")[-1]))
print(format_str.format("OS/ABI:", str(header.identity_os_abi).split(".")[-1]))
print(format_dec.format("ABI Version:", header.identity_abi_version))
print(format_str.format("File Type:", str(header.file_type).split(".")[-1]))
print(format_str.format("Machine Type:", str(header.machine_type).split(".")[-1]))
print(format_str.format("Object File Version:", str(header.object_file_version).split(".")[-1]))
Expand Down
4 changes: 4 additions & 0 deletions include/LIEF/ELF/Header.hpp
Expand Up @@ -151,6 +151,9 @@ class LIEF_API Header : public Object {
//! @brief Identifies the version of the ABI for which the object is prepared
OS_ABI identity_os_abi(void) const;

//! @brief ABI Version
uint32_t identity_abi_version(void) const;

void file_type(E_TYPE type);
void machine_type(ARCH machineType);
void object_file_version(VERSION version);
Expand All @@ -170,6 +173,7 @@ class LIEF_API Header : public Object {
void identity_data(ELF_DATA data);
void identity_version(VERSION version);
void identity_os_abi(OS_ABI osabi);
void identity_abi_version(uint32_t version);

virtual void accept(Visitor& visitor) const override;

Expand Down
9 changes: 9 additions & 0 deletions src/ELF/Header.cpp
Expand Up @@ -422,6 +422,10 @@ OS_ABI Header::identity_os_abi(void) const {
return static_cast<OS_ABI>(this->identity_[static_cast<size_t>(IDENTITY::EI_OSABI)]);
}

uint32_t Header::identity_abi_version(void) const {
return static_cast<uint32_t>(this->identity_[static_cast<size_t>(IDENTITY::EI_ABIVERSION)]);
}

void Header::file_type(E_TYPE type) {
this->file_type_ = type;
}
Expand Down Expand Up @@ -517,6 +521,10 @@ void Header::identity_os_abi(OS_ABI osabi) {
this->identity_[static_cast<size_t>(IDENTITY::EI_OSABI)] = static_cast<uint8_t>(osabi);
}

void Header::identity_abi_version(uint32_t version) {
this->identity_[static_cast<size_t>(IDENTITY::EI_ABIVERSION)] = static_cast<uint8_t>(version);
}


void Header::accept(LIEF::Visitor& visitor) const {
visitor.visit(*this);
Expand Down Expand Up @@ -597,6 +605,7 @@ std::ostream& operator<<(std::ostream& os, const Header& hdr)
os << std::setw(33) << std::setfill(' ') << "Endianness:" << to_string(hdr.identity_data()) << std::endl;
os << std::setw(33) << std::setfill(' ') << "Version:" << to_string(hdr.identity_version()) << std::endl;
os << std::setw(33) << std::setfill(' ') << "OS/ABI:" << to_string(hdr.identity_os_abi()) << std::endl;
os << std::setw(33) << std::setfill(' ') << "ABI Version:" << std::dec << hdr.identity_abi_version() << std::endl;
os << std::setw(33) << std::setfill(' ') << "Machine type:" << to_string(hdr.machine_type()) << std::endl;
os << std::setw(33) << std::setfill(' ') << "File type:" << to_string(hdr.file_type()) << std::endl;
os << std::setw(33) << std::setfill(' ') << "Object file version:" << to_string(hdr.object_file_version()) << std::endl;
Expand Down
1 change: 1 addition & 0 deletions src/ELF/json.cpp
Expand Up @@ -195,6 +195,7 @@ void JsonVisitor::visit(const Header& header) {
this->node_["identity_data"] = to_string(header.identity_data());
this->node_["identity_version"] = to_string(header.identity_version());
this->node_["identity_os_abi"] = to_string(header.identity_os_abi());
this->node_["identity_abi_version"] = header.identity_abi_version();
}


Expand Down

0 comments on commit 8d7ec26

Please sign in to comment.