Skip to content
Permalink
Browse files
Introduce Mach-O Build Version command
New API:
  LIEF::MachO::BuildVersion
  LIEF::MachO::BuildToolVersion

  LIEF::MachO::Binary::has_build_version
  LIEF::MachO::Binary::build_version
  • Loading branch information
romainthomas committed Dec 13, 2018
1 parent 2c6f052 commit 6f96723
Show file tree
Hide file tree
Showing 28 changed files with 741 additions and 5 deletions.
@@ -33,6 +33,7 @@ set(LIEF_PYTHON_MACHO_SRC
"${CMAKE_CURRENT_LIST_DIR}/objects/pySubFramework.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyDyldEnvironment.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyEncryptionInfo.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyBuildVersion.cpp"
"${CMAKE_CURRENT_LIST_DIR}/pyEnums.cpp"
)

@@ -300,6 +300,16 @@ void create<Binary>(py::module& m) {
"Return binary's " RST_CLASS_REF(lief.MachO.EncryptionInfo) " if any.",
py::return_value_policy::reference)

.def_property_readonly("has_build_version",
&Binary::has_build_version,
"``True`` if the binary has a " RST_CLASS_REF(lief.MachO.BuildVersion) " command",
py::return_value_policy::reference_internal)

.def_property_readonly("build_version",
static_cast<no_const_getter<BuildVersion&>>(&Binary::build_version),
"Return binary's " RST_CLASS_REF(lief.MachO.BuildVersion) " if any.",
py::return_value_policy::reference)


.def("virtual_address_to_offset",
&Binary::virtual_address_to_offset,
@@ -0,0 +1,128 @@
/* Copyright 2017 R. Thomas
* Copyright 2017 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>

#include <string>
#include <sstream>

#include "LIEF/MachO/hash.hpp"
#include "LIEF/MachO/BuildVersion.hpp"
#include "LIEF/MachO/EnumToString.hpp"

#include "enums_wrapper.hpp"

#include "pyMachO.hpp"

#define PY_ENUM(x) LIEF::MachO::to_string(x), x

namespace LIEF {
namespace MachO {

template<class T>
using getter_t = T (BuildVersion::*)(void) const;

template<class T>
using setter_t = void (BuildVersion::*)(T);


template<>
void create<BuildVersion>(py::module& m) {

py::class_<BuildVersion, LoadCommand> cls(m, "BuildVersion");
py::class_<BuildToolVersion, LIEF::Object> tool_version_cls(m, "BuildToolVersion");


// Build Tool Version
// ==================
tool_version_cls
.def_property_readonly("tool",
&BuildToolVersion::tool,
"" RST_CLASS_REF(.BuildeVersion.TOOLS) " type")

.def_property_readonly("version",
&BuildToolVersion::version,
"Version of the tool")

.def("__eq__", &BuildToolVersion::operator==)
.def("__ne__", &BuildToolVersion::operator!=)
.def("__hash__",
[] (const BuildToolVersion& version) {
return Hash::hash(version);
})

.def("__str__",
[] (const BuildToolVersion& version)
{
std::ostringstream stream;
stream << version;
return stream.str();
});


LIEF::enum_<BuildToolVersion::TOOLS>(tool_version_cls, "TOOLS")
.value(PY_ENUM(BuildToolVersion::TOOLS::UNKNOWN))
.value(PY_ENUM(BuildToolVersion::TOOLS::CLANG))
.value(PY_ENUM(BuildToolVersion::TOOLS::SWIFT))
.value(PY_ENUM(BuildToolVersion::TOOLS::LD));

cls

.def_property("platform",
static_cast<getter_t<BuildVersion::PLATFORMS>>(&BuildVersion::platform),
static_cast<setter_t<BuildVersion::PLATFORMS>>(&BuildVersion::platform),
"Target " RST_CLASS_REF(.BuildVersion.PLATFORMS) "")

.def_property("minos",
static_cast<getter_t<BuildVersion::version_t>>(&BuildVersion::minos),
static_cast<setter_t<BuildVersion::version_t>>(&BuildVersion::minos),
"Minimal OS version on which this binary was built to run")

.def_property("sdk",
static_cast<getter_t<BuildVersion::version_t>>(&BuildVersion::minos),
static_cast<setter_t<BuildVersion::version_t>>(&BuildVersion::minos),
"SDK Version")

.def_property_readonly("tools",
static_cast<getter_t<BuildVersion::tools_list_t>>(&BuildVersion::tools),
"List of " RST_CLASS_REF(BuildToolVersion) " used when while this binary")

.def("__eq__", &BuildVersion::operator==)
.def("__ne__", &BuildVersion::operator!=)
.def("__hash__",
[] (const BuildVersion& version) {
return Hash::hash(version);
})

.def("__str__",
[] (const BuildVersion& version)
{
std::ostringstream stream;
stream << version;
return stream.str();
});


LIEF::enum_<BuildVersion::PLATFORMS>(cls, "PLATFORMS")
.value(PY_ENUM(BuildVersion::PLATFORMS::UNKNOWN))
.value(PY_ENUM(BuildVersion::PLATFORMS::MACOS))
.value(PY_ENUM(BuildVersion::PLATFORMS::IOS))
.value(PY_ENUM(BuildVersion::PLATFORMS::TVOS))
.value(PY_ENUM(BuildVersion::PLATFORMS::WATCHOS));

}

}
}
@@ -138,7 +138,9 @@ void init_enums(py::module& m) {
.value(PY_ENUM(LIEF::MachO::LOAD_COMMAND_TYPES::LC_LINKER_OPTION))
.value(PY_ENUM(LIEF::MachO::LOAD_COMMAND_TYPES::LC_LINKER_OPTIMIZATION_HINT))
.value(PY_ENUM(LIEF::MachO::LOAD_COMMAND_TYPES::LC_VERSION_MIN_TVOS))
.value(PY_ENUM(LIEF::MachO::LOAD_COMMAND_TYPES::LC_VERSION_MIN_WATCHOS));
.value(PY_ENUM(LIEF::MachO::LOAD_COMMAND_TYPES::LC_VERSION_MIN_WATCHOS))
.value(PY_ENUM(LIEF::MachO::LOAD_COMMAND_TYPES::LC_NOTE))
.value(PY_ENUM(LIEF::MachO::LOAD_COMMAND_TYPES::LC_BUILD_VERSION));


LIEF::enum_<LIEF::MachO::MACHO_SECTION_TYPES>(m, "SECTION_TYPES")
@@ -64,6 +64,7 @@ void init_objects(py::module& m) {
CREATE(SubFramework, m);
CREATE(DyldEnvironment, m);
CREATE(EncryptionInfo, m);
CREATE(BuildVersion, m);
}

}
@@ -77,6 +77,7 @@ SPECIALIZE_CREATE(SegmentSplitInfo);
SPECIALIZE_CREATE(SubFramework);
SPECIALIZE_CREATE(DyldEnvironment);
SPECIALIZE_CREATE(EncryptionInfo);
SPECIALIZE_CREATE(BuildVersion);

}
}
@@ -285,6 +285,24 @@ Encryption Info

----------


Build Version
*************

.. doxygenclass:: LIEF::MachO::BuildVersion
:project: lief

----------


Build Tool Version
******************

.. doxygenclass:: LIEF::MachO::BuildToolVersion
:project: lief

----------

Utilities
*********

@@ -353,6 +353,27 @@ Encryption Info

----------

Build Version
*************

.. autoclass:: lief.MachO.BuildVersion
:members:
:inherited-members:
:undoc-members:

----------

Build Tool Version
******************

.. autoclass:: lief.MachO.BuildToolVersion
:members:
:inherited-members:
:undoc-members:

----------




Enums
@@ -672,7 +672,22 @@ def print_functions(binary):
for idx, f in enumerate(binary.functions):
print(" [{:d}] {}: 0x{:x}".format(idx, f.name, f.address))

@exceptions_handler(Exception)
def print_build_version(binary):
print("== Build Version ==\n")

build_version = binary.build_version

print("Platform: {}".format(str(build_version.platform).split(".")[-1]))
print("Min OS: {:d}.{:d}.{:d}".format(*build_version.minos))
print("SDK: {:d}.{:d}.{:d}".format(*build_version.sdk))

tools = build_version.tools
if len(tools) > 0:
print("~~ Tools ({}) ~~".format(len(tools)))
for tool in tools:
tool_str = str(tool.tool).split(".")[-1]
print(" {} - {}.{}.{}".format(tool_str, *tool.version))

def main():
parser = argparse.ArgumentParser(usage='%(prog)s [options] <macho-file>')
@@ -808,6 +823,10 @@ def main():
action='store_true', dest='show_functions',
help='All functions found in the binary')

parser.add_argument('--build-version',
action='store_true', dest='show_build_version',
help='Show build version')

parser.add_argument("binary",
metavar="<macho-file>",
help='Target Mach-O File')
@@ -924,6 +943,9 @@ def main():
if args.show_functions or args.show_all:
print_functions(binary)

if (args.show_build_version or args.show_all) and binary.has_build_version:
print_build_version(binary)

sys.exit(EXIT_STATUS)


@@ -47,6 +47,7 @@
#include "LIEF/MachO/SubFramework.hpp"
#include "LIEF/MachO/DyldEnvironment.hpp"
#include "LIEF/MachO/EncryptionInfo.hpp"
#include "LIEF/MachO/BuildVersion.hpp"

namespace LIEF {
namespace MachO {
@@ -403,6 +404,13 @@ class LIEF_API Binary : public LIEF::Binary {
DyldEnvironment& dyld_environment(void);
const DyldEnvironment& dyld_environment(void) const;

//! ``true`` if the binary has Build Version command.
bool has_build_version(void) const;

//! Return the MachO::BuildVersion
BuildVersion& build_version(void);
const BuildVersion& build_version(void) const;

template<class T>
LIEF_LOCAL bool has_command(void) const;

0 comments on commit 6f96723

Please sign in to comment.