2017-03-30 16:56:49 +02:00
|
|
|
/* 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 "pyELF.hpp"
|
|
|
|
|
2018-03-13 16:49:02 +01:00
|
|
|
#include "LIEF/ELF/hash.hpp"
|
2017-03-30 16:56:49 +02:00
|
|
|
#include "LIEF/ELF/Relocation.hpp"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using getter_t = T (Relocation::*)(void) const;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using setter_t = void (Relocation::*)(T);
|
|
|
|
|
|
|
|
void init_ELF_Relocation_class(py::module& m) {
|
|
|
|
// Relocation object
|
2017-09-05 22:00:31 +02:00
|
|
|
py::class_<Relocation, LIEF::Relocation>(m, "Relocation")
|
Improve the ELF part of LIEF
Major changes (features):
* Enable adding multiple sections/segments - Executable (PIE or not), Library
* Enable adding multiple dynamic entries (DT_NEEDED, DT_INIT etc)
* Enable adding multiple relocations
* Enable adding multiple dynamic symbols
* Enable segment replacement
Major changes (API):
* Getters Binary::get_*name*() has been renamed to "name()"
* Binary::add(const DynamicEntry& entry) - To add an entry in the dynamic table
* Section& Binary::add(const Section& section, bool loaded = true) - To add a section(s)
* Segment& Binary::add(const Segment& segment, uint64_t base = 0) - To add segments
* Segment& replace(const Segment& new_segment, const Segment& original_segment, uint64_t base = 0)
* Binary's last_offset_section(), last_offset_segment(), next_virtual_address()
to have information about offset
* Binary's add_library(), get_library(), has_library() to handle
DT_NEEDED entries
Other changes:
* Binary::insert_content() - Use add(const Section&) or add(const Segment&) instead
* ELF's DataHandler has been cleaned
* Through LIEF::Section one can look for integers, strings, data
within the section (see LIEF::Section::search,
LIEF::Section::search_all)
* Through LIEF::Binary one can get *xref* of a number (or address)
see LIEF::Binary::xref function
* To access to the Abstract binary in Python, one can now use
the 'abstract' attribute. (e.g. binary.abstract.header.is_32)
Resolve: #83
Resolve: #66
Resolve: #48
2017-09-02 08:54:54 +02:00
|
|
|
.def(py::init<>())
|
|
|
|
.def(py::init<uint64_t, uint32_t, int64_t, bool>(),
|
|
|
|
"address"_a, "type"_a = 0, "addend"_a = 0, "is_rela"_a = false)
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
.def_property("addend",
|
|
|
|
static_cast<getter_t<int64_t>>(&Relocation::addend),
|
2017-04-26 09:27:56 +02:00
|
|
|
static_cast<setter_t<int64_t>>(&Relocation::addend),
|
|
|
|
"Additional value")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
2017-07-11 11:22:21 +02:00
|
|
|
.def_property("purpose",
|
|
|
|
static_cast<getter_t<RELOCATION_PURPOSES>>(&Relocation::purpose),
|
|
|
|
static_cast<setter_t<RELOCATION_PURPOSES>>(&Relocation::purpose),
|
|
|
|
"Purpose (" RST_CLASS_REF(lief.ELF.RELOCATION_PURPOSES) ") of the relocation")
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
.def_property("type",
|
|
|
|
static_cast<getter_t<uint32_t>>(&Relocation::type),
|
2017-04-26 09:27:56 +02:00
|
|
|
static_cast<setter_t<uint32_t>>(&Relocation::type),
|
|
|
|
"Relocation type.\n\n"
|
|
|
|
"See:\n\n"
|
|
|
|
"\t\t * " RST_CLASS_REF(lief.ELF.RELOCATION_X86_64) "\n\n"
|
|
|
|
"\t\t * " RST_CLASS_REF(lief.ELF.RELOCATION_ARM) "\n\n"
|
|
|
|
"\t\t * " RST_CLASS_REF(lief.ELF.RELOCATION_i386) "\n\n")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def_property_readonly("has_symbol",
|
2017-04-26 09:27:56 +02:00
|
|
|
&Relocation::has_symbol,
|
|
|
|
"``True`` if a " RST_CLASS_REF(lief.ELF.Symbol) " is associated with the relocations")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def_property_readonly("symbol",
|
|
|
|
static_cast<Symbol& (Relocation::*)(void)>(&Relocation::symbol),
|
2017-04-26 09:27:56 +02:00
|
|
|
"" RST_CLASS_REF(lief.ELF.Symbol) " associated with the relocation",
|
2017-03-30 16:56:49 +02:00
|
|
|
py::return_value_policy::reference_internal)
|
|
|
|
|
2018-04-20 10:42:04 +02:00
|
|
|
.def_property_readonly("has_section",
|
|
|
|
&Relocation::has_section,
|
|
|
|
"``True`` if a this relocation has a " RST_CLASS_REF(lief.ELF.Section) " associated")
|
|
|
|
|
|
|
|
.def_property_readonly("section",
|
|
|
|
static_cast<Section& (Relocation::*)(void)>(&Relocation::section),
|
|
|
|
"" RST_CLASS_REF(lief.ELF.Section) " to which the relocation applies",
|
|
|
|
py::return_value_policy::reference)
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
.def_property_readonly("is_rela",
|
2017-04-26 09:27:56 +02:00
|
|
|
static_cast<getter_t<bool>>(&Relocation::is_rela),
|
|
|
|
"``True`` if the relocation uses the :attr:`~lief.ELF.Relocation.addend` proprety")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def_property_readonly("is_rel",
|
2017-04-26 09:27:56 +02:00
|
|
|
static_cast<getter_t<bool>>(&Relocation::is_rel),
|
|
|
|
"``True`` if the relocation doesn't use the :attr:`~lief.ELF.Relocation.addend` proprety")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def("__eq__", &Relocation::operator==)
|
|
|
|
.def("__ne__", &Relocation::operator!=)
|
|
|
|
.def("__hash__",
|
|
|
|
[] (const Relocation& relocation) {
|
2018-03-13 16:49:02 +01:00
|
|
|
return Hash::hash(relocation);
|
2017-03-30 16:56:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
.def("__str__",
|
|
|
|
[] (const Relocation& relocation)
|
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << relocation;
|
|
|
|
std::string str = stream.str();
|
|
|
|
return str;
|
|
|
|
});
|
|
|
|
}
|