-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathelf_unit.h
229 lines (201 loc) · 7.04 KB
/
elf_unit.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#pragma once
// (c) Robert Muth - see LICENSE for more info
#include <iostream>
#include <map>
#include <string_view>
#include <vector>
#include "BE/Elf/elfhelper.h"
#include "Util/assert.h"
namespace cwerg::elf {
template <typename elfsize_t>
struct Unit {
Section<elfsize_t>* const sec_rodata;
Section<elfsize_t>* const sec_text;
Section<elfsize_t>* const sec_data;
Section<elfsize_t>* const sec_bss;
std::vector<Reloc<elfsize_t>> relocations;
std::vector<std::unique_ptr<Symbol<elfsize_t>>> symbols;
std::map<std::string_view, Symbol<elfsize_t>*> global_symbol_map;
std::map<std::string_view, Symbol<elfsize_t>*> local_symbol_map;
std::map<std::string_view, std::unique_ptr<std::string>> string_table;
Unit()
: sec_rodata(new Section<elfsize_t>()),
sec_text(new Section<elfsize_t>()),
sec_data(new Section<elfsize_t>()),
sec_bss(new Section<elfsize_t>()) {
sec_rodata->InitRodata(1);
sec_text->InitText(1);
sec_data->InitData(1);
sec_bss->InitBss(1);
}
// Alternatively, manually populate it with the primitives below.
// See ParseFromAsm's implementation for inspiration.
void FunStart(std::string_view name,
unsigned alignment,
std::string_view padding) {
ASSERT(current_fun_name.empty(), "");
current_fun_name = name;
sec_text->PadData(alignment, padding);
AddSymbol(name, sec_text, false);
}
void FunStart(std::string_view name,
unsigned alignment,
std::function<void(size_t, std::string*)> padder) {
ASSERT(current_fun_name.empty(), "");
current_fun_name = name;
sec_text->PadData(alignment, padder);
AddSymbol(name, sec_text, false);
}
void AddLabel(std::string_view name,
unsigned alignment,
std::string_view padding) {
sec_text->PadData(alignment, padding);
ASSERT(!current_fun_name.empty(), "");
AddSymbol(name, sec_text, true);
}
void AddLabel(std::string_view name,
unsigned alignment,
std::function<void(size_t, std::string*)> padder) {
sec_text->PadData(alignment, padder);
ASSERT(!current_fun_name.empty(), "");
AddSymbol(name, sec_text, true);
}
void FunEnd() {
ASSERT(!current_fun_name.empty(), "");
current_fun_name = "";
local_symbol_map.clear();
}
void MemStart(std::string_view name,
unsigned alignment,
std::string_view kind,
std::string_view padding,
bool is_local) {
ASSERT(current_mem_sec == nullptr, "");
if (kind == "rodata") {
current_mem_sec = sec_rodata;
} else if (kind == "data") {
current_mem_sec = sec_data;
} else if (kind == "bss") {
current_mem_sec = sec_bss;
} else {
ASSERT(false, "bad mem kind " << kind);
}
current_mem_sec->PadData(alignment, padding);
AddSymbol(name, current_mem_sec, is_local);
}
void AddData(unsigned repeats, const void* data, size_t len) {
ASSERT(current_mem_sec != nullptr, "");
for (unsigned i = 0; i < repeats; ++i) {
current_mem_sec->AddData({(const char*)data, len});
}
}
void AddFunAddr(unsigned size,
uint32_t reloc_kind,
std::string_view fun_name) {
ASSERT(current_mem_sec != nullptr, "");
auto* sym = FindOrAddSymbol(fun_name, false);
AddReloc(reloc_kind, current_mem_sec, sym, 0);
current_mem_sec->AddDataRepeatedBytes(size, 0);
}
void AddBblAddr(unsigned size,
uint32_t reloc_kind,
std::string_view bbl_name) {
ASSERT(current_mem_sec != nullptr, "add bbl addr outside a mem directive");
auto* sym = FindOrAddSymbol(bbl_name, true);
AddReloc(reloc_kind, current_mem_sec, sym, 0);
current_mem_sec->AddDataRepeatedBytes(size, 0);
}
void AddMemAddr(unsigned size,
uint32_t reloc_kind,
std::string_view mem_name,
uint32_t addend) {
ASSERT(current_mem_sec != nullptr, "memaddr outside of mem");
ASSERT(addend == 0, "NYI");
auto* sym = FindOrAddSymbol(mem_name, false);
AddReloc(reloc_kind, current_mem_sec, sym, 0);
current_mem_sec->AddDataRepeatedBytes(size, 0);
}
void MemEnd() {
ASSERT(current_mem_sec != nullptr, "");
current_mem_sec = nullptr;
}
std::string_view InternString(std::string_view s) {
auto it = string_table.find(s);
if (it != string_table.end()) return it->first;
auto* sp = new std::string(s.data(), s.size());
std::string_view key(*sp);
string_table[key] = std::unique_ptr<std::string>(sp);
return key;
}
// Add a symbol for Section sec at the current offset
Symbol<elfsize_t>* AddSymbol(std::string_view name,
Section<elfsize_t>* sec,
bool is_local) {
name = InternString(name);
auto* the_map = is_local ? &local_symbol_map : &global_symbol_map;
auto it = the_map->find(name);
Symbol<elfsize_t>* sym = it == the_map->end() ? nullptr : it->second;
if (sym == nullptr) {
sym = new Symbol<elfsize_t>();
sym->Init(name, is_local, sec, sec == nullptr ? ~0U : sec->data->size());
symbols.emplace_back(std::unique_ptr<Symbol<elfsize_t>>(sym));
(*the_map)[name] = sym;
return sym;
}
if (sec != nullptr) {
ASSERT(sym->section == nullptr,
"Symbol " << name << " already has sec local:" << is_local);
sym->section = sec;
sym->sym.st_value = sec->data->size();
}
return sym;
}
Symbol<elfsize_t>* FindOrAddSymbol(std::string_view name, bool is_local) {
auto* the_map = is_local ? &local_symbol_map : &global_symbol_map;
auto it = the_map->find(name);
if (it != the_map->end()) {
return it->second;
} else {
return AddSymbol(name, nullptr, is_local);
}
}
// Add a Reloc for Section sec at the current offset
void AddReloc(uint32_t reloc_kind,
const Section<elfsize_t>* sec,
const Symbol<elfsize_t>* sym,
int32_t addend,
uint32_t reloc_offset_addend = 0) {
relocations.emplace_back(Reloc<elfsize_t>());
relocations.back().Init(
reloc_kind, sec, sec->data->size() + reloc_offset_addend, sym, addend);
}
void AddLinkerDefs() {
if (sec_bss->shdr.sh_size > 0) {
sec_bss->PadData(16, {"\0", 1});
AddSymbol("$$rw_data_end", sec_bss, false);
} else if (sec_data->shdr.sh_size > 0) {
sec_data->PadData(16, {"\0", 1});
AddSymbol("$$rw_data_end", sec_data, false);
}
}
private:
std::string_view current_fun_name = "";
Section<elfsize_t>* current_mem_sec = nullptr;
};
template <typename elfsize_t>
std::ostream& operator<<(std::ostream& os, const Unit<elfsize_t>& s) {
os << *s.sec_text << "\n"
<< *s.sec_rodata << "\n"
<< *s.sec_data << "\n"
<< *s.sec_bss << "\n";
os << "String Table\n";
for (const auto& sym : s.symbols) {
os << *sym << "\n";
}
os << "Reloc Table\n";
for (const auto& rel : s.relocations) {
os << rel << "\n";
}
return os;
}
} // namespace cwerg::elf