Ghidra is a software reverse engineering (SRE) framework
GP-3675 Removed use of aligned-length and forced alignment when creating data on selected regions
ghidra1554f1c87abc4
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/data/CreateDataBackgroundCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/data/CreateDataBackgroundCmd.javaindex bb550afa54..3fc774b768 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/data/CreateDataBackgroundCmd.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/data/CreateDataBackgroundCmd.java@@ -126,38 +126,15 @@ public class CreateDataBackgroundCmd extends BackgroundCommand {return true;}- private static Address alignAddress(Address addr, int alignment) {- if (addr == null) {- return null;- }- long mod = addr.getOffset() % alignment;- if (mod == 0) {- return addr;- }- try {- return addr.addNoWrap(alignment - mod);- }- catch (AddressOverflowException e) {- // ignore- }- return null;- }-private void createData(Address start, Address end, DataType dataType, Program p,TaskMonitor monitor) throws CodeUnitInsertionException {- int alignment = 1;- if (newDataType.getLength() != newDataType.getAlignedLength()) {- // datatypes whose length does not match their aligned-length must- // be properly aligned to account for padding (e.g., x86-32 80-bit floats)- alignment = newDataType.getAlignment();- }+ Address nextAddr = start;+ Listing listing = p.getListing();+ listing.clearCodeUnits(start, end, false);int initialProgress = bytesApplied;- Listing listing = p.getListing();- listing.clearCodeUnits(start, end, false);- Address nextAddr = alignAddress(start, alignment);int length = (int) end.subtract(nextAddr) + 1;while (nextAddr != null && nextAddr.compareTo(end) <= 0) {if (monitor.isCancelled()) {@@ -165,19 +142,22 @@ public class CreateDataBackgroundCmd extends BackgroundCommand {}Data d = listing.createData(nextAddr, dataType, length);- Address maxDataAddr = d.getMaxAddress();- bytesApplied = initialProgress + (int) maxDataAddr.subtract(start) + 1;- nextAddr = alignAddress(maxDataAddr.next(), alignment);- if (nextAddr != null) {- length = (int) end.subtract(nextAddr) + 1;+ int dataLength = d.getLength();+ bytesApplied = initialProgress + dataLength;++ try {+ nextAddr = nextAddr.addNoWrap(dataLength);+ length -= dataLength;+ }+ catch (AddressOverflowException e) {+ return;}monitor.setProgress(bytesApplied);if (++numDataCreated % 10000 == 0) {monitor.setMessage("Created " + numDataCreated);- // Allow the Swing thread a chance to paint components that may require- // a DB lock.+ // Allow the Swing thread a chance to paint components that may require lockSwing.allowSwingToProcessEvents();}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorModel.javaindex 7b08494749..ef54361532 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorModel.java@@ -212,7 +212,7 @@ public abstract class CompositeEditorModel extends CompositeViewerModel implemen}DataType resultDt = DataUtilities.reconcileAppliedDataType(currentDt, dt, true);- int resultLen = resultDt.getAlignedLength();+ int resultLen = resultDt.getLength();if (resultDt instanceof Dynamic) {resultLen = DataTypeHelper.requestDtSize(getProvider(), resultDt.getDisplayName(),@@ -222,7 +222,9 @@ public abstract class CompositeEditorModel extends CompositeViewerModel implementhrow new InvalidDataTypeException("Data types of size 0 are not allowed.");}- return DataTypeInstance.getDataTypeInstance(resultDt, resultLen, true);+ // TODO: Need to handle proper placement for big-endian within a larger component (i.e., right-justified)+ return DataTypeInstance.getDataTypeInstance(resultDt, resultLen,+ viewComposite.isPackingEnabled());}/**diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/DataTypeInstance.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/DataTypeInstance.javaindex 560848301d..c75dca418b 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/DataTypeInstance.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/DataTypeInstance.java@@ -77,6 +77,11 @@ public class DataTypeInstance {/*** Generate a data-type instance* Factory and Dynamic data-types are NOT handled.+ * <br>+ * This container does not dictate the placement of a fixed-length type within this+ * container. It is suggested that big-endian use should evaulate the datatype+ * at the far end of the container.+ ** @param dataType data type* @param buf memory buffer* @param useAlignedLength if true a fixed-length primitive data type will use its@@ -95,6 +100,11 @@ public class DataTypeInstance {/*** Attempt to create a fixed-length data-type instance.* Factory and non-sizable Dynamic data-types are NOT handled.+ * <br>+ * This container does not dictate the placement of a fixed-length type within this+ * container. It is suggested that big-endian use should evaulate the datatype+ * at the far end of the container.+ ** @param dataType data type* @param length length for sizable Dynamic data-types, otherwise ignored* @param useAlignedLength if true a fixed-length primitive data type will use its@@ -128,6 +138,7 @@ public class DataTypeInstance {}}else if (useAlignedLength) {+ // TODO: big-endian should place type at end of this containerlength = dataType.getAlignedLength();}else {@@ -145,10 +156,11 @@ public class DataTypeInstance {* Attempt to create a data-type instance associated with a specific memory location.* Factory and Dynamic data-types are handled.* <br>- * NOTE: fixed-length primitive datatypes assume {@link DataType#getLength() raw datatype length}- * intended for {@link Data} use.+ * This container does not dictate the placement of a fixed-length type within this+ * container. It is suggested that big-endian use should evaulate the datatype+ * at the far end of the container.*- * @param dataType+ * @param dataType the data type* @param buf memory location* @param length length for sizable Dynamic data-types, otherwise ignored* @param useAlignedLength if true a fixed-length primitive data type will use its