Ghidra is a software reverse engineering (SRE) framework
GP-4451 add new DWARF5 DW_OP expression opcodes
dev747368ec37c0f3020a
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionEvaluator.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionEvaluator.javaindex 046e406582..f1bfe59582 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionEvaluator.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionEvaluator.java@@ -17,10 +17,12 @@ package ghidra.app.util.bin.format.dwarf.expression;import static ghidra.app.util.bin.format.dwarf.expression.DWARFExpressionOpCodes.*;+import java.io.IOException;import java.util.ArrayDeque;import java.util.Objects;import ghidra.app.util.bin.format.dwarf.*;+import ghidra.app.util.bin.format.dwarf.attribs.DWARFForm;import ghidra.program.model.lang.Register;/**@@ -41,20 +43,11 @@ public class DWARFExpressionEvaluator {*/private static final int DEFAULT_MAX_STEP_COUNT = 1000;- private final int intSize;+ private final DWARFProgram dprog;+ private final DWARFCompilationUnit cu;private int maxStepCount = DEFAULT_MAX_STEP_COUNT;- /**- * Mirror of {@link DWARFCompilationUnit#getPointerSize()}- */- private final byte pointerSize;-- /**- * Mirror of {@link DWARFProgram#isLittleEndian()}- */- private final boolean isLittleEndian;-private DWARFRegisterMappings registerMappings;/**@@ -87,17 +80,14 @@ public class DWARFExpressionEvaluator {private int currentOpIndex = -1;public DWARFExpressionEvaluator(DWARFCompilationUnit cu) {- this(cu.getPointerSize(), cu.getProgram().isLittleEndian(), cu.getIntSize(),- cu.getProgram().getRegisterMappings());+ this.cu = cu;+ this.dprog = cu.getProgram();+ this.registerMappings =+ Objects.requireNonNullElse(dprog.getRegisterMappings(), DWARFRegisterMappings.DUMMY);}- public DWARFExpressionEvaluator(byte pointerSize, boolean isLittleEndian, int intSize,- DWARFRegisterMappings registerMappings) {- this.pointerSize = pointerSize;- this.isLittleEndian = isLittleEndian;- this.intSize = intSize;- this.registerMappings =- Objects.requireNonNullElse(registerMappings, DWARFRegisterMappings.DUMMY);+ public DWARFCompilationUnit getDWARFCompilationUnit() {+ return cu;}public void setFrameBase(long fb) {@@ -143,8 +133,8 @@ public class DWARFExpressionEvaluator {}public DWARFExpression readExpr(byte[] exprBytes) throws DWARFExpressionException {- DWARFExpression tmp =- DWARFExpression.read(exprBytes, pointerSize, isLittleEndian, intSize);+ DWARFExpression tmp = DWARFExpression.read(exprBytes, cu.getPointerSize(),+ dprog.isLittleEndian(), cu.getIntSize());return tmp;}@@ -526,6 +516,30 @@ public class DWARFExpressionEvaluator {// (on the host) but that the result of this expression gives you valuedwarfStackValue = true;break;++ case DW_OP_addrx:+ try {+ long addr = dprog.getAddress(DWARFForm.DW_FORM_addrx,+ currentOp.getOperandValue(0), cu);+ push(addr);+ break;+ }+ catch (IOException e) {+ throw new DWARFExpressionException(+ "Invalid indirect address index: " + currentOp.getOperandValue(0));+ }+ case DW_OP_constx: // same as addrx, but different relocation-able specifications+ try {+ long addr = dprog.getAddress(DWARFForm.DW_FORM_addrx,+ currentOp.getOperandValue(0), cu);+ push(addr);+ break;+ }+ catch (IOException e) {+ throw new DWARFExpressionException(+ "Invalid indirect address index: " + currentOp.getOperandValue(0));+ }+default:throw new DWARFExpressionException("Unimplemented DWARF expression opcode " +DWARFExpressionOpCodes.toString(opcode));@@ -546,8 +560,8 @@ public class DWARFExpressionEvaluator {@Overridepublic String toString() {- return "DWARFExpressionEvaluator [pointerSize=" + pointerSize + ", isLittleEndian=" +- isLittleEndian + ", frameOffset=" + frameOffset + ", lastRegister=" + lastRegister ++ return "DWARFExpressionEvaluator [frameOffset=" + frameOffset + ", lastRegister=" ++ lastRegister +", lastStackRelative=" + lastStackRelative + ", registerLoc=" + registerLoc +", isDeref=" + isDeref + ", dwarfStackValue=" + dwarfStackValue +", useUnknownRegister=" + useUnknownRegister + "]\nStack:\n" + getStackAsString() +diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionOpCodes.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionOpCodes.javaindex 6afbd81374..24c764ee8a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionOpCodes.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionOpCodes.java@@ -180,6 +180,19 @@ public class DWARFExpressionOpCodes {public static final int DW_OP_bit_piece = 0x9d;public static final int DW_OP_implicit_value = 0x9e;public static final int DW_OP_stack_value = 0x9f;++ // DWARF5+ public static final int DW_OP_implicit_pointer = 0xa0;+ public static final int DW_OP_addrx = 0xa1;+ public static final int DW_OP_constx = 0xa2;+ public static final int DW_OP_entry_value = 0xa3;+ public static final int DW_OP_const_type = 0xa4;+ public static final int DW_OP_regval_type = 0xa5;+ public static final int DW_OP_deref_type = 0xa6;+ public static final int DW_OP_xderef_type = 0xa7;+ public static final int DW_OP_convert = 0xa8;+ public static final int DW_OP_reinterpret = 0xa9;+public static final int DW_OP_lo_user = 0xe0;public static final int DW_OP_hi_user = 0xff;@@ -191,9 +204,11 @@ public class DWARFExpressionOpCodes {/*** These opcodes are known, but can not be evaluated in the current Ghidra DWARF code*/- public static final int[] UNSUPPORTED_OPCODES_LIST = { DW_OP_deref_size, DW_OP_xderef,- DW_OP_xderef_size, DW_OP_push_object_address, DW_OP_form_tls_address, DW_OP_call2,- DW_OP_call4, DW_OP_call_ref, DW_OP_implicit_value };+ public static final int[] UNSUPPORTED_OPCODES_LIST =+ { DW_OP_deref_size, DW_OP_xderef, DW_OP_xderef_size, DW_OP_push_object_address,+ DW_OP_form_tls_address, DW_OP_call2, DW_OP_call4, DW_OP_call_ref, DW_OP_implicit_value,+ DW_OP_implicit_pointer, DW_OP_entry_value, DW_OP_const_type, DW_OP_regval_type,+ DW_OP_deref_type, DW_OP_xderef_type, DW_OP_convert, DW_OP_reinterpret };/*** These opcodes are known, but can not be evaluated in the current Ghidra DWARF code.@@ -242,6 +257,18 @@ public class DWARFExpressionOpCodes {addOperandTypeMapping(DW_OP_call_ref, DWARF_INT);// U_INT OR U_LONG depending on DWARF32 or DWARF64addOperandTypeMapping(DW_OP_bit_piece, U_LEB128, U_LEB128);addOperandTypeMapping(DW_OP_implicit_value, U_LEB128, SIZED_BLOB);++ // dwarf5+ addOperandTypeMapping(DW_OP_implicit_pointer, DWARF_INT, S_LEB128);+ addOperandTypeMapping(DW_OP_addrx, U_LEB128);+ addOperandTypeMapping(DW_OP_constx, U_LEB128);+ addOperandTypeMapping(DW_OP_entry_value, U_LEB128, SIZED_BLOB);+ addOperandTypeMapping(DW_OP_const_type, U_LEB128, U_BYTE, SIZED_BLOB);+ addOperandTypeMapping(DW_OP_regval_type, U_LEB128, U_LEB128);+ addOperandTypeMapping(DW_OP_deref_type, U_BYTE, U_LEB128);+ addOperandTypeMapping(DW_OP_xderef_type, U_BYTE, U_LEB128);+ addOperandTypeMapping(DW_OP_convert, U_LEB128);+ addOperandTypeMapping(DW_OP_reinterpret, U_LEB128);}public static final DWARFExpressionOperandType[] EMPTY_OPERANDTYPES = {};diff --git a/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionEvaluatorTest.java b/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionEvaluatorTest.javaindex a27d3f3fc6..f66be2c654 100644--- a/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionEvaluatorTest.java+++ b/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf/expression/DWARFExpressionEvaluatorTest.java@@ -22,17 +22,18 @@ import java.io.IOException;import org.junit.*;-import ghidra.app.util.bin.format.dwarf.DWARFRegisterMappings;-import ghidra.app.util.bin.format.dwarf.expression.*;+import ghidra.app.util.bin.format.dwarf.DWARFTestBase;-public class DWARFExpressionEvaluatorTest {+public class DWARFExpressionEvaluatorTest extends DWARFTestBase {DWARFExpressionEvaluator evaluator;+ @Override@Before- public void setup() {- evaluator = new DWARFExpressionEvaluator((byte) 8, true, 4 /* dwarf32 */,- DWARFRegisterMappings.DUMMY);+ public void setUp() throws Exception {+ super.setUp();+ ensureCompUnit();+ evaluator = new DWARFExpressionEvaluator(cu);}/**@@ -167,12 +168,19 @@ public class DWARFExpressionEvaluatorTest {/* 53 */ (byte) DW_OP_call_ref, 4, 3, 2, 1,- /* 54 */ (byte) DW_OP_implicit_value, (byte) 0x05, 1, 2, 3, 4, 5//+ /* 54 */ (byte) DW_OP_implicit_value, (byte) 0x05, 1, 2, 3, 4, 5, //++ /* 55 */ (byte) DW_OP_implicit_pointer, 1, 0, 0, 0, 2,++ /* 56 */ (byte) DW_OP_addrx, 0++});// @formatter:on+ assertEquals(4, evaluator.getDWARFCompilationUnit().getIntSize());assertNotNull("Did not successfully instantiate DWARFExpression", expr);- assertEquals("Did not read all opcodes", 55, expr.getOpCount());+ assertEquals("Did not read all opcodes", 57, expr.getOpCount());assertEquals(0x55, expr.getOp(1).getOperandValue(0));assertEquals(0xfe, expr.getOp(2).getOperandValue(0));@@ -205,6 +213,11 @@ public class DWARFExpressionEvaluatorTest {assertEquals(0x33, expr.getOp(20).getOperandValue(0));assertEquals(128, expr.getOp(21).getOperandValue(0));assertEquals(-128, expr.getOp(22).getOperandValue(0));++ assertEquals(5, expr.getOp(54).getOperandValue(0));++ assertEquals(1, expr.getOp(55).getOperandValue(0));+ assertEquals(2, expr.getOp(55).getOperandValue(1));}@Test@@ -402,4 +415,31 @@ public class DWARFExpressionEvaluatorTest {Assert.assertEquals(dee.getExpression().getOpCount(), 3);}}++ @Test+ public void testAddrx() throws DWARFExpressionException {+ // test that OP_addrx fails with invalid index. Needs real test+ DWARFExpression expr = evaluator.readExpr(new byte[] { (byte) DW_OP_addrx, 0 });+ try {+ evaluator.evaluate(expr);+ fail();+ }+ catch (DWARFExpressionException dee) {+ // good+ }+ }++ @Test+ public void testConstx() throws DWARFExpressionException {+ // test that OP_constx fails with invalid index. Needs real test+ DWARFExpression expr = evaluator.readExpr(new byte[] { (byte) DW_OP_constx, 0 });+ try {+ evaluator.evaluate(expr);+ fail();+ }+ catch (DWARFExpressionException dee) {+ // good+ }++ }}