27
27
import ghidra .program .model .address .AddressSet ;
28
28
import ghidra .program .model .address .AddressSetView ;
29
29
import ghidra .program .model .address .AddressSpace ;
30
+ import ghidra .program .model .address .AddressOutOfBoundsException ;
30
31
31
32
import ghidra .program .model .block .BasicBlockModel ;
32
33
import ghidra .program .model .block .CodeBlock ;
@@ -576,9 +577,13 @@ private void serialize(DataType data_type) throws Exception {
576
577
serializePrototype ((FunctionSignature ) data_type );
577
578
578
579
} else if (data_type instanceof PartialUnion ) {
579
- name ("kind" ).value ("todo:PartialUnion" ); // TODO(pag): Implement this
580
- name ("size" ).value (data_type .getLength ());
581
-
580
+ DataType parent = ((PartialUnion ) data_type ).getParent ();
581
+ if (parent != data_type ) {
582
+ serialize (parent );
583
+ } else {
584
+ // PartialUnion stripped type is undefined type
585
+ serialize (((PartialUnion ) data_type ).getStrippedDataType ());
586
+ }
582
587
} else if (data_type instanceof BitFieldDataType ) {
583
588
name ("kind" ).value ("todo:BitFieldDataType" ); // TODO(pag): Implement this
584
589
name ("size" ).value (data_type .getLength ());
@@ -846,7 +851,20 @@ private Address convertAddressToRamSpace(Address address) throws Exception {
846
851
if (address == null ) {
847
852
return null ;
848
853
}
849
- return ram_space .getAddress (address .toString (false ));
854
+
855
+ try {
856
+ // If already in RAM space, just return it
857
+ if (address .getAddressSpace ().getName ().equals ("ram" )) {
858
+ return address ;
859
+ }
860
+
861
+ // Get the numeric offset and create a new address in RAM space
862
+ long offset = address .getOffset ();
863
+ return program .getAddressFactory ().getDefaultAddressSpace ().getAddress (offset );
864
+ } catch (Exception e ) {
865
+ println (String .format ("Failed to convert address %s to RAM space: %s" , address , e .getMessage ()));
866
+ return null ;
867
+ }
850
868
}
851
869
852
870
private boolean isCharPointer (Varnode node ) throws Exception {
@@ -875,6 +893,9 @@ private String findNullTerminatedString(Address address, Pointer pointer) throws
875
893
}
876
894
877
895
Address ram_address = convertAddressToRamSpace (address );
896
+ if (ram_address == null ) {
897
+ return null ;
898
+ }
878
899
MemoryBufferImpl memoryBuffer = new MemoryBufferImpl (program .getMemory (), ram_address );
879
900
DataType char_type = pointer .getDataType ();
880
901
//println("Debug: char_type = " + char_type.getName() + ", size = " + char_type.getLength());
@@ -891,14 +912,31 @@ private String findNullTerminatedString(Address address, Pointer pointer) throws
891
912
}
892
913
893
914
private Data getDataReferencedAsConstant (Varnode node ) throws Exception {
915
+ // check if node is null
916
+ if (node == null ) {
917
+ return null ;
918
+ }
919
+
920
+ // Only process constant nodes that aren't nulls (address 0 in constant space)
894
921
if (!node .isConstant () || node .getAddress ().equals (constant_space .getAddress (0 ))) {
895
922
return null ;
896
923
}
924
+
897
925
// Ghidra sometime fail to resolve references to Data and show it as const.
898
926
// Check if it is referencing Data as constant from `ram` addresspace.
899
- Address ram_address = convertAddressToRamSpace (node .getAddress ());
900
- Data data = getDataAt (ram_address );
901
- return data ;
927
+ try {
928
+ // Convert the constant value to a potential RAM address
929
+ Address ram_address = convertAddressToRamSpace (node .getAddress ());
930
+ if (ram_address == null ) {
931
+ return null ;
932
+ }
933
+ return getDataAt (ram_address );
934
+
935
+ } catch (AddressOutOfBoundsException e ) {
936
+ println ("Address conversion out of bounds for constant: " + e .getMessage ());
937
+ }
938
+
939
+ return null ;
902
940
}
903
941
904
942
// Serialize an input or output varnode.
0 commit comments