Luna/kernel/src/binfmt/BinaryFormat.cpp

40 lines
946 B
C++
Raw Normal View History

#include "binfmt/BinaryFormat.h"
#include "binfmt/ELF.h"
#include "binfmt/Script.h"
struct BinaryFormatDescriptor
{
binfmt_loader_creator_t creator;
void* arg;
};
Vector<BinaryFormatDescriptor> g_binary_formats;
Result<void> BinaryFormat::init()
{
TRY(register_binary_format(ELFLoader::create, nullptr));
TRY(register_binary_format(ScriptLoader::create, nullptr));
return {};
}
Result<void> BinaryFormat::register_binary_format(binfmt_loader_creator_t creator, void* arg)
{
return g_binary_formats.try_append({ creator, arg });
}
Result<SharedPtr<BinaryFormatLoader>> BinaryFormat::create_loader(SharedPtr<VFS::Inode> inode)
{
for (const auto& format : g_binary_formats)
{
auto loader = TRY(format.creator(inode, format.arg));
if (TRY(loader->sniff())) return loader;
}
return err(ENOEXEC);
}
BinaryFormatLoader::BinaryFormatLoader(SharedPtr<VFS::Inode> inode) : m_inode(inode)
{
}