forked from F8LEFT/SoFixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElfRebuilder.h
134 lines (104 loc) · 3.27 KB
/
ElfRebuilder.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
123
124
125
126
127
128
129
130
131
132
133
134
//===------------------------------------------------------------*- C++ -*-===//
//
// Created by F8LEFT on 2017/6/4.
// Copyright (c) 2017. All rights reserved.
//===----------------------------------------------------------------------===//
// Rebuild elf file with ElfReader
//===----------------------------------------------------------------------===//
#ifndef SOFIXER_ELFREBUILDER_H
#define SOFIXER_ELFREBUILDER_H
#include <cstdint>
#include "ElfReader.h"
#include <vector>
#include <string>
#define SOINFO_NAME_LEN 128
struct soinfo {
public:
const char* name = "name";
const Elf_Phdr* phdr = nullptr;
size_t phnum = 0;
Elf_Addr entry = 0;
uint8_t * base = 0;
unsigned size = 0;
Elf_Addr min_load;
Elf_Addr max_load;
uint32_t unused1 = 0; // DO NOT USE, maintained for compatibility.
Elf_Dyn* dynamic = nullptr;
size_t dynamic_count = 0;
Elf_Word dynamic_flags = 0;
uint32_t unused2 = 0; // DO NOT USE, maintained for compatibility
uint32_t unused3 = 0; // DO NOT USE, maintained for compatibility
unsigned flags = 0;
const char* strtab = nullptr;
Elf_Sym* symtab = nullptr;
uint8_t * hash = 0;
size_t strtabsize = 0;
size_t nbucket = 0;
size_t nchain = 0;
unsigned* bucket = nullptr;
unsigned* chain = nullptr;
Elf_Addr * plt_got = nullptr;
Elf_Rel* plt_rel = nullptr;
size_t plt_rel_count = 0;
Elf_Rel* rel = nullptr;
size_t rel_count = 0;
void* preinit_array = nullptr;
size_t preinit_array_count = 0;
void** init_array = nullptr;
size_t init_array_count = 0;
void** fini_array = nullptr;
size_t fini_array_count = 0;
void* init_func = nullptr;
void* fini_func = nullptr;
// ARM EABI section used for stack unwinding.
Elf_Addr * ARM_exidx = nullptr;
size_t ARM_exidx_count = 0;
unsigned mips_symtabno = 0;
unsigned mips_local_gotno = 0;
unsigned mips_gotsym = 0;
// When you read a virtual address from the ELF file, add this
// value to get the corresponding address in the process' address space.
uint8_t * load_bias = nullptr;
bool has_text_relocations = false;
bool has_DT_SYMBOLIC = false;
};
class ElfRebuilder {
public:
ElfRebuilder(ElfReader* elf_reader);
~ElfRebuilder() { if(rebuild_data != nullptr) delete []rebuild_data; }
bool Rebuild();
void* getRebuildData() { return rebuild_data; }
size_t getRebuildSize() { return rebuild_size; }
private:
bool RebuildPhdr();
bool RebuildShdr();
bool ReadSoInfo();
bool RebuildRelocs();
bool RebuildFin();
ElfReader* elf_reader_;
soinfo si;
int rebuild_size = 0;
uint8_t * rebuild_data = nullptr;
Elf_Word sDYNSYM = 0;
Elf_Word sDYNSTR = 0;
Elf_Word sHASH = 0;
Elf_Word sRELDYN = 0;
Elf_Word sRELPLT = 0;
Elf_Word sPLT = 0;
Elf_Word sTEXTTAB = 0;
Elf_Word sARMEXIDX = 0;
Elf_Word sFINIARRAY = 0;
Elf_Word sINITARRAY = 0;
Elf_Word sDYNAMIC = 0;
Elf_Word sGOT = 0;
Elf_Word sDATA = 0;
Elf_Word sBSS = 0;
Elf_Word sSHSTRTAB = 0;
std::vector<Elf_Shdr> shdrs;
std::string shstrtab;
private:
bool isPatchInit = false;
public:
void setPatchInit(bool b) { isPatchInit = b; }
};
#endif //SOFIXER_ELFREBUILDER_H