Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/util/elf_file.h
4214 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "common/heap_array.h"
7
#include "common/types.h"
8
9
#include <functional>
10
11
class Error;
12
13
class ELFFile
14
{
15
public:
16
using DataArray = DynamicHeapArray<u8>;
17
18
// ELF header constants
19
static constexpr u8 EI_NIDENT = 16;
20
static constexpr u16 ET_EXEC = 2;
21
static constexpr u16 ET_DYN = 3;
22
static constexpr u16 EM_MIPS = 8;
23
static constexpr u16 SHN_UNDEF = 0;
24
static constexpr u32 SHT_NULL = 0;
25
static constexpr u32 SHT_PROGBITS = 1;
26
static constexpr u32 SHT_SYMTAB = 2;
27
static constexpr u32 SHT_STRTAB = 3;
28
static constexpr u32 SHT_RELA = 4;
29
static constexpr u32 SHT_HASH = 5;
30
static constexpr u32 SHT_DYNAMIC = 6;
31
static constexpr u32 SHT_NOTE = 7;
32
static constexpr u32 SHT_NOBITS = 8;
33
static constexpr u32 SHT_REL = 9;
34
static constexpr u32 SHT_SHLIB = 10;
35
static constexpr u32 SHT_DYNSYM = 11;
36
static constexpr u32 SHT_NUM = 12;
37
static constexpr u32 PT_NULL = 0;
38
static constexpr u32 PT_LOAD = 1;
39
static constexpr u32 PT_DYNAMIC = 2;
40
static constexpr u32 PT_INTERP = 3;
41
static constexpr u32 PT_NOTE = 4;
42
static constexpr u32 PT_SHLIB = 5;
43
static constexpr u32 PT_PHDR = 6;
44
static constexpr u32 PT_TLS = 7;
45
46
// ELF Header structure
47
struct Elf32_Ehdr
48
{
49
u8 e_ident[EI_NIDENT]; // Magic number and other information
50
u16 e_type; // Object file type
51
u16 e_machine; // Architecture
52
u32 e_version; // Object file version
53
u32 e_entry; // Entry point virtual address
54
u32 e_phoff; // Program header table file offset
55
u32 e_shoff; // Section header table file offset
56
u32 e_flags; // Processor-specific flags
57
u16 e_ehsize; // ELF header size in bytes
58
u16 e_phentsize; // Program header table entry size
59
u16 e_phnum; // Program header table entry count
60
u16 e_shentsize; // Section header table entry size
61
u16 e_shnum; // Section header table entry count
62
u16 e_shstrndx; // Section header string table index
63
};
64
65
// Section header structure
66
struct Elf32_Shdr
67
{
68
u32 sh_name; // Section name (string tbl index)
69
u32 sh_type; // Section type
70
u32 sh_flags; // Section flags
71
u32 sh_addr; // Section virtual addr at execution
72
u32 sh_offset; // Section file offset
73
u32 sh_size; // Section size in bytes
74
u32 sh_link; // Link to another section
75
u32 sh_info; // Additional section information
76
u32 sh_addralign; // Section alignment
77
u32 sh_entsize; // Entry size if section holds table
78
};
79
80
// Program header structure
81
struct Elf32_Phdr
82
{
83
u32 p_type;
84
u32 p_offset;
85
u32 p_vaddr;
86
u32 p_paddr;
87
u32 p_filesz;
88
u32 p_memsz;
89
u32 p_flags;
90
u32 p_align;
91
};
92
93
public:
94
ELFFile();
95
~ELFFile();
96
97
static bool IsValidElfHeader(const std::span<const u8> data, Error* error = nullptr);
98
static bool IsValidElfHeader(const Elf32_Ehdr& header, Error* error = nullptr);
99
100
const Elf32_Ehdr& GetELFHeader() const;
101
u32 GetEntryPoint() const;
102
103
const Elf32_Shdr* GetSectionHeader(u32 index) const;
104
std::string_view GetSectionName(const Elf32_Shdr& section) const;
105
u32 GetSectionCount() const;
106
107
const Elf32_Phdr* GetProgramHeader(u32 index) const;
108
u32 GetProgramHeaderCount() const;
109
110
bool Open(const char* path, Error* error);
111
bool Open(DataArray data, Error* error);
112
113
using LoadExecutableSectionCallback =
114
std::function<bool(std::span<const u8> data, u32 dest_vaddr, u32 dest_size, Error* error)>;
115
bool LoadExecutableSections(const LoadExecutableSectionCallback& callback, Error* error) const;
116
117
private:
118
DataArray m_data;
119
};
120
121