more changes for filebytes

This commit is contained in:
ghidravore 2019-07-17 18:30:32 -04:00
parent 8576bd66e9
commit a8934fbd95
3 changed files with 100 additions and 118 deletions

View file

@ -21,7 +21,7 @@ import java.util.*;
import generic.continues.ContinuesFactory; import generic.continues.ContinuesFactory;
import generic.continues.RethrowContinuesFactory; import generic.continues.RethrowContinuesFactory;
import ghidra.app.util.MemoryBlockUtil; import ghidra.app.util.MemoryBlockUtils;
import ghidra.app.util.Option; import ghidra.app.util.Option;
import ghidra.app.util.bin.ByteProvider; import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.bin.format.FactoryBundledWithBinaryReader; import ghidra.app.util.bin.format.FactoryBundledWithBinaryReader;
@ -30,6 +30,7 @@ import ghidra.app.util.bin.format.mz.OldStyleExecutable;
import ghidra.app.util.importer.MessageLog; import ghidra.app.util.importer.MessageLog;
import ghidra.app.util.importer.MessageLogContinuesFactory; import ghidra.app.util.importer.MessageLogContinuesFactory;
import ghidra.framework.store.LockException; import ghidra.framework.store.LockException;
import ghidra.program.database.mem.FileBytes;
import ghidra.program.model.address.*; import ghidra.program.model.address.*;
import ghidra.program.model.lang.Register; import ghidra.program.model.lang.Register;
import ghidra.program.model.listing.*; import ghidra.program.model.listing.*;
@ -88,6 +89,7 @@ public class MzLoader extends AbstractLibrarySupportLoader {
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog, public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
TaskMonitor monitor, MessageLog log) throws IOException { TaskMonitor monitor, MessageLog log) throws IOException {
FileBytes fileBytes = MemoryBlockUtils.createFileBytes(prog, provider);
AddressFactory af = prog.getAddressFactory(); AddressFactory af = prog.getAddressFactory();
if (!(af.getDefaultAddressSpace() instanceof SegmentedAddressSpace)) { if (!(af.getDefaultAddressSpace() instanceof SegmentedAddressSpace)) {
throw new IOException("Selected Language must have a segmented address space."); throw new IOException("Selected Language must have a segmented address space.");
@ -99,8 +101,6 @@ public class MzLoader extends AbstractLibrarySupportLoader {
Memory memory = prog.getMemory(); Memory memory = prog.getMemory();
ContinuesFactory factory = MessageLogContinuesFactory.create(log); ContinuesFactory factory = MessageLogContinuesFactory.create(log);
MemoryBlockUtil mbu = new MemoryBlockUtil(prog);
try {
OldStyleExecutable ose = new OldStyleExecutable(factory, provider); OldStyleExecutable ose = new OldStyleExecutable(factory, provider);
DOSHeader dos = ose.getDOSHeader(); DOSHeader dos = ose.getDOSHeader();
FactoryBundledWithBinaryReader reader = ose.getBinaryReader(); FactoryBundledWithBinaryReader reader = ose.getBinaryReader();
@ -109,7 +109,7 @@ public class MzLoader extends AbstractLibrarySupportLoader {
return; return;
} }
monitor.setMessage("Processing segments..."); monitor.setMessage("Processing segments...");
processSegments(mbu, space, reader, dos, log, monitor); processSegments(prog, fileBytes, space, reader, dos, log, monitor);
if (monitor.isCancelled()) { if (monitor.isCancelled()) {
return; return;
@ -134,20 +134,10 @@ public class MzLoader extends AbstractLibrarySupportLoader {
} }
monitor.setMessage("Setting registers..."); monitor.setMessage("Setting registers...");
Symbol entrySymbol = SymbolUtilities.getLabelOrFunctionSymbol(prog, ENTRY_NAME, Symbol entrySymbol =
err -> log.error("MZ", err)); SymbolUtilities.getLabelOrFunctionSymbol(prog, ENTRY_NAME, err -> log.error("MZ", err));
setRegisters(context, entrySymbol, memory.getBlocks(), dos); setRegisters(context, entrySymbol, memory.getBlocks(), dos);
}
finally {
String messages = mbu.getMessages();
if (messages.length() != 0) {
log.appendMsg(messages);
}
mbu.dispose();
mbu = null;
}
} }
private void setRegisters(ProgramContext context, Symbol entry, MemoryBlock[] blocks, private void setRegisters(ProgramContext context, Symbol entry, MemoryBlock[] blocks,
@ -259,7 +249,7 @@ public class MzLoader extends AbstractLibrarySupportLoader {
} }
} }
private void processSegments(MemoryBlockUtil mbu, SegmentedAddressSpace space, private void processSegments(Program program, FileBytes fileBytes, SegmentedAddressSpace space,
FactoryBundledWithBinaryReader reader, DOSHeader dos, MessageLog log, FactoryBundledWithBinaryReader reader, DOSHeader dos, MessageLog log,
TaskMonitor monitor) { TaskMonitor monitor) {
try { try {
@ -328,12 +318,12 @@ public class MzLoader extends AbstractLibrarySupportLoader {
} }
if (numBytes > 0) { if (numBytes > 0) {
bytes = reader.readByteArray(readLoc, numBytes); bytes = reader.readByteArray(readLoc, numBytes);
mbu.createInitializedBlock("Seg_" + i, start, bytes, "", "mz", true, true, true, MemoryBlockUtils.createInitializedBlock(program, false, "Seg_" + i, start,
monitor); fileBytes, readLoc, numBytes, "", "mz", true, true, true, log);
} }
if (numUninitBytes > 0) { if (numUninitBytes > 0) {
mbu.createUninitializedBlock(false, "Seg_" + i + "u", start.add(numBytes), MemoryBlockUtils.createUninitializedBlock(program, false, "Seg_" + i + "u",
numUninitBytes, "", "mz", true, true, false); start.add(numBytes), numUninitBytes, "", "mz", true, true, false, log);
} }
} }

View file

@ -21,7 +21,7 @@ import java.util.Arrays;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
import ghidra.app.util.MemoryBlockUtil; import ghidra.app.util.MemoryBlockUtils;
import ghidra.app.util.importer.MessageLog; import ghidra.app.util.importer.MessageLog;
import ghidra.program.database.mem.SourceInfo; import ghidra.program.database.mem.SourceInfo;
import ghidra.program.model.address.*; import ghidra.program.model.address.*;
@ -56,15 +56,13 @@ class MemoryMapXmlMgr {
String directory) String directory)
throws SAXParseException, FileNotFoundException, CancelledException { throws SAXParseException, FileNotFoundException, CancelledException {
MemoryBlockUtil mbu = new MemoryBlockUtil(program);
try {
XmlElement element = parser.next(); XmlElement element = parser.next();
element = parser.next(); element = parser.next();
while (element.getName().equals("MEMORY_SECTION")) { while (element.getName().equals("MEMORY_SECTION")) {
if (monitor.isCancelled()) { if (monitor.isCancelled()) {
throw new CancelledException(); throw new CancelledException();
} }
processMemoryBlock(element, parser, directory, mbu, monitor); processMemoryBlock(element, parser, directory, program, monitor);
element = parser.next(); element = parser.next();
} }
if (element.isStart() || !element.getName().equals("MEMORY_MAP")) { if (element.isStart() || !element.getName().equals("MEMORY_MAP")) {
@ -72,13 +70,9 @@ class MemoryMapXmlMgr {
null, null, parser.getLineNumber(), parser.getColumnNumber()); null, null, parser.getLineNumber(), parser.getColumnNumber());
} }
} }
finally {
mbu.dispose();
}
}
private void processMemoryBlock(XmlElement memorySectionElement, XmlPullParser parser, private void processMemoryBlock(XmlElement memorySectionElement, XmlPullParser parser,
String directory, MemoryBlockUtil mbu, TaskMonitor monitor) String directory, Program program, TaskMonitor monitor)
throws FileNotFoundException { throws FileNotFoundException {
String name = memorySectionElement.getAttribute("NAME"); String name = memorySectionElement.getAttribute("NAME");
@ -132,8 +126,9 @@ class MemoryMapXmlMgr {
} }
if (overlayName != null) { if (overlayName != null) {
MemoryBlock block = MemoryBlock block =
mbu.createOverlayBlock(overlayName, addr, new ByteArrayInputStream(bytes), MemoryBlockUtils.createInitializedBlock(program, true, overlayName, addr,
bytes.length, comment, null, r, w, x, monitor); new ByteArrayInputStream(bytes),
bytes.length, comment, null, r, w, x, log, monitor);
if (block != null) { if (block != null) {
block.setVolatile(isVolatile); block.setVolatile(isVolatile);
if (!name.equals(overlayName)) { if (!name.equals(overlayName)) {
@ -142,26 +137,38 @@ class MemoryMapXmlMgr {
} }
} }
else { else {
MemoryBlock block = mbu.createInitializedBlock(name, addr, bytes, comment, null,
r, w, x, monitor); MemoryBlock block = MemoryBlockUtils.createInitializedBlock(program, false,
name, addr, new ByteArrayInputStream(bytes), bytes.length, comment, null,
r, w, x, log, monitor);
if (block != null) { if (block != null) {
block.setVolatile(isVolatile); block.setVolatile(isVolatile);
} }
} }
} }
else if (element.getName().equals("BIT_MAPPED") || else if (element.getName().equals("BIT_MAPPED")) {
element.getName().equals("BYTE_MAPPED")) {
Address sourceAddr = factory.getAddress(element.getAttribute("SOURCE_ADDRESS")); Address sourceAddr = factory.getAddress(element.getAttribute("SOURCE_ADDRESS"));
MemoryBlock block = mbu.createMappedBlock(element.getName().equals("BIT_MAPPED"),
name, addr, sourceAddr, length, comment, null, r, w, x); MemoryBlock block = MemoryBlockUtils.createBitMappedBlock(program, overlayName,
addr, sourceAddr, length, comment, comment, r, w, x, log);
if (block != null) {
block.setVolatile(isVolatile);
}
parser.next(); // consume end of Bit_mapped
}
else if (element.getName().equals("BYTE_MAPPED")) {
Address sourceAddr = factory.getAddress(element.getAttribute("SOURCE_ADDRESS"));
MemoryBlock block = MemoryBlockUtils.createByteMappedBlock(program, overlayName,
addr, sourceAddr, length, comment, comment, r, w, x, log);
if (block != null) { if (block != null) {
block.setVolatile(isVolatile); block.setVolatile(isVolatile);
} }
parser.next(); // consume end of Bit_mapped parser.next(); // consume end of Bit_mapped
} }
else { else {
MemoryBlock block = mbu.createUninitializedBlock(overlayName != null, name, addr, MemoryBlock block = MemoryBlockUtils.createUninitializedBlock(program,
length, comment, null, r, w, x); overlayName != null, name, addr, length, comment, null, r, w, x, log);
if (block != null) { if (block != null) {
block.setVolatile(isVolatile); block.setVolatile(isVolatile);
if (overlayName != null && !name.equals(overlayName)) { if (overlayName != null && !name.equals(overlayName)) {

View file

@ -26,8 +26,9 @@
import java.util.Iterator; import java.util.Iterator;
import ghidra.app.script.GhidraScript; import ghidra.app.script.GhidraScript;
import ghidra.app.util.MemoryBlockUtil; import ghidra.app.util.MemoryBlockUtils;
import ghidra.app.util.NamespaceUtils; import ghidra.app.util.NamespaceUtils;
import ghidra.app.util.importer.MessageLog;
import ghidra.program.model.listing.*; import ghidra.program.model.listing.*;
import ghidra.program.model.mem.*; import ghidra.program.model.mem.*;
import ghidra.program.model.symbol.*; import ghidra.program.model.symbol.*;
@ -241,10 +242,9 @@ public class MergeTwoProgramsScript extends GhidraScript {
private void mergeMemory( Program currProgram, Program otherProgram ) throws Exception { private void mergeMemory( Program currProgram, Program otherProgram ) throws Exception {
monitor.setMessage( "Merging memory..." ); monitor.setMessage( "Merging memory..." );
MemoryBlockUtil mbu = new MemoryBlockUtil(currProgram);
try {
Memory otherMemory = otherProgram.getMemory(); Memory otherMemory = otherProgram.getMemory();
MemoryBlock[] otherBlocks = otherMemory.getBlocks(); MemoryBlock[] otherBlocks = otherMemory.getBlocks();
MessageLog log = new MessageLog();
for (MemoryBlock otherBlock : otherBlocks) { for (MemoryBlock otherBlock : otherBlocks) {
if (monitor.isCancelled()) { if (monitor.isCancelled()) {
break; break;
@ -254,33 +254,18 @@ public class MergeTwoProgramsScript extends GhidraScript {
continue; continue;
} }
if (otherBlock.isInitialized()) { if (otherBlock.isInitialized()) {
mbu.createInitializedBlock( otherBlock.getName(), MemoryBlockUtils.createInitializedBlock(currProgram, false, otherBlock.getName(),
otherBlock.getStart(), otherBlock.getStart(), otherBlock.getData(), otherBlock.getSize(),
otherBlock.getData(), otherBlock.getComment(), otherBlock.getSourceName(), otherBlock.isRead(),
otherBlock.getSize(), otherBlock.isWrite(), otherBlock.isExecute(), log, monitor);
otherBlock.getComment(),
otherBlock.getSourceName(),
otherBlock.isRead(),
otherBlock.isWrite(),
otherBlock.isExecute(),
monitor );
} }
else { else {
mbu.createUninitializedBlock( false, MemoryBlockUtils.createUninitializedBlock(currProgram, false, otherBlock.getName(),
otherBlock.getName(), otherBlock.getStart(), otherBlock.getSize(), otherBlock.getComment(),
otherBlock.getStart(), otherBlock.getSourceName(), otherBlock.isRead(), otherBlock.isWrite(),
otherBlock.getSize(), otherBlock.isExecute(), log);
otherBlock.getComment(),
otherBlock.getSourceName(),
otherBlock.isRead(),
otherBlock.isWrite(),
otherBlock.isExecute() );
} }
} }
} }
finally {
mbu.dispose();
}
}
} }