Skip to content

Commit

Permalink
Add Mach-O symbol stubs iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Aug 31, 2024
1 parent 990dbca commit 993f24b
Show file tree
Hide file tree
Showing 26 changed files with 974 additions and 63 deletions.
20 changes: 20 additions & 0 deletions api/python/lief/MachO.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, ClassVar, Iterator, Optional, Union

from typing import overload
import collections.abc
import io
import lief # type: ignore
import lief.MachO # type: ignore
Expand All @@ -20,6 +21,7 @@ import lief.MachO.LoadCommand # type: ignore
import lief.MachO.Relocation # type: ignore
import lief.MachO.Section # type: ignore
import lief.MachO.SegmentCommand # type: ignore
import lief.MachO.Stub # type: ignore
import lief.MachO.Symbol # type: ignore
import lief.MachO.TwoLevelHints # type: ignore
import lief.objc # type: ignore
Expand Down Expand Up @@ -344,6 +346,8 @@ class Binary(lief.Binary):
@property
def symbol_command(self) -> lief.MachO.SymbolCommand: ...
@property
def symbol_stubs(self) -> collections.abc.Sequence[lief.MachO.Stub]: ...
@property
def symbols(self) -> lief.MachO.Binary.it_symbols: ... # type: ignore
@property
def thread_command(self) -> lief.MachO.ThreadCommand: ...
Expand Down Expand Up @@ -1562,6 +1566,22 @@ class SourceVersion(LoadCommand):
version: list[int]
def __init__(self, *args, **kwargs) -> None: ...

class Stub:
class target_info_t:
arch: lief.MachO.Header.CPU_TYPE
subtype: int
@overload
def __init__(self) -> None: ...
@overload
def __init__(self, arg0: lief.MachO.Header.CPU_TYPE, arg1: int, /) -> None: ...
def __init__(self, target_info: lief.MachO.Stub.target_info_t, address: int, raw_stub: list[int]) -> None: ...
@property
def address(self) -> int: ...
@property
def raw(self) -> memoryview: ...
@property
def target(self) -> Union[int,lief.lief_errors]: ...

class SubClient(LoadCommand):
client: str
def __init__(self, *args, **kwargs) -> None: ...
Expand Down
76 changes: 39 additions & 37 deletions api/python/src/MachO/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,55 @@
#include "MachO/enums.hpp"
#include "MachO/pyMachO.hpp"

#include <LIEF/MachO/ParserConfig.hpp>
#include <LIEF/MachO/Parser.hpp>
#include <LIEF/MachO/FatBinary.hpp>
#include <LIEF/MachO/Binary.hpp>
#include <LIEF/MachO/Header.hpp>
#include <LIEF/MachO/LoadCommand.hpp>
#include <LIEF/MachO/UUIDCommand.hpp>
#include <LIEF/MachO/SymbolCommand.hpp>
#include <LIEF/MachO/SegmentCommand.hpp>
#include <LIEF/MachO/Section.hpp>
#include <LIEF/MachO/MainCommand.hpp>
#include <LIEF/MachO/DynamicSymbolCommand.hpp>
#include <LIEF/MachO/DylinkerCommand.hpp>
#include <LIEF/MachO/DyldInfo.hpp>
#include <LIEF/MachO/BindingInfo.hpp>
#include <LIEF/MachO/BuildVersion.hpp>
#include <LIEF/MachO/Builder.hpp>
#include <LIEF/MachO/ChainedBindingInfo.hpp>
#include <LIEF/MachO/CodeSignature.hpp>
#include <LIEF/MachO/CodeSignatureDir.hpp>
#include <LIEF/MachO/DataCodeEntry.hpp>
#include <LIEF/MachO/DataInCode.hpp>
#include <LIEF/MachO/DyldBindingInfo.hpp>
#include <LIEF/MachO/DyldChainedFixups.hpp>
#include <LIEF/MachO/DyldEnvironment.hpp>
#include <LIEF/MachO/DyldExportsTrie.hpp>
#include <LIEF/MachO/DyldInfo.hpp>
#include <LIEF/MachO/DylibCommand.hpp>
#include <LIEF/MachO/ThreadCommand.hpp>
#include <LIEF/MachO/DylinkerCommand.hpp>
#include <LIEF/MachO/DynamicSymbolCommand.hpp>
#include <LIEF/MachO/EncryptionInfo.hpp>
#include <LIEF/MachO/ExportInfo.hpp>
#include <LIEF/MachO/FatBinary.hpp>
#include <LIEF/MachO/FilesetCommand.hpp>
#include <LIEF/MachO/FunctionStarts.hpp>
#include <LIEF/MachO/Header.hpp>
#include <LIEF/MachO/IndirectBindingInfo.hpp>
#include <LIEF/MachO/LinkerOptHint.hpp>
#include <LIEF/MachO/LoadCommand.hpp>
#include <LIEF/MachO/MainCommand.hpp>
#include <LIEF/MachO/Parser.hpp>
#include <LIEF/MachO/ParserConfig.hpp>
#include <LIEF/MachO/RPathCommand.hpp>
#include <LIEF/MachO/Symbol.hpp>
#include <LIEF/MachO/Routine.hpp>
#include <LIEF/MachO/Relocation.hpp>
#include <LIEF/MachO/RelocationObject.hpp>
#include <LIEF/MachO/RelocationDyld.hpp>
#include <LIEF/MachO/RelocationFixup.hpp>
#include <LIEF/MachO/BindingInfo.hpp>
#include <LIEF/MachO/DyldBindingInfo.hpp>
#include <LIEF/MachO/ExportInfo.hpp>
#include <LIEF/MachO/FunctionStarts.hpp>
#include <LIEF/MachO/CodeSignature.hpp>
#include <LIEF/MachO/CodeSignatureDir.hpp>
#include <LIEF/MachO/DataInCode.hpp>
#include <LIEF/MachO/DataCodeEntry.hpp>
#include <LIEF/MachO/SourceVersion.hpp>
#include <LIEF/MachO/VersionMin.hpp>
#include <LIEF/MachO/RelocationObject.hpp>
#include <LIEF/MachO/Routine.hpp>
#include <LIEF/MachO/Section.hpp>
#include <LIEF/MachO/SegmentCommand.hpp>
#include <LIEF/MachO/SegmentSplitInfo.hpp>
#include <LIEF/MachO/SubFramework.hpp>
#include <LIEF/MachO/SourceVersion.hpp>
#include <LIEF/MachO/Stub.hpp>
#include <LIEF/MachO/SubClient.hpp>
#include <LIEF/MachO/DyldEnvironment.hpp>
#include <LIEF/MachO/EncryptionInfo.hpp>
#include <LIEF/MachO/BuildVersion.hpp>
#include <LIEF/MachO/FilesetCommand.hpp>
#include <LIEF/MachO/ChainedBindingInfo.hpp>
#include <LIEF/MachO/IndirectBindingInfo.hpp>
#include <LIEF/MachO/UnknownCommand.hpp>
#include <LIEF/MachO/SubFramework.hpp>
#include <LIEF/MachO/Symbol.hpp>
#include <LIEF/MachO/SymbolCommand.hpp>
#include <LIEF/MachO/ThreadCommand.hpp>
#include <LIEF/MachO/TwoLevelHints.hpp>
#include <LIEF/MachO/LinkerOptHint.hpp>
#include <LIEF/MachO/Builder.hpp>
#include <LIEF/MachO/UUIDCommand.hpp>
#include <LIEF/MachO/UnknownCommand.hpp>
#include <LIEF/MachO/VersionMin.hpp>

#define CREATE(X,Y) create<X>(Y)

Expand Down Expand Up @@ -119,6 +120,7 @@ void init_objects(nb::module_& m) {
CREATE(LinkerOptHint, m);
CREATE(IndirectBindingInfo, m);
CREATE(UnknownCommand, m);
CREATE(Stub, m);
CREATE(Builder, m);
}

Expand Down
1 change: 1 addition & 0 deletions api/python/src/MachO/objects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ target_sources(pyLIEF PRIVATE
pySegmentCommand.cpp
pySegmentSplitInfo.cpp
pySourceVersion.cpp
pyStub.cpp
pySubClient.cpp
pySubFramework.cpp
pySymbol.cpp
Expand Down
17 changes: 16 additions & 1 deletion api/python/src/MachO/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <nanobind/stl/string.h>
#include <nanobind/stl/unique_ptr.h>
#include "nanobind/extra/memoryview.hpp"
#include "nanobind/extra/random_access_iterator.hpp"

#include "LIEF/MachO/Binary.hpp"
#include "LIEF/MachO/BuildVersion.hpp"
Expand Down Expand Up @@ -65,7 +66,6 @@
#include "pyIterator.hpp"

namespace LIEF::MachO::py {

template<>
void create<Binary>(nb::module_& m) {
using namespace LIEF::py;
Expand Down Expand Up @@ -680,6 +680,21 @@ void create<Binary>(nb::module_& m) {
)doc"_doc
)

.def_prop_ro("symbol_stubs",
[] (const Binary& self) {
auto stubs = self.symbol_stubs();
return nb::make_random_access_iterator(nb::type<Binary>(), "stub_iterator", stubs);
}, nb::keep_alive<0, 1>(),
R"doc(
Return an iterator over the symbol stubs.
These stubs are involved when calling an **imported** function and are
similar to the ELF's plt/got mechanism.
There are located in sections like: ``__stubs,__auth_stubs,__symbol_stub,__picsymbolstub4``
)doc"_doc
)

.def_prop_ro("has_nx_heap", &Binary::has_nx_heap,
R"doc(
Return True if the **heap** is flagged as non-executable. False
Expand Down
94 changes: 94 additions & 0 deletions api/python/src/MachO/objects/pyStub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* Copyright 2017 - 2024 R. Thomas
* Copyright 2017 - 2024 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 <sstream>
#include "LIEF/MachO/Stub.hpp"

#include "nanobind/utils.hpp"
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>

#include "MachO/pyMachO.hpp"

#include "pyLIEF.hpp"
#include "pyErr.hpp"

namespace LIEF::MachO::py {

template<>
void create<Stub>(nb::module_& m) {
nb::class_<Stub> object(m, "Stub",
R"doc(
This class represents a stub entry in sections like ``__stubs,__auth_stubs``.
It wraps assembly instructions which are used to access the *got* where the
address of the symbol is resolved.
Example:
.. code-block:: text
0000000236a3c1bc: ___memcpy_chk
adrp x17, #0x241513aa8
add x17, x17, #0x241513aa8
ldr x16, [x17]
braa x16, x17
)doc"_doc
);

nb::class_<Stub::target_info_t>(object, "target_info_t")
.def(nb::init<>())
.def(nb::init<Header::CPU_TYPE, uint32_t>())
.def_rw("arch", &Stub::target_info_t::arch)
.def_rw("subtype", &Stub::target_info_t::subtype);

object
.def(nb::init<Stub::target_info_t, uint64_t, std::vector<uint8_t>>(),
"target_info"_a, "address"_a, "raw_stub"_a
)
.def_prop_ro("address", &Stub::address,
"The virtual address where the stub is located"_doc
)
.def_prop_ro("raw",
[] (const Stub& stub) {
return nb::to_memoryview(stub.raw());
},
"The (raw) instructions of this entry as a memory view of bytes"_doc)

.def_prop_ro("target",
[] (Stub& self) {
return LIEF::py::error_or(&Stub::target, self);
},
R"doc(
The address resolved by this stub.
For instance, given this stub:
.. code-block::
0x3eec: adrp x16, #4096
0x3ef0: ldr x16, [x16, #24]
0x3ef4: br x16
The function returns: ``0x4018``.
.. warning::
This function is only available with LIEF's extended version
)doc"_doc)

LIEF_DEFAULT_STR(Stub);
}
}
103 changes: 103 additions & 0 deletions api/python/src/nanobind/extra/random_access_iterator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef PY_LIEF_RANDOM_ACCESS_IT_H
#define PY_LIEF_RANDOM_ACCESS_IT_H

#include <nanobind/nanobind.h>
#include <nanobind/make_iterator.h>

NAMESPACE_BEGIN(NB_NAMESPACE)
namespace detail {
template<class Element>
class TypedRandomIterator : public nanobind::iterator {
public:
static constexpr auto Name = const_name("collections.abc.Sequence[") + make_caster<Element>::Name + const_name("]");
TypedRandomIterator(nanobind::iterator&& it) :
nanobind::iterator::iterator(std::move(it))
{}
};

template <typename Access, rv_policy Policy, typename Iterator,
typename Sentinel, typename ValueType, typename... Extra>
struct random_iterator_state {
Iterator it;
Iterator begin;
Sentinel end;
bool first_or_done;
};

template <typename Access, rv_policy Policy, typename Iterator,
typename Sentinel, typename ValueType, typename... Extra>
iterator make_rnd_iterator_impl(handle scope, const char *name,
Iterator &&first, Sentinel &&last,
Extra &&...extra) {
using State = random_iterator_state<Access, Policy, Iterator, Sentinel, ValueType, Extra...>;

if (!type<State>().is_valid()) {
class_<State>(scope, name)
.def("__iter__", [](handle h) { return h; })
.def("__len__", [](State &s) { return std::distance(s.begin, s.end); })
.def("__getitem__",
[] (State& s, Py_ssize_t i) -> ValueType {
const size_t size = std::distance(s.begin, s.end);
if (i < 0) {
i += static_cast<Py_ssize_t>(size);
}
if (i < 0 || static_cast<size_t>(i) >= size) {
throw nanobind::index_error();
}
Iterator it = s.begin + i;
return Access()(it);
}, std::forward<Extra>(extra)..., Policy)

.def("__next__",
[](State &s) -> ValueType {
if (!s.first_or_done)
++s.it;
else
s.first_or_done = false;

if (s.it == s.end) {
s.first_or_done = true;
throw stop_iteration();
}

return Access()(s.it);
},
std::forward<Extra>(extra)...,
Policy);
}
auto begin = first;
return borrow<iterator>(cast(State{ std::forward<Iterator>(first),
std::move(begin),
std::forward<Sentinel>(last), true }));
}
}


template <rv_policy Policy = rv_policy::reference_internal,
typename Iterator,
typename Sentinel,
typename ValueType = typename detail::iterator_access<Iterator>::result_type,
typename... Extra>
detail::TypedRandomIterator<ValueType> make_random_access_iterator(handle scope, const char *name, Iterator &&first, Sentinel &&last, Extra &&...extra) {
return detail::make_rnd_iterator_impl<detail::iterator_access<Iterator>, Policy,
Iterator, Sentinel, ValueType, Extra...>(
scope, name, std::forward<Iterator>(first),
std::forward<Sentinel>(last), std::forward<Extra>(extra)...);
}

template <rv_policy Policy = rv_policy::reference_internal,
typename Type,
typename ValueType = typename detail::iterator_access<typename Type::IteratorTy>::result_type,
typename... Extra>
detail::TypedRandomIterator<ValueType> make_random_access_iterator(
handle scope, const char *name, Type &value, Extra &&...extra)
{
return make_random_access_iterator<Policy>(
scope, name, std::begin(value), std::end(value),
std::forward<Extra>(extra)...
);
}

NAMESPACE_END(NB_NAMESPACE)

#endif
Loading

0 comments on commit 993f24b

Please sign in to comment.