Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6892 writeOpcode for ordinal

caheckmane9d14bb5561f
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Encoder.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Encoder.java
index 45f085bd33..15273c6041 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Encoder.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Encoder.java
@@ -138,4 +138,12 @@ public interface Encoder {
* @throws IOException for errors in the underlying stream
*/
void writeOpcode(AttributeId attribId, OpCode opcode) throws IOException;
+
+ /**
+ * Write a p-code operation opcode into the encoding, given the opcode ordinal, defined in {@link PcodeOp}
+ * @param attribId is the attribute to associate with the opcode
+ * @param opcode is the opcode ordinal
+ * @throws IOException for errors in the underlying stream
+ */
+ void writeOpcode(AttributeId attribId, int opcode) throws IOException;
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedEncode.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedEncode.java
index d2fbf89ee1..c5e4f14ce5 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedEncode.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedEncode.java
@@ -207,6 +207,12 @@ public class PackedEncode implements Encoder {
writeInteger((TYPECODE_SIGNEDINT_POSITIVE << TYPECODE_SHIFT), opcode.ordinal());
}
+ @Override
+ public void writeOpcode(AttributeId attribId, int opcode) throws IOException {
+ writeHeader(ATTRIBUTE, attribId.id());
+ writeInteger((TYPECODE_SIGNEDINT_POSITIVE << TYPECODE_SHIFT), opcode);
+ }
+
/**
* @return the underlying stream
*/
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeOp.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeOp.java
index e89815450b..ba657b0883 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeOp.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeOp.java
@@ -439,7 +439,7 @@ public class PcodeOp {
*/
public void encodeRaw(Encoder encoder, AddressFactory addrFactory) throws IOException {
encoder.openElement(ELEM_OP);
- encoder.writeSignedInteger(ATTRIB_CODE, opcode);
+ encoder.writeOpcode(ATTRIB_CODE, opcode);
encoder.writeSignedInteger(ATTRIB_SIZE, input.length);
if (output == null) {
encoder.openElement(ELEM_VOID);
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/XmlEncode.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/XmlEncode.java
index c74da2042e..7809b2eadb 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/XmlEncode.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/XmlEncode.java
@@ -15,7 +15,7 @@
*/
package ghidra.program.model.pcode;
-import static ghidra.program.model.pcode.AttributeId.ATTRIB_CONTENT;
+import static ghidra.program.model.pcode.AttributeId.*;
import java.io.IOException;
import java.io.OutputStream;
@@ -250,6 +250,11 @@ public class XmlEncode implements CachedEncoder {
buffer.append("\"");
}
+ @Override
+ public void writeOpcode(AttributeId attribId, int opcode) throws IOException {
+ writeOpcode(attribId, OpCode.getOpcode(opcode));
+ }
+
@Override
public void writeTo(OutputStream stream) throws IOException {
byte[] res = buffer.toString().getBytes();