forked from KokoSoft/Programmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Elf.hpp
79 lines (61 loc) · 1.59 KB
/
Elf.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2023 Koko Software. All rights reserved.
*
* Author: Adrian Warecki <[email protected]>
*/
#ifndef __ELF_HPP__
#define __ELF_HPP__
#include <fstream>
#include <filesystem>
#include <vector>
#include "elf.h"
namespace elf {
class StringsTable;
class SectionHeader : public Elf32_Shdr {
public:
SectionHeader();
SectionHeader(std::istream* stream, std::streamsize file_size = 0);
void update_name(const StringsTable &str);
std::string name_str;
};
class Section {
public:
void read(std::istream* stream, const SectionHeader* header,
std::streamsize file_size = 0);
protected:
SectionHeader header;
std::shared_ptr<unsigned char[]> buffer;
};
class StringsTable: public Section {
public:
std::string get(unsigned int index) const;
void print();
};
class SymbolTable: public Section {
public:
uint32_t get(std::string name);
void print(const StringsTable* str = nullptr);
};
class Elf {
public:
Elf(std::filesystem::path path);
void print();
void read_section(Section §ion, unsigned int index);
void read_section(Section §ion, std::string name);
int find_section(const std::string name);
protected:
std::ifstream file;
std::streamsize file_size;
std::vector<SectionHeader> sections;
std::vector<Elf32_Phdr> programs;
private:
Elf32_Ehdr file_header;
void read(void *buf, std::streamsize size);
void read_header();
void read_programs();
void read_sections();
void update_section_names();
};
};
#endif /* __ELF_HPP__ */