-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection.h
More file actions
74 lines (68 loc) · 2.58 KB
/
section.h
File metadata and controls
74 lines (68 loc) · 2.58 KB
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
/*
* Auto-added header
* File: section.h
* Author: camradeling
* Email: camradeling@gmail.com
* 2025
*/
//------------------------------------------------------------------------------------------------------------------------------
#ifndef ELFMAN_SECTION_H
#define ELFMAN_SECTION_H
//------------------------------------------------------------------------------------------------------------------------------
#include <cstring>
#include <stdint.h>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>
#include <memory>
#include <map>
#include <elf.h>
//------------------------------------------------------------------------------------------------------------------------------
namespace ElfMan
{
//------------------------------------------------------------------------------------------------------------------------------
class ObjectFile;
//------------------------------------------------------------------------------------------------------------------------------
class Section {
public:
Section(Elf32_Shdr* header, ObjectFile* obj) {
memcpy(&shdr, header, sizeof(shdr));
object = obj;
}
virtual ~Section() = default;
virtual std::vector<uint8_t> serialize() = 0;
std::string name();
// getters
uint32_t name_index() { return shdr.sh_name; }
uint32_t offset() { return shdr.sh_offset; }
uint32_t size() { return shdr.sh_size; }
uint32_t link() { return shdr.sh_link; }
uint32_t info() { return shdr.sh_info; }
uint32_t addralign() { return shdr.sh_addralign; }
uint32_t type() const { return shdr.sh_type; }
const Elf32_Shdr* header() const { return &shdr; }
// setters
void offset(uint32_t off) { shdr.sh_offset = off; }
void info(uint32_t inf) { shdr.sh_info = inf; }
void size(uint32_t sz) { shdr.sh_size = sz; }
using FactoryFunc = std::function<std::shared_ptr<Section>(
Elf32_Shdr*, const uint8_t*, uint32_t, ObjectFile*)>;
// fabric method
static std::shared_ptr<Section> from_bytes(
Elf32_Shdr* header,
const uint8_t* buffer,
uint32_t total_sz,
ObjectFile* obj);
int index = 0;
static void register_factory(uint32_t sh_type, FactoryFunc func);
protected:
Elf32_Shdr shdr;
ObjectFile* object;
private:
static std::map<uint32_t, FactoryFunc>& registry();
};
//------------------------------------------------------------------------------------------------------------------------------
} //namespace ElfMan
//------------------------------------------------------------------------------------------------------------------------------
#endif/*ELFMAN_SECTION_H*/