nabla-cli 0.2.1

An OSS tool for reverse engineering and binary composition analysis
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
use crate::binary::{BinaryAnalysis, CodeSection};
use capstone::prelude::*;
use petgraph::Direction;
use petgraph::algo::{dijkstra, dominators};
use petgraph::graph::{DiGraph, NodeIndex};
use petgraph::visit::EdgeRef;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Represents different types of basic blocks in the Control Flow Graph
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BasicBlockType {
    /// Entry point of a function
    FunctionEntry { function_name: String, address: u64 },
    /// Regular basic block with sequential instructions
    Sequential {
        start_address: u64,
        end_address: u64,
    },
    /// Conditional branch block
    ConditionalBranch { address: u64, condition: String },
    /// Unconditional jump
    UnconditionalJump { address: u64, target: u64 },
    /// Function call site
    FunctionCall { address: u64, target: String },
    /// Return statement
    Return { address: u64 },
    /// Exception handler
    ExceptionHandler { address: u64, handler_type: String },
}

/// Represents edge types in the CFG
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EdgeType {
    /// Sequential execution (fall-through)
    Sequential,
    /// Conditional branch taken
    ConditionalTrue,
    /// Conditional branch not taken
    ConditionalFalse,
    /// Unconditional jump
    Jump,
    /// Function call
    Call,
    /// Function return
    Return,
    /// Exception flow
    Exception,
}

/// Basic block containing instructions and metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BasicBlock {
    pub block_type: BasicBlockType,
    pub instructions: Vec<Instruction>,
    pub size_bytes: usize,
    pub execution_count: Option<u64>, // For profiling integration
}

/// Disassembled instruction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Instruction {
    pub address: u64,
    pub mnemonic: String,
    pub operands: String,
    pub bytes: Vec<u8>,
    pub size: u32,
    pub is_branch: bool,
    pub is_call: bool,
    pub is_return: bool,
    pub branch_target: Option<u64>,
}

/// Control Flow Graph with proper basic block analysis
#[derive(Debug)]
pub struct ControlFlowGraph {
    pub graph: DiGraph<BasicBlock, EdgeType>,
    /// Maps addresses to node indices for quick lookup
    address_to_node: HashMap<u64, NodeIndex>,
    /// Maps function names to their entry blocks
    function_entries: HashMap<String, NodeIndex>,
    /// Detected loops in the CFG
    pub loops: Vec<LoopInfo>,
    /// Dominator tree information
    pub dominators: Option<dominators::Dominators<NodeIndex>>,
}

/// Information about detected loops
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoopInfo {
    pub header: u64,
    pub back_edges: Vec<(u64, u64)>,
    pub loop_blocks: HashSet<u64>,
    pub nesting_level: usize,
    pub loop_type: LoopType,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LoopType {
    Natural,
    Irreducible,
    Infinite,
}

/// Results of inter-procedural analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallGraphAnalysis {
    pub call_sites: Vec<CallSite>,
    pub function_summaries: HashMap<String, FunctionSummary>,
    pub recursive_functions: Vec<String>,
    pub dead_functions: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallSite {
    pub caller_address: u64,
    pub callee: String,
    pub call_type: CallType,
    pub arguments: Vec<String>, // Detected argument patterns
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CallType {
    Direct,
    Indirect,
    Virtual,
    Tail,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionSummary {
    pub entry_address: u64,
    pub size_bytes: usize,
    pub basic_blocks: usize,
    pub cyclomatic_complexity: usize,
    pub calls_made: Vec<String>,
    pub calls_received: Vec<String>,
    pub has_loops: bool,
    pub max_call_depth: usize,
}

impl ControlFlowGraph {
    pub fn new() -> Self {
        Self {
            graph: DiGraph::new(),
            address_to_node: HashMap::new(),
            function_entries: HashMap::new(),
            loops: Vec::new(),
            dominators: None,
        }
    }

    /// Build CFG from binary analysis with Capstone disassembly
    pub fn build_cfg(analysis: &BinaryAnalysis) -> Result<Self, String> {
        let mut cfg = Self::new();

        // Initialize Capstone based on architecture
        let cs = cfg.init_capstone(&analysis.architecture)?;

        // Use binary data for advanced analysis if available
        if let Some(binary_data) = &analysis.binary_data {
            cfg.build_basic_blocks_from_binary(analysis, &cs, binary_data)?;
        } else {
            // Fallback to symbol-based analysis
            cfg.build_basic_blocks_from_symbols(analysis, &cs)?;
        }

        // Detect loops
        cfg.detect_loops();

        // Build dominator tree
        cfg.build_dominators();

        Ok(cfg)
    }

    pub fn init_capstone(&self, architecture: &str) -> Result<Capstone, String> {
        match architecture.to_lowercase().as_str() {
            "x86_64" | "amd64" => Capstone::new()
                .x86()
                .mode(arch::x86::ArchMode::Mode64)
                .syntax(arch::x86::ArchSyntax::Intel)
                .detail(true)
                .build()
                .map_err(|e| format!("Failed to initialize Capstone for x86_64: {}", e)),
            "i386" | "x86" => Capstone::new()
                .x86()
                .mode(arch::x86::ArchMode::Mode32)
                .syntax(arch::x86::ArchSyntax::Intel)
                .detail(true)
                .build()
                .map_err(|e| format!("Failed to initialize Capstone for x86: {}", e)),
            "arm" => Capstone::new()
                .arm()
                .mode(arch::arm::ArchMode::Arm)
                .detail(true)
                .build()
                .map_err(|e| format!("Failed to initialize Capstone for ARM: {}", e)),
            "aarch64" | "arm64" => Capstone::new()
                .arm64()
                .mode(arch::arm64::ArchMode::Arm)
                .detail(true)
                .build()
                .map_err(|e| format!("Failed to initialize Capstone for ARM64: {}", e)),
            "arm_cortex_m" => Capstone::new()
                .arm()
                .mode(arch::arm::ArchMode::Thumb)
                .detail(true)
                .build()
                .map_err(|e| format!("Failed to initialize Capstone for ARM Cortex-M: {}", e)),
            _ => Err(format!("Unsupported architecture: {}", architecture)),
        }
    }

    pub fn build_basic_blocks_from_symbols(
        &mut self,
        analysis: &BinaryAnalysis,
        _cs: &Capstone,
    ) -> Result<(), String> {
        // For now, create basic blocks from function symbols
        // In a real implementation, we'd disassemble the actual binary data

        let entry_point_addr = 0x1000; // Placeholder - would extract from binary

        // Create entry point basic block
        let entry_block = BasicBlock {
            block_type: BasicBlockType::FunctionEntry {
                function_name: "_start".to_string(),
                address: entry_point_addr,
            },
            instructions: vec![Instruction {
                address: entry_point_addr,
                mnemonic: "push".to_string(),
                operands: "rbp".to_string(),
                bytes: vec![0x55],
                size: 1,
                is_branch: false,
                is_call: false,
                is_return: false,
                branch_target: None,
            }],
            size_bytes: 1,
            execution_count: None,
        };

        let entry_node = self.graph.add_node(entry_block);
        self.address_to_node.insert(entry_point_addr, entry_node);
        self.function_entries
            .insert("_start".to_string(), entry_node);

        // Create basic blocks for detected symbols (functions)
        let mut current_addr = entry_point_addr + 0x100;

        for symbol in &analysis.detected_symbols {
            if !symbol.is_empty() && !symbol.starts_with("__") {
                let func_block = BasicBlock {
                    block_type: BasicBlockType::FunctionEntry {
                        function_name: symbol.clone(),
                        address: current_addr,
                    },
                    instructions: self.create_placeholder_instructions(current_addr, symbol),
                    size_bytes: 32, // Placeholder
                    execution_count: None,
                };

                let func_node = self.graph.add_node(func_block);
                self.address_to_node.insert(current_addr, func_node);
                self.function_entries.insert(symbol.clone(), func_node);

                // Add edge from entry point to this function (simplified)
                self.graph.add_edge(entry_node, func_node, EdgeType::Call);

                current_addr += 0x100;
            }
        }

        // Create call sites for imported functions
        for import in &analysis.imports {
            let call_addr = current_addr;
            let call_block = BasicBlock {
                block_type: BasicBlockType::FunctionCall {
                    address: call_addr,
                    target: import.clone(),
                },
                instructions: vec![Instruction {
                    address: call_addr,
                    mnemonic: "call".to_string(),
                    operands: import.clone(),
                    bytes: vec![0xE8, 0x00, 0x00, 0x00, 0x00], // Placeholder
                    size: 5,
                    is_branch: false,
                    is_call: true,
                    is_return: false,
                    branch_target: None,
                }],
                size_bytes: 5,
                execution_count: None,
            };

            let call_node = self.graph.add_node(call_block);
            self.address_to_node.insert(call_addr, call_node);

            current_addr += 0x10;
        }

        Ok(())
    }

   pub fn build_basic_blocks_from_binary(
        &mut self,
        analysis: &BinaryAnalysis,
        cs: &Capstone,
        binary_data: &[u8],
    ) -> Result<(), String> {
        tracing::info!(
            "Performing advanced binary disassembly for {} format (size: {} bytes)",
            analysis.format,
            binary_data.len()
        );

        // Extract entry point based on binary format
        let entry_point = self.extract_entry_point_from_binary(analysis, binary_data)?;

        // Get executable code sections based on format
        let code_sections =
            self.extract_code_sections_by_format(analysis, binary_data, entry_point)?;

        // Disassemble each code section
        for section in &code_sections {
            self.disassemble_code_section(cs, binary_data, section, entry_point)?;
        }

        // Build edges between basic blocks
        self.build_control_flow_edges()?;

        Ok(())
    }

    fn extract_entry_point_from_binary(
        &self,
        analysis: &BinaryAnalysis,
        binary_data: &[u8],
    ) -> Result<u64, String> {
        // First check if entry point is explicitly provided
        if let Some(ep_str) = &analysis.entry_point {
            if let Ok(ep) = u64::from_str_radix(ep_str.trim_start_matches("0x"), 16) {
                return Ok(ep);
            }
        }

        // Extract entry point based on binary format
        match analysis.format.as_str() {
            "ELF" => self.extract_elf_entry_point(binary_data),
            "PE" => self.extract_pe_entry_point(binary_data),
            "Intel HEX" => self.extract_intel_hex_entry_point(binary_data),
            "Motorola S-record" => self.extract_srec_entry_point(binary_data),
            "ARM Cortex-M" => self.extract_arm_cortex_m_entry_point(binary_data),
            "Raw Firmware" => self.extract_raw_firmware_entry_point(analysis, binary_data),
            "DICOM" => Ok(0x0), // DICOM doesn't have traditional entry points
            _ => {
                tracing::warn!(
                    "Unknown binary format: {}, using default entry point",
                    analysis.format
                );
                Ok(0x1000)
            }
        }
    }

    pub fn extract_code_sections_by_format(
        &self,
        analysis: &BinaryAnalysis,
        binary_data: &[u8],
        entry_point: u64,
    ) -> Result<Vec<CodeSection>, String> {
        if !analysis.code_sections.is_empty() {
            return Ok(analysis.code_sections.clone());
        }

        // Generate code sections based on binary format
        match analysis.format.as_str() {
            "ELF" => self.extract_elf_code_sections(binary_data),
            "PE" => self.extract_pe_code_sections(binary_data),
            "Intel HEX" => self.extract_intel_hex_code_sections(binary_data, entry_point),
            "Motorola S-record" => self.extract_srec_code_sections(binary_data, entry_point),
            "ARM Cortex-M" => self.extract_arm_cortex_m_code_sections(binary_data, entry_point),
            "Raw Firmware" => self.extract_raw_firmware_code_sections(binary_data, entry_point),
            _ => {
                // Default single code section
                Ok(vec![CodeSection {
                    name: ".text".to_string(),
                    start_address: entry_point,
                    end_address: entry_point + std::cmp::min(binary_data.len() as u64, 4096),
                    size: std::cmp::min(binary_data.len() as u64, 4096),
                    permissions: "rx".to_string(),
                    section_type: crate::binary::CodeSectionType::Text,
                }])
            }
        }
    }

    // ELF Entry Point and Code Section Extraction
   pub fn extract_elf_entry_point(&self, binary_data: &[u8]) -> Result<u64, String> {
        if binary_data.len() < 64 {
            return Ok(0x1000);
        }

        // Check ELF magic
        if &binary_data[0..4] != b"\x7fELF" {
            return Ok(0x1000);
        }

        let is_64bit = binary_data[4] == 2;

        if is_64bit && binary_data.len() >= 32 {
            let entry_bytes = &binary_data[24..32];
            let entry_point = u64::from_le_bytes(entry_bytes.try_into().unwrap_or([0; 8]));
            Ok(entry_point)
        } else if binary_data.len() >= 28 {
            let entry_bytes = &binary_data[24..28];
            let entry_point = u32::from_le_bytes(entry_bytes.try_into().unwrap_or([0; 4])) as u64;
            Ok(entry_point)
        } else {
            Ok(0x1000)
        }
    }

    pub fn extract_elf_code_sections(&self, binary_data: &[u8]) -> Result<Vec<CodeSection>, String> {
        if binary_data.len() < 64 || &binary_data[0..4] != b"\x7fELF" {
            let entry_point = self.extract_elf_entry_point(binary_data)?;
            return Ok(vec![CodeSection {
                name: ".text".to_string(),
                start_address: entry_point,
                end_address: entry_point + std::cmp::min(binary_data.len() as u64 / 2, 8192),
                size: std::cmp::min(binary_data.len() as u64 / 2, 8192),
                permissions: "rx".to_string(),
                section_type: crate::binary::CodeSectionType::Text,
            }]);
        }

        let is_64bit = binary_data[4] == 2;
        let is_little_endian = binary_data[5] == 1;

        // Parse ELF header to get section header table
        let (shoff, shentsize, shnum, shstrndx) = if is_64bit {
            if binary_data.len() < 64 {
                return self.extract_elf_code_sections_fallback(binary_data);
            }
            let shoff = if is_little_endian {
                u64::from_le_bytes(binary_data[40..48].try_into().unwrap_or([0; 8]))
            } else {
                u64::from_be_bytes(binary_data[40..48].try_into().unwrap_or([0; 8]))
            };
            let shentsize = if is_little_endian {
                u16::from_le_bytes(binary_data[58..60].try_into().unwrap_or([0; 2]))
            } else {
                u16::from_be_bytes(binary_data[58..60].try_into().unwrap_or([0; 2]))
            };
            let shnum = if is_little_endian {
                u16::from_le_bytes(binary_data[60..62].try_into().unwrap_or([0; 2]))
            } else {
                u16::from_be_bytes(binary_data[60..62].try_into().unwrap_or([0; 2]))
            };
            let shstrndx = if is_little_endian {
                u16::from_le_bytes(binary_data[62..64].try_into().unwrap_or([0; 2]))
            } else {
                u16::from_be_bytes(binary_data[62..64].try_into().unwrap_or([0; 2]))
            };
            (shoff, shentsize, shnum, shstrndx)
        } else {
            if binary_data.len() < 52 {
                return self.extract_elf_code_sections_fallback(binary_data);
            }
            let shoff = if is_little_endian {
                u32::from_le_bytes(binary_data[32..36].try_into().unwrap_or([0; 4])) as u64
            } else {
                u32::from_be_bytes(binary_data[32..36].try_into().unwrap_or([0; 4])) as u64
            };
            let shentsize = if is_little_endian {
                u16::from_le_bytes(binary_data[46..48].try_into().unwrap_or([0; 2]))
            } else {
                u16::from_be_bytes(binary_data[46..48].try_into().unwrap_or([0; 2]))
            };
            let shnum = if is_little_endian {
                u16::from_le_bytes(binary_data[48..50].try_into().unwrap_or([0; 2]))
            } else {
                u16::from_be_bytes(binary_data[48..50].try_into().unwrap_or([0; 2]))
            };
            let shstrndx = if is_little_endian {
                u16::from_le_bytes(binary_data[50..52].try_into().unwrap_or([0; 2]))
            } else {
                u16::from_be_bytes(binary_data[50..52].try_into().unwrap_or([0; 2]))
            };
            (shoff, shentsize, shnum, shstrndx)
        };

        // Validate section header table parameters
        if shoff == 0 || shnum == 0 || shentsize == 0 {
            return self.extract_elf_code_sections_fallback(binary_data);
        }

        if shoff as usize + (shnum as usize * shentsize as usize) > binary_data.len() {
            return self.extract_elf_code_sections_fallback(binary_data);
        }

        // Get string table section for section names
        let string_table = if shstrndx != 0 && (shstrndx as usize) < shnum as usize {
            self.get_elf_string_table(
                binary_data,
                shoff,
                shentsize,
                shstrndx,
                is_64bit,
                is_little_endian,
            )
        } else {
            None
        };

        let mut code_sections = Vec::new();

        // Parse section headers
        for i in 0..shnum {
            let section_offset = shoff as usize + (i as usize * shentsize as usize);
            if section_offset + shentsize as usize > binary_data.len() {
                break;
            }

            let section_data = &binary_data[section_offset..section_offset + shentsize as usize];

            let (sh_name, sh_type, sh_flags, sh_addr, sh_size) = if is_64bit {
                if section_data.len() < 64 {
                    continue;
                }
                let sh_name = if is_little_endian {
                    u32::from_le_bytes(section_data[0..4].try_into().unwrap_or([0; 4]))
                } else {
                    u32::from_be_bytes(section_data[0..4].try_into().unwrap_or([0; 4]))
                };
                let sh_type = if is_little_endian {
                    u32::from_le_bytes(section_data[4..8].try_into().unwrap_or([0; 4]))
                } else {
                    u32::from_be_bytes(section_data[4..8].try_into().unwrap_or([0; 4]))
                };
                let sh_flags = if is_little_endian {
                    u64::from_le_bytes(section_data[8..16].try_into().unwrap_or([0; 8]))
                } else {
                    u64::from_be_bytes(section_data[8..16].try_into().unwrap_or([0; 8]))
                };
                let sh_addr = if is_little_endian {
                    u64::from_le_bytes(section_data[16..24].try_into().unwrap_or([0; 8]))
                } else {
                    u64::from_be_bytes(section_data[16..24].try_into().unwrap_or([0; 8]))
                };
                let sh_size = if is_little_endian {
                    u64::from_le_bytes(section_data[32..40].try_into().unwrap_or([0; 8]))
                } else {
                    u64::from_be_bytes(section_data[32..40].try_into().unwrap_or([0; 8]))
                };
                (sh_name, sh_type, sh_flags, sh_addr, sh_size)
            } else {
                if section_data.len() < 40 {
                    continue;
                }
                let sh_name = if is_little_endian {
                    u32::from_le_bytes(section_data[0..4].try_into().unwrap_or([0; 4]))
                } else {
                    u32::from_be_bytes(section_data[0..4].try_into().unwrap_or([0; 4]))
                };
                let sh_type = if is_little_endian {
                    u32::from_le_bytes(section_data[4..8].try_into().unwrap_or([0; 4]))
                } else {
                    u32::from_be_bytes(section_data[4..8].try_into().unwrap_or([0; 4]))
                };
                let sh_flags = if is_little_endian {
                    u32::from_le_bytes(section_data[8..12].try_into().unwrap_or([0; 4])) as u64
                } else {
                    u32::from_be_bytes(section_data[8..12].try_into().unwrap_or([0; 4])) as u64
                };
                let sh_addr = if is_little_endian {
                    u32::from_le_bytes(section_data[12..16].try_into().unwrap_or([0; 4])) as u64
                } else {
                    u32::from_be_bytes(section_data[12..16].try_into().unwrap_or([0; 4])) as u64
                };
                let sh_size = if is_little_endian {
                    u32::from_le_bytes(section_data[20..24].try_into().unwrap_or([0; 4])) as u64
                } else {
                    u32::from_be_bytes(section_data[20..24].try_into().unwrap_or([0; 4])) as u64
                };
                (sh_name, sh_type, sh_flags, sh_addr, sh_size)
            };

            // Check if this is an executable section (SHT_PROGBITS with SHF_EXECINSTR)
            const SHT_PROGBITS: u32 = 1;
            const SHF_EXECINSTR: u64 = 0x4;

            if sh_type == SHT_PROGBITS && (sh_flags & SHF_EXECINSTR) != 0 && sh_size > 0 {
                // Get section name from string table
                let section_name = if let Some(ref strtab) = string_table {
                    self.get_string_from_table(strtab, sh_name as usize)
                        .unwrap_or_else(|| format!(".section_{}", i))
                } else {
                    format!(".section_{}", i)
                };

                code_sections.push(CodeSection {
                    name: section_name,
                    start_address: sh_addr,
                    end_address: sh_addr + sh_size,
                    size: sh_size,
                    permissions: "rx".to_string(),
                    section_type: crate::binary::CodeSectionType::Text,
                });
            }
        }

        if code_sections.is_empty() {
            return self.extract_elf_code_sections_fallback(binary_data);
        }

        Ok(code_sections)
    }

    fn extract_elf_code_sections_fallback(
        &self,
        binary_data: &[u8],
    ) -> Result<Vec<CodeSection>, String> {
        let entry_point = self.extract_elf_entry_point(binary_data)?;
        Ok(vec![CodeSection {
            name: ".text".to_string(),
            start_address: entry_point,
            end_address: entry_point + std::cmp::min(binary_data.len() as u64 / 2, 8192),
            size: std::cmp::min(binary_data.len() as u64 / 2, 8192),
            permissions: "rx".to_string(),
            section_type: crate::binary::CodeSectionType::Text,
        }])
    }

    pub fn get_elf_string_table(
        &self,
        binary_data: &[u8],
        shoff: u64,
        shentsize: u16,
        shstrndx: u16,
        is_64bit: bool,
        is_little_endian: bool,
    ) -> Option<Vec<u8>> {
        let strtab_section_offset = shoff as usize + (shstrndx as usize * shentsize as usize);
        if strtab_section_offset + shentsize as usize > binary_data.len() {
            return None;
        }

        let section_data =
            &binary_data[strtab_section_offset..strtab_section_offset + shentsize as usize];

        let (sh_offset, sh_size) = if is_64bit {
            if section_data.len() < 64 {
                return None;
            }
            let sh_offset = if is_little_endian {
                u64::from_le_bytes(section_data[24..32].try_into().ok()?)
            } else {
                u64::from_be_bytes(section_data[24..32].try_into().ok()?)
            };
            let sh_size = if is_little_endian {
                u64::from_le_bytes(section_data[32..40].try_into().ok()?)
            } else {
                u64::from_be_bytes(section_data[32..40].try_into().ok()?)
            };
            (sh_offset, sh_size)
        } else {
            if section_data.len() < 40 {
                return None;
            }
            let sh_offset = if is_little_endian {
                u32::from_le_bytes(section_data[16..20].try_into().ok()?) as u64
            } else {
                u32::from_be_bytes(section_data[16..20].try_into().ok()?) as u64
            };
            let sh_size = if is_little_endian {
                u32::from_le_bytes(section_data[20..24].try_into().ok()?) as u64
            } else {
                u32::from_be_bytes(section_data[20..24].try_into().ok()?) as u64
            };
            (sh_offset, sh_size)
        };

        if sh_offset as usize + sh_size as usize > binary_data.len() {
            return None;
        }

        Some(binary_data[sh_offset as usize..(sh_offset + sh_size) as usize].to_vec())
    }

    pub fn get_string_from_table(&self, string_table: &[u8], offset: usize) -> Option<String> {
        if offset >= string_table.len() {
            return None;
        }

        let mut end = offset;
        while end < string_table.len() && string_table[end] != 0 {
            end += 1;
        }

        String::from_utf8(string_table[offset..end].to_vec()).ok()
    }

    // PE Entry Point and Code Section Extraction
    pub fn extract_pe_entry_point(&self, binary_data: &[u8]) -> Result<u64, String> {
        if binary_data.len() < 64 || &binary_data[0..2] != b"MZ" {
            return Ok(0x401000); // Default PE image base
        }

        let pe_offset =
            u32::from_le_bytes(binary_data[0x3C..0x3C + 4].try_into().unwrap_or([0; 4])) as usize;

        if pe_offset + 40 > binary_data.len() || &binary_data[pe_offset..pe_offset + 4] != b"PE\0\0"
        {
            return Ok(0x401000);
        }

        let entry_rva_offset = pe_offset + 24 + 16;
        if entry_rva_offset + 4 <= binary_data.len() {
            let entry_rva = u32::from_le_bytes(
                binary_data[entry_rva_offset..entry_rva_offset + 4]
                    .try_into()
                    .unwrap_or([0; 4]),
            );
            Ok(entry_rva as u64 + 0x400000)
        } else {
            Ok(0x401000)
        }
    }

    pub fn extract_pe_code_sections(&self, binary_data: &[u8]) -> Result<Vec<CodeSection>, String> {
        let entry_point = self.extract_pe_entry_point(binary_data)?;
        Ok(vec![CodeSection {
            name: ".text".to_string(),
            start_address: entry_point,
            end_address: entry_point + std::cmp::min(binary_data.len() as u64 / 2, 8192),
            size: std::cmp::min(binary_data.len() as u64 / 2, 8192),
            permissions: "rx".to_string(),
            section_type: crate::binary::CodeSectionType::Text,
        }])
    }

    // Intel HEX Entry Point and Code Section Extraction
    pub fn extract_intel_hex_entry_point(&self, binary_data: &[u8]) -> Result<u64, String> {
        let hex_content = String::from_utf8_lossy(binary_data);
        let mut lowest_address = u64::MAX;
        let mut entry_point_found = None;

        for line in hex_content.lines() {
            if !line.starts_with(':') || line.len() < 11 {
                continue;
            }

            if let Ok(record_type) = u8::from_str_radix(&line[7..9], 16) {
                match record_type {
                    0x00 => {
                        // Data record
                        if let Ok(address) = u16::from_str_radix(&line[3..7], 16) {
                            let addr = address as u64;
                            if addr < lowest_address {
                                lowest_address = addr;
                            }
                        }
                    }
                    0x05 => {
                        // Start Linear Address
                        if line.len() >= 19 {
                            if let Ok(entry) = u32::from_str_radix(&line[9..17], 16) {
                                entry_point_found = Some(entry as u64);
                            }
                        }
                    }
                    0x03 => {
                        // Start Segment Address
                        if line.len() >= 15 {
                            if let Ok(cs) = u16::from_str_radix(&line[9..13], 16) {
                                if let Ok(ip) = u16::from_str_radix(&line[13..17], 16) {
                                    entry_point_found = Some((cs as u64) * 16 + (ip as u64));
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        Ok(entry_point_found.unwrap_or(if lowest_address != u64::MAX {
            lowest_address
        } else {
            0x0000
        }))
    }

    pub fn extract_intel_hex_code_sections(
        &self,
        binary_data: &[u8],
        entry_point: u64,
    ) -> Result<Vec<CodeSection>, String> {
        let hex_content = String::from_utf8_lossy(binary_data);
        let mut memory_regions = Vec::new();
        let mut current_base_address = 0u64;

        for line in hex_content.lines() {
            if !line.starts_with(':') || line.len() < 11 {
                continue;
            }

            if let Ok(record_type) = u8::from_str_radix(&line[7..9], 16) {
                match record_type {
                    0x00 => {
                        // Data record
                        if let Ok(byte_count) = u8::from_str_radix(&line[1..3], 16) {
                            if let Ok(address) = u16::from_str_radix(&line[3..7], 16) {
                                let full_address = current_base_address + (address as u64);
                                memory_regions.push((full_address, byte_count as u64));
                            }
                        }
                    }
                    0x04 => {
                        // Extended Linear Address
                        if let Ok(base) = u16::from_str_radix(&line[9..13], 16) {
                            current_base_address = (base as u64) << 16;
                        }
                    }
                    _ => {}
                }
            }
        }

        if memory_regions.is_empty() {
            return Ok(vec![CodeSection {
                name: "flash".to_string(),
                start_address: entry_point,
                end_address: entry_point + 1024,
                size: 1024,
                permissions: "rx".to_string(),
                section_type: crate::binary::CodeSectionType::Text,
            }]);
        }

        memory_regions.sort_by_key(|&(addr, _)| addr);
        let total_size: u64 = memory_regions.iter().map(|&(_, size)| size).sum();

        Ok(vec![CodeSection {
            name: "flash".to_string(),
            start_address: memory_regions[0].0,
            end_address: memory_regions[0].0 + total_size,
            size: total_size,
            permissions: "rx".to_string(),
            section_type: crate::binary::CodeSectionType::Text,
        }])
    }

    // Motorola S-Record Entry Point and Code Section Extraction
    pub fn extract_srec_entry_point(&self, binary_data: &[u8]) -> Result<u64, String> {
        let srec_content = String::from_utf8_lossy(binary_data);
        let mut entry_point = None;
        let mut lowest_address = u64::MAX;

        for line in srec_content.lines() {
            if !line.starts_with('S') || line.len() < 4 {
                continue;
            }

            let record_type = &line[1..2];
            match record_type {
                "1" | "2" | "3" => {
                    // Data records
                    if let Ok(_byte_count) = u8::from_str_radix(&line[2..4], 16) {
                        let addr_len = match record_type {
                            "1" => 4,
                            "2" => 6,
                            "3" => 8,
                            _ => 4,
                        };

                        if line.len() >= 4 + addr_len {
                            if let Ok(address) = u64::from_str_radix(&line[4..4 + addr_len], 16) {
                                if address < lowest_address {
                                    lowest_address = address;
                                }
                            }
                        }
                    }
                }
                "7" | "8" | "9" => {
                    // Start address records
                    let addr_len = match record_type {
                        "9" => 4,
                        "8" => 6,
                        "7" => 8,
                        _ => 4,
                    };

                    if line.len() >= 4 + addr_len {
                        if let Ok(start_addr) = u64::from_str_radix(&line[4..4 + addr_len], 16) {
                            entry_point = Some(start_addr);
                        }
                    }
                }
                _ => {}
            }
        }

        Ok(entry_point.unwrap_or(if lowest_address != u64::MAX {
            lowest_address
        } else {
            0x0000
        }))
    }

    pub fn extract_srec_code_sections(
        &self,
        binary_data: &[u8],
        entry_point: u64,
    ) -> Result<Vec<CodeSection>, String> {
        let srec_content = String::from_utf8_lossy(binary_data);
        let mut memory_regions = Vec::new();

        for line in srec_content.lines() {
            if !line.starts_with('S') || line.len() < 4 {
                continue;
            }

            let record_type = &line[1..2];
            if matches!(record_type, "1" | "2" | "3") {
                if let Ok(byte_count) = u8::from_str_radix(&line[2..4], 16) {
                    let addr_len = match record_type {
                        "1" => 4,
                        "2" => 6,
                        "3" => 8,
                        _ => 4,
                    };

                    if line.len() >= 4 + addr_len {
                        if let Ok(address) = u64::from_str_radix(&line[4..4 + addr_len], 16) {
                            let data_bytes = byte_count - (addr_len as u8 / 2) - 1;
                            memory_regions.push((address, data_bytes as u64));
                        }
                    }
                }
            }
        }

        if memory_regions.is_empty() {
            return Ok(vec![CodeSection {
                name: "program".to_string(),
                start_address: entry_point,
                end_address: entry_point + 1024,
                size: 1024,
                permissions: "rx".to_string(),
                section_type: crate::binary::CodeSectionType::Text,
            }]);
        }

        memory_regions.sort_by_key(|&(addr, _)| addr);
        let total_size: u64 = memory_regions.iter().map(|&(_, size)| size).sum();

        Ok(vec![CodeSection {
            name: "program".to_string(),
            start_address: memory_regions[0].0,
            end_address: memory_regions[0].0 + total_size,
            size: total_size,
            permissions: "rx".to_string(),
            section_type: crate::binary::CodeSectionType::Text,
        }])
    }

    // ARM Cortex-M Entry Point and Code Section Extraction
    pub fn extract_arm_cortex_m_entry_point(&self, binary_data: &[u8]) -> Result<u64, String> {
        if binary_data.len() < 8 {
            return Ok(0x00000000);
        }

        // Reset vector is at offset 4 (little-endian)
        let reset_vector = u32::from_le_bytes(binary_data[4..8].try_into().unwrap_or([0; 4]));

        // ARM Cortex-M addresses have bit 0 set for Thumb mode, clear it
        Ok((reset_vector & !1) as u64)
    }

    fn extract_arm_cortex_m_code_sections(
        &self,
        binary_data: &[u8],
        entry_point: u64,
    ) -> Result<Vec<CodeSection>, String> {
        let flash_base = if entry_point >= 0x08000000 {
            0x08000000
        } else {
            0x00000000
        };

        Ok(vec![CodeSection {
            name: "flash".to_string(),
            start_address: flash_base,
            end_address: flash_base + binary_data.len() as u64,
            size: binary_data.len() as u64,
            permissions: "rx".to_string(),
            section_type: crate::binary::CodeSectionType::Text,
        }])
    }

    // Raw Firmware Entry Point and Code Section Extraction
    pub fn extract_raw_firmware_entry_point(
        &self,
        analysis: &BinaryAnalysis,
        _binary_data: &[u8],
    ) -> Result<u64, String> {
        match analysis.architecture.as_str() {
            "ARM" | "ARM64" | "AARCH64" => {
                if analysis
                    .detected_symbols
                    .iter()
                    .any(|s| s.contains("cortex") || s.contains("thumb"))
                {
                    Ok(0x08000000) // STM32-style
                } else {
                    Ok(0x00000000) // Generic ARM
                }
            }
            "x86" | "x86_64" => Ok(0x00100000), // Common x86 firmware base
            "MIPS" => Ok(0xBFC00000),           // MIPS boot ROM
            "PowerPC" => Ok(0xFFF00000),        // PowerPC boot vector
            "RISC-V" => Ok(0x80000000),         // RISC-V common base
            _ => Ok(0x00000000),                // Default
        }
    }

    pub fn extract_raw_firmware_code_sections(
        &self,
        binary_data: &[u8],
        entry_point: u64,
    ) -> Result<Vec<CodeSection>, String> {
        Ok(vec![CodeSection {
            name: "firmware".to_string(),
            start_address: entry_point,
            end_address: entry_point + binary_data.len() as u64,
            size: binary_data.len() as u64,
            permissions: "rx".to_string(),
            section_type: crate::binary::CodeSectionType::Text,
        }])
    }

    pub fn disassemble_code_section(
        &mut self,
        cs: &Capstone,
        binary_data: &[u8],
        section: &CodeSection,
        entry_point: u64,
    ) -> Result<(), String> {
        let section_start = section.start_address as usize;
        let section_size = section.size as usize;

        // Bounds checking
        if section_start >= binary_data.len() {
            return Ok(()); // Section is beyond binary data
        }

        let actual_size = std::cmp::min(section_size, binary_data.len() - section_start);
        let section_data = &binary_data[section_start..section_start + actual_size];

        tracing::info!(
            "Disassembling section {} at 0x{:x} (size: {})",
            section.name,
            section.start_address,
            actual_size
        );

        // Disassemble the section
        let instructions = cs
            .disasm_all(section_data, section.start_address)
            .map_err(|e| format!("Disassembly failed: {}", e))?;

        // Build basic blocks from disassembled instructions
        self.build_basic_blocks_from_instructions(&instructions, entry_point)?;

        Ok(())
    }

    pub fn build_basic_blocks_from_instructions(
        &mut self,
        instructions: &capstone::Instructions,
        entry_point: u64,
    ) -> Result<(), String> {
        if instructions.is_empty() {
            return Ok(());
        }

        // Find basic block boundaries
        let mut block_starts = std::collections::HashSet::new();
        block_starts.insert(entry_point);

        // Add instruction addresses that start basic blocks
        for insn in instructions.iter() {
            let addr = insn.address();

            // Check if this is a jump target or function start
            if self.is_block_boundary_instruction(&insn) {
                block_starts.insert(addr);

                // Also mark the next instruction as a block start
                if let Some(next_insn) = instructions.iter().find(|i| i.address() > addr) {
                    block_starts.insert(next_insn.address());
                }
            }

            // Mark targets of branch instructions
            if let Some(target) = self.get_branch_target(&insn) {
                block_starts.insert(target);
            }
        }

        // Convert to sorted vector
        let mut starts: Vec<u64> = block_starts.into_iter().collect();
        starts.sort();

        // Build basic blocks
        for i in 0..starts.len() {
            let start_addr = starts[i];
            let end_addr = if i + 1 < starts.len() {
                starts[i + 1]
            } else {
                instructions
                    .iter()
                    .last()
                    .map(|i| i.address() + i.bytes().len() as u64)
                    .unwrap_or(start_addr + 1)
            };

            // Collect instructions for this block
            let block_instructions: Vec<Instruction> = instructions
                .iter()
                .filter(|insn| insn.address() >= start_addr && insn.address() < end_addr)
                .map(|insn| Instruction {
                    address: insn.address(),
                    mnemonic: insn.mnemonic().unwrap_or("").to_string(),
                    operands: insn.op_str().unwrap_or("").to_string(),
                    bytes: insn.bytes().to_vec(),
                    size: insn.bytes().len() as u32,
                    is_branch: self.is_branch_instruction(&insn),
                    is_call: self.is_call_instruction(&insn),
                    is_return: self.is_return_instruction(&insn),
                    branch_target: self.get_branch_target(&insn),
                })
                .collect();

            if block_instructions.is_empty() {
                continue;
            }

            // Determine block type with improved heuristics
            let block_type = if start_addr == entry_point {
                BasicBlockType::FunctionEntry {
                    function_name: format!("entry_{:x}", start_addr),
                    address: start_addr,
                }
            } else if self.is_function_entry_heuristic(start_addr, &block_instructions) {
                BasicBlockType::FunctionEntry {
                    function_name: format!("func_{:x}", start_addr),
                    address: start_addr,
                }
            } else if self.is_conditional_block(&block_instructions) {
                BasicBlockType::ConditionalBranch {
                    address: start_addr,
                    condition: self.extract_condition(&block_instructions),
                }
            } else if self.is_call_block(&block_instructions) {
                BasicBlockType::FunctionCall {
                    address: start_addr,
                    target: self.extract_call_target(&block_instructions),
                }
            } else {
                BasicBlockType::Sequential {
                    start_address: start_addr,
                    end_address: end_addr,
                }
            };

            let basic_block = BasicBlock {
                instructions: block_instructions,
                block_type,
                size_bytes: (end_addr - start_addr) as usize,
                execution_count: None,
            };

            // Track function entries before moving basic_block
            let function_name = if let BasicBlockType::FunctionEntry { function_name, .. } =
                &basic_block.block_type
            {
                Some(function_name.clone())
            } else {
                None
            };

            let node = self.graph.add_node(basic_block);
            self.address_to_node.insert(start_addr, node);

            if let Some(name) = function_name {
                self.function_entries.insert(name, node);
            }
        }

        Ok(())
    }

    pub fn get_block_end_address(&self, block: &BasicBlock) -> u64 {
        if let Some(last_insn) = block.instructions.last() {
            last_insn.address + last_insn.size as u64
        } else {
            match &block.block_type {
                BasicBlockType::FunctionEntry { address, .. } => *address,
                BasicBlockType::Sequential { end_address, .. } => *end_address,
                BasicBlockType::ConditionalBranch { address, .. } => *address,
                BasicBlockType::UnconditionalJump { address, .. } => *address,
                BasicBlockType::FunctionCall { address, .. } => *address,
                BasicBlockType::Return { address } => *address,
                BasicBlockType::ExceptionHandler { address, .. } => *address,
            }
        }
    }

    pub fn build_control_flow_edges(&mut self) -> Result<(), String> {
        let nodes: Vec<_> = self.graph.node_indices().collect();

        for node_idx in nodes {
            if let Some(block) = self.graph.node_weight(node_idx).cloned() {
                let end_address = self.get_block_end_address(&block);
                match &block.block_type {
                    BasicBlockType::Sequential { .. } => {
                        if let Some(next_node) = self.find_block_at_address(end_address) {
                            self.graph
                                .add_edge(node_idx, next_node, EdgeType::Sequential);
                        }
                    }
                    BasicBlockType::ConditionalBranch { .. } => {
                        // Connect to both fall-through and branch target
                        if let Some(next_node) = self.find_block_at_address(end_address) {
                            self.graph
                                .add_edge(node_idx, next_node, EdgeType::ConditionalFalse);
                        }
                        // TODO: Extract and connect to branch target
                    }
                    BasicBlockType::FunctionCall { .. } => {
                        if let Some(return_node) = self.find_block_at_address(end_address) {
                            self.graph.add_edge(node_idx, return_node, EdgeType::Call);
                        }
                    }
                    _ => {
                        if let Some(next_node) = self.find_block_at_address(end_address) {
                            self.graph
                                .add_edge(node_idx, next_node, EdgeType::Sequential);
                        }
                    }
                }
            }
        }

        Ok(())
    }

    pub fn find_block_at_address(&self, address: u64) -> Option<NodeIndex> {
        self.address_to_node.get(&address).copied()
    }

    // Helper methods for instruction analysis
    pub fn is_function_entry_heuristic(&self, addr: u64, instructions: &[Instruction]) -> bool {
        if instructions.is_empty() {
            return false;
        }

        let first_insn = &instructions[0];

        // Common function prologue patterns
        match first_insn.mnemonic.as_str() {
            // x86/x64 function prologues
            "push" if first_insn.operands.contains("bp") || first_insn.operands.contains("rbp") => {
                true
            }
            "mov" if first_insn.operands.contains("bp") || first_insn.operands.contains("rbp") => {
                true
            }
            "sub" if first_insn.operands.contains("sp") || first_insn.operands.contains("rsp") => {
                true
            }

            // ARM function prologues
            "push" if first_insn.operands.contains("lr") => true,
            "stmdb" if first_insn.operands.contains("sp!") => true,
            "str" if first_insn.operands.contains("lr") => true,

            // RISC-V function prologues
            "addi" if first_insn.operands.contains("sp") => true,
            "sd" if first_insn.operands.contains("ra") => true,

            _ => {
                // Check if address is aligned (common for function entries)
                addr % 4 == 0 || addr % 8 == 0
            }
        }
    }

    pub fn is_block_boundary_instruction(&self, insn: &capstone::Insn) -> bool {
        if let Some(mnemonic) = insn.mnemonic() {
            matches!(
                mnemonic,
                // x86/x64
                "ret" | "retq" | "retn" |           // Returns
                "jmp" | "jmpq" |                    // Unconditional jumps
                "je" | "jne" | "jz" | "jnz" |       // Conditional jumps
                "jl" | "jle" | "jg" | "jge" |
                "ja" | "jae" | "jb" | "jbe" |
                "jo" | "jno" | "js" | "jns" |
                "call" | "callq" |                  // Function calls
                
                // ARM
                "bx" | "blx" | "b" | "bl" |
                "bmi" | "bpl" | "bvs" | "bvc" | "bhi" | "bls" |
                
                // RISC-V/ARM/MIPS shared conditional branches
                "beq" | "bne" | "blt" | "ble" | "bgt" | "bge" |
                
                // RISC-V/MIPS specific
                "bltu" | "bgeu" | "jal" | "jalr" |
                "j" | "jr" | "bgtz" | "blez" | "bltz" | "bgez"
            )
        } else {
            false
        }
    }

    pub fn is_branch_instruction(&self, insn: &capstone::Insn) -> bool {
        if let Some(mnemonic) = insn.mnemonic() {
            matches!(
                mnemonic,
                "jmp"
                    | "jmpq"
                    | "je"
                    | "jne"
                    | "jz"
                    | "jnz"
                    | "jl"
                    | "jle"
                    | "jg"
                    | "jge"
                    | "ja"
                    | "jae"
                    | "jb"
                    | "jbe"
                    | "jo"
                    | "jno"
                    | "js"
                    | "jns"
                    | "b"
                    | "bl"
                    | "bx"
                    | "blx"
                    | "beq"
                    | "bne"
                    | "blt"
                    | "ble"
                    | "bgt"
                    | "bge"
                    | "bmi"
                    | "bpl"
                    | "bvs"
                    | "bvc"
                    | "bhi"
                    | "bls"
                    | "jal"
                    | "jalr"
                    | "j"
                    | "jr"
                    | "bgtz"
                    | "blez"
                    | "bltz"
                    | "bgez"
                    | "bltu"
                    | "bgeu"
            )
        } else {
            false
        }
    }

    pub fn is_call_instruction(&self, insn: &capstone::Insn) -> bool {
        if let Some(mnemonic) = insn.mnemonic() {
            matches!(mnemonic, "call" | "callq" | "bl" | "blx" | "jal" | "jalr")
        } else {
            false
        }
    }

    pub fn is_return_instruction(&self, insn: &capstone::Insn) -> bool {
        if let Some(mnemonic) = insn.mnemonic() {
            matches!(mnemonic, "ret" | "retq" | "retn" | "bx" | "jr")
        } else {
            false
        }
    }

    pub fn get_branch_target(&self, insn: &capstone::Insn) -> Option<u64> {
        if let Some(op_str) = insn.op_str() {
            // Try to parse hex addresses (e.g., "0x401000")
            if op_str.starts_with("0x") {
                if let Ok(addr) = u64::from_str_radix(&op_str[2..], 16) {
                    return Some(addr);
                }
            }
            // Try to parse decimal addresses
            if let Ok(addr) = op_str.parse::<u64>() {
                return Some(addr);
            }
        }
        None
    }

    pub fn is_conditional_block(&self, instructions: &[Instruction]) -> bool {
        instructions.iter().any(|insn| {
            matches!(
                insn.mnemonic.as_str(),
                // x86/x64 conditional jumps
                "je" | "jne" | "jz" | "jnz" |
                "jl" | "jle" | "jg" | "jge" |
                "ja" | "jae" | "jb" | "jbe" |
                "jo" | "jno" | "js" | "jns" |
                
                // ARM specific
                "bmi" | "bpl" | "bvs" | "bvc" | "bhi" | "bls" |
                
                // Shared conditional branches (ARM/RISC-V/MIPS)
                "beq" | "bne" | "blt" | "ble" | "bgt" | "bge" |
                
                // RISC-V/MIPS specific
                "bltu" | "bgeu" | "bgtz" | "blez" | "bltz" | "bgez"
            )
        })
    }

   pub fn is_call_block(&self, instructions: &[Instruction]) -> bool {
        instructions.iter().any(|insn| {
            matches!(
                insn.mnemonic.as_str(),
                "call" | "callq" |      // x86/x64
                "bl" | "blx" |          // ARM
                "jal" | "jalr" // RISC-V
            )
        })
    }

    pub fn extract_condition(&self, instructions: &[Instruction]) -> String {
        for insn in instructions {
            if matches!(
                insn.mnemonic.as_str(),
                "je" | "jne"
                    | "jz"
                    | "jnz"
                    | "jl"
                    | "jle"
                    | "jg"
                    | "jge"
                    | "ja"
                    | "jae"
                    | "jb"
                    | "jbe"
                    | "jo"
                    | "jno"
                    | "js"
                    | "jns"
                    | "beq"
                    | "bne"
                    | "blt"
                    | "ble"
                    | "bgt"
                    | "bge"
                    | "bmi"
                    | "bpl"
                    | "bvs"
                    | "bvc"
                    | "bhi"
                    | "bls"
                    | "bgtz"
                    | "blez"
                    | "bltz"
                    | "bgez"
                    | "bltu"
                    | "bgeu"
            ) {
                return format!("{} {}", insn.mnemonic, insn.operands);
            }
        }
        "unknown".to_string()
    }

    pub fn extract_call_target(&self, instructions: &[Instruction]) -> String {
        for insn in instructions {
            if matches!(
                insn.mnemonic.as_str(),
                "call" | "callq" | "bl" | "blx" | "jal" | "jalr"
            ) {
                return insn.operands.clone();
            }
        }
        "unknown".to_string()
    }

    pub fn create_placeholder_instructions(
        &self,
        start_addr: u64,
        function_name: &str,
    ) -> Vec<Instruction> {
        // Create realistic instruction sequences based on function names
        let mut instructions = Vec::new();
        let mut addr = start_addr;

        // Function prologue
        instructions.push(Instruction {
            address: addr,
            mnemonic: "push".to_string(),
            operands: "rbp".to_string(),
            bytes: vec![0x55],
            size: 1,
            is_branch: false,
            is_call: false,
            is_return: false,
            branch_target: None,
        });
        addr += 1;

        instructions.push(Instruction {
            address: addr,
            mnemonic: "mov".to_string(),
            operands: "rbp, rsp".to_string(),
            bytes: vec![0x48, 0x89, 0xE5],
            size: 3,
            is_branch: false,
            is_call: false,
            is_return: false,
            branch_target: None,
        });
        addr += 3;

        // Add function-specific instructions based on name patterns
        if function_name.contains("malloc") || function_name.contains("alloc") {
            instructions.push(Instruction {
                address: addr,
                mnemonic: "test".to_string(),
                operands: "rdi, rdi".to_string(),
                bytes: vec![0x48, 0x85, 0xFF],
                size: 3,
                is_branch: false,
                is_call: false,
                is_return: false,
                branch_target: None,
            });
            addr += 3;

            instructions.push(Instruction {
                address: addr,
                mnemonic: "jz".to_string(),
                operands: format!("0x{:x}", addr + 10),
                bytes: vec![0x74, 0x08],
                size: 2,
                is_branch: true,
                is_call: false,
                is_return: false,
                branch_target: Some(addr + 10),
            });
        }

        // Function epilogue
        addr += 10;
        instructions.push(Instruction {
            address: addr,
            mnemonic: "pop".to_string(),
            operands: "rbp".to_string(),
            bytes: vec![0x5D],
            size: 1,
            is_branch: false,
            is_call: false,
            is_return: false,
            branch_target: None,
        });
        addr += 1;

        instructions.push(Instruction {
            address: addr,
            mnemonic: "ret".to_string(),
            operands: "".to_string(),
            bytes: vec![0xC3],
            size: 1,
            is_branch: false,
            is_call: false,
            is_return: true,
            branch_target: None,
        });

        instructions
    }

    pub fn detect_loops(&mut self) {
        // Implement natural loop detection using dominator analysis
        // This is a simplified version - would need proper back-edge detection

        for node_idx in self.graph.node_indices() {
            if let Some(block) = self.graph.node_weight(node_idx) {
                // Look for back edges (edges to blocks that dominate the current block)
                for edge in self.graph.edges_directed(node_idx, Direction::Outgoing) {
                    let target = edge.target();

                    // Simple heuristic: if target address < current address, it might be a back edge
                    if let (Some(current_addr), Some(target_addr)) = (
                        self.get_block_address(block),
                        self.get_block_address_by_node(target),
                    ) {
                        if target_addr < current_addr {
                            // Potential back edge - create loop info
                            let loop_info = LoopInfo {
                                header: target_addr,
                                back_edges: vec![(current_addr, target_addr)],
                                loop_blocks: HashSet::from([current_addr, target_addr]),
                                nesting_level: 1,
                                loop_type: LoopType::Natural,
                            };

                            self.loops.push(loop_info);
                        }
                    }
                }
            }
        }
    }

    pub fn build_dominators(&mut self) {
        if let Some(entry_node) = self.graph.node_indices().next() {
            self.dominators = Some(dominators::simple_fast(&self.graph, entry_node));
        }
    }

    pub fn get_block_address(&self, block: &BasicBlock) -> Option<u64> {
        match &block.block_type {
            BasicBlockType::FunctionEntry { address, .. } => Some(*address),
            BasicBlockType::Sequential { start_address, .. } => Some(*start_address),
            BasicBlockType::ConditionalBranch { address, .. } => Some(*address),
            BasicBlockType::UnconditionalJump { address, .. } => Some(*address),
            BasicBlockType::FunctionCall { address, .. } => Some(*address),
            BasicBlockType::Return { address } => Some(*address),
            BasicBlockType::ExceptionHandler { address, .. } => Some(*address),
        }
    }

   pub fn get_block_address_by_node(&self, node_idx: NodeIndex) -> Option<u64> {
        self.graph
            .node_weight(node_idx)
            .and_then(|block| self.get_block_address(block))
    }

    /// Analyze call graph relationships
    pub fn analyze_call_graph(&self, analysis: &BinaryAnalysis) -> CallGraphAnalysis {
        let mut call_sites = Vec::new();
        let mut function_summaries = HashMap::new();
        let mut recursive_functions = Vec::new();
        let mut dead_functions = Vec::new();

        // Build function summaries
        for (func_name, &node_idx) in &self.function_entries {
            let mut calls_made = Vec::new();
            let mut total_size = 0;

            // Traverse all blocks reachable from this function entry
            let reachable = dijkstra(&self.graph, node_idx, None, |_| 1);
            let basic_blocks = reachable.len();

            // Analyze outgoing call edges
            for edge in self.graph.edges_directed(node_idx, Direction::Outgoing) {
                if matches!(edge.weight(), EdgeType::Call) {
                    if let Some(target_block) = self.graph.node_weight(edge.target()) {
                        if let BasicBlockType::FunctionCall { target, .. } =
                            &target_block.block_type
                        {
                            calls_made.push(target.clone());

                            call_sites.push(CallSite {
                                caller_address: self.get_block_address(target_block).unwrap_or(0),
                                callee: target.clone(),
                                call_type: CallType::Direct,
                                arguments: vec![], // Would analyze calling convention
                            });
                        }
                    }
                }
            }

            // Calculate cyclomatic complexity (simplified)
            let mut complexity = 1; // Base complexity
            for reachable_node in reachable.keys() {
                if let Some(block) = self.graph.node_weight(*reachable_node) {
                    total_size += block.size_bytes;

                    match &block.block_type {
                        BasicBlockType::ConditionalBranch { .. } => complexity += 1,
                        _ => {}
                    }
                }
            }

            // Check for recursion
            if calls_made.contains(func_name) {
                recursive_functions.push(func_name.clone());
            }

            function_summaries.insert(
                func_name.clone(),
                FunctionSummary {
                    entry_address: self.get_block_address_by_node(node_idx).unwrap_or(0),
                    size_bytes: total_size,
                    basic_blocks,
                    cyclomatic_complexity: complexity,
                    calls_made,
                    calls_received: vec![], // Would be filled by reverse analysis
                    has_loops: self.loops.iter().any(|l| {
                        l.loop_blocks
                            .contains(&self.get_block_address_by_node(node_idx).unwrap_or(0))
                    }),
                    max_call_depth: 1, // Would require recursive analysis
                },
            );
        }

        // Find dead functions (not called by anyone)
        for symbol in &analysis.detected_symbols {
            if !call_sites.iter().any(|cs| cs.callee == *symbol)
                && symbol != "_start"
                && symbol != "main"
            {
                dead_functions.push(symbol.clone());
            }
        }

        CallGraphAnalysis {
            call_sites,
            function_summaries,
            recursive_functions,
            dead_functions,
        }
    }

    /// Get basic blocks that are loop headers
    #[allow(dead_code)]
    pub fn get_loop_headers(&self) -> Vec<u64> {
        self.loops.iter().map(|l| l.header).collect()
    }

    /// Check if an address is in a loop
    #[allow(dead_code)]
    pub fn is_in_loop(&self, address: u64) -> bool {
        self.loops.iter().any(|l| l.loop_blocks.contains(&address))
    }

    /// Get dominator information for a block
    #[allow(dead_code)]
    pub fn get_dominators(&self, node: NodeIndex) -> Option<Vec<NodeIndex>> {
        self.dominators.as_ref().map(|dom| {
            let mut dominators = Vec::new();
            let mut current = Some(node);

            while let Some(n) = current {
                dominators.push(n);
                current = dom.immediate_dominator(n);
            }

            dominators
        })
    }
}

/// Separate exploitability analysis that uses the CFG
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExploitabilityAnalysis {
    pub is_reachable: bool,
    pub path: Option<Vec<String>>,
    pub sink: String,
    pub confidence: f32,
    pub attack_vectors: Vec<AttackVector>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttackVector {
    pub vector_type: String,
    pub entry_points: Vec<u64>,
    pub vulnerable_path: Vec<u64>,
    pub prerequisites: Vec<String>,
    pub impact: String,
}

impl ExploitabilityAnalysis {
    pub fn analyze(cfg: &ControlFlowGraph, sources: &[String], sink: &str) -> Self {
        // Use the CFG to perform more sophisticated reachability analysis
        let mut is_reachable = false;
        let mut path = None;
        let mut attack_vectors = Vec::new();

        // Find sink node
        if let Some(&sink_node) = cfg.function_entries.get(sink) {
            // Check reachability from each source
            for source in sources {
                if let Some(&source_node) = cfg.function_entries.get(source) {
                    let paths = dijkstra(&cfg.graph, source_node, Some(sink_node), |_| 1);

                    if paths.contains_key(&sink_node) {
                        is_reachable = true;

                        // Build path description
                        let mut path_nodes = Vec::new();
                        let current = sink_node;
                        while let Some(block) = cfg.graph.node_weight(current) {
                            if let Some(addr) = cfg.get_block_address(block) {
                                path_nodes.push(format!("0x{:x}", addr));
                                // This is simplified - would need proper path reconstruction
                                break;
                            }
                        }

                        path = Some(vec![source.clone(), sink.to_string()]);

                        // Create attack vector
                        attack_vectors.push(AttackVector {
                            vector_type: "Control Flow".to_string(),
                            entry_points: vec![
                                cfg.get_block_address_by_node(source_node).unwrap_or(0),
                            ],
                            vulnerable_path: vec![
                                cfg.get_block_address_by_node(sink_node).unwrap_or(0),
                            ],
                            prerequisites: vec!["User input control".to_string()],
                            impact: "Code execution".to_string(),
                        });

                        break;
                    }
                }
            }
        }

        ExploitabilityAnalysis {
            is_reachable,
            path,
            sink: sink.to_string(),
            confidence: if is_reachable { 0.8 } else { 0.0 },
            attack_vectors,
        }
    }
}