forked from GJDuck/e9patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe9elf.h
More file actions
163 lines (138 loc) · 4.19 KB
/
e9elf.h
File metadata and controls
163 lines (138 loc) · 4.19 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (C) 2022 National University of Singapore
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __E9ELF_H
#define __E9ELF_H
#include <list>
#include <map>
#include <string>
#include <vector>
#include <cstdint>
#include <elf.h>
#include "e9tool.h"
#include "e9types.h"
#ifndef PT_GNU_PROPERTY
#define PT_GNU_PROPERTY 0x6474e553
#define NT_GNU_PROPERTY_TYPE_0 5
#define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002
#define GNU_PROPERTY_X86_FEATURE_1_IBT 0x1
#define GNU_PROPERTY_X86_FEATURE_1_SHSTK 0x2
#endif
/*
* GNU_PROPERTY handling.
*/
struct property_s
{
uint32_t type;
uint32_t datasz;
uint8_t data[];
};
/*
* Symbol.
*/
struct Symbol
{
const char * const name; // Symbol name
const TypeSig sig; // Symbol typesig.
bool operator<(const Symbol &sym) const
{
int cmp = strcmp(name, sym.name);
if (cmp != 0)
return (cmp < 0);
return (sig < sym.sig);
}
Symbol(const char *name, TypeSig sig) : name(name), sig(sig)
{
;
}
};
/*
* Symbol cache. INTPTR_MIN=missing, (>0)=original, (<0)=derived.
*/
typedef std::map<Symbol, intptr_t> Symbols;
/*
* ELF file.
*/
namespace e9tool
{
struct ELF
{
// Data
const char *filename; // Filename.
const uint8_t *data; // File data.
size_t size; // File size.
intptr_t base; // Base address.
intptr_t end; // End address.
// Strtab
const char *strs;
// Program headers
const Elf64_Phdr *phdrs; // Elf PHDRs.
size_t phnum; // Number of PHDRs.
// Sections
SectionInfo sections;
// Executable sections (sorted by offset)
std::vector<const Elf64_Shdr *> exes;
// Symbols
SymbolInfo dynsyms;
SymbolInfo syms; // Only if not stripped
// GOT
GOTInfo got;
// PLT
PLTInfo plt;
BinaryType type; // Binary type.
bool reloc; // Needs relocation?
bool dynlink; // Dynamically linked?
struct
{
bool ibt; // Intel CET: Indirect Branch Tracking
bool shstk; // Intel CET: Shadow Stack
} cet;
Targets targets; // Jump/Call targets [optional]
BBs bbs; // Basic blocks [optional]
Fs fs; // Functions [optional]
mutable Symbols symbols; // Symbol cache.
std::list<Elf64_Shdr> sec_cache;// Extra allocated sections (PE).
std::list<Elf64_Sym> sym_cache; // Extra allocated symbols (PE).
std::string str_cache; // Extra allocated strings (PE).
mutable bool emitted = false; // Emitted?
};
};
/*
* Call trampoline object.
*/
namespace e9tool
{
struct Call
{
const CallABI abi;
const CallJump jmp;
const PatchPos pos;
const bool state;
const ELF * const target;
const char *const entry;
const std::vector<ArgumentKind> args;
Call(CallABI abi, CallJump jmp, PatchPos pos, bool state,
const ELF *target, const char *entry,
const std::vector<ArgumentKind> &args) :
abi(abi), jmp(jmp), pos(pos), state(state), target(target),
entry(entry),
args(args) // copy
{
;
}
};
};
#endif