SimpleKernel 1.17.0
Loading...
Searching...
No Matches
KernelElf Class Reference

ELF 文件相关 More...

#include <kernel_elf.hpp>

Collaboration diagram for KernelElf:
Collaboration graph

Public Member Functions

auto GetElfSize () const -> size_t
 获取 elf 文件大小
 
构造/析构函数
 KernelElf (uint64_t elf_addr)
 构造函数
 
 KernelElf ()=default
 
 KernelElf (const KernelElf &)=default
 
 KernelElf (KernelElf &&)=default
 
auto operator= (const KernelElf &) -> KernelElf &=default
 
auto operator= (KernelElf &&) -> KernelElf &=default
 
 ~KernelElf ()=default
 

Public Attributes

std::span< Elf64_Sym > symtab {}
 符号表
 
uint8_t * strtab {nullptr}
 字符串表
 

Protected Member Functions

auto CheckElfIdentity () const -> Expected< void >
 

Protected Attributes

elf 文件相关
std::span< uint8_t > elf_ {}
 
Elf64_Ehdr ehdr_ {}
 
std::span< Elf64_Phdr > phdr_ {}
 
std::span< Elf64_Shdr > shdr_ {}
 

Private Member Functions

auto CheckElfMagic () const -> Expected< void >
 
auto CheckElfClass () const -> Expected< void >
 

Detailed Description

ELF 文件相关

Definition at line 22 of file kernel_elf.hpp.

Constructor & Destructor Documentation

◆ KernelElf() [1/4]

KernelElf::KernelElf ( uint64_t  elf_addr)
inlineexplicit

构造函数

Parameters
elf_addrelf 地址

Definition at line 42 of file kernel_elf.hpp.

42 {
43 assert(elf_addr != 0U && "elf_addr is null");
44
45 elf_ = std::span<uint8_t>(reinterpret_cast<uint8_t*>(elf_addr), EI_NIDENT);
46
47 // 检查 elf 头数据
48 CheckElfIdentity().or_else([](Error err) -> Expected<void> {
49 klog::Err("KernelElf NOT valid ELF file: {}", err.message());
50 while (true) {
52 }
53 return {};
54 });
55
56 ehdr_ = *reinterpret_cast<const Elf64_Ehdr*>(elf_.data());
57
58 // 重新计算 elf 大小
59 size_t max_size = EI_NIDENT;
60 if (ehdr_.e_phoff != 0) {
61 size_t ph_end = ehdr_.e_phoff + ehdr_.e_phnum * ehdr_.e_phentsize;
62 if (ph_end > max_size) {
63 max_size = ph_end;
64 }
65 }
66 if (ehdr_.e_shoff != 0) {
67 size_t sh_end = ehdr_.e_shoff + ehdr_.e_shnum * ehdr_.e_shentsize;
68 if (sh_end > max_size) {
69 max_size = sh_end;
70 }
71 const auto* shdrs =
72 reinterpret_cast<const Elf64_Shdr*>(elf_.data() + ehdr_.e_shoff);
73 for (int i = 0; i < ehdr_.e_shnum; ++i) {
74 size_t section_end = shdrs[i].sh_offset + shdrs[i].sh_size;
75 if (section_end > max_size) {
76 max_size = section_end;
77 }
78 }
79 }
80 elf_ = std::span<uint8_t>(reinterpret_cast<uint8_t*>(elf_addr), max_size);
81
82 phdr_ = std::span<Elf64_Phdr>(
83 reinterpret_cast<Elf64_Phdr*>(elf_.data() + ehdr_.e_phoff),
84 ehdr_.e_phnum);
85
86 shdr_ = std::span<Elf64_Shdr>(
87 reinterpret_cast<Elf64_Shdr*>(elf_.data() + ehdr_.e_shoff),
88 ehdr_.e_shnum);
89
90 const auto* shstrtab = reinterpret_cast<const char*>(elf_.data()) +
91 shdr_[ehdr_.e_shstrndx].sh_offset;
92 for (auto shdr : shdr_) {
93 if (strcmp(shstrtab + shdr.sh_name, ".symtab") == 0) {
94 symtab = std::span<Elf64_Sym>(
95 reinterpret_cast<Elf64_Sym*>(elf_.data() + shdr.sh_offset),
96 (shdr.sh_size / sizeof(Elf64_Sym)));
97 } else if (strcmp(shstrtab + shdr.sh_name, ".strtab") == 0) {
98 strtab = elf_.data() + shdr.sh_offset;
99 }
100 }
101 }
std::span< uint8_t > elf_
std::span< Elf64_Shdr > shdr_
uint8_t * strtab
字符串表
std::span< Elf64_Phdr > phdr_
Elf64_Ehdr ehdr_
auto CheckElfIdentity() const -> Expected< void >
std::span< Elf64_Sym > symtab
符号表
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
void Pause()
Definition cpu_io.h:20
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
#define strcmp
错误类型,用于 std::expected
Definition expected.hpp:343
constexpr auto message() const -> const char *
Definition expected.hpp:358
Here is the call graph for this function:

◆ KernelElf() [2/4]

KernelElf::KernelElf ( )
default

◆ KernelElf() [3/4]

KernelElf::KernelElf ( const KernelElf )
default

◆ KernelElf() [4/4]

KernelElf::KernelElf ( KernelElf &&  )
default

◆ ~KernelElf()

KernelElf::~KernelElf ( )
default

Member Function Documentation

◆ CheckElfClass()

auto KernelElf::CheckElfClass ( ) const -> Expected<void>
inlineprivate

检查 ELF class (32/64 bit)

Definition at line 143 of file kernel_elf.hpp.

143 {
144 if (elf_[EI_CLASS] == ELFCLASS32) {
145 return std::unexpected(Error(ErrorCode::kElfUnsupported32Bit));
146 }
147 if (elf_[EI_CLASS] != ELFCLASS64) {
148 return std::unexpected(Error(ErrorCode::kElfInvalidClass));
149 }
150 return {};
151 }
@ kElfUnsupported32Bit
@ kElfInvalidClass
Here is the caller graph for this function:

◆ CheckElfIdentity()

auto KernelElf::CheckElfIdentity ( ) const -> Expected<void>
inlineprotected

检查 elf 标识

Returns
成功返回 Expected<void>,失败返回错误

Definition at line 124 of file kernel_elf.hpp.

124 {
125 return CheckElfMagic().and_then([this]() { return CheckElfClass(); });
126 }
auto CheckElfClass() const -> Expected< void >
auto CheckElfMagic() const -> Expected< void >
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckElfMagic()

auto KernelElf::CheckElfMagic ( ) const -> Expected<void>
inlineprivate

检查 ELF magic number

Definition at line 132 of file kernel_elf.hpp.

132 {
133 if ((elf_[EI_MAG0] != ELFMAG0) || (elf_[EI_MAG1] != ELFMAG1) ||
134 (elf_[EI_MAG2] != ELFMAG2) || (elf_[EI_MAG3] != ELFMAG3)) {
135 return std::unexpected(Error(ErrorCode::kElfInvalidMagic));
136 }
137 return {};
138 }
@ kElfInvalidMagic
Here is the caller graph for this function:

◆ GetElfSize()

auto KernelElf::GetElfSize ( ) const -> size_t
inline

获取 elf 文件大小

Returns
elf 文件大小

Definition at line 33 of file kernel_elf.hpp.

33{ return elf_.size(); }
Here is the caller graph for this function:

◆ operator=() [1/2]

auto KernelElf::operator= ( const KernelElf ) -> KernelElf &=default
default

◆ operator=() [2/2]

auto KernelElf::operator= ( KernelElf &&  ) -> KernelElf &=default
default

Member Data Documentation

◆ ehdr_

Elf64_Ehdr KernelElf::ehdr_ {}
protected

Definition at line 115 of file kernel_elf.hpp.

115{};

◆ elf_

std::span<uint8_t> KernelElf::elf_ {}
protected

Definition at line 114 of file kernel_elf.hpp.

114{};

◆ phdr_

std::span<Elf64_Phdr> KernelElf::phdr_ {}
protected

Definition at line 116 of file kernel_elf.hpp.

116{};

◆ shdr_

std::span<Elf64_Shdr> KernelElf::shdr_ {}
protected

Definition at line 117 of file kernel_elf.hpp.

117{};

◆ strtab

uint8_t* KernelElf::strtab {nullptr}

字符串表

Definition at line 27 of file kernel_elf.hpp.

27{nullptr};

◆ symtab

std::span<Elf64_Sym> KernelElf::symtab {}

符号表

Definition at line 25 of file kernel_elf.hpp.

25{};

The documentation for this class was generated from the following file: