Skip to content

Ghidra is a software reverse engineering (SRE) framework

BitFields - added Edit Bitfield Action for Unaligned Structures.

ghidra15ac462441a8b
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 9bceec84b6..f905a891d8 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
@@ -193,7 +193,7 @@ public class BitFieldEditorPanel extends JPanel {
private JComponent createDataTypeChoiceEditor() {
- dtChoiceEditor = new DataTypeSelectionEditor(dtmService, -1, AllowedDataTypes.BITFIELD_USE);
+ dtChoiceEditor = new DataTypeSelectionEditor(dtmService, -1, AllowedDataTypes.BITFIELD_BASE_TYPE);
dtChoiceEditor.setConsumeEnterKeyPress(false);
dtChoiceEditor.setTabCommitsEdit(true);
//dtChoiceEditor.setPreferredDataTypeManager(composite.getDataTypeManager());
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/EditBitFieldAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/EditBitFieldAction.java
new file mode 100644
index 0000000000..5146aadc11
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/EditBitFieldAction.java
@@ -0,0 +1,97 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.app.plugin.core.compositeeditor;
+
+import java.awt.Component;
+import java.awt.Window;
+
+import javax.swing.JTable;
+import javax.swing.SwingUtilities;
+
+import docking.ActionContext;
+import docking.DockingWindowManager;
+import ghidra.program.model.data.DataTypeComponent;
+import ghidra.program.model.data.Structure;
+import ghidra.util.exception.AssertException;
+
+/**
+ * Action for use in the composite data type editor.
+ * This action has help associated with it.
+ */
+public class EditBitFieldAction extends CompositeEditorTableAction {
+
+ private final static String ACTION_NAME = "Edit Bitfield";
+ private final static String GROUP_NAME = BITFIELD_ACTION_GROUP;
+ private final static String DESCRIPTION = "Edit an existing bitfield";
+ private static String[] popupPath = new String[] { ACTION_NAME };
+
+ public EditBitFieldAction(CompositeEditorProvider provider) {
+ super(provider, EDIT_ACTION_PREFIX + ACTION_NAME, GROUP_NAME, popupPath, null, null);
+ setDescription(DESCRIPTION);
+ if (!(model instanceof CompEditorModel)) {
+ throw new AssertException("unsupported use");
+ }
+ adjustEnablement();
+ }
+
+ private DataTypeComponent getUnalignedBitFieldComponent() {
+ CompEditorModel editorModel = (CompEditorModel) model;
+ if ((editorModel.viewComposite instanceof Structure) &&
+ !editorModel.viewComposite.isInternallyAligned() &&
+ editorModel.getNumSelectedRows() == 1) {
+ int rowIndex = model.getSelectedRows()[0];
+ if (rowIndex < model.getNumComponents()) {
+ DataTypeComponent dtComponent = model.getComponent(rowIndex);
+ if (dtComponent.isBitFieldComponent()) {
+ return dtComponent;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public void actionPerformed(ActionContext context) {
+
+ CompEditorModel editorModel = (CompEditorModel) model;
+
+ DataTypeComponent dtComponent = getUnalignedBitFieldComponent();
+ if (dtComponent == null) {
+ return;
+ }
+
+ BitFieldEditorDialog dlg = new BitFieldEditorDialog(editorModel.viewComposite,
+ provider.dtmService, dtComponent.getOrdinal(),
+ ordinal -> refreshTableAndSelection(editorModel, ordinal));
+ Component c = provider.getComponent();
+ Window w = SwingUtilities.windowForComponent(c);
+ DockingWindowManager.showDialog(w, dlg, c);
+ requestTableFocus();
+ }
+
+ private void refreshTableAndSelection(CompEditorModel editorModel, int ordinal) {
+ editorModel.fireTableDataChanged();
+ editorModel.compositeInfoChanged();
+ JTable editorTable = provider.getTable();
+ editorTable.getSelectionModel().setSelectionInterval(ordinal, ordinal);
+ }
+
+ @Override
+ public void adjustEnablement() {
+ setEnabled(getUnalignedBitFieldComponent() != null);
+ }
+
+}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorProvider.java
index 09385c0678..83bf377612 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorProvider.java
@@ -70,7 +70,8 @@ public class StructureEditorProvider extends CompositeEditorProvider {
new EditFieldAction(this),
new HexNumbersAction(this),
new CreateInternalStructureAction(this),
- new AddBitFieldAction(this)
+ new AddBitFieldAction(this),
+ new EditBitFieldAction(this)
};
//@formatter:on
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorProvider.java
index ce42abd343..d76688f114 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorProvider.java
@@ -64,7 +64,8 @@ public class UnionEditorProvider extends CompositeEditorProvider {
new EditComponentAction(this),
new EditFieldAction(this),
new HexNumbersAction(this),
- new AddBitFieldAction(this)
+ new AddBitFieldAction(this),
+ new EditBitFieldAction(this)
};
//@formatter:on
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/util/data/DataTypeParser.java b/Ghidra/Features/Base/src/main/java/ghidra/util/data/DataTypeParser.java
index 976abcdb25..34c6c378d0 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/util/data/DataTypeParser.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/util/data/DataTypeParser.java
@@ -28,7 +28,7 @@ public class DataTypeParser {
public enum AllowedDataTypes {
/**
- * All data-types are permitted
+ * All data-types are permitted (excluding bitfields)
*/
ALL,
/**
@@ -51,7 +51,7 @@ public class DataTypeParser {
* Only Enums, Integer types and those Typedefs based on them
* for use as a bitfield base datatype
*/
- BITFIELD_USE
+ BITFIELD_BASE_TYPE
}
private DataTypeManager sourceDataTypeManager; // may be null
@@ -186,7 +186,7 @@ public class DataTypeParser {
throw new InvalidDataTypeException("fixed-length or string data-type required");
}
break;
- case BITFIELD_USE:
+ case BITFIELD_BASE_TYPE:
if (!BitFieldDataType.isValidBaseDataType(dt)) {
throw new InvalidDataTypeException(
"enum or integer derived data-type required");