-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghidra_decompile_elf.py
More file actions
1102 lines (906 loc) · 38.3 KB
/
ghidra_decompile_elf.py
File metadata and controls
1102 lines (906 loc) · 38.3 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
LibSurgeon - Ghidra Headless Decompilation Script for ELF Files
This script runs in Ghidra's Headless mode to automatically analyze
and decompile ELF files with intelligent module grouping.
Features:
- Module grouping by function name prefix
- C++ class/struct analysis
- Virtual function table (vtable) parsing
- Improved struct field access annotation
Module Grouping Strategies:
- prefix: Group by function name prefix (e.g., xxBmp*, xxFnt*)
- alpha: Group by first letter (A-Z)
- camelcase: Extract CamelCase words as module names
- single: All functions in one file
For library (.a/.o) file processing, use ghidra_decompile_lib.py instead.
"""
import os
import re
import sys
from collections import OrderedDict, defaultdict
# 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.program.model.data import (
ArrayDataType,
EnumDataType,
FunctionDefinitionDataType,
PointerDataType,
StructureDataType,
TypedefDataType,
)
from ghidra.program.model.symbol import SourceType
from ghidra.util.task import ConsoleTaskMonitor
from java.io import File
# Import shared utilities from ghidra_common
from ghidra_common import (
GHIDRA_TYPE_MAP,
UNKNOWN_TYPE_DEFS,
demangle_cpp_name,
enhance_decompiled_code,
extract_class_from_method,
extract_function_signature,
extract_namespace,
generate_header_file,
generate_master_header,
generate_types_header,
normalize_code_types,
normalize_ghidra_type,
sanitize_filename,
)
# ============================================================
# C++ Class and Virtual Function Analysis
# ============================================================
class CppClassInfo:
"""Information about a C++ class extracted from analysis"""
def __init__(self, name):
self.name = name
self.methods = [] # [(func_name, display_name, is_virtual, vtable_index)]
self.vtable_addr = None
self.vtable_funcs = [] # [(index, func_addr, func_name)]
self.struct_type = None # Associated Ghidra struct type
self.parent_class = None
self.size = 0
def is_virtual_method(func, program):
"""
Check if a function is likely a virtual method.
Heuristics:
1. Referenced in a vtable (data section with function pointers)
2. Has __thiscall convention with 'this' as first param
3. Name matches virtual method patterns
"""
func_name = func.getName()
# Check calling convention
calling_conv = func.getCallingConventionName()
if calling_conv and "thiscall" in calling_conv.lower():
return True
# Check if function is referenced from data section (potential vtable)
refs = program.getReferenceManager().getReferencesTo(func.getEntryPoint())
for ref in refs:
if ref.getReferenceType().isData():
from_addr = ref.getFromAddress()
# Check if reference is from a potential vtable location
mem = program.getMemory()
block = mem.getBlock(from_addr)
if block and (block.getName() in [".rodata", ".data", ".data.rel.ro"]):
return True
return False
def analyze_vtables(program, monitor):
"""
Analyze virtual function tables in the program.
Returns dict: vtable_addr -> [(index, func_addr, func_name)]
"""
vtables = {}
symbol_table = program.getSymbolTable()
mem = program.getMemory()
listing = program.getListing()
# Look for symbols that match vtable patterns
vtable_patterns = [
r"^_ZTV", # Itanium ABI: _ZTV<class>
r"^vtable\s+for\s+", # Demangled vtable
r"^__vt_", # Some compilers
r"_vtbl$", # ARM/RVCT pattern
]
for symbol in symbol_table.getAllSymbols(True):
if monitor.isCancelled():
break
sym_name = symbol.getName()
sym_addr = symbol.getAddress()
# Check if this looks like a vtable
is_vtable = False
class_name = None
for pattern in vtable_patterns:
if re.match(pattern, sym_name, re.IGNORECASE):
is_vtable = True
# Try to extract class name
if sym_name.startswith("_ZTV"):
# Demangle
demangled = demangle_cpp_name(sym_name, program)
if "vtable for " in demangled:
class_name = demangled.replace("vtable for ", "").strip()
else:
class_name = sym_name[4:] # Remove _ZTV prefix
break
if not is_vtable:
continue
# Parse vtable entries (array of function pointers)
vtable_entries = []
ptr_size = program.getDefaultPointerSize()
current_addr = sym_addr
index = 0
# Skip RTTI pointer and offset-to-top (first 2 entries for Itanium ABI)
# This is platform-specific, so we try to detect valid function pointers
max_entries = 100 # Safety limit
while index < max_entries:
try:
# Read pointer value
if ptr_size == 4:
ptr_value = mem.getInt(current_addr)
else:
ptr_value = mem.getLong(current_addr)
# Check if this points to a function
ptr_addr = (
program.getAddressFactory()
.getDefaultAddressSpace()
.getAddress(ptr_value)
)
func_at = listing.getFunctionAt(ptr_addr)
if func_at:
vtable_entries.append((index, ptr_addr, func_at.getName()))
index += 1
current_addr = current_addr.add(ptr_size)
elif index > 2: # Allow first 2 entries to be non-functions (RTTI)
break
else:
index += 1
current_addr = current_addr.add(ptr_size)
except:
break
if vtable_entries:
vtables[sym_addr] = {"class_name": class_name, "entries": vtable_entries}
return vtables
def analyze_cpp_classes(program, module_functions, vtables, monitor):
"""
Analyze C++ classes from function signatures and vtables.
Returns dict: class_name -> CppClassInfo
"""
classes = {}
# Collect methods from function signatures
for module_name, funcs in module_functions.items():
for func, display_name, mangled_name in funcs:
if monitor.isCancelled():
break
class_name = extract_class_from_method(display_name)
if not class_name:
continue
if class_name not in classes:
classes[class_name] = CppClassInfo(class_name)
# Check if virtual
is_virtual = is_virtual_method(func, program)
vtable_index = -1
# Find in vtables
for vt_addr, vt_info in vtables.items():
if vt_info["class_name"] == class_name:
for idx, func_addr, fname in vt_info["entries"]:
if func.getEntryPoint().equals(func_addr):
is_virtual = True
vtable_index = idx
break
method_name = display_name.split("::")[-1].split("(")[0].strip()
classes[class_name].methods.append(
(func.getName(), method_name, is_virtual, vtable_index)
)
# Associate vtables with classes
for vt_addr, vt_info in vtables.items():
class_name = vt_info["class_name"]
if class_name and class_name in classes:
classes[class_name].vtable_addr = vt_addr
classes[class_name].vtable_funcs = vt_info["entries"]
return classes
def generate_class_header(output_dir, classes, program_name):
"""Generate a header file documenting discovered C++ classes"""
if not classes:
return None
header_file = os.path.join(output_dir, "_classes.h")
with open(header_file, "w") as f:
f.write("/**\n")
f.write(" * C++ Class Analysis\n")
f.write(" * Source: {}\n".format(program_name))
f.write(" * Classes found: {}\n".format(len(classes)))
f.write(" *\n")
f.write(" * Auto-generated by LibSurgeon\n")
f.write(" * NOTE: This is analysis output, not compilable code\n")
f.write(" */\n\n")
f.write("#ifndef _CLASSES_H_\n")
f.write("#define _CLASSES_H_\n\n")
f.write('#include "_types.h"\n\n')
# Write class declarations
for class_name in sorted(classes.keys()):
cls = classes[class_name]
f.write("/* " + "=" * 56 + " */\n")
f.write("/* Class: {} */\n".format(class_name))
f.write("/* " + "=" * 56 + " */\n\n")
# Virtual table info
if cls.vtable_addr:
f.write(
"/* VTable at: 0x{:08x} */\n".format(cls.vtable_addr.getOffset())
)
f.write("/* Virtual methods: {} */\n".format(len(cls.vtable_funcs)))
if cls.vtable_funcs:
f.write("/*\n")
f.write(" * Virtual Function Table:\n")
for idx, func_addr, func_name in cls.vtable_funcs:
f.write(
" * [{}] {} @ 0x{:08x}\n".format(
idx, func_name, func_addr.getOffset()
)
)
f.write(" */\n")
f.write("\n")
# Class declaration (forward)
safe_name = class_name.replace("::", "_")
f.write("typedef struct {} {};\n".format(safe_name, safe_name))
f.write("struct {} {{\n".format(safe_name))
# Add vtable pointer if class has virtual methods
if cls.vtable_funcs:
f.write(" void **_vptr; /* Virtual function table pointer */\n")
f.write(" /* TODO: Add member fields based on analysis */\n")
f.write("}};\n\n")
# Method declarations
f.write("/* Methods ({}) */\n".format(len(cls.methods)))
for mangled, method_name, is_virtual, vt_idx in sorted(
cls.methods, key=lambda x: x[1]
):
virtual_mark = (
"[virtual:{}] ".format(vt_idx) if is_virtual and vt_idx >= 0 else ""
)
virtual_kw = "virtual " if is_virtual else ""
f.write("/* {}{}{} */\n".format(virtual_mark, virtual_kw, method_name))
f.write("\n")
f.write("#endif /* _CLASSES_H_ */\n")
return header_file
def get_decompiled_function_elf(
decomp_ifc, func, monitor, class_info=None, struct_info=None
):
"""
Decompile a single function and return C code with normalized types.
ELF-specific version with class/struct enhancement.
"""
try:
results = decomp_ifc.decompileFunction(func, 60, monitor)
if results and results.decompileCompleted():
code = results.getDecompiledFunction().getC()
# Normalize Ghidra-specific types to standard C types
code = normalize_code_types(code)
# Enhance with class/struct analysis
code = enhance_decompiled_code(code, class_info or {}, struct_info or {})
return code
except Exception as e:
print(" [Error] Failed to decompile {}: {}".format(func.getName(), str(e)))
return None
# ============================================================
# Module Grouping Strategies
# ============================================================
def extract_prefix(func_name, min_prefix_len=2, max_prefix_len=30):
"""
Extract meaningful prefix from function name for grouping.
Examples:
xxBmpInit -> xxBmp
xxFntGetMetrics -> xxFnt
GfxCreateSurface -> Gfx
aa_bb_init -> aa_bb
ApplicationApplication_goHome -> ApplicationApplication
CoreView__ReInit -> CoreView
"""
# Skip auto-generated names
if func_name.startswith("FUN_") or func_name.startswith("DAT_"):
return "_generated"
# Handle C-style underscore names with double underscore as separator
# e.g., CoreView__ReInit -> CoreView
if "__" in func_name:
parts = func_name.split("__")
if len(parts) >= 1 and len(parts[0]) >= min_prefix_len:
return parts[0]
# Handle single underscore as method separator
# e.g., ApplicationApplication_goHome -> ApplicationApplication
if "_" in func_name and not func_name.startswith("_"):
parts = func_name.split("_")
if len(parts) >= 2:
# Check if first part looks like a class/module name (CamelCase or all caps)
first_part = parts[0]
if len(first_part) >= min_prefix_len:
# If it's CamelCase or reasonable length, use it
if re.match(r"^[A-Z][a-zA-Z0-9]+$", first_part) or len(first_part) >= 4:
return first_part
# Try first two parts for compound names
if len(parts) >= 2:
compound = parts[0] + parts[1]
if len(compound) <= max_prefix_len:
return compound
# Handle pure CamelCase names
# Find the first "word boundary" after initial capitals
# xxBmpInit -> xxBmp, CoreView -> Core
match = re.match(r"^([A-Z][a-z]+[A-Z][a-z]*)", func_name)
if match:
prefix = match.group(1)
if min_prefix_len <= len(prefix) <= max_prefix_len:
return prefix
# Simpler pattern: First CamelCase word
match = re.match(r"^([A-Z][a-z]+)", func_name)
if match and len(match.group(1)) >= min_prefix_len:
return match.group(1)
# Lowercase prefix (c-style: xx_init)
match = re.match(r"^([a-z][a-z0-9]*_[a-z0-9]+)", func_name)
if match:
return match.group(1)
# Just first lowercase word
match = re.match(r"^([a-z]+)", func_name)
if match and len(match.group(1)) >= min_prefix_len:
return match.group(1)
# All caps prefix (HAL_Init -> HAL)
match = re.match(r"^([A-Z]+)_", func_name)
if match and len(match.group(1)) >= min_prefix_len:
return match.group(1)
return "_misc"
def get_module_name_by_alpha(func_name, display_name):
"""Get module name using alphabetic strategy (A-Z)"""
name_to_check = display_name if display_name else func_name
# Skip auto-generated names
if name_to_check.startswith("FUN_") or name_to_check.startswith("DAT_"):
return "_generated"
first_char = name_to_check[0].upper() if name_to_check else "_"
if first_char.isalpha():
return first_char
return "_symbols"
def get_module_name_by_camelcase(func_name, display_name):
"""Get module name using CamelCase word extraction"""
name_to_check = display_name if display_name else func_name
# Skip auto-generated names
if name_to_check.startswith("FUN_") or name_to_check.startswith("DAT_"):
return "_generated"
# Extract CamelCase words
if "_" in name_to_check:
words = name_to_check.split("_")
else:
words = re.findall(r"[A-Z][a-z]*|[a-z]+|[0-9]+", name_to_check)
if len(words) >= 2:
return words[0] + words[1]
elif len(words) == 1:
return words[0]
return "_misc"
def get_module_name(func_name, display_name, strategy="prefix"):
"""Get module name based on specified strategy"""
name_to_check = display_name if display_name else func_name
if strategy == "prefix":
return extract_prefix(name_to_check)
elif strategy == "alpha":
return get_module_name_by_alpha(func_name, display_name)
elif strategy == "camelcase":
return get_module_name_by_camelcase(func_name, display_name)
elif strategy == "single":
return "all_functions"
else:
return extract_prefix(name_to_check)
def write_file_header(f, module_name, func_count):
"""Write file header for a module"""
f.write("/**\n")
f.write(" * Module: {}\n".format(module_name))
f.write(" * Functions: {}\n".format(func_count))
f.write(" * \n")
f.write(" * Generated by LibSurgeon (Ghidra-based decompiler)\n")
f.write(" * \n")
f.write(
" * WARNING: This is automatically generated code from reverse engineering.\n"
)
f.write(
" * It may not compile directly and is intended for educational purposes only.\n"
)
f.write(" */\n\n")
f.write("#include <stdint.h>\n")
f.write("#include <stdbool.h>\n")
f.write("#include <stddef.h>\n")
f.write('#include "../include/_types.h"\n\n')
def format_data_type(dt, indent=0):
"""Format a Ghidra DataType to C code string"""
if dt is None:
return None
type_name = dt.getName()
# Apply type normalization
if type_name in GHIDRA_TYPE_MAP:
return GHIDRA_TYPE_MAP[type_name]
return type_name
def extract_struct_definition(dt, indent=0):
"""Extract C struct/enum definition from a Ghidra DataType"""
indent_str = " " * indent
type_name = dt.getName()
# Handle Structures
if isinstance(dt, StructureDataType):
lines = []
lines.append("{}typedef struct {} {{".format(indent_str, type_name))
for component in dt.getComponents():
comp_name = component.getFieldName()
comp_type = component.getDataType()
comp_offset = component.getOffset()
comp_size = component.getLength()
if comp_name is None:
comp_name = "field_0x{:x}".format(comp_offset)
# Get type string with normalization
type_str = format_data_type(comp_type)
if type_str is None:
type_str = comp_type.getDisplayName()
# Normalize the type
type_str = normalize_ghidra_type(type_str)
# Handle arrays
if isinstance(comp_type, ArrayDataType):
elem_type = comp_type.getDataType()
elem_type_str = format_data_type(elem_type)
if elem_type_str is None:
elem_type_str = elem_type.getDisplayName()
elem_type_str = normalize_ghidra_type(elem_type_str)
array_len = comp_type.getNumElements()
lines.append(
"{} {} {}[{}]; /* offset: 0x{:x}, size: {} */".format(
indent_str,
elem_type_str,
comp_name,
array_len,
comp_offset,
comp_size,
)
)
else:
lines.append(
"{} {} {}; /* offset: 0x{:x}, size: {} */".format(
indent_str, type_str, comp_name, comp_offset, comp_size
)
)
lines.append(
"{}}} {}; /* size: {} */".format(indent_str, type_name, dt.getLength())
)
return "\n".join(lines)
# Handle Enums
elif isinstance(dt, EnumDataType):
lines = []
lines.append("{}typedef enum {} {{".format(indent_str, type_name))
values = list(dt.getValues())
for i, value in enumerate(sorted(values)):
name = dt.getName(value)
suffix = "," if i < len(values) - 1 else ""
lines.append("{} {} = {}{}".format(indent_str, name, value, suffix))
lines.append("{}}} {};".format(indent_str, type_name))
return "\n".join(lines)
# Handle Typedefs
elif isinstance(dt, TypedefDataType):
base_type = dt.getBaseDataType()
base_name = format_data_type(base_type)
if base_name is None:
base_name = base_type.getDisplayName()
base_name = normalize_ghidra_type(base_name)
return "{}typedef {} {};".format(indent_str, base_name, type_name)
return None
def collect_data_types(program):
"""Collect all user-defined data types from the program"""
dtm = program.getDataTypeManager()
structs = []
enums = []
typedefs = []
# Iterate through all data types
for dt in dtm.getAllDataTypes():
category = dt.getCategoryPath().getPath()
# Skip built-in types and library types
if category.startswith("/"):
cat_parts = category.split("/")
if len(cat_parts) > 1 and cat_parts[1] in ["BuiltInTypes", "windows"]:
continue
name = dt.getName()
# Skip anonymous types and Ghidra internal types
if name.startswith("_") and name[1:].isdigit():
continue
if name.startswith("undefined"):
continue
if isinstance(dt, StructureDataType):
structs.append(dt)
elif isinstance(dt, EnumDataType):
enums.append(dt)
elif isinstance(dt, TypedefDataType):
typedefs.append(dt)
return structs, enums, typedefs
def generate_types_header(output_dir, program_name, structs, enums, typedefs):
"""Generate a header file containing all extracted types"""
header_file = os.path.join(output_dir, "_types.h")
with open(header_file, "w") as f:
f.write("/**\n")
f.write(" * Data Types Header\n")
f.write(" * Source: {}\n".format(program_name))
f.write(" * Structures: {}\n".format(len(structs)))
f.write(" * Enums: {}\n".format(len(enums)))
f.write(" * Typedefs: {}\n".format(len(typedefs)))
f.write(" * \n")
f.write(" * Auto-generated by LibSurgeon from ELF decompilation\n")
f.write(" */\n\n")
f.write("#ifndef _TYPES_H_\n")
f.write("#define _TYPES_H_\n\n")
f.write("#include <stdint.h>\n")
f.write("#include <stdbool.h>\n")
f.write("#include <stddef.h>\n\n")
# Write unknown type definitions first
f.write(UNKNOWN_TYPE_DEFS)
f.write("\n")
# Write forward declarations for structures
if structs:
f.write("/* Forward Declarations */\n")
for dt in sorted(structs, key=lambda x: x.getName()):
f.write("struct {};\n".format(dt.getName()))
f.write("\n")
# Write enums
if enums:
f.write("/* ============================================ */\n")
f.write("/* ENUMS */\n")
f.write("/* ============================================ */\n\n")
for dt in sorted(enums, key=lambda x: x.getName()):
definition = extract_struct_definition(dt)
if definition:
f.write(definition)
f.write("\n\n")
# Write typedefs
if typedefs:
f.write("/* ============================================ */\n")
f.write("/* TYPEDEFS */\n")
f.write("/* ============================================ */\n\n")
for dt in sorted(typedefs, key=lambda x: x.getName()):
definition = extract_struct_definition(dt)
if definition:
f.write(definition)
f.write("\n")
f.write("\n")
# Write structures
if structs:
f.write("/* ============================================ */\n")
f.write("/* STRUCTURES */\n")
f.write("/* ============================================ */\n\n")
for dt in sorted(structs, key=lambda x: x.getName()):
definition = extract_struct_definition(dt)
if definition:
f.write(definition)
f.write("\n\n")
f.write("#endif /* _TYPES_H_ */\n")
return header_file
def main():
print("=" * 60)
print("LibSurgeon - ELF Decompilation Script (Module Grouping)")
print("=" * 60)
# Get output directory and strategy from script arguments
args = getScriptArgs()
output_dir = "/tmp/libsurgeon_decompiled"
strategy = "prefix" # Default strategy
if args:
if len(args) > 0:
output_dir = args[0]
if len(args) > 1:
strategy = args[1]
# Get current program name
program_name = currentProgram.getName()
print("\n[Info] Processing: {}".format(program_name))
print("[Info] Output directory: {}".format(output_dir))
print("[Info] Grouping strategy: {}".format(strategy))
# Create output directories (src for .cpp, include for .h)
output_path = File(output_dir)
if not output_path.exists():
output_path.mkdirs()
src_dir = os.path.join(output_dir, "src")
include_dir = os.path.join(output_dir, "include")
src_path = File(src_dir)
if not src_path.exists():
src_path.mkdirs()
include_path = File(include_dir)
if not include_path.exists():
include_path.mkdirs()
print("[Info] Source directory: {}".format(src_dir))
print("[Info] Include directory: {}".format(include_dir))
# 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
try:
decomp_options = decomp_ifc.getOptions()
if decomp_options is not None:
decomp_options.setEliminateUnreachable(True)
except:
print("[Warn] Could not configure decompiler options")
# Extract data types (structures, enums, typedefs)
print("\n[Info] Extracting data types...")
structs, enums, typedefs = collect_data_types(currentProgram)
print(
"[Info] Found {} structures, {} enums, {} typedefs".format(
len(structs), len(enums), len(typedefs)
)
)
if structs or enums or typedefs:
types_header = generate_types_header(
include_dir, program_name, structs, enums, typedefs
)
print("[Info] Generated types header: include/_types.h")
# Get all functions
func_manager = currentProgram.getFunctionManager()
functions = func_manager.getFunctions(True)
# Collect all functions and group by module
print("\n[Info] Analyzing functions...")
module_functions = defaultdict(
list
) # module_name -> [(func, display_name, mangled_name)]
namespaces_found = set()
func_count = 0
thunk_count = 0
external_count = 0
for func in functions:
if monitor.isCancelled():
break
func_name = func.getName()
# Skip thunks and external functions
if func.isThunk():
thunk_count += 1
continue
if func.isExternal():
external_count += 1
continue
# Try to demangle C++ names
display_name = func_name
if func_name.startswith("_Z"):
demangled = demangle_cpp_name(func_name, currentProgram)
if demangled and demangled != func_name:
display_name = demangled
# Track namespace
ns = extract_namespace(demangled)
if ns:
namespaces_found.add(ns)
# Determine module
module_name = get_module_name(func_name, display_name, strategy)
module_functions[module_name].append((func, display_name, func_name))
func_count += 1
print("[Info] Found {} functions to decompile".format(func_count))
print("[Info] Skipped {} thunks, {} externals".format(thunk_count, external_count))
print("[Info] Grouped into {} modules".format(len(module_functions)))
if namespaces_found:
print("[Info] C++ Namespaces: {}".format(", ".join(sorted(namespaces_found))))
# Analyze C++ virtual tables
print("\n[Info] Analyzing virtual function tables...")
vtables = analyze_vtables(currentProgram, monitor)
print("[Info] Found {} vtables".format(len(vtables)))
# Analyze C++ classes
print("[Info] Analyzing C++ classes...")
cpp_classes = analyze_cpp_classes(
currentProgram, module_functions, vtables, monitor
)
print("[Info] Found {} C++ classes".format(len(cpp_classes)))
# Count virtual methods
virtual_method_count = sum(
1
for cls in cpp_classes.values()
for _, _, is_virtual, _ in cls.methods
if is_virtual
)
if virtual_method_count > 0:
print("[Info] Identified {} virtual methods".format(virtual_method_count))
# Generate class header
if cpp_classes:
class_header = generate_class_header(include_dir, cpp_classes, program_name)
if class_header:
print("[Info] Generated class header: include/_classes.h")
# Build struct info map for code enhancement
struct_info = {}
for s in structs:
struct_info[s.getName()] = s
# Print module summary (top 20)
print("\n[Info] Module breakdown (top 20):")
sorted_modules = sorted(module_functions.items(), key=lambda x: -len(x[1]))
for module_name, funcs in sorted_modules[:20]:
print(" - {}: {} functions".format(module_name, len(funcs)))
if len(sorted_modules) > 20:
print(" ... and {} more modules".format(len(sorted_modules) - 20))
# Decompile and write each module
print("\n[Info] Decompiling modules...")
# Output progress header for shell script to parse
print("[PROGRESS_TOTAL] {}".format(func_count))
total_decompiled = 0
total_failed = 0
current_func = 0
module_index = 0
total_modules = len(module_functions)
# Store function signatures for header file generation
module_signatures = defaultdict(list) # module_name -> [(func_name, signature)]
for module_name in sorted(module_functions.keys()):
funcs = module_functions[module_name]
module_index += 1
# Create output filename in src directory
safe_module_name = sanitize_filename(module_name)
output_file = os.path.join(src_dir, "{}.cpp".format(safe_module_name))
# Only print module info for first 5 and last one, or if total <= 10
if total_modules <= 10 or module_index <= 5 or module_index == total_modules:
print(
"\n [{}/{}] Processing module: {} ({} functions)".format(
module_index, total_modules, module_name, len(funcs)
)
)
elif module_index == 6:
print("\n ... processing {} more modules ...".format(total_modules - 6))
module_decompiled = 0
module_failed = 0
with open(output_file, "w") as f:
write_file_header(f, module_name, len(funcs))
# Add include for the module's own header (in ../include/)
f.write('#include "../include/{}.h"\n\n'.format(safe_module_name))
# Sort functions by display name
sorted_funcs = sorted(funcs, key=lambda x: x[1])
for func, display_name, mangled_name in sorted_funcs:
if monitor.isCancelled():
break
current_func += 1
# Output progress for shell script to parse
print(
"[PROGRESS] {}/{} {}".format(
current_func, func_count, display_name[:50]
)
)
# Decompile with class/struct enhancement
decompiled = get_decompiled_function_elf(
decomp_ifc, func, monitor, cpp_classes, struct_info
)
if decompiled:
# Extract signature for header file
signature = extract_function_signature(decompiled)
if signature:
module_signatures[module_name].append((display_name, signature))
# Check if this is a virtual method
class_name = extract_class_from_method(display_name)
is_virtual = False
vtable_idx = -1
if class_name and class_name in cpp_classes:
for m_mangled, m_name, m_virtual, m_idx in cpp_classes[
class_name
].methods:
if m_mangled == mangled_name:
is_virtual = m_virtual
vtable_idx = m_idx
break
f.write("// " + "=" * 60 + "\n")
f.write("// Function: {}\n".format(display_name))
if mangled_name != display_name:
f.write("// Mangled: {}\n".format(mangled_name))
if class_name:
f.write("// Class: {}\n".format(class_name))
if is_virtual:
if vtable_idx >= 0:
f.write(
"// Virtual: Yes (vtable index {})\n".format(vtable_idx)
)
else:
f.write("// Virtual: Yes\n")
f.write(
"// Address: 0x{:08x}\n".format(
func.getEntryPoint().getOffset()
)
)
f.write("// " + "=" * 60 + "\n\n")
f.write(decompiled)
f.write("\n")
module_decompiled += 1
else:
f.write(
"// [FAILED] Could not decompile: {}\n".format(display_name)
)
f.write(
"// Address: 0x{:08x}\n\n".format(
func.getEntryPoint().getOffset()
)
)
module_failed += 1
# Only print result for first 5 and last one, or if total <= 10
if total_modules <= 10 or module_index <= 5 or module_index == total_modules:
print(
" -> {}.cpp: {} OK, {} failed".format(
safe_module_name, module_decompiled, module_failed
)
)
total_decompiled += module_decompiled
total_failed += module_failed
# Close decompiler
decomp_ifc.dispose()