-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection.cpp
More file actions
68 lines (68 loc) · 2.94 KB
/
section.cpp
File metadata and controls
68 lines (68 loc) · 2.94 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
/*
* Auto-added header
* File: section.cpp
* Author: camradeling
* Email: camradeling@gmail.com
* 2025
*/
//------------------------------------------------------------------------------------------------------------------------------
#include <cstring>
//------------------------------------------------------------------------------------------------------------------------------
#include "memory_helpers.h"
#include "section.h"
#include "symbol_section.h"
#include "relocation_section.h"
#include "symbol.h"
#include "rel.h"
#include "object_file.h"
#include "logger.h"
//------------------------------------------------------------------------------------------------------------------------------
std::map<uint32_t, ElfMan::Section::FactoryFunc>& ElfMan::Section::registry() {
static std::map<uint32_t, ElfMan::Section::FactoryFunc> instance;
return instance;
}
//------------------------------------------------------------------------------------------------------------------------------
void ElfMan::Section::register_factory(uint32_t sh_type, ElfMan::Section::FactoryFunc func) {
registry()[sh_type] = std::move(func);
}
//------------------------------------------------------------------------------------------------------------------------------
std::shared_ptr<ElfMan::Section> ElfMan::Section::from_bytes(
Elf32_Shdr* header,
const uint8_t* buffer,
uint32_t total_sz,
ObjectFile* obj)
{
auto it = registry().find(header->sh_type);
if (it != registry().end()) {
return (it->second)(header, buffer, total_sz, obj);
}
// fallback — raw section
return std::make_shared<RawSection>(header, buffer, total_sz, obj);
}
//------------------------------------------------------------------------------------------------------------------------------
std::string ElfMan::Section::name()
{
if(!object)
return "*error1*";
std::shared_ptr<RawSection> strtab = object->section_strtab_section;
if(!strtab || shdr.sh_name >= strtab->shdr.sh_size)
return "*error2*";
return std::string((char*)&strtab->data.data()[shdr.sh_name]);
}
//------------------------------------------------------------------------------------------------------------------------------
bool ElfMan::RelocationSection::registered = []{
Section::register_factory(SHT_REL,
[](Elf32_Shdr* h, const uint8_t* b, uint32_t sz, ObjectFile* o) {
return std::make_shared<RelocationSection>(h, b, sz, o);
});
return true;
}();
//------------------------------------------------------------------------------------------------------------------------------
bool ElfMan::SymbolSection::registered = []{
Section::register_factory(SHT_SYMTAB,
[](Elf32_Shdr* h, const uint8_t* b, uint32_t sz, ObjectFile* o) {
return std::make_shared<SymbolSection>(h, b, sz, o);
});
return true;
}();
//------------------------------------------------------------------------------------------------------------------------------