Java Code Examples for ghidra.program.model.listing.Program#getDataTypeManager()
The following examples show how to use
ghidra.program.model.listing.Program#getDataTypeManager() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MSDataTypeUtils.java From ghidra with Apache License 2.0 | 6 votes |
/** * Gets the named data type from the program or the windows data type archive. If neither * the program or data type archive has an equivalent data type then the original data type * is returned. * @param program the program for the data type. * @param comparisonDt the data type it should match * @return the matching data type */ public static DataType getMatchingDataType(Program program, DataType comparisonDt) { DataTypeManager programDTM = program.getDataTypeManager(); DataType matchingDt = findMatchingDataType(comparisonDt, programDTM); if (matchingDt == null) { try { DataTypeManager winDTM = getWinDTM(program); if (winDTM != null) { matchingDt = findMatchingDataType(comparisonDt, winDTM); } } catch (IOException | DuplicateIdException e) { // Can't get data type archive so just do nothing. } } return (matchingDt != null) ? matchingDt : comparisonDt; }
Example 2
Source File: TypeDescriptorModel.java From ghidra with Apache License 2.0 | 6 votes |
/** * Gets the actual name string for this type descriptor. * @return the type name or null. * @throws InvalidDataTypeException if valid TypeDescriptor data can't be created at the * model's address. */ private String doGetTypeName() throws InvalidDataTypeException { // last component is the type descriptor name. Address nameAddress = getComponentAddressOfTypeName(); // Could be null. if (nameAddress == null) { return null; } Program program = getProgram(); TerminatedStringDataType terminatedStringDt = new TerminatedStringDataType(program.getDataTypeManager()); DumbMemBufferImpl nameMemBuffer = new DumbMemBufferImpl(getMemBuffer().getMemory(), nameAddress); Object value = terminatedStringDt.getValue(nameMemBuffer, SettingsImpl.NO_SETTINGS, 1); if (value instanceof String) { originalTypeName = (String) value; if (originalTypeName != null) { mdComplexType = getMDComplexType(program, originalTypeName); // Can be null. } } hasProcessedName = true; return originalTypeName; }
Example 3
Source File: RTTI2DataType.java From ghidra with Apache License 2.0 | 6 votes |
@Override protected DataTypeComponent[] getAllComponents(MemBuffer buf) { ArrayList<DataTypeComponent> list = new ArrayList<>(); Memory memory = buf.getMemory(); Address addr = buf.getAddress(); Program program = memory.getProgram(); DataTypeManager dtm = program.getDataTypeManager(); DataType rtti1Dt = new RTTI1DataType(dtm); boolean is64Bit = is64Bit(program); DataType rtti1RefDt = getReferenceDataType(program, rtti1Dt); for (int ordinal = 0; addr != null && validRefData(memory, addr); ordinal++) { DataTypeComponent comp = new ReadOnlyDataTypeComponent(rtti1RefDt, this, 4, ordinal, ordinal * 4, null, is64Bit ? "rtti1Displacement" : "rtti1Pointer"); list.add(comp); addr = addr.add(4); // Add the data type size. } return list.toArray(new DataTypeComponent[list.size()]); }
Example 4
Source File: Rtti2Model.java From ghidra with Apache License 2.0 | 6 votes |
private DataType getDataType(Program program, DataType rtti1Dt) { setIsDataTypeAlreadyBasedOnCount(true); DataTypeManager dataTypeManager = program.getDataTypeManager(); int numElements = getCount(); // Each entry is a 4 byte value. if (numElements == 0) { numElements = getNumEntries(program, getAddress()); } if (numElements <= 0) { return null; // invalid for rtti 2. } DataType individualEntryDataType = getIndividualEntryDataType(program, rtti1Dt); ArrayDataType array = new ArrayDataType(individualEntryDataType, numElements, rtti1Dt.getLength(), dataTypeManager); return MSDataTypeUtils.getMatchingDataType(program, array); }
Example 5
Source File: Rtti3Model.java From ghidra with Apache License 2.0 | 6 votes |
/** * This gets the ClassHierarchyDescriptor (RTTI 3) structure for the indicated program. * @param program the program which will contain this data type. * @return the ClassHierarchyDescriptor (RTTI 3) structure. */ static DataType getSimpleDataType(Program program) { DataTypeManager dataTypeManager = program.getDataTypeManager(); boolean is64Bit = MSDataTypeUtils.is64Bit(program); CategoryPath categoryPath = new CategoryPath(CATEGORY_PATH); StructureDataType struct = getAlignedPack4Structure(dataTypeManager, categoryPath, STRUCTURE_NAME); // Add the components. DWordDataType dWordDataType = new DWordDataType(dataTypeManager); struct.add(dWordDataType, "signature", null); struct.add(dWordDataType, "attributes", "bit flags"); struct.add(dWordDataType, "numBaseClasses", "number of base classes (i.e. rtti1Count)"); DataType rtti2Dt = Rtti2Model.getSimpleIndividualEntryDataType(program); DataType rtti2RefDt = is64Bit ? new ImageBaseOffset32DataType(dataTypeManager) : new PointerDataType(rtti2Dt); struct.add(rtti2RefDt, "pBaseClassArray", "ref to BaseClassArray (RTTI 2)"); return new TypedefDataType(categoryPath, DATA_TYPE_NAME, struct, dataTypeManager); }
Example 6
Source File: PcodeDataTypeManager.java From ghidra with Apache License 2.0 | 5 votes |
public PcodeDataTypeManager(Program prog) { program = prog; progDataTypes = prog.getDataTypeManager(); dataOrganization = progDataTypes.getDataOrganization(); voidInputIsVarargs = true; // By default, do not lock-in void parameter lists displayLanguage = prog.getCompilerSpec().getDecompilerOutputLanguage(prog); if (displayLanguage != DecompilerLanguage.C_LANGUAGE) { voidInputIsVarargs = false; } generateCoreTypes(); sortCoreTypes(); pointerWordSize = ((SleighLanguage) prog.getLanguage()).getDefaultPointerWordSize(); }
Example 7
Source File: EHUnwindModel.java From ghidra with Apache License 2.0 | 5 votes |
/** * This gets the UnwindMapEntry structure for the indicated program. * @param program the program which will contain this data type. * @return the UnwindMapEntry structure. */ public static DataType getDataType(Program program) { DataTypeManager dataTypeManager = program.getDataTypeManager(); boolean isRelative = isRelative(program); CategoryPath categoryPath = new CategoryPath(CATEGORY_PATH); StructureDataType struct = getAlignedPack4Structure(dataTypeManager, categoryPath, STRUCTURE_NAME); // Add the components. DataType compDt; /* comps[0] */ DataType ehStateDt = MSDataTypeUtils.getEHStateDataType(program); if (ehStateDt == null) { ehStateDt = new IntegerDataType(dataTypeManager); } struct.add(ehStateDt, "toState", null); /* comps[1] */ if (isRelative) { compDt = new ImageBaseOffset32DataType(dataTypeManager); } else { FunctionDefinitionDataType functionDefDt = new FunctionDefinitionDataType( new CategoryPath("/ehdata.h/functions"), "action", dataTypeManager); functionDefDt.setReturnType(new VoidDataType(dataTypeManager)); compDt = new PointerDataType(functionDefDt, dataTypeManager); } struct.add(compDt, "action", null); TypedefDataType typedefDt = new TypedefDataType(categoryPath, DATA_TYPE_NAME, struct, dataTypeManager); return MSDataTypeUtils.getMatchingDataType(program, typedefDt); }
Example 8
Source File: RTTI1DataType.java From ghidra with Apache License 2.0 | 5 votes |
@Override protected DataTypeComponent[] getAllComponents(MemBuffer buf) { Program program = buf.getMemory().getProgram(); DataTypeManager dtm = program.getDataTypeManager(); DataType rtti0Dt = new RTTI0DataType(dtm); DataType rtti0RefDt = getReferenceDataType(program, rtti0Dt); DataType rtti3Dt = new RTTI3DataType(dtm); DataType rtti3RefDt = getReferenceDataType(program, rtti3Dt); DataTypeComponent[] comps = new DataTypeComponent[5]; comps[0] = new ReadOnlyDataTypeComponent(rtti0RefDt, this, 4, 0, RTTI_0_OFFSET, "pTypeDescriptor", is64Bit(program) ? "rtti0Displacement" : "rtti0Pointer"); comps[1] = new ReadOnlyDataTypeComponent(new DWordDataType(), this, 4, 1, NUM_CONTAINED_BASES_OFFSET, "numContainedBases", "number of direct bases of this base class"); // Commented the following out and replaced it with a PMD structure data type. // Leaving it in until we are certain we want the actual PMD structure in this dynamic data type. // comps[2] = new ReadOnlyDataTypeComponent(new DWordDataType(), this, 4, 2, PMD_MDISP_OFFSET, // "PMD.mdisp", "vftable offset"); // comps[3] = new ReadOnlyDataTypeComponent(new DWordDataType(), this, 4, 3, PMD_PDISP_OFFSET, // "PMD.pdisp", // "vbtable offset (-1: vftable is at displacement PMD.mdisp inside the class)"); // comps[4] = new ReadOnlyDataTypeComponent(new DWordDataType(), this, 4, 4, PMD_VDISP_OFFSET, // "PMD.vdisp", "Displacement of the base class vftable pointer inside the vbtable"); Structure pmdDataType = MSDataTypeUtils.getPMDDataType(program); comps[2] = new ReadOnlyDataTypeComponent(pmdDataType, this, PMD_LENGTH, 2, PMD_OFFSET, "where", ""); comps[3] = new ReadOnlyDataTypeComponent(new DWordDataType(), this, 4, 3, ATTRIBUTES_OFFSET, "attributes", null); comps[4] = new ReadOnlyDataTypeComponent(rtti3RefDt, this, 4, 4, RTTI_3_OFFSET, "pClassHierarchyDescriptor", is64Bit(program) ? "rtti3Displacement" : "rtti3Pointer"); return comps; }
Example 9
Source File: Rtti2Model.java From ghidra with Apache License 2.0 | 5 votes |
/** * This gets the data type for an individual entry in the array of RTTI 1 references * produced by this model. * @param program the program which will contain this data type. * @param rtti1Dt the RTTI 1 data type associated with this RTTI 2. * @return the data type for an individual array entry. */ public static DataType getIndividualEntryDataType(Program program, DataType rtti1Dt) { DataTypeManager dataTypeManager = program.getDataTypeManager(); if (MSDataTypeUtils.is64Bit(program)) { return new ImageBaseOffset32DataType(dataTypeManager); } return new PointerDataType(rtti1Dt, dataTypeManager); }
Example 10
Source File: Rtti3Model.java From ghidra with Apache License 2.0 | 5 votes |
/** * Make the indicated RTTI 3 refer to the indicated RTTI 1 through the RTTI 2 reference. * @param rtti3Dt the RTTI 3 data type * @param program the program that contains the data types * @param rtti1Dt the RTTI 1 data type */ static void setRtti1DataType(DataType rtti3Dt, Program program, DataType rtti1Dt) { DataTypeManager dataTypeManager = program.getDataTypeManager(); boolean is64Bit = MSDataTypeUtils.is64Bit(program); Structure rtti3Struct = (Structure) DataTypeUtils.getBaseDataType(rtti3Dt); DataType individualRtti2EntryDt = Rtti2Model.getIndividualEntryDataType(program, rtti1Dt); DataType rtti2RefDt = is64Bit ? new ImageBaseOffset32DataType(dataTypeManager) : new PointerDataType(individualRtti2EntryDt); rtti3Struct.replace(BASE_ARRAY_PTR_ORDINAL, rtti2RefDt, rtti2RefDt.getLength(), "pBaseClassArray", "ref to BaseClassArray (RTTI 2)"); }
Example 11
Source File: EHESTypeListModel.java From ghidra with Apache License 2.0 | 5 votes |
/** * This gets the ESTypeList structure for the indicated program. * @param program the program which will contain this data type. * @return the ESTypeList structure. */ public static DataType getDataType(Program program) { DataTypeManager dataTypeManager = program.getDataTypeManager(); boolean isRelative = isRelative(program); CategoryPath categoryPath = new CategoryPath(CATEGORY_PATH); StructureDataType struct = getAlignedPack4Structure(dataTypeManager, categoryPath, STRUCTURE_NAME); // Add the components. DataType compDt; /* comps[0] */ compDt = new IntegerDataType(dataTypeManager); struct.add(compDt, "nCount", null); /* comps[1] */ if (isRelative) { compDt = new ImageBaseOffset32DataType(dataTypeManager); struct.add(compDt, "dispTypeArray", null); } else { compDt = new PointerDataType(EHCatchHandlerModel.getDataType(program), dataTypeManager); struct.add(compDt, "pTypeArray", null); } TypedefDataType typeDefDt = new TypedefDataType(categoryPath, DATA_TYPE_NAME, struct, dataTypeManager); return MSDataTypeUtils.getMatchingDataType(program, typeDefDt); }
Example 12
Source File: SetEquateDialog.java From ghidra with Apache License 2.0 | 5 votes |
/** * Constructor * * @param tool the EquatePlugin that launched this dialog(used to validate input) * @param program the program the equate is located in. * @param value the equate value to set. */ public SetEquateDialog(PluginTool tool, Program program, Scalar value) { super("Set Equate", true, true, true, false); this.tool = tool; this.program = program; this.scalar = value; this.dataTypeManager = program.getDataTypeManager(); this.equateTable = program.getEquateTable(); addWorkPanel(buildMainPanel()); addOKButton(); addCancelButton(); setRememberSize(false); }
Example 13
Source File: EHCatchHandlerModel.java From ghidra with Apache License 2.0 | 4 votes |
/** * This gets the HandlerType structure for the indicated program. * @param program the program which will contain this data type. * @return the HandlerType structure. */ public static DataType getDataType(Program program) { DataTypeManager dataTypeManager = program.getDataTypeManager(); boolean isRelative = isRelative(program); CategoryPath categoryPath = new CategoryPath(CATEGORY_PATH); StructureDataType struct = getAlignedPack4Structure(dataTypeManager, categoryPath, STRUCTURE_NAME); // Add the components. DataType compDt; /* comps[0] */ compDt = new UnsignedIntegerDataType(dataTypeManager); struct.add(compDt, "adjectives", null); /* comps[1] */ if (isRelative) { compDt = new ImageBaseOffset32DataType(dataTypeManager); struct.add(compDt, "dispType", null); } else { compDt = new PointerDataType(TypeDescriptorModel.getDataType(program), dataTypeManager); struct.add(compDt, "pType", null); } /* comps[2] */ if (isRelative) { compDt = new IntegerDataType(dataTypeManager); } else { compDt = new TypedefDataType(new CategoryPath("/crtdefs.h"), "ptrdiff_t", new IntegerDataType(dataTypeManager), dataTypeManager); } struct.add(compDt, "dispCatchObj", null); /* comps[3] */ if (isRelative) { compDt = new ImageBaseOffset32DataType(dataTypeManager); struct.add(compDt, "dispOfHandler", null); } else { compDt = new PointerDataType(new VoidDataType(dataTypeManager), dataTypeManager); struct.add(compDt, "addressOfHandler", null); } // Only some 64 bit programs have this displacement of the address of the function frame. /* comps[4] */ if (isRelative) { // Needs more checking here still. Incorrectly puts it in all 64 bit programs. compDt = new DWordDataType(dataTypeManager); struct.add(compDt, "dispFrame", null); } TypedefDataType typeDefDt = new TypedefDataType(categoryPath, DATA_TYPE_NAME, struct, dataTypeManager); return MSDataTypeUtils.getMatchingDataType(program, typeDefDt); }
Example 14
Source File: RTTI0DataType.java From ghidra with Apache License 2.0 | 4 votes |
@Override protected DataTypeComponent[] getAllComponents(MemBuffer buf) { Program program = buf.getMemory().getProgram(); Pointer pointer = new PointerDataType(program.getDataTypeManager()); DataTypeComponent[] comps = new DataTypeComponent[3]; comps[0] = new ReadOnlyDataTypeComponent(pointer, this, pointer.getLength(), 0, VF_TABLE_POINTER_OFFSET, "pVFTable", "vfTablePointer"); int dataPointerOffset = getSpareDataOffset(program); comps[1] = new ReadOnlyDataTypeComponent(pointer, this, pointer.getLength(), 1, dataPointerOffset, "spare", "pointer to spare data"); Address start = buf.getAddress(); int nameOffset = getNameOffset(program); Address nameAddress = start.add(nameOffset); MemoryBufferImpl nameBuf = new MemoryBufferImpl(buf.getMemory(), nameAddress, 1024); DataTypeInstance dti = DataTypeInstance.getDataTypeInstance(new TerminatedStringDataType(), nameBuf); if (dti != null) { comps[2] = new ReadOnlyDataTypeComponent(dti.getDataType(), this, dti.getLength(), 2, nameOffset, "name", null); } else { comps[2] = new ReadOnlyDataTypeComponent(DataType.DEFAULT, this, 1, 2, nameOffset, "name", null); } int length = nameOffset + dti.getLength(); int pointerSize = is64Bit(program) ? 8 : 4; int mod = length % pointerSize; if (mod == 0) { return comps; } int offset = length; int bytesNeeded = (mod != 0) ? (pointerSize - mod) : 0; DataType dt = new ArrayDataType(new ByteDataType(), bytesNeeded, 1); DataTypeComponent[] alignedComps = new DataTypeComponent[4]; System.arraycopy(comps, 0, alignedComps, 0, 3); alignedComps[3] = new ReadOnlyDataTypeComponent(dt, this, bytesNeeded, 3, offset, "alignmentBytes", null); return alignedComps; }
Example 15
Source File: VfTableModel.java From ghidra with Apache License 2.0 | 4 votes |
@Override public void validateModelSpecificInfo() throws InvalidDataTypeException { Program program = getProgram(); Address startAddress = getAddress(); // Get the model from the meta pointer. Address metaAddress = getMetaAddress(); Address rtti4Address = getAbsoluteAddress(program, metaAddress); rtti4Model = new Rtti4Model(program, rtti4Address, validationOptions); // Get the table DataType individualEntryDataType = new PointerDataType(program.getDataTypeManager()); long entrySize = individualEntryDataType.getLength(); // Each entry is a pointer to where a function can possibly be created. long numEntries = getCount(); if (numEntries == 0) { throw new InvalidDataTypeException( getName() + " data type at " + getAddress() + " doesn't have a valid vf table."); } Address vfTableFieldAddress = startAddress; for (int ordinal = 0; ordinal < numEntries && vfTableFieldAddress != null; ordinal++) { // Each component is a pointer (to a function). Address functionAddress = getAbsoluteAddress(program, vfTableFieldAddress); if (functionAddress == null) { throw new InvalidDataTypeException( getName() + " at " + getAddress() + " doesn't refer to a valid function."); } try { vfTableFieldAddress = vfTableFieldAddress.add(entrySize); // Add the data type size. } catch (AddressOutOfBoundsException e) { if (ordinal < (numEntries - 1)) { throw new InvalidDataTypeException( getName() + " at " + getAddress() + " isn't valid."); } break; } } }
Example 16
Source File: ProgramArchive.java From ghidra with Apache License 2.0 | 4 votes |
ProgramArchive(Program program) { this.program = program; this.dataTypeManager = program.getDataTypeManager(); }
Example 17
Source File: RTTI3DataType.java From ghidra with Apache License 2.0 | 4 votes |
@Override public boolean isValid(Program program, Address startAddress, DataValidationOptions validationOptions) { Memory memory = program.getMemory(); if (!memory.contains(startAddress)) { return false; } // RTTI4 should start on a 4 byte boundary. if (startAddress.getOffset() % 4 != 0) { return false; } Listing listing = program.getListing(); Address endAddress = startAddress.add(LENGTH - 1); try { MSDataTypeUtils.getBytes(memory, startAddress, LENGTH); } catch (InvalidDataTypeException e) { return false; // Couldn't get enough bytes from memory for an RTTI3. } if (!validationOptions.shouldIgnoreInstructions() && containsInstruction(listing, startAddress, endAddress)) { return false; } if (!validationOptions.shouldIgnoreDefinedData() && containsDefinedData(listing, startAddress, endAddress)) { return false; } // First 8 bytes is 2 dword numeric values. // Next four bytes after 2 dwords should be number of RTTI1 pointers in RTTI2. long rtti1Count = getRtti1Count(memory, startAddress); if (rtti1Count < 0 || rtti1Count > MAX_RTTI_1_COUNT) { // For now assume we shouldn't be seeing more than 1000 pointers in RTTI2. return false; } boolean validateReferredToData = validationOptions.shouldValidateReferredToData(); // Last component should refer to RTTI2. Address rtti2Address = getRtti2Address(memory, startAddress); RTTI2DataType rtti2 = new RTTI2DataType(rtti1Count, program.getDataTypeManager()); if (rtti2Address == null || (validateReferredToData && !rtti2.isValid(program, rtti2Address, validationOptions))) { return false; } return true; }
Example 18
Source File: CParserUtils.java From ghidra with Apache License 2.0 | 4 votes |
/** * Parse the given function signature text. Any exceptions will be handled herein * by showing an error dialog (null is returned in that case). * * @param service the service used to access DataTypeManagers or null to use only the program's * data type manager. * @param program the program against which data types will be resolved * @param signatureText the signature to parse * @param handleExceptions true signals that this method should deal with exceptions, * showing error messages as necessary; false signals to throw any encountered * parsing exceptions. This allows clients to perform exception handling that * better matches their workflow. * @return the data type that is created as a result of parsing; null if there was a problem */ public static FunctionDefinitionDataType parseSignature(DataTypeManagerService service, Program program, String signatureText, boolean handleExceptions) throws ParseException { DataTypeManager[] dataTypeManagers = service != null ? getDataTypeManagers(service) : new DataTypeManager[] { program.getDataTypeManager() }; CParser parser = new CParser(program.getDataTypeManager(), false, dataTypeManagers); String[] signatureParts = splitFunctionSignature(signatureText); if (signatureParts == null) { Msg.debug(CParserUtils.class, "Invalid signature: unable to isolate function name : " + signatureText); return null; } String replacedText = signatureParts[0] + " " + getTempName(signatureParts[1].length()) + signatureParts[2]; DataType dt = null; try { // parse the signature dt = parser.parse(replacedText + ";"); if (!(dt instanceof FunctionDefinitionDataType)) { return null; } // put back the old signature name dt.setName(signatureParts[1]); return (FunctionDefinitionDataType) dt; } catch (InvalidNameException | DuplicateNameException e) { // can't happen since we are calling setName() with the value that was // previously set (this can change in the future if we ever modify the // name before we restore it) Msg.debug(CParserUtils.class, "Logging an exception that cannot happen (the code must have changed)", e); } catch (Throwable t) { if (!handleExceptions) { throw t; } String msg = handleParseProblem(t, signatureText); if (msg != null) { Msg.showError(CParserUtils.class, null, "Invalid Function Signature", msg); } else { Msg.debug(CParserUtils.class, "Error parsing signature: " + signatureText, t); } } return null; }
Example 19
Source File: ConstantPoolDex.java From ghidra with Apache License 2.0 | 4 votes |
public ConstantPoolDex(Program program) throws IOException { this.program = program; DexAnalysisState analysisState = DexAnalysisState.getState(program); dexHeader = analysisState.getHeader(); dtManager = program.getDataTypeManager(); }
Example 20
Source File: MSDataTypeUtils.java From ghidra with Apache License 2.0 | 2 votes |
/** * Gets the appropriate reference data type. If program is 64 bit, then a 32-bit image * base offset data type will be returned. Otherwise, a default pointer to the * referredToDataType will be returned. * @param program the program that will contain the returned data type * @param referredToDataType the data type that is at the address being referred to by the * pointer or image base offset. Otherwise, null. * @return the image base offset or pointer reference data type */ public static DataType getReferenceDataType(Program program, DataType referredToDataType) { DataTypeManager dtm = program.getDataTypeManager(); return is64Bit(program) ? new ImageBaseOffset32DataType(dtm) : new PointerDataType(referredToDataType); }