forked from F8LEFT/SoFixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElfReader.h
122 lines (90 loc) · 3.35 KB
/
ElfReader.h
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//===------------------------------------------------------------*- C++ -*-===//
//
// Created by F8LEFT on 2017/6/3.
// Copyright (c) 2017. All rights reserved.
//===----------------------------------------------------------------------===//
// Parse and read elf file.
//===----------------------------------------------------------------------===//
#ifndef SOFIXER_ELFREADER_H
#define SOFIXER_ELFREADER_H
#include "macros.h"
#include "FileReader.h"
#include <cstdint>
#include <cstddef>
#include <memory.h>
class ElfRebuilder;
class ObElfReader;
class ElfReader {
public:
ElfReader();
virtual ~ElfReader();
virtual bool Load();
bool setSource(const char* source);
size_t phdr_count() { return phdr_num_; }
uint8_t * load_start() { return load_start_; }
Elf_Addr load_size() { return load_size_; }
uint8_t * load_bias() { return load_bias_; }
const Elf_Phdr* loaded_phdr() { return loaded_phdr_; }
const Elf_Ehdr* record_ehdr() { return &header_; }
protected:
bool ReadElfHeader();
bool VerifyElfHeader();
bool ReadProgramHeader();
bool ReserveAddressSpace(uint32_t padding_size = 0);
bool LoadSegments();
bool FindPhdr();
bool CheckPhdr(uint8_t *);
// If I have change anything in phtr_table_, just apply the chagnes into loaded_phdr.
void ApplyPhdrTable();
virtual void GetDynamicSection(Elf_Dyn** dynamic, size_t* dynamic_count, Elf_Word* dynamic_flags);
const char* name_;
FileReader* source_ = nullptr;
Elf_Ehdr header_;
size_t phdr_num_;
void* phdr_mmap_;
Elf_Phdr* phdr_table_;
Elf_Addr phdr_size_;
// First page of reserved address space.
uint8_t * load_start_;
// Size in bytes of reserved address space.
Elf_Addr load_size_;
Elf_Addr pad_size_;
size_t file_size;
// Load bias.
uint8_t * load_bias_;
// Loaded phdr.
const Elf_Phdr* loaded_phdr_;
private:
friend class ElfRebuilder;
friend class ObElfReader;
};
size_t
phdr_table_get_load_size(const Elf_Phdr* phdr_table,
size_t phdr_count,
Elf_Addr* min_vaddr = NULL,
Elf_Addr* max_vaddr = NULL);
int
phdr_table_protect_segments(const Elf_Phdr* phdr_table,
int phdr_count,
uint8_t * load_bias);
int
phdr_table_unprotect_segments(const Elf_Phdr* phdr_table,
int phdr_count,
uint8_t * load_bias);
int
phdr_table_protect_gnu_relro(const Elf_Phdr* phdr_table,
int phdr_count,
uint8_t *load_bias);
int phdr_table_get_arm_exidx(const Elf_Phdr* phdr_table,
int phdr_count,
uint8_t * load_bias,
Elf_Addr** arm_exidx,
unsigned* arm_exidix_count);
void
phdr_table_get_dynamic_section(const Elf_Phdr* phdr_table,
int phdr_count,
uint8_t * load_bias,
Elf_Dyn** dynamic,
size_t* dynamic_count,
Elf_Word* dynamic_flags);
#endif //SOFIXER_ELFREADER_H