-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghidra_decompile_lib.py
More file actions
574 lines (473 loc) · 18.7 KB
/
ghidra_decompile_lib.py
File metadata and controls
574 lines (473 loc) · 18.7 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
LibSurgeon - Ghidra Headless Decompilation Script for Library Files
This script runs in Ghidra's Headless mode to automatically analyze
and decompile object files (.o) from static libraries (.a).
For ELF file processing, use ghidra_decompile_elf.py instead.
DWARF Debug Info Support:
- Automatically detects and uses DWARF debug information
- Preserves original variable names from debug info
- Supports both ELF (ARM/x86) and COFF/PE formats
"""
import os
import sys
# Add the script's directory to Python path for importing ghidra_common
script_dir = os.path.dirname(os.path.abspath(__file__))
if script_dir not in sys.path:
sys.path.insert(0, script_dir)
# Ghidra Python scripts use Jython with Ghidra's API
from ghidra.app.decompiler import DecompInterface
from ghidra.util.task import ConsoleTaskMonitor
from java.io import File
# Import shared utilities
from ghidra_common import (
demangle_cpp_name,
extract_class_name,
extract_function_signature,
extract_namespace,
generate_header_file,
generate_types_header,
get_decompiled_function_basic,
sanitize_filename,
should_skip_function,
write_file_header,
)
def check_debug_info(program):
"""
Check if the program has DWARF debug information.
Returns:
tuple: (has_debug_info, debug_format, details)
"""
has_debug = False
debug_format = "none"
details = []
try:
# Check for debug sections in memory blocks
memory = program.getMemory()
blocks = memory.getBlocks()
debug_sections = []
for block in blocks:
name = block.getName()
if name.startswith(".debug"):
debug_sections.append(name)
has_debug = True
if debug_sections:
debug_format = "DWARF"
details.append("Debug sections: {}".format(", ".join(debug_sections)))
# Check for source file information
listing = program.getListing()
func_iter = listing.getFunctions(True)
source_files = set()
for func in func_iter:
# Get source file from function's source location
try:
source_info = func.getComment()
if source_info and (".cpp" in source_info or ".c" in source_info):
source_files.add(source_info.split(":")[-1].strip())
except:
pass
if source_files:
details.append("Source files referenced: {}".format(len(source_files)))
# Check data type manager for imported types
dtm = program.getDataTypeManager()
if dtm:
type_count = dtm.getDataTypeCount(True)
if type_count > 10: # More than basic types
details.append("Data types imported: {}".format(type_count))
has_debug = True
except Exception as e:
details.append("Debug check error: {}".format(str(e)))
return (has_debug, debug_format, details)
def get_function_local_variables(func):
"""
Extract local variable information from a function.
Args:
func: Ghidra Function object
Returns:
list: List of (name, type, storage) tuples for local variables
"""
variables = []
try:
# Get all local variables (including parameters)
all_vars = func.getAllVariables()
for var in all_vars:
name = var.getName()
var_type = var.getDataType().getName() if var.getDataType() else "unknown"
storage = str(var.getVariableStorage())
# Skip auto-generated names like local_XX, param_X
if not (
name.startswith("local_")
or name.startswith("param_")
or name.startswith("in_")
or name.startswith("uVar")
):
variables.append((name, var_type, storage))
except:
pass
return variables
def get_function_parameters_with_names(func):
"""
Get function parameters with their original names from debug info.
Args:
func: Ghidra Function object
Returns:
list: List of (name, type) tuples for parameters
"""
params = []
try:
for param in func.getParameters():
name = param.getName()
param_type = (
param.getDataType().getName() if param.getDataType() else "unknown"
)
params.append((name, param_type))
except:
pass
return params
def apply_dwarf_variable_names(func, decomp_ifc, monitor):
"""
Try to apply DWARF variable names to the decompiled function.
This function checks if the function has debug info with variable names
and attempts to apply them to the high-level decompilation.
Args:
func: Ghidra Function object
decomp_ifc: DecompInterface
monitor: Task monitor
Returns:
bool: True if any variable names were applied
"""
try:
from ghidra.app.decompiler import DecompileResults
from ghidra.program.model.pcode import HighFunction
# Get the high function from decompilation
results = decomp_ifc.decompileFunction(func, 60, monitor)
if not results or not results.decompileCompleted():
return False
high_func = results.getHighFunction()
if not high_func:
return False
# Get local symbol map
local_symbols = high_func.getLocalSymbolMap()
if not local_symbols:
return False
# Check function's stored variables for debug names
stored_vars = func.getAllVariables()
debug_names = {}
for var in stored_vars:
name = var.getName()
# Check if this is a meaningful name (not auto-generated)
if not (
name.startswith("local_")
or name.startswith("param_")
or name.startswith("in_")
or name.startswith("uVar")
or name.startswith("iVar")
or name.startswith("pVar")
):
storage = var.getVariableStorage()
debug_names[str(storage)] = name
return len(debug_names) > 0
except Exception as e:
return False
def get_dwarf_variable_mapping(func):
"""
Get mapping of DWARF variable names for a function.
Returns a dict mapping auto-generated names to original DWARF names.
This can be used to add comments or perform substitution.
Args:
func: Ghidra Function object
Returns:
dict: Mapping of {auto_name: dwarf_name}
"""
mapping = {}
try:
# Get parameters
for i, param in enumerate(func.getParameters()):
name = param.getName()
auto_name = "param_{}".format(i + 1)
if name and not name.startswith("param_"):
mapping[auto_name] = name
# Get local variables
for var in func.getLocalVariables():
name = var.getName()
if name and not (
name.startswith("local_")
or name.startswith("uVar")
or name.startswith("iVar")
or name.startswith("pVar")
):
# Try to find the corresponding auto-generated name
# This is tricky because Ghidra may use different naming
pass
except Exception as e:
pass
return mapping
def add_dwarf_variable_comments(code, func):
"""
Add comments about original DWARF variable names to decompiled code.
Args:
code: Decompiled C code string
func: Ghidra Function object
Returns:
str: Code with added comments about original variable names
"""
if not code:
return code
try:
# Collect original variable names from function
original_params = []
for param in func.getParameters():
name = param.getName()
ptype = param.getDataType().getName() if param.getDataType() else "?"
if name and not name.startswith("param_"):
original_params.append("{} {}".format(ptype, name))
original_locals = []
for var in func.getLocalVariables():
name = var.getName()
vtype = var.getDataType().getName() if var.getDataType() else "?"
if name and not (
name.startswith("local_")
or name.startswith("uVar")
or name.startswith("iVar")
or name.startswith("pVar")
or name.startswith("in_")
):
original_locals.append("{} {}".format(vtype, name))
# If we have original names, add a comment
if original_params or original_locals:
comment_lines = []
if original_params:
comment_lines.append(
"/* Original params: {} */".format(", ".join(original_params))
)
if original_locals:
comment_lines.append(
"/* Original locals: {} */".format(", ".join(original_locals[:5]))
)
if len(original_locals) > 5:
comment_lines[-1] = comment_lines[-1].replace(
" */", " + {} more */".format(len(original_locals) - 5)
)
# Insert after function signature (before opening brace)
brace_pos = code.find("{")
if brace_pos > 0:
comment = "\n".join(comment_lines)
code = code[:brace_pos] + "\n" + comment + "\n" + code[brace_pos:]
except Exception as e:
pass
return code
def main():
print("=" * 60)
print("LibSurgeon - Ghidra Decompilation Script (Library Mode)")
print("=" * 60)
# Get output directory from script arguments
args = getScriptArgs()
if args and len(args) > 0:
output_dir = args[0]
else:
output_dir = "/tmp/libsurgeon_decompiled"
# Get include directory from second argument (optional)
if args and len(args) > 1:
include_dir = args[1]
else:
# Default: create include dir alongside output_dir
include_dir = os.path.join(os.path.dirname(output_dir), "include")
# Get current program name
program_name = currentProgram.getName()
print("\n[Info] Processing: {}".format(program_name))
print("[Info] Output directory: {}".format(output_dir))
print("[Info] Include directory: {}".format(include_dir))
# Check for debug information
has_debug, debug_format, debug_details = check_debug_info(currentProgram)
if has_debug:
print("[Info] Debug information detected: {}".format(debug_format))
for detail in debug_details:
print(" - {}".format(detail))
else:
print("[Info] No debug information found - using heuristic analysis")
# Create output directories
output_path = File(output_dir)
if not output_path.exists():
output_path.mkdirs()
include_path = File(include_dir)
if not include_path.exists():
include_path.mkdirs()
# Initialize decompiler
monitor = ConsoleTaskMonitor()
decomp_ifc = DecompInterface()
if not decomp_ifc.openProgram(currentProgram):
print("[Error] Failed to open program in decompiler")
return
# Configure decompiler options for better debug info utilization
try:
decomp_options = decomp_ifc.getOptions()
if decomp_options is not None:
decomp_options.setEliminateUnreachable(True)
# These options help preserve debug info in output
try:
# Try to enable options that preserve variable names
decomp_options.grabFromProgram(currentProgram)
except:
pass
except:
print("[Warn] Could not configure decompiler options")
# Get all functions
func_manager = currentProgram.getFunctionManager()
functions = func_manager.getFunctions(True)
# Organize functions by class/namespace
class_functions = {}
standalone_functions = []
namespaces_found = set()
func_count = 0
skipped_count = 0
for func in functions:
if monitor.isCancelled():
break
# Skip external symbols and special sections
if should_skip_function(func, currentProgram):
skipped_count += 1
continue
func_name = func.getName()
# Try to demangle
if func_name.startswith("_Z"):
demangled = demangle_cpp_name(func_name, currentProgram)
if demangled and demangled != func_name:
# Track namespace
ns = extract_namespace(demangled)
if ns:
namespaces_found.add(ns)
class_name = extract_class_name(demangled)
if class_name:
if class_name not in class_functions:
class_functions[class_name] = []
class_functions[class_name].append((func, demangled))
else:
standalone_functions.append((func, demangled))
else:
standalone_functions.append((func, func_name))
else:
standalone_functions.append((func, func_name))
func_count += 1
print("[Info] Found {} functions to decompile".format(func_count))
print("[Info] Skipped {} external/special functions".format(skipped_count))
print("[Info] Found {} classes".format(len(class_functions)))
if namespaces_found:
print("[Info] Namespaces: {}".format(", ".join(sorted(namespaces_found))))
# Count functions with preserved variable names (from debug info)
funcs_with_debug_vars = 0
total_preserved_vars = 0
# Generate output file
# Use .o filename as base name
base_name = program_name.replace(".o", "")
output_file = os.path.join(
output_dir, sanitize_filename(base_name) + "_decompiled.cpp"
)
decompiled_count = 0
failed_count = 0
with open(output_file, "w") as f:
# Write file header with debug info status
write_file_header(f, base_name, func_count, program_name)
# Add debug info note if present
if has_debug:
f.write("/* Debug Information: {} */\n".format(debug_format))
f.write("/* Variable names preserved from original source */\n\n")
# Detect and write namespace
primary_namespace = None
if namespaces_found:
# Use the most common namespace
primary_namespace = sorted(namespaces_found)[0]
f.write("namespace {} {{\n\n".format(primary_namespace))
# Collect function signatures for header generation
func_signatures = []
# Write decompiled code organized by class
for class_name, funcs in sorted(class_functions.items()):
f.write("// ============================================================\n")
f.write("// Class: {}\n".format(class_name))
f.write(
"// ============================================================\n\n"
)
for func, demangled_name in funcs:
# Check for preserved variable names
local_vars = get_function_local_variables(func)
params = get_function_parameters_with_names(func)
if local_vars or params:
funcs_with_debug_vars += 1
total_preserved_vars += len(local_vars) + len(params)
decompiled = get_decompiled_function_basic(decomp_ifc, func, monitor)
if decompiled:
f.write(decompiled)
f.write("\n")
decompiled_count += 1
# Extract function signature for header
signature = extract_function_signature(decompiled)
if signature:
func_signatures.append((demangled_name, signature))
else:
f.write(
"// [FAILED] Could not decompile: {}\n\n".format(demangled_name)
)
failed_count += 1
# Standalone functions
if standalone_functions:
f.write("// ============================================================\n")
f.write("// Standalone Functions\n")
f.write(
"// ============================================================\n\n"
)
for func, display_name in standalone_functions:
# Check for preserved variable names
local_vars = get_function_local_variables(func)
params = get_function_parameters_with_names(func)
if local_vars or params:
funcs_with_debug_vars += 1
total_preserved_vars += len(local_vars) + len(params)
decompiled = get_decompiled_function_basic(decomp_ifc, func, monitor)
if decompiled:
f.write(decompiled)
f.write("\n")
decompiled_count += 1
# Extract function signature for header
signature = extract_function_signature(decompiled)
if signature:
func_signatures.append((display_name, signature))
else:
f.write(
"// [FAILED] Could not decompile: {}\n\n".format(display_name)
)
failed_count += 1
# Close namespace if used
if primary_namespace:
f.write("}} // namespace {}\n".format(primary_namespace))
# Generate header file to include directory
header_file = None
if func_signatures:
header_file = generate_header_file(
include_dir, base_name, func_signatures, "library decompilation"
)
# Also generate types header
generate_types_header(include_dir)
print("[Info] Generated header file: {}".format(header_file))
# Close decompiler
decomp_ifc.dispose()
print("\n[Result] Decompilation complete!")
print(" - Successfully decompiled: {} functions".format(decompiled_count))
print(" - Failed: {} functions".format(failed_count))
print(" - Output file: {}".format(output_file))
if header_file:
print(" - Header file: {}".format(header_file))
# Report debug info utilization
if has_debug:
print("\n[Debug Info] DWARF information utilized:")
print(
" - Functions with preserved variable names: {}".format(
funcs_with_debug_vars
)
)
print(" - Total preserved variables: {}".format(total_preserved_vars))
# Run main function
if __name__ == "__main__":
main()
else:
# Running as script in Ghidra
main()