diff --git a/bootloader/installer/CMakeLists.txt b/bootloader/installer/CMakeLists.txt index 65e3dc35f4..f2abc69aff 100644 --- a/bootloader/installer/CMakeLists.txt +++ b/bootloader/installer/CMakeLists.txt @@ -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) diff --git a/bootloader/installer/ELF.cpp b/bootloader/installer/ELF.cpp index e0a53815bd..bc2d0845ea 100644 --- a/bootloader/installer/ELF.cpp +++ b/bootloader/installer/ELF.cpp @@ -1,5 +1,7 @@ #include "ELF.h" +#include + #include #include #include @@ -8,6 +10,8 @@ #include #include +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(m_mmap); + return *reinterpret_cast(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(section_array_start + index * elf_header.e_shentsize); + return *reinterpret_cast(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); diff --git a/bootloader/installer/ELF.h b/bootloader/installer/ELF.h index d04812884e..a127be189f 100644 --- a/bootloader/installer/ELF.h +++ b/bootloader/installer/ELF.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -7,15 +9,13 @@ #include #include -#include - class ELFFile { public: ELFFile(std::string_view path); ~ELFFile(); - const Elf64_Ehdr& elf_header() const; + const LibELF::ElfNativeFileHeader& elf_header() const; std::optional> 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: