8#include <etl/singleton.h>
43 assert(elf_addr != 0U &&
"elf_addr is null");
45 elf_ = std::span<uint8_t>(
reinterpret_cast<uint8_t*
>(elf_addr), EI_NIDENT);
56 ehdr_ = *
reinterpret_cast<const Elf64_Ehdr*
>(
elf_.data());
59 size_t max_size = EI_NIDENT;
60 if (
ehdr_.e_phoff != 0) {
62 if (ph_end > max_size) {
66 if (
ehdr_.e_shoff != 0) {
68 if (sh_end > max_size) {
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;
80 elf_ = std::span<uint8_t>(
reinterpret_cast<uint8_t*
>(elf_addr), max_size);
82 phdr_ = std::span<Elf64_Phdr>(
83 reinterpret_cast<Elf64_Phdr*
>(
elf_.data() +
ehdr_.e_phoff),
86 shdr_ = std::span<Elf64_Shdr>(
87 reinterpret_cast<Elf64_Shdr*
>(
elf_.data() +
ehdr_.e_shoff),
90 const auto* shstrtab =
reinterpret_cast<const char*
>(
elf_.data()) +
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) {
133 if ((
elf_[EI_MAG0] != ELFMAG0) || (
elf_[EI_MAG1] != ELFMAG1) ||
134 (
elf_[EI_MAG2] != ELFMAG2) || (
elf_[EI_MAG3] != ELFMAG3)) {
144 if (
elf_[EI_CLASS] == ELFCLASS32) {
147 if (
elf_[EI_CLASS] != ELFCLASS64) {
auto CheckElfClass() const -> Expected< void >
KernelElf(const KernelElf &)=default
auto CheckElfMagic() const -> Expected< void >
std::span< uint8_t > elf_
auto operator=(KernelElf &&) -> KernelElf &=default
auto GetElfSize() const -> size_t
获取 elf 文件大小
std::span< Elf64_Shdr > shdr_
std::span< Elf64_Phdr > phdr_
KernelElf(KernelElf &&)=default
auto CheckElfIdentity() const -> Expected< void >
auto operator=(const KernelElf &) -> KernelElf &=default
KernelElf(uint64_t elf_addr)
构造函数
std::span< Elf64_Sym > symtab
符号表
std::expected< T, Error > Expected
std::expected 别名模板
etl::singleton< KernelElf > KernelElfSingleton
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
constexpr auto message() const -> const char *