Skip to content

Ghidra is a software reverse engineering (SRE) framework

BitFields - handle launch of bitfield editor via double click (unaligned structure only)

ghidra10a22915976fe
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldEditorPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldEditorPanel.java
index 45cc6049b0..9bceec84b6 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldEditorPanel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldEditorPanel.java
@@ -359,7 +359,7 @@ public class BitFieldEditorPanel extends JPanel {
long allocationSize = useCurrentAllocation ? (Long) allocSizeModel.getValue()
: initialBaseDataType.getLength();
placementComponent.initAdd((int) allocationSize, 1, bitOffset);
- initControls(null, initialBaseDataType);
+ initControls(null, initialBaseDataType, 1);
enableControls(true);
}
@@ -397,7 +397,8 @@ public class BitFieldEditorPanel extends JPanel {
// TODO: adjust offset and allocationSize if needed
placementComponent.setAllocationOffset(allocationOffset);
placementComponent.init(allocationSize, bitfieldDtc);
- initControls(initialFieldName, initialBaseDataType);
+ bitFieldAllocation = placementComponent.getBitFieldAllocation(); // get updated instance
+ initControls(initialFieldName, initialBaseDataType, bitFieldAllocation.getBitSize());
enableControls(bitfieldDtc != null);
}
@@ -405,7 +406,8 @@ public class BitFieldEditorPanel extends JPanel {
placementComponent.componentDeleted(ordinal);
}
- private void initControls(String initialFieldName, DataType initialBaseDataType) {
+ private void initControls(String initialFieldName, DataType initialBaseDataType,
+ int initialBitSize) {
updating = true;
try {
baseDataType = initialBaseDataType;
@@ -416,7 +418,7 @@ public class BitFieldEditorPanel extends JPanel {
BitFieldAllocation bitFieldAllocation = placementComponent.getBitFieldAllocation();
allocSizeModel.setValue((long) bitFieldAllocation.getAllocationByteSize());
int allocBits = 8 * bitFieldAllocation.getAllocationByteSize();
- bitSizeModel.setValue(1L);
+ bitSizeModel.setValue((long) initialBitSize);
bitOffsetModel.setMaximum((long) allocBits - 1);
bitOffsetModel.setValue((long) bitFieldAllocation.getBitOffset());
updateBitSizeModel();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java
index 10bad8a058..8a1f79f220 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java
@@ -446,8 +446,8 @@ public class BitFieldPlacementComponent extends JPanel {
class BitFieldAllocation {
private final int allocationByteSize;
- private int bitSize;
- private int bitOffset;
+ private final int bitSize;
+ private final int bitOffset;
private boolean hasConflict;
// bit layout normalized to big-endian layout
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java
index 381a661b98..b5ad18477a 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java
@@ -30,6 +30,7 @@ import javax.swing.event.ChangeEvent;
import javax.swing.table.*;
import javax.swing.text.JTextComponent;
+import docking.DockingWindowManager;
import docking.action.DockingActionIf;
import docking.actions.KeyBindingUtils;
import docking.dnd.*;
@@ -148,6 +149,29 @@ public abstract class CompositeEditorPanel extends JPanel
table.setDefaultRenderer(DataTypeInstance.class, dtiCellRenderer);
}
+ private boolean launchBitFieldEditor(int modelColumn, int editingRow) {
+ if (model.viewComposite instanceof Structure &&
+ !model.viewComposite.isInternallyAligned() &&
+ model.getDataTypeColumn() == modelColumn && editingRow < model.getNumComponents()) {
+ // check if we are attempting to edit a bitfield
+ DataTypeComponent dtComponent = model.getComponent(editingRow);
+ if (dtComponent.isBitFieldComponent()) {
+ table.getCellEditor().cancelCellEditing();
+
+ BitFieldEditorDialog dlg = new BitFieldEditorDialog(model.viewComposite,
+ provider.dtmService, editingRow, ordinal -> {
+ model.fireTableDataChanged();
+ model.compositeInfoChanged();
+ });
+ Component c = provider.getComponent();
+ Window w = SwingUtilities.windowForComponent(c);
+ DockingWindowManager.showDialog(w, dlg, c);
+ return true;
+ }
+ }
+ return false;
+ }
+
private void setupTableCellEditor() {
table.addPropertyChangeListener("tableCellEditor", evt -> {
@@ -161,7 +185,9 @@ public abstract class CompositeEditorPanel extends JPanel
SwingUtilities.invokeLater(() -> {
int editingRow = table.getEditingRow();
int modelColumn = table.convertColumnIndexToModel(table.getEditingColumn());
- model.beginEditingField(editingRow, modelColumn);
+ if (!launchBitFieldEditor(modelColumn, editingRow)) {
+ model.beginEditingField(editingRow, modelColumn);
+ }
});
}
});
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/BitFieldDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/BitFieldDataType.java
index 61dda1d869..84a7eaa6d9 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/BitFieldDataType.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/BitFieldDataType.java
@@ -29,6 +29,9 @@ import ghidra.util.exception.AssertException;
* less than or equal to the base datatype size and will always be the smallest possible size
* to contain the bitfield and offset within the least significant byte containing the
* lsb of the bitfield.
+ * <p>
+ * NOTE: This datatype implementation is intended for internal use only. Creating bitfields
+ * must be accomplished directly via a Structure or Union instance API method.
*/
public class BitFieldDataType extends AbstractDataType {