Skip to content

Commit

Permalink
Merge ExecSpace into ELFLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Nov 4, 2018
1 parent d90b891 commit 422b540
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 120 deletions.
34 changes: 29 additions & 5 deletions ELFLoader/ELFLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

//#define ELFLOADER_DEBUG

ELFLoader::ELFLoader(ExecSpace& execSpace, ByteBuffer&& buffer)
: m_execSpace(execSpace)
ELFLoader::ELFLoader(ByteBuffer&& buffer)
{
m_image = make<ELFImage>(move(buffer));
}
Expand Down Expand Up @@ -43,7 +42,7 @@ bool ELFLoader::layout()
#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
m_execSpace.allocate_section(program_header.laddr(), program_header.size_in_memory(), program_header.alignment(), program_header.is_readable(), 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());
});

m_image->forEachSectionOfType(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) {
Expand Down Expand Up @@ -85,7 +84,7 @@ bool ELFLoader::layout()
void* ELFLoader::lookup(const ELFImage::Symbol& symbol)
{
if (symbol.section().isUndefined())
return m_execSpace.symbolPtr(symbol.name());
return symbol_ptr(symbol.name());
return areaForSection(symbol.section()) + symbol.value();
}

Expand Down Expand Up @@ -178,10 +177,35 @@ void ELFLoader::exportSymbols()
ptr = areaForSection(symbol.section()) + symbol.value();
else
ASSERT_NOT_REACHED();
m_execSpace.addSymbol(symbol.name(), ptr, symbol.size());
add_symbol(symbol.name(), ptr, symbol.size());
}
// FIXME: What about other symbol types?
return true;
});
}

char* ELFLoader::symbol_ptr(const char* name)
{
if (auto it = m_symbols.find(name); it != m_symbols.end()) {
auto& symbol = (*it).value;
#ifdef EXECSPACE_DEBUG
kprintf("[ELFLoader] symbol_ptr(%s) dump:\n", name);
disassemble(symbol.ptr, symbol.size);
#endif
return symbol.ptr;
}
return nullptr;
}

bool ELFLoader::allocate_section(LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable)
{
ASSERT(alloc_section_hook);
char namebuf[16];
ksprintf(namebuf, "elf-%s%s", is_readable ? "r" : "", is_writable ? "w" : "");
return alloc_section_hook(laddr, size, alignment, is_readable, is_writable, namebuf);
}

void ELFLoader::add_symbol(String&& name, char* ptr, unsigned size)
{
m_symbols.set(move(name), { ptr, size });
}
21 changes: 18 additions & 3 deletions ELFLoader/ELFLoader.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#pragma once

#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
#include "ExecSpace.h"
#include "ELFImage.h"

class ELFLoader {
public:
ELFLoader(ExecSpace&, ByteBuffer&&);
ELFLoader(ByteBuffer&&);
~ELFLoader();

bool load();
Function<void*(LinearAddress, size_t, size_t, bool, bool, const String&)> alloc_section_hook;
char* symbol_ptr(const char* name);
void add_symbol(String&& name, char* ptr, unsigned size);
bool allocate_section(LinearAddress, size_t, size_t alignment, bool is_readable, bool is_writable);

private:
bool layout();
Expand All @@ -21,7 +25,18 @@ class ELFLoader {
char* areaForSection(const ELFImage::Section&);
char* areaForSectionName(const char*);

ExecSpace& m_execSpace;
struct PtrAndSize {
PtrAndSize() { }
PtrAndSize(char* p, unsigned s)
: ptr(p)
, size(s)
{
}

char* ptr { nullptr };
unsigned size { 0 };
};
HashMap<String, PtrAndSize> m_symbols;
HashMap<String, char*> m_sections;
OwnPtr<ELFImage> m_image;
};
Expand Down
58 changes: 0 additions & 58 deletions ELFLoader/ExecSpace.cpp

This file was deleted.

43 changes: 0 additions & 43 deletions ELFLoader/ExecSpace.h

This file was deleted.

3 changes: 1 addition & 2 deletions Kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ VFS_OBJS = \

ELFLOADER_OBJS = \
../ELFLoader/ELFImage.o \
../ELFLoader/ELFLoader.o \
../ELFLoader/ExecSpace.o
../ELFLoader/ELFLoader.o

AK_OBJS = \
../AK/String.o \
Expand Down
14 changes: 6 additions & 8 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "system.h"
#include <VirtualFileSystem/FileHandle.h>
#include <VirtualFileSystem/VirtualFileSystem.h>
#include <ELFLoader/ExecSpace.h>
#include <ELFLoader/ELFLoader.h>
#include "MemoryManager.h"
#include "errno.h"
#include "i8253.h"
Expand Down Expand Up @@ -302,9 +302,6 @@ int Process::exec(const String& path, Vector<String>&& arguments, Vector<String>
PageDirectory* old_page_directory;
PageDirectory* new_page_directory;
{
ExecSpace space;
Region* region = nullptr;

InterruptDisabler disabler;
// Okay, here comes the sleight of hand, pay close attention..
auto old_regions = move(m_regions);
Expand All @@ -313,13 +310,14 @@ int Process::exec(const String& path, Vector<String>&& arguments, Vector<String>
MM.populate_page_directory(*new_page_directory);
m_page_directory = new_page_directory;
MM.enter_process_paging_scope(*this);
space.alloc_section_hook = [&] (LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable, const String& name) {
ELFLoader loader(move(elfData));
loader.alloc_section_hook = [&] (LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable, const String& name) {
ASSERT(size);
size = ((size / 4096) + 1) * 4096; // FIXME: Use ceil_div?
region = allocate_region(laddr, size, String(name), is_readable, is_writable);
(void) allocate_region(laddr, size, String(name), is_readable, is_writable);
return laddr.asPtr();
};
bool success = space.loadELF(move(elfData));
bool success = loader.load();
if (!success) {
m_page_directory = old_page_directory;
MM.enter_process_paging_scope(*this);
Expand All @@ -329,7 +327,7 @@ int Process::exec(const String& path, Vector<String>&& arguments, Vector<String>
return -ENOEXEC;
}

entry_eip = (dword)space.symbolPtr("_start");
entry_eip = (dword)loader.symbol_ptr("_start");
if (!entry_eip) {
m_page_directory = old_page_directory;
MM.enter_process_paging_scope(*this);
Expand Down
2 changes: 1 addition & 1 deletion Kernel/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ static void init_stage2()

ExecSpace space;
space.loadELF(move(testExecutableData));
auto* elf_entry = space.symbolPtr("_start");
auto* elf_entry = space.symbol_ptr("_start");
ASSERT(elf_entry);

typedef int (*MainFunctionPtr)(void);
Expand Down

0 comments on commit 422b540

Please sign in to comment.