-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsleighbase.cc
284 lines (255 loc) · 8.39 KB
/
sleighbase.cc
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sleighbase.hh"
namespace ghidra {
const int4 SleighBase::SLA_FORMAT_VERSION = 3;
const uint4 SleighBase::MAX_UNIQUE_SIZE = 128;
int4 SourceFileIndexer::index(const string filename){
auto it = fileToIndex.find(filename);
if (fileToIndex.end() != it){
return it->second;
}
fileToIndex[filename] = leastUnusedIndex;
indexToFile[leastUnusedIndex] = filename;
return leastUnusedIndex++;
}
int4 SourceFileIndexer::getIndex(string filename){
return fileToIndex[filename];
}
string SourceFileIndexer::getFilename(int4 index){
return indexToFile[index];
}
void SourceFileIndexer::restoreXml(const Element *el){
const List &sourceFiles(el->getChildren());
List::const_iterator iter = sourceFiles.begin();
for (; iter != sourceFiles.end(); ++iter){
string filename = (*iter)->getAttributeValue("name");
int4 index = stoi((*iter)->getAttributeValue("index"),NULL,10);
fileToIndex[filename] = index;
indexToFile[index] = filename;
}
}
void SourceFileIndexer::saveXml(ostream& s) const {
s << "<sourcefiles>\n";
for (int4 i = 0; i < leastUnusedIndex; ++i){
s << ("<sourcefile name=\"");
const char *str = indexToFile.at(i).c_str();
xml_escape(s,str);
s << "\" index=\"" << dec << i << "\"/>\n";
}
s << "</sourcefiles>\n";
}
SleighBase::SleighBase(void)
{
root = (SubtableSymbol *)0;
maxdelayslotbytes = 0;
unique_allocatemask = 0;
numSections = 0;
}
/// Assuming the symbol table is populated, iterate through the table collecting
/// registers (for the map), user-op names, and context fields.
void SleighBase::buildXrefs(vector<string> &errorPairs)
{
SymbolScope *glb = symtab.getGlobalScope();
SymbolTree::const_iterator iter;
SleighSymbol *sym;
ostringstream s;
for(iter=glb->begin();iter!=glb->end();++iter) {
sym = *iter;
if (sym->getType() == SleighSymbol::varnode_symbol) {
pair<VarnodeData,string> ins(((VarnodeSymbol *)sym)->getFixedVarnode(),sym->getName());
pair<map<VarnodeData,string>::iterator,bool> res = varnode_xref.insert(ins);
if (!res.second) {
errorPairs.push_back(sym->getName());
errorPairs.push_back((*(res.first)).second);
}
}
else if (sym->getType() == SleighSymbol::userop_symbol) {
int4 index = ((UserOpSymbol *)sym)->getIndex();
while(userop.size() <= index)
userop.push_back("");
userop[index] = sym->getName();
}
else if (sym->getType() == SleighSymbol::context_symbol) {
ContextSymbol *csym = (ContextSymbol *)sym;
ContextField *field = (ContextField *)csym->getPatternValue();
int4 startbit = field->getStartBit();
int4 endbit = field->getEndBit();
registerContext(csym->getName(),startbit,endbit);
}
}
}
/// If \b this SleighBase is being reused with a new program, the context
/// variables need to be registered with the new program's database
void SleighBase::reregisterContext(void)
{
SymbolScope *glb = symtab.getGlobalScope();
SymbolTree::const_iterator iter;
SleighSymbol *sym;
for(iter=glb->begin();iter!=glb->end();++iter) {
sym = *iter;
if (sym->getType() == SleighSymbol::context_symbol) {
ContextSymbol *csym = (ContextSymbol *)sym;
ContextField *field = (ContextField *)csym->getPatternValue();
int4 startbit = field->getStartBit();
int4 endbit = field->getEndBit();
registerContext(csym->getName(),startbit,endbit);
}
}
}
const VarnodeData &SleighBase::getRegister(const string &nm) const
{
VarnodeSymbol *sym = (VarnodeSymbol *)findSymbol(nm);
if (sym == (VarnodeSymbol *)0)
throw SleighError("Unknown register name: "+nm);
if (sym->getType() != SleighSymbol::varnode_symbol)
throw SleighError("Symbol is not a register: "+nm);
return sym->getFixedVarnode();
}
string SleighBase::getRegisterName(AddrSpace *base,uintb off,int4 size) const
{
VarnodeData sym;
sym.space = base;
sym.offset = off;
sym.size = size;
map<VarnodeData,string>::const_iterator iter = varnode_xref.upper_bound(sym); // First point greater than offset
if (iter == varnode_xref.begin()) return "";
iter--;
const VarnodeData &point((*iter).first);
if (point.space != base) return "";
uintb offbase = point.offset;
if (point.offset+point.size >= off+size)
return (*iter).second;
while(iter != varnode_xref.begin()) {
--iter;
const VarnodeData &point((*iter).first);
if ((point.space != base)||(point.offset != offbase)) return "";
if (point.offset+point.size >= off+size)
return (*iter).second;
}
return "";
}
void SleighBase::getAllRegisters(map<VarnodeData,string> ®list) const
{
reglist = varnode_xref;
}
void SleighBase::getUserOpNames(vector<string> &res) const
{
res = userop; // Return list of all language defined user ops (with index)
}
/// This does the bulk of the work of creating a .sla file
/// \param s is the output stream
void SleighBase::saveXml(ostream &s) const
{
s << "<sleigh";
a_v_i(s,"version",SLA_FORMAT_VERSION);
a_v_b(s,"bigendian",isBigEndian());
a_v_i(s,"align",alignment);
a_v_u(s,"uniqbase",getUniqueBase());
if (maxdelayslotbytes > 0)
a_v_u(s,"maxdelay",maxdelayslotbytes);
if (unique_allocatemask != 0)
a_v_u(s,"uniqmask",unique_allocatemask);
if (numSections != 0)
a_v_u(s,"numsections",numSections);
s << ">\n";
indexer.saveXml(s);
s << "<spaces";
a_v(s,"defaultspace",getDefaultCodeSpace()->getName());
s << ">\n";
for(int4 i=0;i<numSpaces();++i) {
AddrSpace *spc = getSpace(i);
if (spc == (AddrSpace *)0) continue;
if ((spc->getType()==IPTR_CONSTANT) ||
(spc->getType()==IPTR_FSPEC)||
(spc->getType()==IPTR_IOP)||
(spc->getType()==IPTR_JOIN))
continue;
spc->saveXml(s);
}
s << "</spaces>\n";
symtab.saveXml(s);
s << "</sleigh>\n";
}
/// This parses the main \<sleigh> tag (from a .sla file), which includes the description
/// of address spaces and the symbol table, with its associated decoding tables
/// \param el is the root XML element
void SleighBase::restoreXml(const Element *el)
{
maxdelayslotbytes = 0;
unique_allocatemask = 0;
numSections = 0;
int4 version = 0;
setBigEndian(xml_readbool(el->getAttributeValue("bigendian")));
{
istringstream s(el->getAttributeValue("align"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> alignment;
}
{
istringstream s(el->getAttributeValue("uniqbase"));
s.unsetf(ios::dec | ios::hex | ios::oct);
uintm ubase;
s >> ubase;
setUniqueBase(ubase);
}
int4 numattr = el->getNumAttributes();
for(int4 i=0;i<numattr;++i) {
const string &attrname( el->getAttributeName(i) );
if (attrname == "maxdelay") {
istringstream s1(el->getAttributeValue(i));
s1.unsetf(ios::dec | ios::hex | ios::oct);
s1 >> maxdelayslotbytes;
}
else if (attrname == "uniqmask") {
istringstream s2(el->getAttributeValue(i));
s2.unsetf(ios::dec | ios::hex | ios::oct);
s2 >> unique_allocatemask;
}
else if (attrname == "numsections") {
istringstream s3(el->getAttributeValue(i));
s3.unsetf(ios::dec | ios::hex | ios::oct);
s3 >> numSections;
}
else if (attrname == "version") {
istringstream s(el->getAttributeValue(i));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> version;
}
}
if (version != SLA_FORMAT_VERSION)
throw LowlevelError(".sla file has wrong format");
const List &list(el->getChildren());
List::const_iterator iter;
iter = list.begin();
while((*iter)->getName() == "floatformat") {
floatformats.emplace_back();
floatformats.back().restoreXml(*iter);
++iter;
}
indexer.restoreXml(*iter);
iter++;
XmlDecode decoder(this,*iter);
decodeSpaces(decoder,this);
iter++;
symtab.restoreXml(*iter,this);
root = (SubtableSymbol *)symtab.getGlobalScope()->findSymbol("instruction");
vector<string> errorPairs;
buildXrefs(errorPairs);
if (!errorPairs.empty())
throw SleighError("Duplicate register pairs");
}
} // End namespace ghidra