|
| 1 | +/* Copyright 2017 R. Thomas |
| 2 | + * Copyright 2017 Quarkslab |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <algorithm> |
| 17 | + |
| 18 | +#include <string> |
| 19 | +#include <sstream> |
| 20 | + |
| 21 | +#include "LIEF/visitors/Hash.hpp" |
| 22 | +#include "LIEF/MachO/Relocation.hpp" |
| 23 | + |
| 24 | +#include "pyMachO.hpp" |
| 25 | + |
| 26 | +template<class T> |
| 27 | +using getter_t = T (Relocation::*)(void) const; |
| 28 | + |
| 29 | +template<class T> |
| 30 | +using setter_t = void (Relocation::*)(T); |
| 31 | + |
| 32 | + |
| 33 | +void init_MachO_Relocation_class(py::module& m) { |
| 34 | + |
| 35 | + py::class_<Relocation>(m, "Relocation") |
| 36 | + |
| 37 | + .def_property("address", |
| 38 | + static_cast<getter_t<uint32_t>>(&Relocation::address), |
| 39 | + static_cast<setter_t<uint32_t>>(&Relocation::address), |
| 40 | + "For :attr:`~lief.MachO.FILE_TYPES.OBJECT` this is an " |
| 41 | + "offset from the start of the " RST_CLASS_REF(lief.MachO.Section) " " |
| 42 | + "to the item containing the address requiring relocation.", |
| 43 | + py::return_value_policy::reference_internal) |
| 44 | + |
| 45 | + .def_property("pc_relative", |
| 46 | + static_cast<getter_t<bool>>(&Relocation::is_pc_relative), |
| 47 | + static_cast<setter_t<bool>>(&Relocation::pc_relative), |
| 48 | + "Indicates whether the item containing the address to be " |
| 49 | + "relocated is part of a CPU instruction that uses PC-relative addressing.\n\n" |
| 50 | + |
| 51 | + "For addresses contained in PC-relative instructions, the CPU adds the address of " |
| 52 | + "the instruction to the address contained in the instruction.", |
| 53 | + py::return_value_policy::reference_internal) |
| 54 | + |
| 55 | + .def_property("size", |
| 56 | + static_cast<getter_t<uint8_t>>(&Relocation::size), |
| 57 | + static_cast<setter_t<uint8_t>>(&Relocation::size), |
| 58 | + "Indicates the length of the item containing the address to be relocated.\n\n" |
| 59 | + |
| 60 | + "The following table lists values and the corresponding address length:\n" |
| 61 | + "\t * 0: 1 byte\n" |
| 62 | + "\t * 1: 2 byte\n" |
| 63 | + "\t * 2: 4 byte\n" |
| 64 | + "\t * 3: 4 byte\n", |
| 65 | + py::return_value_policy::reference_internal) |
| 66 | + |
| 67 | + .def_property("type", |
| 68 | + static_cast<getter_t<uint8_t>>(&Relocation::type), |
| 69 | + static_cast<setter_t<uint8_t>>(&Relocation::type), |
| 70 | + "Type of the relocation according to the :attr:`~lief.MachO.Relocation.architecture`\n\n" |
| 71 | + |
| 72 | + "See:\n" |
| 73 | + "\t * " RST_CLASS_REF(lief.MachO.X86_RELOCATION) "\n" |
| 74 | + "\t * " RST_CLASS_REF(lief.MachO.X86_64_RELOCATION) "\n" |
| 75 | + "\t * " RST_CLASS_REF(lief.MachO.PPC_RELOCATION) "\n" |
| 76 | + "\t * " RST_CLASS_REF(lief.MachO.ARM_RELOCATION) "\n" |
| 77 | + "\t * " RST_CLASS_REF(lief.MachO.ARM64_RELOCATION) "\n", |
| 78 | + py::return_value_policy::reference_internal) |
| 79 | + |
| 80 | + .def_property("value", |
| 81 | + static_cast<getter_t<int32_t>>(&Relocation::value), |
| 82 | + static_cast<setter_t<int32_t>>(&Relocation::value), |
| 83 | + "For **scattered** relocations, the address of the relocatable expression " |
| 84 | + "for the item in the file that needs to be updated if the address is changed.\n\n" |
| 85 | + |
| 86 | + "For relocatable expressions with the difference of two section addresses, " |
| 87 | + "the address from which to subtract (in mathematical terms, the minuend) " |
| 88 | + "is contained in the first relocation entry and the address to subtract (the subtrahend) " |
| 89 | + "is contained in the second relocation entry.", |
| 90 | + py::return_value_policy::reference_internal) |
| 91 | + |
| 92 | + |
| 93 | + .def_property_readonly("architecture", |
| 94 | + &Relocation::architecture, |
| 95 | + "" RST_CLASS_REF(lief.MachO.CPU_TYPES) " of the relocation") |
| 96 | + |
| 97 | + .def_property_readonly("is_scattered", |
| 98 | + &Relocation::is_scattered, |
| 99 | + "``True`` if the relocation is a scattered one") |
| 100 | + |
| 101 | + .def_property_readonly("has_symbol", |
| 102 | + &Relocation::has_symbol, |
| 103 | + "``True`` if the relocation has a " RST_CLASS_REF(lief.MachO.Symbol) " associated with") |
| 104 | + |
| 105 | + .def_property_readonly("symbol", |
| 106 | + static_cast<Symbol& (Relocation::*)(void)>(&Relocation::symbol), |
| 107 | + "" RST_CLASS_REF(lief.MachO.Symbol) " associated with the relocation (if any)", |
| 108 | + py::return_value_policy::reference) |
| 109 | + |
| 110 | + .def_property_readonly("has_section", |
| 111 | + &Relocation::has_section, |
| 112 | + "``True`` if the relocation has a " RST_CLASS_REF(lief.MachO.Section) " associated with") |
| 113 | + |
| 114 | + .def_property_readonly("section", |
| 115 | + static_cast<Section& (Relocation::*)(void)>(&Relocation::section), |
| 116 | + "" RST_CLASS_REF(lief.MachO.Section) " associated with the relocation (if any)", |
| 117 | + py::return_value_policy::reference) |
| 118 | + |
| 119 | + .def("__eq__", &Relocation::operator==) |
| 120 | + .def("__ne__", &Relocation::operator!=) |
| 121 | + .def("__hash__", |
| 122 | + [] (const Relocation& relocation) { |
| 123 | + return LIEF::Hash::hash(relocation); |
| 124 | + }) |
| 125 | + |
| 126 | + |
| 127 | + .def("__str__", |
| 128 | + [] (const Relocation& relocation) |
| 129 | + { |
| 130 | + std::ostringstream stream; |
| 131 | + stream << relocation; |
| 132 | + std::string str = stream.str(); |
| 133 | + return str; |
| 134 | + }); |
| 135 | + |
| 136 | +} |
0 commit comments