mirror of
https://github.com/lief-project/LIEF
synced 2026-06-08 15:30:44 +00:00
Add Mach-O symbol stubs iterator
This commit is contained in:
@@ -38,6 +38,7 @@ target_sources(pyLIEF PRIVATE
|
||||
pySegmentCommand.cpp
|
||||
pySegmentSplitInfo.cpp
|
||||
pySourceVersion.cpp
|
||||
pyStub.cpp
|
||||
pySubClient.cpp
|
||||
pySubFramework.cpp
|
||||
pySymbol.cpp
|
||||
|
||||
@@ -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"
|
||||
@@ -65,7 +66,6 @@
|
||||
#include "pyIterator.hpp"
|
||||
|
||||
namespace LIEF::MachO::py {
|
||||
|
||||
template<>
|
||||
void create<Binary>(nb::module_& m) {
|
||||
using namespace LIEF::py;
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user