forked from Bananymous/banan-os
update main #1
|
@ -11,4 +11,7 @@ set(SOURCES
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(x86_64-banan_os-bootloader-installer ${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)
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include "ELF.h"
|
#include "ELF.h"
|
||||||
|
|
||||||
|
#include <LibELF/Values.h>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
@ -8,6 +10,8 @@
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
using namespace LibELF;
|
||||||
|
|
||||||
ELFFile::ELFFile(std::string_view path)
|
ELFFile::ELFFile(std::string_view path)
|
||||||
: m_path(path)
|
: m_path(path)
|
||||||
{
|
{
|
||||||
|
@ -49,14 +53,14 @@ ELFFile::~ELFFile()
|
||||||
m_fd = -1;
|
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
|
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;
|
std::cerr << m_path << " is too small to be a ELF executable" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
|
@ -75,9 +79,13 @@ bool ELFFile::validate_elf_header() const
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if ARCH(x86_64)
|
||||||
if (elf_header.e_ident[EI_CLASS] != ELFCLASS64)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,24 +107,18 @@ bool ELFFile::validate_elf_header() const
|
||||||
return false;
|
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;
|
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();
|
const auto& elf_header = this->elf_header();
|
||||||
assert(index < elf_header.e_shnum);
|
assert(index < elf_header.e_shnum);
|
||||||
const uint8_t* section_array_start = m_mmap + elf_header.e_shoff;
|
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();
|
const auto& elf_header = this->elf_header();
|
||||||
assert(elf_header.e_shstrndx != SHN_UNDEF);
|
assert(elf_header.e_shstrndx != SHN_UNDEF);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibELF/Types.h>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <span>
|
#include <span>
|
||||||
|
@ -7,15 +9,13 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <elf.h>
|
|
||||||
|
|
||||||
class ELFFile
|
class ELFFile
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ELFFile(std::string_view path);
|
ELFFile(std::string_view path);
|
||||||
~ELFFile();
|
~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;
|
std::optional<std::span<const uint8_t>> find_section(std::string_view name) const;
|
||||||
|
|
||||||
bool success() const { return m_success; }
|
bool success() const { return m_success; }
|
||||||
|
@ -23,8 +23,8 @@ public:
|
||||||
std::string_view path() const { return m_path; }
|
std::string_view path() const { return m_path; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Elf64_Shdr& section_header(std::size_t index) const;
|
const LibELF::ElfNativeSectionHeader& section_header(std::size_t index) const;
|
||||||
std::string_view section_name(const Elf64_Shdr&) const;
|
std::string_view section_name(const LibELF::ElfNativeSectionHeader&) const;
|
||||||
bool validate_elf_header() const;
|
bool validate_elf_header() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue