Skip to content

Ghidra is a software reverse engineering (SRE) framework

BitFields - resolved some editor layout and action enablement issues

ghidra191de56968018
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/AddBitFieldAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/AddBitFieldAction.java
index 9ca932a613..c374a7a1b0 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/AddBitFieldAction.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/AddBitFieldAction.java
@@ -23,7 +23,6 @@ import javax.swing.SwingUtilities;
import docking.ActionContext;
import docking.DockingWindowManager;
-import ghidra.program.model.data.*;
import ghidra.util.exception.AssertException;
/**
@@ -56,32 +55,12 @@ public class AddBitFieldAction extends CompositeEditorTableAction {
}
int rowIndex = model.getSelectedRows()[0];
- if (editorModel.isAligned()) {
- // Insert before selected component
- // ordinal based, user input needed:
- // 1. bitfield base datatype
- // 2. bitfield size
- // 3. bitfield name (can be renamed later)
- int ordinal = -1;
- DataType baseDataType = null;
- if (!editorModel.isAtEnd(rowIndex)) {
- DataTypeComponent component = editorModel.getComponent(rowIndex);
- ordinal = component.getOrdinal();
- if (component.isBitFieldComponent()) {
- BitFieldDataType currentBitfield = (BitFieldDataType) component.getDataType();
- baseDataType = currentBitfield.getBaseDataType();
- }
- }
- insertBitField(ordinal, baseDataType);
- }
- else {
- BitFieldEditorDialog dlg =
- new BitFieldEditorDialog(editorModel.viewComposite, provider.dtmService,
- -(rowIndex + 1), ordinal -> refreshTableAndSelection(editorModel, ordinal));
- Component c = provider.getComponent();
- Window w = SwingUtilities.windowForComponent(c);
- DockingWindowManager.showDialog(w, dlg, c);
- }
+ BitFieldEditorDialog dlg =
+ new BitFieldEditorDialog(editorModel.viewComposite, provider.dtmService,
+ -(rowIndex + 1), ordinal -> refreshTableAndSelection(editorModel, ordinal));
+ Component c = provider.getComponent();
+ Window w = SwingUtilities.windowForComponent(c);
+ DockingWindowManager.showDialog(w, dlg, c);
requestTableFocus();
}
@@ -93,17 +72,12 @@ public class AddBitFieldAction extends CompositeEditorTableAction {
editorTable.getSelectionModel().setSelectionInterval(ordinal, ordinal);
}
- private void insertBitField(int ordinal, DataType baseDataType) {
- // TODO Auto-generated method stub
-
- }
-
@Override
public void adjustEnablement() {
boolean enabled = true;
CompEditorModel editorModel = (CompEditorModel) model;
- if (editorModel.viewComposite == null || editorModel.getNumSelectedRows() != 1 ||
- editorModel.isFlexibleArraySelection()) {
+ if (editorModel.viewComposite == null || editorModel.isAligned() ||
+ editorModel.getNumSelectedRows() != 1 || editorModel.isFlexibleArraySelection()) {
enabled = false;
}
setEnabled(enabled);
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 acb4dbf196..54ad82e116 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
@@ -19,6 +19,7 @@ import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
+import javax.swing.border.EmptyBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
@@ -87,17 +88,22 @@ public class BitFieldEditorPanel extends JPanel {
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
- createPlacementPanel();
-
if (composite instanceof Structure) {
add(createAllocationOffsetPanel());
}
- add(placementComponent);
+ add(createPlacementPanel());
+ add(createLegendPanel());
add(createEntryPanel());
enableControls(false);
}
+ private JPanel createLegendPanel() {
+ JPanel legendPanel = new JPanel(new BorderLayout());
+ legendPanel.add(new BitFieldPlacementComponent.BitFieldLegend(), BorderLayout.WEST);
+ return legendPanel;
+ }
+
private JPanel createAllocationOffsetPanel() {
JPanel panel = new JPanel(new HorizontalLayout(5));
@@ -320,10 +326,14 @@ public class BitFieldEditorPanel extends JPanel {
placementComponent.addMouseListener(bitSelectionHandler);
placementComponent.addMouseMotionListener(bitSelectionHandler);
- JScrollPane scrollPane =
- new JScrollPane(placementComponent, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
- ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+ JPanel p = new JPanel(new BorderLayout());
+ p.add(placementComponent, BorderLayout.WEST);
+ p.setBorder(new EmptyBorder(0, 0, 5, 0));
+
+ JScrollPane scrollPane = new JScrollPane(p, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
+ ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.getViewport().setBackground(getBackground());
+ scrollPane.setBorder(null);
midPanel.add(scrollPane);
return midPanel;
@@ -448,12 +458,12 @@ public class BitFieldEditorPanel extends JPanel {
boolean endCurrentEdit() {
if (placementComponent.isEditing()) {
- String currentOp = placementComponent.isAdding() ? "add" : "edit";
- int option = OptionDialog.showYesNoDialog(this, "Confirm Edit Action",
- "Cancel current bitfield " + currentOp + " operation?");
- if (option != OptionDialog.YES_OPTION) {
- return false;
- }
+// String currentOp = placementComponent.isAdding() ? "add" : "edit";
+// int option = OptionDialog.showYesNoDialog(this, "Confirm Edit Action",
+// "Cancel current bitfield " + currentOp + " operation?");
+// if (option != OptionDialog.YES_OPTION) {
+// return false;
+// }
placementComponent.cancelEdit();
enableControls(false);
}
@@ -490,14 +500,18 @@ public class BitFieldEditorPanel extends JPanel {
}
private void enableControls(boolean enable) {
+ dtChoiceEditor.getBrowseButton().setEnabled(enable);
+ dtChoiceEditor.getDropDownTextField().setEnabled(enable);
+ fieldNameTextField.setEnabled(enable);
allocSizeInput.setEnabled(enable);
bitSizeInput.setEnabled(enable);
bitOffsetInput.setEnabled(enable);
if (!enable) {
// TODO: set placementComponent mode to NONE
+ dtChoiceEditor.getDropDownTextField().setText("");
+ fieldNameTextField.setText(null);
bitOffsetModel.setValue(0L);
bitSizeModel.setValue(1L);
- fieldNameTextField.setText(null);
}
}
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 8a1f79f220..aa56aa5e67 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
@@ -20,13 +20,13 @@ import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.HashSet;
-import javax.swing.JPanel;
-import javax.swing.ToolTipManager;
+import javax.swing.*;
import ghidra.program.model.data.*;
import ghidra.program.model.data.Composite;
import ghidra.util.Msg;
import ghidra.util.exception.AssertException;
+import resources.icons.ColorIcon;
public class BitFieldPlacementComponent extends JPanel {
@@ -39,14 +39,16 @@ public class BitFieldPlacementComponent extends JPanel {
private static final int SCROLLBAR_THICKNESS = 10;
private static final int MY_HEIGHT = (2 * CELL_HEIGHT) + (3 * BYTE_SEPARATOR_THICKNESS);
+ private static final int LENEND_BOX_SIZE = 16;
+
private static final Color TEXT_COLOR = Color.black;
private static final Color LINE_COLOR = Color.black;
private static final Color BYTE_HEADER_COLOR = new Color(0xdfdfdf);
- private static final Color UNDEFINED_BIT_COLOR = new Color(0xe8e8e8);
+ private static final Color UNDEFINED_BIT_COLOR = new Color(0xf8f8f8);
private static final Color BITFIELD_BITS_COLOR = Color.green;
private static final Color CONFLICT_BITS_COLOR = Color.yellow;
private static final Color BITFIELD_COMPONENT_COLOR = new Color(0xcfcfff);
- private static final Color NON_BITFIELD_COMPONENT_COLOR = new Color(0xafafff);
+ private static final Color NON_BITFIELD_COMPONENT_COLOR = new Color(0xafafaf);
private static final Color INTERIOR_LINE_COLOR = new Color(0xbfbfbf);
private final Composite composite;
@@ -58,6 +60,30 @@ public class BitFieldPlacementComponent extends JPanel {
private EditMode editMode = EditMode.NONE;
private int editOrdinal = -1; // FIXME: improve insert use
+ public static class BitFieldLegend extends JPanel {
+
+ BitFieldLegend() {
+ setLayout(new GridLayout(2, 3, 5, 5));
+ //setLayout(new RowColumnLayout(10, 10, RowColumnLayout.ROW, 0));
+ add(new JLabel("Undefined bits",
+ new ColorIcon(UNDEFINED_BIT_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
+ SwingConstants.LEFT));
+ add(new JLabel("Defined bitfield",
+ new ColorIcon(BITFIELD_COMPONENT_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
+ SwingConstants.LEFT));
+ add(new JLabel("Defined non-bitfield",
+ new ColorIcon(NON_BITFIELD_COMPONENT_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
+ SwingConstants.LEFT));
+ add(new JLabel("Edit bitfield bits",
+ new ColorIcon(BITFIELD_BITS_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
+ SwingConstants.LEFT));
+ add(new JLabel("Conflict bits",
+ new ColorIcon(CONFLICT_BITS_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
+ SwingConstants.LEFT));
+ }
+
+ }
+
BitFieldPlacementComponent(Composite composite) {
this.composite = composite;
bigEndian = composite.getDataOrganization().isBigEndian();
diff --git a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/bitfields.h b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/bitfields.h
index 5b4acfa710..dd1d430d0c 100644
--- a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/bitfields.h
+++ b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/bitfields.h
@@ -20,7 +20,15 @@ struct B1 {
unsigned short b:6; // gcc groups with previous non-bitfield
int c:8; // gcc groups with previous two fields (including non-bitfield)
short d:4;
-};
+};
+
+struct B1flex {
+ char a;
+ unsigned short b:6; // gcc groups with previous non-bitfield
+ int c:8; // gcc groups with previous two fields (including non-bitfield)
+ short d:4;
+ long flex[];
+};
struct B2 {
char a;
diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/util/layout/RowColumnLayout.java b/Ghidra/Framework/Generic/src/main/java/ghidra/util/layout/RowColumnLayout.java
index 87ec0cb69c..b2e225f90f 100644
--- a/Ghidra/Framework/Generic/src/main/java/ghidra/util/layout/RowColumnLayout.java
+++ b/Ghidra/Framework/Generic/src/main/java/ghidra/util/layout/RowColumnLayout.java
@@ -86,7 +86,7 @@ public class RowColumnLayout implements LayoutManager {
numCols = (n + numRows - 1) / numRows;
}
else {
- int height = Math.max(maxSize - insets.top - insets.left, compHeight);
+ int height = Math.max(maxSize - insets.top - insets.bottom, compHeight);
numRows = (height + vgap) / (compHeight + vgap);
numCols = (n + numRows - 1) / numRows;
numRows = (n + numCols - 1) / numCols;