Skip to content

Commit

Permalink
Support new Mach-O command:
Browse files Browse the repository at this point in the history
  * Segment split info
  * Sub framework
  * Dyld environment
  • Loading branch information
romainthomas committed May 14, 2018
1 parent d1a7252 commit 9e3b5b4
Show file tree
Hide file tree
Showing 26 changed files with 947 additions and 50 deletions.
3 changes: 3 additions & 0 deletions api/python/MachO/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ set(LIEF_PYTHON_MACHO_SRC
"${CMAKE_CURRENT_LIST_DIR}/objects/pyParserConfig.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyDynamicSymbolCommand.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyCodeSignature.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pySegmentSplitInfo.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyDataInCode.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyDataCodeEntry.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pySubFramework.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyDyldEnvironment.cpp"
"${CMAKE_CURRENT_LIST_DIR}/pyMachOStructures.cpp"
)

Expand Down
30 changes: 30 additions & 0 deletions api/python/MachO/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,36 @@ void init_MachO_Binary_class(py::module& m) {
"Return binary's " RST_CLASS_REF(lief.MachO.DataInCode) " if any.",
py::return_value_policy::reference)

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

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

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

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

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

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


.def("virtual_address_to_offset",
&Binary::virtual_address_to_offset,
Expand Down
59 changes: 59 additions & 0 deletions api/python/MachO/objects/pyDyldEnvironment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* 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/DyldEnvironment.hpp"

#include "pyMachO.hpp"

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

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


void init_MachO_DyldEnvironment_class(py::module& m) {

py::class_<DyldEnvironment, LoadCommand>(m, "DyldEnvironment")

.def_property("value",
static_cast<getter_t<const std::string&>>(&DyldEnvironment::value),
static_cast<setter_t<const std::string&>>(&DyldEnvironment::value),
"Environment variable as a string",
py::return_value_policy::reference_internal)

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


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

}
64 changes: 64 additions & 0 deletions api/python/MachO/objects/pySegmentSplitInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* 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/SegmentSplitInfo.hpp"

#include "pyMachO.hpp"

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

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


void init_MachO_SegmentSplitInfo_class(py::module& m) {

py::class_<SegmentSplitInfo, LoadCommand>(m, "SegmentSplitInfo")

.def_property("data_offset",
static_cast<getter_t<uint32_t>>(&SegmentSplitInfo::data_offset),
static_cast<setter_t<uint32_t>>(&SegmentSplitInfo::data_offset),
"Offset in the binary where data start")

.def_property("data_size",
static_cast<getter_t<uint32_t>>(&SegmentSplitInfo::data_size),
static_cast<setter_t<uint32_t>>(&SegmentSplitInfo::data_size),
"Size of the raw data")

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


.def("__str__",
[] (const SegmentSplitInfo& func)
{
std::ostringstream stream;
stream << func;
std::string str = stream.str();
return str;
});

}
59 changes: 59 additions & 0 deletions api/python/MachO/objects/pySubFramework.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* 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/SubFramework.hpp"

#include "pyMachO.hpp"

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

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


void init_MachO_SubFramework_class(py::module& m) {

py::class_<SubFramework, LoadCommand>(m, "SubFramework")

.def_property("umbrella",
static_cast<getter_t<const std::string&>>(&SubFramework::umbrella),
static_cast<setter_t<const std::string&>>(&SubFramework::umbrella),
"")

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


.def("__str__",
[] (const SubFramework& func)
{
std::ostringstream stream;
stream << func;
std::string str = stream.str();
return str;
});

}
3 changes: 3 additions & 0 deletions api/python/MachO/pyMachO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void init_MachO_module(py::module& m) {
init_MachO_RPathCommand_class(LIEF_MachO_module);
init_MachO_DynamicSymbolCommand_class(LIEF_MachO_module);
init_MachO_CodeSignature_class(LIEF_MachO_module);
init_MachO_SegmentSplitInfo_class(LIEF_MachO_module);
init_MachO_SubFramework_class(LIEF_MachO_module);
init_MachO_DyldEnvironment_class(LIEF_MachO_module);

init_MachO_DataInCode_class(LIEF_MachO_module);
init_MachO_DataCodeEntry_class(LIEF_MachO_module);
Expand Down
3 changes: 3 additions & 0 deletions api/python/MachO/pyMachO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ void init_MachO_DynamicSymbolCommand_class(py::module&);
void init_MachO_CodeSignature_class(py::module&);
void init_MachO_DataInCode_class(py::module&);
void init_MachO_DataCodeEntry_class(py::module&);
void init_MachO_SegmentSplitInfo_class(py::module&);
void init_MachO_SubFramework_class(py::module&);
void init_MachO_DyldEnvironment_class(py::module&);

// Enums
void init_MachO_Structures_enum(py::module&);
Expand Down
24 changes: 24 additions & 0 deletions doc/sphinx/api/cpp/macho.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,30 @@ Data Code Entry

----------

Segment Split Info
******************

.. doxygenclass:: LIEF::MachO::SegmentSplitInfo
:project: lief

----------

Sub-Framework
*************

.. doxygenclass:: LIEF::MachO::SubFramework
:project: lief

----------

Dyld Environment
****************

.. doxygenclass:: LIEF::MachO::DyldEnvironment
:project: lief

----------

Utilities
*********

Expand Down
31 changes: 30 additions & 1 deletion doc/sphinx/api/python/macho.rst
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ Data Code Entry
:inherited-members:
:undoc-members:


TYPES
~~~~~

Expand All @@ -322,6 +321,36 @@ TYPES

----------

Segment Split Info
******************

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

----------

Sub Framework
*************

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

----------

Dyld Environment
****************

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

----------


Enums
*****
Expand Down
Loading

0 comments on commit 9e3b5b4

Please sign in to comment.