LibELF: We use BAN::Vector<uint8_t> as elf storage

This is made possible by the dynamic kmalloc
This commit is contained in:
Bananymous 2023-06-18 23:29:23 +03:00
parent 9a7b2587af
commit 275a730485
2 changed files with 16 additions and 25 deletions

View File

@ -23,21 +23,12 @@ namespace LibELF
#ifdef __is_kernel #ifdef __is_kernel
BAN::ErrorOr<BAN::UniqPtr<ELF>> ELF::load_from_file(BAN::RefPtr<Inode> inode) BAN::ErrorOr<BAN::UniqPtr<ELF>> ELF::load_from_file(BAN::RefPtr<Inode> inode)
{ {
PageTable::current().lock(); BAN::Vector<uint8_t> buffer;
size_t page_count = BAN::Math::div_round_up<size_t>(inode->size(), PAGE_SIZE); TRY(buffer.resize(inode->size()));
vaddr_t vaddr = PageTable::current().get_free_contiguous_pages(page_count, (vaddr_t)g_kernel_end);
auto virtual_range = BAN::UniqPtr<VirtualRange>::adopt(
VirtualRange::create(
PageTable::current(),
vaddr, page_count * PAGE_SIZE,
PageTable::Flags::ReadWrite | PageTable::Flags::Present
)
);
PageTable::current().unlock();
TRY(inode->read(0, (void*)vaddr, inode->size())); TRY(inode->read(0, buffer.data(), inode->size()));
ELF* elf_ptr = new ELF(BAN::move(virtual_range), inode->size()); ELF* elf_ptr = new ELF(BAN::move(buffer));
if (elf_ptr == nullptr) if (elf_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);

View File

@ -57,16 +57,16 @@ namespace LibELF
bool is_x86_64() const; bool is_x86_64() const;
private: private:
#ifdef __is_kernel //#ifdef __is_kernel
ELF(BAN::UniqPtr<Kernel::VirtualRange>&& storage, size_t size) // ELF(BAN::UniqPtr<Kernel::VirtualRange>&& storage, size_t size)
: m_storage(BAN::move(storage)) // : m_storage(BAN::move(storage))
, m_data((const uint8_t*)m_storage->vaddr(), size) // , m_data((const uint8_t*)m_storage->vaddr(), size)
{} // {}
#else //#else
ELF(BAN::Vector<uint8_t>&& data) ELF(BAN::Vector<uint8_t>&& data)
: m_data(BAN::move(data)) : m_data(BAN::move(data))
{} {}
#endif //#endif
BAN::ErrorOr<void> load(); BAN::ErrorOr<void> load();
bool parse_elf64_file_header(const Elf64FileHeader&); bool parse_elf64_file_header(const Elf64FileHeader&);
@ -78,12 +78,12 @@ namespace LibELF
bool parse_elf32_section_header(const Elf32SectionHeader&); bool parse_elf32_section_header(const Elf32SectionHeader&);
private: private:
#ifdef __is_kernel //#ifdef __is_kernel
BAN::UniqPtr<Kernel::VirtualRange> m_storage; // BAN::UniqPtr<Kernel::VirtualRange> m_storage;
BAN::Span<const uint8_t> m_data; // BAN::Span<const uint8_t> m_data;
#else //#else
const BAN::Vector<uint8_t> m_data; const BAN::Vector<uint8_t> m_data;
#endif //#endif
}; };
} }