Java Code Examples for ghidra.app.util.importer.MessageLog#appendMsg()

The following examples show how to use ghidra.app.util.importer.MessageLog#appendMsg() . 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: CoffLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void processEntryPoint(CoffFileHeader header, Program program, TaskMonitor monitor,
		MessageLog log) {

	AoutHeader optionalHeader = header.getOptionalHeader();
	if (optionalHeader != null) {
		int lentry = optionalHeader.getEntry();
		try {
			Address entry = CoffSectionHeader.getAddress(program.getLanguage(), lentry,
				program.getLanguage().getDefaultSpace());
			program.getSymbolTable().addExternalEntryPoint(entry);
			program.getSymbolTable().createLabel(entry, "entry", SourceType.IMPORTED);
		}
		catch (Exception e) {
			log.appendMsg(
				"Unable to create entry point symbol at " + Integer.toHexString(lentry));
			log.appendMsg("\t" + e.getMessage());
		}
	}
}
 
Example 2
Source File: DexMarkupInstructionsAnalyzer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void processString( Program program, Instruction instruction, int operand, DexHeader header, int stringIndex, MessageLog log ) {
		List< StringIDItem > strings = header.getStrings( );
		if ( stringIndex < 0 || stringIndex > strings.size( ) ) {
			log.appendMsg( "string index not found: " + stringIndex );
			return;
		}
		StringIDItem stringIDItem = strings.get( stringIndex );
		StringDataItem stringDataItem = stringIDItem.getStringDataItem( );
		if ( stringDataItem == null ) {
			log.appendMsg( "string data item is null: " + stringIndex );
			return;
		}
		AddressSpace defaultAddressSpace = program.getAddressFactory().getDefaultAddressSpace();
		Address stringAddr = defaultAddressSpace.getAddress(stringIDItem.getStringDataOffset());
		program.getReferenceManager().addMemoryReference(instruction.getMinAddress(), stringAddr,
			RefType.DATA, SourceType.ANALYSIS, operand);
//		setEquate( program, instruction.getMinAddress( ), operand, stringDataItem.getString( ), stringIndex );
//		program.getListing( ).setComment( instruction.getMinAddress( ), CodeUnit.EOL_COMMENT, stringDataItem.getString( ) );
	}
 
Example 3
Source File: AbstractLibrarySupportLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the library exports file, if necessary
 * 
 * @param libName the name of the library
 * @param libFile the library file
 * @param log the message log
 * @param monitor the task monitor
 * @param size the language size
 * @param program the loaded library program
 * @throws CancelledException thrown is task cancelled
 * 
 */
protected void createExportsFile(String libName, File libFile, MessageLog log,
		TaskMonitor monitor, int size, Program program) throws CancelledException {

	if (!LibraryLookupTable.libraryLookupTableFileExists(libName, size) ||
		!LibraryLookupTable.hasFileAndPathAndTimeStampMatch(libFile, size)) {
		try {
			// Need to write correct library exports file (LibrarySymbolTable)
			// for use with related imports
			LibraryLookupTable.createFile(program, true, monitor);
		}
		catch (IOException e) {
			log.appendMsg("Unable to create exports file for " + libFile);
			Msg.error(this, "Unable to create exports file for " + libFile, e);
		}
	}
}
 
Example 4
Source File: DyldCacheHeader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void markupLocalSymbolsInfo(Program program, AddressSpace space, TaskMonitor monitor,
		MessageLog log) throws CancelledException {
	monitor.setMessage("Marking up DYLD local symbols info...");
	monitor.initialize(1);
	try {
		if (localSymbolsInfo != null) {
			Address addr = fileOffsetToAddr(localSymbolsOffset, program, space);
			DataUtilities.createData(program, addr, localSymbolsInfo.toDataType(), -1, false,
				DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
			localSymbolsInfo.markup(program, addr, monitor, log);
		}
		monitor.incrementProgress(1);
	}
	catch (CodeUnitInsertionException | DuplicateNameException | IOException e) {
		log.appendMsg(DyldCacheHeader.class.getSimpleName(),
			"Failed to markup dyld_cache_local_symbols_info.");
	}
}
 
Example 5
Source File: MapLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
		TaskMonitor monitor, MessageLog log) throws IOException {

	if (!prog.getExecutableFormat().equals(PeLoader.PE_NAME)) {
		throw new IOException("Program must be a " + PeLoader.PE_NAME);
	}

	SymbolTable symtab = prog.getSymbolTable();
	AddressSpace space = prog.getAddressFactory().getDefaultAddressSpace();
	List<MapExport> exports = parseExports(provider, log);
	int successCount = 0;
	for (MapExport exp : exports) {
		try {
			symtab.createLabel(space.getAddress(exp.addr), exp.name,
				SourceType.IMPORTED).setPrimary();
			successCount++;
		}
		catch (InvalidInputException e) {
			log.appendMsg(e.getMessage());
		}
	}
	log.appendMsg("Added " + successCount + " symbols.");
}
 
Example 6
Source File: DyldCacheAccelerateInfo.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void parseAcceleratorDof(Program program, Address accelerateInfoAddr, MessageLog log,
		TaskMonitor monitor) throws CancelledException {
	monitor.setMessage("Parsing DYLD DOF sections...");
	monitor.initialize(dofSectionsCount);
	reader.setPointerIndex(dofSectionsOffset);
	try {
		for (int i = 0; i < dofSectionsCount; ++i) {
			acceleratorDofList.add(new DyldCacheAcceleratorDof(reader));
			monitor.checkCanceled();
			monitor.incrementProgress(1);
		}
	}
	catch (IOException e) {
		log.appendMsg(DyldCacheAccelerateInfo.class.getSimpleName(),
			"Failed to parse dyld_cache_accelerator_dof.");
	}
}
 
Example 7
Source File: BuildVersionCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {

	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName());
	}
}
 
Example 8
Source File: PeLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void processSymbols(FileHeader fileHeader, Map<SectionHeader, Address> sectionToAddress,
		Program program, TaskMonitor monitor, MessageLog log) {
	List<DebugCOFFSymbol> symbols = fileHeader.getSymbols();
	int errorCount = 0;
	for (DebugCOFFSymbol symbol : symbols) {
		if (!processDebugCoffSymbol(symbol, fileHeader, sectionToAddress, program, monitor)) {
			++errorCount;
		}
	}

	if (errorCount != 0) {
		log.appendMsg(
			"Failed to apply " + errorCount + " symbols contained within unknown sections.");
	}
}
 
Example 9
Source File: ElfRelocationHandler.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Generate warning log entry and bookmark at relocationAddress where
 * import issue occurred.
 * @param program 
 * @param relocationAddress relocation address to be bookmarked
 * @param type relocation type
 * @param symbolName symbol name
 * @param symbolIndex symbol index
 * @param msg message associated with warning
 * @param log import log
 */
public static void markAsWarning(Program program, Address relocationAddress, String type,
		String symbolName, long symbolIndex, String msg, MessageLog log) {
	
	symbolName = symbolName == null ? "<no name>" : symbolName;
	log.appendMsg("Elf Relocation Warning: Type = " + type + " at " + relocationAddress +
		", Symbol = " + symbolName + ": " + msg);
	BookmarkManager bookmarkManager = program.getBookmarkManager();
	bookmarkManager.setBookmark(relocationAddress, BookmarkType.WARNING,
		"Relocation_Type_" + type,
		"Unhandled Elf relocation ("+type+") at address: " + relocationAddress +
			". Symbol = " + symbolName + " (" + Long.toHexString(symbolIndex) + ")" +
			". " + msg);
}
 
Example 10
Source File: UuidCommand.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName());
	}
}
 
Example 11
Source File: DexCondenseFillerBytesAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean analyze( Program program, AddressSetView set, TaskMonitor monitor, MessageLog log ) throws Exception {
	AlignmentDataType alignmentDataType = new AlignmentDataType( );

	Address address = toAddr( program, DexUtil.METHOD_ADDRESS );
	MemoryBlock block = program.getMemory().getBlock( address );

	if ( block == null ) {
		log.appendMsg( "Can't locate block with method byte code!" );
		return false;
	}

	AddressSet blockSet = new AddressSet( block.getStart( ), block.getEnd( ) );

	AddressSetView undefinedSet = program.getListing().getUndefinedRanges( blockSet, true, monitor );

	monitor.setMaximum( undefinedSet.getNumAddressRanges() );
	monitor.setProgress( 0 );
	monitor.setMessage( "DEX: condensing filler bytes" );

	AddressRangeIterator addressRanges = undefinedSet.getAddressRanges();
	while ( addressRanges.hasNext( ) ) {
		monitor.checkCanceled( );
		monitor.incrementProgress( 1 );
		AddressRange addressRange = addressRanges.next();
		if ( isRangeAllSameBytes( program, addressRange, (byte) 0xff, monitor ) ) {
			program.getListing().createData( addressRange.getMinAddress(), alignmentDataType, (int)addressRange.getLength() );
		}
	}

	//collapseFillerBytes( program, monitor );

	return true;
}
 
Example 12
Source File: DebugDataDirectory.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,
		DataTypeConflictException, IOException {

	monitor.setMessage(program.getName()+": debug...");
	Address addr = PeUtils.getMarkupAddress(program, isBinary, ntHeader, virtualAddress);
	if (!program.getMemory().contains(addr)) {
		return;
	}
	createDirectoryBookmark(program, addr);

	AddressSpace space = program.getAddressFactory().getDefaultAddressSpace();

	DebugDirectory [] ddarr = parser.getDebugDirectories();
	for (DebugDirectory dd : ddarr) {
		PeUtils.createData(program, addr, dd.toDataType(), log);
		addr = addr.add(DebugDirectory.IMAGE_SIZEOF_DEBUG_DIRECTORY);

		Address dataAddr = getDataAddress(dd, isBinary, space, ntHeader);
		if (dataAddr != null) {
			boolean success = createFragment(program, "Debug Data", dataAddr, dataAddr.add(dd.getSizeOfData()));
			if (!success) {
				log.appendMsg("Unable to create fragment: Debug Data");
			}
		}
	}

	markupDebigMisc(program, isBinary, log, space);
	markupDebugCodeView(program, isBinary, log, space);
}
 
Example 13
Source File: ThreadCommand.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	try {
		if (isBinary) {
			createFragment(api, baseAddress, parentModule);
			Address addr = baseAddress.getNewAddress(getStartIndex());
			api.createData(addr, toDataType());
		}
	}
	catch (Exception e) {
		log.appendMsg("Unable to create " + getCommandName() + " - " + e.getMessage());
	}
}
 
Example 14
Source File: GccExceptionAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private int analyzeEhFrameHeaderSection(Program program, TaskMonitor monitor, MessageLog log) {

		try {
			EhFrameHeaderSection ehframehdrSection = new EhFrameHeaderSection(program);
			return ehframehdrSection.analyze(monitor);
		}
		catch (AddressOutOfBoundsException | MemoryAccessException
				| ExceptionHandlerFrameException e) {
			log.appendMsg("Error analyzing GCC EH Frame Header exception table");
			log.appendException(e);
		}
		return 0;
	}
 
Example 15
Source File: AbstractLibrarySupportLoader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to import all libraries listed in {@code unprocessedLibs}, placing the newly
 * created {@link DomainObject} instances in {@code programList}.
 *
 * @param programFolder The domain folder where the new program will be stored, if null
 *   the program should not be pre-saved. NOTE: the newly imported libraries will not be written
 *   to this folder yet, that is handled in a later follow on step.
 * @param paths A list of paths on the local filesystem to search for library files.
 * @param loadSpec The {@link LoadSpec}.
 * @param options The load options.
 * @param log The log.
 * @param consumer A consumer object for {@link DomainObject}s generated.
 * @param unprocessedLibs A list of libraries that need to be loaded.
 * @param processedLibs A list of libraries that have been loaded (used to prevent the same 
 *   library from being processed more than once)
 * @param programList A list to hold newly loaded programs and libraries.  Any program
 *      added to the list is the callers responsibility to release.
 * @param monitor A cancelable task monitor.
 * @throws IOException if there was an IO-related problem loading.
 * @throws CancelledException if the user cancelled the load.
 */
private void loadLibraries(DomainFolder programFolder, List<String> paths, LoadSpec loadSpec,
		List<Option> options, MessageLog log, Object consumer, Set<String> unprocessedLibs,
		Set<String> processedLibs, List<Program> programList, TaskMonitor monitor)
		throws CancelledException, IOException {

	// TODO warning - hack alert
	if (loadSpec.getLoader() instanceof PeLoader) {
		if (!isCreateExportSymbolFiles(options) && !isLoadLibraries(options)) {
			return;
		}
	}
	else {
		if (!isLoadLibraries(options)) {
			return;
		}
	}

	for (String libName : new HashSet<>(unprocessedLibs)) {
		monitor.checkCanceled();
		unprocessedLibs.remove(libName);
		if (processedLibs.contains(libName)) {
			continue;
		}
		boolean libImported = false;
		if (findAlreadyImportedLibrary(libName, programFolder) == null) {
			log.appendMsg("Searching for referenced library: " + libName + " ...");
			String simpleLibName = FilenameUtils.getName(libName);

			List<File> candidateLibFiles =
				findLibraryFileToImport(FilenameUtils.separatorsToUnix(libName), paths);
			for (File libFile : candidateLibFiles) {
				monitor.checkCanceled();
				if (importLibrary(simpleLibName, programFolder, libFile, loadSpec, options, log,
					consumer, unprocessedLibs, programList, monitor)) {
					libImported = true;
					log.appendMsg("Found and imported external library: " + libFile);
					break;
				}
			}
			if (!libImported) {
				log.appendMsg("Unable to find external library: " + libName);
			}
		}
		processedLibs.add(libName);
	}
	log.appendMsg(
		"Finished importing referenced libraries for: " + programList.get(0).getName());
}
 
Example 16
Source File: PdbAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
File lookForPdb(Program program, boolean includePeSpecifiedPdbPath, MessageLog log) {
	String message = "";
	File pdb;

	try {

		pdb = PdbParser.findPDB(program, includePeSpecifiedPdbPath, symbolsRepositoryPath);

		if (pdb == null) {

			PdbMissingState missingState =
				AnalysisStateInfo.getAnalysisState(program, PdbMissingState.class);
			if (missingState != null) {
				return null; // already notified user
			}
			AnalysisStateInfo.putAnalysisState(program, new PdbMissingState());

			String pdbName = program.getOptions(Program.PROGRAM_INFO).getString(
				PdbParserConstants.PDB_FILE, (String) null);
			if (pdbName == null) {
				message = "Program has no associated PDB file.";
			}
			else {
				message = "Unable to locate PDB file \"" + pdbName + "\" with matching GUID.";
			}
			if (SystemUtilities.isInHeadlessMode()) {
				message += "\n Use a script to set the PDB file location. I.e.,\n" +
					"    setAnalysisOption(currentProgram, \"PDB.Symbol Repository Path\", \"/path/to/pdb/folder\");\n" +
					" This must be done using a pre-script (prior to analysis).";
			}
			else {
				message += "\n You may set the PDB \"Symbol Repository Path\"" +
					"\n using \"Edit->Options for [program]\" prior to analysis." +
					"\nIt is important that a PDB is used during initial analysis " +
					"\nif available.";
			}
		}

		return pdb;
	}
	catch (PdbException pe) {
		message += pe.getMessage();
	}
	finally {
		if (message.length() > 0) {
			log.appendMsg(getName(), message);
			log.setStatus(message);
		}
	}

	return null;
}
 
Example 17
Source File: GccExceptionAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void handleStandardSections(Program program, TaskMonitor monitor, MessageLog log)
		throws CancelledException {

	int fdeTableCount = analyzeEhFrameHeaderSection(program, monitor, log);
	// If the EHFrameHeader doesn't exist, the fdeTableCount will be 0.

	monitor.checkCanceled();

	try {
		/*
		 * If the .eh_frame section exists, build the structures
		 * contained within this program section.
		 */

		EhFrameSection ehframeSection = new EhFrameSection(monitor, program);
		List<RegionDescriptor> regions = ehframeSection.analyze(fdeTableCount);

		AddressSet ehProtected = new AddressSet();

		for (RegionDescriptor region : regions) {

			monitor.checkCanceled();
			ehProtected.add(region.getRange());

			LSDACallSiteTable callSiteTable = region.getCallSiteTable();
			if (callSiteTable != null) {

				// Process this table's call site records.
				for (LSDACallSiteRecord cs : callSiteTable.getCallSiteRecords()) {
					monitor.checkCanceled();
					processCallSiteRecord(program, ehProtected, region, cs);
				}
			}
		}

	}
	catch (MemoryAccessException | ExceptionHandlerFrameException e) {
		log.appendMsg("Error analyzing GCC exception tables");
		log.appendException(e);
	}
}
 
Example 18
Source File: OmfLoader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
protected void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
		Program program, TaskMonitor monitor, MessageLog log)
		throws IOException, CancelledException {

	OmfFileHeader header = null;
	BinaryReader reader = OmfFileHeader.createReader(provider);
	try {
		header = OmfFileHeader.parse(reader, monitor);
		header.resolveNames();
		header.sortSegmentDataBlocks();
		OmfFileHeader.doLinking(IMAGE_BASE, header.getSegments(), header.getGroups());
	}
	catch (OmfException ex) {
		if (header == null) {
			throw new IOException("OMF File header was corrupted");
		}
		log.appendMsg("File was corrupted - leaving partial program " + provider.getName());
	}

	// We don't use the file bytes to create block because the bytes are manipulated before
	// forming the block.  Creating the FileBytes anyway in case later we want access to all
	// the original bytes.
	MemoryBlockUtils.createFileBytes(program, provider, monitor);

	int id = program.startTransaction("loading program from OMF");
	boolean success = false;
	try {
		processSegmentHeaders(reader, header, program, monitor, log);
		processExternalSymbols(header, program, monitor, log);
		processPublicSymbols(header, program, monitor, log);
		processRelocations(header, program, monitor, log);
		success = true;
	}
	catch (AddressOverflowException e) {
		throw new IOException(e);
	}
	finally {
		program.endTransaction(id, success);
	}
}
 
Example 19
Source File: AbstractLibrarySupportLoader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Imports a library file into a ghidra project. Use this method if you already have
 * a {@link ByteProvider} available.
 * 
 * @param libName the name of the library to import
 * @param libFolder the library folder
 * @param libFile the library file to load
 * @param provider the byte provider
 * @param loadSpec the {@link LoadSpec}
 * @param options the load options
 * @param log the message log
 * @param consumer consumer object for the {@link Program} generated
 * @param unprocessedLibs list of libraries that need to be loaded
 * @param programList list of programs to add the imported library to
 * @param monitor the task monitor
 * @return true if the load was successful
 * @throws CancelledException if the user cancelled the load operation
 * @throws IOException if there was an error during the load
 */
protected boolean importLibrary(String libName, DomainFolder libFolder, File libFile,
		ByteProvider provider, LoadSpec loadSpec, List<Option> options, MessageLog log,
		Object consumer, Set<String> unprocessedLibs, List<Program> programList,
		TaskMonitor monitor) throws CancelledException, IOException {

	Program lib = null;
	int size = loadSpec.getLanguageCompilerSpec().getLanguageDescription().getSize();

	LoadSpec libLoadSpec = getLoadSpec(loadSpec, provider);
	if (libLoadSpec == null) {
		log.appendMsg("Skipping library which is the wrong architecture: " + libFile);
		return false;
	}
	if (!isLoadLibraries(options)) {
		// TODO: LibraryLookupTable support currently assumes Windows for x86 (32 or 64 bit).
		//       Need to investigate adding support for other architectures
		if (LibraryLookupTable.hasFileAndPathAndTimeStampMatch(libFile, size)) {
			return true;// no need to really import it
		}
		else if (LibraryLookupTable.libraryLookupTableFileExists(libName, size)) {
			log.appendMsg("WARNING! Using existing exports file for " + libName +
				" which may not be an exact match");
			return true;// pretend it was imported to prevent it from giving up the related imports
		}
	}

	lib = doLoad(provider, libName, libFolder, libLoadSpec, options, log, consumer, monitor,
		unprocessedLibs);

	if (lib == null) {
		log.appendMsg("Library " + libFile + " failed to load for some reason");
		return false;
	}

	createExportsFile(libName, libFile, log, monitor, size, lib);

	if (isLoadLibraries(options)) {
		programList.add(lib);
	}
	else {
		lib.release(consumer);
	}

	return true;

}
 
Example 20
Source File: CoffLoader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void handleRelocationError(Program program, MessageLog log, Address address,
		String message) {
	program.getBookmarkManager().setBookmark(address, BookmarkType.ERROR, "Relocations",
		message);
	log.appendMsg(message);
}