Java Code Examples for ghidra.app.util.MemoryBlockUtils#createFileBytes()

The following examples show how to use ghidra.app.util.MemoryBlockUtils#createFileBytes() . 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: MachoLoader.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 program, TaskMonitor monitor, MessageLog log) throws IOException {

	try {
		FileBytes fileBytes = MemoryBlockUtils.createFileBytes(program, provider, monitor);

		// A Mach-O file may contain PRELINK information.  If so, we use a special
		// program builder that knows how to deal with it.
		List<PrelinkMap> prelinkList = MachoPrelinkUtils.parsePrelinkXml(provider, monitor);
		if (!prelinkList.isEmpty()) {
			MachoPrelinkProgramBuilder.buildProgram(program, provider, fileBytes, prelinkList,
				log, monitor);
		}
		else {
			MachoProgramBuilder.buildProgram(program, provider, fileBytes, log, monitor);
		}
	}
	catch (Exception e) {
		throw new IOException(e.getMessage());
	}
}
 
Example 2
Source File: MemoryBlockHelper.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
public void addSection(String name, long addressOffset, long offset, long length, boolean read, boolean write, boolean execute)
{
    try
    {
        FileBytes fileBytes = MemoryBlockUtils.createFileBytes(this.program, this.byteProvider, offset, length, this.monitor);
        MemoryBlockUtils.createInitializedBlock(this.program, false, name, this.program.getImageBase().add(addressOffset), fileBytes, 0, length, "", null, read, write, execute, this.log);
    }
    catch (Exception e)
    {
        Msg.error(this, "Failed to add section " + name, e);
    }
    
    this.flushLog();
}
 
Example 3
Source File: CoffLoader.java    From ghidra with Apache License 2.0 5 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 {

	boolean performFakeLinking = performFakeLinking(options);

	CoffFileHeader header = new CoffFileHeader(provider);
	header.parse(provider, monitor);

	Map<CoffSectionHeader, Address> sectionsMap = new HashMap<>();
	Map<CoffSymbol, Symbol> symbolsMap = new HashMap<>();

	FileBytes fileBytes = MemoryBlockUtils.createFileBytes(program, provider, monitor);

	int id = program.startTransaction("loading program from COFF");
	boolean success = false;
	try {
		processSectionHeaders(provider, header, program, fileBytes, monitor, log, sectionsMap,
			performFakeLinking);
		processSymbols(header, program, monitor, log, sectionsMap, symbolsMap);
		processEntryPoint(header, program, monitor, log);
		processRelocations(header, program, sectionsMap, symbolsMap, log, monitor);
		success = true;
	}
	catch (AddressOverflowException e) {
		throw new IOException(e);
	}
	finally {
		program.endTransaction(id, success);
	}
}
 
Example 4
Source File: PrelinkFileSystem.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public Program getProgram(GFile file, LanguageService languageService, TaskMonitor monitor,
		Object consumer) throws Exception {
	Long offset = fileToMachoOffsetMap.get(file);
	if (offset == null) {
		return null;
	}
	MachHeader machHeader =
		MachHeader.createMachHeader(RethrowContinuesFactory.INSTANCE, provider, offset, true);
	LanguageCompilerSpecPair lcs = MacosxLanguageHelper.getLanguageCompilerSpecPair(
		languageService, machHeader.getCpuType(), machHeader.getCpuSubType());
	Program program =
		new ProgramDB(file.getName(), lcs.getLanguage(), lcs.getCompilerSpec(), consumer);
	int id = program.startTransaction(getName());
	boolean success = false;
	try {
		FileBytes fileBytes = MemoryBlockUtils.createFileBytes(program, provider, offset,
			provider.length() - offset, monitor);
		ByteProvider providerWrapper =
			new ByteProviderWrapper(provider, offset, provider.length() - offset);
		MachoProgramBuilder.buildProgram(program, providerWrapper, fileBytes, new MessageLog(),
			monitor);
		program.setExecutableFormat(MachoLoader.MACH_O_NAME);
		program.setExecutablePath(file.getPath());

		if (file.equals(systemKextFile)) {
			processSystemKext(languageService, program, monitor);
		}

		success = true;
	}
	catch (Exception e) {
		throw e;
	}
	finally {
		program.endTransaction(id, success);
		if (!success) {
			program.release(consumer);
		}
	}
	return program;
}
 
Example 5
Source File: NeLoader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
		TaskMonitor monitor, MessageLog log) throws IOException, CancelledException {

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing new executable...");

	initVars();

	ContinuesFactory factory = MessageLogContinuesFactory.create(log);

	// 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(prog, provider, monitor);

	SegmentedAddressSpace space =
		(SegmentedAddressSpace) prog.getAddressFactory().getDefaultAddressSpace();
	NewExecutable ne = new NewExecutable(factory, provider, space.getAddress(SEGMENT_START, 0));
	WindowsHeader wh = ne.getWindowsHeader();
	InformationBlock ib = wh.getInformationBlock();
	SegmentTable st = wh.getSegmentTable();
	ResourceTable rt = wh.getResourceTable();
	EntryTable et = wh.getEntryTable();
	ResidentNameTable rnt = wh.getResidentNameTable();
	NonResidentNameTable nrnt = wh.getNonResidentNameTable();
	ImportedNameTable imp = wh.getImportedNameTable();
	ModuleReferenceTable mrt = wh.getModuleReferenceTable();

	Listing listing = prog.getListing();
	SymbolTable symbolTable = prog.getSymbolTable();
	Memory memory = prog.getMemory();
	ProgramContext context = prog.getProgramContext();
	RelocationTable relocTable = prog.getRelocationTable();

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing segment table...");
	processSegmentTable(log, ib, st, space, prog, context, monitor);
	if (prog.getMemory().isEmpty()) {
		Msg.error(this, "Empty memory for " + prog);
		return;
	}

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing resource table...");
	processResourceTable(log, prog, rt, space, monitor);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing module reference table...");
	processModuleReferenceTable(mrt, st, imp, prog, space, log, monitor);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing entry table...");
	processEntryTable(st, ib, et, symbolTable, space);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing non-resident name table...");
	processNonResidentNameTable(nrnt, symbolTable);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing resident name table...");
	processResidentNameTable(rnt, symbolTable);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing segment relocations...");
	processRelocations(st, imp, mrt, relocTable, prog, memory, space, log, monitor);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing information block...");
	processInformationBlock(ib, nrnt, memory, listing);

	processProperties(ib, prog, monitor);

}
 
Example 6
Source File: MzLoader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
		TaskMonitor monitor, MessageLog log) throws IOException, CancelledException {

	FileBytes fileBytes = MemoryBlockUtils.createFileBytes(prog, provider, monitor);
	AddressFactory af = prog.getAddressFactory();
	if (!(af.getDefaultAddressSpace() instanceof SegmentedAddressSpace)) {
		throw new IOException("Selected Language must have a segmented address space.");
	}

	SegmentedAddressSpace space = (SegmentedAddressSpace) af.getDefaultAddressSpace();
	SymbolTable symbolTable = prog.getSymbolTable();
	ProgramContext context = prog.getProgramContext();
	Memory memory = prog.getMemory();

	ContinuesFactory factory = MessageLogContinuesFactory.create(log);
	OldStyleExecutable ose = new OldStyleExecutable(factory, provider);
	DOSHeader dos = ose.getDOSHeader();
	FactoryBundledWithBinaryReader reader = ose.getBinaryReader();

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing segments...");
	processSegments(prog, fileBytes, space, reader, dos, log, monitor);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Adjusting segments...");
	adjustSegmentStarts(prog);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing relocations...");
	doRelocations(prog, reader, dos);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Processing symbols...");
	createSymbols(space, symbolTable, dos);

	if (monitor.isCancelled()) {
		return;
	}
	monitor.setMessage("Setting registers...");

	Symbol entrySymbol =
		SymbolUtilities.getLabelOrFunctionSymbol(prog, ENTRY_NAME, err -> log.error("MZ", err));
	setRegisters(context, entrySymbol, memory.getBlocks(), dos);

}
 
Example 7
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);
	}
}