Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2079 Add explicit compare method for TypePointerRel

caheckman36a1a33b2511
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
index 97134636cb..e4ad058a1f 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
@@ -1870,6 +1870,26 @@ void TypePointerRel::printRaw(ostream &s) const
s << ']';
}
+int4 TypePointerRel::compare(const Datatype &op,int4 level) const
+
+{
+ int4 res = TypePointer::compare(op,level); // Compare as plain pointers first
+ if (res != 0) return res;
+ // Both must be relative pointers
+ TypePointerRel *tp = (TypePointerRel *) &op;
+ // Its possible a formal relative pointer gets compared to its equivalent ephemeral version.
+ // In which case, we prefer the formal version.
+ if (stripped == (TypePointer *)0) {
+ if (tp->stripped != (TypePointer *)0)
+ return -1;
+ }
+ else {
+ if (tp->stripped == (TypePointer *)0)
+ return 1;
+ }
+ return 0;
+}
+
int4 TypePointerRel::compareDependency(const Datatype &op) const
{
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
index bd4fc6a7e3..41ea5a1937 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
@@ -461,6 +461,7 @@ public:
/// \return the offset value in \e address \e units
int4 getPointerOffset(void) const { return AddrSpace::byteToAddressInt(offset, wordsize); }
virtual void printRaw(ostream &s) const;
+ virtual int4 compare(const Datatype &op,int4 level) const;
virtual int4 compareDependency(const Datatype &op) const;
virtual Datatype *clone(void) const { return new TypePointerRel(*this); }
virtual void saveXml(ostream &s) const;