Skip to content

Commit

Permalink
Remove bootstrapping code from ELFLoader.
Browse files Browse the repository at this point in the history
This runs inside the kernel now, and I no longer need the emulated version.
  • Loading branch information
awesomekling committed Nov 4, 2018
1 parent 9bd0945 commit d90b891
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 196 deletions.
8 changes: 0 additions & 8 deletions ELFLoader/ELFImage.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
#include "ELFImage.h"
#include <AK/kstdio.h>

#ifdef SERENITY
ELFImage::ELFImage(ByteBuffer&& buffer)
: m_buffer(buffer)
{
m_isValid = parse();
}
#else
ELFImage::ELFImage(MappedFile&& file)
: m_file(move(file))
{
m_isValid = parse();
}
#endif

ELFImage::~ELFImage()
{
Expand Down
8 changes: 0 additions & 8 deletions ELFLoader/ELFImage.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#pragma once

#ifndef SERENITY
#include <AK/MappedFile.h>
#endif

#include <AK/OwnPtr.h>
#include <AK/HashMap.h>
#include <AK/String.h>
Expand All @@ -12,11 +8,7 @@

class ELFImage {
public:
#ifdef SERENITY
explicit ELFImage(ByteBuffer&&);
#else
explicit ELFImage(MappedFile&&);
#endif
~ELFImage();
void dump();
bool isValid() const { return m_isValid; }
Expand Down
15 changes: 6 additions & 9 deletions ELFLoader/ELFLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@

//#define ELFLOADER_DEBUG

#ifdef SERENITY
ELFLoader::ELFLoader(ExecSpace& execSpace, ByteBuffer&& file)
#else
ELFLoader::ELFLoader(ExecSpace& execSpace, MappedFile&& file)
#endif
ELFLoader::ELFLoader(ExecSpace& execSpace, ByteBuffer&& buffer)
: m_execSpace(execSpace)
{
m_image = make<ELFImage>(move(file));
m_image = make<ELFImage>(move(buffer));
}

ELFLoader::~ELFLoader()
Expand Down Expand Up @@ -58,9 +54,10 @@ bool ELFLoader::layout()
return true;
char* ptr = (char*)section.address();
if (!ptr) {
kprintf("ELFLoader: failed to allocate section '%s'\n", section.name());
failed = true;
return false;
#ifdef ELFLOADER_DEBUG
kprintf("ELFLoader: ignoring section '%s' with null address\n", section.name());
#endif
return true;
}
memcpy(ptr, section.rawData(), section.size());
m_sections.set(section.name(), move(ptr));
Expand Down
5 changes: 0 additions & 5 deletions ELFLoader/ELFLoader.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#pragma once

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

class ELFLoader {
public:
#ifdef SERENITY
ELFLoader(ExecSpace&, ByteBuffer&&);
#else
ELFLoader(ExecSpace&, MappedFile&&);
#endif
~ELFLoader();

bool load();
Expand Down
64 changes: 0 additions & 64 deletions ELFLoader/ExecSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,17 @@
#include "ELFLoader.h"
#include <AK/Types.h>

#ifndef SERENITY
#include <AK/TemporaryFile.h>
#endif

//#define EXECSPACE_DEBUG

ExecSpace::ExecSpace()
{
initializeBuiltins();
}

ExecSpace::~ExecSpace()
{
}

#ifdef SERENITY
int puts(const char* str)
{
kprintf("%s\n", str);
return 0;
}
#endif

void ExecSpace::initializeBuiltins()
{
#ifndef SERENITY
m_symbols.set("puts", { (char*)puts, 0 });
#endif
}

#ifdef SERENITY
bool ExecSpace::loadELF(ByteBuffer&& file)
#else
bool ExecSpace::loadELF(MappedFile&& file)
#endif
{
ELFLoader loader(*this, move(file));
if (!loader.load())
Expand All @@ -53,37 +29,6 @@ bool ExecSpace::loadELF(MappedFile&& file)
return true;
}

#ifdef EXECSPACE_DEBUG
static void disassemble(const char* data, size_t length)
{
if (!length)
return;

#ifdef SERENITY
for (unsigned i = 0; i < length; ++i) {
kprintf("%b ", (unsigned char)data[i]);
}
kprintf("\n");
#else
TemporaryFile temp;
if (!temp.isValid()) {
fprintf(stderr, "Unable to create temp file for disassembly.\n");
return;
}
fprintf(temp.stream(), "db ");
for (unsigned i = 0; i < length; ++i) {
fprintf(temp.stream(), "0x%02x, ", (unsigned char)data[i]);
}
fprintf(temp.stream(), "\n");
temp.sync();

char cmdbuf[128];
ksprintf(cmdbuf, "nasm -f bin -o /dev/stdout %s | ndisasm -b32 -", temp.fileName().characters());
system(cmdbuf);
#endif
}
#endif

char* ExecSpace::symbolPtr(const char* name)
{
if (auto it = m_symbols.find(name); it != m_symbols.end()) {
Expand All @@ -97,15 +42,6 @@ char* ExecSpace::symbolPtr(const char* name)
return nullptr;
}

void ExecSpace::allocateUniverse(size_t size)
{
ASSERT(!m_universe);
if (hookableAlloc)
m_universe = static_cast<char*>(hookableAlloc("elf-sec", size));
else
m_universe = static_cast<char*>(kmalloc(size));
}

bool ExecSpace::allocate_section(LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable)
{
ASSERT(alloc_section_hook);
Expand Down
12 changes: 1 addition & 11 deletions ELFLoader/ExecSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/MappedFile.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
#include <AK/String.h>
#include "types.h"

class ELFLoader;
Expand All @@ -26,28 +26,18 @@ class ExecSpace {
ExecSpace();
~ExecSpace();

Function<void*(const String&, size_t)> hookableAlloc;
Function<void*(LinearAddress, size_t, size_t, bool, bool, const String&)> alloc_section_hook;

#ifdef SERENITY
bool loadELF(ByteBuffer&&);
#else
bool loadELF(MappedFile&&);
#endif

char* symbolPtr(const char* name);

void addSymbol(String&& name, char* ptr, unsigned size);

void allocateUniverse(size_t);

bool allocate_section(LinearAddress, size_t, size_t alignment, bool is_readable, bool is_writable);

private:
void initializeBuiltins();

Vector<char*> m_allocated_regions;
HashMap<String, PtrAndSize> m_symbols;
char* m_universe { nullptr };
};

38 changes: 0 additions & 38 deletions ELFLoader/Makefile

This file was deleted.

29 changes: 0 additions & 29 deletions ELFLoader/_test.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions ELFLoader/test.cpp

This file was deleted.

0 comments on commit d90b891

Please sign in to comment.