Merge branch 'GP-0_ryanmkurtz_omf'

This commit is contained in:
Ryan Kurtz 2024-10-28 15:26:00 -04:00
commit 6d5a5da013
5 changed files with 38 additions and 34 deletions

View file

@ -17,7 +17,9 @@ package ghidra.app.util.bin;
import java.io.IOException; import java.io.IOException;
import ghidra.docking.settings.SettingsDefinition;
import ghidra.program.model.data.*; import ghidra.program.model.data.*;
import ghidra.program.model.listing.Data;
import ghidra.util.exception.DuplicateNameException; import ghidra.util.exception.DuplicateNameException;
/** /**
@ -112,4 +114,26 @@ public interface StructConverter {
* @see ghidra.program.model.data.StructureDataType * @see ghidra.program.model.data.StructureDataType
*/ */
public DataType toDataType() throws DuplicateNameException, IOException; public DataType toDataType() throws DuplicateNameException, IOException;
/**
* Recursively sets the given {@link Data} and its components to big/little endian
*
* @param data The {@link Data}
* @param bigEndian True to set to big endian; false to set to little endian
* @throws Exception if there was a problem setting the endianness
*/
public static void setEndian(Data data, boolean bigEndian) throws Exception {
for (int i = 0; i < data.getNumComponents(); i++) {
Data component = data.getComponent(i);
SettingsDefinition[] settings = component.getDataType().getSettingsDefinitions();
for (int j = 0; j < settings.length; j++) {
if (settings[j] instanceof EndianSettingsDefinition endianSetting) {
endianSetting.setBigEndian(component, bigEndian);
}
}
for (int j = 0; j < component.getNumComponents(); j++) {
setEndian(component.getComponent(j), bigEndian);
}
}
}
} }

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -18,6 +18,7 @@ package ghidra.app.util.bin.format.macho.commands;
import java.io.IOException; import java.io.IOException;
import ghidra.app.util.bin.BinaryReader; import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.StructConverter;
import ghidra.app.util.bin.format.macho.MachHeader; import ghidra.app.util.bin.format.macho.MachHeader;
import ghidra.app.util.bin.format.macho.commands.codesignature.CodeSignatureBlobParser; import ghidra.app.util.bin.format.macho.commands.codesignature.CodeSignatureBlobParser;
import ghidra.app.util.bin.format.macho.commands.codesignature.CodeSignatureGenericBlob; import ghidra.app.util.bin.format.macho.commands.codesignature.CodeSignatureGenericBlob;
@ -64,7 +65,7 @@ public class CodeSignatureCommand extends LinkEditDataCommand {
try { try {
Data d = DataUtilities.createData(program, addr, blob.toDataType(), -1, Data d = DataUtilities.createData(program, addr, blob.toDataType(), -1,
DataUtilities.ClearDataMode.CHECK_FOR_SPACE); DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
LoadCommand.setEndian(d, true); StructConverter.setEndian(d, true);
blob.markup(program, addr, header, monitor, log); blob.markup(program, addr, header, monitor, log);
} }
catch (Exception e) { catch (Exception e) {

View file

@ -22,11 +22,9 @@ import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.StructConverter; import ghidra.app.util.bin.StructConverter;
import ghidra.app.util.bin.format.macho.MachHeader; import ghidra.app.util.bin.format.macho.MachHeader;
import ghidra.app.util.importer.MessageLog; import ghidra.app.util.importer.MessageLog;
import ghidra.docking.settings.SettingsDefinition;
import ghidra.program.flatapi.FlatProgramAPI; import ghidra.program.flatapi.FlatProgramAPI;
import ghidra.program.model.address.Address; import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace; import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.data.EndianSettingsDefinition;
import ghidra.program.model.listing.*; import ghidra.program.model.listing.*;
import ghidra.util.exception.CancelledException; import ghidra.util.exception.CancelledException;
import ghidra.util.task.TaskMonitor; import ghidra.util.task.TaskMonitor;
@ -203,28 +201,6 @@ public abstract class LoadCommand implements StructConverter {
return null; return null;
} }
/**
* Recursively sets the given {@link Data} and its components to big/little endian
*
* @param data The {@link Data}
* @param bigEndian True to set to big endian; false to set to little endian
* @throws Exception if there was a problem setting the endianness
*/
public static void setEndian(Data data, boolean bigEndian) throws Exception {
for (int i = 0; i < data.getNumComponents(); i++) {
Data component = data.getComponent(i);
SettingsDefinition[] settings = component.getDataType().getSettingsDefinitions();
for (int j = 0; j < settings.length; j++) {
if (settings[j] instanceof EndianSettingsDefinition endianSetting) {
endianSetting.setBigEndian(component, true);
}
}
for (int j = 0; j < component.getNumComponents(); j++) {
setEndian(component.getComponent(j), bigEndian);
}
}
}
//-------------------Legacy code to support Raw Binary markup---------------------------------- //-------------------Legacy code to support Raw Binary markup----------------------------------
/** /**
* Marks-up this {@link LoadCommand} with data structures and comments. Assumes the program * Marks-up this {@link LoadCommand} with data structures and comments. Assumes the program

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -20,9 +20,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import ghidra.app.util.bin.BinaryReader; import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.StructConverter;
import ghidra.app.util.bin.format.macho.MachConstants; import ghidra.app.util.bin.format.macho.MachConstants;
import ghidra.app.util.bin.format.macho.MachHeader; import ghidra.app.util.bin.format.macho.MachHeader;
import ghidra.app.util.bin.format.macho.commands.LoadCommand;
import ghidra.app.util.importer.MessageLog; import ghidra.app.util.importer.MessageLog;
import ghidra.program.model.address.Address; import ghidra.program.model.address.Address;
import ghidra.program.model.data.*; import ghidra.program.model.data.*;
@ -97,7 +97,7 @@ public class CodeSignatureSuperBlob extends CodeSignatureGenericBlob {
CodeSignatureGenericBlob blob = indexBlobs.get(i); CodeSignatureGenericBlob blob = indexBlobs.get(i);
Data d = DataUtilities.createData(program, addr.add(blobIndex.getOffset()), Data d = DataUtilities.createData(program, addr.add(blobIndex.getOffset()),
blob.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE); blob.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
LoadCommand.setEndian(d, true); StructConverter.setEndian(d, true);
blob.markup(program, addr.add(blobIndex.getOffset()), header, monitor, log); blob.markup(program, addr.add(blobIndex.getOffset()), header, monitor, log);
} }
} }

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -21,6 +21,7 @@ import java.util.*;
import ghidra.app.util.MemoryBlockUtils; import ghidra.app.util.MemoryBlockUtils;
import ghidra.app.util.Option; import ghidra.app.util.Option;
import ghidra.app.util.bin.ByteProvider; import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.bin.StructConverter;
import ghidra.app.util.bin.format.omf.*; import ghidra.app.util.bin.format.omf.*;
import ghidra.app.util.bin.format.omf.omf51.Omf51RecordFactory; import ghidra.app.util.bin.format.omf.omf51.Omf51RecordFactory;
import ghidra.app.util.importer.MessageLog; import ghidra.app.util.importer.MessageLog;
@ -28,6 +29,7 @@ import ghidra.program.database.mem.FileBytes;
import ghidra.program.model.address.Address; import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace; import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.data.DataUtilities; import ghidra.program.model.data.DataUtilities;
import ghidra.program.model.listing.Data;
import ghidra.program.model.listing.Program; import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.MemoryBlock; import ghidra.program.model.mem.MemoryBlock;
import ghidra.util.exception.CancelledException; import ghidra.util.exception.CancelledException;
@ -95,8 +97,9 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
Address start = headerBlock.getStart(); Address start = headerBlock.getStart();
for (OmfRecord record : records) { for (OmfRecord record : records) {
DataUtilities.createData(program, start.add(record.getRecordOffset()), Data d = DataUtilities.createData(program, start.add(record.getRecordOffset()),
record.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE); record.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
StructConverter.setEndian(d, false);
} }
} }
catch (Exception e) { catch (Exception e) {