Bitfields - added simple bitfield viewer and corrected missing support

for bitfields and flex arrays
This commit is contained in:
ghidra1 2019-06-05 18:26:57 -04:00
parent 52f6bfc127
commit 31163bca26
26 changed files with 747 additions and 233 deletions

View file

@ -2151,7 +2151,7 @@ public class CodeManager implements ErrorHandler, ManagerDB {
}
private void createReference(Data data, Address toAddr, List<Address> longSegmentAddressList) {
if (toAddr == null) {
if (toAddr == null || !toAddr.isLoadedMemoryAddress()) {
return;
}

View file

@ -421,6 +421,12 @@ abstract public class DataTypeManagerDB implements DataTypeManager {
for (DataTypeComponent comp : comps) {
comp.getDataType().addParent(dt);
}
if (dt instanceof Structure) {
Structure struct = (Structure) dt;
if (struct.hasFlexibleArrayComponent()) {
struct.getFlexibleArrayComponent().getDataType().addParent(dt);
}
}
}
else if (dt instanceof FunctionDefinition) {
FunctionDefinition funDef = (FunctionDefinition) dt;

View file

@ -649,6 +649,7 @@ class StructureDB extends CompositeDB implements Structure {
return 1; // Unaligned
}
if (alignment <= 0) {
// just in case - alignment should have been previously determined and stored
StructurePackResult packResult = AlignedStructureInspector.packComponents(this);
alignment = packResult.alignment;
}

View file

@ -23,6 +23,11 @@ import javax.help.UnsupportedOperationException;
import ghidra.docking.settings.Settings;
import ghidra.util.exception.DuplicateNameException;
/**
* <code>AlignedStructureInspector</code> provides a simple instance of a structure
* member container used to perform alignment operations without forcing modification
* of the actual structure.
*/
public class AlignedStructureInspector extends AlignedStructurePacker {
private AlignedStructureInspector(Structure structure) {
@ -34,6 +39,9 @@ public class AlignedStructureInspector extends AlignedStructurePacker {
for (DataTypeComponent c : structure.getComponents()) {
list.add(new ReadOnlyComponentWrapper(c));
}
if (structure.hasFlexibleArrayComponent()) {
list.add(new ReadOnlyComponentWrapper(structure.getFlexibleArrayComponent()));
}
return list;
}
@ -55,10 +63,10 @@ public class AlignedStructureInspector extends AlignedStructurePacker {
}
@Override
public void update(int ordinal, int offset, int length) {
this.ordinal = ordinal;
this.offset = offset;
this.length = length;
public void update(int ord, int off, int len) {
this.ordinal = ord;
this.offset = off;
this.length = len;
}
@Override
@ -73,7 +81,7 @@ public class AlignedStructureInspector extends AlignedStructurePacker {
@Override
public boolean isFlexibleArrayComponent() {
return false;
return component.isFlexibleArrayComponent();
}
@Override

View file

@ -340,7 +340,18 @@ public class BitFieldDataType extends AbstractDataType {
@Override
public String getDescription() {
return getName() + " BitField";
StringBuffer sbuf = new StringBuffer();
sbuf.append(Integer.toString(effectiveBitSize));
sbuf.append("-bit ");
DataType dt = getBaseDataType();
sbuf.append(dt.getDisplayName());
sbuf.append(" bitfield");
if (effectiveBitSize != bitSize) {
sbuf.append(" (declared as ");
sbuf.append(Integer.toString(bitSize));
sbuf.append("-bits)");
}
return sbuf.toString();
}
@Override

View file

@ -242,7 +242,7 @@ public class DataTypeWriter {
}
Msg.error(this, "Factory data types may not be written - type: " + dt, iae);
}
if (dt instanceof Pointer || dt instanceof Array) {
if (dt instanceof Pointer || dt instanceof Array || dt instanceof BitFieldDataType) {
write(getBaseDataType(dt), monitor);
return;
}
@ -308,6 +308,9 @@ public class DataTypeWriter {
else if (dt instanceof BuiltInDataType) {
writeBuiltIn((BuiltInDataType) dt, monitor);
}
else if (dt instanceof BitFieldDataType) {
// skip
}
else {
writer.write(EOL);
writer.write(EOL);
@ -540,7 +543,12 @@ public class DataTypeWriter {
if (componentString == null) {
if (dataType instanceof Array) {
if (dataType instanceof BitFieldDataType) {
BitFieldDataType bfDt = (BitFieldDataType) dataType;
name += ":" + bfDt.getDeclaredBitSize();
dataType = bfDt.getBaseDataType();
}
else if (dataType instanceof Array) {
Array array = (Array) dataType;
name += getArrayDimensions(array);
dataType = getArrayBaseType(array);
@ -637,6 +645,7 @@ public class DataTypeWriter {
return;
}
}
// TODO: A comment explaining the special 'P' case would be helpful!! Smells like fish.
else if (baseType instanceof Pointer && typedefName.startsWith("P")) {
DataType dt = ((Pointer) baseType).getDataType();
if (dt instanceof TypeDef) {
@ -765,6 +774,10 @@ public class DataTypeWriter {
Pointer pointer = (Pointer) dt;
dt = pointer.getDataType();
}
else if (dt instanceof BitFieldDataType) {
BitFieldDataType bitfieldDt = (BitFieldDataType) dt;
dt = bitfieldDt.getBaseDataType();
}
else {
break;
}

View file

@ -25,9 +25,9 @@ public interface InternalDataTypeComponent extends DataTypeComponent {
/**
* Update component ordinal, offset and length during alignment
* @param ordinal
* @param offset
* @param length
* @param ordinal updated ordinal
* @param offset updated offset
* @param length updated byte length
*/
void update(int ordinal, int offset, int length);