2018-10-10 09:53:07 +00:00
|
|
|
#include "ELFLoader.h"
|
2018-10-18 13:03:10 +00:00
|
|
|
#include <AK/kstdio.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-23 09:03:26 +00:00
|
|
|
//#define ELFLOADER_DEBUG
|
2018-11-12 12:25:16 +00:00
|
|
|
//#define SUPPORT_RELOCATIONS
|
2018-10-23 09:03:26 +00:00
|
|
|
|
2018-11-08 20:20:09 +00:00
|
|
|
ELFLoader::ELFLoader(const byte* buffer)
|
|
|
|
: m_image(buffer)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ELFLoader::~ELFLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ELFLoader::load()
|
|
|
|
{
|
2018-10-23 09:03:26 +00:00
|
|
|
#ifdef ELFLOADER_DEBUG
|
2018-11-04 13:09:30 +00:00
|
|
|
m_image.dump();
|
2018-10-23 09:03:26 +00:00
|
|
|
#endif
|
2018-11-04 13:09:30 +00:00
|
|
|
if (!m_image.is_valid())
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
|
2018-10-25 08:00:37 +00:00
|
|
|
if (!layout())
|
|
|
|
return false;
|
2018-11-12 12:25:16 +00:00
|
|
|
#ifdef SUPPORT_RELOCATIONS
|
2018-11-04 13:09:30 +00:00
|
|
|
if (!perform_relocations())
|
2018-10-25 08:00:37 +00:00
|
|
|
return false;
|
2018-11-12 12:25:16 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:00:37 +00:00
|
|
|
bool ELFLoader::layout()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-23 09:03:26 +00:00
|
|
|
#ifdef ELFLOADER_DEBUG
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("ELFLoader: Layout\n");
|
2018-10-23 09:03:26 +00:00
|
|
|
#endif
|
2018-11-03 09:11:56 +00:00
|
|
|
|
2018-11-03 10:28:23 +00:00
|
|
|
bool failed = false;
|
2018-11-04 13:09:30 +00:00
|
|
|
m_image.for_each_program_header([&] (const ELFImage::ProgramHeader& program_header) {
|
2018-11-03 09:11:56 +00:00
|
|
|
if (program_header.type() != PT_LOAD)
|
|
|
|
return;
|
|
|
|
#ifdef ELFLOADER_DEBUG
|
|
|
|
kprintf("PH: L%x %u r:%u w:%u\n", program_header.laddr().get(), program_header.size_in_memory(), program_header.is_readable(), program_header.is_writable());
|
|
|
|
#endif
|
2018-11-08 20:20:09 +00:00
|
|
|
if (program_header.is_writable()) {
|
|
|
|
allocate_section(program_header.laddr(), program_header.size_in_memory(), program_header.alignment(), program_header.is_readable(), program_header.is_writable());
|
|
|
|
} else {
|
|
|
|
map_section(program_header.laddr(), program_header.size_in_memory(), program_header.alignment(), program_header.offset(), program_header.is_readable(), program_header.is_writable());
|
|
|
|
}
|
2018-11-03 09:11:56 +00:00
|
|
|
});
|
|
|
|
|
2018-11-12 12:25:16 +00:00
|
|
|
m_image.for_each_section_of_type(SHT_PROGBITS, [] (const ELFImage::Section& section) {
|
2018-10-23 09:03:26 +00:00
|
|
|
#ifdef ELFLOADER_DEBUG
|
2018-11-03 10:28:23 +00:00
|
|
|
kprintf("ELFLoader: Copying progbits section: %s\n", section.name());
|
2018-10-23 09:03:26 +00:00
|
|
|
#endif
|
2018-10-25 08:00:37 +00:00
|
|
|
if (!section.size())
|
|
|
|
return true;
|
2018-11-03 10:28:23 +00:00
|
|
|
char* ptr = (char*)section.address();
|
2018-10-25 08:00:37 +00:00
|
|
|
if (!ptr) {
|
2018-11-04 12:42:44 +00:00
|
|
|
#ifdef ELFLOADER_DEBUG
|
|
|
|
kprintf("ELFLoader: ignoring section '%s' with null address\n", section.name());
|
|
|
|
#endif
|
|
|
|
return true;
|
2018-10-25 08:00:37 +00:00
|
|
|
}
|
2018-11-12 12:25:16 +00:00
|
|
|
// If this section isn't writable, it's already mmapped.
|
|
|
|
if (section.is_writable())
|
|
|
|
memcpy(ptr, section.raw_data(), section.size());
|
|
|
|
#ifdef SUPPORT_RELOCATIONS
|
2018-10-18 13:03:10 +00:00
|
|
|
m_sections.set(section.name(), move(ptr));
|
2018-11-12 12:25:16 +00:00
|
|
|
#endif
|
2018-10-25 08:00:37 +00:00
|
|
|
return true;
|
2018-10-10 09:53:07 +00:00
|
|
|
});
|
2018-11-12 12:25:16 +00:00
|
|
|
m_image.for_each_section_of_type(SHT_NOBITS, [&failed] (const ELFImage::Section& section) {
|
2018-10-25 10:06:00 +00:00
|
|
|
#ifdef ELFLOADER_DEBUG
|
2018-11-03 10:28:23 +00:00
|
|
|
kprintf("ELFLoader: Copying nobits section: %s\n", section.name());
|
2018-10-25 10:06:00 +00:00
|
|
|
#endif
|
|
|
|
if (!section.size())
|
|
|
|
return true;
|
2018-11-03 10:28:23 +00:00
|
|
|
char* ptr = (char*)section.address();
|
2018-10-25 10:06:00 +00:00
|
|
|
if (!ptr) {
|
|
|
|
kprintf("ELFLoader: failed to allocate section '%s'\n", section.name());
|
|
|
|
failed = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memset(ptr, 0, section.size());
|
2018-11-12 12:25:16 +00:00
|
|
|
#ifdef SUPPORT_RELOCATIONS
|
2018-10-25 10:06:00 +00:00
|
|
|
m_sections.set(section.name(), move(ptr));
|
2018-11-12 12:25:16 +00:00
|
|
|
#endif
|
2018-10-25 10:06:00 +00:00
|
|
|
return true;
|
|
|
|
});
|
2018-10-25 08:00:37 +00:00
|
|
|
return !failed;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 12:25:16 +00:00
|
|
|
#ifdef SUPPORT_RELOCATIONS
|
2018-10-10 09:53:07 +00:00
|
|
|
void* ELFLoader::lookup(const ELFImage::Symbol& symbol)
|
|
|
|
{
|
2018-11-04 13:09:30 +00:00
|
|
|
if (symbol.section().is_undefined())
|
2018-11-04 12:52:53 +00:00
|
|
|
return symbol_ptr(symbol.name());
|
2018-11-04 13:09:30 +00:00
|
|
|
return area_for_section(symbol.section()) + symbol.value();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2018-11-12 12:25:16 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-11-12 12:25:16 +00:00
|
|
|
#ifdef SUPPORT_RELOCATIONS
|
2018-11-04 13:09:30 +00:00
|
|
|
char* ELFLoader::area_for_section(const ELFImage::Section& section)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-11-04 13:09:30 +00:00
|
|
|
return area_for_section_name(section.name());
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 13:09:30 +00:00
|
|
|
char* ELFLoader::area_for_section_name(const char* name)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (auto it = m_sections.find(name); it != m_sections.end())
|
|
|
|
return (*it).value;
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-12 12:25:16 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-11-12 12:25:16 +00:00
|
|
|
#ifdef SUPPORT_RELOCATIONS
|
2018-11-04 13:09:30 +00:00
|
|
|
bool ELFLoader::perform_relocations()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-23 09:03:26 +00:00
|
|
|
#ifdef ELFLOADER_DEBUG
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("ELFLoader: Performing relocations\n");
|
2018-10-23 09:03:26 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-25 08:00:37 +00:00
|
|
|
bool failed = false;
|
|
|
|
|
2018-11-04 13:09:30 +00:00
|
|
|
m_image.for_each_section_of_type(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) -> bool {
|
2018-10-10 09:53:07 +00:00
|
|
|
auto& relocations = section.relocations();
|
2018-11-04 13:09:30 +00:00
|
|
|
if (relocations.is_undefined())
|
2018-10-25 08:00:37 +00:00
|
|
|
return true;
|
2018-11-04 13:09:30 +00:00
|
|
|
relocations.for_each_relocation([this, section, &failed] (const ELFImage::Relocation& relocation) {
|
2018-10-10 09:53:07 +00:00
|
|
|
auto symbol = relocation.symbol();
|
2018-11-04 13:09:30 +00:00
|
|
|
auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(area_for_section(section) + relocation.offset());
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
switch (relocation.type()) {
|
|
|
|
case R_386_PC32: {
|
2018-11-04 13:09:30 +00:00
|
|
|
char* target_ptr = (char*)lookup(symbol);
|
|
|
|
if (!target_ptr) {
|
2018-10-25 08:00:37 +00:00
|
|
|
kprintf("ELFLoader: unresolved symbol '%s'\n", symbol.name());
|
|
|
|
failed = true;
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
ptrdiff_t relative_offset = (char*)target_ptr - ((char*)&patch_ptr + 4);
|
2018-10-23 09:03:26 +00:00
|
|
|
#ifdef ELFLOADER_DEBUG
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("ELFLoader: Relocate PC32: offset=%x, symbol=%u(%s) value=%x target=%p, offset=%d\n",
|
2018-10-10 09:53:07 +00:00
|
|
|
relocation.offset(),
|
|
|
|
symbol.index(),
|
|
|
|
symbol.name(),
|
|
|
|
symbol.value(),
|
2018-11-04 13:09:30 +00:00
|
|
|
target_ptr,
|
2019-01-31 16:31:23 +00:00
|
|
|
relative_offset
|
2018-10-10 09:53:07 +00:00
|
|
|
);
|
2018-10-23 09:03:26 +00:00
|
|
|
#endif
|
2019-01-31 16:31:23 +00:00
|
|
|
patch_ptr = relative_offset;
|
2018-10-10 09:53:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_386_32: {
|
2018-10-23 09:03:26 +00:00
|
|
|
#ifdef ELFLOADER_DEBUG
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("ELFLoader: Relocate Abs32: symbol=%u(%s), value=%x, section=%s\n",
|
2018-10-10 09:53:07 +00:00
|
|
|
symbol.index(),
|
|
|
|
symbol.name(),
|
|
|
|
symbol.value(),
|
|
|
|
symbol.section().name()
|
|
|
|
);
|
2018-10-23 09:03:26 +00:00
|
|
|
#endif
|
2018-11-04 13:09:30 +00:00
|
|
|
char* target_ptr = area_for_section(symbol.section()) + symbol.value();
|
|
|
|
patch_ptr += (ptrdiff_t)target_ptr;
|
2018-10-10 09:53:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
break;
|
|
|
|
}
|
2018-10-25 08:00:37 +00:00
|
|
|
return true;
|
2018-10-10 09:53:07 +00:00
|
|
|
});
|
2018-10-25 08:00:37 +00:00
|
|
|
return !failed;
|
2018-10-10 09:53:07 +00:00
|
|
|
});
|
2018-10-25 08:00:37 +00:00
|
|
|
return !failed;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2018-11-12 12:25:16 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-11-12 12:25:16 +00:00
|
|
|
char* ELFLoader::symbol_ptr(const char* name)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-11-12 12:25:16 +00:00
|
|
|
char* found_ptr = nullptr;
|
2018-11-04 13:09:30 +00:00
|
|
|
m_image.for_each_symbol([&] (const ELFImage::Symbol symbol) {
|
2018-11-12 12:25:16 +00:00
|
|
|
if (symbol.type() != STT_FUNC)
|
|
|
|
return true;
|
|
|
|
if (strcmp(symbol.name(), name))
|
|
|
|
return true;
|
|
|
|
if (m_image.is_executable())
|
|
|
|
found_ptr = (char*)symbol.value();
|
|
|
|
#ifdef SUPPORT_RELOCATIONS
|
|
|
|
else if (m_image.is_relocatable())
|
|
|
|
found_ptr = area_for_section(symbol.section()) + symbol.value();
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
2018-10-10 09:53:07 +00:00
|
|
|
});
|
2018-11-12 12:25:16 +00:00
|
|
|
return found_ptr;
|
2018-11-04 12:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ELFLoader::allocate_section(LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable)
|
|
|
|
{
|
|
|
|
ASSERT(alloc_section_hook);
|
2019-01-30 15:28:51 +00:00
|
|
|
return alloc_section_hook(laddr, size, alignment, is_readable, is_writable, String::format("elf-alloc-%s%s", is_readable ? "r" : "", is_writable ? "w" : ""));
|
2018-11-04 12:52:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:20:09 +00:00
|
|
|
bool ELFLoader::map_section(LinearAddress laddr, size_t size, size_t alignment, size_t offset_in_image, bool is_readable, bool is_writable)
|
|
|
|
{
|
|
|
|
ASSERT(alloc_section_hook);
|
2019-01-30 15:28:51 +00:00
|
|
|
return map_section_hook(laddr, size, alignment, offset_in_image, is_readable, is_writable, String::format("elf-map-%s%s", is_readable ? "r" : "", is_writable ? "w" : ""));
|
2018-11-08 20:20:09 +00:00
|
|
|
}
|