The following script shows hows to create a structure and set its field to Big Endian byte order using the Ghidra Python API.
from ghidra.program.model.data import DataTypeConflictHandler
from ghidra.program.model.data import EndianSettingsDefinition
from ghidra.app.util.cparser.C import CParser
mystruct_txt = """
struct mystruct{
uint32_t field1;
uint32_t field2;
};"""
# Get Data Type Manager
data_type_manager = currentProgram.getDataTypeManager()
# Create CParser
parser = CParser(data_type_manager)
# Parse structure
parsed_datatype = parser.parse(mystruct_txt)
# Add parsed type to data type manager
datatype = data_type_manager.addDataType(parsed_datatype, DataTypeConflictHandler.DEFAULT_HANDLER)
# Extract the first structure member i.e. mystruct.field1
field1 = datatype.components[0]
# Get Default Settings
field1_settings = field1.getDefaultSettings()
# Set endianess to big
field1_settings.setLong('endian', EndianSettingsDefinition.BIG)
If you have already created the structure earlier (using the editor or otherwise) you can omit the part where the structure is created and use getDataType
to obtain it from the DataTypeManagerDB
as shown below.
datatype = data_type_manager.getDataType("/mystruct")
field1 = datatype.components[0]
field1_settings = field1.getDefaultSettings()
field1_settings.setLong('endian', EndianSettingsDefinition.BIG)
After applying the structure to a piece data, you can right click on the field -> Data -> Default Settings and check that the default endianness is indeed Big Endian.