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 "DynamicEntry.hpp"
|
|
|
|
|
|
|
|
namespace LIEF {
|
|
|
|
namespace ELF {
|
|
|
|
|
|
|
|
void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
|
|
|
|
|
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
|
|
|
it_dynamic_entries dyn_entries = binary->dynamic_entries();
|
2017-03-30 16:56:49 +02:00
|
|
|
c_binary->dynamic_entries = static_cast<Elf_DynamicEntry_t**>(
|
|
|
|
malloc((dyn_entries.size() + 1) * sizeof(Elf_DynamicEntry_t**)));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < dyn_entries.size(); ++i) {
|
|
|
|
DynamicEntry& entry = dyn_entries[i];
|
|
|
|
switch(entry.tag()) {
|
|
|
|
case DYNAMIC_TAGS::DT_NEEDED:
|
|
|
|
{
|
|
|
|
|
|
|
|
Elf_DynamicEntry_Library_t* e = static_cast<Elf_DynamicEntry_Library_t*>(
|
|
|
|
malloc(sizeof(Elf_DynamicEntry_Library_t)));
|
|
|
|
|
|
|
|
e->tag = static_cast<enum ::DYNAMIC_TAGS>(entry.tag());
|
|
|
|
e->value = entry.value();
|
|
|
|
e->name = dynamic_cast<DynamicEntryLibrary*>(&entry)->name().c_str();
|
|
|
|
|
|
|
|
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);
|
|
|
|
break;
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_SONAME:
|
|
|
|
{
|
|
|
|
Elf_DynamicEntry_SharedObject_t* e = static_cast<Elf_DynamicEntry_SharedObject_t*>(
|
|
|
|
malloc(sizeof(Elf_DynamicEntry_SharedObject_t)));
|
|
|
|
|
|
|
|
e->tag = static_cast<enum ::DYNAMIC_TAGS>(entry.tag());
|
|
|
|
e->value = entry.value();
|
|
|
|
e->name = dynamic_cast<DynamicSharedObject*>(&entry)->name().c_str();
|
|
|
|
|
|
|
|
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_RPATH:
|
|
|
|
{
|
|
|
|
Elf_DynamicEntry_Rpath_t* e = static_cast<Elf_DynamicEntry_Rpath_t*>(
|
|
|
|
malloc(sizeof(Elf_DynamicEntry_Rpath_t)));
|
|
|
|
|
|
|
|
e->tag = static_cast<enum ::DYNAMIC_TAGS>(entry.tag());
|
|
|
|
e->value = entry.value();
|
|
|
|
e->rpath = dynamic_cast<DynamicEntryRpath*>(&entry)->name().c_str();
|
|
|
|
|
|
|
|
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_RUNPATH:
|
|
|
|
{
|
|
|
|
Elf_DynamicEntry_RunPath_t* e = static_cast<Elf_DynamicEntry_RunPath_t*>(
|
|
|
|
malloc(sizeof(Elf_DynamicEntry_RunPath_t)));
|
|
|
|
|
|
|
|
e->tag = static_cast<enum ::DYNAMIC_TAGS>(entry.tag());
|
|
|
|
e->value = entry.value();
|
|
|
|
e->runpath = dynamic_cast<DynamicEntryRunPath*>(&entry)->name().c_str();
|
|
|
|
|
|
|
|
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_INIT_ARRAY:
|
|
|
|
case DYNAMIC_TAGS::DT_FINI_ARRAY:
|
|
|
|
case DYNAMIC_TAGS::DT_PREINIT_ARRAY:
|
|
|
|
{
|
|
|
|
Elf_DynamicEntry_Array_t* e = static_cast<Elf_DynamicEntry_Array_t*>(
|
|
|
|
malloc(sizeof(Elf_DynamicEntry_Array_t)));
|
|
|
|
|
|
|
|
e->tag = static_cast<enum ::DYNAMIC_TAGS>(entry.tag());
|
|
|
|
e->value = entry.value();
|
|
|
|
const std::vector<uint64_t>& array = dynamic_cast<DynamicEntryArray*>(&entry)->array();
|
|
|
|
e->array = static_cast<uint64_t*>(malloc((array.size() + 1) * sizeof(uint64_t)));
|
|
|
|
for (size_t i = 0; i < array.size(); ++i) {
|
|
|
|
e->array[i] = array[i];
|
|
|
|
}
|
|
|
|
e->array[array.size()] = 0;
|
|
|
|
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2017-08-01 15:47:05 +02:00
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_FLAGS:
|
|
|
|
{
|
|
|
|
Elf_DynamicEntry_Flags_t* e = static_cast<Elf_DynamicEntry_Flags_t*>(
|
|
|
|
malloc(sizeof(Elf_DynamicEntry_Flags_t)));
|
|
|
|
|
|
|
|
e->tag = static_cast<enum ::DYNAMIC_TAGS>(entry.tag());
|
|
|
|
e->value = entry.value();
|
|
|
|
const dynamic_flags_list_t& flags = dynamic_cast<DynamicEntryFlags*>(&entry)->flags();
|
|
|
|
e->flags = static_cast<enum ::DYNAMIC_FLAGS*>(malloc((flags.size() + 1) * sizeof(DYNAMIC_FLAGS)));
|
|
|
|
e->flags_1 = nullptr;
|
|
|
|
|
|
|
|
auto&& it = std::begin(flags);
|
|
|
|
|
|
|
|
for (size_t i = 0; it != std::end(flags); ++i, ++it) {
|
|
|
|
e->flags[i] = static_cast<enum ::DYNAMIC_FLAGS>(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
e->flags[flags.size()] = static_cast<enum ::DYNAMIC_FLAGS>(0);
|
|
|
|
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_FLAGS_1:
|
|
|
|
{
|
|
|
|
Elf_DynamicEntry_Flags_t* e = static_cast<Elf_DynamicEntry_Flags_t*>(
|
|
|
|
malloc(sizeof(Elf_DynamicEntry_Flags_t)));
|
|
|
|
|
|
|
|
e->tag = static_cast<enum ::DYNAMIC_TAGS>(entry.tag());
|
|
|
|
e->value = entry.value();
|
|
|
|
const dynamic_flags_list_t& flags = dynamic_cast<DynamicEntryFlags*>(&entry)->flags();
|
|
|
|
e->flags_1 = static_cast<enum ::DYNAMIC_FLAGS_1*>(malloc((flags.size() + 1) * sizeof(DYNAMIC_FLAGS_1)));
|
|
|
|
e->flags = nullptr;
|
|
|
|
|
|
|
|
auto&& it = std::begin(flags);
|
|
|
|
|
|
|
|
for (size_t i = 0; it != std::end(flags); ++i, ++it) {
|
|
|
|
e->flags_1[i] = static_cast<enum ::DYNAMIC_FLAGS_1>(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
e->flags_1[flags.size()] = static_cast<enum ::DYNAMIC_FLAGS_1>(0);
|
|
|
|
c_binary->dynamic_entries[i] = reinterpret_cast<Elf_DynamicEntry_t*>(e);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
c_binary->dynamic_entries[i] =
|
|
|
|
static_cast<Elf_DynamicEntry_t*>(malloc(sizeof(Elf_DynamicEntry_t)));
|
|
|
|
c_binary->dynamic_entries[i]->tag = static_cast<enum ::DYNAMIC_TAGS>(entry.tag());
|
|
|
|
c_binary->dynamic_entries[i]->value = entry.value();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c_binary->dynamic_entries[dyn_entries.size()] = nullptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void destroy_dynamic_entries(Elf_Binary_t* c_binary) {
|
|
|
|
|
|
|
|
Elf_DynamicEntry_t **dynamic_entries = c_binary->dynamic_entries;
|
|
|
|
for (size_t idx = 0; dynamic_entries[idx] != NULL; ++idx) {
|
|
|
|
switch(dynamic_entries[idx]->tag) {
|
|
|
|
case DYNAMIC_TAGS::DT_NEEDED:
|
|
|
|
{
|
|
|
|
free(reinterpret_cast<Elf_DynamicEntry_Library_t*>(dynamic_entries[idx]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_SONAME:
|
|
|
|
{
|
|
|
|
free(reinterpret_cast<Elf_DynamicEntry_SharedObject_t*>(dynamic_entries[idx]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_RPATH:
|
|
|
|
{
|
|
|
|
free(reinterpret_cast<Elf_DynamicEntry_Rpath_t*>(dynamic_entries[idx]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_RUNPATH:
|
|
|
|
{
|
|
|
|
free(reinterpret_cast<Elf_DynamicEntry_RunPath_t*>(dynamic_entries[idx]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_INIT_ARRAY:
|
|
|
|
case DYNAMIC_TAGS::DT_FINI_ARRAY:
|
|
|
|
case DYNAMIC_TAGS::DT_PREINIT_ARRAY:
|
|
|
|
{
|
|
|
|
free(reinterpret_cast<Elf_DynamicEntry_Array_t*>(dynamic_entries[idx]));
|
|
|
|
break;
|
|
|
|
}
|
2017-08-01 15:47:05 +02:00
|
|
|
|
|
|
|
case DYNAMIC_TAGS::DT_FLAGS:
|
|
|
|
case DYNAMIC_TAGS::DT_FLAGS_1:
|
|
|
|
{
|
|
|
|
free(reinterpret_cast<Elf_DynamicEntry_Flags_t*>(dynamic_entries[idx]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
|
|
|
|
free(dynamic_entries[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(c_binary->dynamic_entries);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|