ghidra.util.exception.DuplicateNameException Java Examples

The following examples show how to use ghidra.util.exception.DuplicateNameException. 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: ISO9660PathTable.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	Structure struc;
	if (littleEndian) {
		struc = new StructureDataType("ISO9660TypeLPathTable", 0);
	}
	else {
		struc = new StructureDataType("ISO9660TypeMPathTable", 0);
	}

	struc.add(BYTE, "Directory Identifier Length", "Length of Directory Identifier");
	struc.add(BYTE, "Extended Attribute Record Length", "Length of Extended Attribute Record");
	struc.add(DWORD, "Location of Extent", "Location of Extent in Little-endian format");
	struc.add(WORD, "Directory Number",
		"Number of parent directory (an index in to the path table)");
	struc.add(new ArrayDataType(BYTE, directoryIdentifier.length, 1), "Directory Identifier",
		"Directory Identifier");
	if (paddingFieldPresent) {
		struc.add(BYTE, "Padding Field", "Padding Field");
	}
	return struc;
}
 
Example #2
Source File: LoadConfigDataDirectory.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
		NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
		DataTypeConflictException, IOException {

	monitor.setMessage(program.getName()+": load config directory...");
	Address addr = PeUtils.getMarkupAddress(program, isBinary, ntHeader, virtualAddress);
	if (!program.getMemory().contains(addr)) {
		return;
	}
	createDirectoryBookmark(program, addr);
	
	PeUtils.createData(program, addr, lcd.toDataType(), log);

	markupSeHandler(program, isBinary, monitor, log, ntHeader);
	ControlFlowGuard.markup(lcd, program, log, ntHeader);
}
 
Example #3
Source File: AbstractFrameSection.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a label indicating there is an CIE at the address.
 * @param curAddress the address with the CIE
 */
protected void createCieLabel(Address curAddress) {
	try {
		String cieLabel = "cie_" + curAddress.toString();
		Symbol cieSym = program.getSymbolTable().getPrimarySymbol(curAddress);
		if (cieSym == null) {
			cieSym =
				program.getSymbolTable().createLabel(curAddress, cieLabel, SourceType.ANALYSIS);
		}
		else {
			cieSym.setName(cieLabel, SourceType.ANALYSIS);
		}
	}
	catch (InvalidInputException | DuplicateNameException e) {
		/* Output message and keep going even though couldn't create the label. */
		Msg.info(this, "Unable to label CIE -- " + e.getMessage(), e);
	}
}
 
Example #4
Source File: ThreadStateARM.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	StructureDataType struct = new StructureDataType("ARM_THREAD_STATE", 0);
	struct.add(DWORD, "r0", null);
	struct.add(DWORD, "r1", null);
	struct.add(DWORD, "r2", null);
	struct.add(DWORD, "r3", null);
	struct.add(DWORD, "r4", null);
	struct.add(DWORD, "r5", null);
	struct.add(DWORD, "r6", null);
	struct.add(DWORD, "r7", null);
	struct.add(DWORD, "r8", null);
	struct.add(DWORD, "r9", null);
	struct.add(DWORD, "r10", null);
	struct.add(DWORD, "r11", null);
	struct.add(DWORD, "r12", null);
	struct.add(DWORD, "sp", null);
	struct.add(DWORD, "lr", null);
	struct.add(DWORD, "pc", null);
	struct.add(DWORD, "cpsr", null);
	struct.setCategoryPath(new CategoryPath(MachConstants.DATA_TYPE_CATEGORY));
	return struct;
}
 
Example #5
Source File: JavaLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void doLoad(ByteProvider provider, Program program, TaskMonitor monitor)
		throws LockException, MemoryConflictException, AddressOverflowException,
		CancelledException, DuplicateNameException, IOException {
	AddressFactory af = program.getAddressFactory();
	AddressSpace space = af.getAddressSpace(CONSTANT_POOL);
	Memory memory = program.getMemory();
	alignmentReg = program.getRegister("alignmentPad");

	BinaryReader reader = new BinaryReader(provider, false);
	ClassFileJava classFile = new ClassFileJava(reader);

	Address address = space.getAddress(0);

	// Create a block of memory with just the right size
	memory.createInitializedBlock("_" + provider.getName() + "_", address,
		provider.getInputStream(0), provider.length(), monitor, false);

	createMethodLookupMemoryBlock(program, monitor);
	createMethodMemoryBlocks(program, provider, classFile, monitor);

}
 
Example #6
Source File: BinaryPropertyListTrailer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	Structure structure = new StructureDataType( "bplist_trailer", 0 );
	structure.add( BYTE, "unk0", null );
	structure.add( BYTE, "unk1", null );
	structure.add( BYTE, "unk2", null );
	structure.add( BYTE, "unk3", null );
	structure.add( BYTE, "unk4", null );
	structure.add( BYTE, "unk5", null );
	structure.add( BYTE, "offsetSize", null );
	structure.add( BYTE, "objectRefSize", null );
	structure.add( QWORD, "objectCount", null );
	structure.add( QWORD, "topObject", null );
	structure.add( QWORD, "offsetTableOffset", null );
	return structure;
}
 
Example #7
Source File: ExternalManagerDBTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetExternalLocationsByMemAddr()
		throws InvalidInputException, DuplicateNameException {

	extMgr.addExtLocation("ext1", "label0", null, SourceType.USER_DEFINED);
	extMgr.addExtLocation("ext1", "label1", addr(1000), SourceType.USER_DEFINED);
	extMgr.addExtLocation("ext2", "label1", null, SourceType.USER_DEFINED);
	extMgr.addExtLocation("ext2", "label2", null, SourceType.USER_DEFINED);

	ExternalLocation loc5 =
		extMgr.addExtLocation("ext2", null, addr(2000), SourceType.USER_DEFINED);

	ExternalLocationIterator iter = extMgr.getExternalLocations(addr(2000));
	assertTrue(iter.hasNext());
	assertEquals(loc5, iter.next());
	assertTrue(!iter.hasNext());
	assertNull(iter.next());

}
 
Example #8
Source File: Section.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	StructureDataType struct = new StructureDataType("section", 0);
	struct.add(new StringDataType(), MachConstants.NAME_LENGTH, "sectname", null);
	struct.add(new StringDataType(), MachConstants.NAME_LENGTH, "segname", null);
	if (is32bit) {
		struct.add(DWORD, "addr", null);
		struct.add(DWORD, "size", null);
	}
	else {
		struct.add(QWORD, "addr", null);
		struct.add(QWORD, "size", null);
	}
	struct.add(DWORD, "offset", null);
	struct.add(DWORD, "align", null);
	struct.add(DWORD, "reloff", null);
	struct.add(DWORD, "nrelocs", null);
	struct.add(DWORD, "flags", null);
	struct.add(DWORD, "reserved1", null);
	struct.add(DWORD, "reserved2", null);
	if (!is32bit) {
		struct.add(DWORD, "reserved3", null);
	}
	struct.setCategoryPath(new CategoryPath(MachConstants.DATA_TYPE_CATEGORY));
	return struct;
}
 
Example #9
Source File: CategoryNode.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void valueChanged(Object newValue) {
	int transactionID = category.getDataTypeManager().startTransaction("rename");
	try {
		category.setName(newValue.toString());
	}
	catch (DuplicateNameException e) {
		Msg.showError(getClass(), null, "Rename Failed",
			"Category by the name " + newValue + " already exists in this category.");
	}
	catch (InvalidNameException exc) {
		String msg = exc.getMessage();
		if (msg == null) {
			msg = "Invalid name specified: " + newValue;
		}
		Msg.showError(getClass(), null, "Invalid name specified", exc.getMessage());
	}
	finally {
		category.getDataTypeManager().endTransaction(transactionID, true);
	}
}
 
Example #10
Source File: NSString.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	Structure structure = new StructureDataType( "NSString_" + string.length( ), 0 );
	addHeader( structure, string.length( ) );
	if ( string.length( ) > 0 ) {
		if ( type == NSStringTypes.TYPE_ASCII ) {
			structure.add( STRING, string.length( ), "ascii", null );
		}
		else if ( type == NSStringTypes.TYPE_UTF16BE ) {
			structure.add( UTF16, string.length( ) * 2, "utf16be", null );
		}
		else {
			throw new RuntimeException( );
		}
	}

	return structure;
}
 
Example #11
Source File: ExternalManagerDBTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testOriginalImportName()
		throws InvalidInputException, DuplicateNameException, CircularDependencyException {
	ExternalLocation extLoc =
		extMgr.addExtLocation("ext1", "foo", addr(1000), SourceType.IMPORTED);

	extLoc.setName(extLoc.getParentNameSpace(), "bar", SourceType.ANALYSIS);

	assertEquals("bar", extLoc.getLabel());
	assertEquals("foo", extLoc.getOriginalImportedName());
	assertEquals(SourceType.ANALYSIS, extLoc.getSource());

	extLoc.restoreOriginalName();

	assertEquals("foo", extLoc.getLabel());
	assertEquals(null, extLoc.getOriginalImportedName());
	assertEquals(SourceType.IMPORTED, extLoc.getSource());

}
 
Example #12
Source File: ExceptionHandlerJava.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	Structure structure = new StructureDataType("exception_handler", 0);
	structure.add(WORD, "start_pc", null);
	structure.add(WORD, "end_pc", null);
	structure.add(WORD, "handler_pc", null);
	structure.add(WORD, "catch_type", null);
	return structure;
}
 
Example #13
Source File: CliMetadataDirectory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
		NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
		IOException, MemoryAccessException {

	if (metadataRoot == null) {
		return;
	}

	monitor.setMessage("[" + program.getName() + "]: CLI metadata...");

	// Get our program address
	Address addr = PeUtils.getMarkupAddress(program, isBinary, ntHeader, virtualAddress);
	if (!program.getMemory().contains(addr)) {
		return;
	}

	// Create bookmark
	createDirectoryBookmark(program, addr);

	// Create data type
	DataType dt = metadataRoot.toDataType();
	dt.setCategoryPath(new CategoryPath("/PE/CLI"));
	PeUtils.createData(program, addr, dt, log);

	// Markup metadata header
	metadataRoot.markup(program, isBinary, monitor, log, ntHeader);
}
 
Example #14
Source File: RenameVariableTask.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void commit() throws DuplicateNameException, InvalidInputException {
	if (commitRequired) {
		HighFunctionDBUtil.commitParamsToDatabase(hfunction, false, signatureSrcType);
		if (signatureSrcType != SourceType.DEFAULT) {
			HighFunctionDBUtil.commitReturnToDatabase(hfunction, signatureSrcType);
		}
	}
	HighFunctionDBUtil.updateDBVariable(highSymbol, newName, null, srctype);
}
 
Example #15
Source File: EncodedArrayItem.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public DataType toDataType( ) throws DuplicateNameException, IOException {
	DataType dataType = array.toDataType( );
	Structure structure = new StructureDataType( "encoded_array_item_" + dataType.getLength( ), 0 );
	structure.add( dataType, "value", null );
	structure.setCategoryPath( new CategoryPath( "/dex/encoded_array_item" ) );
	return structure;
}
 
Example #16
Source File: LongNamesMember.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public DataType toDataType() throws DuplicateNameException, IOException {
	String name = StructConverterUtil.parseName(LongNamesMember.class);
	String uniqueName = name + "_" + _nStrings;
	Structure struct = new StructureDataType(uniqueName, 0);
	for (int i = 0 ; i < _nStrings ; ++i) {
		struct.add(STRING, lengths.get(i), "string["+i+"]", null);
	}
	return struct;
}
 
Example #17
Source File: AbstractExternalMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
ExternalLocation createExternalLabel(final Program program, final String[] path,
		final Address memoryAddress, final SourceType sourceType)
		throws DuplicateNameException, InvalidInputException {

	SymbolTable symbolTable = program.getSymbolTable();
	ExternalManager externalManager = program.getExternalManager();
	int nameIndex = path.length - 1;

	Library externalLibrary;
	Symbol librarySymbol = getUniqueSymbol(program, path[0], program.getGlobalNamespace());
	if (librarySymbol != null) {
		externalLibrary = (Library) librarySymbol.getObject();
	}
	else {
		externalLibrary = symbolTable.createExternalLibrary(path[0], sourceType);
	}

	Namespace currentNamespace = externalLibrary;
	for (int i = 1; i < nameIndex; i++) {
		Symbol nextNamespaceSymbol = getUniqueSymbol(program, path[i], currentNamespace);
		if (nextNamespaceSymbol != null) {
			currentNamespace = (Namespace) nextNamespaceSymbol.getObject();
		}
		else {
			currentNamespace =
				symbolTable.createNameSpace(currentNamespace, path[i], sourceType);
		}
	}

	ExternalLocation externalLocation = externalManager.addExtLocation(currentNamespace,
		path[nameIndex], memoryAddress, sourceType);

	return externalLocation;
}
 
Example #18
Source File: CreateDataCmdTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateDataOnDefaultPointer()
		throws InvalidInputException, DuplicateNameException {

	Address addr = addr(UNDEFINED_AREA);
	CreateDataCmd cmd = new CreateDataCmd(addr, false, true, new PointerDataType());
	cmd.applyTo(program);

	// Add external reference from pointer
	program.getReferenceManager().addExternalReference(addr, "OtherFile", "ExtLabel", null,
		SourceType.USER_DEFINED, 0, RefType.DATA);

	// Undefined* becomes Byte*
	cmd = new CreateDataCmd(addr, false, true, new ByteDataType());
	cmd.applyTo(program);

	Data d = listing.getDataAt(addr);
	assertNotNull(d);
	assertTrue(d.isDefined());
	assertEquals(4, d.getLength());
	DataType dt = d.getDataType();
	assertTrue(dt instanceof Pointer);
	assertEquals(addr.getPointerSize(), dt.getLength());
	Pointer pdt = (Pointer) dt;
	assertTrue(pdt.getDataType() instanceof ByteDataType);

	d = listing.getDataAfter(addr);
	assertNotNull(d);
	assertTrue(!d.isDefined());
	assertEquals(4, d.getMinAddress().getOffset() - UNDEFINED_AREA);

	// Verify that reference is still intact
	CodeUnit cu = listing.getCodeUnitAt(addr);
	assertNotNull(cu.getExternalReference(0));

}
 
Example #19
Source File: ProgramAddressFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void initHashSpace(Language language) {
	try {
		addAddressSpace(AddressSpace.HASH_SPACE);
	}
	catch (DuplicateNameException e) {
		throw new IllegalStateException("Language must not define 'HASH' space: " +
			language.getLanguageID().getIdAsString());
	}
}
 
Example #20
Source File: BaseRelocationDataDirectory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @see ghidra.app.util.bin.StructConverter#toDataType()
 */
@Override
public DataType toDataType() throws DuplicateNameException {
    StructureDataType struct = new StructureDataType(NAME, 0);
    for (BaseRelocation reloc : relocs) {
        struct.add(reloc.toDataType());
    }
    struct.setCategoryPath(new CategoryPath("/PE"));
    return struct;
}
 
Example #21
Source File: MultipleSymbolStringable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Namespace createNamespace(Program program, NamespaceInfo info, Namespace namespace)
		throws DuplicateNameException, InvalidInputException {
	SymbolTable symbolTable = program.getSymbolTable();
	String name = info.name;
	SymbolType type = info.symbolType;
	SourceType sourceType = info.sourceType;

	if (type == SymbolType.LIBRARY) {
		return symbolTable.createExternalLibrary(name, sourceType);
	}
	else if (type == SymbolType.CLASS) {
		return symbolTable.createClass(namespace, name, sourceType);
	}
	return symbolTable.createNameSpace(namespace, name, sourceType);
}
 
Example #22
Source File: ConstantPoolStringInfo.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	String name = "CONSTANT_String_info";
	Structure structure = new StructureDataType(name, 0);
	structure.add(BYTE, "tag", null);
	structure.add(WORD, "string_index", null);
	return structure;
}
 
Example #23
Source File: ContainerHeader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public DataType toDataType() throws DuplicateNameException, IOException {
	Structure struct = new StructureDataType("ContainerHeader", 0);
	struct.add(STRING, 4, "tag1", null);
	struct.add(STRING, 4, "tag2", null);
	struct.add(STRING, 4, "architecture", null);
	struct.add(DWORD, "formatVersion", null);
	struct.add(new MacintoshTimeStampDataType(), "dateTimeStamp", null);
	struct.add(DWORD, "oldDefVersion", null);
	struct.add(DWORD, "oldImpVersion", null);
	struct.add(DWORD, "currentVersion", null);
	struct.add(WORD, "sectionCount", null);
	struct.add(WORD, "instSectionCount", null);
	struct.add(DWORD, "reservedA", null);
	return struct;
}
 
Example #24
Source File: FunctionNameMarkupItemTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
Parameter addStackParameter(Function function, String name, SourceType sourceType,
		DataType dataType, int stackOffset, String comment)
		throws InvalidInputException, DuplicateNameException {
	Parameter parameter1 =
		new ParameterImpl(name, dataType, stackOffset, function.getProgram());
	parameter1.setComment(comment);
	return function.addParameter(parameter1, sourceType);
}
 
Example #25
Source File: ISO9660BaseVolume.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	Structure struc = new StructureDataType("ISO9660VolumeDescriptor", 0);

	struc.add(BYTE, "Type Code", "Type of volume descriptor");
	struc.add(new ArrayDataType(BYTE, identifier.length, 1), "Standard Identifier",
		"Always 'CD001'");
	struc.add(BYTE, "Version", "Always 0x01");

	return struc;
}
 
Example #26
Source File: AddParameterCommand.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @see ghidra.framework.cmd.Command#applyTo(ghidra.framework.model.DomainObject)
 */
@Override
public boolean applyTo(DomainObject obj) {
	String name = null;
	try {
		Parameter parameter2add = getParameter((Program) obj);
		name = parameter2add.getName();
		if (function.insertParameter(ordinal, parameter2add, source) == null) {
			statusMessage = "Create parameter failed";
			return false;
		}
	}
	catch (DuplicateNameException e) {
		statusMessage = "Parameter named " + name + " already exists";
		return false;
	}
	catch (Exception exc) {
		Throwable cause = exc.getCause();
		if (cause != null) {
			statusMessage = cause.getMessage();
			if (statusMessage == null) {
				statusMessage = cause.getClass().getName();
			}
		}
		else {
			statusMessage = exc.getMessage();
			if (statusMessage == null) {
				statusMessage = exc.getClass().getName();
			}
		}
		return false;
	}

	return true;
}
 
Example #27
Source File: DebugDataDirectory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void markupDebigMisc(Program program, boolean isBinary,
		MessageLog log, AddressSpace space) throws DuplicateNameException {
	DebugMisc dm = parser.getDebugMisc();
	if (dm != null) {
		Address dataAddr = getDataAddress(dm.getDebugDirectory(), isBinary, space, ntHeader);
		if (dataAddr != null) {
			setPlateComment(program, dataAddr, "Misc Debug Info");
			PeUtils.createData(program, dataAddr, dm.toDataType(), log);
		}
	}
}
 
Example #28
Source File: FunctionNameStringable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void applyFunctionName(Program program, Function function)
		throws DuplicateNameException, InvalidInputException, CircularDependencyException {

	Address entryPoint = function.getEntryPoint();
	if (entryPoint.isMemoryAddress() && sourceType == SourceType.DEFAULT) {
		// Apply a default name by removing the current one.
		program.getSymbolTable().removeSymbolSpecial(function.getSymbol());
		return;
	}

	SymbolTable symbolTable = program.getSymbolTable();
	Namespace namespace = program.getGlobalNamespace();
	for (NamespaceInfo info : namespaceInfos) {
		Namespace ns = symbolTable.getNamespace(info.name, namespace);
		if (ns != null) {
			if (function.isExternal() != ns.isExternal()) {
				throw new DuplicateNameException("Conflicting namespace: " + info.name);
			}
			if (info.symbolType == SymbolType.CLASS &&
				ns.getSymbol().getSymbolType() == SymbolType.NAMESPACE) {
				// Promote existing namespace to class
				ns = NamespaceUtils.convertNamespaceToClass(ns);
			}
			namespace = ns;
		}
		else {
			namespace = createNamespace(program, info, namespace);
		}
	}

	Symbol s = function.getSymbol();
	s.setNameAndNamespace(symbolName, namespace, sourceType);
}
 
Example #29
Source File: ISO9660SetTerminator.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	Structure struc = new StructureDataType("ISO9600SetTerminator", 0);
	struc.add(BYTE, "Type", "Volume Descriptor Type");
	struc.add(new ArrayDataType(BYTE, super.getIdentifier().length, 1), "Identifier",
		"Identifier");
	struc.add(BYTE, "Version", "Volume Descriptor Version");

	return struc;
}
 
Example #30
Source File: MemoryBlockUtils.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Adjusts the name of the fragment at the given address to the given name.
 * @param program the program whose fragment is to be renamed.
 * @param address the address of the fragment to be renamed.
 * @param name the new name for the fragment.
 */
public static void adjustFragment(Program program, Address address, String name) {
	Listing listing = program.getListing();
	String[] treeNames = listing.getTreeNames();
	for (String treeName : treeNames) {
		try {
			ProgramFragment frag = listing.getFragment(treeName, address);
			frag.setName(name);
		}
		catch (DuplicateNameException e) {
			Msg.warn(MemoryBlockUtils.class,
				"Could not rename fragment to match newly created block because of name conflict");
		}
	}
}