Build ELFLoader into Kernel.

This commit is contained in:
Andreas Kling 2018-10-18 15:03:10 +02:00
parent 3649638259
commit c149d2a8f0
Notes: sideshowbarker 2024-07-19 18:46:21 +09:00
7 changed files with 81 additions and 52 deletions

View File

@ -13,6 +13,8 @@ typedef signed long long int signed_qword;
typedef dword size_t; typedef dword size_t;
typedef signed_dword ssize_t; typedef signed_dword ssize_t;
typedef signed_dword ptrdiff_t;
#else #else
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>

View File

@ -1,13 +1,19 @@
#include "ELFImage.h" #include "ELFImage.h"
#include <cstdio> #include <AK/kstdio.h>
#include <cstring>
#include <cstdlib>
ELFImage::ELFImage(MappedFile&& file) #ifdef SERENITY_KERNEL
: m_file(std::move(file)) ELFImage::ELFImage(const byte* data)
: m_data(data)
{ {
m_isValid = parse(); m_isValid = parse();
} }
#else
ELFImage::ELFImage(MappedFile&& file)
: m_file(move(file))
{
m_isValid = parse();
}
#endif
ELFImage::~ELFImage() ELFImage::~ELFImage()
{ {
@ -41,43 +47,43 @@ unsigned ELFImage::symbolCount() const
void ELFImage::dump() void ELFImage::dump()
{ {
printf("AK::ELFImage{%p} {\n", this); kprintf("AK::ELFImage{%p} {\n", this);
printf(" isValid: %u\n", isValid()); kprintf(" isValid: %u\n", isValid());
if (!isValid()) { if (!isValid()) {
printf("}\n"); kprintf("}\n");
return; return;
} }
printf(" type: %s\n", objectFileTypeToString(header().e_type)); kprintf(" type: %s\n", objectFileTypeToString(header().e_type));
printf(" machine: %u\n", header().e_machine); kprintf(" machine: %u\n", header().e_machine);
printf(" entry: %08x\n", header().e_entry); kprintf(" entry: %08x\n", header().e_entry);
printf(" shoff: %u\n", header().e_shoff); kprintf(" shoff: %u\n", header().e_shoff);
printf(" shnum: %u\n", header().e_shnum); kprintf(" shnum: %u\n", header().e_shnum);
printf(" shstrndx: %u\n", header().e_shstrndx); kprintf(" shstrndx: %u\n", header().e_shstrndx);
for (unsigned i = 0; i < header().e_shnum; ++i) { for (unsigned i = 0; i < header().e_shnum; ++i) {
auto& section = this->section(i); auto& section = this->section(i);
printf(" Section %u: {\n", i); kprintf(" Section %u: {\n", i);
printf(" name: %s\n", section.name()); kprintf(" name: %s\n", section.name());
printf(" type: %x\n", section.type()); kprintf(" type: %x\n", section.type());
printf(" offset: %x\n", section.offset()); kprintf(" offset: %x\n", section.offset());
printf(" size: %u\n", section.size()); kprintf(" size: %u\n", section.size());
printf(" \n"); kprintf(" \n");
printf(" }\n"); kprintf(" }\n");
} }
printf("Symbol count: %u (table is %u)\n", symbolCount(), m_symbolTableSectionIndex); kprintf("Symbol count: %u (table is %u)\n", symbolCount(), m_symbolTableSectionIndex);
for (unsigned i = 1; i < symbolCount(); ++i) { for (unsigned i = 1; i < symbolCount(); ++i) {
auto& sym = symbol(i); auto& sym = symbol(i);
printf("Symbol @%u:\n", i); kprintf("Symbol @%u:\n", i);
printf(" Name: %s\n", sym.name()); kprintf(" Name: %s\n", sym.name());
printf(" In section: %s\n", sectionIndexToString(sym.sectionIndex())); kprintf(" In section: %s\n", sectionIndexToString(sym.sectionIndex()));
printf(" Value: %08x\n", sym.value()); kprintf(" Value: %08x\n", sym.value());
printf(" Size: %u\n", sym.size()); kprintf(" Size: %u\n", sym.size());
} }
printf("}\n"); kprintf("}\n");
} }
unsigned ELFImage::sectionCount() const unsigned ELFImage::sectionCount() const
@ -107,7 +113,7 @@ bool ELFImage::parse()
// Then create a name-to-index map. // Then create a name-to-index map.
for (unsigned i = 0; i < sectionCount(); ++i) { for (unsigned i = 0; i < sectionCount(); ++i) {
auto& section = this->section(i); auto& section = this->section(i);
m_sections.set(section.name(), std::move(i)); m_sections.set(section.name(), move(i));
} }
return true; return true;
} }
@ -130,7 +136,11 @@ const char* ELFImage::tableString(unsigned offset) const
const char* ELFImage::rawData(unsigned offset) const const char* ELFImage::rawData(unsigned offset) const
{ {
#ifdef SERENITY_KERNEL
return reinterpret_cast<const char*>(m_data) + offset;
#else
return reinterpret_cast<const char*>(m_file.pointer()) + offset; return reinterpret_cast<const char*>(m_file.pointer()) + offset;
#endif
} }
const Elf32_Ehdr& ELFImage::header() const const Elf32_Ehdr& ELFImage::header() const
@ -168,14 +178,14 @@ const ELFImage::RelocationSection ELFImage::Section::relocations() const
{ {
// FIXME: This is ugly. // FIXME: This is ugly.
char relocationSectionName[128]; char relocationSectionName[128];
sprintf(relocationSectionName, ".rel%s", name()); ksprintf(relocationSectionName, ".rel%s", name());
printf("looking for '%s'\n", relocationSectionName); kprintf("looking for '%s'\n", relocationSectionName);
auto relocationSection = m_image.lookupSection(relocationSectionName); auto relocationSection = m_image.lookupSection(relocationSectionName);
if (relocationSection.type() != SHT_REL) if (relocationSection.type() != SHT_REL)
return static_cast<const RelocationSection>(m_image.section(0)); return static_cast<const RelocationSection>(m_image.section(0));
printf("Found relocations for %s in %s\n", name(), relocationSection.name()); kprintf("Found relocations for %s in %s\n", name(), relocationSection.name());
return static_cast<const RelocationSection>(relocationSection); return static_cast<const RelocationSection>(relocationSection);
} }

View File

@ -1,13 +1,21 @@
#pragma once #pragma once
#include <AK/HashMap.h> #ifndef SERENITY_KERNEL
#include <AK/MappedFile.h> #include <AK/MappedFile.h>
#endif
#include <AK/OwnPtr.h> #include <AK/OwnPtr.h>
#include <AK/HashMap.h>
#include <AK/String.h>
#include "elf.h" #include "elf.h"
class ELFImage { class ELFImage {
public: public:
#ifdef SERENITY_KERNEL
explicit ELFImage(const byte* data);
#else
explicit ELFImage(MappedFile&&); explicit ELFImage(MappedFile&&);
#endif
~ELFImage(); ~ELFImage();
void dump(); void dump();
bool isValid() const { return m_isValid; } bool isValid() const { return m_isValid; }
@ -123,7 +131,11 @@ private:
const char* sectionHeaderTableString(unsigned offset) const; const char* sectionHeaderTableString(unsigned offset) const;
const char* sectionIndexToString(unsigned index); const char* sectionIndexToString(unsigned index);
#ifdef SERENITY_KERNEL
const byte* m_data;
#else
MappedFile m_file; MappedFile m_file;
#endif
HashMap<String, unsigned> m_sections; HashMap<String, unsigned> m_sections;
bool m_isValid { false }; bool m_isValid { false };
unsigned m_symbolTableSectionIndex { 0 }; unsigned m_symbolTableSectionIndex { 0 };

View File

@ -1,12 +1,10 @@
#include "ELFLoader.h" #include "ELFLoader.h"
#include <cstdio> #include <AK/kstdio.h>
#include <cstring>
#include <cstdlib>
ELFLoader::ELFLoader(ExecSpace& execSpace, MappedFile&& file) ELFLoader::ELFLoader(ExecSpace& execSpace, MappedFile&& file)
: m_execSpace(execSpace) : m_execSpace(execSpace)
{ {
m_image = make<ELFImage>(std::move(file)); m_image = make<ELFImage>(move(file));
} }
ELFLoader::~ELFLoader() ELFLoader::~ELFLoader()
@ -28,12 +26,12 @@ bool ELFLoader::load()
void ELFLoader::layout() void ELFLoader::layout()
{ {
printf("[ELFLoader] Layout\n"); kprintf("[ELFLoader] Layout\n");
m_image->forEachSectionOfType(SHT_PROGBITS, [this] (const ELFImage::Section& section) { m_image->forEachSectionOfType(SHT_PROGBITS, [this] (const ELFImage::Section& section) {
printf("[ELFLoader] Allocating progbits section: %s\n", section.name()); kprintf("[ELFLoader] Allocating progbits section: %s\n", section.name());
char* ptr = m_execSpace.allocateArea(section.name(), section.size()); char* ptr = m_execSpace.allocateArea(section.name(), section.size());
memcpy(ptr, section.rawData(), section.size()); memcpy(ptr, section.rawData(), section.size());
m_sections.set(section.name(), std::move(ptr)); m_sections.set(section.name(), move(ptr));
}); });
} }
@ -59,7 +57,7 @@ char* ELFLoader::areaForSectionName(const char* name)
void ELFLoader::performRelocations() void ELFLoader::performRelocations()
{ {
printf("[ELFLoader] Performing relocations\n"); kprintf("[ELFLoader] Performing relocations\n");
m_image->forEachSectionOfType(SHT_PROGBITS, [this] (const ELFImage::Section& section) { m_image->forEachSectionOfType(SHT_PROGBITS, [this] (const ELFImage::Section& section) {
auto& relocations = section.relocations(); auto& relocations = section.relocations();
@ -73,7 +71,7 @@ void ELFLoader::performRelocations()
case R_386_PC32: { case R_386_PC32: {
char* targetPtr = (char*)lookup(symbol); char* targetPtr = (char*)lookup(symbol);
ptrdiff_t relativeOffset = (char*)targetPtr - ((char*)&patchPtr + 4); ptrdiff_t relativeOffset = (char*)targetPtr - ((char*)&patchPtr + 4);
printf("[ELFLoader] Relocate PC32: offset=%08x, symbol=%u(%s) value=%08x target=%p, offset=%d\n", kprintf("[ELFLoader] Relocate PC32: offset=%08x, symbol=%u(%s) value=%08x target=%p, offset=%d\n",
relocation.offset(), relocation.offset(),
symbol.index(), symbol.index(),
symbol.name(), symbol.name(),
@ -85,7 +83,7 @@ void ELFLoader::performRelocations()
break; break;
} }
case R_386_32: { case R_386_32: {
printf("[ELFLoader] Relocate Abs32: symbol=%u(%s), value=%08x, section=%s\n", kprintf("[ELFLoader] Relocate Abs32: symbol=%u(%s), value=%08x, section=%s\n",
symbol.index(), symbol.index(),
symbol.name(), symbol.name(),
symbol.value(), symbol.value(),
@ -106,7 +104,7 @@ void ELFLoader::performRelocations()
void ELFLoader::exportSymbols() void ELFLoader::exportSymbols()
{ {
m_image->forEachSymbol([&] (const ELFImage::Symbol symbol) { m_image->forEachSymbol([&] (const ELFImage::Symbol symbol) {
printf("symbol: %u, type=%u, name=%s\n", symbol.index(), symbol.type(), symbol.name()); kprintf("symbol: %u, type=%u, name=%s\n", symbol.index(), symbol.type(), symbol.name());
if (symbol.type() == STT_FUNC) if (symbol.type() == STT_FUNC)
m_execSpace.addSymbol(symbol.name(), areaForSectionName(".text") + symbol.value(), symbol.size()); m_execSpace.addSymbol(symbol.name(), areaForSectionName(".text") + symbol.value(), symbol.size());
// FIXME: What about other symbol types? // FIXME: What about other symbol types?

View File

@ -1,7 +1,7 @@
#include "ExecSpace.h" #include "ExecSpace.h"
#include "ELFLoader.h" #include "ELFLoader.h"
#include <AK/TemporaryFile.h> #include <AK/TemporaryFile.h>
#include <unistd.h> #include <AK/Types.h>
ExecSpace::ExecSpace() ExecSpace::ExecSpace()
{ {
@ -19,7 +19,7 @@ void ExecSpace::initializeBuiltins()
bool ExecSpace::loadELF(MappedFile&& file) bool ExecSpace::loadELF(MappedFile&& file)
{ {
ELFLoader loader(*this, std::move(file)); ELFLoader loader(*this, move(file));
if (!loader.load()) if (!loader.load())
return false; return false;
printf("[ExecSpace] ELF loaded, symbol map now:\n"); printf("[ExecSpace] ELF loaded, symbol map now:\n");
@ -34,6 +34,8 @@ bool ExecSpace::loadELF(MappedFile&& file)
static void disassemble(const char* data, size_t length) static void disassemble(const char* data, size_t length)
{ {
#ifdef SERENITY_KERNEL
#else
if (!length) if (!length)
return; return;
@ -52,6 +54,7 @@ static void disassemble(const char* data, size_t length)
char cmdbuf[128]; char cmdbuf[128];
sprintf(cmdbuf, "nasm -f bin -o /dev/stdout %s | ndisasm -b32 -", temp.fileName().characters()); sprintf(cmdbuf, "nasm -f bin -o /dev/stdout %s | ndisasm -b32 -", temp.fileName().characters());
system(cmdbuf); system(cmdbuf);
#endif
} }
char* ExecSpace::symbolPtr(const char* name) char* ExecSpace::symbolPtr(const char* name)
@ -65,16 +68,15 @@ char* ExecSpace::symbolPtr(const char* name)
return nullptr; return nullptr;
} }
char* ExecSpace::allocateArea(String&& name, unsigned size) char* ExecSpace::allocateArea(String&& name, unsigned size)
{ {
char* ptr = static_cast<char*>(malloc(size)); char* ptr = static_cast<char*>(kmalloc(size));
ASSERT(ptr); ASSERT(ptr);
m_areas.append(make<Area>(std::move(name), ptr, size)); m_areas.append(make<Area>(move(name), ptr, size));
return ptr; return ptr;
} }
void ExecSpace::addSymbol(String&& name, char* ptr, unsigned size) void ExecSpace::addSymbol(String&& name, char* ptr, unsigned size)
{ {
m_symbols.set(std::move(name), { ptr, size }); m_symbols.set(move(name), { ptr, size });
} }

View File

@ -11,7 +11,7 @@ class ExecSpace {
public: public:
struct Area { struct Area {
Area(String&& n, char* m, unsigned s) Area(String&& n, char* m, unsigned s)
: name(std::move(n)) : name(move(n))
, memory(m) , memory(m)
, size(s) , size(s)
{ {

View File

@ -34,11 +34,16 @@ VFS_OBJS = \
../VirtualFileSystem/VirtualFileSystem.o \ ../VirtualFileSystem/VirtualFileSystem.o \
../VirtualFileSystem/FileHandle.o ../VirtualFileSystem/FileHandle.o
ELFLOADER_OBJS = \
../ELFLoader/ELFImage.o \
../ELFLoader/ELFLoader.o \
../ELFLoader/ExecSpace.o
AK_OBJS = \ AK_OBJS = \
../AK/String.o \ ../AK/String.o \
../AK/StringImpl.o ../AK/StringImpl.o
OBJS = $(KERNEL_OBJS) $(VFS_OBJS) $(AK_OBJS) OBJS = $(KERNEL_OBJS) $(VFS_OBJS) $(AK_OBJS) $(ELFLOADER_OBJS)
NASM = nasm NASM = nasm
KERNEL = kernel KERNEL = kernel