Bootloader: installer now uses banan os elf headers intead of Linux's

This commit is contained in:
Bananymous 2023-11-13 21:30:53 +02:00
parent 2c2ee6636f
commit cbb9422ee0
3 changed files with 24 additions and 19 deletions

View File

@ -11,4 +11,7 @@ set(SOURCES
)
add_executable(x86_64-banan_os-bootloader-installer ${SOURCES})
target_compile_options(x86_64-banan_os-bootloader-installer PUBLIC -O2 -std=c++20)
target_compile_options(x86_64-banan_os-bootloader-installer PRIVATE -O2 -std=c++20)
target_compile_definitions(x86_64-banan_os-bootloader-installer PRIVATE __arch=x86_64)
target_include_directories(x86_64-banan_os-bootloader-installer PRIVATE ${CMAKE_SOURCE_DIR}/../../LibELF/include)
target_include_directories(x86_64-banan_os-bootloader-installer PRIVATE ${CMAKE_SOURCE_DIR}/../../kernel/include)

View File

@ -1,5 +1,7 @@
#include "ELF.h"
#include <LibELF/Values.h>
#include <cassert>
#include <cerrno>
#include <cstring>
@ -8,6 +10,8 @@
#include <sys/mman.h>
#include <unistd.h>
using namespace LibELF;
ELFFile::ELFFile(std::string_view path)
: m_path(path)
{
@ -49,14 +53,14 @@ ELFFile::~ELFFile()
m_fd = -1;
}
const Elf64_Ehdr& ELFFile::elf_header() const
const ElfNativeFileHeader& ELFFile::elf_header() const
{
return *reinterpret_cast<Elf64_Ehdr*>(m_mmap);
return *reinterpret_cast<LibELF::ElfNativeFileHeader*>(m_mmap);
}
bool ELFFile::validate_elf_header() const
{
if (m_stat.st_size < sizeof(Elf64_Ehdr))
if (m_stat.st_size < sizeof(ElfNativeFileHeader))
{
std::cerr << m_path << " is too small to be a ELF executable" << std::endl;
return false;
@ -75,9 +79,13 @@ bool ELFFile::validate_elf_header() const
return false;
}
#if ARCH(x86_64)
if (elf_header.e_ident[EI_CLASS] != ELFCLASS64)
#elif ARCH(i386)
if (elf_header.e_ident[EI_CLASS] != ELFCLASS32)
#endif
{
std::cerr << m_path << " is not 64 bit ELF" << std::endl;
std::cerr << m_path << " architecture doesn't match" << std::endl;
return false;
}
@ -99,24 +107,18 @@ bool ELFFile::validate_elf_header() const
return false;
}
if (elf_header.e_machine != EM_X86_64)
{
std::cerr << m_path << " is not an x86_64 ELF file" << std::endl;
return false;
}
return true;
}
const Elf64_Shdr& ELFFile::section_header(std::size_t index) const
const ElfNativeSectionHeader& ELFFile::section_header(std::size_t index) const
{
const auto& elf_header = this->elf_header();
assert(index < elf_header.e_shnum);
const uint8_t* section_array_start = m_mmap + elf_header.e_shoff;
return *reinterpret_cast<const Elf64_Shdr*>(section_array_start + index * elf_header.e_shentsize);
return *reinterpret_cast<const ElfNativeSectionHeader*>(section_array_start + index * elf_header.e_shentsize);
}
std::string_view ELFFile::section_name(const Elf64_Shdr& section_header) const
std::string_view ELFFile::section_name(const ElfNativeSectionHeader& section_header) const
{
const auto& elf_header = this->elf_header();
assert(elf_header.e_shstrndx != SHN_UNDEF);

View File

@ -1,5 +1,7 @@
#pragma once
#include <LibELF/Types.h>
#include <cstdint>
#include <optional>
#include <span>
@ -7,15 +9,13 @@
#include <string>
#include <sys/stat.h>
#include <elf.h>
class ELFFile
{
public:
ELFFile(std::string_view path);
~ELFFile();
const Elf64_Ehdr& elf_header() const;
const LibELF::ElfNativeFileHeader& elf_header() const;
std::optional<std::span<const uint8_t>> find_section(std::string_view name) const;
bool success() const { return m_success; }
@ -23,8 +23,8 @@ public:
std::string_view path() const { return m_path; }
private:
const Elf64_Shdr& section_header(std::size_t index) const;
std::string_view section_name(const Elf64_Shdr&) const;
const LibELF::ElfNativeSectionHeader& section_header(std::size_t index) const;
std::string_view section_name(const LibELF::ElfNativeSectionHeader&) const;
bool validate_elf_header() const;
private: