sleigh-sys 0.1.0

Rust bindings for Ghidra's Sleigh decompiler
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* ###
 * 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 "database_ghidra.hh"
#include "funcdata.hh"

Scope *ScopeGhidra::buildSubScope(uint8 id,const string &nm)

{
  return new ScopeGhidraNamespace(id,nm,ghidra);
}

/// \param g is the Architecture and connection to the Ghidra client
///
ScopeGhidra::ScopeGhidra(ArchitectureGhidra *g)
  : Scope(0,"",g,this)
{
  ghidra = g;
  cache = new ScopeInternal(0,"",g,this);
  cacheDirty = false;
}

ScopeGhidra::~ScopeGhidra(void)

{
  delete cache;
}

/// The Ghidra client reports a \e namespace id associated with
/// Symbol. Determine if a matching \e namespace Scope already exists in the cache and build
/// it if it isn't. This may mean creating a new \e namespace Scope.
/// \param id is the ID associated with the Ghidra namespace
/// \return the Scope matching the id.
Scope *ScopeGhidra::reresolveScope(uint8 id) const

{
  if (id == 0) return cache;
  Database *symboltab = ghidra->symboltab;
  Scope *cacheScope = symboltab->resolveScope(id);
  if (cacheScope != (Scope *)0)
    return cacheScope;		// Scope was previously cached

  Document *doc = ghidra->getNamespacePath(id);
  if (doc == (Document *)0)
    throw LowlevelError("Could not get namespace info");

  Scope *curscope = symboltab->getGlobalScope();	// Get pointer to ourselves (which is not const)
  try {
    const List &list(doc->getRoot()->getChildren());
    List::const_iterator iter = list.begin();
    ++iter;		// Skip element describing the root scope
    while(iter != list.end()) {
      const Element *el = *iter;
      ++iter;
      uint8 scopeId;
      istringstream s(el->getAttributeValue("id"));
      s.unsetf(ios::dec | ios::hex | ios::oct);
      s >> scopeId;
      curscope = symboltab->findCreateScope(scopeId, el->getContent(), curscope);
    }
    delete doc;
  }
  catch(LowlevelError &err) {
    delete doc;
    throw err;
  }
  return curscope;
}

/// The Ghidra client can respond to a query negatively by sending a
/// \<hole> tag, which describes the (largest) range of addresses containing
/// the query address that do not have any Symbol mapped to them. This object
/// stores this information in the \b holes map, which it consults to avoid
/// sending queries for the same unmapped address repeatedly. The tag may
/// also contain boolean property information about the memory range, which
/// also gets stored.
/// \param el is the \<hole> element
void ScopeGhidra::processHole(const Element *el) const

{
  Range range;
  range.restoreXml(el,ghidra);
  holes.insertRange(range.getSpace(),range.getFirst(),range.getLast());
  uint4 flags = 0;
  for(int4 i=0;i<el->getNumAttributes();++i) {
    if ((el->getAttributeName(i)=="readonly")&&
	xml_readbool(el->getAttributeValue(i)))
      flags |= Varnode::readonly;
    else if ((el->getAttributeName(i)=="volatile")&&
	     xml_readbool(el->getAttributeValue(i)))
      flags |= Varnode::volatil;
  }
  if (flags != 0) {
    ghidra->symboltab->setPropertyRange(flags,range);
    cacheDirty = true;
  }
}

/// Build the global object described by the XML document
/// and put it in the cache. The XML can either be a
/// \<hole> tag, describing the absence of symbols at the queried
/// address, or one of the symbol tags
/// \param doc is the XML document
/// \return the newly constructed Symbol or NULL if there was a hole
Symbol *ScopeGhidra::dump2Cache(Document *doc) const

{
  const Element *el = doc->getRoot();
  Symbol *sym = (Symbol *)0;

  if (el->getName() == "hole") {
    processHole(el);
    return sym;
  }

  List::const_iterator iter = el->getChildren().begin();
  uint8 scopeId;
  {
    istringstream s(el->getAttributeValue("id"));
    s.unsetf(ios::dec | ios::hex | ios::oct);
    s >> scopeId;
  }

  Scope *scope = reresolveScope(scopeId);
  el = *iter;
  try {
    sym = scope->addMapSym(el);
  }
  catch(RecovError &err) {
    // Duplicate name error (when trying to create the new function's scope)
    // Check for function object where the full size is not stored in the cache
    // Entries for functions always start at the entry address
    // of the function in order to deal with non-contiguous functions
    // But ghidra will still return the function for queries at addresses in the
    // interior of the function
    const Element *symel = *el->getChildren().begin();
    if (symel->getName() == "function") {	// Make sure new record is for a function
      const Element *baseAddrEl = *symel->getChildren().begin();
      Address baseaddr = Address::restoreXml( baseAddrEl, glb );	// Decode address from record
      vector<Symbol *> symList;
      scope->queryByName(symel->getAttributeValue("name"),symList);	// Lookup symbols with duplicate name
      for(int4 i=0;i<symList.size();++i) {
	FunctionSymbol *funcSym = dynamic_cast<FunctionSymbol *>(symList[i]);
	if (funcSym != (FunctionSymbol *)0) {				// If duplicate symbol is for function
	  if (funcSym->getFunction()->getAddress() == baseaddr) {	//   and the address matches
	    sym = funcSym;						// use the old symbol
	    break;
	  }
	}
      }
    }
    if (sym == (Symbol *)0) {
      ostringstream s;
      s << err.explain << ": entry didn't cache";
      throw LowlevelError(s.str());
    }
  }
  if (sym != (Symbol *)0) {
    SymbolEntry *entry = sym->getFirstWholeMap();
    if (entry != (SymbolEntry *)0) {
      if (scope != cache) {	// We have a namespace cache
	// With a global namespace, mark the address range as a "hole", so the same query won't
	// go up again.  With this limitation, a function can only refer to a single global symbol at a
	// a given address
 	AddrSpace *spc = entry->getAddr().getSpace();
 	uintb first = entry->getAddr().getOffset();
	uintb last = first+entry->getSize()-1;
	holes.insertRange(spc,first,last);
      }
// 	// Add a range to the namespace, so that -map_scope-
// 	// can pick up references to this symbol
// 	ghidra->symboltab->addRange(scope,spc,first,last);

// The decompiler does not currently maintain properties itself
// So we need to cache any properties returned by the query
      uint4 props = sym->getFlags() & (Varnode::readonly | Varnode::volatil);
      if (props != 0) {
	Range rng(entry->getAddr().getSpace(),entry->getFirst(),entry->getLast());
	ghidra->symboltab->setPropertyRange(props,rng);
	cacheDirty = true;
      }
//       uint4 flags = ghidra->symboltab->getProperty(entry->getAddr());
//       if (flags != 0)
// 	scope->setAttribute(sym,flags);
    }
  }
  return sym;
}

/// Determine if the given address should be sent to the Ghidra client
/// at all, by checking the hole map and other factors.
/// If it passes, send the query to the client, process the result,
/// and update the cache. If a Symbol is ultimately recovered, return it.
/// \param addr is the address to potentially query
/// \return the matching Symbol or NULL if there is hole
Symbol *ScopeGhidra::removeQuery(const Address &addr) const

{
  Document *doc;
  Symbol *sym = (Symbol *)0;

  // Don't send up queries on constants or uniques
  //  if (addr.getSpace()->getType() != IPTR_PROCESSOR) return sym;
  //  if (!cache->inScope(addr,1,Address())) return (Symbol *)0;

  // There is an efficiency/functionality trade-off problem here.
  // We don't want to send every address query up to Ghidra
  // as this is inefficient. Previously we only sent up if the
  // the address was in the discovery scope, but there is the
  // possibility that there is a global symbol that is not in
  // the discovery scope.  Now we send up if there is ANY
  // part of the space the address is in that is part of the
  // discovery scope.
  if (addr.getSpace()->getIndex() >= spacerange.size())
    return (Symbol *)0;
  if (spacerange[addr.getSpace()->getIndex()] == 0)
    return (Symbol *)0;

  // Have we queried this address before
  if (holes.inRange(addr,1)) return (Symbol *)0;
  doc = ghidra->getMappedSymbolsXML(addr); // Query GHIDRA about this address
  if (doc != (Document *)0) {
    sym = dump2Cache(doc);	// Add it to the cache
    delete doc;
  }
  return sym;
}

void ScopeGhidra::addRange(AddrSpace *spc,uintb first,uintb last)

{
  Scope::addRange(spc,first,last);
  int4 ind = spc->getIndex();	// Mark all spaces that have a range
  while(spacerange.size() <= ind)
    spacerange.push_back(0);
  spacerange[ind] = 1;
}

void ScopeGhidra::clear(void)

{
  cache->clear();
  holes.clear();
  if (cacheDirty) {
    ghidra->symboltab->setProperties(flagbaseDefault); // Restore database properties to defaults
    cacheDirty = false;
  }
}

SymbolEntry *ScopeGhidra::findAddr(const Address &addr,
					    const Address &usepoint) const
{
  SymbolEntry *entry;
  entry = cache->findAddr(addr,usepoint);
  if (entry == (SymbolEntry *)0) { // Didn't find symbol
    entry = cache->findContainer(addr,1,Address());
    if (entry != (SymbolEntry *)0)
      return (SymbolEntry *)0;	// Address is already queried, but symbol doesn't start at our address
    Symbol *sym = removeQuery(addr); // Query server
    if (sym != (Symbol *)0)
      entry = sym->getMapEntry(addr);
    // entry may be null for certain queries, ghidra may return symbol of size <8 with
    // address equal to START of function, even though the query was for an address INTERNAL to the function
  }
  if ((entry != (SymbolEntry *)0)&&(entry->getAddr()==addr))
    return entry;
  return (SymbolEntry *)0;
}

SymbolEntry *ScopeGhidra::findContainer(const Address &addr,int4 size,
						 const Address &usepoint) const
{
  SymbolEntry *entry;
  entry = cache->findClosestFit(addr,size,usepoint);
  if (entry == (SymbolEntry *)0) {
    Symbol *sym = removeQuery(addr);
    if (sym != (Symbol *)0)
      entry = sym->getMapEntry(addr);
    // entry may be null for certain queries, ghidra may return symbol of size <8 with
    // address equal to START of function, even though the query was for an address INTERNAL to the function
  }
  if (entry != (SymbolEntry *)0) {
    // Entry contains addr, does it contain addr+size
    uintb last = entry->getAddr().getOffset() + entry->getSize() -1;
    if (last >= addr.getOffset() + size-1)
      return entry;
  }
  return (SymbolEntry *)0;
}

ExternRefSymbol *ScopeGhidra::findExternalRef(const Address &addr) const

{
  ExternRefSymbol *sym;
  sym = cache->findExternalRef(addr);
  if (sym == (ExternRefSymbol *)0) {
    // Check if this address has already been queried,
    // (returning a symbol other than an external ref symbol)
    SymbolEntry *entry;
    entry = cache->findContainer(addr,1,Address());
    if (entry == (SymbolEntry *)0)
      sym = dynamic_cast<ExternRefSymbol *>(removeQuery(addr));
  }
  return sym;
}

Funcdata *ScopeGhidra::findFunction(const Address &addr) const

{
  Funcdata *resFd = cache->findFunction(addr);
  if (resFd == (Funcdata *)0) {
    // Check if this address has already been queried,
    // (returning a symbol other than a function_symbol)
    SymbolEntry *entry = cache->findContainer(addr,1,Address());
    if (entry == (SymbolEntry *)0) {
      FunctionSymbol *sym;
      sym = dynamic_cast<FunctionSymbol *>(removeQuery(addr));
      if (sym != (FunctionSymbol *)0)
	resFd = sym->getFunction();
    }
  }
  return resFd;
}

LabSymbol *ScopeGhidra::findCodeLabel(const Address &addr) const

{
  LabSymbol *sym;
  sym = cache->findCodeLabel(addr);
  if (sym == (LabSymbol *)0) {
    // Check if this address has already been queried,
    // (returning a symbol other than a code label)
    SymbolEntry *entry;
    entry = cache->findAddr(addr,Address());
    if (entry == (SymbolEntry *)0) {
      string symname = ghidra->getCodeLabel(addr);	// Do the remote query
      if (!symname.empty())
	sym = cache->addCodeLabel(addr,symname);
    }
  }
  return sym;
}

Funcdata *ScopeGhidra::resolveExternalRefFunction(ExternRefSymbol *sym) const

{
  Funcdata *resFd = (Funcdata *)0;
  const Scope *basescope = ghidra->symboltab->mapScope(this,sym->getRefAddr(),Address());
  // Truncate search at this scope, we don't want
  // the usual remote_query if the function isn't in cache
  // this won't recover external functions, but will just
  // return the externalref symbol again
  stackFunction(basescope,this,sym->getRefAddr(),&resFd);
  if (resFd == (Funcdata *)0)
    resFd = cache->findFunction(sym->getRefAddr());
  if (resFd == (Funcdata *)0) {
    // If the function isn't in cache, we use the special
    // getExternalRefXML interface to recover the external function
    Document *doc;
    SymbolEntry *entry = sym->getFirstWholeMap();
    doc = ghidra->getExternalRefXML(entry->getAddr());
    if (doc != (Document *)0) {
      FunctionSymbol *funcSym;
      // Make sure referenced function is cached
      funcSym = dynamic_cast<FunctionSymbol *>(dump2Cache(doc));
      delete doc;
      if (funcSym != (FunctionSymbol *)0)
	resFd = funcSym->getFunction();
    }
  }
  return resFd;
}

SymbolEntry *ScopeGhidra::addSymbol(const string &nm,Datatype *ct,
				    const Address &addr,const Address &usepoint)
{
  // We do not inform Ghidra of the new symbol, we just
  // stick it in the cache.  This allows the mapglobals action
  // to build global variables that Ghidra knows nothing about
  return cache->addSymbol(nm,ct,addr,usepoint);
}

SymbolEntry *ScopeGhidraNamespace::addMapInternal(Symbol *sym,uint4 exfl,const Address &addr,int4 off,int4 sz,
						  const RangeList &uselim)
{
  SymbolEntry *res;
  res = ScopeInternal::addMapInternal(sym,exfl,addr,off,sz,uselim);
  glb->symboltab->addRange(this,res->getAddr().getSpace(),res->getFirst(),res->getLast());
  return res;
}

bool ScopeGhidraNamespace::isNameUsed(const string &nm,const Scope *op2) const

{
  if (ArchitectureGhidra::isDynamicSymbolName(nm))
    return false;		// Just assume default FUN_ and DAT_ names don't collide
  const ScopeGhidraNamespace *otherScope = dynamic_cast<const ScopeGhidraNamespace *>(op2);
  uint8 otherId = (otherScope != (const ScopeGhidraNamespace *)0) ? otherScope->getId() : 0;
  return ghidra->isNameUsed(nm, uniqueId, otherId);
}