Skip to content

Ghidra is a software reverse engineering (SRE) framework

Adjustments to STORE casting

caheckmana700c5227531
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/typeop.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/typeop.cc
index 0ceff89a11..3ca75d8eec 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/typeop.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/typeop.cc
@@ -427,17 +427,37 @@ TypeOpStore::TypeOpStore(TypeFactory *t) : TypeOp(t,CPUI_STORE,"store")
Datatype *TypeOpStore::getInputCast(const PcodeOp *op,int4 slot,const CastStrategy *castStrategy) const
{
- if (slot!=1) return (Datatype *)0;
- Datatype *reqtype = op->getIn(2)->getHigh()->getType(); // Cast storage pointer to match what's being stored
- Datatype *curtype = op->getIn(1)->getHigh()->getType();
+ if (slot==0) return (Datatype *)0;
+ const Varnode *pointerVn = op->getIn(1);
+ Datatype *pointerType = pointerVn->getHigh()->getType();
+ Datatype *valueType = op->getIn(2)->getHigh()->getType();
AddrSpace *spc = Address::getSpaceFromConst(op->getIn(0)->getAddr());
- if (curtype->getMetatype() == TYPE_PTR)
- curtype = ((TypePointer *)curtype)->getPtrTo();
+ int4 destSize;
+ if (pointerType->getMetatype() == TYPE_PTR) {
+ pointerType = ((TypePointer *)pointerType)->getPtrTo();
+ destSize = pointerType->getSize();
+ }
else
- return tlst->getTypePointer(op->getIn(1)->getSize(),reqtype,spc->getWordSize());
- reqtype = castStrategy->castStandard(reqtype,curtype,false,true);
- if (reqtype == (Datatype *)0) return reqtype;
- return tlst->getTypePointer(op->getIn(1)->getSize(),reqtype,spc->getWordSize());
+ destSize = -1;
+ if (destSize != valueType->getSize()) {
+ if (slot == 1)
+ return tlst->getTypePointer(pointerVn->getSize(),valueType,spc->getWordSize());
+ else
+ return (Datatype *)0;
+ }
+ if (slot == 1) {
+ if (pointerVn->isWritten() && pointerVn->getDef()->code() == CPUI_CAST) {
+ if (pointerVn->isImplied() && pointerVn->loneDescend() == op) {
+ // CAST is already in place, test if it is casting to the right type
+ Datatype *newType = tlst->getTypePointer(pointerVn->getSize(), valueType, spc->getWordSize());
+ if (pointerVn->getHigh()->getType() != newType)
+ return newType;
+ }
+ }
+ return (Datatype *)0;
+ }
+ // If we reach here, cast the value, not the pointer
+ return castStrategy->castStandard(pointerType,valueType,false,true);
}
void TypeOpStore::printRaw(ostream &s,const PcodeOp *op)