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/Abstract/Section.hpp"
|
|
|
|
#include "LIEF/ELF/Section.hpp"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using getter_t = T (Section::*)(void) const;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using setter_t = void (Section::*)(T);
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using no_const_getter = T (Section::*)(void);
|
|
|
|
|
|
|
|
void init_ELF_Section_class(py::module& m) {
|
|
|
|
|
|
|
|
// Section object
|
|
|
|
py::class_<Section, LIEF::Section>(m, "Section")
|
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<>(),
|
|
|
|
"Default constructor")
|
|
|
|
|
|
|
|
.def(py::init<const std::string&, ELF_SECTION_TYPES>(),
|
|
|
|
"Constructor from name and type",
|
|
|
|
"name"_a, "type"_a = ELF_SECTION_TYPES::SHT_PROGBITS)
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def("__init__",
|
|
|
|
[] (Section& section, std::vector<uint8_t>& content, ELF_CLASS type)
|
|
|
|
{
|
|
|
|
new (§ion) Section(content.data(), type);
|
|
|
|
})
|
|
|
|
|
|
|
|
.def_property_readonly("name_idx",
|
|
|
|
static_cast<getter_t<uint32_t>>(&Section::name_idx),
|
|
|
|
"Index of the section's name in the string table\n\n"
|
|
|
|
".. warning:: The value will probably change when re-building binary.")
|
|
|
|
|
|
|
|
.def_property("type",
|
2017-08-21 09:20:49 +02:00
|
|
|
static_cast<getter_t<ELF_SECTION_TYPES>>(&Section::type),
|
|
|
|
static_cast<setter_t<ELF_SECTION_TYPES>>(&Section::type),
|
2017-03-30 16:56:49 +02:00
|
|
|
"Return a " RST_CLASS_REF(lief.ELF.SECTION_TYPES) "")
|
|
|
|
|
|
|
|
.def_property("flags",
|
|
|
|
static_cast<getter_t<uint64_t>>(&Section::flags),
|
|
|
|
static_cast<setter_t<uint64_t>>(&Section::flags),
|
2017-08-25 15:57:59 +02:00
|
|
|
"Return section's flags as an integer")
|
|
|
|
|
|
|
|
.def_property_readonly("flags_list",
|
|
|
|
&Section::flags_list,
|
|
|
|
"Return section's flags as a list of " RST_CLASS_REF(lief.ELF.SECTION_FLAGS) "")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def_property("virtual_address",
|
|
|
|
static_cast<getter_t<uint64_t>>(&Section::virtual_address),
|
|
|
|
static_cast<setter_t<uint64_t>>(&Section::virtual_address),
|
|
|
|
"Return address where the section will be mapped in memory\n\n"
|
|
|
|
".. warning:: This value is not reliable use segment's virtual address "
|
|
|
|
"(:attr:`~lief.ELF.Segment.virtual_address`) instead.")
|
|
|
|
|
|
|
|
.def_property("file_offset",
|
|
|
|
static_cast<getter_t<uint64_t>>(&Section::file_offset),
|
|
|
|
static_cast<setter_t<uint64_t>>(&Section::file_offset),
|
2017-04-26 09:27:56 +02:00
|
|
|
"Offset of the section's content")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def_property_readonly("original_size",
|
|
|
|
static_cast<getter_t<uint64_t>>(&Section::original_size),
|
|
|
|
"original data size. Without modification we have `original_size == size`")
|
|
|
|
|
|
|
|
.def_property("alignment",
|
|
|
|
static_cast<getter_t<uint64_t>>(&Section::alignment),
|
2017-04-26 09:27:56 +02:00
|
|
|
static_cast<setter_t<uint64_t>>(&Section::alignment),
|
|
|
|
"Section alignment")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def_property("information",
|
|
|
|
static_cast<getter_t<uint64_t>>(&Section::information),
|
2017-04-26 09:27:56 +02:00
|
|
|
static_cast<setter_t<uint32_t>>(&Section::information),
|
|
|
|
"Section information (this value depends on the section)")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def_property("entry_size",
|
|
|
|
static_cast<getter_t<uint64_t>>(&Section::entry_size),
|
|
|
|
static_cast<setter_t<uint64_t>>(&Section::entry_size),
|
2017-04-26 09:27:56 +02:00
|
|
|
"If section's content is an array, `entry_size` holds the element size\n\n"
|
2017-03-30 16:56:49 +02:00
|
|
|
":Example:\n"
|
|
|
|
"\tThe `.dynamic` section contains an array of " RST_CLASS_REF(lief.ELF.DynamicEntry) ". As the size "
|
|
|
|
"of a dynamic entry is 0x10 (for ELF64), entry_size will contains this value\n\n"
|
|
|
|
".. warning:: This value is not necessarily reliable.")
|
|
|
|
|
|
|
|
.def_property("link",
|
|
|
|
static_cast<getter_t<uint32_t>>(&Section::link),
|
2017-04-26 09:27:56 +02:00
|
|
|
static_cast<setter_t<uint32_t>>(&Section::link),
|
|
|
|
"Index to another section")
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def_property_readonly("segments",
|
|
|
|
static_cast<no_const_getter<it_segments>>(&Section::segments),
|
2017-04-16 10:21:23 +02:00
|
|
|
"Return segment(s) associated with the given section",
|
2017-03-30 16:56:49 +02:00
|
|
|
py::return_value_policy::reference_internal)
|
|
|
|
|
2018-02-28 16:05:38 +01:00
|
|
|
.def("clear",
|
|
|
|
&Section::clear,
|
|
|
|
"Clear the content of the section with the given ``value``",
|
|
|
|
"value"_a = 0,
|
|
|
|
py::return_value_policy::reference)
|
2017-03-30 16:56:49 +02:00
|
|
|
|
2017-08-02 08:19:26 +02:00
|
|
|
.def("add",
|
|
|
|
&Section::add,
|
|
|
|
"Add the given " RST_CLASS_REF(lief.ELF.SECTION_FLAGS) " to the list of "
|
|
|
|
":attr:`~lief.ELF.Section.flags`",
|
|
|
|
"flag"_a)
|
|
|
|
|
|
|
|
.def("remove",
|
|
|
|
&Section::remove,
|
|
|
|
"Remove the given " RST_CLASS_REF(lief.ELF.SECTION_FLAGS) " to the list of "
|
|
|
|
":attr:`~lief.ELF.Section.flags`",
|
|
|
|
"flag"_a)
|
|
|
|
|
|
|
|
|
|
|
|
.def("has",
|
2017-08-21 09:20:49 +02:00
|
|
|
static_cast<bool (Section::*)(ELF_SECTION_FLAGS) const>(&Section::has),
|
2017-08-02 08:19:26 +02:00
|
|
|
"Check if the given " RST_CLASS_REF(lief.ELF.SECTION_FLAGS) " is present",
|
|
|
|
"flag"_a)
|
|
|
|
|
|
|
|
.def("has",
|
|
|
|
static_cast<bool (Section::*)(const Segment&) const>(&Section::has),
|
|
|
|
"Check if the given " RST_CLASS_REF(lief.ELF.Segment) " is present "
|
|
|
|
"in :attr:`~lief.ELF.Section.segments`",
|
|
|
|
"segment"_a)
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def("__eq__", &Section::operator==)
|
|
|
|
.def("__ne__", &Section::operator!=)
|
|
|
|
.def("__hash__",
|
|
|
|
[] (const Section& section) {
|
2018-03-13 16:49:02 +01:00
|
|
|
return Hash::hash(section);
|
2017-03-30 16:56:49 +02:00
|
|
|
})
|
|
|
|
|
2017-08-21 09:20:49 +02:00
|
|
|
.def(py::self += ELF_SECTION_FLAGS())
|
|
|
|
.def(py::self -= ELF_SECTION_FLAGS())
|
2017-08-02 08:19:26 +02:00
|
|
|
|
|
|
|
.def("__contains__",
|
2017-08-21 09:20:49 +02:00
|
|
|
static_cast<bool (Section::*)(ELF_SECTION_FLAGS) const>(&Section::has),
|
2017-08-02 08:19:26 +02:00
|
|
|
"Check if the given " RST_CLASS_REF(lief.ELF.SECTION_FLAGS) " is present")
|
|
|
|
|
|
|
|
|
|
|
|
.def("__contains__",
|
|
|
|
static_cast<bool (Section::*)(const Segment&) const>(&Section::has),
|
|
|
|
"Check if the given " RST_CLASS_REF(lief.ELF.Segment) " is present "
|
|
|
|
"in :attr:`~lief.ELF.Section.segments`")
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
.def("__str__",
|
|
|
|
[] (const Section& section)
|
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << section;
|
|
|
|
std::string str = stream.str();
|
|
|
|
return str;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|